跳转至

In-Context Routing (ICR): 一次训练、处处可用的 attention-level 隐式 ICL

会议: ICML 2026
arXiv: 2509.22854
代码: https://github.com/Lijiaqian1/In-Context-Routing.git
领域: LLM 高效推理 / 隐式 ICL / Attention 编辑
关键词: 隐式 ICL, 注意力路由, Principal ICL Directions, 跨域泛化, 零样本推理

一句话总结

ICR 不在 residual stream 注入 shift vector,而是从多域 ICL 中用 PCA 抽出 Principal ICL Directions (PIDs) 作为 attention logits 的 low-rank 修正方向,配 query-conditioned router 自适应调制;一次训练后能在 12 个 in/out-of-domain 任务上零样本推理,无任务特定检索/再训练,在 OOD 上不像 vector-based 方法那样退化。

研究背景与动机

领域现状:In-Context Learning (ICL) 让 LLM 通过 prompt 里加 few-shot 例子学会新任务,但有两个痛点——(1) 加 ICD 让序列变长、推理成本翻倍;(2) 性能脆弱,对 ICD 顺序/格式敏感。隐式 ICL 把 ICD 转成 dense vector 注入到模型隐藏层去模拟 ICL 效果(Hendel et al. 2023, Liu et al. 2023, Li et al. 2024)。

现有痛点:vector-based 隐式 ICL 用 shift vector \(\mathbf{V}^l_{\mathrm{shift}}\) 加到 residual stream(\(\tilde{\mathbf{h}}^l = \mathbf{h}^l + \beta^l \cdot \mathbf{V}^l_{\mathrm{shift}}\))但局限明显——(1) 固定大小 vector 容量有限,加新知识就要新 vector;(2) post-hoc 加在 residual 上不能控制信息流;(3) 训练时跟特定任务绑定,OOD 时退化甚至比 zero-shot 还差(M2IV 在 7 个 OOD 任务有 3 个 collapse)。

核心矛盾:要泛化就需要"任务无关的 ICL 模式",但 vector-based 方法的 shift 是任务特定的;要"task-agnostic"必须把 ICL 机制本身(不是 ICD 内容)提炼出来。

本文目标:找一个 task-agnostic 的 ICL pattern + 一次训练后可跨域复用 + 不依赖任务检索或再训练。

切入角度:观察发现多任务 ICL prompting 有时能超过 zero-shot 和最强 single-source few-shot,但也可能拖累(Figure 1)——这说明 ICL 的"用处"不在 ICD 内容而在 latent cross-task pattern;显式 prompting 引入噪声反而盖住这个 pattern。所以应该深入 attention space 提炼这个 pattern。

核心 idea:跨多个域做 explicit ICL,收集每层每个 prompt 末 token 的 query/key projections,PCA 提取 Principal ICL Directions (PIDs) \(U_q^l, U_k^l \in \mathbb{R}^{d \times r}\);用 query-conditioned router 算 routing vector \(\alpha^l(x)\) 和 head gate \(\gamma^l(x)\),把 low-rank 修正 \(\Delta \mathbf{A}^l = (Q_{\mathrm{zs}} U_q^l) \mathrm{diag}(\alpha^l) (K_{\mathrm{zs}} U_k^l)^\top\) 加到 attention logits。

方法详解

整体框架

三阶段:(1) PIDs 提取——跨 \(\mathbb{D}\) 个域跑 explicit ICL,每个 prompt 末 token 的 \(Q^l, K^l\) projection 堆成 ICL bases \(\tilde{Q}^l, \tilde{K}^l \in \mathbb{R}^{N \times d}\),PCA 取 top-\(r\) 主方向得 \(U_q^l, U_k^l\);(2) Query-conditioned router 训练——冻 LLM,训两个 MLP \(g_{\theta_\alpha}, g_{\theta_\gamma}\) 根据 query embedding 输出 \(\alpha(x) \in \mathbb{R}^{L \times r}\) 和 gate \(\gamma(x) \in \mathbb{R}^{L \times H}\),loss = CE + confidence align + sparse + gate regularization;(3) 零样本推理——给任意新 query,router 算 \(\alpha, \gamma\),attention logits 修正为 \(\tilde{\mathbf{A}}^{l,h}(x) = \mathbf{A}^{l,h}(x) + \gamma^{l,h}(x) (Q_{\mathrm{zs}}^l U_q^l) \mathrm{diag}(\alpha^l(x)) (K_{\mathrm{zs}}^l U_k^l)^\top\)

关键设计

  1. Principal ICL Directions (PIDs):用 PCA 提炼跨域 ICL pattern:

    • 功能:从 attention space 抽出 task-agnostic、generalizable 的 ICL 结构方向,作为 attention 修正的"原料"。
    • 核心思路:每个 prompt 末 token 是 contextual information 的整合点,其 \(Q/K\) projection 携带"应该用 ICL 模式回答"的信号。跨 \(\mathbb{D}\) 域收集 \(\tilde{Q}^l, \tilde{K}^l\)(每行一个 prompt),PCA 取 top-\(r\)\(U_q^l, U_k^l \in \mathbb{R}^{d \times r}\)。理论支撑用 Spiked Covariance Model:\(\Sigma_Q^{(\mathbb{d})} = S_q \Lambda_q S_q^\top + B_{q, \mathbb{d}} \Gamma_{q, \mathbb{d}} B_{q, \mathbb{d}}^\top + \sigma^2 I\)\(S_q\) 是跨域共享的 ICL 结构,\(B_{q, \mathbb{d}}\) 是域特定变化。若 \(\{B_{q, \mathbb{d}}\}\) 足够多样且方向不一致,pooled covariance \(\mathbb{E}[\hat{\Sigma}_Q] = S_q \Lambda_q S_q^\top + \sigma^2 I + \frac{1}{N} \sum |\mathcal{D}_\mathbb{d}| B_{q, \mathbb{d}} \Gamma_{q, \mathbb{d}} B_{q, \mathbb{d}}^\top\) 里第三项 average out 到 isotropy,PCA 的 top eigenvectors 自然落到 \(S_q\) 上——即 PIDs 恢复出跨域稳定的 ICL pattern。
    • 设计动机:vector-based 方法把 ICD 信息压成 fixed-size shift vector 容量有限;PCA on attention bases 直接抓"什么样的 query-key matching geometry 让 ICL 起作用"这个 structural pattern,几何上比 additive shift 更接近 ICL 的真实机制(Olsson et al. 2022 已证 attention heads 是 ICL 的核心)。
  2. Query-conditioned router:低秩调制 + head gate:

    • 功能:根据当前 query 自适应决定每层每方向 PID 的强度和每个 head 的参与度,不依赖任务标签。
    • 核心思路:用冻结的 text encoder 算 query embedding \(E(x)\);两个 2-layer MLP 并行:\(\alpha(x) = \tanh(g_{\theta_\alpha}(E(x))) \in \mathbb{R}^{L \times r}\) 给每层每方向打权重 \(\in [-1, 1]\)\(\gamma(x) = \sigma(g_{\theta_\gamma}(E(x))) \in \mathbb{R}^{L \times H}\) 给每层每 head 打 \([0, 1]\) gate。修正后 attention logits 是 \(\tilde{\mathbf{A}}^{l,h}(x) = \mathbf{A}^{l,h}(x) + \gamma^{l,h}(x) (Q_{\mathrm{zs}}^l U_q^l) \mathrm{diag}(\alpha^l(x)) (K_{\mathrm{zs}}^l U_k^l)^\top\)。注意修正是 layer-shared bias × head gate,不是 per-head 独立 modulation——既保有差异化又控制参数量。
    • 设计动机:固定 routing 容易过拟合训练任务;query-conditioned 让 router 学到"什么 query 需要什么 ICL 模式"。tanh 在 \(\alpha\) 上让方向可正可负(增强或抑制 PID),sigmoid 在 \(\gamma\) 上让 head 可选择性激活。这两者组合提供精细的 task-adaptive routing 能力同时 router 参数量极小(\(\le 10M\) vs LLM 7B)。
  3. 多目标训练:CE + confidence align + sparse + gate:

    • 功能:让 router 既学到正确答案(CE),又不降低 zero-shot confidence(防退化),还鼓励 sparse routing(解释性 + 不过修改 attention)。
    • 核心思路:(a) Supervised CE \(\mathcal{L}_{\mathrm{CE}} = -\frac{1}{B} \sum_i \log P^{\mathrm{ICR}}(y_i | x_i)\);(b) Confidence alignment \(\mathcal{L}_{\mathrm{conf}} = \frac{1}{B} \sum \mathrm{ReLU}(H(\mathrm{softmax}(p_i^{\mathrm{ICR}})) - H(\mathrm{softmax}(p_i^{\mathrm{zs}})))\) 惩罚 ICR 比 zero-shot 更不 confident 的情况;(c) Sparse routing \(\mathcal{L}_{\mathrm{spar}} = \mathbb{E}_x[\frac{1}{L} \sum_l w^l \|\alpha^l(x)\|_1 / r]\) with \(w^l\) linearly increasing(后层 sparsity 更重要);(d) Head gate \(\mathcal{L}_{\mathrm{gate}} = \mathbb{E}_x[\frac{1}{L} \sum_l \|\gamma^l(x)\|_1 / H]\)。组合 \(\mathcal{L} = \mathcal{L}_{\mathrm{CE}} + \lambda_{\mathrm{conf}} \mathcal{L}_{\mathrm{conf}} + \lambda_{\mathrm{spar}} \mathcal{L}_{\mathrm{spar}} + \lambda_{\mathrm{gate}} \mathcal{L}_{\mathrm{gate}}\)
    • 设计动机:纯 CE 让 router 退化到"绕过 ICL 机制"的捷径;confidence align 强制 ICR 至少跟 zero-shot 一样自信,防止 router 通过 underconfidence 取巧;sparse loss 让最终调制接近 identity(最小化 intervention),层依赖的 \(w^l\) 反映"早层 broad 处理、后层 specific 决策"的语言模型 layer-wise 结构。

推理过程

给任意新 query:(1) 算 \(E(x)\);(2) router 输出 \(\alpha(x), \gamma(x)\);(3) Eq. 10 算 attention 修正;(4) 走 standard forward。整个过程:(a) 无需 retrieval;(b) 无需 retraining;(c) 计算 overhead 只有 router 两个 MLP,可忽略。

实验关键数据

主实验:12 个 benchmark(5 ID + 4 Near OOD + 3 Far OOD)

模型/方法 AG SST-2 TREC CSQA PIQA SST-5 MR MRPC CB COPA CREAK AI2SciE 平均 Collapse
Llama2-7B
Zero-shot 67.0 78.6 56.6 22.4 52.2 25.8 72.2 44.4 37.5 63.0 51.8 34.8 50.5
Few-shot* 81.0 95.2 84.6 58.0 59.8 37.4 98.6 68.2 41.1 82.0 50.8 45.4 66.8 1
I2CL 85.5 86.0 78.6 23.8 55.6 27.6 71.6 42.4 38.2 63.6 52.6 35.0 55.0 2
LIVE 86.0 86.2 81.0 24.2 56.4 32.8 73.8 47.6 40.8 64.8 51.0 34.6 56.6 2
M2IV 86.4 86.4 81.5 24.8 56.8 30.8 74.0 46.0 42.6 64.8 54.0 35.2 56.9 0
ICR 86.6 86.4 83.8 24.8 57.0 38.6 79.8 53.4 46.4 68.0 56.4 37.2 59.9 0
Qwen2.5-7B
Zero-shot 66.8 54.0 65.8 80.4 76.2 31.4 64.4 72.4 83.9 92.0 77.8 90.4 71.3
Few-shot* 80.2 95.6 67.6 82.2 86.0 37.2 70.2 76.2 83.9 95.0 59.7 95.8 77.5 1
I2CL 77.0 86.4 68.6 81.6 81.2 34.6 69.0 70.8 80.6 92.6 74.8 91.8 75.6 3
LIVE 79.0 87.8 70.4 81.6 82.0 30.8 68.6 69.4 81.0 93.2 72.8 91.8 75.7 4
M2IV 79.6 89.0 70.8 81.8 82.5 31.6 71.2 71.0 76.0 93.5 74.6 92.4 76.2 3
ICR 80.4 91.0 70.6 82.0 82.6 41.4 89.4 73.2 84.6 95.0 79.2 93.2 80.2 0

ICR 在两个 LLM 上都是 SOTA:Llama2-7B 平均 59.9 vs M2IV 56.9(+3.0),Qwen2.5-7B 平均 80.2 vs M2IV 76.2(+4.0)。最重要的是 Collapse=0——其他 baseline 在 4 个 OOD 任务上经常比 zero-shot 还差(如 LIVE 在 Qwen 上 4 个任务 collapse),ICR 一个都没退化。

In-domain 子集(Qwen2.5-7B)

方法 AG SST-2 TREC CSQA PIQA 平均
TV 70.4 78.2 64.6 80.6 74.6 73.7
FV 68.4 76.8 66.2 78.8 80.0 74.0
ICV 74.6 83.0 67.2 81.3 77.2 76.7
ELICIT 70.4 78.5 65.0 79.2 76.4 74.3

跟其他 attention/vector 操控方法比 ICR 也明显胜出。

关键发现

  • ICR 是唯一 OOD 不 collapse 的方法:在 Qwen 上其他 baseline 4 个 OOD 任务里有 3-4 个比 zero-shot 还差,ICR 全部不退化甚至涨——证明 attention-level pattern 比 residual-level vector 更 generalizable。
  • 跨 LLM 都有效:Llama2-7B 和 Qwen2.5-7B 架构差异不小,ICR 都是 SOTA,说明 PIDs + router 的设计是 model-agnostic 的。
  • MR (Near OOD) 上 ICR vs 其他差距最大:Qwen 上 ICR 89.4 vs 其他 ~70(+19),说明 attention-level routing 在与训练域接近但不同的任务上特别强。
  • CREAK (Far OOD) ICR 反超 few-shot:Llama 上 ICR 56.4 vs few-shot 50.8,证明 attention pattern 抓的是任务无关的"how to do ICL"机制,反而比直接展示 example 更稳定(few-shot 在 CREAK 上低于 zero-shot)。
  • ICR 不需要任务检索:M2IV/LIVE 需要在推理时根据任务找对应 vector,ICR 一次训练后所有任务用同一组 PIDs + 同一个 router,部署极简。

亮点与洞察

  • attention-level pattern vs residual-level vector 的范式转移:把"ICL 是 hidden state 的偏移"改成"ICL 是 attention 几何的调制",更接近 ICL 的机制本质(Olsson et al. 2022 已论证 induction heads 是 ICL 关键)。
  • Spiked Covariance + PCA 给 PIDs 理论基础:用 covariance 分解证明 PCA 在多域 ICL bases 上自然恢复跨域共享方向 \(S_q\),让"PIDs 提炼出真正的 ICL pattern"不是 empirical claim 而是有数学支撑。
  • Train once, reuse everywhere:12 个任务用同一组 PIDs + 同一个 router 零样本推理,是 implicit ICL 第一个真正实现 task-agnostic generalization 的方法。
  • 多目标 loss 的结构性收益:confidence align 防退化、sparse routing 防过修改 attention、layer-wise weighted sparsity 反映模型结构——每个 loss 项都有明确动机和 ablation 验证。
  • Router 参数量极小:两个 2-layer MLP + 几个 PID 矩阵,相比 LLM 7B 可忽略,部署时跟 zero-shot 同 latency。
  • Layer-wise PIDs:每层独立提 PIDs 而非全局一套,给不同 layer 的不同 ICL 角色(早层 retrieval、后层 reasoning)留出空间。

局限与展望

  • PIDs 提取需要原始 ICL 数据:虽然一次性,但仍需多个域的 labeled examples 跑 explicit ICL,对没有标注数据的领域适用性受限。
  • Router 训练数据是 mixed-domain:训练数据组成可能影响 OOD 泛化,最优 domain mix 没系统消融。
  • rank \(r\) 选择:PIDs 的 rank \(r\) 是超参,太低损失信息太高过拟合,论文给定值但没扫描分析。
  • 没在 reasoning-heavy 任务上验证:12 任务以 classification/QA 为主,对 math reasoning、code generation 等长 CoT 任务 ICR 是否有效未知。
  • 跨 model size 的扩展性:所有实验在 7B 模型上,70B+ 模型上 PIDs 数量、router 容量、训练成本如何变化没讨论。
  • 跟 LoRA / Prompt Tuning 没正面比:作为 parameter-efficient 方法 ICR 跟 LoRA、Prefix Tuning 的 ID-OOD 综合对比缺失。
  • Confidence align loss 的 underconfidence shortcut:理论上仍可能学到"伪装 confidence"的捷径,没分析 router 学到的 routing 是否真有 semantic meaning。

相关工作与启发

  • vs I2CL / LIVE / M2IV:这些是 vector-based 隐式 ICL 的前作,本文证明它们 OOD collapse 是 paradigm 问题(residual-level shift),不是工程问题;ICR 切到 attention-level 一招破解。
  • vs ICV / FV / TV / ELICIT:这些是 attention-space 工作但 task-specific,ICR 是首个 task-agnostic + train-once 的 attention routing 方法。
  • vs LoRA / Prefix Tuning:那些是 PEFT,需要每个任务一组 adapter;ICR 一组 PIDs + 一个 router 跨任务,部署更简单。
  • vs Mixture of Experts:router 思路相似(query-conditioned 选 expert),但 ICR 在 attention space 而非 FFN,且 expert 是 PID 方向而非完整 MLP,参数量小三个数量级。
  • vs FlashAttention:他们优化 attention 计算 kernel,ICR 修改 attention logits 内容,正交可叠加。
  • 启发:(1) 任何"原本是 prompt 行为"都可以试着提炼成 attention 几何 → 内化进模型;(2) PCA on attention activations 是 universally useful 的提取工具;(3) low-rank attention modulation + query-conditioned routing 是高效注入"任务模式"的 pattern。

评分

  • 新颖性: ⭐⭐⭐⭐⭐ "attention routing" 范式转移 + PIDs 数学基础 + query-conditioned router 整套设计是 ICL 工程的方法学创新。
  • 实验充分度: ⭐⭐⭐⭐⭐ 2 LLM × 12 任务(覆盖 ID/Near OOD/Far OOD)+ collapse 量化 + 多种 baseline + 多消融,证据链完整。
  • 写作质量: ⭐⭐⭐⭐ Section 2 形式化清晰,Section 3 method 复现简洁;Spiked Covariance 分析放在 appendix 削弱主文论证完整性。
  • 价值: ⭐⭐⭐⭐⭐ 直接解决隐式 ICL 跨域泛化痛点,部署友好(零检索、零再训),对 LLM 推理优化社区是 plug-and-play 升级。