llamafactory-cli train \
--stage sft \
--do_train True \
--model_name_or_path Qwen/Qwen2.5-1.5B-Instruct \
--preprocessing_num_workers 16 \
--finetuning_type lora \
--template qwen \
--flash_attn disabled \
--dataset_dir data \
--dataset my_pay_alpaca \
--cutoff_len 2048 \
--learning_rate 5e-05 \
--num_train_epochs 30 \
--max_samples 1000 \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 8 \
--lr_scheduler_type cosine \
--max_grad_norm 1.0 \
--logging_steps 5 \
--save_steps 100 \
--warmup_steps 0 \
--packing False \
--report_to none \
--output_dir saves/Qwen2.5-1.5B-Instruct/lora/train_2025-06-13-001 \
--bf16 True \
--plot_loss True \
--trust_remote_code True \
--ddp_timeout 180000000 \
--include_num_input_tokens_seen True \
--optim adamw_torch \
--lora_rank 8 \
--lora_alpha 16 \
--lora_dropout 0 \
--overwrite_output_dir True \
--lora_target all
命令结构分解
llamafactory-cli train \
--stage sft \
--do_train True \
--model_name_or_path Qwen/Qwen2.5-1.5B-Instruct \
...
模型与训练类型
参数 |
说明 |
--stage sft |
当前训练阶段为 监督微调(Supervised Fine-Tuning) |
--do_train True |
执行训练任务 |
--model_name_or_path Qwen/Qwen2.5-1.5B-Instruct |
基础模型,来自 HuggingFace 的 Qwen2.5 系列(带指令微调能力) |
--template qwen |
使用 Qwen 专属 prompt 模板(例如加 system、user、assistant 格式) |
--trust_remote_code True |
允许使用模型的自定义代码(Qwen 必须加) |
LoRA 微调参数
参数 |
说明 |
--finetuning_type lora |
使用 LoRA 微调,显存占用低 |
--lora_rank 8 |
LoRA 的秩,控制参数规模(通常是 4/8/16) |
--lora_alpha 16 |
LoRA 的缩放因子(一般 = 2 × rank) |
--lora_dropout 0 |
Dropout 防止过拟合,这里设为 0 表示关闭 |
--lora_target all |
对所有可适配层做 LoRA,若只调 attention 可指定为 q_proj,v_proj |
数据集与预处理
参数 |
说明 |
--dataset_dir data |
数据目录是 data/ |
--dataset my_pay_alpaca |
数据集名称为 my_pay_alpaca ,应在 data/my_pay_alpaca 下存在 JSON 文件 |
--preprocessing_num_workers 16 |
用于数据预处理的线程数,提高加载速度 |
--cutoff_len 2048 |
截断输入长度,防止超出模型上下文 |
训练细节控制
参数 |
说明 |
--num_train_epochs 30 |
训练轮数设为 30 |
--max_samples 1000 |
最多读取 1000 条数据(调试用) |
--per_device_train_batch_size 1 |
每张卡每步训练 1 条样本(配合累积) |
--gradient_accumulation_steps 8 |
梯度累积 8 次,相当于总 batch_size = 8 |
--learning_rate 5e-5 |
初始学习率 |
--lr_scheduler_type cosine |
使用余弦退火调度器 |
--max_grad_norm 1.0 |
梯度裁剪,避免梯度爆炸 |
--warmup_steps 0 |
热身步数为 0 |
日志与可视化
参数 |
说明 |
--logging_steps 5 |
每 5 步输出一次训练日志 |
--save_steps 100 |
每 100 步保存一次 checkpoint |
--plot_loss True |
训练完后绘制 loss 曲线图 |
--report_to none |
不上报到 WandB 或 TensorBoard,可换为 wandb |
--include_num_input_tokens_seen True |
输出已训练 token 总数 |
资源配置
参数 |
说明 |
--flash_attn disabled |
禁用 Flash Attention(默认 Qwen 用不了) |
--bf16 True |
启用 bfloat16 训练(需硬件支持) |
--ddp_timeout 180000000 |
分布式训练超时时间设置(可防止初始化过早失败) |
输出控制
参数 |
说明 |
--output_dir saves/Qwen2.5-1.5B-Instruct/lora/train_2025-06-13-001 |
输出保存路径(权重等都会保存在这里) |
--overwrite_output_dir True |
如果目录存在,则覆盖原内容 |
--packing False |
不开启多样本合并,适合非 packed 格式数据(默认适合指令式数据) |
总结要点
这条命令的设置目标是:
- 用 Qwen2.5-1.5B-Instruct 做指令微调
- 使用自己的
my_pay_alpaca
数据
- 使用 LoRA(8 rank,显存友好)
- 总 batch size 为 8(1 × 8)
- 限制最多 1000 条数据做训练(用于验证训练流程)
- 日志频繁输出 + loss 可视化
- 适配 Qwen 的 tokenizer 和格式