1.摘要

用llama-factory的0.9.1版本微调DeepSeek-R1-Distill-Qwen-1.5B,微调数据是对文档段进行问答对提取的4000条样本。使用微调后的模型进行推理。合并微调后的模型。

微调GPU配置H20-96G。

2. 准备工作

2.1 服务器配置

H20-96G

python3.10.8

touch 2.1.2+cu121

torchvision  0.16.2+cu121

CUDA Version: 12.4

2.2 微调框架lama-factory-0.9.1

2.2.1 网址

https://github.com/hiyouga/LLaMA-Factory/tree/v0.9.1

2.2.2 安装

cd  LLaMA-Factory-0.9.1

pip install -e '.[torch,metrics]'

2.3 微调模型DeepSeek-R1-Distill-Qwen-1.5B

2.3.1 网址

https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B/tree/main

2.4 微调数据

从文档数据中提取问答对,一共4000个文档数据段,每个文档数据段最长不超过1k,每个文档数据段对应着6个左右的问答对。

2.6 微调数据文件question_answer.json中的数据格式

{

"input_sys""",

"instruction":"",

"input""",

"output""",

}

把微数据文件question_answer.json放到LLaMA-Factory/data/下面。

2.7 修改LLaMA-Factory/data/dataset_info.json

在data/dataset_info.json中增加:

"question_answer": {

"file_name": "question_answer.json",

"columns": {

"system": "input_sys",

"prompt": "instruction",

"query": "input",

"response": "output"

}

}

2.8 修改微调配置文件

在/examples/train_lora/目录下面增加deepseek_r1_1.5_lora_sft.yaml文件

cp llama3_lora_sft.yaml  deepseek_r1_1.5_lora_sft.yaml

2.8.1 在deepseek_r1_1.5_lora_sft.yaml文件中修改下面几项:
### model

model_name_or_path: /root/LLM/DeepSeek-R1-Distill-Qwen-1.5B
### dataset

dataset: question_answer

cutoff_len: 32000
max_samples: 4000

### output

output_dir: /root/LLM/saves-sft/deepseek-1.5b/lora/sft

3. 开始微调

cd  LLaMA-Factory-0.9.1

CUDA_VISIBLE_DEVICES=0 llamafactory-cli train examples/ train_lora/deepseek_r1_1.5_lora_sft.yaml

3.1 运行成功

3.2 显存占用18G

3.3 微调时loss和学习率的变化情况

3.4 最后跑完用时36分钟

4. 推理

4.1 修改推理配置文件

4.1.1 在目录examples/inference下面新增文件deepseek-r1.yaml:

cp llama3_lora_sft.yaml  deepseek-r1.yaml

4.1.2 修改文件deepseek-r1.yaml

model_name_or_path: /root/LLM/DeepSeek-R1-Distill-Qwen-1.5B

adapter_name_or_path: /root/LLM/saves-sft/deepseek-1.5b/lora/sft

4.2 执行推理

CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat examples/inference/deepseek-r1.yaml

运行成功。

推理占用显存4个G

5. 模型文件合并

5.1 修改配置文件

5.1.1 在目录examples/merge_lora下面新增文件deepseek_lora_sft.yaml

cp llama3_lora_sft.yaml  deepseek_lora_sft.yaml

5.1.2 修改文件deepseek-r1.yaml

### model

model_name_or_path: /root/LLM/DeepSeek-R1-Distill-Qwen-1.5B

adapter_name_or_path: /root/LLM/saves-sft/deepseek-1.5b/lora/sft

### export

export_dir: /root/LLM/models/deepseek_lora_sft

5.2 执行合并

llamafactory-cli export examples/merge_lora/deepseek_lora_sft.yaml

合并后的模型文件

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐