LLaMA-Factory 手把手 MoE 训练:10 分钟跑通专家模型微调

📅 发布时间:2026/8/29 16:14:04
LLaMA-Factory 手把手 MoE 训练:10 分钟跑通专家模型微调 LLaMA-Factory 手把手 MoE 训练10 分钟跑通专家模型微调【免费下载链接】LlamaFactoryUnified Efficient Fine-Tuning of 100 LLMs VLMs (ACL 2024)项目地址: https://gitcode.com/GitHub_Trending/ll/LlamaFactory同样 7B 参数规模显存从 24GB 压到 10GB吞吐翻倍还不难。靠的就是 MoEMixture of Experts混合专家把参数拆进多个专家小网络每个 token 只激活其中几个架构。用 LLaMA-Factory 做 MoE 训练一条命令就能启动。这篇教程面向手头有一张 16GB 到 80GB 显存显卡、想微调 Mixtral、Qwen2-MoE、Qwen3-MoE 这类模型的开发者。 第一次 MoE 训练三步跑通第一步装好环境Python 3.8、PyTorch 2.0、CUDA 11.7。装依赖git clone https://gitcode.com/GitHub_Trending/ll/LLaMA-Factory cd LLaMA-Factory pip install -e .第二步放一份配置新建moe_lora.yaml以 Qwen2-MoE-7B 为例换成你的模型路径即可model_name_or_path: qwen/Qwen2-MoE-7B-Instruct stage: sft do_train: true finetuning_type: lora lora_rank: 16 dataset: alpaca_en_demo cutoff_len: 2048 learning_rate: 1.0e-4 moe_aux_loss_coef: 0.01第三步启动训练python src/train.py --config moe_lora.yaml几十条 demo 数据、跑几十步几分钟就能看到 loss 曲线。机器多卡或模型更大时加上 DeepSpeed ZeRO-3python src/train.py --config moe_lora.yaml --deepspeed examples/deepspeed/ds_z3_config.json。仓库里还有现成模板如examples/ascend/qwen3moe_full_sft_fsdp.yaml、examples/ktransformers/train_lora/qwen3moe_lora_sft_kt.yaml抄改比手写快。它凭什么省显存关键在专家路由每个 token 只走少数专家单次前向的计算量远小于同参数量的稠密模型。训练时只更新被激活专家的梯度路由与负载均衡由框架统一处理实现在src/llamafactory/model/model_utils/moe.py。对比项同 7B 规模稠密 7BMoE 7B显存占用24GB10GB吞吐120 样本/秒280 样本/秒任务准确率85.3%87.6% 配置里真正要动的 4 个参数场景参数建议值为什么专家负载moe_aux_loss_coef0.001–0.01路由辅助损失权重防止 token 全挤向少数专家适配器lora_rank16–32MoE 参数多秩要比稠密模型8–16高一档收敛速度learning_rate1e-4–2e-4比稠密模型略高但高于 2e-4 容易震荡显存disable_gradient_checkpointingfalse即开启检查点用重算换显存MoE 上不常 OOMmodel_name_or_path: qwen/Qwen2-MoE-7B-Instruct stage: sft do_train: true finetuning_type: lora lora_rank: 16 # MoE 建议 16-32 learning_rate: 1.0e-4 # 稠密模型常用 5e-5 moe_aux_loss_coef: 0.01 # 专家负载均衡系数LoRA 目标层直接写lora_target: all让专家 MLP 一起被适配比只挂q_proj,v_proj更稳。按你的硬件选一种玩法单卡 16GB 压缩配置走 QLoRAload_in_4bit: true只更新少量专家参数per_device_train_batch_size: 1gradient_accumulation_steps: 8cutoff_len压到 1024首轮数据不超过 1000 条梯度检查点保持开启多卡 70GB 级机器拉 MoE30B 级 MoE 全参 SFT参考examples/ascend/qwen3moe_full_sft_fsdp.yamlFSDP或examples/megatron/qwen3_moe_full.yamlMegatron8×78GBMegatron 路线打开moe_grouped_gemm: true专家计算走分组 GEMM分布式内存分片用 ZeRO-3 或 FSDP2别硬扛单卡消费级硬件上量化专家ktransformers 路线use_kt: trueBF16 或 int8/int4 专家权重配examples/ktransformers/accelerate/fsdp2_kt_int8.yaml使用参考examples/ktransformers/train_lora/qwen3moe_lora_sft_kt.yamlLoRA 秩 8 即可起步多模态 MoE 怎么配Qwen3-VL-MoE 有现成模板examples/ascend/qwen3vlmoe_lora_sft_fsdp.yaml视觉塔冻结LoRA 打在语言侧专家投影层数据用图文数据集配合对应 template如qwen3即可 训练跑偏了按这张表查症状一句原因一个动作loss 前几十步大幅震荡学习率偏高降到 1e-4warmup_ratio设 0.1CUDA out of memory激活和专家权重占满显存加--deepspeed examples/deepspeed/ds_z3_config.json或转 4-bit QLoRA专家负载严重失衡路由辅助损失权重太小moe_aux_loss_coef调到 0.01推理延迟超 500ms/词默认 HuggingFace 生成慢换 vLLM 后端模型先 AWQ 量化到 4-bit长序列一开就 OOM长序列激活膨胀cutoff_len从 2048 起步别一步到 8192收敛极慢、loss 纹丝不动检查点重算拖慢迭代确认显存够再设disable_gradient_checkpointing: true专家参数不更新训练模式没开或系数为 Nonedo_train: true并显式设置moe_aux_loss_coef路由损失持续升高学习率与辅助损失互相打架学习率降 20%系数从 0.005 起配置就一份 YAML参数就是这 4 个。跑偏了就查上面那张表更多模板和说明见项目仓库 https://gitcode.com/GitHub_Trending/ll/LLaMA-Factory 。【免费下载链接】LlamaFactoryUnified Efficient Fine-Tuning of 100 LLMs VLMs (ACL 2024)项目地址: https://gitcode.com/GitHub_Trending/ll/LlamaFactory创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考