
kvpress
一行代码压缩 KV 缓存,让长上下文推理更快更省显存
项目预览
项目数据
项目介绍
Star 增长趋势
本站每日记录该项目 GitHub 星标变化,观测越久曲线越完整
数据来源:GitHub API 逐日快照 · 收录于 2026-08-09 · 当前星标 ★ 1246
使用教程
环境要求
- Python 环境(可用 pip 安装包)
- 本地源码安装需先安装 uv(uv sync 使用)
- 运行压缩推理需要 transformers 与 PyTorch
- 长上下文模型推理需要足够显存(README 举例 1M tokens 的 Llama 3.1-70B float16 需 330GB)
- 多 GPU 推理需 accelerate(README 说明通过 accelerate 支持)
安装与启动步骤
-
1安装 kvpress
最简安装方式,直接用 pip 从 PyPI 安装 kvpress 包。
pip install kvpress -
2本地源码安装
使用 uv 从 GitHub 克隆仓库并同步依赖,适合本地开发与修改源码。
git clone https://github.com/NVIDIA/kvpress.git cd kvpress uv sync -
3安装可选依赖
需要评估工具或 flash-attn 时,用 --extra 安装可选依赖,普通使用可跳过。
git clone https://github.com/NVIDIA/kvpress.git cd kvpress uv sync --extra eval --extra flash-attn -
4运行压缩推理
导入 kvpress 后 pipeline 自动注册为 kv-press-text-generation,用 press 对上下文做压缩。
from transformers import pipeline from kvpress import ExpectedAttentionPress model = "Qwen/Qwen3-8B" pipe = pipeline("kv-press-text-generation", model=model, device_map="auto", dtype="auto") context = "A very long text you want to compress once and for all" question = "\nA question about the compressed context" press = ExpectedAttentionPress(compression_ratio=0.5) answer = pipe(context, question=question, press=press)["answer"] -
5启用解码阶段压缩
实验特性 DecodingPress 包装 ScorerPress,在生成过程中周期性压缩 KV 缓存。
from transformers import pipeline from kvpress import KnormPress from kvpress import DecodingPress -
6多 GPU 推理
README 说明 kvpress 通过 accelerate 支持多 GPU,设置 device_map="auto" 即可。
pipe = pipeline("kv-press-text-generation", model=model, device_map="auto")
关键配置
| 配置项 | 必填 | 说明 | 示例 |
|---|---|---|---|
compression_ratio | 是 | 每个 press 关联的压缩率,控制 KV 缓存压缩程度 | 0.5 |
device_map | 否 | 模型放置策略,设为 auto 可自动分配(含多 GPU) | auto |
dtype | 否 | 模型权重加载精度,auto 自动选择 | auto |
base_press | 否 | DecodingPress 使用的基础 ScorerPress | KNormPress |
compression_interval | 否 | DecodingPress 每隔多少步压缩一次,默认 512 | 512 |
target_size | 否 | DecodingPress 压缩后的目标缓存大小,默认 2048 | 2048 |
如何确认成功
运行示例代码不报错并成功输出 answer;导入 kvpress 后 transformers 中可用 kv-press-text-generation 管道名。
常见问题
Q:如何在多个 GPU 上推理?
A:kvpress 通过 accelerate 支持多 GPU,构造 pipeline 时设置 device_map="auto" 即可,无需其他改动。
Q:压缩作用在哪些 token 上?
A:示例中压缩只应用于 context token,因此可以对同一份压缩后的上下文提出不同问题来评估压缩效果。
Q:解码阶段压缩和默认压缩有什么区别?
A:默认在 prefilling 阶段压缩;DecodingPress 在生成过程中周期压缩,用 target_size 而非 compression_ratio 控制缓存大小,属实验特性。
Q:压缩率如何选择?
A:创建 press 时传入 compression_ratio,例如 ExpectedAttentionPress(compression_ratio=0.5)。
Q:有没有更完整的示例?
A:README 指向 notebooks/wikipedia_demo.ipynb 的 Wikipedia notebook demo,Colab 上也有同版本可运行。
注意事项
- 本地源码安装使用 uv(uv sync),不是 pip。
- DecodingPress 为实验性功能,使用前注意稳定性。
- hidden_states_buffer_size 默认 256,部分 press 不需要缓冲隐藏状态时可设为 0。
- 只依据 README 内容,未提及的端口、路径、版本号等请勿自行添加。
核心亮点
- 统一 API 集成多种压缩算法,切换方法只需改参数,实验效率高
- 支持 HuggingFace 模型无缝接入,无需改动模型代码即可压缩
- 内置评估工具,可量化压缩对困惑度和下游任务的影响
不足之处
- 文档/社区待观察
- 压缩方法的效果依赖模型和任务,需自行调参验证
适用场景
- 长文本生成场景下降低显存占用,支持更大批处理或更长上下文
- 边缘设备或资源受限环境部署 LLM,减少内存需求
- 研究对比不同 KV 压缩算法在特定模型上的效果
替代项目
llm-kv-cache、KVQuant、Quantized KV Cache
同类项目推荐
hermes-hudui
开源
给持久记忆 AI 智能体装一块实时意识仪表盘
Web UI consciousness monitor for Hermes — the AI agent with persistent memory
hyperspace-db
开源
1-bit 量化省内存,原生支持双曲层级向量检索
[H] HyperspaceDB is a high-performance, vector database. It features 1-bit quantizat···
gpu_poor
开源
选显卡前先算一算:模型显存和速度一键估算
Calculate token/s & GPU memory requirement for any LLM. Supports llama.cpp/ggml/bnb···
steward
开源
给AI代理装上安全钱包,密钥策略一把抓
Agent wallet infrastructure — encrypted keys, policy enforcement, credential proxy,···
lucebox
开源
消费级显卡也能跑出更快的本地大模型推理
LLM speculative inference server for heterogeneous hardware & consumer GPUs
agentos
开源
把代理当进程跑,无需沙箱虚拟机,直接嵌入现有后端。
Tiny, fast, Linux-compatible sandboxes. Powered by WebAssembly.
Deep-learning-in-cloud
开源
一张清单,快速选对深度学习云服务商
List of Deep Learning Cloud Providers
SkillCorpus
开源
把零散技能文件,变成智能体随手可用的检索库
Open-source infrastructure that turns scattered SKILL.md files into curated, retriev···
antfly
开源
用 Zig 打造高性能 RAG 智能体,快速实现知识库问答