AutoDL精调Llama系列模型(SFT+UI简单操作)
在AutoDL平台创建实例,借助llama-factory工具,高效精调大模型,实现资源优化与性能提升,助力AI项目落地。一、准备实例(AutoDL)
https://i-blog.csdnimg.cn/direct/b7686631b117430d91d7a33cde8d4096.png
https://i-blog.csdnimg.cn/direct/0f1a1fe1577b43c4b5a4f19be37eaff7.png
JupyterLab方式
https://i-blog.csdnimg.cn/direct/5bebd9c3da224bbbbdf5c814de41a6e2.png
连接Vscode(ssh)
https://i-blog.csdnimg.cn/direct/7d253cf626374badbbfbd2c5ef2cb5eb.png
二、准备微调
1.部署安装Llama-Factory
参考:使用 LLaMA Factory 举行大语言模型微调
# 克隆仓库
git clone https://github.com/hiyouga/LLaMA-Factory.git
# 创建虚拟环境
conda create -n llama_factory python=3.10
# 激活虚拟环境
conda activate llama_factory
# 安装依赖
cd LLaMA-Factory
pip install -r requirements.txt
2.下载模型
方法一:huggingface
# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
git clone https://huggingface.co/hiyouga/Llama-2-Chinese-13b-chat
# If you want to clone without large files - just their pointers
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/hiyouga/Llama-2-Chinese-13b-chat
注意:Autodl科学上网 Autodl学术资源加快
# 在终端中使用
source /etc/network_turbo
# 在Notebook中使用
import subprocess
import os
result = subprocess.run('bash -c "source /etc/network_turbo && env | grep proxy"', shell=True, capture_output=True, text=True)
output = result.stdout
for line in output.splitlines():
if '=' in line:
var, value = line.split('=', 1)
os.environ = value
# 取消学术加速
unset http_proxy && unset https_proxy
https://i-blog.csdnimg.cn/direct/167dbda0c8684cf09697f6b0062e1968.png
方法二:魔塔社区
from modelscope import snapshot_download
model_path="modelscope/Llama2-Chinese-13b-Chat-ms"
cache_path="model/Llama2-Chinese-13b-Chat"
snapshot_download(model_path, cache_dir=cache_path)
注意:假如你是使用 AutoDL 举行部署的话,在conda activate llama_factory之前须要先执行一下conda init bash命令来初始化一下 conda 环境,然后重新打开一个终端窗口,再执行conda activate llama_factory命令。
3.开始微调
启动 LLaMA Factory 的 WebUI 页面,执行命令如下:
CUDA_VISIBLE_DEVICES=0,1 python src/webui.py #两卡,若一卡就写对应卡号即可
(1)将模型路径更改为你下载的路径
模型路径:/root/autodl-tmp/pre_work/model/Llama3-8B-Chinese-Chat/LLM-Research/Llama3-8B-Chinese-Chat
数据集路径:/root/LLaMA-Factory/data
输出目录:/root/autodl-tmp/pre_work/output/result/8b_train_2024-11-27-14-54-31
yaml目录:/root/autodl-tmp/pre_work/output/yaml/8b_2024-11-27-14-54-31.yaml
https://i-blog.csdnimg.cn/direct/eba741da9ef749c79dcb4cdd89fea162.png
在微调方法中,可以选择full、freeze、lora,若要适用qlora则选择量化等级,并在UI界面的对应位置(默认菜单已折叠)调整对应参数。
(2)制作自己的数据集
(仔细阅读LLaMA-Factory/data/README_zh.md,下以指令监视微调数据集为例)
样例数据集:(alpaca_zh_demo.json),instruction 列对应的内容会与 input 列对应的内容拼接后作为人类指令,即人类指令为 instruction\ninput。而 output 列对应的内容为模型回复。
假如指定,system 列对应的内容将被作为系统提示词。history 列是由多个字符串二元组构成的列表,分别代表汗青消息中每轮对话的指令和回复。注意在指令监视微调时,汗青消息中的回复内容也会被用于模型学习。
[
{
"instruction": "人类指令(必填)",
"input": "人类输入(选填)",
"output": "模型回答(必填)",
"system": "系统提示词(选填)",
"history": [
["第一轮指令(选填)", "第一轮回答(选填)"],
["第二轮指令(选填)", "第二轮回答(选填)"]
]
}
]
对于上述格式的数据,dataset_info.json 中的数据集形貌应为:
"数据集名称": {
"file_name": "data.json",
"columns": {
"prompt": "instruction",
"query": "input",
"response": "output",
"system": "system",
"history": "history"
}
}
(3)修改保存路径
https://i-blog.csdnimg.cn/direct/dcc878e2ad3342e3acf64d59137c62cf.png
(4)微调参数
参考:Llama2技能细节&开源影响-SFT参数
https://i-blog.csdnimg.cn/direct/0a3a06d4cfc842edb398ce7e76f52978.png
https://i-blog.csdnimg.cn/direct/56afd74cb6c74735a80c9c210bb1bf5a.png
注意
[*]微调的时候要注意模型须要的显存大小,否则就会“CUDA Out Of Memory”
https://i-blog.csdnimg.cn/direct/49a5a4fab0e84cdf8da05c702c5515aa.png
[*]单机多卡、多机多卡等多种分布式训练方式
Llama-factory分布式训练
https://i-blog.csdnimg.cn/direct/b080c52041724a4c86f4bbd363b3e5cb.png
三、测试、对话、导出模型
依次操作即可
页:
[1]