ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

LlamaFactory 示例实战指南:LoRA、QLoRA、全参微调、模型导出与推理部署

LlamaFactory 示例实战指南:LoRA、QLoRA、全参微调、模型导出与推理部署 LlamaFactory 示例实战指南LoRA、QLoRA、全参微调、模型导出与推理部署【免费下载链接】LlamaFactoryUnified Efficient Fine-Tuning of 100 LLMs VLMs (ACL 2024)项目地址: https://gitcode.com/GitHub_Trending/ll/LlamaFactoryLlamaFactory 在 examples 目录下维护了一套覆盖主流微调范式的完整示例集合从 LoRA、QLoRA 到全参微调、偏好对齐训练再到 LoRA 合并、GPTQ 量化导出与多后端推理部署。本文以 examples/README.md 为骨架逐条解读其中的命令与配置并结合仓库内真实 YAML 配置文件与源码入口帮助你在单卡、多卡、多机及 NPU 环境下直接复制可用的训练与部署方案。一、基本用法与参数覆盖机制所有示例命令都要求在 LlamaFactory 项目根目录下执行。CLI 入口是llamafactory-cli其实现位于 src/llamafactory/cli.py根据环境变量USE_V1决定分发到新版还是常规 launcher因此同一套命令在新旧两套训练引擎间保持兼容。指定计算设备的方式是环境变量GPU 使用CUDA_VISIBLE_DEVICESNPU昇腾使用ASCEND_RT_VISIBLE_DEVICES不设置时默认使用全部可见设备。基础用法——传入一个 YAML 配置文件即可启动训练llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml进阶用法——命令行参数可以覆盖 YAML 中的同名配置例如调整学习率与日志步数CUDA_VISIBLE_DEVICES0,1 llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml \ learning_rate1e-5 \ logging_steps1也可以直接执行封装好的 shell 脚本bash examples/train_lora/qwen3_lora_sft.sh配置文件结构解析以 examples/train_lora/qwen3_lora_sft.yaml 为例仓库中的示例配置统一分为 model / method / dataset / output / train / eval 六个区块理解这一结构后所有示例配置都可举一反三### model model_name_or_path: Qwen/Qwen3-4B-Instruct-2507 trust_remote_code: true ### method stage: sft # 训练阶段 do_train: true finetuning_type: lora # lora / full / freeze lora_rank: 8 lora_target: all # 对全部线性层注入 LoRA ### dataset dataset: identity,alpaca_en_demo # 在 data/dataset_info.json 中注册的数据集 template: qwen3_nothink # 对话模板 cutoff_len: 2048 # 最大截断长度 max_samples: 1000 # 演示用限制样本数 preprocessing_num_workers: 16 ### output output_dir: saves/qwen3-4b/lora/sft logging_steps: 10 save_steps: 500 plot_loss: true report_to: none # choices: [none, wandb, tensorboard, swanlab, mlflow] ### train per_device_train_batch_size: 1 gradient_accumulation_steps: 8 learning_rate: 1.0e-4 num_train_epochs: 3.0 lr_scheduler_type: cosine warmup_ratio: 0.1 bf16: true ### eval # eval_dataset: alpaca_en_demo # val_size: 0.1 # eval_strategy: steps # eval_steps: 500几个值得注意的细节dataset字段引用的数据集名必须先在 data/dataset_info.json 中注册仓库内置了identity.json、alpaca_en_demo.json、dpo_en_demo.json、kto_en_demo.json等演示数据report_to支持 wandb、tensorboard、swanlab、mlflow 等多种实验追踪后端eval 区块默认注释取消注释即可在训练中进行周期性评估。二、LoRA 微调全场景继续预训练Continual Pre-Trainingllamafactory-cli train examples/train_lora/qwen3_lora_pretrain.yaml监督微调SFTllamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml多模态监督微调使用 Qwen3-VL 的视觉语言模型做 LoRA SFTllamafactory-cli train examples/train_lora/qwen3vl_lora_sft.yaml偏好对齐训练DPO / ORPO / SimPO同一份配置文件即可切换三种偏好优化损失见 examples/train_lora/qwen3_lora_dpo.yaml 中的关键参数llamafactory-cli train examples/train_lora/qwen3_lora_dpo.yamlstage: dpo pref_beta: 0.1 # DPO 的 KL 惩罚系数 pref_loss: sigmoid # choices: [sigmoid (dpo), orpo, simpo] dataset: dpo_en_demo # 偏好对数据 learning_rate: 5.0e-6 # 偏好训练常用更小学习率多模态场景同理llamafactory-cli train examples/train_lora/qwen3vl_lora_dpo.yaml奖励建模与 KTOllamafactory-cli train examples/train_lora/qwen3_lora_reward.yaml # 奖励模型 llamafactory-cli train examples/train_lora/qwen3_lora_kto.yaml # KTO 训练大数据集预处理tokenized_path对于大规模数据集先单独执行一次训练配置完成 tokenization 落盘之后再通过配置中的tokenized_path直接加载预处理结果可显著节省启动时间。参见 examples/train_lora/qwen3_preprocess.yamlllamafactory-cli train examples/train_lora/qwen3_preprocess.yaml### dataset dataset: identity,alpaca_en_demo template: qwen3_nothink cutoff_len: 2048 tokenized_path: saves/qwen3-4b/dataset/sft # 预处理数据落盘路径多机 LoRA SFT使用FORCE_TORCHRUN1启用 torchrun 启动方式并在两台机器上分别执行各自设置不同的NODE_RANKFORCE_TORCHRUN1 NNODES2 NODE_RANK0 MASTER_ADDR192.168.0.1 MASTER_PORT29500 llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml FORCE_TORCHRUN1 NNODES2 NODE_RANK1 MASTER_ADDR192.168.0.1 MASTER_PORT29500 llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml其中MASTER_ADDR/MASTER_PORT是节点 0 的地址与端口各节点需保证网络互通。DeepSpeed ZeRO-3 权重分片LoRA 训练同样可以启用 DeepSpeed ZeRO-3 进行权重分片配置见 examples/train_lora/qwen3_lora_sft_ds3.yaml与基础 LoRA 配置的差异仅在method区块中多出一行FORCE_TORCHRUN1 llamafactory-cli train examples/train_lora/qwen3_lora_sft_ds3.yamldeepspeed: examples/deepspeed/ds_z3_config.json # choices: [ds_z0_config.json, ds_z2_config.json, ds_z3_config.json]仓库在 examples/deepspeed/ 下提供了 ds_z0、ds_z2、ds_z3 及多种 offload 变体的现成配置可直接替换引用。基于 Ray 的多 GPU 训练通过USE_RAY1环境变量启用 Ray 启动器见 examples/train_lora/qwen3_lora_sft_ray.yamlUSE_RAY1 llamafactory-cli train examples/train_lora/qwen3_lora_sft_ray.yaml### ray ray_num_workers: 4 # Number of GPUs to use. # ray_init_kwargs: # runtime_env: # env_vars: # YOUR-ENV-VAR-HERE: YOUR-ENV-VAR-HERE # pip: # - emojiRay 方案还支持dataset_dir: REMOTE:llamafactory/demo_data这种远端数据源写法适合多机共享同一数据集的场景。三、QLoRA 量化微调在 4-bit/8-bit 量化基座上训练 LoRA可将显存需求压缩到消费级 GPU 可承受的范围。推荐方式是 Bitsandbytes/HQQ/EETQ 在线量化示例为 examples/train_qlora/qwen3_lora_sft_otfq.yamlllamafactory-cli train examples/train_qlora/qwen3_lora_sft_otfq.yaml其核心是 model 区块的两个量化参数quantization_bit: 4 # choices: [8 (bnb/hqq/eetq), 4 (bnb/hqq), 3 (hqq), 2 (hqq)] quantization_method: bnb # choices: [bnb, hqq, eetq]其余字段lora_rank、lora_target、数据集、训练超参与非量化 LoRA 配置一致。此外还有针对特定量化格式的变体# 昇腾 NPU 上的 4-bit Bitsandbytes 量化 SFT llamafactory-cli train examples/train_qlora/qwen3_lora_sft_bnb_npu.yaml # 4/8-bit GPTQ 量化模型上的 SFT llamafactory-cli train examples/train_qlora/llama3_lora_sft_gptq.yaml # 4-bit AWQ 量化模型上的 SFT llamafactory-cli train examples/train_qlora/llama3_lora_sft_awq.yaml # 2-bit AQLM 量化模型上的 SFT llamafactory-cli train examples/train_qlora/llama3_lora_sft_aqlm.yamlGPTQ/AWQ/AQLM 要求基座模型本身已经是该格式导出好的量化权重如 Qwen/Qwen3-4B-GPTQ-4bit 等与 bnb/hqq/eetq 的运行时在线量化不同各格式的额外依赖分别在 requirements/gptq.txt、requirements/aqlm.txt 等文件中声明安装时需要单独拉取。四、全参微调单节点全参 SFT全参训练对显存需求最高官方示例默认搭配 DeepSpeed ZeRO-3见 examples/train_full/qwen3_full_sft.yaml。与 LoRA 版本相比关键差异是finetuning_type: full、去掉了 LoRA 参数并引入 DeepSpeed 配置学习率也从 1e-4 降到 1e-5全参微调的常规量级FORCE_TORCHRUN1 llamafactory-cli train examples/train_full/qwen3_full_sft.yaml### method stage: sft do_train: true finetuning_type: full deepspeed: examples/deepspeed/ds_z3_config.json ### train gradient_accumulation_steps: 2 # 比 LoRA 示例更小的累积步数 learning_rate: 1.0e-5多机全参 SFTFORCE_TORCHRUN1 NNODES2 NODE_RANK0 MASTER_ADDR192.168.0.1 MASTER_PORT29500 llamafactory-cli train examples/train_full/qwen3_full_sft.yaml FORCE_TORCHRUN1 NNODES2 NODE_RANK1 MASTER_ADDR192.168.0.1 MASTER_PORT29500 llamafactory-cli train examples/train_full/qwen3_full_sft.yaml弹性容错多机训练在至少MIN_NNODES台、至多MAX_NNODES台机器上执行同一条命令配合MAX_RESTARTS失败重试次数即可启动弹性作业RDZV_ID是所有参与节点共享的唯一作业 ID。参数语义与 torchrun 弹性启动文档一致FORCE_TORCHRUN1 MIN_NNODES1 MAX_NNODES3 MAX_RESTARTS3 RDZV_IDllamafactory MASTER_ADDR192.168.0.1 MASTER_PORT29500 llamafactory-cli train examples/train_full/qwen3_full_sft.yaml多模态全参 SFTFORCE_TORCHRUN1 llamafactory-cli train examples/train_full/qwen3vl_full_sft.yaml五、LoRA 合并与模型量化导出合并 LoRA 适配器注意合并 LoRA 时不要使用量化模型也不要设置quantization_bit。llamafactory-cli export examples/merge_lora/qwen3_lora_sft.yaml导出配置 examples/merge_lora/qwen3_lora_sft.yaml 的核心字段model_name_or_path: Qwen/Qwen3-4B-Instruct-2507 adapter_name_or_path: saves/qwen3-4b/lora/sft # 训练产出的 LoRA 适配器 template: qwen3_nothink ### export export_dir: saves/qwen3_sft_merged export_size: 5 # 按该大小分片保存 export_device: cpu # choices: [cpu, auto]CPU 合并可节省显存 export_legacy_format: false使用 AutoGPTQ 量化导出将全精度模型导出为 GPTQ 4-bit 量化权重见 examples/merge_lora/qwen3_gptq.yamlllamafactory-cli export examples/merge_lora/qwen3_gptq.yaml### export export_dir: saves/qwen3_gptq export_quantization_bit: 4 export_quantization_dataset: data/c4_demo.jsonl # 量化校准数据 export_size: 5 export_device: cpu export_legacy_format: falseexport_quantization_dataset指定的校准集用于统计权重量化的数值分布仓库内置的 data/c4_demo.jsonl 可直接使用。导出 Ollama modelfile对已导出的全精度 checkpoint 运行 export可生成 Ollama 所需的 modelfile便于将自训模型注册到本地 Ollama 服务llamafactory-cli export examples/merge_lora/qwen3_full_sft.yaml其配置 examples/merge_lora/qwen3_full_sft.yaml 中model_name_or_path指向训练产出的全参 checkpoint 目录saves/qwen3-4b/full/sft其余 export 字段与上述一致。六、推理与部署微调后的模型使用 vLLM 多 GPU 批量评测先用 vLLM 批量生成预测再用评测脚本计算 BLEU/ROUGEpython scripts/vllm_infer.py --model_name_or_path Qwen/Qwen3-4B-Instruct-2507 --template qwen3_nothink --dataset alpaca_en_demo python scripts/eval_bleu_rouge.py generated_predictions.jsonl对应脚本为 scripts/vllm_infer.py 与 scripts/eval_bleu_rouge.py。CLI / Web UI 聊天框llamafactory-cli chat examples/inference/qwen3_lora_sft.yaml # 终端聊天 llamafactory-cli webchat examples/inference/qwen3_lora_sft.yaml # Gradio Web UI启动 OpenAI 风格 APIllamafactory-cli api examples/inference/qwen3_lora_sft.yaml三种部署方式共用同一份推理配置 examples/inference/qwen3_lora_sft.yamlmodel_name_or_path: Qwen/Qwen3-4B-Instruct-2507 adapter_name_or_path: saves/qwen3-4b/lora/sft template: qwen3_nothink infer_backend: huggingface # choices: [huggingface, vllm, sglang, ktransformers] trust_remote_code: true其中infer_backend可切换为vllm或sglang以获得更高吞吐仓库内 src/llamafactory/chat/vllm_engine.py、src/llamafactory/chat/sglang_engine.py 分别是对应推理引擎的实现vLLM/SGLang 的额外依赖在 requirements/vllm.txt 与 requirements/sglang.txt 中声明。七、进阶训练方法Extrasexamples/extras/ 目录汇集了若干论文级训练技巧的落地配置每个方法都有独立的 requirements 文件安装对应依赖后即可直接训练# 显存高效的低秩梯度优化器 llamafactory-cli train examples/extras/galore/llama3_full_sft.yaml # GaLore llamafactory-cli train examples/extras/apollo/llama3_full_sft.yaml # APOLLO llamafactory-cli train examples/extras/badam/llama3_full_sft.yaml # BAdam批量自适应参数微调 llamafactory-cli train examples/extras/adam_mini/qwen2_full_sft.yaml # Adam-mini llamafactory-cli train examples/extras/muon/qwen2_full_sft.yaml # Muon 优化器 llamafactory-cli train examples/extras/loraplus/llama3_lora_sft.yaml # LoRA差异化学习率 llamafactory-cli train examples/extras/pissa/llama3_lora_sft.yaml # PiSSASVD 初始化 LoRA llamafactory-cli train examples/extras/mod/llama3_full_sft.yaml # Mixture-of-Depths # LLaMA-Pro先扩展层数再冻结微调 bash examples/extras/llama_pro/expand.sh llamafactory-cli train examples/extras/llama_pro/llama3_freeze_sft.yaml # FSDP QLoRA需先运行初始化脚本 bash examples/extras/fsdp_qlora/train.sh llamafactory-cli train examples/extras/oft/llama3_oft_sft.yaml # OFT正交微调 llamafactory-cli train examples/extras/qoft/llama3_oft_sft_bnb_npu.yaml # QOFT量化正交微调NPU这些方法对应的可选依赖声明在 requirements/ 目录下如 requirements/galore.txt、requirements/apollo.txt、requirements/badam.txt、requirements/adam-mini.txt 等其中 Muon 优化器的实现位于 src/llamafactory/third_party/muon/LLaMA-Pro 的层扩展脚本为 scripts/llama_pro.py。八、延伸阅读仓库内还有若干未在本文命令清单中展开的示例目录可按需查看examples/accelerate/FSDP / FSDP2 单文件加速配置含 Qwen3.5 MoE 变体examples/ascend/昇腾 NPU 上的 Qwen3 系列 FSDP/FSDP2 训练示例examples/megatron/ 与 examples/megatron_bridge/Megatron 大规模并行训练配置examples/ktransformers/CPUGPU 混合推理框架下的 MoE 量化训练examples/v1/新版v1训练引擎的批处理策略、冻结训练与 FSDP2 配置examples/merge_lora/qwen3_gptq.yaml 等 merge 目录下的多模型导出示例。总结examples/README.md 提供了 LlamaFactory 从数据预处理、LoRA/QLoRA/全参训练、偏好对齐到适配器合并、GPTQ 量化导出与多后端推理部署的完整命令清单。仓库中每个命令都对应一份可直接运行的 YAML 配置且统一遵循「命令行参数覆盖 YAML 字段」的机制——修改examples/train_lora/qwen3_lora_sft.yaml中的数据集、模板与训练超参即可适配你自己的微调任务。建议从 LoRA SFT 示例起步跑通后再按需切换到 QLoRA 降低显存、全参 DeepSpeed 提升上限或进入 Extras 探索论文级训练技巧。【免费下载链接】LlamaFactoryUnified Efficient Fine-Tuning of 100 LLMs VLMs (ACL 2024)项目地址: https://gitcode.com/GitHub_Trending/ll/LlamaFactory创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表