本教程演示了如何使用 LLaMA-Factory 框架,通过直接偏好优化(Direct Preference Optimization,DPO) 对语言模型进行微调。DPO 是一种基于人类偏好来训练模型的方法,能够使模型输出更加对齐人类期望,更加以用户为中心。

1 环境配置

软硬件要求:CPU 支持 AMX,系统的 glibc 版本大于等于 2.32,建议 GPU 显存大于等于 32G。

Step 1: 创建 KTransformers 的 conda 环境

1
2
3
4
conda create -n Kllama python=3.12 # choose from : [3.11, 3.12, 3.13]
conda activate Kllama
conda install -y -c conda-forge libstdcxx-ng gcc_impl_linux-64
conda install -y -c nvidia/label/cuda-12.8.0 cuda-runtime

Step 2: 安装 LLaMA-Factory

1
2
3
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]" --no-build-isolation

Step 3: 安装 KTransformers

Option 1: 从 https://github.com/kvcache-ai/ktransformers/releases/tag/v0.4.4 下载并安装与 TorchPython 版本相匹配的 KTransformers wheel 包。

✨ CUDA 版本可以与 wheel 文件名中标注的版本不同。

1
pip install https://github.com/kvcache-ai/ktransformers/releases/download/v0.4.4/ktransformers-0.4.4+cu128torch29fancy-cp312-cp312-linux_x86_64.whl

❗❗❗ wheel 的 python, coda, torch 的版本号必须与当前环境一致

Option 2: 从源码安装 KTransformers

1
2
3
4
5
6
git clone --depth 1 https://github.com/kvcache-ai/ktransformers.git
cd ktransformers/kt-sft
export TORCH_CUDA_ARCH_LIST="8.0;8.9;9.0" # set according to your GPU

pip install -r "requirements-sft.txt"
KTRANSFORMERS_FORCE_BUILD=TRUE pip install -v . --no-build-isolation

Step 4: 安装 Flash-attention wheel

https://github.com/Dao-AILab/flash-attention/releases 下载并安装与 TorchPython 版本相匹配的 Flash-Attention wheel 包。

1
2
3
4
5
# abi=True/False can find from below
# import torch
# print(torch._C._GLIBCXX_USE_CXX11_ABI)

pip install https://github.com/Dao-AILab/flash-attention/releases/download/v2.8.3/flash_attn-2.8.3+cu12torch2.9cxx11abiTRUE-cp312-cp312-linux_x86_64.whl

❗❗❗ wheel 的 python, coda, torch 的版本号必须与当前环境一致,还需要检查 abi 是 True 还是 False

Step 5: (可选) 如果想使用 flash_infer

1
2
git clone https://github.com/kvcache-ai/custom_flashinfer.git
pip install custom_flashinfer/

2 DPO 训练

2.1 准备模型

本篇博客使用 DeepSeek-V2-Lite-Chat 模型作为演示,如果有需要可以替换为其他模型。

2.2 配置训练参数文件

(1)examples/train_lora/deepseek2_lora_dpo_kt.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
### model
model_name_or_path: DeepSeek-V2-Lite-Chat
trust_remote_code: true

### method
stage: dpo
do_train: true
finetuning_type: lora
lora_rank: 8
lora_target: all
pref_beta: 0.1
pref_loss: sigmoid  # choices: [sigmoid (dpo), orpo, simpo]

### dataset
dataset: dpo_en_demo
template: deepseek
cutoff_len: 2048
max_samples: 1000
overwrite_cache: true
preprocessing_num_workers: 16
dataloader_num_workers: 4

### output
output_dir: saves/Kllama_deepseekV2_DPO
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true
save_only_model: false
report_to: none  # choices: [none, wandb, tensorboard, swanlab, mlflow]

### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 5.0e-6
num_train_epochs: 2
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000
resume_from_checkpoint: null

### ktransformers
use_kt: true # use KTransformers as LoRA sft backend
kt_optimize_rule: examples/kt_optimize_rules/DeepSeek-V2-Lite-Chat-sft-amx.yaml
cpu_infer: 64
chunk_size: 8192

(2)examples/inference/deepseek2_lora_dpo_kt.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
model_name_or_path: DeepSeek-V2-Lite-Chat 
adapter_name_or_path: saves/Kllama_deepseekV2_DPO
template: deepseek
infer_backend: ktransformers  # choices: [huggingface, vllm, sglang, ktransformers]
trust_remote_code: true

use_kt: true # use KTransformers as LoRA sft backend to inference
kt_optimize_rule: examples/kt_optimize_rules/DeepSeek-V2-Lite-Chat-sft-amx.yaml
cpu_infer: 32
chunk_size: 8192

2.3 训练模型

1
2
# For LoRA SFT
USE_KT=1 llamafactory-cli train examples/train_lora/deepseek2_lora_dpo_kt.yaml

训练结果如下:

image-20251223152536433

image-20251223152548458

2.4 模型推理

1
2
# For Chat with model after LoRA SFT
llamafactory-cli chat examples/inference/deepseek2_lora_dpo_kt.yaml

image-20251223153114783

2.5 使用模型 API

1
2
# For API with model after LoRA SFT
llamafactory-cli api examples/inference/deepseek2_lora_dpo_kt.yaml

image-20251223153402473

报错示例

  • 环境安装错误

f525c0db5631bbd7e5c8cf0ec1580104

PyTorch , Python , FlashAttention, cuda 都必须保证一致,在使用 wheel 安装 FlashAttention 和 KTransformers 之前,使用如下命令查看安装的 python, torch 的版本

1
pip list

会得到所有包的版本号,找到 torch 的版本号,例如

1
torch                    2.9.1

结合环境配置 Step 1 安装的 python 版本和 cuda runtime 版本,可以确定 FlashAttention 和 KTransformers 的 wheel 包。

然后去 https://github.com/kvcache-ai/ktransformers/releases/tag/v0.4.4https://github.com/Dao-AILab/flash-attention/releases 下载对应的版本。

本博客使用 python=3.12,torch=2.9.1,cuda=12.8,并且是 x86 系统,故应该安装:ktransformers-0.4.4+cu128torch29fancy-cp312-cp312-linux_x86_64.whlflash_attn-2.8.3+cu12torch2.9cxx11abiTRUE-cp312-cp312-linux_x86_64.whl

其中 cu 后缀表示 cuda 版本,torch 后缀表示 torch 版本,cp 后缀表示 python 版本,cxx 后缀表示 c++ 版本,abi 后缀表示 abi 是否启用。

  • KTransformers 只支持带有 AMX 功能的 CPU

adf7dad879efc4cf4155008019f9c1e6

CPU 的 AMX 指的是 Intel Advanced Matrix Extensions(高级矩阵扩展),是 Intel 在 服务器/高性能 CPU 上推出的一套 面向矩阵计算的硬件加速指令集,主要用于 AI / 深度学习 / HPC 场景。可使用如下命令查看是否支持 AMX

1
lscpu | grep amx

出现类似于如下的内容

1
amx_tile amx_int8 amx_bf16

表示 CPU 支持 AMX,如果未出现上述内容,表示 CPU 不支持 AMX 需要换一台机器。