Zikai Xiao, Ziyang Wang, Wen Ma, Yan Zhang, Wei Shen, Yan Wang, Luqi Gong, Zuozhu Liu Zhejiang University, USTC, ByteDance, Zhejiang Lab | 2025-06 https://arxiv.org/abs/2506.08371
PCD:训练免费对比解码,对 RoPE 低频分量施加过旋转构造 local-aware logits $L^$,与标准 logits 对比 $\tilde{L}=(1+\beta)L-\beta L^$,缓解长上下文 gold token 后验显著度衰减(PSA)。InfiniteBench KV-Retrieval 8K +7.0%,RULER VT 4K +7.78 F1。
主流 RoPE-based LLM(Llama-3、Qwen-2 等)虽然训练支持百万级 context window,实际有效利用率在 16K 以上急剧下降——"lost in the middle"和"know but don't tell"是两个已被识别的表现。
作者在解码空间发现了更精确的统计现象——Posterior Salience Attenuation (PSA):salience score $S(L)$(gold token 的均值倒数排名)从 $L$=1K 时 ~0.49 单调跌到 $L$=26K 时 ~0.22。但 gold token 绝对排名始终在 top-8 内(128K 词表的 top 0.006%)。这意味着不是召回失败而是排序失败:模型编码了正确答案但无法以足够置信度选出。

Paper's Figure 5, verbatim (caption: "Logits visualization at context length 2542 vs 24326").
左图 context=2542 时 gold token(红点)logit=19.3 远超蓝色 distractor;右图 context=24326 时 gold logit 跌到 13.4,已被多个 distractor 追上。gold token 的绝对 logit 值被压低,但相对位置(top-K rank)仍较高——这正是 PCD 能 work 的前提。
Positional Contrastive Decoding (PCD) 三步:
核心技术壁垒:不在"对比解码"高层 idea(DoLa 早已提出),而在三个工程细节的精确联合设计——(1) 过渡函数 $T(x)$ 必须从高频到低频逐步加大扰动($T(0)=1$ 不扰动高频,$T(1)=2-e^\alpha \approx 0.78$ 扰动低频),硬切破坏中间频率语义;(2) frequency ratio $B/B'=10^4$——太大(1e-1)局部信号失真,太小(1e-6)与原始无差异;(3) top-$\gamma$=30 限制 contrast 仅在高概率候选上做,否则全词表低概率噪声被放大。三者缺一,PCD 退化到与 base 无显著差异。
Llama-3-8B-262K 上核心数字:
| Benchmark | Baseline | PCD | Delta |
|---|---|---|---|
| InfiniteBench KV-Retrieval 8K | 72.0 | 79.0 | +7.0% |
| InfiniteBench KV-Retrieval 16K | 52.0 | 55.0 | +3.0% |
| RULER Variable Tracking 4K | 74.02 | 81.80 | +7.78 F1 |
| RULER Variable Tracking 8K | 71.21 | 77.92 | +6.71 F1 |
| LongBench Avg | 25.98 | 26.87 | +0.89 |
全面优于 DoLa / MsPoE / Beam-Search / SegR / Rephrasing 等 training-free baseline。Theorem A.5 证明 contrastive score 衰减率被改善为 $(\ln B'/\ln B)^{2/d}<1$。

Paper's Figure 1, verbatim (caption: "An illustration of PCD, contrasting logits from long-aware and local-aware attention, and amplifying the gains of large-scale short-to-long training").
以 Tordesillas 条约 QA 为例:standard attention 下 Portugal (0.524) 与 Spain (0.489) 几乎打平;local-aware attention 强偏 proximal token Portugal (0.851);PCD 对比后正确选出 Spain (0.691)。一图说清 PCD 本质——把 proximal bias 分量从标准 logit 中减掉,让 long-range 信号裸露。
PCD 是解码算法,不修改模型结构。实验基座为 Llama-3-8B 及其长上下文微调变体:
| 参数 | 值 |
|---|---|
| Layers | 32 |
| Hidden dim | 4096 |
| Attention | GQA: 32 Q heads / 8 KV heads, head dim = 128 |
| FFN | SwiGLU, intermediate = 14336 |
| Vocab | 128256 |
| Norm | RMSNorm (pre-norm) |
| Position encoding | RoPE, $B$ = 500000 |
三个实验变体:
| 变体 | 训练方 | Native context |
|---|---|---|
| Llama-3-8B | Meta | 8K |
| Llama-3-8B-262K | Gradient AI | 262144 |
| Llama-3-8B-1048K | Gradient AI | 1048576 |
PCD 的插入点在 transformer forward 之后、softmax-sampler 之前:标准 forward 输出 $L$,再用 modified RoPE 跑一次 forward 输出 $L^*$,sampler 拿到 $\tilde{L}$ 做 argmax/sampling。
由于 PCD 不是模型发布而是解码算法,以下 model-specific architecture drawings 标注 N/A:
| Symbol | Definition | Physical meaning |
|---|---|---|
| $S(L)$ | 公式 1: salience score | Gold token 的均值倒数排名,$\in (0,1]$,越大越好 |
| $\theta_j$ | $B^{-2(j-1)/d}$ | 第 $j$ 个旋转子空间角频率,几何级数递减 |
| $R_{\Theta,m}^d$ | Block-diagonal rotation matrix | 位置 $m$ 处的 RoPE 旋转矩阵 |
| $T(x)$ | $2-\exp(\alpha x)$, $x=j/(d/2)$ | 过渡函数:高频端 $T(0)=1$,低频端 $T(1)=2-e^\alpha$ |
| $\theta^*_j$ | $T\cdot\theta_j + (1-T)\cdot\theta'_j$ | 混合角频率:标准与过旋转之间平滑过渡 |
| $B, B'$ | 标准 / 降低后的 RoPE base | $B=500000$, $B'=50$($B/B'=10^4$) |
| $\beta$ | Contrast intensity | 对比强度系数,最佳值 2.5 |
| $\gamma$ | Top-K for contrast | 参与对比的候选数,最佳值 30 |
| $A_j, \phi_j$ | Lemma A.2: amplitude, phase | 第 $j$ 子空间 attention 振幅与相位 |
公式 1 — Salience Score:
$$S(L) = \frac{1}{|Q|}\sum_{i=1}^{|Q|}\frac{1}{1 + \sum_{v \in \mathcal{V}} \mathbb{I}\bigl(P_{f(\theta)}(v \mid x_{\leq L}^{(i)}) > P_{f(\theta)}(y_i^* \mid x_{\leq L}^{(i)})\bigr)}$$
分母计数概率高于 gold token 的词汇数量(即 rank),取倒数后均值即为 salience score。$S=1$ 表示 gold token 为 top-1,$S \to 1/|\mathcal{V}|$ 表示排名极低。PSA 的核心观测:$S(L)$ 随 $L$ 单调递减。
公式 2–3 — RoPE Rotation:
$$R_{\Theta,m}^d = \bigoplus_{j=1}^{d/2} \begin{pmatrix} \cos m\theta_j & -\sin m\theta_j \\ \sin m\theta_j & \cos m\theta_j \end{pmatrix}$$
$d/2$ 个正交 2×2 旋转子块的直和。$\theta_j = B^{-2(j-1)/d}$ 几何递减:$j$=1(高频)负责局部位置区分,$j$=$d/2$(低频)负责全局位置编码。
公式 4 — Blended Frequency:
$$\theta^*_j = T\!\left(\frac{j}{d/2}\right)\theta_j + \left(1 - T\!\left(\frac{j}{d/2}\right)\right)\theta'_j$$
过渡函数 $T(x)=2-\exp(\alpha x)$ 使高频端(小 $j$, $T\approx 1$)几乎不变,低频端(大 $j$, $T<1$)偏向 $\theta'_j$(过旋转),构造 local-aware attention pattern。
公式 5 — Contrastive Logits:
$$\tilde{L} = (1+\beta)L - \beta L^*$$
$(1+\beta)$ 放大标准 logits 同时减去 $\beta$ 倍 local-aware logits,效果是消除 proximal bias 分量,裸露 long-range 信号。$\beta=0$ 退化为标准解码。
Theorem A.5 — Decay Improvement:
$$\limsup_{k \to \infty} \frac{\ln |S_{\text{CD}}(k)|}{\ln |S(k)|} \leq \left(\frac{\ln B'}{\ln B}\right)^{2/d} < 1$$
Contrastive score 的渐近衰减率是原始 score 衰减率的 $(\ln B'/\ln B)^{2/d}$ 次幂。$B' < B$ 保证比值 $<1$,即 PCD attention score 在远距离衰减更慢。对 $B=500000$, $B'=50$, $d=128$(head dim):$(\ln 50/\ln 500000)^{2/128} \approx (3.91/13.12)^{0.0156} \approx 0.982$。
| # | Check | Result | ||||
|---|---|---|---|---|---|---|
| 1 | 量纲一致性 (公式 1): $S(L)$ 为倒数排名均值,无量纲,$\in (0,1]$ | ✓ rank=1 → $S$=1; rank→$\ | \mathcal{V}\ | $ → $S$→$1/\ | \mathcal{V}\ | $ |
| 2 | 正交性 (公式 2): 每个 2×2 旋转子块满足 $R^T R = I$ | ✓ $\cos^2 m\theta_j + \sin^2 m\theta_j = 1$ | ||||
| 3 | 边界条件 (公式 4): $T(0)=2-e^0=1$(高频无扰动);$T(1)=2-e^{0.2}\approx 0.78$(低频扰动 ~22%) | ✓ 高频保持不变,低频逐步过旋转 | ||||
| 4 | 退化路径 (公式 5): $\beta=0 \Rightarrow \tilde{L}=L$;$B'=B \Rightarrow L^*=L \Rightarrow \tilde{L}=L$ | ✓ 两种退化均回到标准解码 | ||||
| 5 | 谱分解 (Lemma A.2): $S(k) = \sum_j A_j\cos(k\theta_j+\phi_j)$,利用旋转内积不变性 $\langle Ru, Rv\rangle = \langle u, R^\top v\rangle$ | ✓ 每个 2D 子空间贡献一个余弦分量,总和为 Fourier-like 级数 | ||||
| 6 | 衰减上界 (Lemma A.4 → Theorem A.5): 将 $\sum_j$ 在临界索引 $j_0 = \lceil \frac{d}{2}\ln k / \ln B \rceil$ 处分裂,高频部分代数衰减 $O(k^{-d/2})$,低频部分指数衰减 $O(e^{-k^{2/d}\ln B})$ | ✓ 但中间使用非标准 bound $\ | \cos x\ | \leq e^{-x^2/2}$ (for $x\geq 1$),validity 未完全论证 |
Model-specific checks:
N/A — PCD 为训练免费方法,不涉及任何训练阶段。
| Stage | Goal | Data (tokens + mix) | LR schedule | Context | Techniques |
|---|---|---|---|---|---|
| [无训练阶段] | — | — | — | — | PCD 在推理时应用,不涉及训练 |

Paper's Figure 2, verbatim (caption: "PCD effectively alleviates the decrease in salience scores with increasing input length").
蓝色 greedy decoding 的 $S(L)$ 从 ~0.49 (1K) 单调跌到 ~0.22 (26K);橙色 PCD 全程高出 0.05–0.08,终点保持在 ~0.30。PCD 不能完全消除衰减(limitation 之一),但显著减缓下降速率。

Paper's Figure 3(a), verbatim (caption: "Distribution of gold label ranks across samples").
二维 heatmap 显示 gold token rank 随 context 长度的变化:短文本时 rank=2 占主导;长文本时 rank 退化到 3–4,但几乎不越过 rank=6,在 128K 词表中始终在 top 0.006% 内。这为 PCD "只需翻几位"的逻辑提供了数据基础。

Paper's Figure 3(b), verbatim (caption: "Single Layer Analysis: PCD mitigates salience attenuation by decelerating long-term decay").
单层 attention 仿真(seq len=16384, $d$=512, $B$=$10^6$, $B'$=$10^4$, $\beta$=0.6):橙色 Local-Aware 急速衰减(强 proximal bias);蓝色 Standard 缓慢衰减到 ~7;绿色 Contrastive 全程在 11–13 之间波动,几乎无衰减。数值验证 Theorem A.5。

Paper's Figure 4, verbatim (caption: "Long-term decay simulation of attention scores under varying hyperparameter settings").
8 个 subplot 分别变化 $d \in \{512, 1024\}$, $\beta \in \{0.6, 1.5, 2.5\}$, $\alpha \in \{0.1, 0.4\}$ 下三种 attention 的 decay 曲线。Contrastive 曲线在所有配置下稳定高于 Standard,证明 PCD 对超参变化的鲁棒性。
| Model (ctx) | Method | KV-Retr 4K | 8K | 16K | VT 4K | 8K | 16K |
|---|---|---|---|---|---|---|---|
| Llama-3-8B (8K) | Base | 97.6 | 92.4 | — | 66.38 | 57.93 | — |
| PCD | 100 | 91.0 (−1.4) | — | 68.91 | 60.07 | — | |
| Llama-3-8B (262K) | Base | 89.2 | 72.0 | 52.0 | 74.02 | 71.21 | 64.40 |
| DoLa-High | 93.0 | 76.0 | 54.0 | 77.19 | 72.87 | 67.29 | |
| MsPoE | 90.0 | 72.0 | 51.0 | 74.29 | 70.03 | 65.10 | |
| SegR | 93.0 | 76.0 | 54.0 | 0.0 | 0.0 | 0.0 | |
| Rephrasing | 92.0 | 73.0 | 50.0 | 81.60 | 79.28 | 70.56 | |
| PCD | 92.0 | 79.0 (+7.0) | 55.0 (+3.0) | 81.80 | 77.92 | 69.04 | |
| Llama-3-8B (1048K) | Base | 94.0 | 92.0 | 84.0 | 67.15 | 72.78 | 65.21 |
| PCD | 95.0 | 96.0 | 87.0 | 66.77 (−0.38) | 71.19 (−1.59) | 69.11 |
PCD 在 262K 模型 8K 段 KV-Retrieval 上最突出(+7.0%)。SegR 在 Variable Tracking 上完全崩溃(0.0 F1)——打乱语义顺序对依赖变量跟踪的任务是致命的。Rephrasing 在 VT 8K/16K 上略胜 PCD(79.28/70.56 vs 77.92/69.04),但在 KV-Retrieval 上全面落后。1048K 模型在 VT 4K/8K 出现轻微回归(−0.38/−1.59),暗示 PCD 在 model-native context 内部的收益消失。
| Method | MFQA-zh | NQA | MFQA-en | 2WikiMQA | Qasper | Musique | HotpotQA | Avg |
|---|---|---|---|---|---|---|---|---|
| Base | 46.72 | 20.03 | 51.27 | 15.50 | 26.26 | 6.87 | 15.22 | 25.98 |
| MsPoE | 50.02 | 18.96 | 51.39 | 13.97 | 24.86 | 7.59 | 17.16 | 26.27 |
| SegR | 4.86 | 4.18 | 27.41 | 10.13 | 26.41 | 3.94 | 8.31 | 12.18 |
| Rephrasing | 45.13 | 18.94 | 49.53 | 13.22 | 28.70 | 6.25 | 13.28 | 25.02 |
| PCD | 51.09 | 20.31 | 50.11 (−1.16) | 16.47 | 27.13 | 6.70 (−0.17) | 15.29 | 26.87 |
LongBench 上提升幅度 +0.89 远小于合成 KV-Retrieval 的 +7.0%。MFQA-en 和 Musique 出现轻微退步。这暗示 PSA 只是长上下文失败的一部分原因;multi-hop reasoning 的瓶颈可能不在 token-level 排序。SegR 在真实多文档 QA 上彻底崩盘(12.18 vs base 25.98)。
| 超参 | 测试范围 | 推荐值 | 最佳 Acc (%) | Variance (%) |
|---|---|---|---|---|
| Base (no PCD) | — | — | 72.00 | — |
| $\alpha$ (transition) | [0.1, 0.5] | 0.1–0.2 | 78.50 | 1.2 |
| $\beta$ (contrast) | [1.0, 4.0] | 1.5–2.5 | 77.90 | 3.1 |
| $B'/B$ (frequency) | [1e-6, 1e-1] | 1e-4 | 75.80 | 2.3 |
| top-$\gamma$ | [10, 200] | 20–30 | 71.50 | 1.8 |
$\beta$ 是主敏感超参(variance 3.1%,其余 ≤2.3%),最佳值 2.5。最佳配置下 72.0→78.5(+6.5%)。top-$\gamma$ 降到 30 也几乎不丢分,意味着 contrast 计算量很小。
| Step | Premise | Conclusion | Evidence |
|---|---|---|---|
| 1 | 长上下文 LLM 在 16K+ token 后 gold token 的 $S(L)$ 单调衰减(~0.49→~0.22),但绝对排名始终在 top-8 内(128K 词表 top 0.006%) | 长上下文性能下降是排序失败而非召回失败:模型编码了正确答案但无法以足够置信度选出 | Fig 2 (salience curve), Fig 3a (rank distribution), Fig 5 (logits scatter 19.3→13.4) |
| 2 | RoPE 角频率 $\theta_j=B^{-2(j-1)/d}$ 几何递减:高频(小 $j$)负责 local 位置区分,低频(大 $j$)负责 global 位置编码。降低 base 到 $B'$ 使 $\theta'_j > \theta_j$,attention 更快衰减 → 强 local bias | 对低频分量施加过旋转(保持高频不变)可构造 local-aware attention pattern,作为对比基准 | §2.2 公式 2–4;经验观察:扰动高频会导致 model collapse |
| 3 | 对比 $\tilde{L}=(1+\beta)L-\beta L^*$ 减去 local-aware 分量后,残差即 long-range 信号净增量。Theorem A.5 证明渐近衰减率压到 $(\ln B'/\ln B)^{2/d}$ 次幂 | PCD 的 long-range attention decay 显著慢于标准 RoPE,gold token 相对显著度在长距离上被保持 | Theorem A.5 推导 + Fig 3b 仿真验证(contrastive 曲线 11–13,几乎不衰减) |
| 4 | Llama-3-8B 三变体(8K/262K/1048K)上实验:KV-Retrieval 8K +7.0%, VT 4K +7.78 F1, LongBench Avg +0.89;全面优于 6 个 training-free baseline | PCD 在 training-free 长上下文解码方法中达到当前最优 | Table 2 (RULER + InfiniteBench), Table 3 (LongBench) |
[实现未公开] — 截至 2026-05,论文未公开代码仓库。GitHub / HuggingFace 搜索均未找到官方实现。算法可基于 HuggingFace transformers LogitsProcessor API 在 ~100–200 行内实现。
PCD 的可复现壁垒在于四个超参的联合搜索空间:
论文仅报告了 Llama-3-8B + InfiniteBench 上的最优超参组合,迁移到其他模型(Qwen / DeepSeek / Mistral)需要重新搜索。
N/A — 论文未讨论 PCD 与量化的交互。PCD 修改 logits 而非权重,理论上与 GPTQ / AWQ / FP8 等方案正交兼容。但 contrastive logits 的数值精度需求($\beta$=2.5 的放大效应)可能使 FP16 以下精度不安全。
| 维度 | 评估 |
|---|---|
| 推理开销 | ~2× standard forward(分别计算 $L$ 和 $L^*$)。论文未报告实测 throughput/latency 数据。若复用 K projection 仅重算 RoPE rotation,理论可压到 ~1.3× |
| KV Cache | Standard forward KV cache 可正常使用。Local-aware forward 需独立 KV cache(RoPE base 不同→K 值不同),或每次重算。增加约 1× KV memory |
| 最低硬件 | 与 Llama-3-8B 相同:1× A100 80G (FP16) 或 4× RTX 4090。PCD 不增加模型参数 |
| Continuous batching | 需 sampler 支持双路 logits 输入。vLLM / SGLang 无原生支持,需自定义 LogitsProcessor |
| 高并发 | 双 forward 在 batch 内 KV cache 不能共享(RoPE base 不同),高并发 throughput 约减半。适合 batch=1 或小 batch 场景 |
| Prefill vs Decode | PCD 仅在 decode 阶段有效(生成 token 时需要两组 logits)。Prefill 阶段不受影响 |