LLaMa Factory大模型微调
- 大模型微调
-
- 平台&硬件
- LLaMA-Factory安装
- [hfd下载hugging face模型](#hfd下载hugging face模型)
- 自我认知微调
- Alpaca数据集指令监督微调
- 断点续训
大模型微调
微调自我认知+微调特定领域数据集。
平台&硬件
- Ubuntu20.04
- 显卡:M40 24G + 2080TI 22G
- 微调框架:LLaMA-Factory
LLaMA-Factory安装
SHELL
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]"
启动项目所提供的webui操作界面(因为我这里有两张型号不一样的显卡,经过测试不能混合训练,因此指定使用单卡2080TI):
SHELL
export CUDA_VISIBLE_DEVICES=0
llamafactory-cli webui
URL:http://localhost:7860/
如果报错ValueError: Unknown scheme for proxy URL URL('socks://127.0.0.1:7897/')
解决方案参考解决openai调用出现的valueerror: unknown scheme for proxy url url('socks://127.0.0.1:7890/'),使用下面命令查看系统的代理设置
SHELL
env|grep -i proxy
看看有没有出现下面的all_proxy的设置。
SHELL
no_proxy=localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,::1
https_proxy=http://127.0.0.1:7897/
NO_PROXY=localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,::1
HTTPS_PROXY=http://127.0.0.1:7897/
HTTP_PROXY=http://127.0.0.1:7897/
http_proxy=http://127.0.0.1:7897/
ALL_PROXY=socks://127.0.0.1:7897/
all_proxy=socks://127.0.0.1:7897/
如果有all_proxy的设置,使用下面命令设置为空就行。(原理未知)
SHELL
export ALL_PROXY=''
export all_proxy=''
hfd下载hugging face模型
以Qwen/Qwen2.5-7B-Instruct为例
不确定模型的名字的话,打开链接https://hf-mirror.com/api/models
,搜相应的模型名字,比如我这里直接搜Qwen2.5-7B-Instruct
,然后得知模型全称是Qwen/Qwen2.5-7B-Instruct
shell
sudo apt install aria2
pip install -U huggingface_hub
wget https://hf-mirror.com/hfd/hfd.sh
chmod a+x hfd.sh
export HF_ENDPOINT=https://hf-mirror.com
./hfd.sh Qwen/Qwen2.5-7B-Instruct --tool aria2c -x 10
自我认知微调
主要是配置以下参数:
- 模型名称:Qwen2.5-7B-Instruct
- 模型路径:配置为fd下载的本地模型路径
其他参数暂时用默认,训练轮数设置为20。开始训练。
训练完成后,选择相应的检查点路径,随后测试chat。
最后export导出模型。
Alpaca数据集指令监督微调
按Alpaca格式准备好数据集
SHELL
[
{
"instruction": "人类指令(必填)",
"input": "人类输入(选填)",
"output": "模型回答(必填)",
"system": "系统提示词(选填)",
"history": [
["第一轮指令(选填)", "第一轮回答(选填)"],
["第二轮指令(选填)", "第二轮回答(选填)"]
]
}
]
复制数据集到data
目录下,修改data/dataset_info.json
举例:
"数据集名称": {
"file_name": "data.json",
"columns": {
"prompt": "instruction",
"query": "input",
"response": "output",
"system": "system",
"history": "history"
}
}
如果数据集没有相应的键,就先去掉data/dataset_info.json
对应的键。比如我的数据集没有history,我就把data/dataset_info.json
关于history的部分去掉。
断点续训
- 配置
检查点路径
为对应checkpoint对应位置(一般在saves
文件夹) - 如果有训练参数文件,加载训练参数(下次开始训练前可以点击保存,在
config
文件夹可以找到) - 配置
输出目录
和配置路径
- 点击预览命令,主要查看
adapter_name_or_path
和output_dir
参数
参考:
使用微调神器LLaMA-Factory轻松改变大语言模型的自我认知
使用hfd工具快速下载huggingface模型权重-应该是目前最快的