KTransformers:告别天价显卡!国产框架让单卡24G显存跑DeepSeek-R1 671B大模型:推理速度飙升28倍

❤️ 如果你也关注 AI 的发展现状,且对 AI 应用开发感兴趣,我会每日分享大模型与 AI 领域的开源项目和应用,提供运行实例和实用教程,帮助你快速上手AI技术!

🥦 微信公众号|搜一搜:蚝油菜花 🥦


💥 “还在为千亿模型租天价显卡?清华团队用CPU/GPU协同计算,让4090跑起671B参数全量模型!”

大家好,我是蚝油菜花。如果你也经历过——

  • 💸 看着API调用账单瑟瑟发抖,微调一次模型吃掉半月算力预算
  • 🖥️ 盯着OOM报错抓狂,为了跑通模型不得不阉割关键参数
  • ⏳ 等着预处理结果打瞌睡,长文本任务动辄消耗咖啡+泡面时间…

今天介绍的 KTransformers 将彻底改写游戏规则!这个由清华大学KVCache.AI团队与趋境科技联合研发的开源框架,通过GPU/CPU异构计算与MoE架构深度优化,实现了:

  • ✅ 24GB显存运行671B满血模型 —— 消费级显卡的逆袭
  • ✅ 286 tokens/s预处理速度 —— 比传统方案快28倍的闪电响应
  • ✅ CUDA Graph+4bit量化 —— 让千亿模型部署进家用电脑

从AI初创团队到高校实验室,开发者们正在用它突破算力围城。接下来我们将深入解析其MoE卸载策略与Marlin算子优化,手把手教你在本地搭建千亿级AI推理引擎!

🚀 快速阅读

KTransformers 是一个开源的大语言模型推理优化框架,旨在通过GPU/CPU异构计算策略和MoE架构的稀疏性,显著提升大模型的推理速度并降低硬件要求。

  1. 核心功能:支持在仅24GB显存的单张显卡上运行671B参数的满血版大模型,预处理速度最高可达286 tokens/s,推理生成速度达14 tokens/s。
  2. 技术原理:基于MoE架构的稀疏矩阵卸载、offload策略、高性能算子优化和CUDA Graph优化等技术,大幅降低显存需求并提升推理效率。

KTransformers 是什么

KTransformers

KTransformers 是由清华大学 KVCache.AI 团队联合趋境科技推出的开源项目,旨在优化大语言模型的推理性能,降低硬件门槛。它基于 GPU/CPU 异构计算策略,利用 MoE 架构的稀疏性,支持在仅 24GB 显存的单张显卡上运行 DeepSeek-R1、V3 等 671B 参数的满血版大模型,预处理速度最高可达 286 tokens/s,推理生成速度最高能达到 14 tokens/s。

项目通过基于计算强度的 offload 策略、高性能算子和 CUDA Graph 优化等技术,显著提升了推理速度,使得普通用户和中小团队能够在消费级硬件上运行千亿级参数模型,实现“家庭化”部署。

KTransformers 的主要功能

  • 支持超大模型的本地推理:支持在仅 24GB 显存的单张显卡上运行 DeepSeek-R1 等 671B 参数的满血版大模型,打破传统硬件限制。
  • 提升推理速度:预处理速度最高可达 286 tokens/s,推理生成速度达 14 tokens/s。
  • 兼容多种模型和算子:支持 DeepSeek 系列及其他 MoE 架构模型,提供灵活的模板注入框架,支持用户切换量化策略和内核替换,适应不同优化需求。
  • 降低硬件门槛:将大模型的显存需求大幅降低,让普通用户和中小团队能在消费级硬件上运行千亿级参数模型,实现“家庭化”部署。
  • 支持长序列任务:整合 Intel AMX 指令集,CPU 预填充速度可达 286 tokens/s,相比传统方案快 28 倍,将长序列任务的处理时间从“分钟级”缩短到“秒级”。

KTransformers 的技术原理

  • MoE架构:将稀疏的 MoE 矩阵卸载到 CPU/DRAM 上处理,稠密部分保留在 GPU 上,大幅降低显存需求。
  • offload策略:根据计算强度将任务分配到 GPU 和 CPU:计算强度高的任务(如 MLA 算子)优先分配到 GPU,计算强度低的任务分配到 CPU。
  • 高性能算子优化
    • CPU端:用 llamafile 作为 CPU 内核,结合多线程、任务调度、负载均衡等优化,提升 CPU 推理效率。
    • GPU端:引入 Marlin 算子,专门优化量化矩阵计算,相比传统库(如 Torch)实现 3.87 倍的加速效果。
  • CUDA Graph 优化:基于 CUDA Graph 减少 Python 调用开销,降低 CPU/GPU 通信的断点,实现高效的异构计算协同。每次 decode 仅需一个完整的 CUDA Graph 调用,显著提升推理性能。
  • 量化与存储优化:采用 4bit 量化技术,进一步压缩模型存储需求,仅需 24GB 显存即可运行 671B 参数模型。同时优化 KV 缓存大小,减少存储开销。
  • 模板注入框架:提供基于 YAML 的模板注入框架,支持用户灵活切换量化策略、内核替换等优化方式,适应不同场景的需求。

如何运行 KTransformers

准备工作

在开始安装和运行 KTransformers 之前,您需要确保系统满足以下依赖项:

1. 安装 CUDA

确保您的系统安装了 CUDA 12.1 或更高版本。如果没有安装,可以从NVIDIA 官方网站下载并安装。安装完成后,添加 CUDA 到系统路径:

  • NVIDIA 官方网站https://developer.nvidia.com/cuda-downloads
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export CUDA_PATH=/usr/local/cuda
2. 安装编译工具

确保您的 Linux 系统安装了 GCC、G++ 和 CMake。可以使用以下命令安装:

sudo apt-get update
sudo apt-get install gcc g++ cmake ninja-build
3. 创建 Python 虚拟环境

推荐使用 Conda 创建一个 Python 3.11 的虚拟环境来运行程序。可以通过以下命令创建并激活环境:

conda create --name ktransformers python=3.11
conda activate ktransformers

如果您是首次使用 Conda,可能需要先运行 conda init 并重新打开终端。

4. 安装依赖库

确保安装了 PyTorch、packaging、ninja 等依赖库:

pip install torch packaging ninja cpufeature numpy

安装 KTransformers

1. 下载源代码并编译

首先,克隆 KTransformers 的 GitHub 仓库并初始化子模块:

git clone https://github.com/kvcache-ai/ktransformers.git
cd ktransformers
git submodule init
git submodule update

如果您需要运行带有 Web 界面的版本,请先编译 Web 界面(可选):

# 请参考相关文档进行编译

然后根据您的操作系统选择安装方式:

1.1 Linux 系统

对于普通用户,可以使用简单的安装脚本:

bash install.sh

如果您有双 CPU 和 1TB 内存的服务器,可以启用 NUMA 优化:

export USE_NUMA=1
bash install.sh
1.2 Windows 系统

在 Windows 系统上,使用 install.bat 脚本进行安装:

install.bat
2. 使用 Makefile 编译和格式化代码

如果您是开发者,可以使用 Makefile 来编译和格式化代码。Makefile 的详细用法请参考相关文档

  • 相关文档https://github.com/kvcache-ai/ktransformers/doc/en/makefile_usage.html

本地聊天测试

KTransformers 提供了一个简单的命令行本地聊天脚本,用于测试模型。请注意,这是一个非常简单的测试工具,仅支持一轮对话,不保留历史输入。如果您想体验模型的完整功能,建议使用 RESTful API 和 Web UI。

运行示例
  1. 下载模型权重:
# 在克隆的仓库根目录下执行以下命令
mkdir DeepSeek-V2-Lite-Chat-GGUF
cd DeepSeek-V2-Lite-Chat-GGUF
wget https://huggingface.co/mzwing/DeepSeek-V2-Lite-Chat-GGUF/resolve/main/DeepSeek-V2-Lite-Chat.Q4_K_M.gguf -O DeepSeek-V2-Lite-Chat.Q4_K_M.gguf
cd ..
  1. 启动本地聊天:
python -m ktransformers.local_chat --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF

如果遇到连接 Hugging Face 的问题,可以尝试以下命令:

GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite
python -m ktransformers.local_chat --model_path ./DeepSeek-V2-Lite --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF

支持的模型和量化格式

1. 支持的模型
支持的模型已弃用的模型
DeepSeek-R1InternLM2.5-7B-Chat-1M
DeepSeek-V3
DeepSeek-V2
DeepSeek-V2.5
Qwen2-57B
DeepSeek-V2-Lite
Mixtral-8x7B
Mixtral-8x22B
2. 支持的量化格式
支持的格式已弃用的格式
Q2_K_LIQ2_XXS
Q2_K_XS
Q3_K_M
Q4_K_M
Q5_K_M
Q6_K
Q8_0

推荐的模型配置

模型名称模型大小VRAM最低 DRAM推荐 DRAM
DeepSeek-R1-q4_k_m377G14G382G512G
DeepSeek-V3-q4_k_m377G14G382G512G
DeepSeek-V2-q4_k_m133G11G136G192G
DeepSeek-V2.5-q4_k_m133G11G136G192G
DeepSeek-V2.5-IQ4_XS117G10G107G128G
Qwen2-57B-A14B-Instruct-q4_k_m33G8G34G64G
DeepSeek-V2-Lite-q4_k_m9.7G3G13G16G
Mixtral-8x7B-q4_k_m25G1.6G51G64G
Mixtral-8x22B-q4_k_m80G4G86.1G96G
InternLM2.5-7B-Chat-1M15.5G15.5G8G (32K 上下文)150G (1M 上下文)

RESTful API 和 Web UI(已弃用)

1. 无 Web 界面启动
ktransformers --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path /path/to/DeepSeek-V2-Lite-Chat-GGUF --port 10002
2. 带 Web 界面启动
ktransformers --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path /path/to/DeepSeek-V2-Lite-Chat-GGUF --port 10002 --web True
3. 使用 Transformers 启动

如果您想使用 Transformers 启动服务器,model_path 需要包含 .safetensors 文件:

ktransformers --type transformers --model_path /mnt/data/model/Qwen2-0.5B-Instruct --port 10002 --web True

访问 Web 界面的 URL 为:http://localhost:10002/web/index.html#/chat

更多关于 RESTful API 服务器的信息,请参考相关文档。您还可以参考Tabby 集成示例

  • 相关文档https://github.com/kvcache-ai/ktransformers/doc/en/api/server/server.html
  • Tabby 集成示例https://github.com/kvcache-ai/ktransformers/doc/en/api/server/tabby.html

资源


❤️ 如果你也关注 AI 的发展现状,且对 AI 应用开发感兴趣,我会每日分享大模型与 AI 领域的开源项目和应用,提供运行实例和实用教程,帮助你快速上手AI技术!

🥦 微信公众号|搜一搜:蚝油菜花 🥦

### KTransformers Library Information and Resources KTransformers is a specialized library designed to facilitate the use of transformer models within Kotlin-based applications, providing an interface that integrates seamlessly with existing machine learning frameworks such as Hugging Face's Transformers[^1]. The primary focus of this library lies in simplifying the process of deploying state-of-the-art NLP models into production environments by abstracting away much of the complexity associated with model management. #### Key Features - **Model Integration**: Supports integration with popular pre-trained models like BERT, RoBERTa, DistilBERT among others. - **Efficient Inference**: Optimized for efficient inference on both CPU and GPU hardware through leveraging advanced computational techniques. - **Ease of Use**: Designed with simplicity in mind, offering intuitive APIs tailored specifically towards developers working primarily in Kotlin. #### Installation Guide To incorporate KTransformers into projects, one can add dependencies via Gradle or Maven repositories depending upon project setup preferences: For Gradle users: ```groovy dependencies { implementation 'com.example.ktransformers:ktransformers:<version>' } ``` Maven configuration would look similar but adapted according to specific requirements documented officially. #### Example Usage Code Snippet Below demonstrates how easily one might load a tokenizer alongside initializing a sequence classification pipeline using KTransformers: ```kotlin import com.example.ktransformers.pipeline.SequenceClassificationPipeline import com.example.ktransformers.tokenization.BertTokenizer fun main() { val tokenizer = BertTokenizer.fromPretrained("bert-base-uncased") val classifier = SequenceClassificationPipeline(modelNameOrPath = "distilbert-base-uncased-finetuned-sst-2-english", tokenizer) println(classifier.invoke("I love programming!")) } ``` --related questions-- 1. What are some common issues encountered while setting up KTransformers? 2. How does KTransformers compare against other libraries when it comes to performance benchmarks? 3. Can you provide examples where KTransformers has been successfully deployed at scale? 4. Are there any community forums dedicated solely to discussing developments around KTransformers?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值