知识讲堂

← 返回日报
算法理论 第一讲

KV缓存压缩

历史演进

Transformer 自回归解码需缓存全部历史 token 的 Key-Value 向量,其内存随序列长度线性增长,在长上下文与高并发场景下成为 GPU 显存的首要瓶颈——KV 缓存压缩正是为突破这一瓶颈而生。

2017
Transformer 与 KV 缓存机制的确立

Vaswani et al. 在 "Attention Is All You Need" 中提出 Transformer。自回归推理时,为避免对已处理 token 重复计算注意力,每层将 Key 和 Value 向量缓存下来供后续 token 查询,这就是 KV Cache。在当时的短序列场景(几百 token)下,KV 缓存的内存开销微不足道,但它为日后的内存危机埋下了伏笔。

2019-2020
稀疏与线性注意力的理论奠基

Child et al. (OpenAI, 2019) 提出 Sparse Transformer,用固定稀疏模式(strided + local)将注意力从 $O(n^2)$ 降至 $O(n\sqrt{n})$,首次证明"并非所有 token 对都需要计算注意力"。Wang et al. (2020) 提出 Linformer,通过低秩投影将 K、V 矩阵压缩到固定维度。Katharopoulos et al. (2020) 提出线性注意力,用核函数近似 softmax 使注意力可递推计算。这些工作建立了"注意力天然稀疏、可被压缩"的理论基础,但多数需从头训练,无法直接应用于已有大模型。

2023
训练后压缩的实用突破:H2O 与 StreamingLLM

Zhang et al. (NeurIPS 2023) 提出 H2O(Heavy-Hitter Oracle),通过统计发现注意力分数高度集中在少数"重击手"token 上,据此设计动态驱逐策略——仅保留累积注意力最高的 token,其余驱逐。同年 Xiao et al. (MIT) 发现"注意力汇聚"(Attention Sink)现象:无论输入内容如何,序列开头几个 token 总获得异常高的注意力权重,这是 softmax 归一化的数学副产物。StreamingLLM 据此提出"保留初始 sink token + 滑动窗口"的极简策略,使 LLM 以恒定内存处理无限长流式输入。这两项工作的核心贡献是证明了 KV 缓存可以在不重新训练的前提下大幅压缩,开启了训练后压缩的实用化浪潮。

2024
多维度压缩策略的系统化

这一年涌现出覆盖驱逐、合并、量化、架构四个维度的方案。SnapKV (Li et al.) 通过观察窗口自动识别每个注意力头的关键 token 模式,实现自适应驱逐。PyramidKV 发现底层注意力分散需更多 token、高层注意力集中可大幅压缩,提出金字塔形逐层预算分配。KIVI (Liu et al.) 从量化角度切入,对 Key 按通道、Value 按 token 分别量化到 2-bit,将缓存压缩至 1/8。最具架构影响力的是 DeepSeek-V2 提出的 MLA(Multi-head Latent Attention),将 KV 联合压缩为低维潜在向量,从根本上重新定义了缓存结构。

2025-2026
端到端学习压缩与规模化部署

最新研究(如本日论文 [9])转向端到端学习压缩:训练专用压缩模块将长上下文 KV 缓存映射为紧凑表示,同时通过与语言模型联合优化保持输出质量。这标志着从"手工启发式规则"到"数据驱动学习策略"的范式转变,且开始关注压缩本身的计算效率——压缩一个 100K 上下文不应比推理本身更慢。

核心思想
在有限显存预算下,智能选择"记住哪些 token、遗忘哪些、以何精度记住",使大模型以恒定或亚线性内存处理任意长上下文,同时最大限度保留生成质量。
数学结构

标准多头注意力中,KV 缓存的总内存为 $M_{KV} = 2 \times L \times H_{KV} \times n \times d_h \times b$,其中因子 2 对应 K 和 V,$L$ 为层数,$H_{KV}$ 为 KV 头数(GQA 下远小于查询头数),$n$ 为序列长度,$d_h$ 为每头维度,$b$ 为每元素字节数。以 LLaMA-2-70B($L=80, H_{KV}=8, d_h=128$)处理 128K 上下文(FP16)为例,$M_{KV} = 2 \times 80 \times 8 \times 128000 \times 128 \times 2 \approx 40$ GB,已超过单张 A100 的一半显存。驱逐策略的核心是定义 token 重要性。H2O 将 token $i$ 的重要性定义为累积注意力分数:$s_i = \sum_{j > i} A_{j,i}$,其中 $A_{j,i} = \text{softmax}(q_j k_i^T / \sqrt{d_h})$ 是位置 $j$ 对位置 $i$ 的注意力权重。保留 $s_i$ 最大的 $m$ 个 token,压缩比 $r = m/n$。这一定义的合理性在于:累积注意力高的 token 是被后续生成反复"查阅"的信息锚点,丢弃它们会导致信息断裂。量化压缩方面,KIVI 的关键洞察是 K 和 V 的数值分布特征不同,需要不同的量化粒度。Key 参与点积 $q \cdot k$,通道间相对大小决定注意力分数,因此按通道(per-channel)量化:$\hat{k}_{:,c} = \text{round}((k_{:,c} - z_c) / s_c) \cdot s_c + z_c$。Value 参与加权求和 $\sum_i A_i v_i$,token 间相对大小决定输出贡献,因此按 token(per-token)量化:$\hat{v}_{t,:} = \text{round}((v_{t,:} - z_t) / s_t) \cdot s_t + z_t$。MLA 从架构层面压缩:将 $h_t$ 通过下投影矩阵 $W^{DKV} \in \mathbb{R}^{d \times d_c}$ 映射为潜在向量 $c_t = W^{DKV} h_t$($d_c \ll H \cdot d_h$),推理时再通过上投影恢复 $k_t = W^{UK} c_t, v_t = W^{UV} c_t$。缓存中只存 $c_t$,内存从 $O(2 H d_h)$ 降至 $O(d_c)$,DeepSeek-V2 中 $d_c = 512$ 而 $2Hd_h = 32768$,压缩比达 64 倍。

工作机制

KV 缓存压缩的整体逻辑是:在自回归解码的每一步,对不断增长的 KV 缓存施加压缩操作(驱逐、合并、量化或潜在映射),使其内存占用保持在预算内,同时最大限度保留对后续生成至关重要的上下文信息。

Step 1注意力模式分析与校准

在部署压缩策略前,需在校准数据集上分析模型的注意力分布特征。通过在每层注册 attention hook,收集注意力权重矩阵,统计三个关键特征:(a) Attention Sink 强度——序列首 token 获得的平均注意力占比,通常在 20%-60% 之间;(b) 局部性半径——最近多少个 token 覆盖了 80% 的注意力质量;(c) 层间方差——不同层的注意力集中度差异。这一步之所以必要,是因为不同模型(甚至同一模型的不同层)的注意力模式差异巨大,盲目套用统一策略会导致某些层严重退化。校准通常只需几百条样本,耗时几分钟。

Step 2逐层预算分配

根据校准结果,为每层分配 KV 缓存预算。PyramidKV 的策略最具代表性:将 $L$ 层按注意力集中度排序,底层(注意力分散)分配更多 token 预算,高层(注意力集中)分配更少,总预算满足 $\sum_{l=1}^{L} b_l = B$。具体实现中,可以用注意力熵 $H_l = -\sum_i A_i \log A_i$ 作为分散度指标,熵越高的层分配越多预算。此外,需为 Attention Sink token(通常前 4 个)在所有层预留固定位置,因为驱逐它们会导致注意力分数分布崩溃。关键设计决策:预算分配是静态(部署前确定)还是动态(根据每条输入实时调整)?静态更简单高效,动态更灵活但引入额外计算开销。

Step 3在线压缩执行

在推理过程中,每当 KV 缓存超出预算时触发压缩。三类策略可独立或组合使用:(a) 驱逐(Eviction):丢弃低重要性 token 的 KV 向量。StreamingLLM 的实现最简洁:def evict(kv_cache, new_kv, sink=4, window=1024): kv_cache.append(new_kv) if len(kv_cache) > sink + window: kv_cache = kv_cache[:sink] + kv_cache[-window:] return kv_cache H2O 则维护重要性分数的在线估计,每步更新并驱逐最低分 token。驱逐是不可逆的——一旦丢弃,信息永久丢失。(b) 合并(Merging):将相邻或相似 token 的 KV 向量加权平均。D2O (2024) 按注意力权重加权合并:$\tilde{v} = \frac{s_i v_i + s_j v_j}{s_i + s_j}$。合并比驱逐保留更多信息,但引入近似误差,且合并后的"虚拟 token"不对应真实输入位置,可能影响位置编码(RoPE)的准确性。(c) 量化(Quantization):降低数值精度。KIVI 的实现将缓存分为两个区域:最近 $r$ 个 token 保持 FP16 全精度(它们对当前生成最关键),其余 token 量化到 2-bit。量化操作本身很快(几微秒),且与驱逐正交——可以先驱逐再量化,双重压缩。

Step 4质量监控与回退机制

压缩后需验证输出质量。离线评估使用困惑度(PPL)、长文本检索(Needle-in-a-Haystack)、下游任务准确率等指标。在线部署中,可以监控生成 token 的 logit 熵:如果压缩后熵突然升高(模型变得"不确定"),说明压缩过于激进,需要回退到更保守的策略。端到端学习压缩方法(论文 [9] 的方向)通过将压缩模块与语言模型联合训练来自动学习最优压缩策略,避免手工调参。训练目标通常是最小化压缩前后输出分布的 KL 散度:$\mathcal{L} = D_{KL}(P_{full} \| P_{compressed})$。

Step 5推理引擎集成

KV 缓存压缩需与推理引擎的内存管理深度集成。vLLM 的 PagedAttention 将 KV 缓存组织为固定大小的物理块(block),压缩策略需在块级别操作——驱逐整个块而非单个 token,以避免内存碎片。TensorRT-LLM 原生支持 INT8/FP8 KV 缓存量化,只需在构建引擎时设置 `kv_cache_type=int8`。SGLang 的 RadixAttention 通过前缀树共享不同请求间的公共 KV 缓存,压缩策略需与共享机制兼容——不能驱逐被多个请求引用的 token。集成时的关键工程决策:(a) 压缩触发时机——每 N 步定期压缩(可预测延迟)还是缓存满时紧急压缩(延迟不可预测);(b) 与连续批处理的兼容——同一批次中不同请求可能处于不同压缩阶段;(c) 压缩操作是否可以与 GPU 计算重叠(pipeline 化)。

长远价值

KV 缓存压缩直接决定了 LLM 的最大上下文长度和并发服务能力。vLLM PagedAttention、DeepSeek-V2 MLA、TensorRT-LLM INT8 KV Cache 已在工业界大规模部署。对于音视频多模态模型,视觉和音频 token 数量远超文本(一段 10 秒视频可产生数千 token),KV 缓存压缩是实现实时多模态推理的关键使能技术。随着上下文窗口从 128K 向 1M+ 扩展,该技术的重要性只会持续增长。

前沿动向

当前前沿包括:(1) 端到端学习压缩——用可训练模块替代手工规则,自动适应不同任务;(2) 跨层 KV 共享——不同层复用压缩后的潜在表示,进一步减少冗余;(3) 多模态差异化压缩——视觉 token 与文本 token 采用不同压缩策略;(4) 硬件协同设计——针对 FP4/FP6 等新数据类型优化量化方案。核心未解决问题:极端压缩比下如何保持长距离推理链的准确性。

往期讲解档案 103 个知识点

2026年06月09日浏览器端模型推理WebGPUBrowser InferenceClient-side ML
2026年06月02日残差向量量化Residual Vector QuantizationAudio TokenizationNeural Codec
2026年05月31日彩色噪声扩散采样Colored NoiseSpectral BiasSDE Solver
2026年05月31日算子融合编译原理Kernel FusionOperator Fusiontorch.compile
2026年05月20日AI生成内容溯源 (7 chars) ✓Content ProvenanceC2PASynthID
2026年05月19日推理委托设计模式 (8 chars) ✓ Let me now write the full content for both topics. Actually, I realize I should double-check: is "Runtime Abstraction" or "Delegate Pattern" too close to any covered keyword? Looking at the list: Cluster Scheduling, Co-design, Collective Communication, Computational Expressiveness, CUTLASS... No, none of these overlap. OK, let me finalize and write everything out. Let me reconsider the engineering topic one more time. The title "推理委托设计模式" is good but maybe I should make it even more specific and interesting. What about focusing on the "graph partitioning" aspect - how ML runtimes decide which operations to delegate to which backend? This is a more specific and technically interesting angle. No, I think the broader "delegate pattern" is better because it covers the full picture and is more useful to the reader. Let me write it all out now. For the engineering topic, let me think about the history: - Early days: frameworks were monolithic (Caffe, early TensorFlow) - TensorRT (2017): NVIDIA's specialized inference engine, initially standalone - ONNX Runtime (2019): Microsoft introduced "Execution Providers" - the first major delegate-like abstraction - TFLite delegates (2018-2019): TensorFlow Lite introduced delegate API for GPU, NNAPI, etc. - ExecuTorch (2023-2024): PyTorch's answer to edge inference with a clean delegate API - 2025-2026: MLX delegate, Qualcomm AI Engine Direct delegate, etc. This is a rich history. Let me write it up. Actually, I want to make sure I have the dates right: - TensorRT was first released around 2016-2017 - ONNX was announced in 2017 - ONNX Runtime was open-sourced in 2018-2019 - TFLite was released in 2017, delegates came later - ExecuTorch was announced at PyTorch Conference 2023 OK, let me write everything now. I'll be thorough. One more thought: for the MATH section of the engineering topic, I could discuss the graph partitioning problem formally, or I could discuss the cost model for deciding whether to delegate. Let me think... The graph partitioning for delegation can be formalized as: - Given a computation graph $G = (V, E)$ where $V$ is the set of operations and $E$ is data dependencies - And a set of delegates $D = \{d_1, ..., d_m\}$ where each delegate supports a subset of operations $S_i \subseteq V$ - Find a partition $P = \{P_0, P_1, ..., P_m\}$ (where $P_0$ is the fallback/CPU partition) that minimizes total execution time - Subject to: each $
2026年05月17日连续批处理异步化
2026年05月10日连续潜在扩散语言模型Continuous Latent DiffusionNon-autoregressive LMDiffusion Language Model
2026年05月10日零样本跨语言语音克隆Zero-shot Voice CloningCross-lingual TTSIPA Phoneme Representation
2026年05月09日最优传输音频距离Optimal TransportFréchet Audio DistanceRiemannian Metric
2026年05月09日推荐系统推理核内广播优化Intra-kernel BroadcastEmbedding TableRecommendation Inference
2026年05月08日扩散Transformer异常值令牌Outlier TokensDiffusion TransformerAttention Sink
2026年05月08日WebRTC低延迟语音工程WebRTCReal-time AudioJitter Buffer
2026年05月07日音素级深度伪造检测原理Phoneme-level Deepfake DetectionSelf-supervised Speech EmbeddingEmotional Speech Synthesis
2026年05月07日AI训练网络协议工程哲学Multi-path Reliable ConnectionRDMA over EthernetCollective Communication
2026年05月05日对抗解纠缠说话人验证Speaker DisentanglementAdversarial TrainingCross-lingual Verification
2026年05月05日LLM推理为何用语言Chain-of-ThoughtLatent ReasoningToken Space
2026年05月01日跨架构知识蒸馏原理Cross-Architecture DistillationDiffusion LMAutoregressive Teacher
2026年05月01日AI评估计算瓶颈工程LLM Evaluation InfrastructureBenchmark SaturationEval Compute Bottleneck
2026年04月29日平衡传输语音增强Schrödinger BridgeStochastic Differential EquationSpeech Enhancement
2026年04月29日深度学习理论形成Deep Learning TheoryLoss LandscapeNeural Tangent Kernel
2026年04月28日语义进度函数原理Semantic Progress FunctionDiffusion TrajectoryNonlinear Denoising
2026年04月28日大模型OCR选型认知OCR BenchmarkModel Selection BiasCost-Performance Tradeoff
2026年04月27日说话人验证核心原理Speaker Verificationd-vectorECAPA-TDNN
2026年04月27日GPU核函数语言选型GPU Kernel EngineeringCuTe DSLCUTLASS
2026年04月26日时长控制TTS原理Duration ModelingProsody ControlAutoregressive TTS
2026年04月26日AI研究价值评估困境Research EvaluationPublication BiasPeer Review
2026年04月25日全双工对话建模原理Full-Duplex DialogueTurn-TakingVoice Activity Detection
2026年04月25日流式TTS文本规范化Text NormalizationStreaming TTSInverse Text Normalization
2026年04月24日离散扩散语言模型原理Discrete DiffusionMasked Diffusion Language ModelAbsorbing Diffusion
2026年04月24日跟进ML研究的认知工程Information OverloadResearch TriageSpaced Repetition
2026年04月23日一致性正则化ASR原理Consistency RegularizationUnified ASRTransducer
2026年04月23日流式TTS文本规范化工程Text NormalizationStreaming TTSInverse Text Normalization
2026年04月22日神经编码器伪影检测Neural Audio CodecArtifact DetectionForensic Residual
2026年04月22日AI研究复现危机工程Reproducibility CrisisML EngineeringExperimental Rigor
2026年04月21日扩散SNR偏差校正原理Signal-to-Noise RatioDiffusion Timestep BiasScore Matching
2026年04月21日论文复现危机根治工程Reproducibility CrisisAblation StudyExperimental Rigor
2026年04月20日音频时序定位原理Temporal GroundingAudio Event DetectionCross-modal Alignment
2026年04月20日论文复现危机根因Reproducibility CrisisBenchmark OverfittingEvaluation Validity
2026年04月19日流匹配对齐原理Flow Matching AlignmentReward Gradient BackpropagationTrajectory Optimization
2026年04月19日分布式训练任务编排Distributed Training OrchestrationCluster SchedulingFault Tolerance
2026年04月18日最优传输信号融合Optimal TransportWasserstein BarycenterTime-Frequency Resolution
2026年04月18日分布式训练任务调度Distributed Training OrchestrationJob SchedulingCluster Resource Management
2026年04月17日扩散语音识别原理Masked Diffusion Language ModelDiscrete DiffusionASR Decoding
2026年04月17日Mel尺度跨文化偏差Mel ScalePsychoacousticsCultural Bias
2026年04月16日音频水印对抗原理Audio WatermarkingSemi-FragilePsychoacoustic Masking
2026年04月16日推测解码草稿树工程Speculative DecodingDraft TreeBlock Diffusion
2026年04月15日对抗流模型原理Continuous Normalizing FlowAdversarial TrainingFlow Matching
2026年04月15日代理状态可观测性工程Agent ObservabilityDistributed TracingState Machine Debugging
2026年04月14日过程奖励模型原理Process Reward ModelStep-level SupervisionReasoning Chain
2026年04月13日离散令牌音源分离Discrete Token ModelingSource SeparationConditional Generation
2026年04月13日超算API工程哲学Distributed Training OrchestrationSupercomputer API DesignFault Tolerance
2026年04月12日信息瓶颈原理演进Information BottleneckVariational IBDisentanglement
2026年04月12日Safetensors格式工程哲学SafetensorsModel SerializationMemory-Mapped IO
2026年04月11日归一化层演进原理Layer NormalizationRMS NormalizationBatch Normalization
2026年04月11日GEMM自调优后端工程GEMM AutotuningTorchInductorCuteDSL
2026年04月10日多令牌预测原理Multi-Token PredictionSpeculative DecodingMedusa Heads
2026年04月10日ML从业者认知校准Calibration BiasCapability IllusionBenchmark Overfitting
2026年04月09日编码器-解码器LM原理Encoder-Decoder LMCross-Attention ConditioningSequence-to-Sequence
2026年04月09日torch.compile归一化优化torch.compileLayerNormRMSNorm
2026年04月08日KV缓存压缩原理KV Cache CompressionRoPE Position EncodingAttention Score Estimation
2026年04月08日音效基础模型工程Sound Effect GenerationFoundation ModelFoley Synthesis
2026年04月07日可验证奖励强化学习Verifiable RewardRLVRProcess Reward Model
2026年04月07日LLM技能退化认知机制Cognitive OffloadingSkill AtrophyDesirable Difficulty
2026年04月06日音素可解释说话人验证Phoneme-aware Speaker VerificationInterpretable BiometricsLocal Acoustic Evidence
2026年04月06日音频幻觉攻击评估Hallucination AttackAudio Language Model ReliabilityAdversarial Probing
2026年04月05日潜在空间推理原理Latent Space ReasoningContinuous RepresentationToken-Free Inference
2026年04月05日mRNA模型极低成本训练Biology Foundation ModelCross-Species TransferLow-Budget Training
2026年04月04日编码器-解码器TTS原理Encoder-Decoder TTSText ConditioningPositional Capacity
2026年04月04日大模型训练的MXFP8工程MXFP8MicroscalingMixed Precision Training
2026年04月03日在线知识蒸馏原理Online DistillationKnowledge TransferStudent-Teacher
2026年04月03日MoE专家并行调度工程Expert ParallelismMixture of ExpertsAll-to-All Communication
2026年04月02日波形潜空间扩散TTSwaveform latent diffusionnon-autoregressive TTSlatent space acoustic modeling
2026年04月02日波形隐空间扩散原理waveform latent spacediffusion TTSVAE audio codec
2026年04月02日LLM量化权重工程weight quantizationLLM compression4-bit quantization
2026年04月02日扩散语言模型离散生成Discrete DiffusionMasked Diffusion Language ModelNon-autoregressive TTS
2026年04月02日LLM后训练库工程演进RLHF engineeringPPO training stabilityreward hacking
2026年04月02日声学证据瓶颈原理Audio Evidence BottleneckAcoustic GroundingAudio Language Model
2026年04月02日状态空间模型音频建模State Space ModelMambaSelective Scan
2026年04月02日实时语音增强工程选型Real-time Speech EnhancementNoise SuppressionStreaming Inference
2026年04月02日对话上下文压缩原理Context CompressionAbstractive SummarizationCross-Attention Fusion
2026年04月02日说话人匿名化工程Speaker AnonymizationVoice ConversionStreaming Inference
2026年04月02日视听语音识别融合Audio-Visual Speech RecognitionLip ReadingViseme
2026年04月02日GPU训练吞吐加速工程MXFP8MoE TrainingExpert Parallelism
2026年04月01日熵驱动多样性生成diversity samplingtypicality biasrepulsion in latent space
2026年04月01日说话人分割工程选型speaker diarizationbenchmark methodologystreaming ASR pipeline
2026年03月31日转向检测联合建模turn-taking detectionvoice activity detectionjoint acoustic-linguistic modeling
2026年03月31日基准测试的系统性失效benchmark contaminationevaluation validityLLM judge reliability
2026年03月31日扩散模型声学生成diffusion modelscore matchingstochastic differential equation
2026年03月31日TTS开源生态竞争open-weight TTStime-to-first-audiomultilingual speech synthesis
2026年03月30日注意力机制变体演进Multi-Head AttentionGrouped Query AttentionMulti-head Latent Attention
2026年03月30日设备端语音推理架构on-device inferenceExecuTorchvoice agent pipeline
2026年03月29日混合自回归流匹配TTSautoregressive semantic tokensflow matching acoustic decoderhybrid TTS architecture
2026年03月29日NCCL超时诊断方法论NCCL watchdog timeoutdistributed training debuggingcollective communication
2026年03月29日混合架构音频表示Mambastate space modelaudio representation learning
2026年03月29日DeepSeek预训练加速工程MXFP8 trainingexpert parallelismMoE pretraining
2026年03月27日说话人验证度量学习speaker verificationmetric learningcurriculum learning
2026年03月27日MX浮点格式加速训练MXFP8microscalingmixed precision training
2026年03月26日TTS模型极限压缩model compressionknowledge distillationTTS on-device
2026年03月26日小模型极限压缩哲学model compressionknowledge distillationquantization
2026年03月25日流匹配生成原理flow matchingrectified flowODE
2026年03月25日神经音频编解码器neural audio codecresidual vector quantizationEnCodec
2026年03月25日推测解码加速推理speculative decodingdraft modeltoken verification