LLama-Factory大模型训练框架,基于自己数据集微调qwen7B模型实战

news/2024/7/8 2:28:57 标签: llama

一,项目简介

LLama-Factory,大模型训练框架,支持多种模型,多种训练方式,

项目github地址:link

项目特色

  • 多种模型:LLaMA、LLaVA、Mistral、Mixtral-MoE、Qwen、Yi、Gemma、Baichuan、ChatGLM、Phi 等等。
  • 集成方法:(增量)预训练、(多模态)指令监督微调、奖励模型训练、PPO 训练、DPO 训练、KTO 训练、ORPO 训练等等。
  • 多种精度:16 比特全参数微调、冻结微调、LoRA 微调和基于 AQLM/AWQ/GPTQ/LLM.int8/HQQ/EETQ 的 2/3/4/5/6/8 比特 QLoRA 微调。
  • 先进算法:GaLore、BAdam、DoRA、LongLoRA、LLaMA Pro、Mixture-of-Depths、LoRA+、LoftQ、PiSSA 和 Agent 微调。
  • 实用技巧:FlashAttention-2、Unsloth、RoPE scaling、NEFTune 和 rsLoRA。
  • 实验监控:LlamaBoard、TensorBoard、Wandb、MLflow 等等。
  • 极速推理:基于 vLLM 的 OpenAI 风格 API、浏览器界面和命令行接口。

二, 支持训练模型以及地址

或者去魔搭社区,是真的快

link

模型名模型大小Template
Baichuan 27B/13Bbaichuan2
BLOOM/BLOOMZ560M/1.1B/1.7B/3B/7.1B/176B-
ChatGLM36Bchatglm3
Command R35B/104Bcohere
DeepSeek (Code/MoE)7B/16B/67B/236Bdeepseek
Falcon7B/11B/40B/180Bfalcon
Gemma/Gemma 2/CodeGemma2B/7B/9B/27Bgemma
GLM-49Bglm4
InternLM27B/20Bintern2
Llama7B/13B/33B/65B-
Llama 27B/13B/70Bllama2
Llama 38B/70Bllama3
LLaVA-1.57B/13Bvicuna
Mistral/Mixtral7B/8x7B/8x22Bmistral
OLMo1B/7B-
PaliGemma3Bgemma
Phi-1.5/Phi-21.3B/2.7B-
Phi-34B/7B/14Bphi
Qwen/Qwen1.5/Qwen2 (Code/MoE)0.5B/1.5B/4B/7B/14B/32B/72B/110Bqwen
StarCoder 23B/7B/15B-
XVERSE7B/13B/65Bxverse
Yi/Yi-1.56B/9B/34Byi
Yi-VL6B/34Byi_vl
Yuan 22B/51B/102Byuan

三,硬件依赖

* 估算值

方法精度7B13B30B70B110B8x7B8x22B
FullAMP120GB240GB600GB1200GB2000GB900GB2400GB
Full1660GB120GB300GB600GB900GB400GB1200GB
Freeze1620GB40GB80GB200GB360GB160GB400GB
LoRA/GaLore/BAdam1616GB32GB64GB160GB240GB120GB320GB
QLoRA810GB20GB40GB80GB140GB60GB160GB
QLoRA46GB12GB24GB48GB72GB30GB96GB
QLoRA24GB8GB16GB24GB48GB18GB48GB

四,安装环境和训练实战

4.1 环境安装
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]"
4.2 构建自己的数据集
[{
	"input": "2023年3月16日14时55分许,鄂温克族自治旗伊敏河镇发生一起一般事故,造成一人死亡,直接经济损失人民币200万元。",
	"output": "任务1:“是”,原文中提到了负面新闻,这些词汇与负面新闻相关。任务2:“不是”,由于原文没有提到了负面情绪,这和没有关系,因此不是。",
	"instruction": "你是一个舆情分析专家,擅长分析一段文字的舆情类型。现在请你判断下述语句,任务1,是否与负面新闻,你的回答 只能从是或不是选择一个,任务2,是否与负面情绪,你的回答 只能从是或不是中选择一个。例如:待判断语句:今天合肥的天气真好。你的回复:1,不是,2,不是。现在待判断语句为:{}"
}]

解析:在指令监督微调时,instruction 列对应的内容会与 input 列对应的内容拼接后作为人类指令,即人类指令为 instruction\ninput。而 output 列对应的内容为模型回答。

如果指定,system 列对应的内容将被作为系统提示词。

[
  {
    "instruction": "人类指令,就是你要问模型的pormopt(必填)",
    "input": "人类输入,输入的句子(选填)",
    "output": "模型回答(必填)",
    "system": "系统提示词(选填)",
    "history": [
      ["第一轮指令(选填)", "第一轮回答(选填)"],
      ["第二轮指令(选填)", "第二轮回答(选填)"]
    ]
  }
]
注册自己的数据集

将自己的数据集放到data目录下

vim data/dataset_info.json
### 添加一行内容
 "my_train_data": {
    "file_name": "my_train_data.json"
  },

记着名字,一会训练要指定数据集名称

五,修改对应的yaml文件
### model
model_name_or_path:原始模型地址

### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all

### dataset
dataset: my_train,alpaca_en_demo(混合训练的样本集,防止知识遗忘,可以不用)
template: qwen
cutoff_len: 4096
max_samples: 1000
overwrite_cache: true
preprocessing_num_workers: 16

### output
output_dir: saves/qwen/lora/sft
logging_steps: 10
save_steps: 100
plot_loss: true
overwrite_output_dir: true

### 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
ddp_timeout: 180000000

### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500
开始训练

lora 指令微调

llamafactory-cli train examples/train_lora/mytrain_lora_sft.yaml

命令行

CUDA_VISIBLE_DEVICES=0,1,2,3 python src/train_bash.py  --stage sft     --do_train     --model_name_or_path  /app/model  --dataset my_train_data    --finetuning_type lora     --lora_target q_proj,v_proj     --output_dir /app/output   --overwrite_cache     --per_device_train_batch_size 1     --gradient_accumulation_steps 1     --lr_scheduler_type cosine     --logging_steps 10     --save_steps 1000     --learning_rate 5e-5     --num_train_epochs 3.0     --template yi
合并模型
llamafactory-cli export examples/merge_lora/my_lora_sft.yaml
### vi examples\merge_lora\llama3_lora_sft.yaml改成自己路径就行了
### model
model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
adapter_name_or_path: saves/llama3-8b/lora/sft
template: llama3
finetuning_type: lora

### export
export_dir: models/llama3_lora_sft
export_size: 2
export_device: cpu
export_legacy_format: false

或者

CUDA_VISBLE_DEVICES=0 python /app/src/export_model.py --model_name_or_path /app/model/ --adapter_name_or_path /app/output/checkpoint-3000/ --template default --finetuning_type lora --export_dir /app/lora_resul
t/20240422_1519 --export_size 2 --export_legacy_format False

模型推理

vi inference/yam.py,修改对应路径

model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
adapter_name_or_path: saves/llama3-8b/lora/sft
template: llama3
finetuning_type: lora
llamafactory-cli chat examples/inference/llama3_lora_sft.yaml

或者
·```
python /app/src/cli_demo.py --model_name_or_path /app/lora_result/20240422_1519/ --template=qwen


未完待续·....

http://www.niftyadmin.cn/n/5536113.html

相关文章

《自动驾驶中的SLAM技术》第2章: 基础数学知识回顾 习题

自动驾驶中的SLAM技术 第2章: 基础数学知识回顾 习题 1 分别使用左右扰动模型,计算 ∂ R − 1 p ∂ R \frac{\partial \mathbf{R}^{-1}\mathbf{p}}{\partial \mathbf{R}} ∂R∂R−1p​。 左扰动模型 ∂ R − 1 p ∂ R lim ⁡ δ ϕ → 0 ( E x p ( δ ϕ ) R…

数据结构(3.8)——栈的应用

栈在括号匹配中的应用 流程图 代码 #include <stdio.h> #include <stdlib.h> #define MaxSize 10typedef struct {char data[MaxSize];int top; } SqStack;// 初始化栈 void InitStack(SqStack* S) {S->top -1; // 初始化栈顶指针 }// 判空 bool StackEmpty(…

JavaScript 原型链那些事

在讲原型之前我们先来了解一下函数。 在JS中&#xff0c;函数的本质就是对象&#xff0c;它与其他对象不同的是&#xff0c;创建它的构造函数与创建其他对象的构造函数不一样。那产生函数对象的构造函数是什么呢&#xff1f;是一个叫做Function的特殊函数&#xff0c;通过newFu…

nginx部署多个项目;vue打包项目部署设置子路径访问;一个根域名(端口)配置多个子项目

本文解决&#xff1a; vue打包项目部署设置子路径访问&#xff1b;nginx部署多个子项目&#xff1b;一个ip/域名 端口 配置多个子项目&#xff1b;配置后&#xff0c;项目能访问&#xff0c;但是刷新页面就丢失的问题 注&#xff1a;本文需要nginx配置基础。基础不牢的可见文…

代码随想录leetcode200题之额外题目

目录 1 介绍2 训练3 参考 1 介绍 本博客用来记录代码随想录leetcode200题之额外题目相关题目。 2 训练 题目1&#xff1a;1365. 有多少小于当前数字的数字 解题思路&#xff1a;二分查找。 C代码如下&#xff0c; class Solution { public:vector<int> smallerNumb…

SpringBoot应用配置桥接Prometheus入门

SpringBoot应用配置Prometheus步骤 SpringBoot应用依赖要求PrometheusGrafanaGrafana监控界面模板 SpringBoot应用依赖要求 <!-- 监控系统健康情况的工具 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot…

前端Debugger时复制的JS对象字符转JSON对象

前端debugger时&#xff0c;复制的对象在控制台输出时是如下格式&#xff0c;需要转换为对象格式来进行验证操作 bridgeId : 4118 createBy : null createTime : "2023-03-24 10:35:26" createUserId : 1 具体实现代码&#xff1a; // 转换transform (text) {l…