我来为你详细介绍 KTransformers,并搜索一些最新的信息来补充。
根据搜索到的最新信息,我来为你全面介绍 KTransformers:
KTransformers 简介
KTransformers (简称 KT)是由 kvcache-ai 团队开发的一个专为大语言模型(LLM)推理优化的高性能框架 ,核心目标是让超大模型(如 DeepSeek-V3/R1、Kimi-K2 等)能够在消费级硬件(单卡 24GB 显存 + 大内存)上高效运行。
核心设计理念
KTransformers 采用 CPU+GPU 异构计算架构:
- GPU:处理热数据(激活值、注意力计算、热专家)
- CPU :处理冷数据(MoE 的冷专家计算),利用大内存容量和 Intel AMX 指令集加速
这种设计突破了显存限制,让 671B 参数的 DeepSeek-R1 可以在单张 RTX 4090 上运行 。
关键特性
1. Intel AMX 加速(核心亮点)
- AMX(Advanced Matrix Extensions)是 Intel 第四代至强可扩展处理器(Sapphire Rapids 及更新)引入的矩阵计算指令集
- KTransformers 针对 AMX 优化了 MoE(混合专家)模型的 CPU 计算内核,支持 INT8 和 BF16 精度
- 性能表现:
- 单核理论算力:INT8 可达 4 TOPS
- Xeon4 CPU 实测:BF16 达 21 TFLOPS,INT8 达 35 TOPS,约为 PyTorch 通用 AMX Kernel 的 4 倍
2. 支持的模型
- DeepSeek 系列:V3、V3.2、R1(671B 满血版)
- Kimi-K2 系列:K2、K2-Thinking
- Qwen3 系列:Qwen3、Qwen3-MoE
- MiniMax 系列:M2、M2.1
- GLM-4.6V 等多模态模型
3. 量化与优化
- 支持 GGUF 格式(Q4_K_M、BF16 等)
- RAWINT4 方法:CPU 和 GPU 共享同一套 INT4 权重,无需分别转换
- Layerwise Prefill:分层预填充,显著降低长上下文场景的内存占用
4. 最新版本特性(v0.4.4 / v0.5.0)
- v0.4.4:新增 RL-DPO 训练支持、GLM-4.6V 支持、Docker 部署、NUMA 优化
- v0.5.0 :原生 FP8 MoE Kernel (AVX 优化)、全新
kt-cli工具、Layerwise Prefill 性能提升
硬件要求
必需条件
| 组件 | 要求 | 说明 |
|---|---|---|
| CPU | Intel 第四代至强可扩展(Sapphire Rapids)或更新 | 需支持 AMX 指令集(amx-bf16, amx-int8, amx-tile) |
| GPU | NVIDIA GPU(推荐 RTX 4090 24GB) | 用于热专家计算和注意力 |
| 内存 | ≥ 382GB(运行 DeepSeek-R1 671B) | CPU 承载大部分专家权重 |
| CUDA | 12.4+ | 需与 PyTorch 版本匹配 |
检查 AMX 支持
bash
lscpu | grep -i amx
# 应输出包含:amx-bf16 amx-int8 amx-tile
⚠️ 注意 :虽然 KTransformers 从 v0.4.3 开始增加了 AMD BLIS 后端 和 AVX-512 回退 支持,但 AMX 仍是获得最佳性能的关键 。
安装与版本匹配
根据你提供的环境配置(Python 3.12、PyTorch 2.9.1、CUDA 12.8),正确的 wheel 包选择逻辑如下:
命名规则解析
以 ktransformers-0.4.4+cu128torch29fancy-cp312-cp312-linux_x86_64.whl 为例:
cu128→ CUDA 12.8torch29→ PyTorch 2.9cp312→ Python 3.12linux_x86_64→ Linux x86 架构
下载地址
- KTransformers: https://github.com/kvcache-ai/ktransformers/releases/tag/v0.4.4
- FlashAttention: https://github.com/Dao-AILab/flash-attention/releases
典型部署场景
场景 1:本地工作站(推荐)
- 配置:Intel Xeon W-3400 系列(支持 AMX)+ RTX 4090 + 512GB DDR5
- 性能 :DeepSeek-V3 可达 418 tokens/s(端到端吞吐量)
场景 2:云服务器
- 配置:Intel Xeon Gold 6430(双路,64核)+ 2×RTX 4090 + 480GB 内存
- 优化 :需设置
USE_NUMA=1启用 NUMA 优化
场景 3:Docker 部署(v0.4.4+ 支持)
bash
docker pull nvidia/cuda:12.8.0-devel-ubuntu22.04
配置示例(AMX 启用)
在 YAML 配置文件中指定 AMX 后端:
yaml
- match:
name: "^model\\.layers\\..*\\.mlp\\.experts$"
replace:
class: ktransformers.operators.experts.KTransformersExperts
kwargs:
prefill_device: "cuda"
prefill_op: "KExpertsTorch"
generate_device: "cpu"
generate_op: "KExpertsCPU"
out_device: "cuda"
backend: "AMXInt8" # 或 "AMXBF16"
总结
KTransformers 是目前在消费级硬件上运行超大规模 MoE 模型(如 DeepSeek-R1 671B)的最佳解决方案之一,其核心优势在于:
- AMX 指令集深度优化:让 CPU 成为 MoE 推理的加速器,而非瓶颈
- 异构计算架构:GPU 处理热路径,CPU 处理冷路径,最大化硬件利用率
- 低门槛部署:单卡 24GB 显存即可运行 671B 模型,大幅降低 AI 应用成本
如果你的 CPU 支持 AMX,KTransformers 是目前本地部署 DeepSeek 满血版的最优选择;如果不支持 AMX,也可使用 AVX-512 后端,但性能会有所下降。
你说得对,我需要纠正这个错误。让我搜索确认一下 KTransformers 的出处。
非常抱歉,我之前的回答有误!你说得完全正确,KTransformers 确实是清华大学出品的。
让我重新为你介绍:
KTransformers 的出处
KTransformers 是由 清华大学 KVCache.AI 团队 与 趋境科技(Approaching.AI) 联合研发的开源项目 。
核心团队背景
- 清华大学 MADSys 实验室(Department of Computer Science and Technology):主导系统架构和内核优化研究
- 趋境科技(Approaching.AI):清华系创业公司,负责工程化和产品落地
- 首席顾问:郑纬民院士(中国计算机体系结构领域权威专家)
学术背书
项目论文 《KTransformers: Unleashing the Full Potential of CPU/GPU Hybrid Inference for MoE Models》 入选了计算机系统领域顶级会议 SOSP 2025(被誉为"计算机系统领域奥斯卡")。
论文作者单位明确标注为清华大学计算机科学与技术系 :
"The authors affiliated with Tsinghua University are all in the Department of Computer Science and Technology, Beijing National Research Center for Information Science and Technology (BNRist), Tsinghua University, China."
开发时间线
- 2024年7月:首次开源发布(GitHub 20k+ Stars)
- 2025年2月:重大更新,支持单卡 RTX 4090 运行 DeepSeek-R1/V3 671B 满血版
- 2025年10月:论文入选 SOSP 2025,并与 SGLang 框架合作
为什么容易混淆?
GitHub 上的组织名是 kvcache-ai(而非直接的 Tsinghua),这是因为:
- KVCache.AI 是清华团队的技术品牌名称
- 项目采用产学研合作模式,由清华 MADSys 实验室与趋境科技联合维护
- 代码仓库托管在
github.com/kvcache-ai/ktransformers下
总结
| 项目 | 信息 |
|---|---|
| 出品方 | 清华大学 KVCache.AI 团队 + 趋境科技 |
| 学术归属 | 清华大学计算机系 MADSys 实验室 |
| 首席顾问 | 郑纬民院士 |
| 核心贡献者 | 章明星(副教授,项目指导老师)、陈鸿涛等 |
| 产业方 | 趋境科技(Approaching.AI) |
感谢你的纠正!这确实是一个国产的、源自清华大学的优秀开源项目,代表了国内在 AI 系统软件领域的顶尖水平。
微调
KTransformers 联合 LLaMA-Factory 进行 RL-DPO 训练
2025年12月23日 · 3 分钟 · 566 字 · hiyouga | 语言:
En
| Edit this post
本教程演示了如何使用 LLaMA-Factory 框架,通过直接偏好优化(Direct Preference Optimization,DPO) 对语言模型进行微调。DPO 是一种基于人类偏好来训练模型的方法,能够使模型输出更加对齐人类期望,更加以用户为中心。
1 环境配置
软硬件要求:CPU 支持 AMX,系统的 glibc 版本大于等于 2.32,建议 GPU 显存大于等于 32G。
Step 1: 创建 KTransformers 的 conda 环境
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
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 下载并安装与 Torch 和 Python 版本相匹配的 KTransformers wheel 包。
✨ CUDA 版本可以与 wheel 文件名中标注的版本不同。
❗❗❗ wheel 的 python, coda, torch 的版本号必须与当前环境一致
Option 2: 从源码安装 KTransformers
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 下载并安装与 Torch 和 Python 版本相匹配的 Flash-Attention wheel 包。
abi=True/False can find from below
import torch
print(torch._C._GLIBCXX_USE_CXX11_ABI)
❗❗❗ wheel 的 python, coda, torch 的版本号必须与当前环境一致,还需要检查 abi 是 True 还是 False
Step 5: (可选) 如果想使用 flash_infer
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
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
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 训练模型
For LoRA SFT
USE_KT=1 llamafactory-cli train examples/train_lora/deepseek2_lora_dpo_kt.yaml
训练结果如下:
image-20251223152536433
image-20251223152548458
2.4 模型推理
For Chat with model after LoRA SFT
llamafactory-cli chat examples/inference/deepseek2_lora_dpo_kt.yaml
image-20251223153114783
2.5 使用模型 API
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 的版本
pip list
会得到所有包的版本号,找到 torch 的版本号,例如
torch 2.9.1
结合环境配置 Step 1 安装的 python 版本和 cuda runtime 版本,可以确定 FlashAttention 和 KTransformers 的 wheel 包。
然后去 https://github.com/kvcache-ai/ktransformers/releases/tag/v0.4.4 和 https://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.whl 和 flash_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
lscpu | grep amx
出现类似于如下的内容
amx_tile amx_int8 amx_bf16
表示 CPU 支持 AMX,如果未出现上述内容,表示 CPU 不支持 AMX 需要换一台机器。