已开启
新增支持模型GLM5 #926
Minds创建于 4月1日
新增支持模型GLM5 #926
已开启
共 9 个文件变更+1138-264
| @@ -1,96 +1,187 @@ | |||
| 1 | -pkill -9 python | 1 | +set -x |
| 2 | -ray stop --force | ||
| 3 | -rm -rf /tmp/ray | ||
| 4 | -export RAY_DEDUP_LOGS=0 | ||
| 5 | -export RAY_DEBUG=0 | ||
| 6 | -export RAY_DEBUG_POST_MORTEM=0 | ||
| 7 | -export HYDRA_FULL_ERROR=1 | ||
| 8 | -export ASCEND_LAUNCH_BLOCKING=0 | ||
| 9 | -export ASCEND_RT_VISIBLE_DEVICES='0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15' | ||
| 10 | -export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 | ||
| 11 | -#TASK_QUEUE_ENABLE,下发优化,图模式设置为1,非图模式设置为2 | ||
| 12 | -export TASK_QUEUE_ENABLE=1 | ||
| 13 | -export HCCL_ASYNC_ERROR_HANDLING=0 | ||
| 14 | -export HCCL_EXEC_TIMEOUT=7200 | ||
| 15 | -export HCCL_CONNECT_TIMEOUT=7200 | ||
| 16 | -export GLOO_CONNECT_TIMEOUT=7200 | ||
| 17 | -export HCCL_IF_BASE_PORT=50000 | ||
| 18 | -export HCCL_HOST_SOCKET_PORT_RANGE="60000-60050" | ||
| 19 | -export HCCL_NPU_SOCKET_PORT_RANGE="61000-61050" | ||
| 20 | -export HCCL_BUFFSIZE=400 | ||
| 21 | -export LD_PRELOAD="/usr/local/lib/libjemalloc.so.2" | ||
| 22 | -export CPU_AFFINITY_CONF=1 | ||
| 23 | -export PYTORCH_NPU_ALLOC_CONF="max_split_size_mb:2048" | ||
| 24 | -export PYTHONUNBUFFERED=1 | ||
| 25 | 2 | ||
| 26 | -## VLLM AND CUSTOM | 3 | +# # 0. download HF checkpoint |
| 27 | -export VLLM_VERSION="0.13.0" | 4 | +# # remove the `quantization_config` in the `config.json` |
| 28 | -# 修改为 VLLM_ASCEND 编译后生成的自定义算子路径 | 5 | +# # set `num_nextn_predict_layers=0` to disable MTP, which is not currently supported |
| 29 | -export ASCEND_CUSTOM_OPP_PATH='/vllm-ascend/vllm_ascend/_cann_ops_custom/vendors/vllm-ascend' | 6 | +# huggingface-cli download deepseek-ai/DeepSeek-V3-0324 |
| 30 | -export LD_LIBRARY_PATH="/vllm-ascend/vllm_ascend/_cann_ops_custom/vendors/vllm-ascend/op_api/lib:$LD_LIBRARY_PATH" | 7 | + |
| 8 | +# no offline dist checkpoint needed, now with mbridge>=0.13.0, we can directly init model from huggingface downloaded fp8 weights | ||
| 9 | +# tested on docker://verlai/verl:app-verl0.5-transformers4.55.4-vllm0.10.0-mcore0.13.0-te2.2 | ||
| 10 | +hf_weights="DeepSeek-V3.2-Exp-bf16" | ||
| 11 | +DIST_CKPT_PATH="" | ||
| 12 | + | ||
| 13 | +export RAY_DEDUP_LOGS="0" | ||
| 31 | export VLLM_ASCEND_ENABLE_NZ=0 | 14 | export VLLM_ASCEND_ENABLE_NZ=0 |
| 32 | 15 | ||
| 33 | -#修改为当前需要跑的用例路径 | 16 | +# 2. run the script |
| 34 | -DEFAULT_SH="./grpo_deepseekv3.2exp_megatron_A3.sh" | ||
| 35 | -echo "Use $DEFAULT_SH" | ||
| 36 | 17 | ||
| 37 | -ulimit -n 32768 | 18 | +train_files=/dapo-math-17k.parquet |
| 38 | -mkdir logs | 19 | +test_files=/dapo-math-17k.parquet |
| 39 | 20 | ||
| 40 | -export NNODES=16 | 21 | +ALL_OFFLOAD=${ALL_OFFLOAD:-True} |
| 41 | -NPUS_PER_NODE=16 | 22 | +COMMON_PARAM_OFFLOAD=${COMMON_PARAM_OFFLOAD:-$ALL_OFFLOAD} |
| 23 | +COMMON_GRAD_OFFLOAD=${COMMON_GRAD_OFFLOAD:-$ALL_OFFLOAD} | ||
| 24 | +COMMON_OPTIMIZER_OFFLOAD=${COMMON_OPTIMIZER_OFFLOAD:-$ALL_OFFLOAD} | ||
| 42 | 25 | ||
| 43 | -#修改为当前节点的通信网卡 | 26 | +ACTOR_PARAM_OFFLOAD=${ACTOR_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} |
| 44 | -export SOCKET_IFNAME="" | 27 | +ACTOR_GRAD_OFFLOAD=${ACTOR_GRAD_OFFLOAD:-$COMMON_GRAD_OFFLOAD} |
| 45 | -export HCCL_SOCKET_IFNAME=$SOCKET_IFNAME | 28 | +ACTOR_OPTIMIZER_OFFLOAD=${ACTOR_OPTIMIZER_OFFLOAD:-$COMMON_OPTIMIZER_OFFLOAD} |
| 46 | -export TP_SOCKET_IFNAME=$SOCKET_IFNAME | 29 | +REF_PARAM_OFFLOAD=${REF_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} |
| 47 | -export GLOO_SOCKET_IFNAME=$SOCKET_IFNAME | 30 | +CRITIC_PARAM_OFFLOAD=${CRITIC_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} |
| 48 | - | 31 | +CRITIC_GRAD_OFFLOAD=${CRITIC_GRAD_OFFLOAD:-$COMMON_GRAD_OFFLOAD} |
| 49 | -#获取当前节点IP | 32 | +CRITIC_OPTIMIZER_OFFLOAD=${CRITIC_OPTIMIZER_OFFLOAD:-$COMMON_OPTIMIZER_OFFLOAD} |
| 50 | -CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') | 33 | +RM_PARAM_OFFLOAD=${RM_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} |
| 51 | -#修改为对应主节点IP | ||
| 52 | -MASTER_ADDR= | ||
| 53 | -# export MASTER_ADDR=$CURRENT_IP # 单机 | ||
| 54 | 34 | ||
| 55 | 35 | ||
| 56 | -if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then | 36 | +first_layer=3 |
| 57 | - # 主节点启动 | 37 | +last_layer=2 |
| 58 | - ray start --head --port 6379 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8265 --resources='{"NPU": '$NPUS_PER_NODE'}' | 38 | +# PP=16,[3, 4×14, 2] |
| 59 | 39 | ||
| 60 | - while true; do | 40 | +NNODES=16 |
| 61 | - ray_status_output=$(ray status) | 41 | +PP=16 |
| 62 | - npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) | 42 | +TP=8 |
| 63 | - npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') | 43 | +EP=16 |
| 64 | - device_count=$((npu_count_int / $NPUS_PER_NODE)) | 44 | +CP=1 |
| 45 | +ETP=1 | ||
| 46 | +INFER_TP=64 | ||
| 47 | +max_num_seqs=128 | ||
| 65 | 48 | ||
| 66 | - # 判断 device_count 是否与 NNODES 相等 | 49 | +experiment_name='dsv3-32nodes' |
| 67 | - if [ "$device_count" -eq "$NNODES" ]; then | 50 | +n_gpus_per_node=16 |
| 68 | - echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." | ||
| 69 | - ray status | ||
| 70 | - bash $DEFAULT_SH | ||
| 71 | - break | ||
| 72 | - else | ||
| 73 | - echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" | ||
| 74 | - sleep 5 | ||
| 75 | - fi | ||
| 76 | - done | ||
| 77 | -else | ||
| 78 | - # 子节点尝试往主节点注册ray直到成功 | ||
| 79 | - while true; do | ||
| 80 | - # 尝试连接 Ray 集群 | ||
| 81 | - ray start --address="$MASTER_ADDR:6379" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP | ||
| 82 | 51 | ||
| 83 | - # 检查连接是否成功 | ||
| 84 | - ray status | ||
| 85 | - if [ $? -eq 0 ]; then | ||
| 86 | - echo "Successfully connected to the Ray cluster!" | ||
| 87 | - break | ||
| 88 | - else | ||
| 89 | - echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." | ||
| 90 | - sleep 5 | ||
| 91 | - fi | ||
| 92 | - done | ||
| 93 | -fi | ||
| 94 | 52 | ||
| 95 | -sleep 600 | 53 | +train_batch_size=128 |
| 54 | +ppo_mini_batch_size=64 | ||
| 55 | +n_resp_per_prompt=4 | ||
| 96 | 56 | ||
| 57 | +balance_batch=False | ||
| 58 | + | ||
| 59 | +max_prompt_length=$((1024 * 16)) | ||
| 60 | +max_response_length=$(( 1024 * 8 )) | ||
| 61 | +total_length=$(($max_prompt_length+$max_response_length)) | ||
| 62 | + | ||
| 63 | +use_dynamic_bsz=False | ||
| 64 | +actor_ppo_max_token_len=$(((max_prompt_length + max_response_length) * 1)) | ||
| 65 | +infer_ppo_max_token_len=$(((max_prompt_length + max_response_length) * 1)) | ||
| 66 | + | ||
| 67 | +use_kl_in_reward=False | ||
| 68 | +kl_coef=0.0 | ||
| 69 | +use_kl_loss=True | ||
| 70 | +kl_loss_coef=0.001 | ||
| 71 | + | ||
| 72 | +clip_ratio_low=0.2 | ||
| 73 | +clip_ratio_high=0.28 | ||
| 74 | + | ||
| 75 | +exp_name="685B-${NNODES}-train-pp${PP}-tp${TP}-ep${EP}-CP${CP}-actor-length${actor_ppo_max_token_len}_final" | ||
| 76 | +CKPTS_DIR=${CKPTS_DIR:-"${exp_name}"} | ||
| 77 | + | ||
| 78 | +python3 -m verl.trainer.main_ppo \ | ||
| 79 | + --config-path=./config --config-name='ppo_megatron_trainer'\ | ||
| 80 | + algorithm.adv_estimator=grpo \ | ||
| 81 | + data.train_files="$train_files" \ | ||
| 82 | + data.val_files="$test_files" \ | ||
| 83 | + data.train_batch_size=$train_batch_size \ | ||
| 84 | + data.max_prompt_length=$max_prompt_length \ | ||
| 85 | + data.max_response_length=$max_response_length \ | ||
| 86 | + data.filter_overlong_prompts=False \ | ||
| 87 | + data.truncation='left' \ | ||
| 88 | + actor_rollout_ref.nccl_timeout=7200 \ | ||
| 89 | + actor_rollout_ref.model.path=$hf_weights \ | ||
| 90 | + actor_rollout_ref.actor.optim.lr=1e-6 \ | ||
| 91 | + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} \ | ||
| 92 | + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 \ | ||
| 93 | + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=1 \ | ||
| 94 | + actor_rollout_ref.actor.use_torch_compile=False \ | ||
| 95 | + actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} \ | ||
| 96 | + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1 \ | ||
| 97 | + actor_rollout_ref.rollout.name=vllm \ | ||
| 98 | + actor_rollout_ref.rollout.gpu_memory_utilization=0.8 \ | ||
| 99 | + actor_rollout_ref.rollout.n=${n_resp_per_prompt} \ | ||
| 100 | + actor_rollout_ref.rollout.temperature=1.0 \ | ||
| 101 | + actor_rollout_ref.rollout.top_p=1.0 \ | ||
| 102 | + actor_rollout_ref.rollout.top_k=-1 \ | ||
| 103 | + actor_rollout_ref.rollout.tensor_model_parallel_size=$INFER_TP \ | ||
| 104 | + actor_rollout_ref.rollout.load_format='dummy' \ | ||
| 105 | + actor_rollout_ref.rollout.max_num_seqs=$max_num_seqs \ | ||
| 106 | + actor_rollout_ref.rollout.calculate_log_probs=True \ | ||
| 107 | + actor_rollout_ref.actor.clip_ratio_low=${clip_ratio_low} \ | ||
| 108 | + actor_rollout_ref.actor.clip_ratio_high=${clip_ratio_high} \ | ||
| 109 | + actor_rollout_ref.actor.clip_ratio_c=10.0 \ | ||
| 110 | + algorithm.use_kl_in_reward=False \ | ||
| 111 | + trainer.logger='["console","tensorboard"]' \ | ||
| 112 | + trainer.project_name='verl_megatron_gsm8k_examples' \ | ||
| 113 | + trainer.experiment_name=$experiment_name \ | ||
| 114 | + trainer.n_gpus_per_node=$n_gpus_per_node \ | ||
| 115 | + trainer.nnodes=$NNODES \ | ||
| 116 | + trainer.save_freq=-1 \ | ||
| 117 | + trainer.test_freq=-1 \ | ||
| 118 | + actor_rollout_ref.model.use_remove_padding=False \ | ||
| 119 | + actor_rollout_ref.actor.megatron.use_remove_padding=False \ | ||
| 120 | + actor_rollout_ref.rollout.enforce_eager=False \ | ||
| 121 | + actor_rollout_ref.model.use_fused_kernels=False \ | ||
| 122 | + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} \ | ||
| 123 | + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} \ | ||
| 124 | + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} \ | ||
| 125 | + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} \ | ||
| 126 | + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} \ | ||
| 127 | + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} \ | ||
| 128 | + actor_rollout_ref.actor.megatron.override_transformer_config.attention_backend='fused' \ | ||
| 129 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn=True \ | ||
| 130 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_distributed_optimizer=True \ | ||
| 131 | + actor_rollout_ref.actor.strategy=megatron \ | ||
| 132 | + actor_rollout_ref.actor.megatron.pipeline_model_parallel_size=$PP \ | ||
| 133 | + actor_rollout_ref.ref.megatron.pipeline_model_parallel_size=$PP \ | ||
| 134 | + actor_rollout_ref.actor.megatron.tensor_model_parallel_size=$TP \ | ||
| 135 | + actor_rollout_ref.ref.megatron.tensor_model_parallel_size=$TP \ | ||
| 136 | + actor_rollout_ref.actor.megatron.expert_model_parallel_size=$EP \ | ||
| 137 | + actor_rollout_ref.ref.megatron.expert_model_parallel_size=$EP \ | ||
| 138 | + actor_rollout_ref.actor.megatron.expert_tensor_parallel_size=$ETP \ | ||
| 139 | + actor_rollout_ref.ref.megatron.expert_tensor_parallel_size=$ETP \ | ||
| 140 | + actor_rollout_ref.actor.megatron.context_parallel_size=$CP \ | ||
| 141 | + +actor_rollout_ref.actor.megatron.override_transformer_config.sequence_parallel=True \ | ||
| 142 | + actor_rollout_ref.actor.megatron.param_offload=True \ | ||
| 143 | + actor_rollout_ref.actor.megatron.optimizer_offload=True \ | ||
| 144 | + actor_rollout_ref.actor.megatron.grad_offload=True \ | ||
| 145 | + actor_rollout_ref.ref.megatron.param_offload=True \ | ||
| 146 | + +actor_rollout_ref.actor.optim.override_optimizer_config.optimizer_offload_fraction=1 \ | ||
| 147 | + +actor_rollout_ref.actor.optim.override_optimizer_config.use_precision_aware_optimizer=True \ | ||
| 148 | + +actor_rollout_ref.actor.optim.override_optimizer_config.optimizer_cpu_offload=True \ | ||
| 149 | + +actor_rollout_ref.actor.megatron.override_transformer_config.gradient_accumulation_fusion=False \ | ||
| 150 | + +actor_rollout_ref.actor.megatron.override_transformer_config.masked_softmax_fusion=False \ | ||
| 151 | + +actor_rollout_ref.actor.megatron.override_transformer_config.bias_dropout_fusion=False \ | ||
| 152 | + +actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_first_pipeline_stage=$first_layer \ | ||
| 153 | + +actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_last_pipeline_stage=$last_layer \ | ||
| 154 | + +actor_rollout_ref.actor.megatron.override_transformer_config.attention_softmax_in_fp32=True \ | ||
| 155 | + actor_rollout_ref.rollout.max_num_batched_tokens=$total_length \ | ||
| 156 | + actor_rollout_ref.actor.megatron.use_mbridge=True \ | ||
| 157 | + actor_rollout_ref.actor.megatron.vanilla_mbridge=False \ | ||
| 158 | + actor_rollout_ref.actor.megatron.use_dist_checkpointing=False \ | ||
| 159 | + actor_rollout_ref.ref.megatron.use_dist_checkpointing=False \ | ||
| 160 | + actor_rollout_ref.actor.megatron.dist_checkpointing_path=$DIST_CKPT_PATH \ | ||
| 161 | + actor_rollout_ref.ref.megatron.dist_checkpointing_path=$DIST_CKPT_PATH \ | ||
| 162 | + trainer.default_local_dir=$CKPTS_DIR \ | ||
| 163 | + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method=uniform \ | ||
| 164 | + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity=full \ | ||
| 165 | + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers=1 \ | ||
| 166 | + +actor_rollout_ref.actor.megatron.override_transformer_config.normalization=RMSNorm \ | ||
| 167 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rmsnorm=True \ | ||
| 168 | + +actor_rollout_ref.actor.megatron.override_transformer_config.swiglu=True \ | ||
| 169 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu=True \ | ||
| 170 | + trainer.rollout_data_dir="/rollout_data_dir/$(date +%Y%m%d_%H%M%S)" \ | ||
| 171 | + +actor_rollout_ref.actor.megatron.override_transformer_config.experimental_attention_variant="dsa" \ | ||
| 172 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_dsa_absorb=True \ | ||
| 173 | + +actor_rollout_ref.actor.megatron.override_transformer_config.dsa_indexer_use_sparse_loss=True \ | ||
| 174 | + +actor_rollout_ref.actor.megatron.override_transformer_config.dsa_indexer_loss_coeff=0.001 \ | ||
| 175 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_lightning_indexer=True \ | ||
| 176 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_sparse_flash_attention=True \ | ||
| 177 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_lightning_indexer_kl_loss=True \ | ||
| 178 | + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_router_enable_expert_bias=True \ | ||
| 179 | + +actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_size=${CP} \ | ||
| 180 | + +actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_algo=kvallgather_cp_algo \ | ||
| 181 | + +actor_rollout_ref.actor.megatron.override_transformer_config.reset_position_ids=False \ | ||
| 182 | + +actor_rollout_ref.actor.megatron.override_transformer_config.use_ascend_mc2=False \ | ||
| 183 | + trainer.resume_mode="disable" \ | ||
| 184 | + trainer.balance_batch=${balance_batch} \ | ||
| 185 | + trainer.device=npu \ | ||
| 186 | + trainer.val_before_train=False \ | ||
| 187 | + trainer.total_epochs=100 2>&1 | tee logs/$(date +%Y%m%d_%H%M%S).log | ||
| @@ -1,188 +1,96 @@ | |||
| 1 | -set -x | 1 | +pkill -9 python |
| 2 | +ray stop --force | ||
| 3 | +rm -rf /tmp/ray | ||
| 4 | +export RAY_DEDUP_LOGS=0 | ||
| 5 | +export RAY_DEBUG=0 | ||
| 6 | +export RAY_DEBUG_POST_MORTEM=0 | ||
| 7 | +export HYDRA_FULL_ERROR=1 | ||
| 8 | +export ASCEND_LAUNCH_BLOCKING=0 | ||
| 9 | +export ASCEND_RT_VISIBLE_DEVICES='0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15' | ||
| 10 | +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 | ||
| 11 | +#TASK_QUEUE_ENABLE,下发优化,图模式设置为1,非图模式设置为2 | ||
| 12 | +export TASK_QUEUE_ENABLE=1 | ||
| 13 | +export HCCL_ASYNC_ERROR_HANDLING=0 | ||
| 14 | +export HCCL_EXEC_TIMEOUT=7200 | ||
| 15 | +export HCCL_CONNECT_TIMEOUT=7200 | ||
| 16 | +export GLOO_CONNECT_TIMEOUT=7200 | ||
| 17 | +export HCCL_IF_BASE_PORT=50000 | ||
| 18 | +export HCCL_HOST_SOCKET_PORT_RANGE="60000-60050" | ||
| 19 | +export HCCL_NPU_SOCKET_PORT_RANGE="61000-61050" | ||
| 20 | +export HCCL_BUFFSIZE=400 | ||
| 21 | +export LD_PRELOAD="/usr/local/lib/libjemalloc.so.2" | ||
| 22 | +export CPU_AFFINITY_CONF=1 | ||
| 23 | +export PYTORCH_NPU_ALLOC_CONF="max_split_size_mb:2048" | ||
| 24 | +export PYTHONUNBUFFERED=1 | ||
| 2 | 25 | ||
| 3 | -# # 0. download HF checkpoint | 26 | +## VLLM AND CUSTOM |
| 4 | -# # remove the `quantization_config` in the `config.json` | 27 | +export VLLM_VERSION="0.13.0" |
| 5 | -# # set `num_nextn_predict_layers=0` to disable MTP, which is not currently supported | 28 | +# 修改为 VLLM_ASCEND 编译后生成的自定义算子路径 |
| 6 | -# huggingface-cli download deepseek-ai/DeepSeek-V3-0324 | 29 | +export ASCEND_CUSTOM_OPP_PATH='/vllm-ascend/vllm_ascend/_cann_ops_custom/vendors/vllm-ascend' |
| 7 | - | 30 | +export LD_LIBRARY_PATH="/vllm-ascend/vllm_ascend/_cann_ops_custom/vendors/vllm-ascend/op_api/lib:$LD_LIBRARY_PATH" |
| 8 | -# no offline dist checkpoint needed, now with mbridge>=0.13.0, we can directly init model from huggingface downloaded fp8 weights | ||
| 9 | -# tested on docker://verlai/verl:app-verl0.5-transformers4.55.4-vllm0.10.0-mcore0.13.0-te2.2 | ||
| 10 | -hf_weights="DeepSeek-V3.2-Exp-bf16" | ||
| 11 | -DIST_CKPT_PATH="" | ||
| 12 | - | ||
| 13 | -export RAY_DEDUP_LOGS="0" | ||
| 14 | export VLLM_ASCEND_ENABLE_NZ=0 | 31 | export VLLM_ASCEND_ENABLE_NZ=0 |
| 15 | 32 | ||
| 16 | -# 2. run the script | 33 | +#修改为当前需要跑的用例路径 |
| 34 | +DEFAULT_SH="./test_grpo_deepseekv3.2exp_megatron_A3.sh" | ||
| 35 | +echo "Use $DEFAULT_SH" | ||
| 17 | 36 | ||
| 18 | -train_files=/dapo-math-17k.parquet | 37 | +ulimit -n 32768 |
| 19 | -test_files=/dapo-math-17k.parquet | 38 | +mkdir logs |
| 20 | 39 | ||
| 21 | -ALL_OFFLOAD=${ALL_OFFLOAD:-True} | 40 | +export NNODES=16 |
| 22 | -COMMON_PARAM_OFFLOAD=${COMMON_PARAM_OFFLOAD:-$ALL_OFFLOAD} | 41 | +NPUS_PER_NODE=16 |
| 23 | -COMMON_GRAD_OFFLOAD=${COMMON_GRAD_OFFLOAD:-$ALL_OFFLOAD} | ||
| 24 | -COMMON_OPTIMIZER_OFFLOAD=${COMMON_OPTIMIZER_OFFLOAD:-$ALL_OFFLOAD} | ||
| 25 | 42 | ||
| 26 | -ACTOR_PARAM_OFFLOAD=${ACTOR_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} | 43 | +#修改为当前节点的通信网卡 |
| 27 | -ACTOR_GRAD_OFFLOAD=${ACTOR_GRAD_OFFLOAD:-$COMMON_GRAD_OFFLOAD} | 44 | +export SOCKET_IFNAME="" |
| 28 | -ACTOR_OPTIMIZER_OFFLOAD=${ACTOR_OPTIMIZER_OFFLOAD:-$COMMON_OPTIMIZER_OFFLOAD} | 45 | +export HCCL_SOCKET_IFNAME=$SOCKET_IFNAME |
| 29 | -REF_PARAM_OFFLOAD=${REF_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} | 46 | +export TP_SOCKET_IFNAME=$SOCKET_IFNAME |
| 30 | -CRITIC_PARAM_OFFLOAD=${CRITIC_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} | 47 | +export GLOO_SOCKET_IFNAME=$SOCKET_IFNAME |
| 31 | -CRITIC_GRAD_OFFLOAD=${CRITIC_GRAD_OFFLOAD:-$COMMON_GRAD_OFFLOAD} | 48 | + |
| 32 | -CRITIC_OPTIMIZER_OFFLOAD=${CRITIC_OPTIMIZER_OFFLOAD:-$COMMON_OPTIMIZER_OFFLOAD} | 49 | +#获取当前节点IP |
| 33 | -RM_PARAM_OFFLOAD=${RM_PARAM_OFFLOAD:-$COMMON_PARAM_OFFLOAD} | 50 | +CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') |
| 51 | +#修改为对应主节点IP | ||
| 52 | +MASTER_ADDR= | ||
| 53 | +# export MASTER_ADDR=$CURRENT_IP # 单机 | ||
| 34 | 54 | ||
| 35 | 55 | ||
| 56 | +if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then | ||
| 57 | + # 主节点启动 | ||
| 58 | + ray start --head --port 6379 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8265 --resources='{"NPU": '$NPUS_PER_NODE'}' | ||
| 36 | 59 | ||
| 37 | -first_layer=3 | 60 | + while true; do |
| 38 | -last_layer=2 | 61 | + ray_status_output=$(ray status) |
| 39 | -# PP=16,[3, 4×14, 2] | 62 | + npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) |
| 63 | + npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') | ||
| 64 | + device_count=$((npu_count_int / $NPUS_PER_NODE)) | ||
| 40 | 65 | ||
| 41 | -NNODES=16 | 66 | + # 判断 device_count 是否与 NNODES 相等 |
| 42 | -PP=16 | 67 | + if [ "$device_count" -eq "$NNODES" ]; then |
| 43 | -TP=8 | 68 | + echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." |
| 44 | -EP=16 | 69 | + ray status |
| 45 | -CP=1 | 70 | + bash $DEFAULT_SH |
| 46 | -ETP=1 | 71 | + break |
| 47 | -INFER_TP=64 | 72 | + else |
| 48 | -max_num_seqs=128 | 73 | + echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" |
| 74 | + sleep 5 | ||
| 75 | + fi | ||
| 76 | + done | ||
| 77 | +else | ||
| 78 | + # 子节点尝试往主节点注册ray直到成功 | ||
| 79 | + while true; do | ||
| 80 | + # 尝试连接 Ray 集群 | ||
| 81 | + ray start --address="$MASTER_ADDR:6379" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP | ||
| 49 | 82 | ||
| 50 | -experiment_name='dsv3-32nodes' | 83 | + # 检查连接是否成功 |
| 51 | -n_gpus_per_node=16 | 84 | + ray status |
| 85 | + if [ $? -eq 0 ]; then | ||
| 86 | + echo "Successfully connected to the Ray cluster!" | ||
| 87 | + break | ||
| 88 | + else | ||
| 89 | + echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." | ||
| 90 | + sleep 5 | ||
| 91 | + fi | ||
| 92 | + done | ||
| 93 | +fi | ||
| 52 | 94 | ||
| 95 | +sleep 600 | ||
| 53 | 96 | ||
| 54 | -train_batch_size=128 | ||
| 55 | -ppo_mini_batch_size=64 | ||
| 56 | -n_resp_per_prompt=4 | ||
| 57 | - | ||
| 58 | -balance_batch=False | ||
| 59 | - | ||
| 60 | -max_prompt_length=$((1024 * 16)) | ||
| 61 | -max_response_length=$(( 1024 * 8 )) | ||
| 62 | -total_length=$(($max_prompt_length+$max_response_length)) | ||
| 63 | - | ||
| 64 | -use_dynamic_bsz=False | ||
| 65 | -actor_ppo_max_token_len=$(((max_prompt_length + max_response_length) * 1)) | ||
| 66 | -infer_ppo_max_token_len=$(((max_prompt_length + max_response_length) * 1)) | ||
| 67 | - | ||
| 68 | -use_kl_in_reward=False | ||
| 69 | -kl_coef=0.0 | ||
| 70 | -use_kl_loss=True | ||
| 71 | -kl_loss_coef=0.001 | ||
| 72 | - | ||
| 73 | -clip_ratio_low=0.2 | ||
| 74 | -clip_ratio_high=0.28 | ||
| 75 | - | ||
| 76 | -exp_name="685B-${NNODES}-train-pp${PP}-tp${TP}-ep${EP}-CP${CP}-actor-length${actor_ppo_max_token_len}_final" | ||
| 77 | -CKPTS_DIR=${CKPTS_DIR:-"${exp_name}"} | ||
| 78 | - | ||
| 79 | -python3 -m verl.trainer.main_ppo \ | ||
| 80 | - --config-path=./config --config-name='ppo_megatron_trainer'\ | ||
| 81 | - algorithm.adv_estimator=grpo \ | ||
| 82 | - data.train_files="$train_files" \ | ||
| 83 | - data.val_files="$test_files" \ | ||
| 84 | - data.train_batch_size=$train_batch_size \ | ||
| 85 | - data.max_prompt_length=$max_prompt_length \ | ||
| 86 | - data.max_response_length=$max_response_length \ | ||
| 87 | - data.filter_overlong_prompts=False \ | ||
| 88 | - data.truncation='left' \ | ||
| 89 | - actor_rollout_ref.nccl_timeout=7200 \ | ||
| 90 | - actor_rollout_ref.model.path=$hf_weights \ | ||
| 91 | - actor_rollout_ref.actor.optim.lr=1e-6 \ | ||
| 92 | - actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} \ | ||
| 93 | - actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 \ | ||
| 94 | - actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=1 \ | ||
| 95 | - actor_rollout_ref.actor.use_torch_compile=False \ | ||
| 96 | - actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} \ | ||
| 97 | - actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1 \ | ||
| 98 | - actor_rollout_ref.rollout.name=vllm \ | ||
| 99 | - actor_rollout_ref.rollout.gpu_memory_utilization=0.8 \ | ||
| 100 | - actor_rollout_ref.rollout.n=${n_resp_per_prompt} \ | ||
| 101 | - actor_rollout_ref.rollout.temperature=1.0 \ | ||
| 102 | - actor_rollout_ref.rollout.top_p=1.0 \ | ||
| 103 | - actor_rollout_ref.rollout.top_k=-1 \ | ||
| 104 | - actor_rollout_ref.rollout.tensor_model_parallel_size=$INFER_TP \ | ||
| 105 | - actor_rollout_ref.rollout.load_format='dummy' \ | ||
| 106 | - actor_rollout_ref.rollout.max_num_seqs=$max_num_seqs \ | ||
| 107 | - actor_rollout_ref.rollout.calculate_log_probs=True \ | ||
| 108 | - actor_rollout_ref.actor.clip_ratio_low=${clip_ratio_low} \ | ||
| 109 | - actor_rollout_ref.actor.clip_ratio_high=${clip_ratio_high} \ | ||
| 110 | - actor_rollout_ref.actor.clip_ratio_c=10.0 \ | ||
| 111 | - algorithm.use_kl_in_reward=False \ | ||
| 112 | - trainer.logger='["console","tensorboard"]' \ | ||
| 113 | - trainer.project_name='verl_megatron_gsm8k_examples' \ | ||
| 114 | - trainer.experiment_name=$experiment_name \ | ||
| 115 | - trainer.n_gpus_per_node=$n_gpus_per_node \ | ||
| 116 | - trainer.nnodes=$NNODES \ | ||
| 117 | - trainer.save_freq=-1 \ | ||
| 118 | - trainer.test_freq=-1 \ | ||
| 119 | - actor_rollout_ref.model.use_remove_padding=False \ | ||
| 120 | - actor_rollout_ref.actor.megatron.use_remove_padding=False \ | ||
| 121 | - actor_rollout_ref.rollout.enforce_eager=False \ | ||
| 122 | - actor_rollout_ref.model.use_fused_kernels=False \ | ||
| 123 | - actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} \ | ||
| 124 | - actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} \ | ||
| 125 | - actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} \ | ||
| 126 | - actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} \ | ||
| 127 | - actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} \ | ||
| 128 | - actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} \ | ||
| 129 | - actor_rollout_ref.actor.megatron.override_transformer_config.attention_backend='fused' \ | ||
| 130 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn=True \ | ||
| 131 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_distributed_optimizer=True \ | ||
| 132 | - actor_rollout_ref.actor.strategy=megatron \ | ||
| 133 | - actor_rollout_ref.actor.megatron.pipeline_model_parallel_size=$PP \ | ||
| 134 | - actor_rollout_ref.ref.megatron.pipeline_model_parallel_size=$PP \ | ||
| 135 | - actor_rollout_ref.actor.megatron.tensor_model_parallel_size=$TP \ | ||
| 136 | - actor_rollout_ref.ref.megatron.tensor_model_parallel_size=$TP \ | ||
| 137 | - actor_rollout_ref.actor.megatron.expert_model_parallel_size=$EP \ | ||
| 138 | - actor_rollout_ref.ref.megatron.expert_model_parallel_size=$EP \ | ||
| 139 | - actor_rollout_ref.actor.megatron.expert_tensor_parallel_size=$ETP \ | ||
| 140 | - actor_rollout_ref.ref.megatron.expert_tensor_parallel_size=$ETP \ | ||
| 141 | - actor_rollout_ref.actor.megatron.context_parallel_size=$CP \ | ||
| 142 | - +actor_rollout_ref.actor.megatron.override_transformer_config.sequence_parallel=True \ | ||
| 143 | - actor_rollout_ref.actor.megatron.param_offload=True \ | ||
| 144 | - actor_rollout_ref.actor.megatron.optimizer_offload=True \ | ||
| 145 | - actor_rollout_ref.actor.megatron.grad_offload=True \ | ||
| 146 | - actor_rollout_ref.ref.megatron.param_offload=True \ | ||
| 147 | - +actor_rollout_ref.actor.optim.override_optimizer_config.optimizer_offload_fraction=1 \ | ||
| 148 | - +actor_rollout_ref.actor.optim.override_optimizer_config.use_precision_aware_optimizer=True \ | ||
| 149 | - +actor_rollout_ref.actor.optim.override_optimizer_config.optimizer_cpu_offload=True \ | ||
| 150 | - +actor_rollout_ref.actor.megatron.override_transformer_config.gradient_accumulation_fusion=False \ | ||
| 151 | - +actor_rollout_ref.actor.megatron.override_transformer_config.masked_softmax_fusion=False \ | ||
| 152 | - +actor_rollout_ref.actor.megatron.override_transformer_config.bias_dropout_fusion=False \ | ||
| 153 | - +actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_first_pipeline_stage=$first_layer \ | ||
| 154 | - +actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_last_pipeline_stage=$last_layer \ | ||
| 155 | - +actor_rollout_ref.actor.megatron.override_transformer_config.attention_softmax_in_fp32=True \ | ||
| 156 | - actor_rollout_ref.rollout.max_num_batched_tokens=$total_length \ | ||
| 157 | - actor_rollout_ref.actor.megatron.use_mbridge=True \ | ||
| 158 | - actor_rollout_ref.actor.megatron.vanilla_mbridge=False \ | ||
| 159 | - actor_rollout_ref.actor.megatron.use_dist_checkpointing=False \ | ||
| 160 | - actor_rollout_ref.ref.megatron.use_dist_checkpointing=False \ | ||
| 161 | - actor_rollout_ref.actor.megatron.dist_checkpointing_path=$DIST_CKPT_PATH \ | ||
| 162 | - actor_rollout_ref.ref.megatron.dist_checkpointing_path=$DIST_CKPT_PATH \ | ||
| 163 | - trainer.default_local_dir=$CKPTS_DIR \ | ||
| 164 | - +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method=uniform \ | ||
| 165 | - +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity=full \ | ||
| 166 | - +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers=1 \ | ||
| 167 | - +actor_rollout_ref.actor.megatron.override_transformer_config.normalization=RMSNorm \ | ||
| 168 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rmsnorm=True \ | ||
| 169 | - +actor_rollout_ref.actor.megatron.override_transformer_config.swiglu=True \ | ||
| 170 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu=True \ | ||
| 171 | - trainer.rollout_data_dir="/rollout_data_dir/$(date +%Y%m%d_%H%M%S)" \ | ||
| 172 | - +actor_rollout_ref.actor.megatron.override_transformer_config.experimental_attention_variant="dsa" \ | ||
| 173 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_dsa_absorb=True \ | ||
| 174 | - +actor_rollout_ref.actor.megatron.override_transformer_config.dsa_indexer_use_sparse_loss=True \ | ||
| 175 | - +actor_rollout_ref.actor.megatron.override_transformer_config.dsa_indexer_loss_coeff=0.001 \ | ||
| 176 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_lightning_indexer=True \ | ||
| 177 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_sparse_flash_attention=True \ | ||
| 178 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_lightning_indexer_kl_loss=True \ | ||
| 179 | - +actor_rollout_ref.actor.megatron.override_transformer_config.moe_router_enable_expert_bias=True \ | ||
| 180 | - +actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_size=${CP} \ | ||
| 181 | - +actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_algo=kvallgather_cp_algo \ | ||
| 182 | - +actor_rollout_ref.actor.megatron.override_transformer_config.reset_position_ids=False \ | ||
| 183 | - +actor_rollout_ref.actor.megatron.override_transformer_config.use_ascend_mc2=False \ | ||
| 184 | - trainer.resume_mode="disable" \ | ||
| 185 | - trainer.balance_batch=${balance_batch} \ | ||
| 186 | - trainer.device=npu \ | ||
| 187 | - trainer.val_before_train=False \ | ||
| 188 | - trainer.total_epochs=100 2>&1 | tee logs/$(date +%Y%m%d_%H%M%S).log | ||
| @@ -0,0 +1,92 @@ | |||
| 1 | +pkill -9 python | ||
| 2 | +ray stop --force | ||
| 3 | +rm -rf /tmp/ray | ||
| 4 | +export RAY_DEDUP_LOGS=0 | ||
| 5 | +export RAY_DEBUG=0 | ||
| 6 | +export RAY_DEBUG_POST_MORTEM=0 | ||
| 7 | +export HYDRA_FULL_ERROR=1 | ||
| 8 | +export ASCEND_LAUNCH_BLOCKING=0 | ||
| 9 | +export ASCEND_RT_VISIBLE_DEVICES='0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15' | ||
| 10 | +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 | ||
| 11 | +#TASK_QUEUE_ENABLE,下发优化,图模式设置为1,非图模式设置为2 | ||
| 12 | +export TASK_QUEUE_ENABLE=1 | ||
| 13 | +export HCCL_ASYNC_ERROR_HANDLING=0 | ||
| 14 | +export HCCL_EXEC_TIMEOUT=7200 | ||
| 15 | +export HCCL_CONNECT_TIMEOUT=7200 | ||
| 16 | +export GLOO_CONNECT_TIMEOUT=7200 | ||
| 17 | +export HCCL_IF_BASE_PORT=50000 | ||
| 18 | +export HCCL_HOST_SOCKET_PORT_RANGE="60000-60050" | ||
| 19 | +export HCCL_NPU_SOCKET_PORT_RANGE="61000-61050" | ||
| 20 | +export HCCL_BUFFSIZE=400 | ||
| 21 | +export LD_PRELOAD="/usr/local/lib/libjemalloc.so.2" | ||
| 22 | +export CPU_AFFINITY_CONF=1 | ||
| 23 | +export PYTORCH_NPU_ALLOC_CONF="max_split_size_mb:2048" | ||
| 24 | +export PYTHONUNBUFFERED=1 | ||
| 25 | + | ||
| 26 | +## VLLM AND CUSTOM | ||
| 27 | +export VLLM_VERSION="0.17.0" | ||
| 28 | +export VLLM_ASCEND_ENABLE_NZ=0 | ||
| 29 | + | ||
| 30 | +#修改为当前需要跑的用例路径 | ||
| 31 | +DEFAULT_SH="./test_grpo_glm5_A3.sh" | ||
| 32 | +echo "Use $DEFAULT_SH" | ||
| 33 | +ulimit -n 32768 | ||
| 34 | +mkdir logs | ||
| 35 | + | ||
| 36 | +export NNODES=32 | ||
| 37 | +NPUS_PER_NODE=16 | ||
| 38 | + | ||
| 39 | +#修改为当前节点的通信网卡 | ||
| 40 | +export SOCKET_IFNAME="" | ||
| 41 | +export HCCL_SOCKET_IFNAME=$SOCKET_IFNAME | ||
| 42 | +export TP_SOCKET_IFNAME=$SOCKET_IFNAME | ||
| 43 | +export GLOO_SOCKET_IFNAME=$SOCKET_IFNAME | ||
| 44 | + | ||
| 45 | +#获取当前节点IP | ||
| 46 | +CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') | ||
| 47 | +#修改为对应主节点IP | ||
| 48 | +MASTER_ADDR= | ||
| 49 | +# export MASTER_ADDR=$CURRENT_IP # 单机 | ||
| 50 | + | ||
| 51 | + | ||
| 52 | +if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then | ||
| 53 | + # 主节点启动 | ||
| 54 | + ray start --head --port 6379 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8265 --resources='{"NPU": '$NPUS_PER_NODE'}' | ||
| 55 | + | ||
| 56 | + while true; do | ||
| 57 | + ray_status_output=$(ray status) | ||
| 58 | + npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) | ||
| 59 | + npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') | ||
| 60 | + device_count=$((npu_count_int / $NPUS_PER_NODE)) | ||
| 61 | + | ||
| 62 | + # 判断 device_count 是否与 NNODES 相等 | ||
| 63 | + if [ "$device_count" -eq "$NNODES" ]; then | ||
| 64 | + echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." | ||
| 65 | + ray status | ||
| 66 | + bash $DEFAULT_SH | ||
| 67 | + break | ||
| 68 | + else | ||
| 69 | + echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" | ||
| 70 | + sleep 5 | ||
| 71 | + fi | ||
| 72 | + done | ||
| 73 | +else | ||
| 74 | + # 子节点尝试往主节点注册ray直到成功 | ||
| 75 | + while true; do | ||
| 76 | + # 尝试连接 Ray 集群 | ||
| 77 | + ray start --address="$MASTER_ADDR:6379" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP | ||
| 78 | + | ||
| 79 | + # 检查连接是否成功 | ||
| 80 | + ray status | ||
| 81 | + if [ $? -eq 0 ]; then | ||
| 82 | + echo "Successfully connected to the Ray cluster!" | ||
| 83 | + break | ||
| 84 | + else | ||
| 85 | + echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." | ||
| 86 | + sleep 5 | ||
| 87 | + fi | ||
| 88 | + done | ||
| 89 | +fi | ||
| 90 | + | ||
| 91 | +sleep 600 | ||
| 92 | + | ||
| @@ -0,0 +1,125 @@ | |||
| 1 | +# 模型简介 | ||
| 2 | +GLM5是智谱AI与清华大学联合发布的下一代基座模型,定位为从"Vibe Coding"转变成"Agentic Engineering",精准指出了当下正在发生的范式转移。过去,大模型的主要用法是Vibe Coding:人类写prompt,AI生成代码片段,本质上仍是"辅助工具"。而GLM-5瞄准的是Agentic Engineering——AI自主规划、执行、自我修正,跨越数十个步骤完成端到端的软件工程任务。这不是"写更多代码",而是"理解工程目标、规划实施路径、处理异常、持续优化"的完整闭环。 | ||
| 3 | + | ||
| 4 | +GLM-5 全程在华为昇腾芯片上基于 MindSpore 框架训练,实现从美国制程硬件的完全独立,既是技术突破,也是自主可控 AI 基础设施的重要里程碑。 | ||
| 5 | + | ||
| 6 | +# 环境配套 | ||
| 7 | + | ||
| 8 | +| **组件** | **配套版本** | **备注** | | ||
| 9 | +| ----------------- | -------------------- | ------------------------------------ | | ||
| 10 | +| python | 3.11 | | | ||
| 11 | +| pytorch | 2.9.0 | | | ||
| 12 | +| vllm | v0.17.0 | commit b31e932 | | ||
| 13 | +| vllm-ascend | v0.17.0rc1 | commit e20f0b1 | | ||
| 14 | +| verl | release/v0.7.1 | commit a35d251 | | ||
| 15 | +| transformers | v5.4.0 | commit 276f140 | | ||
| 16 | +| Megatron | main | commit 1d462bd | | ||
| 17 | +| Megatron-Bridge | main | commit 7cabf71 | | ||
| 18 | +| MindSpeed | dev分支 | commit 07056df5 | | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +# 环境安装 | ||
| 22 | + | ||
| 23 | +## 安装transformers | ||
| 24 | + | ||
| 25 | +```bash | ||
| 26 | +git clone https://github.com/huggingface/transformers.git -b v5.4.0 | ||
| 27 | +cd transformers | ||
| 28 | +pip install -e . | ||
| 29 | +cd .. | ||
| 30 | +``` | ||
| 31 | + | ||
| 32 | +## 安装vllm | ||
| 33 | + | ||
| 34 | +```bash | ||
| 35 | +git clone https://github.com/vllm-project/vllm.git -b v0.17.0 | ||
| 36 | +cd vllm | ||
| 37 | +pip3 install -r requirements/common.txt | ||
| 38 | +pip3 install -r requirements/build.txt | ||
| 39 | +pip install torch==2.9.0 | ||
| 40 | +pip install torch_npu==2.9.0 | ||
| 41 | +VLLM_TARGET_DEVICE=empty pip install -v -e . | ||
| 42 | +cd .. | ||
| 43 | +``` | ||
| 44 | + | ||
| 45 | +## 安装vllm_ascend | ||
| 46 | + | ||
| 47 | +```bash | ||
| 48 | +git clone https://github.com/vllm-project/vllm-ascend.git -b v0.17.0rc1 | ||
| 49 | +cd vllm-ascend | ||
| 50 | +git checkout e20f0b1 | ||
| 51 | +pip install -r requirements-dev.txt | ||
| 52 | +pip install -v -e . | ||
| 53 | +cd .. | ||
| 54 | +``` | ||
| 55 | + | ||
| 56 | +## 安装verl | ||
| 57 | + | ||
| 58 | +```bash | ||
| 59 | +git clone https://github.com/volcengine/verl.git -b release/v0.7.1 | ||
| 60 | +cd verl | ||
| 61 | +git checkout a35d251 | ||
| 62 | +pip install -r requirements-npu.txt | ||
| 63 | +pip install -v -e . | ||
| 64 | +cd .. | ||
| 65 | +``` | ||
| 66 | + | ||
| 67 | +## 安装MindSpeed | ||
| 68 | + | ||
| 69 | +```bash | ||
| 70 | +git clone https://gitcode.com/Ascend/MindSpeed.git -b dev | ||
| 71 | +cd MindSpeed | ||
| 72 | +git checkout 07056df5 | ||
| 73 | +pip install -e . | ||
| 74 | +cd .. | ||
| 75 | +``` | ||
| 76 | + | ||
| 77 | +## 安装MindSpeedRL-patch | ||
| 78 | + | ||
| 79 | +```bash | ||
| 80 | +git clone https://gitcode.com/Ascend/MindSpeed-RL.git | ||
| 81 | +cd vllm-ascend && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/vllm_ascend/e20f0b1/sfa_v1.patch && cd .. | ||
| 82 | +cd verl && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/verl/a35d251/ray_trainer.patch && cd .. | ||
| 83 | +cd MindSpeed && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/mindspeed/07056df535/dsa.patch && cd .. | ||
| 84 | +``` | ||
| 85 | + | ||
| 86 | +## 安装Megatron-LM | ||
| 87 | + | ||
| 88 | +```bash | ||
| 89 | +git clone https://github.com/NVIDIA/Megatron-LM.git | ||
| 90 | +cd Megatron-LM | ||
| 91 | +git checkout 1d462bd37dac21 | ||
| 92 | +git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/megatron-core/1d462bd37dac/glm5.patch | ||
| 93 | +cp -r megatron ../verl/ | ||
| 94 | +cd .. | ||
| 95 | +``` | ||
| 96 | + | ||
| 97 | +## 安装Megatron-Bridge | ||
| 98 | + | ||
| 99 | +```bash | ||
| 100 | +git clone https://github.com/NVIDIA-NeMo/Megatron-Bridge.git | ||
| 101 | +cd Megatron-Bridge | ||
| 102 | +git checkout 7cabf71 | ||
| 103 | +git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/megatron-bridge/7cabf71/glm5.patch | ||
| 104 | +cp -r src/megatron/bridge ../verl/megatron | ||
| 105 | +cd .. | ||
| 106 | +``` | ||
| 107 | + | ||
| 108 | +# 模型运行 | ||
| 109 | + | ||
| 110 | +```bash | ||
| 111 | +cd verl | ||
| 112 | +cp ../MindSpeed-RL/tests/verl_examples/grpo/grpo_glm5_megatron_A3 ./ | ||
| 113 | +``` | ||
| 114 | + | ||
| 115 | +请参照**[verl_npu安装指南](https://gitcode.com/Minds66/MindSpeed-RL/tree/master/verl_npu)** 安装内存管理优化库 | ||
| 116 | + `test_grpo_glm5_megatron_A3.sh`脚本即将到来 | ||
| 117 | + | ||
| 118 | +* `SOCKET_IFNAME`为当前节点的通信网卡 | ||
| 119 | +* `MASTER_ADDR`为对应主节点IP | ||
| 120 | +* `NNODES`为使用的节点数 | ||
| 121 | + | ||
| 122 | + | ||
| 123 | +```bash | ||
| 124 | +bash grpo_glm5_megatron_A3.sh | ||
| 125 | +``` | ||
| @@ -1,4 +1,4 @@ | |||
| 1 | -# | 1 | +# 模型简介 |
| 2 | 2 | ||
| 3 | 由于DeepSeek-V3.2相关依赖的transformers、Megatron-Bridge适配PR尚未合入,因此需要以 **patch 补丁形式** 将关键适配点单独应用到现有版本中。 | 3 | 由于DeepSeek-V3.2相关依赖的transformers、Megatron-Bridge适配PR尚未合入,因此需要以 **patch 补丁形式** 将关键适配点单独应用到现有版本中。 |
| 4 | 4 | ||
| @@ -78,7 +78,7 @@ cd .. | |||
| 78 | git clone https://gitcode.com/Ascend/MindSpeed-RL.git | 78 | git clone https://gitcode.com/Ascend/MindSpeed-RL.git |
| 79 | cd transformers && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/transformers/47b0e478f/transformers.patch && pip install -e . && cd .. | 79 | cd transformers && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/transformers/47b0e478f/transformers.patch && pip install -e . && cd .. |
| 80 | cd vllm && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/vllm/72506c98349/common.patch && cd .. | 80 | cd vllm && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/vllm/72506c98349/common.patch && cd .. |
| 81 | -cd vllm-ascend && git apply --whitespace=fix ../MindSpeed-RL/verl_npu/verl_npu/patch/vllm_ascend/0f812dcc58/sfa_v1.patch && cd.. | 81 | +cd vllm-ascend && git apply --whitespace=fix ../MindSpeed-RL/verl_npu/verl_npu/patch/vllm_ascend/0f812dcc58/sfa_v1.patch && cd .. |
| 82 | cd verl && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/verl/0c06358d6/vllm_async_server.patch && cd .. | 82 | cd verl && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/verl/0c06358d6/vllm_async_server.patch && cd .. |
| 83 | cd MindSpeed && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/mindspeed/07056df535/dsa.patch && cd .. | 83 | cd MindSpeed && git apply ../MindSpeed-RL/verl_npu/verl_npu/patch/mindspeed/07056df535/dsa.patch && cd .. |
| 84 | #需手动卸载triton | 84 | #需手动卸载triton |
| @@ -115,16 +115,16 @@ cp ../MindSpeed-RL/tests/verl_examples/configs/test_grpo_deepseekv3.2exp_megatro | |||
| 115 | cp ../MindSpeed-RL/tests/verl_examples/grpo/grpo_deepseekv3.2exp_megatron_A3.sh ./ | 115 | cp ../MindSpeed-RL/tests/verl_examples/grpo/grpo_deepseekv3.2exp_megatron_A3.sh ./ |
| 116 | ``` | 116 | ``` |
| 117 | 117 | ||
| 118 | -请参照**[verl_npu安装指南](https://gitcode.com/Minds66/MindSpeed-RL/tree/master/verl_npu)** 安装内存管理优化库 | 118 | +请参照[verl_npu安装指南](https://gitcode.com/Ascend/MindSpeed-RL/tree/master/verl_npu)安装内存管理优化库 |
| 119 | -修改`test_grpo_deepseekv3.2exp_megatron_A3.sh`脚本中的相应的配置 | 119 | +修改`grpo_deepseekv3.2exp_megatron_A3.sh`脚本中的相应的配置 |
| 120 | 120 | ||
| 121 | * `ASCEND_CUSTOM_OPP_PATH`为 VLLM_ASCEND 编译后生成的自定义算子路径 | 121 | * `ASCEND_CUSTOM_OPP_PATH`为 VLLM_ASCEND 编译后生成的自定义算子路径 |
| 122 | * `SOCKET_IFNAME`为当前节点的通信网卡 | 122 | * `SOCKET_IFNAME`为当前节点的通信网卡 |
| 123 | * `MASTER_ADDR`为对应主节点IP | 123 | * `MASTER_ADDR`为对应主节点IP |
| 124 | * `NNODES`为使用的节点数 | 124 | * `NNODES`为使用的节点数 |
| 125 | 125 | ||
| 126 | -修改`grpo_deepseekv3.2exp_megatron_A3.sh`脚本中的权重`hf_weights`和所用数据集`train_files`和`test_files` | 126 | +修改`test_grpo_deepseekv3.2exp_megatron_A3.sh`脚本中的权重`hf_weights`和所用数据集`train_files`和`test_files` |
| 127 | 127 | ||
| 128 | ```bash | 128 | ```bash |
| 129 | -bash test_grpo_deepseekv3.2exp_megatron_A3.sh | 129 | +bash grpo_deepseekv3.2exp_megatron_A3.sh |
| 130 | ``` | 130 | ``` |
| @@ -0,0 +1,560 @@ | |||
| 1 | +diff --git a/src/megatron/bridge/models/__init__.py b/src/megatron/bridge/models/__init__.py | ||
| 2 | +index 0faff443..b32820e3 100644 | ||
| 3 | +--- a/src/megatron/bridge/models/__init__.py | ||
| 4 | ++++ b/src/megatron/bridge/models/__init__.py | ||
| 5 | + from megatron.bridge.models.glm import ( | ||
| 6 | + GLM45ModelProvider355B, | ||
| 7 | + GLMMoEModelProvider, | ||
| 8 | + ) | ||
| 9 | ++from megatron.bridge.models.glm_moe_dsa import ( | ||
| 10 | ++ GLM5Bridge, | ||
| 11 | ++) | ||
| 12 | + from megatron.bridge.models.gpt_oss import ( | ||
| 13 | + GPTOSSBridge, | ||
| 14 | + GPTOSSProvider, | ||
| 15 | + __all__ = [ | ||
| 16 | + "GLM45ModelProvider355B", | ||
| 17 | + "GLM45AirModelProvider106B", | ||
| 18 | + "GLM45Bridge", | ||
| 19 | ++ "GLM5Bridge", | ||
| 20 | + "GPTModelProvider", | ||
| 21 | + "GPTOSSBridge", | ||
| 22 | + "GPTOSSProvider", | ||
| 23 | +diff --git a/src/megatron/bridge/models/conversion/model_bridge.py b/src/megatron/bridge/models/conversion/model_bridge.py | ||
| 24 | +index 0f989304..64aa362a 100644 | ||
| 25 | +--- a/src/megatron/bridge/models/conversion/model_bridge.py | ||
| 26 | ++++ b/src/megatron/bridge/models/conversion/model_bridge.py | ||
| 27 | + from megatron.bridge.peft.lora import LoRAMerge | ||
| 28 | + from megatron.bridge.peft.utils import get_adapter_attributes_from_linear | ||
| 29 | + from megatron.bridge.utils.common_utils import print_rank_0 | ||
| 30 | + | ||
| 31 | ++kv_flag = {} | ||
| 32 | ++kv_cnt = 0 | ||
| 33 | ++def get_kv_flag(): | ||
| 34 | ++ global kv_flag | ||
| 35 | ++ return kv_flag | ||
| 36 | + | ||
| 37 | + logger = logging.getLogger(__name__) | ||
| 38 | + | ||
| 39 | + ADAPTER_NAME_MAP = { | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + | ||
| 43 | ++def check_full(param_name): | ||
| 44 | ++ kv_local = get_kv_flag() | ||
| 45 | ++ if "self_attention.linear_k_up_proj.weight" not in param_name and "self_attention.linear_v_up_proj.weight" not in param_name: | ||
| 46 | ++ return True, None | ||
| 47 | ++ name_splits = param_name.split(".") | ||
| 48 | ++ layer_number = str(name_splits[2]) | ||
| 49 | ++ if layer_number in kv_flag: | ||
| 50 | ++ return True, layer_number | ||
| 51 | ++ return False, layer_number | ||
| 52 | ++ | ||
| 53 | ++ | ||
| 54 | + class MegatronWeightTuple(NamedTuple): | ||
| 55 | + """Tuple representing a Megatron model weight with its metadata.""" | ||
| 56 | + | ||
| 57 | + class MegatronModelBridge(Generic[HFPreTrained, ModelProviderTarget, MegatronMod | ||
| 58 | + hf_weights = self.maybe_modify_loaded_hf_weight(task.mapping.hf_param, hf_state_dict) | ||
| 59 | + | ||
| 60 | + # 2) Delegate conversion & distribution to the bridge | ||
| 61 | +- converted_weights = task.mapping.hf_to_megatron(hf_weights, task.megatron_module) | ||
| 62 | ++ if "self_attention.linear_k_up_proj.weight" in task.param_name or "self_attention.linear_v_up_proj.weight" in task.param_name: | ||
| 63 | ++ converted_weights = task.mapping.hf_to_megatron(hf_weights, task.megatron_module, task.param_name) | ||
| 64 | ++ else: | ||
| 65 | ++ converted_weights = task.mapping.hf_to_megatron(hf_weights, task.megatron_module) | ||
| 66 | + | ||
| 67 | + # 3) Copy into Megatron param if this rank received a shard | ||
| 68 | + if converted_weights is not None: | ||
| 69 | + class MegatronModelBridge(Generic[HFPreTrained, ModelProviderTarget, MegatronMod | ||
| 70 | + unwrapped_model = unwrap_model(megatron_model)[0] | ||
| 71 | + model_config = unwrapped_model.config | ||
| 72 | + embeddings_are_tied = self._share_embeddings_and_output_weights(model_config, unwrapped_model) | ||
| 73 | ++ | ||
| 74 | ++ kv_flag_local_2 = get_kv_flag() | ||
| 75 | + for task in self._with_progress_tracking(megatron_to_hf_tasks, "Converting to HuggingFace", show_progress): | ||
| 76 | + converted_weights_dict = task.mapping.megatron_to_hf(task.param_weight, task.megatron_module) | ||
| 77 | ++ hf_name_tmp = None | ||
| 78 | ++ for hf_name, tensor in converted_weights_dict.items(): | ||
| 79 | ++ hf_name_tmp = hf_name | ||
| 80 | ++ | ||
| 81 | ++ global kv_cnt | ||
| 82 | ++ flag, layer_num = check_full(task.global_param_name) | ||
| 83 | ++ if not flag: | ||
| 84 | ++ for hf_name, tensor in converted_weights_dict.items(): | ||
| 85 | ++ kv_flag_local_2[layer_num] = {} | ||
| 86 | ++ kv_flag_local_2[layer_num][task.global_param_name] = tensor.clone() | ||
| 87 | ++ if torch.distributed.get_rank() == 0: | ||
| 88 | ++ print(f"layer_num{layer_num} , {task.global_param_name}, {kv_cnt} continue") | ||
| 89 | ++ continue | ||
| 90 | ++ elif "self_attention.linear_k_up_proj.weight" in task.global_param_name or "self_attention.linear_v_up_proj.weight" in task.global_param_name: | ||
| 91 | ++ if "self_attention.linear_k_up_proj.weight" in task.global_param_name: | ||
| 92 | ++ for param_name, tensor_v in kv_flag_local_2[layer_num].items(): | ||
| 93 | ++ tensor_k = converted_weights_dict[hf_name_tmp].clone() | ||
| 94 | ++ tensor_k = tensor_k.view(64, 192, 512) | ||
| 95 | ++ tensor_v = tensor_v.view(64, 256, 512) | ||
| 96 | ++ new_tensor = torch.cat([tensor_k, tensor_v], dim=1) | ||
| 97 | ++ converted_weights_dict[hf_name_tmp] = new_tensor.view(-1, tensor_k.shape[-1]) | ||
| 98 | ++ else: | ||
| 99 | ++ for param_name, tensor_k in kv_flag_local_2[layer_num].items(): | ||
| 100 | ++ tensor_v = converted_weights_dict[hf_name_tmp].clone() | ||
| 101 | ++ tensor_k = tensor_k.view(64, 192, 512) | ||
| 102 | ++ tensor_v = tensor_v.view(64, 256, 512) | ||
| 103 | ++ new_tensor = torch.cat([tensor_k, tensor_v], dim=1) | ||
| 104 | ++ converted_weights_dict[hf_name_tmp] = new_tensor.view(-1, tensor_k.shape[-1]) | ||
| 105 | ++ | ||
| 106 | ++ del kv_flag_local_2[layer_num] | ||
| 107 | ++ if torch.distributed.get_rank() == 0: | ||
| 108 | ++ print(f"layer_num{layer_num} , {task.global_param_name}, {kv_cnt} done") | ||
| 109 | ++ kv_cnt = kv_cnt + 1 | ||
| 110 | ++ | ||
| 111 | + converted_weights_dict = self.maybe_modify_converted_hf_weight( | ||
| 112 | + task, converted_weights_dict | ||
| 113 | + ) # dict will be none except for one expert; | ||
| 114 | +diff --git a/src/megatron/bridge/models/conversion/param_mapping.py b/src/megatron/bridge/models/conversion/param_mapping.py | ||
| 115 | +index 84adb91b..aa63bed2 100644 | ||
| 116 | +--- a/src/megatron/bridge/models/conversion/param_mapping.py | ||
| 117 | ++++ b/src/megatron/bridge/models/conversion/param_mapping.py | ||
| 118 | + class ColumnParallelMapping(MegatronParamMapping[torch.Tensor]): | ||
| 119 | + self, | ||
| 120 | + hf_weights: torch.Tensor, | ||
| 121 | + megatron_module: nn.Module, | ||
| 122 | ++ param_name=None | ||
| 123 | + ) -> torch.Tensor: | ||
| 124 | + """Split weight along dim 0 and distribute to TP ranks.""" | ||
| 125 | ++ if param_name is not None and "self_attention.linear_k_up_proj.weight" in param_name: | ||
| 126 | ++ # num_attention_heads, qk_nope_head_dim + v_head_dim, lora_rank(-1)/512 | ||
| 127 | ++ hf_weights = hf_weights.reshape(64, 448, -1) | ||
| 128 | ++ # qk_nope_head_dim = 192 | ||
| 129 | ++ hf_weights = hf_weights.split([192, 256],dim=1)[0] | ||
| 130 | ++ hf_weights = hf_weights.view(-1, 512) | ||
| 131 | ++ elif param_name is not None and "self_attention.linear_v_up_proj.weight" in param_name: | ||
| 132 | ++ # num_attention_heads, qk_nope_head_dim + v_head_dim, -1 | ||
| 133 | ++ hf_weights = hf_weights.reshape(64, 448, -1) | ||
| 134 | ++ # v_head_dim = 256 | ||
| 135 | ++ hf_weights = hf_weights.split([192, 256],dim=1)[1] | ||
| 136 | ++ hf_weights = hf_weights.view(-1, 512) | ||
| 137 | + if self.tp_size == 1: | ||
| 138 | + return hf_weights | ||
| 139 | + | ||
| 140 | + class AutoMapping(MegatronParamMapping[torch.Tensor]): | ||
| 141 | + def _detect_parallelism_type(self, module: nn.Module) -> str: | ||
| 142 | + """Detect parallelism type from module.""" | ||
| 143 | + module_type = type(module).__name__ | ||
| 144 | ++ if module_type.startswith("MindSpeed"): | ||
| 145 | ++ module_type = module_type.replace("MindSpeed", "") | ||
| 146 | + | ||
| 147 | + # Handle fused modules like TELayerNormColumnParallelLinear | ||
| 148 | + # These modules have both column-parallel weights (weight, bias) | ||
| 149 | + class AutoMapping(MegatronParamMapping[torch.Tensor]): | ||
| 150 | + | ||
| 151 | + # Check parallel_mode for TELinear | ||
| 152 | + if module_type == "TELinear": | ||
| 153 | +- if module.parallel_mode == "column": | ||
| 154 | ++ parallel_mode = getattr(module, 'parallel_mode', '') | ||
| 155 | ++ if parallel_mode == "column": | ||
| 156 | + return "column" | ||
| 157 | +- elif module.parallel_mode == "row": | ||
| 158 | ++ elif parallel_mode == "row": | ||
| 159 | + return "row" | ||
| 160 | + else: | ||
| 161 | + return "replicated" | ||
| 162 | + class AutoMapping(MegatronParamMapping[torch.Tensor]): | ||
| 163 | + self, | ||
| 164 | + hf_weights: torch.Tensor, | ||
| 165 | + megatron_module: nn.Module, | ||
| 166 | ++ param_name=None | ||
| 167 | + ) -> torch.Tensor: | ||
| 168 | + """Delegate to appropriate mapping based on module type.""" | ||
| 169 | + # Apply permutation if specified (before distribution) | ||
| 170 | + class AutoMapping(MegatronParamMapping[torch.Tensor]): | ||
| 171 | + if self._mapping is None: | ||
| 172 | + self._detected_type = self._detect_parallelism_type(megatron_module) | ||
| 173 | + self._mapping = self._get_or_create_mapping(self._detected_type) | ||
| 174 | +- | ||
| 175 | +- return self._mapping.hf_to_megatron(hf_weights, megatron_module) | ||
| 176 | ++ if param_name is not None: | ||
| 177 | ++ if "self_attention.linear_k_up_proj.weight" in param_name or "self_attention.linear_v_up_proj.weight" in param_name: | ||
| 178 | ++ return self._mapping.hf_to_megatron(hf_weights, megatron_module, param_name) | ||
| 179 | ++ else: | ||
| 180 | ++ return self._mapping.hf_to_megatron(hf_weights, megatron_module) | ||
| 181 | + | ||
| 182 | + def megatron_to_hf( | ||
| 183 | + self, | ||
| 184 | +diff --git a/src/megatron/bridge/models/glm_moe_dsa/__init__.py b/src/megatron/bridge/models/glm_moe_dsa/__init__.py | ||
| 185 | +new file mode 100644 | ||
| 186 | +index 00000000..e13c3198 | ||
| 187 | +--- /dev/null | ||
| 188 | ++++ b/src/megatron/bridge/models/glm_moe_dsa/__init__.py | ||
| 189 | + | ||
| 190 | ++# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| 191 | ++# | ||
| 192 | ++# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 193 | ++# you may not use this file except in compliance with the License. | ||
| 194 | ++# You may obtain a copy of the License at | ||
| 195 | ++# | ||
| 196 | ++# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 197 | ++# | ||
| 198 | ++# Unless required by applicable law or agreed to in writing, software | ||
| 199 | ++# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 200 | ++# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 201 | ++# See the License for the specific language governing permissions and | ||
| 202 | ++# limitations under the License. | ||
| 203 | ++ | ||
| 204 | ++from megatron.bridge.models.glm_moe_dsa.glm5_bridge import GLM5Bridge | ||
| 205 | ++ | ||
| 206 | ++ | ||
| 207 | ++__all__ = [ | ||
| 208 | ++ "GLM5Bridge", | ||
| 209 | ++] | ||
| 210 | + | ||
| 211 | +diff --git a/src/megatron/bridge/models/glm_moe_dsa/glm5_bridge.py b/src/megatron/bridge/models/glm_moe_dsa/glm5_bridge.py | ||
| 212 | +new file mode 100644 | ||
| 213 | +index 00000000..dbe69c34 | ||
| 214 | +--- /dev/null | ||
| 215 | ++++ b/src/megatron/bridge/models/glm_moe_dsa/glm5_bridge.py | ||
| 216 | + | ||
| 217 | ++import torch | ||
| 218 | ++from megatron.core.models.gpt.gpt_model import GPTModel | ||
| 219 | ++ | ||
| 220 | ++from megatron.bridge.models.conversion.mapping_registry import MegatronMappingRegistry | ||
| 221 | ++from megatron.bridge.models.conversion.model_bridge import MegatronModelBridge | ||
| 222 | ++from megatron.bridge.models.conversion.param_mapping import AutoMapping, GatedMLPMapping | ||
| 223 | ++ | ||
| 224 | ++from megatron.bridge.models.glm_moe_dsa.glm5_provider import GLM5ModelProvider | ||
| 225 | ++from megatron.bridge.models.hf_pretrained.causal_lm import PreTrainedCausalLM | ||
| 226 | ++ | ||
| 227 | ++ | ||
| 228 | ++@MegatronModelBridge.register_bridge(source="GlmMoeDsaForCausalLM", target=GPTModel) | ||
| 229 | ++class GLM5Bridge(MegatronModelBridge): | ||
| 230 | ++ """ | ||
| 231 | ++ Megatron Bridge for GLM-5 (MoE + MLA + DSA). | ||
| 232 | ++ | ||
| 233 | ++ This bridge handles conversion between HuggingFace GlmMoeDsaForCausalLM | ||
| 234 | ++ and Megatron-Core GPTModel formats. | ||
| 235 | ++ | ||
| 236 | ++ GLM-5 uses Multi-Latent Attention (MLA), Dynamic Sparse Attention (DSA) | ||
| 237 | ++ indexer layers, and Mixture-of-Experts (MoE). | ||
| 238 | ++ Requires transformers>=5.2.0. | ||
| 239 | ++ | ||
| 240 | ++ Example: | ||
| 241 | ++ >>> from megatron.bridge import AutoBridge | ||
| 242 | ++ >>> bridge = AutoBridge.from_hf_pretrained("zai-org/GLM-5") | ||
| 243 | ++ >>> provider = bridge.to_megatron_provider() | ||
| 244 | ++ """ | ||
| 245 | ++ | ||
| 246 | ++ def provider_bridge(self, hf_pretrained: PreTrainedCausalLM) -> GLM5ModelProvider: | ||
| 247 | ++ hf_config = hf_pretrained.config | ||
| 248 | ++ configs = { | ||
| 249 | ++ "num_layers": hf_config.num_hidden_layers, | ||
| 250 | ++ "hidden_size": hf_config.hidden_size, | ||
| 251 | ++ "ffn_hidden_size": hf_config.intermediate_size, | ||
| 252 | ++ "num_attention_heads": hf_config.num_attention_heads, | ||
| 253 | ++ "num_query_groups": hf_config.num_key_value_heads, | ||
| 254 | ++ "kv_channels": getattr(hf_config, "head_dim", hf_config.hidden_size // hf_config.num_attention_heads), | ||
| 255 | ++ "q_lora_rank": hf_config.q_lora_rank, | ||
| 256 | ++ "kv_lora_rank": hf_config.kv_lora_rank, | ||
| 257 | ++ "num_moe_experts": hf_config.n_routed_experts, | ||
| 258 | ++ "moe_ffn_hidden_size": hf_config.moe_intermediate_size, | ||
| 259 | ++ "moe_shared_expert_intermediate_size": hf_config.moe_intermediate_size * hf_config.n_shared_experts, | ||
| 260 | ++ "moe_layer_freq": [0] * hf_config.first_k_dense_replace + [1] * (hf_config.num_hidden_layers - hf_config.first_k_dense_replace), | ||
| 261 | ++ "moe_router_topk": hf_config.num_experts_per_tok, | ||
| 262 | ++ "moe_router_num_groups": hf_config.n_group, | ||
| 263 | ++ "moe_router_group_topk": hf_config.topk_group, | ||
| 264 | ++ "moe_router_topk_scaling_factor": hf_config.routed_scaling_factor, | ||
| 265 | ++ # MLA dims in MCore format | ||
| 266 | ++ "qk_head_dim": hf_config.qk_nope_head_dim, | ||
| 267 | ++ "qk_pos_emb_head_dim": hf_config.qk_rope_head_dim, | ||
| 268 | ++ "v_head_dim": hf_config.v_head_dim, | ||
| 269 | ++ "vocab_size": hf_config.vocab_size, | ||
| 270 | ++ "rotary_base": hf_config.rope_parameters["rope_theta"], | ||
| 271 | ++ "init_method_std": hf_config.initializer_range, | ||
| 272 | ++ "layernorm_epsilon": hf_config.rms_norm_eps, | ||
| 273 | ++ "multi_latent_attention": True, | ||
| 274 | ++ # DSA indexer params | ||
| 275 | ++ "experimental_attention_variant": "dsa", | ||
| 276 | ++ "dsa_indexer_head_dim": hf_config.index_head_dim, | ||
| 277 | ++ "dsa_indexer_n_heads": hf_config.index_n_heads, | ||
| 278 | ++ "dsa_indexer_topk": hf_config.index_topk, | ||
| 279 | ++ "dsa_indexer_loss_coeff": 0.001, | ||
| 280 | ++ "dsa_indexer_use_sparse_loss": True, | ||
| 281 | ++ # GLM5 uses default rope (no YaRN scaling) | ||
| 282 | ++ "rotary_scaling_factor": 1.0, | ||
| 283 | ++ "mscale": 1.0, | ||
| 284 | ++ "mscale_all_dim": 1.0, | ||
| 285 | ++ } | ||
| 286 | ++ | ||
| 287 | ++ dtype = self.dtype_from_hf(hf_config, default=torch.float32) | ||
| 288 | ++ configs["fp16"] = dtype == torch.float16 | ||
| 289 | ++ configs["bf16"] = dtype == torch.bfloat16 | ||
| 290 | ++ configs["params_dtype"] = dtype | ||
| 291 | ++ configs["make_vocab_size_divisible_by"] = 1280 | ||
| 292 | ++ configs["moe_router_score_function"] = "sigmoid" | ||
| 293 | ++ configs["moe_router_enable_expert_bias"] = True | ||
| 294 | ++ if hasattr(hf_config, "aux_loss_alpha"): | ||
| 295 | ++ configs["moe_aux_loss_coeff"] = hf_config.aux_loss_alpha | ||
| 296 | ++ | ||
| 297 | ++ provider = GLM5ModelProvider(**configs) | ||
| 298 | ++ # from megatron.core.models.gpt.experimental_attention_variant_module_specs import get_transformer_block_with_experimental_attention_variant_spec) | ||
| 299 | ++ # provider.transformer_layer_spec = get_transformer_block_with_experimental_attention_variant_spec | ||
| 300 | ++ | ||
| 301 | ++ provider.normalization = "RMSNorm" | ||
| 302 | ++ provider.gated_linear_unit = True | ||
| 303 | ++ provider.position_embedding_type = "rope" | ||
| 304 | ++ provider.add_bias_linear = False | ||
| 305 | ++ provider.share_embeddings_and_output_weights = False | ||
| 306 | ++ provider.qk_layernorm = True # GLM5 MLA has q_a_layernorm and kv_a_layernorm | ||
| 307 | ++ provider.multi_latent_attention = True | ||
| 308 | ++ provider.moe_grouped_gemm = True | ||
| 309 | ++ provider.moe_router_pre_softmax = True | ||
| 310 | ++ provider.moe_token_dispatcher_type = "alltoall" | ||
| 311 | ++ provider.moe_router_load_balancing_type = "seq_aux_loss" | ||
| 312 | ++ provider.moe_shared_expert_overlap = True | ||
| 313 | ++ provider.moe_router_dtype = "fp32" | ||
| 314 | ++ provider.moe_permute_fusion = True | ||
| 315 | ++ # provider.moe_permute_fusion = False | ||
| 316 | ++ provider.hidden_dropout = 0.0 | ||
| 317 | ++ provider.attention_softmax_in_fp32 = False | ||
| 318 | ++ | ||
| 319 | ++ return provider | ||
| 320 | ++ | ||
| 321 | ++ def mapping_registry(self) -> MegatronMappingRegistry: | ||
| 322 | ++ # mapping_list = get_common_mapping_list() #v32,不用 | ||
| 323 | ++ mapping_list = [] | ||
| 324 | ++ param_mappings = { | ||
| 325 | ++ # Embed | ||
| 326 | ++ "embedding.word_embeddings.weight": "model.embed_tokens.weight", | ||
| 327 | ++ # LM Head | ||
| 328 | ++ "decoder.final_layernorm.weight": "model.norm.weight", | ||
| 329 | ++ "output_layer.weight": "lm_head.weight", | ||
| 330 | ++ } | ||
| 331 | ++ | ||
| 332 | ++ layer_specific_mappings = { | ||
| 333 | ++ # Attention layernorm | ||
| 334 | ++ # "decoder.layers.*.self_attention.linear_qkv.layer_norm_weight": "model.layers.*.input_layernorm.weight", # 不用 | ||
| 335 | ++ "decoder.layers.*.input_layernorm.weight": "model.layers.*.input_layernorm.weight", | ||
| 336 | ++ # Attention output | ||
| 337 | ++ "decoder.layers.*.self_attention.linear_proj.weight": "model.layers.*.self_attn.o_proj.weight", | ||
| 338 | ++ # Post-attention layernorm — MoE layers use pre_mlp_layernorm, dense layers use layer_norm_weight | ||
| 339 | ++ "decoder.layers.*.pre_mlp_layernorm.weight": "model.layers.*.post_attention_layernorm.weight", | ||
| 340 | ++ "decoder.layers.*.mlp.linear_fc1.layer_norm_weight": "model.layers.*.post_attention_layernorm.weight", | ||
| 341 | ++ # MLA weights | ||
| 342 | ++ "decoder.layers.*.self_attention.linear_q_down_proj.weight": "model.layers.*.self_attn.q_a_proj.weight", | ||
| 343 | ++ "decoder.layers.*.self_attention.linear_q_up_proj.weight": "model.layers.*.self_attn.q_b_proj.weight", | ||
| 344 | ++ "decoder.layers.*.self_attention.linear_q_up_proj.layer_norm_weight": "model.layers.*.self_attn.q_a_layernorm.weight", | ||
| 345 | ++ "decoder.layers.*.self_attention.q_layernorm.weight": "model.layers.*.self_attn.q_a_layernorm.weight", | ||
| 346 | ++ "decoder.layers.*.self_attention.linear_kv_down_proj.weight": "model.layers.*.self_attn.kv_a_proj_with_mqa.weight", | ||
| 347 | ++ # "decoder.layers.*.self_attention.linear_kv_up_proj.weight": "model.layers.*.self_attn.kv_b_proj.weight", # DSA | ||
| 348 | ++ "decoder.layers.*.self_attention.linear_k_up_proj.weight": "model.layers.*.self_attn.kv_b_proj.weight",# NPU AbsorbedDSA | ||
| 349 | ++ "decoder.layers.*.self_attention.linear_v_up_proj.weight": "model.layers.*.self_attn.kv_b_proj.weight", | ||
| 350 | ++ "decoder.layers.*.self_attention.linear_kv_up_proj.layer_norm_weight": "model.layers.*.self_attn.kv_a_layernorm.weight", | ||
| 351 | ++ "decoder.layers.*.self_attention.kv_layernorm.weight": "model.layers.*.self_attn.kv_a_layernorm.weight", | ||
| 352 | ++ # For non-MLA attention (fallback) | ||
| 353 | ++ "decoder.layers.*.self_attention.linear_q_proj.weight": "model.layers.*.self_attn.q_proj.weight", | ||
| 354 | ++ # DSA indexer | ||
| 355 | ++ "decoder.layers.*.self_attention.core_attention.indexer.linear_wq_b.weight": "model.layers.*.self_attn.indexer.wq_b.weight", | ||
| 356 | ++ "decoder.layers.*.self_attention.core_attention.indexer.linear_wk.weight": "model.layers.*.self_attn.indexer.wk.weight", | ||
| 357 | ++ "decoder.layers.*.self_attention.core_attention.indexer.k_norm.weight": "model.layers.*.self_attn.indexer.k_norm.weight", | ||
| 358 | ++ "decoder.layers.*.self_attention.core_attention.indexer.k_norm.bias": "model.layers.*.self_attn.indexer.k_norm.bias", | ||
| 359 | ++ "decoder.layers.*.self_attention.core_attention.indexer.linear_weights_proj.weight": "model.layers.*.self_attn.indexer.weights_proj.weight", | ||
| 360 | ++ # Dense MLP | ||
| 361 | ++ "decoder.layers.*.mlp.linear_fc2.weight": "model.layers.*.mlp.down_proj.weight", | ||
| 362 | ++ # MoE router | ||
| 363 | ++ "decoder.layers.*.mlp.router.weight": "model.layers.*.mlp.gate.weight", | ||
| 364 | ++ "decoder.layers.*.mlp.router.expert_bias": "model.layers.*.mlp.gate.e_score_correction_bias", | ||
| 365 | ++ # MoE shared experts | ||
| 366 | ++ "decoder.layers.*.mlp.shared_experts.router.weight": "model.layers.*.mlp.shared_experts.gate.weight", # 不用 | ||
| 367 | ++ "decoder.layers.*.mlp.shared_experts.linear_fc2.weight": "model.layers.*.mlp.shared_experts.down_proj.weight", | ||
| 368 | ++ } | ||
| 369 | ++ | ||
| 370 | ++ for megatron_param, hf_param in param_mappings.items(): | ||
| 371 | ++ mapping_list.append(AutoMapping(megatron_param=megatron_param, hf_param=hf_param)) | ||
| 372 | ++ | ||
| 373 | ++ for megatron_param, hf_param in layer_specific_mappings.items(): | ||
| 374 | ++ mapping_list.append(AutoMapping(megatron_param=megatron_param, hf_param=hf_param)) | ||
| 375 | ++ | ||
| 376 | ++ | ||
| 377 | ++ mapping_list.extend( | ||
| 378 | ++ [ | ||
| 379 | ++ # Dense MLP gate+up → fc1 | ||
| 380 | ++ GatedMLPMapping( | ||
| 381 | ++ megatron_param="decoder.layers.*.mlp.linear_fc1.weight", | ||
| 382 | ++ gate="model.layers.*.mlp.gate_proj.weight", | ||
| 383 | ++ up="model.layers.*.mlp.up_proj.weight", | ||
| 384 | ++ ), | ||
| 385 | ++ | ||
| 386 | ++ # Shared expert gate+up → fc1 | ||
| 387 | ++ GatedMLPMapping( | ||
| 388 | ++ megatron_param="decoder.layers.*.mlp.shared_experts.linear_fc1.weight", | ||
| 389 | ++ gate="model.layers.*.mlp.shared_experts.gate_proj.weight", | ||
| 390 | ++ up="model.layers.*.mlp.shared_experts.up_proj.weight", | ||
| 391 | ++ ), | ||
| 392 | ++ ] | ||
| 393 | ++ ) | ||
| 394 | ++ | ||
| 395 | ++ | ||
| 396 | ++ # MoE expert weights (per-expert format: experts.N.gate_proj / up_proj / down_proj) | ||
| 397 | ++ mapping_list.extend( | ||
| 398 | ++ [ | ||
| 399 | ++ GatedMLPMapping( | ||
| 400 | ++ megatron_param="decoder.layers.*.mlp.experts.linear_fc1.weight*", | ||
| 401 | ++ gate="model.layers.*.mlp.experts.*.gate_proj.weight", | ||
| 402 | ++ up="model.layers.*.mlp.experts.*.up_proj.weight", | ||
| 403 | ++ ), | ||
| 404 | ++ # MoE fc2, dsv32这个放上面去了,都一样的。 | ||
| 405 | ++ AutoMapping( | ||
| 406 | ++ megatron_param="decoder.layers.*.mlp.experts.linear_fc2.weight*", | ||
| 407 | ++ hf_param="model.layers.*.mlp.experts.*.down_proj.weight", | ||
| 408 | ++ ), | ||
| 409 | ++ ] | ||
| 410 | ++ ) | ||
| 411 | ++ return MegatronMappingRegistry(*mapping_list) | ||
| 412 | + | ||
| 413 | +diff --git a/src/megatron/bridge/models/glm_moe_dsa/glm5_provider.py b/src/megatron/bridge/models/glm_moe_dsa/glm5_provider.py | ||
| 414 | +new file mode 100644 | ||
| 415 | +index 00000000..cf6e5edc | ||
| 416 | +--- /dev/null | ||
| 417 | ++++ b/src/megatron/bridge/models/glm_moe_dsa/glm5_provider.py | ||
| 418 | + | ||
| 419 | ++# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| 420 | ++# | ||
| 421 | ++# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 422 | ++# you may not use this file except in compliance with the License. | ||
| 423 | ++# You may obtain a copy of the License at | ||
| 424 | ++# | ||
| 425 | ++# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 426 | ++# | ||
| 427 | ++# Unless required by applicable law or agreed to in writing, software | ||
| 428 | ++# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 429 | ++# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 430 | ++# See the License for the specific language governing permissions and | ||
| 431 | ++# limitations under the License. | ||
| 432 | ++ | ||
| 433 | ++"""GLM5 uses MLAModelProvider directly. This module is kept for import compatibility.""" | ||
| 434 | ++from dataclasses import dataclass | ||
| 435 | ++from functools import partial | ||
| 436 | ++from megatron.bridge.models.gpt_provider import GPTModelProvider | ||
| 437 | ++from megatron.bridge.models.transformer_config import MLATransformerConfig | ||
| 438 | ++ | ||
| 439 | ++ | ||
| 440 | ++from megatron.core.models.gpt.gpt_layer_specs import get_gpt_decoder_block_spec | ||
| 441 | ++from typing import TYPE_CHECKING, Callable, List, Union | ||
| 442 | ++ | ||
| 443 | ++try: | ||
| 444 | ++ import transformer_engine # type: ignore # noqa: F401 | ||
| 445 | ++ | ||
| 446 | ++ HAVE_TE = True | ||
| 447 | ++except (ImportError, ModuleNotFoundError): | ||
| 448 | ++ HAVE_TE = False | ||
| 449 | ++ | ||
| 450 | ++if TYPE_CHECKING: | ||
| 451 | ++ from megatron.core.transformer import ModuleSpec | ||
| 452 | ++ | ||
| 453 | ++if HAVE_TE: | ||
| 454 | ++ from megatron.core.utils import is_te_min_version | ||
| 455 | ++ | ||
| 456 | ++ | ||
| 457 | ++ | ||
| 458 | ++@dataclass | ||
| 459 | ++class GLM5ModelProvider(MLATransformerConfig, GPTModelProvider): | ||
| 460 | ++ """Provider for models using Multi-Latent Attention (MLA). | ||
| 461 | ++ | ||
| 462 | ++ This class combines MLATransformerConfig (which provides MLA-specific fields | ||
| 463 | ++ like q_lora_rank, kv_lora_rank, qk_head_dim, v_head_dim) with GPTModelProvider | ||
| 464 | ++ (which provides the model instantiation logic). | ||
| 465 | ++ | ||
| 466 | ++ Model-specific defaults (normalization, activation, fusions, etc.) should be | ||
| 467 | ++ configured via MEGATRON_DEFAULTS in the respective bridge classes. | ||
| 468 | ++ | ||
| 469 | ++ Used by: | ||
| 470 | ++ - DeepSeek V2/V3 | ||
| 471 | ++ - Kimi K2 | ||
| 472 | ++ - Other MLA-based models | ||
| 473 | ++ """ | ||
| 474 | ++ transformer_layer_spec: Union["ModuleSpec", Callable[["GPTModelProvider"], "ModuleSpec"]] = partial( | ||
| 475 | ++ get_gpt_decoder_block_spec, use_transformer_engine=HAVE_TE | ||
| 476 | ++ ) | ||
| 477 | ++ | ||
| 478 | ++ pass | ||
| 479 | ++ | ||
| 480 | ++ | ||
| 481 | ++ | ||
| 482 | ++__all__ = ["GLM5ModelProvider"] | ||
| 483 | + | ||
| 484 | +diff --git a/src/megatron/bridge/models/gpt_provider.py b/src/megatron/bridge/models/gpt_provider.py | ||
| 485 | +index 6a939800..e5cac258 100644 | ||
| 486 | +--- a/src/megatron/bridge/models/gpt_provider.py | ||
| 487 | ++++ b/src/megatron/bridge/models/gpt_provider.py | ||
| 488 | + from dataclasses import dataclass, field | ||
| 489 | + from functools import partial | ||
| 490 | + from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Union | ||
| 491 | + | ||
| 492 | +-import modelopt.torch.distill as mtd | ||
| 493 | +-import modelopt.torch.distill.plugins.megatron as mtd_mcore | ||
| 494 | + import torch | ||
| 495 | + from megatron.core import parallel_state | ||
| 496 | + from megatron.core.models.gpt import GPTModel as MCoreGPTModel | ||
| 497 | +diff --git a/src/megatron/bridge/models/mla_provider.py b/src/megatron/bridge/models/mla_provider.py | ||
| 498 | +new file mode 100644 | ||
| 499 | +index 00000000..7350d7c5 | ||
| 500 | +--- /dev/null | ||
| 501 | ++++ b/src/megatron/bridge/models/mla_provider.py | ||
| 502 | + | ||
| 503 | ++# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| 504 | ++# | ||
| 505 | ++# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 506 | ++# you may not use this file except in compliance with the License. | ||
| 507 | ++# You may obtain a copy of the License at | ||
| 508 | ++# | ||
| 509 | ++# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 510 | ++# | ||
| 511 | ++# Unless required by applicable law or agreed to in writing, software | ||
| 512 | ++# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 513 | ++# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 514 | ++# See the License for the specific language governing permissions and | ||
| 515 | ++# limitations under the License. | ||
| 516 | ++ | ||
| 517 | ++"""MLA (Multi-Latent Attention) Model Provider. | ||
| 518 | ++ | ||
| 519 | ++This module provides a minimal provider for models using Multi-Latent Attention, | ||
| 520 | ++such as DeepSeek V2/V3 and Kimi K2. | ||
| 521 | ++""" | ||
| 522 | ++ | ||
| 523 | ++from dataclasses import dataclass | ||
| 524 | ++ | ||
| 525 | ++from megatron.bridge.models.gpt_provider import GPTModelProvider | ||
| 526 | ++from megatron.bridge.models.transformer_config import MLATransformerConfig | ||
| 527 | ++ | ||
| 528 | ++ | ||
| 529 | ++@dataclass | ||
| 530 | ++class MLAModelProvider(MLATransformerConfig, GPTModelProvider): | ||
| 531 | ++ """Provider for models using Multi-Latent Attention (MLA). | ||
| 532 | ++ | ||
| 533 | ++ This class combines MLATransformerConfig (which provides MLA-specific fields | ||
| 534 | ++ like q_lora_rank, kv_lora_rank, qk_head_dim, v_head_dim) with GPTModelProvider | ||
| 535 | ++ (which provides the model instantiation logic). | ||
| 536 | ++ | ||
| 537 | ++ Model-specific defaults (normalization, activation, fusions, etc.) should be | ||
| 538 | ++ configured via MEGATRON_DEFAULTS in the respective bridge classes. | ||
| 539 | ++ | ||
| 540 | ++ Used by: | ||
| 541 | ++ - DeepSeek V2/V3 | ||
| 542 | ++ - Kimi K2 | ||
| 543 | ++ - Other MLA-based models | ||
| 544 | ++ """ | ||
| 545 | ++ | ||
| 546 | ++ pass | ||
| 547 | + | ||
| 548 | +diff --git a/src/megatron/bridge/peft/utils.py b/src/megatron/bridge/peft/utils.py | ||
| 549 | +index 01a67590..ce22314b 100644 | ||
| 550 | +--- a/src/megatron/bridge/peft/utils.py | ||
| 551 | ++++ b/src/megatron/bridge/peft/utils.py | ||
| 552 | + HAVE_TE = all( | ||
| 553 | + ) | ||
| 554 | + ) | ||
| 555 | + | ||
| 556 | +-MixedFusedLayerNorm, HAVE_APEX = safe_import_from("apex.normalization.fused_layer_norm", "MixedFusedLayerNorm") | ||
| 557 | ++HAVE_APEX=False | ||
| 558 | + | ||
| 559 | + TECL = (TEColumnParallelLinear, TELayerNormColumnParallelLinear, TEColumnParallelGroupedLinear) | ||
| 560 | + TERL = (TERowParallelLinear, TERowParallelGroupedLinear) | ||
| @@ -0,0 +1,72 @@ | |||
| 1 | +diff --git a/megatron/core/models/common/embeddings/rope_utils.py b/megatron/core/models/common/embeddings/rope_utils.py | ||
| 2 | +index e39540eb1..70783770d 100644 | ||
| 3 | +--- a/megatron/core/models/common/embeddings/rope_utils.py | ||
| 4 | ++++ b/megatron/core/models/common/embeddings/rope_utils.py | ||
| 5 | + __all__ = [ | ||
| 6 | + 'fused_apply_rotary_pos_emb', | ||
| 7 | + 'fused_apply_rotary_pos_emb_thd', | ||
| 8 | + 'get_pos_emb_on_this_cp_rank', | ||
| 9 | ++ 'apply_rotary_pos_emb_bshd_in_complex', | ||
| 10 | + ] | ||
| 11 | + | ||
| 12 | + | ||
| 13 | ++def apply_rotary_pos_emb_bshd_in_complex( | ||
| 14 | ++ t: Tensor, | ||
| 15 | ++ freqs: Tensor, | ||
| 16 | ++ rotary_interleaved: bool = False | ||
| 17 | ++ ) -> Tensor: | ||
| 18 | ++ if rotary_interleaved: | ||
| 19 | ++ s, b, n, d = t.shape | ||
| 20 | ++ t = t.view(s, b, n, 2, d // 2).transpose(4, 3) | ||
| 21 | ++ freqs, _ = freqs.chunk(2, dim=-1) | ||
| 22 | ++ | ||
| 23 | ++ freqs_cis = torch.polar(torch.ones_like(freqs), freqs) | ||
| 24 | ++ | ||
| 25 | ++ x = torch.view_as_complex(t.float().view(*t.shape[:-1], -1, 2)).squeeze(-1) | ||
| 26 | ++ y = torch.view_as_real(x * freqs_cis).flatten(3) | ||
| 27 | ++ if rotary_interleaved: | ||
| 28 | ++ y = torch.cat([y[..., 0::2], y[..., 1::2]], dim=-1) | ||
| 29 | ++ | ||
| 30 | ++ return y.to(t.dtype) | ||
| 31 | ++ | ||
| 32 | ++ | ||
| 33 | + def get_pos_emb_on_this_cp_rank( | ||
| 34 | + pos_emb: Tensor, seq_dim: int, cp_group: torch.distributed.ProcessGroup | ||
| 35 | + ) -> Tensor: | ||
| 36 | +diff --git a/megatron/core/transformer/experimental_attention_variant/dsa.py b/megatron/core/transformer/experimental_attention_variant/dsa.py | ||
| 37 | +index 353b31e9b..5cc3e92f4 100644 | ||
| 38 | +--- a/megatron/core/transformer/experimental_attention_variant/dsa.py | ||
| 39 | ++++ b/megatron/core/transformer/experimental_attention_variant/dsa.py | ||
| 40 | + from megatron.core.models.common.embeddings import ( | ||
| 41 | + YarnRotaryEmbedding, | ||
| 42 | + apply_rotary_pos_emb, | ||
| 43 | + ) | ||
| 44 | ++from megatron.core.models.common.embeddings.rope_utils import apply_rotary_pos_emb_bshd_in_complex | ||
| 45 | + from megatron.core.packed_seq_params import PackedSeqParams | ||
| 46 | + from megatron.core.process_groups_config import ProcessGroupCollection | ||
| 47 | + from megatron.core.tensor_parallel.mappings import gather_from_sequence_parallel_region | ||
| 48 | + class DSAIndexer(MegatronModule): | ||
| 49 | + """Apply RoPE to the input tensor.""" | ||
| 50 | + # x_nope [seqlen, batch, *, index_head_dim - qk_pos_emb_head_dim] | ||
| 51 | + # x_pe [seqlen, batch, *, qk_pos_emb_head_dim] | ||
| 52 | +- x_nope, x_pe = torch.split( | ||
| 53 | +- x, [self.index_head_dim - self.qk_pos_emb_head_dim, self.qk_pos_emb_head_dim], dim=-1 | ||
| 54 | ++ x_pe, x_nope = torch.split( | ||
| 55 | ++ x, [self.index_head_dim - self.qk_pos_emb_head_dim,self.qk_pos_emb_head_dim], dim=-1 | ||
| 56 | + ) | ||
| 57 | +- x_pe = apply_rotary_pos_emb( | ||
| 58 | ++ x_pe = apply_rotary_pos_emb_bshd_in_complex( | ||
| 59 | + x_pe, | ||
| 60 | + rotary_pos_emb, | ||
| 61 | +- config=self.config, | ||
| 62 | +- cu_seqlens=None, | ||
| 63 | +- mscale=mscale, | ||
| 64 | +- cp_group=self.pg_collection.cp, | ||
| 65 | +- ) | ||
| 66 | ++ rotary_interleaved=True) | ||
| 67 | + # [seqlen, batch, *, index_head_dim] | ||
| 68 | +- x = torch.cat([x_nope, x_pe], dim=-1) | ||
| 69 | ++ x = torch.cat([x_pe, x_nope], dim=-1) | ||
| 70 | + return x | ||
| 71 | + | ||
| 72 | + def _compute_index_scores( | ||
| @@ -0,0 +1,13 @@ | |||
| 1 | +diff --git a/verl/trainer/ppo/ray_trainer.py b/verl/trainer/ppo/ray_trainer.py | ||
| 2 | +index e178ffc1..fa34f53a 100644 | ||
| 3 | +--- a/verl/trainer/ppo/ray_trainer.py | ||
| 4 | ++++ b/verl/trainer/ppo/ray_trainer.py | ||
| 5 | + class RayPPOTrainer: | ||
| 6 | + | ||
| 7 | + lines = [] | ||
| 8 | + for i in range(n): | ||
| 9 | +- entry = {k: v[i] for k, v in base_data.items()} | ||
| 10 | ++ entry = {k: str(v[i]) if k == "acc" else v[i] for k, v in base_data.items()} | ||
| 11 | + lines.append(json.dumps(entry, ensure_ascii=False)) | ||
| 12 | + | ||
| 13 | + with open(filename, "w") as f: | ||
| @@ -0,0 +1,13 @@ | |||
| 1 | +diff --git a/vllm_ascend/attention/sfa_v1.py b/vllm_ascend/attention/sfa_v1.py | ||
| 2 | +index f7edb5fb..33074726 100644 | ||
| 3 | +--- a/vllm_ascend/attention/sfa_v1.py | ||
| 4 | ++++ b/vllm_ascend/attention/sfa_v1.py | ||
| 5 | + class AscendSFAImpl(MLAAttentionImpl): | ||
| 6 | + # self.W_UV = maybe_trans_nz(self.W_UV) | ||
| 7 | + | ||
| 8 | + # Dispose kv_b_proj since it is replaced by W_UV and W_UK_T to save memory | ||
| 9 | +- dispose_layer(self.kv_b_proj) | ||
| 10 | ++ #dispose_layer(self.kv_b_proj) | ||
| 11 | + if self.enable_dsa_cp: | ||
| 12 | + if self.enable_dsa_cp_with_layer_shard: | ||
| 13 | + for layer in self.layer_sharding_kwargs or []: | ||