一个强大的LLM微调工具 LLaMa-Factory:手把手教你从零微调大模型
一、配景LLaMa-Factory项目定位是微调工具,目的是整合当前主流的各种高效训练微调技能,适配市场主流的开源模型,形成一个功能丰富,适配性好的训练框架。本次记录基于自身利用llama-factory,简单记录从环境摆设到训练及推理的过程。
https://i-blog.csdnimg.cn/direct/65a15e3847d24de4aaea23d1eec6923d.png
二、准备工作
项目顺遂运行必要几个必备条件
1. 机器本身的硬件和驱动支持(包含显卡驱动,网络环境等)
输入下列命令做校验
nvidia-smi 预期输出如下图,体现GPU当前状态和设置信息
https://i-blog.csdnimg.cn/direct/0fe38f81ccbb48acb0fa4f719a3f7a46.png
2. 相干依赖的python库的正确安装(包含CUDA,Pytorch等)
三、环境安装
我当前用的系统版本如下,当前的安装摆设都是基于此系统进行:
# cat/etc/issue
Ubuntu 20.04.6 LTS \n \l 1. 先把项目克隆到本地的工作目录下
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git 2. 创建假造环境
一样平常安装依赖包都会创建一个假造环境,然后在此假造环境下安装各个包
# 使用python创建虚拟环境:
# 创建虚拟环境:
python -m venv mynamevenv
# 激活:
source mynamevenv/bin/activate
# 关闭/退出虚拟环境:
deactivate
# 使用conda创建虚拟环境:
# 创建虚拟环境:
conda create -n myname_env
# 激活虚拟环境:
source activate myname_env
# 关闭/退出虚拟环境:
conda deactivate
##根据自己的情况选择合适的方式,conda激活虚拟环境时的命令好像与版本有关,不同版本略有差异,可根据提示信息进行修改
##使用python创建的虚拟环境会在当前目录创建一个虚拟环境名称的文件夹,而使用conda创建的虚拟环境需要使用命令'conda env list'查看所有创建的虚拟环境的列表 创建好假造环境并激活后,安装依赖包,此处用的是阿里云的镜像,速率快一些
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple 安装完requirements.txt之后直接运行还会缺少一些环境,再实行下列命令安装相干包
pip install -e "." 3. 大概遇到的问题
只要硬件满足,团体的安装过程没有其他问题,如果遇到如下报错
AssertionError: no sync context manager is incompatible with gradientpartitioning logic of ZeRo stage 3 办理办法:把deepspeed版本改为0.15.4,pip install deepspeed==0.15.4
四、准备训练数据和模型
1. 准备数据
在克隆到本地的LLaMA-Factory项目目录中,data文件目录下,有些示例数据,刚开始但是利用这些数据做测试,先把整个流程测试通。
当然也可以准备自定义数据集,不过必要将数据处理为框架特定的格式,放在data目录下,而且修改dataset_info.json文件。
2. 准备模型
项目支持通过模型名称直接从huggingface和modelscope(国内出的社区,对标huggingface),下载的方式很多,可以直接在社区网站上看到。必要注意的是,很多模型文件相对较大一些,在下载过程中必要注意下载文件的完备性,除了用肉眼去观看文件巨细是否划一外,也可通过readme中提供的原始推理demo,来验证模型文件的正确性和transformers库等软件的可用性。
五、模型微调并推理
全部工作都准备好之后,我们先利用推理模式验证下LLaMa-Factory的推理部门是否正常。
CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat
--model_name_or_path ../models/Meta-Llama-3-8B-Instruct
--template llama3 CUDA_VISIBLE_DEVICES=0,是指定当出息序利用第0张卡,也可以指定多张卡,如CUDA_VISIBLE_DEVICES=0,1,2 等,详细利用哪张可根据自己的设置情况;
--model_name_or_path 参数是自己所利用的大模型的文件路径;
--template 参数是模型回答时所利用的prompt模版,不同模型的模板不同,可以参考LLaMa-Factory的readme。
受机器环境影响,我这里只能利用命令行版本来运行,运行之后会出现如下界面:
https://i-blog.csdnimg.cn/direct/9239c9ee44ae48f9a63d41205aaec94f.png
前面实在另有很多的加载日志信息,我这里没有全都截图在里面,当出现红框标记前的User: 时,我们随便输入我们的信息便可睁开对话,我这里输入的是“三国演义的作者是谁”,稍等一会儿,便会在Assistant位置后输出相干信息,当我们想再次输入信息时,可以先输入“clear”命令清除汗青信息,再输入我们的对话内容大概必要推理的内容,这样会更正确一些。至此说明我们的环境安装设置,是没有问题的。
微调及后续推理等全部的程序入口都是 llamafactory-cli,通过不同的参数控制现在实现什么功能,好比现在是想利用网页版直接推理,以是第一个参数设置为webchat,全部的可选项参数如下:
参数选项
参数说明
version
体现版本信息
train
命令行版本训练
chat
命令行版本推理chat,单条推理
export
模型归并和导出
api
启动API server,供接口调用
eval
利用mmlu等标准数据集做评测
webchat
前端版本纯推理的chat页面
webui
启动LlamaBoard前端页面,包含可视化训练,猜测,chat,模型归并多个子页面
我们利用前端页面来微调训练大模型时,可以直接在页面上进行勾选,但是当我们利用命令行去微调训练时,可以把浩繁的参数都存放在yaml文件里,我现在微调大模型并进行推理的几个步调和命令以及各自的yaml文件如下:
1. 大模型训练微调
CUDA_VISIBLE_DEVICES=0,1 nohup llamafactory-cli train examples/lora_multi_gpu/baichuan2_lora_sft.yaml > log_train_baichuan2.txt &
# 其中baichuan2_lora_sft.yaml文件配置内容如下:
### model
model_name_or_path: ../baichuan2
### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all
### ddp
ddp_timeout: 180000000
deepspeed: examples/deepspeed/ds_z3_config.json
### dataset
dataset: train_data_total
template: baichuan2
cutoff_len: 1024
max_samples: 1000000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: ../baichuan2_train_total
logging_steps: 10
save_steps: 100
plot_loss: true
overwrite_output_dir: true
### train
per_device_train_batch_size: 16
gradient_accumulation_steps: 4
learning_rate: 1.0e-4
num_train_epochs: 30
lr_scheduler_type: cosine
warmup_ratio: 0.1
fp16: true
### eval
val_size: 0.0002
per_device_eval_batch_size: 16
eval_strategy: steps
eval_steps: 100
temperature: 0.3 2. 大模型归并
CUDA_VISIBLE_DEVICES=0 llamafactory-cli export examples/merge_lora/baichuan2_lora_sft.yaml
# 其中baichuan2_lora_sft.yaml文件配置内容如下:
### Note: DO NOT use quantized model or quantization_bit when merging lora adapters
### model
model_name_or_path: ../baichuan2
adapter_name_or_path: ../baichuan2_train_total/checkpoint-300
template: baichuan2
finetuning_type: lora
### export
export_dir: ../baichuan2_train_out_total/output300
export_size: 80
export_device: gpu
export_legacy_format: false 3. 大模型批量推理for补全
CUDA_VISIBLE_DEVICES=1 nohup llamafactory-cli train examples/full_multi_gpu/hx_baichuan2_lora_sft.yaml >hx_log_val_baichuan2.txt &
# 其中hx_baichuan2_lora_sft.yaml文件配置内容如下:
### model
model_name_or_path: ../baichuan2_train_out_total/output300
### method
stage: sft
do_predict: true
finetuning_type: full
### dataset
dataset: val_data_hx
template: baichuan2
cutoff_len: 1024
max_samples: 1000000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: hx_result_baichuan2
overwrite_output_dir: true
### eval
per_device_eval_batch_size: 16
predict_with_generate: true 4. 大模型单条推理
CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat examples/inference/baichuan2_lora_sft.yaml
# 其中baichuan2_lora_sft.yam文件配置内容如下:
model_name_or_path: ../baichuan2_train_out_total/output300
template: baichuan2
temperature: 0.001 5. 大模型批量推理for意图
CUDA_VISIBLE_DEVICES=1 nohup llamafactory-cli train examples/full_multi_gpu/baichuan2_lora_sft.yaml >log_val_baichuan2.txt &
# 其中baichuan2_lora_sft.yaml文件配置内容如下:
### model
model_name_or_path: ../baichuan2_train_out_total/output300
### method
stage: sft
do_predict: true
finetuning_type: full
### dataset
dataset: val_data
template: baichuan2
cutoff_len: 1024
max_samples: 1000000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: result_baichuan2
overwrite_output_dir: true
### eval
per_device_eval_batch_size: 16
predict_with_generate: true
temperature: 0.001
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]