(LLaMa Factory)大模型训练方法--监督微调(Qwen2-0.5B)

1、准备训练数据SFT 的数据格式有多种,例如:Alpaca格式、OpenAI格式等。

复制代码
#其中Alpaca格式如下:

[
  {
    "instruction":"human instruction (required)",
    "input":"human input (optional)",
    "output":"model response (required)",
    "system":"system prompt (optional)",
    "history":[
      [
"human instruction in the first round (optional)","model response in the first round (optional)"
      ],
      [
"human instruction in the second round (optional)","model response in the second round (optional)"
      ]
    ]
  }
]

根据以上的数据格式,我们在ModelScope的数据集找到中文医疗对话数据-Chinese-medical-dialogue符合上述格式。

复制代码
# 使用git命令拉取数据集 至data目录下
git clone https://www.modelscope.cn/datasets/xiaofengalg/Chinese-medical-dialogue.git /mnt/workspace/LLaMA-Factory/data

注册自定义数据 : 在dataset_info.json中添加如下数据集:

复制代码
"custom_sft_train_data":{
"file_name":"Chinese-medical-dialogue/data/train_0001_of_0001.json",
"columns":{
"prompt":"instruction",
"query":"input",
"response":"output"
 }
},

2、配置训练参数

复制代码
• Model name: Qwen2-0.5B
• Model path: saves/Qwen2-0.5B/full/Qwen2_pretrain_output_demo1
• Finetuning method: full
• Stage : Supervised Fine-Tuning
• Dataset: custom_sft_train_data
• Output dir: Qwen2_sft_output_demo1

配置参数说明:

复制代码
• Model path:我们选择第1阶段预训练模型的输出目录。
• Stage:这一阶段,因为我们要进行微调,选择Supervised Fine-Tuning。
• Output dir: 更换一个新的输出路径,以便后续开展第3阶段训练,例如:Qwen2_sft_output_demo1

3、启动训练 : 点击Preview Command预览命令行 , 命令行确认无误后,点击Start即可开启训练。

复制代码
llamafactory-cli train 
--stage sft 
--do_train True
--model_name_or_path saves/Qwen2-0.5B/full/Qwen2_pretrain_output_demo1
--preprocessing_num_workers 16
--finetuning_type full 
--template default 
--flash_attn auto 
--dataset_dir data 
--dataset custom_sft_train_data 
--cutoff_len 1024
--learning_rate 5e-05
--num_train_epochs 3.0
--max_samples 100000
--per_device_train_batch_size 2
--gradient_accumulation_steps 8
--lr_scheduler_type cosine 
--max_grad_norm 1.0
--logging_steps 5  #每 5 步记录一次训练日志。
--save_steps 100   #每 100 步保存一次模型检查点。这里最好将save_steps 设置大一点,否则训练过程会生成非常多的训练日志,导致硬盘空间不足而训练终止。
--warmup_steps 0
--optim adamw_torch 
--packing False
--report_to none 
--output_dir saves/Qwen2-0.5B/full/Qwen2_sft_output_demo1
--bf16 True
--plot_loss True
--ddp_timeout 180000000
--include_num_input_tokens_seen True 

训练过程中,记得实时关注资源的消耗情况:

  • 显存:使用watch -n 1 nvidia-smi 实时查看显存开销。

  • 硬盘:使用watch -n 1 df -h /mnt 实施查看/mnt分区的磁盘使用情况。

历时6小时58分钟后,模型终于训练完毕。(配置的1张4090的卡,显存24GB)

复制代码
# 验证模型

1. 在LLaMA Factory的WebUI界面上,切换至Chat界面
2. Model path: 输入刚才训练模型的输出目录,即saves/Qwen2-0.5B/full/Qwen2_sft_output_demo1
3. 其他配置保持默认不变;
4. 点击Load model,待模型加载成功后,输入看病相关的信息,测试模型的能力。

6、总结

LLaMA-Factory是一个开源的、可自定义的、可扩展的、可部署的、可训练的大模型训练平台。

训练过程的大致步骤为:

  • 按照LLaMA-Factory官方README文档的数据格式,准备训练数据;

  • 按照LlaMA-Factory官方README文档,在的dataset_info.json文件,注册自定义数据

  • 根据训练阶段配置训练参数,包括模型名称、模型路径、训练方法、数据集、输出目录等;

  • 预览训练命名无误后,启动训练。

  • 如果启动训练失败,可以通过切换到启动LLaMA Factory的命令行查看日志信息排查问题。

相关推荐
晨尘光2 天前
在Windows下编译出llama_cpp_python的DLL后,在虚拟环境中使用方法
python·llama
风筝超冷5 天前
LLaMA-Factory - 批量推理(inference)的脚本
llama
bluebonnet276 天前
【agent开发】部署LLM(一)
python·llama
阿牛大牛中7 天前
LLaDa——基于 Diffusion 的大语言模型 打平 LLama 3
人工智能·语言模型·llama
Lilith的AI学习日记7 天前
【AI面试秘籍】| 第25期:RAG的关键痛点及解决方案深度解析
人工智能·深度学习·机器学习·chatgpt·aigc·llama
LChuck9 天前
【大模型微调】魔搭社区GPU进行LLaMA-Factory微调大模型自我认知
人工智能·语言模型·自然语言处理·nlp·llama·魔搭社区·modelscope
燕双嘤9 天前
Fine-tuning:微调技术,训练方式,LLaMA-Factory,ms-swift
llama
装不满的克莱因瓶12 天前
【小白AI教程】大模型知识扫盲通识
人工智能·数学建模·ai·大模型·llm·llama·rag
TGITCIC14 天前
英伟达破局1000 Token/秒!Llama 4以光速重塑AI推理边界
人工智能·大模型·llama·英伟达·大模型速度·ai赛道·大模型基座
天天爱吃肉821815 天前
【 大模型技术驱动智能网联汽车革命:关键技术解析与未来趋势】
语言模型·汽车·llama