Skip to content

Conversation

@Jintao-Huang
Copy link
Collaborator

@Jintao-Huang Jintao-Huang commented Jun 30, 2025

在开始微调之前,请确保您的环境已准备妥当。

对megatron相关依赖的安装可以查看megatron-swift训练文档(可直接使用镜像):https://swift.readthedocs.io/zh-cn/latest/Instruction/Megatron-SWIFT%E8%AE%AD%E7%BB%83.html

git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
pip install -e .

微调数据集准备格式如下(system字段可选),在训练脚本中指定--dataset <dataset_path>即可。

{"messages": [{"role": "user", "content": "浙江的省会在哪?"}, {"role": "assistant", "content": "浙江的省会在杭州。"}]}
  1. HF格式的权重转为Megatron格式,并测试转换精度:
# 4 * 20GiB
CUDA_VISIBLE_DEVICES=0,1,2,3 \
swift export \
    --model PaddlePaddle/ERNIE-4.5-21B-A3B-PT \
    --to_mcore true \
    --torch_dtype bfloat16 \
    --output_dir ERNIE-4.5-21B-A3B-PT-mcore \
    --test_convert_precision true

image

  1. 对ERNIE-4.5-21B-A3B-PT-mcore进行自我认知微调(全参数训练)。在4卡A800上所需显存资源为:4 * 51GiB,训练速度为16s/it。该脚本只是方便跑通测试,建议更换更好的通用数据集进行混合。
# 4 * 51GiB, 16s/it
CUDA_VISIBLE_DEVICES=0,1,2,3 \
megatron sft \
    --load ERNIE-4.5-21B-A3B-PT-mcore \
    --dataset 'AI-ModelScope/alpaca-gpt4-data-zh#500' \
              'AI-ModelScope/alpaca-gpt4-data-en#500' \
              'swift/self-cognition#500' \
    --expert_model_parallel_size 4 \
    --moe_grouped_gemm true \
    --moe_shared_expert_overlap true \
    --moe_aux_loss_coeff 0.01 \
    --micro_batch_size 4 \
    --global_batch_size 16 \
    --recompute_granularity full \
    --recompute_method uniform \
    --recompute_num_layers 1 \
    --finetune true \
    --cross_entropy_loss_fusion true \
    --lr 1e-5 \
    --lr_warmup_fraction 0.05 \
    --min_lr 1e-6 \
    --save megatron_output/ERNIE-4.5-21B-A3B-PT \
    --eval_interval 100 \
    --save_interval 100 \
    --max_length 2048 \
    --max_epochs 1 \
    --num_workers 8 \
    --dataset_num_proc 8 \
    --no_save_optim true \
    --no_save_rng true \
    --sequence_parallel true \
    --optimizer_cpu_offload true \
    --use_precision_aware_optimizer true \
    --attention_backend flash \
    --model_author swift \
    --model_name swift-robot

训练显存占用:
image

训练日志:
image

  1. 将Megatron格式权重转为HF格式,并测试转换精度:
CUDA_VISIBLE_DEVICES=0,1,2,3 \
swift export \
    --mcore_model megatron_output/ERNIE-4.5-21B-A3B-PT/vx-xxx \
    --to_hf true \
    --torch_dtype bfloat16 \
    --output_dir megatron_output/ERNIE-4.5-21B-A3B-PT/vx-xxx-hf \
    --test_convert_precision true

image

  1. 训练完成后,使用以下命令进行推理:
CUDA_VISIBLE_DEVICES=0,1,2,3 \
swift infer \
    --model output/vx-xxx/checkpoint-xxx \
    --stream true \
    --temperature 0 \
    --max_new_tokens 512

image

  1. 推送模型到ModelScope:
swift export \
    --model output/vx-xxx/checkpoint-xxx \
    --push_to_hub true \
    --hub_model_id '<your-model-id>' \
    --hub_token '<your-sdk-token>'

@Jintao-Huang Jintao-Huang merged commit 76c56b0 into modelscope:main Jun 30, 2025
1 of 2 checks passed
@Jintao-Huang Jintao-Huang changed the title support ERNIE [model] support ERNIE-4.5 Jul 2, 2025
@xudongguan202
Copy link

Ernie必须用megatron训练吗,我用普通的sft跑报错了,提示不支持这个config类型

@Jintao-Huang
Copy link
Collaborator Author

是可以训练的。提个issue吧,附带报错信息

@MJy1023
Copy link

MJy1023 commented Sep 23, 2025

请问hf2mcore转换权重过程中是否会把moe gate的fp32参数转为bf16类型

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants