FP16下缓存推理会系统性产生错误,与不缓存结果不同
The Illusion of Equivalence: Systematic FP16 Divergence in KV-Cached Autoregressive Inference

- 用不同计算顺序的浮点累加导致缓存前后结果不一致
- 三模型在GSM8K上100%分词差异,且缓存版准确率更高
- 适合关注大模型推理数值稳定性的研究人员
KV缓存是自回归Transformer推理中的常见优化,长期被认为与无缓存计算在数值上等价。但在标准FP16精度下,缓存开启与关闭的计算路径采用不同的浮点累加顺序,由于FP16不满足结合律,导致解码结果出现确定性差异。在三个开源模型(LLaMA-2-7B、Mistral-7B-v0.3、Gemma-2-2B)上对GSM8K进行评估,发现所有采样策略下均出现100%的分词差异,包括贪心解码,排除了采样随机性的影响;且缓存开启在9种条件中有8种准确率更高,表明偏差方向是系统性的而非随机的。控制实验中使用FP32使偏差降低八数量级,消除分词翻转,翻转率降至0.0%,确认FP16不结合性是唯一因果因素。逐层漂移分析显示:使用分组查询注意力的模型在第一层出现剧烈偏差,而Gemma因更大的头维度和滑动窗口注意力,在各层呈现均匀累积。对残差流进行激活修补无法恢复无缓存轨迹,将因果变量定位至有状态的KV缓存。研究揭示,FP16 KV缓存推理与重计算本质上不等价,并提供了理解现代大模型推理数值不稳定的机制框架。
原文摘要 · Abstract (English)
KV caching is a ubiquitous optimization in autoregressive transformer inference, long presumed to be numerically equivalent to cache-free computation. This assumption fails under standard FP16 precision: cache-ON and cache-OFF execution paths employ different floating-point accumulation orderings which, due to FP16 non-associativity, produce a deterministic divergence in decoded token sequences. Across three open-weight models (LLaMA-2-7B, Mistral-7B-v0.3, Gemma-2-2B) evaluated on GSM8K, we observe a 100\% token divergence rate across all sampling strategies, including greedy decoding, which rules out sampling randomness as a cause, and also with cache-ON yielding higher accuracy in 8 of 9 conditions, where the accuracy difference serves as an indicator that the divergence direction is systematic rather than random. Controlled FP32 falsification reduces divergence by eight orders of magnitude, eliminates token flips, and drops the flip rate to exactly 0.0\%, confirming FP16 non-associativity as the sole causal driver. Layer-wise drift profiling reveals architecturally predictable propagation patterns: models using Grouped-Query Attention exhibit sharp divergence at the first layer, while Gemma's larger head dimension and sliding window attention produce uniform accumulation across all layers. Finally, activation patching of the entire residual stream fails to recover the cache-free trajectory, localizing the causal variable to the stateful KV cache. These findings establish that FP16 KV cache inference is fundamentally non-equivalent to recomputation and provide a mechanistic framework for understanding numerical instability in modern LLM inference systems.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。