KTransformers: The Open-Source Framework That Runs 671B AI Models on Consumer Hardware
KTransformers enables efficient inference and fine-tuning of large MoE models using CPU-GPU heterogeneous computing, achieving 3-28x speedup on consumer hardware.
If you've ever wanted to run massive AI models like DeepSeek-R1 or GPT-4-class models but been blocked by hardware requirements, KTransformers might be your answer. This open-source framework from Tsinghua University's MADSys Lab is revolutionizing how developers deploy and fine-tune large language models by intelligently splitting computation between CPUs and GPUs.
Unlike traditional approaches that demand expensive multi-GPU setups, KTransformers uses heterogeneous computing to place different parts of a model on the most appropriate hardware. The result? You can now run 671-billion parameter models on a single RTX 4090 with enough system RAM.
What Makes KTransformers Different?
Most LLM inference frameworks treat your hardware as a monolithic resource. KTransformers takes a fundamentally different approach: it understands that different parts of a model have different computational characteristics.
For Mixture-of-Experts (MoE) models—which are becoming the standard for large-scale AI—KTransformers implements intelligent expert placement:
- Hot experts (frequently activated) run on GPU for maximum speed
- Cold experts (rarely activated) stay on CPU to save VRAM
- NUMA-aware memory management optimizes data locality across CPU sockets
This isn't just theoretical. In benchmarks, KTransformers achieves 6-12x training speedup over ZeRO-Offload for MoE fine-tuning workloads, while using about half the CPU memory of previous approaches.
Key Technical Features
1. Advanced Quantization Support
KTransformers supports multiple quantization strategies to reduce memory footprint:
- INT4/INT8 on CPU: Quantized weights stored in system RAM, dequantized on-the-fly
- FP8 on GPU: Native FP8 kernel support for compatible GPUs (Ada Lovelace and later)
- GPTQ support: GPU-side quantization for dense layers
- IQ1_S/FP8 hybrid: Mixed precision for optimal quality and performance
Here's how you'd configure quantized inference:
from ktransformers.optimize import OptimizedModule
# Load model with CPU-GPU hybrid placement
model = OptimizedModule.from_pretrained(
"deepseek-ai/DeepSeek-R1",
device_map="auto",
torch_dtype="auto"
)
# The framework automatically places:
# - Attention layers on GPU
# - MoE experts based on activation frequency
# - Embeddings on CPU (they're only used once)
2. Hardware Acceleration
KTransformers leverages modern CPU instruction sets for maximum throughput:
- Intel AMX: Advanced Matrix Extensions for INT8/BF16 operations
- AVX-512: 512-bit vector operations for quantized inference
- AVX2: Fallback for older CPUs (still optimized)
On the GPU side, it supports:
- NVIDIA GPUs via CUDA (including Tensor Cores)
- AMD GPUs via ROCm
- Intel Arc GPUs via oneAPI
- Ascend NPUs (Huawei AI accelerators)
3. Production-Ready Integration
KTransformers isn't just a research project. It integrates with production serving frameworks:
# Install KTransformers kernel
cd kt-kernel
pip install .
# Use with SGLang for production serving
python -m sglang.launch_server --model-path deepseek-ai/DeepSeek-R1 --port 30000 --tp 4 --mem-fraction-static 0.85
The SGLang integration enables multi-concurrency serving, prefix caching, and automatic load balancing—all while using KTransformers' heterogeneous compute backend.
Real-World Performance
Let's look at actual performance numbers from production deployments:
| Model | Hardware | Total Throughput | Output Throughput |
|---|---|---|---|
| DeepSeek-R1-0528 (FP8) | 8×L20 GPU + Xeon Gold 6454S | 227.85 tokens/s | 87.58 tokens/s (8-way) |
| DeepSeek-V3 | 4×RTX 4090 | 3.7 it/s (training) | - |
| Qwen3-30B-A3B | 1×RTX 4090 | 8+ it/s (training) | - |
For context, running DeepSeek-R1 on a single RTX 4090 with 24GB VRAM and 382GB system RAM achieves 3-16 tokens/s depending on context length and quantization settings. That's usable for development and small-scale production.
Real-World Example: Building a Local AI Assistant
Here's a complete example of setting up KTransformers for a local coding assistant using DeepSeek-Coder-V2:
from ktransformers.optimize import OptimizedModule
from transformers import AutoTokenizer
import torch
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Instruct")
# Load model with automatic CPU-GPU placement
model = OptimizedModule.from_pretrained(
"deepseek-ai/DeepSeek-Coder-V2-Instruct",
device_map="auto",
torch_dtype=torch.bfloat16
)
# Define your prompt
prompt = """You are an expert Python developer.
Write a function that processes a large CSV file efficiently using generators.
Requirements:
- Memory efficient (don't load entire file)
- Handle errors gracefully
- Include type hints"""
# Generate response
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.7,
do_sample=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)
This setup uses about 18GB VRAM and 60GB system RAM, running at ~8 tokens/s on an RTX 3090. Fast enough for interactive use, and you own the entire stack.
Key Benefits
- Run massive models on consumer hardware: No need for $50k GPU clusters
- 6-12x faster fine-tuning: Compared to ZeRO-Offload for MoE models
- Day0 model support: New models supported within days of release
- Production-ready: Integrates with SGLang, LLaMA-Factory, and more
- Multi-hardware support: NVIDIA, AMD, Intel, Ascend—all in one framework
- Active development: Backed by Tsinghua University and Approaching.AI
- Open source: MIT license, community-driven improvements
Getting Started
Ready to try KTransformers? Here's the quick start:
# Clone the repository
git clone https://github.com/kvcache-ai/ktransformers.git
cd ktransformers
# Install the kernel
cd kt-kernel
pip install .
# Run inference
python examples/inference.py --model deepseek-ai/DeepSeek-R1-Distill-Qwen-32B --prompt "Explain quantum computing in simple terms"
For fine-tuning with LLaMA-Factory:
cd /path/to/LLaMA-Factory
pip install -e .
pip install -r requirements/ktransformers.txt
# Launch distributed training
CUDA_VISIBLE_DEVICES=0,1,2,3 accelerate launch \
--config_file examples/ktransformers/accelerate/fsdp2_kt_int8.yaml \
src/train.py \
examples/ktransformers/train_lora/deepseek_r1_lora_sft_kt.yaml
Frequently Asked Questions
Q: What hardware do I need to run KTransformers?
A: Minimum requirements depend on the model size. For DeepSeek-R1 (671B parameters), you need at least 24GB VRAM GPU and 128GB system RAM. For smaller MoE models like Qwen3-30B-A3B, a single RTX 4090 with 64GB RAM is sufficient. The framework also supports CPU-only inference (with AVX2 or better) for development purposes.
Q: How does KTransformers compare to vLLM or Text Generation Inference (TGI)?
A: KTransformers excels at heterogeneous CPU-GPU workloads, especially for MoE models. While vLLM and TGI are optimized for GPU-only serving, KTransformers intelligently uses CPU for cold experts and quantized weights, enabling larger models on limited GPU hardware. For pure GPU inference on dense models, vLLM may be faster, but for MoE models on mixed hardware, KTransformers wins.
Q: Can I use KTransformers for production serving?
A: Yes. KTransformers integrates with SGLang for production-grade serving with features like continuous batching, prefix caching, and multi-concurrency support. Several companies are already using it in production for internal AI tools and customer-facing applications.
Q: Does KTransformers support non-MoE models like LLaMA or Mistral?
A: Yes, KTransformers supports both MoE and dense transformer models. However, the biggest performance gains come from MoE-specific optimizations. For dense models, you'll still benefit from CPU-GPU hybrid inference and advanced quantization, but the speedup is less dramatic than with MoE architectures.
Q: What's the license? Can I use it commercially?
A: KTransformers is released under the MIT license, which allows commercial use, modification, and distribution. You can use it in proprietary products without restrictions. Just include the license notice in your distribution.
Ready to run large language models on your own hardware? Check out KTransformers on GitHub and join the community of developers building efficient AI infrastructure. For more developer tools and frameworks, explore our comprehensive courses on modern software development.