In-context learning (ICL) enables language models to adapt to new tasks from demonstrations, but repeatedly including multiple examples increases context length and inference cost. Many existing implicit ICL methods encode demonstrations as fixed vectors, limiting adaptation of the intervention to individual queries and token positions. We propose Attention-Patching, an implicit ICL method that models contextual changes in self-attention outputs through task-specific updates to attention output projections. Using query-answer pairs processed with and without a fixed demonstration set, Attention-Patching fits layer-wise affine transformations from non-contextual attention representations to the demonstration-induced output changes. Across 12 classification benchmarks and four model families, Attention-Patching achieves higher average accuracy than the evaluated implicit ICL baselines while maintaining performance comparable to explicit ICL at near zero-shot inference cost. Further experiments on diverse generation tasks demonstrate its applicability to autoregressive multi-token generation.
Figures & tables
Figure 1: Framework of Attention-Patching. Attention output shifts induced by demonstrations are used to learn task-specific mappings, incorporated into the attention output projections for inference.
Method
Avg.
AG
SST-2
TREC
CSQA
PIQA
SST-5
MR
MRPC
CB
COPA
CREAK
AI2SciE
Llama-3.2-3B-Instruct
0-shot
73.5
58.8
90.8
62.0
76.6
75.6
33.8
89.0
66.9
67.9
94.0
79.2
87.0
ICL
78.6
86.4( ± 1.5)
93.5 ( ± 0.5)
79.9( ± 4.5)
73.4( ± 1.0)
74.9( ± 2.0)
47.8 ( ± 2.6)
91.2 ( ± 0.9)
50.7( ± 7.1)
83.6 ( ± 4.6)
88.8( ± 1.9)
86.0 ( ± 0.4)
86.9( ± 0.7)
LoRA
77.1
87.4 ( ± 0.8)
91.1( ± 0.6)
86.3 ( ± 1.7)
76.3 ( ± 0.5)
76.0( ± 0.5)
43.8( ± 1.8)
88.8( ± 0.8)
56.3( ± 5.2)
64.6( ± 1.5)
94.4 ( ± 0.5)
73.0( ± 2.9)
87.2( ± 0.2)
SITE
77.9
88.8 ( ± 1.7)
91.7( ± 0.5)
78.3( ± 8.4)
73.4( ± 0.7)
75.8( ± 2.5)
38.2( ± 1.7)
90.4( ± 0.4)
68.4 ( ± 0.0)
67.9( ± 10.3)
91.0( ± 4.3)
84.1( ± 1.3)
87.0( ± 1.4)
TV
71.3
68.8( ± 3.8)
90.7( ± 0.3)
69.9( ± 2.7)
76.2( ± 0.2)
76.1 ( ± 1.1)
31.0( ± 5.4)
90.2( ± 0.6)
40.9( ± 13.1)
54.3( ± 19.1)
90.8( ± 2.4)
79.4( ± 1.2)
87.4( ± 0.3)
Table 1: Baseline comparison across classification benchmarks. All results are averaged over five independent runs. The best and second-best results in each column within each model are indicated in bold and underline, respectively. Method abbreviations are defined in Section 4.1 .
Method
Avg.
GSM8K
FLORES(Ko)
FLORES(Sp)
DailyMail
CommonGen
ROUGE-L F1
COMET
ROUGE-L F1
COMET
Llama-3.2-3B-Instruct
Zero-shot
42.9
51.4( ± 0.7)
4.8( ± 0.5)
47.3( ± 0.3)
50.6( ± 0.2)
83.2( ± 0.5)
16.8( ± 0.3)
45.8( ± 0.4)
LoRA
47.4
56.1( ± 2.9)
11.4( ± 1.3)
62.7( ± 3.3)
51.1( ± 0.4)
84.2( ± 0.2)
18.1( ± 1.4)
48.2( ± 0.5)
Many-shot (1K)
55.1
69.6 ( ± 3.3)
21.3 ( ± 1.0)
79.3 ( ± 0.5)
51.8 ( ± 0.1)
85.0 ( ± 0.4)
22.1 ( ± 0.9)
56.9 ( ± 1.4)
SITE
47.9
69.4( ± 2.6)
8.6( ± 0.7)
56.6( ± 0.7)
50.7( ± 1.1)
84.0( ± 0.4)
16.9( ± 0.3)
49.1( ± 0.2)
Table 2: Multi-token generation results under different context lengths, averaged over three independent runs. We report flexible-match accuracy on GSM8K, ROUGE-L F1 and COMET scores on FLORES, and ROUGE-L F1 scores on DailyMail and CommonGen. The best and second-best results in each column within each model and context length are indicated in bold and underline, respectively.
Figure 2: Average inference FLOPs and TTFT on GSM8K using Llama-3.2-3B-Instruct across different context lengths, averaged over 500 samples.
Method
AG
SST-2
TREC
CSQA
PIQA
SST-5
MR
MRPC
CB
COPA
CREAK
AI2SciE
Avg.
Bias
58.8
87.1
68.7
76.1
75.8
34.2
89.0
68.2
58.2
93.8
77.2
87.0
72.8
Weight
85.7
93.1
84.6
75.7
76.0
46.6
90.8
68.3
80.0
92.6
86.0
87.6
80.6
Full
85.8
93.0
83.9
76.2
75.8
46.7
91.1
68.7
79.6
93.2
86.0
87.7
80.7
Table 3: Component ablation of Attention-Patching on Llama-3.2-3B-Instruct. We report average accuracy across the 12 classification benchmarks.
Figure 3: Singular structure of adaptive steering on Llama-3.2-3B-Instruct. Average accuracy across 12 classification tasks (left) and cosine similarity to the bias-only steering vector averaged across tasks and layers (right) using rank- k truncated SVD of the weight patch.
Figure 4: Layer-wise structure of the learned attention patches across classification benchmarks. The top row shows the normalized Frobenius norm of the patch weights, while the bottom row shows their stable rank. Each column corresponds to a model architecture, and each curve represents one classification task.
Figure 5: Correlation between performance changes induced by explicit ICL and AP relative to zero-shot inference. Each point represents a task.
Appendix figures & tables12 assets
Supplementary material from the paper’s appendix.
Appendix
Figure 6: Selected patch strength α for each task and model. The left panel corresponds to classification tasks, where values are averaged across five independent runs, and the right panel corresponds to multi-token generation tasks, where values are averaged across the evaluated context lengths and three independent runs for each model. In each experiment, α is selected on the held-out validation set following the procedure described above.
Table 4: Prompt templates used in the classification experiments.
Method
Avg.
AG
SST-2
TREC
CSQA
PIQA
SST-5
MR
MRPC
CB
COPA
CREAK
AI2SciE
Gemma-2-2b-it
0-shot
72.7
75.6
92.6
72.8
69.8
71.8
36.8
91.6
45.3
62.5
94.0
72.4
87.4
ICL
80.1
83.3( ± 2.4)
93.6 ( ± 1.2)
83.3 ( ± 3.0)
72.2 ( ± 0.9)
75.5 ( ± 1.0)
48.0 ( ± 1.8)
92.2 ( ± 0.7)
69.2 ( ± 4.1)
76.1 ( ± 9.8)
92.0( ± 1.4)
87.0 ( ± 1.1)
88.8 ( ± 0.6)
LoRA
75.2
87.0 ( ± 2.3)
91.6( ± 1.5)
81.7( ± 5.5)
68.0( ± 1.3)
75.2 ( ± 0.6)
44.6( ± 1.7)
91.0( ± 1.8)
45.2( ± 11.4)
71.4( ± 4.6)
94.4( ± 0.5)
64.8( ± 2.2)
87.9( ± 0.2)
SITE
78.8
89.6 ( ± 1.2)
93.0( ± 0.4)
82.9( ± 2.5)
69.0( ± 1.9)
74.5( ± 1.5)
36.0( ± 1.3)
91.3( ± 0.7)
68.4( ± 0.0)
73.2( ± 5.6)
94.6( ± 0.5)
86.4 ( ± 2.3)
87.0( ± 1.3)
TV
73.7
85.4( ± 1.0)
92.5( ± 0.1)
76.4( ± 0.1)
70.3( ± 0.3)
71.8( ± 0.3)
37.7( ± 1.2)
90.0( ± 1.8)
43.5( ± 3.3)
57.1( ± 4.0)
93.4( ± 0.9)
79.3( ± 4.0)
86.6( ± 1.2)
Appendix
Table 5: Baseline comparison across classification benchmarks. All results are averaged over five independent runs. The best and second-best results in each column within each model are indicated in bold and underline, respectively.
Method
Avg.
GSM8K
FLORES(Ko)
FLORES(Sp)
DailyMail
CommonGen
ROUGE-L F1
COMET
ROUGE-L F1
COMET
Gemma-2-2b-it
Zero-shot
51.7
55.2( ± 1.2)
20.9( ± 0.7)
81.5( ± 0.1)
52.7( ± 0.4)
85.5( ± 0.3)
18.7( ± 0.2)
47.3( ± 0.8)
LoRA
51.9
57.2( ± 1.7)
21.1( ± 0.6)
80.9( ± 0.4)
52.8( ± 0.3)
85.8( ± 0.2)
15.9( ± 0.3)
49.4( ± 1.2)
Many-shot (1K)
53.2
48.5( ± 3.1)
22.6 ( ± 0.6)
84.4 ( ± 0.4)
53.4 ( ± 0.2)
85.9 ( ± 0.3)
23.4 ( ± 0.6)
54.4 ( ± 1.3)
SITE
50.8
45.9( ± 1.5)
21.6( ± 0.8)
83.3( ± 0.4)
51.5( ± 0.2)
85.4( ± 0.2)
18.4( ± 1.0)
49.8( ± 0.9)
Appendix
Table 6: Multi-token generation results under different context lengths, averaged over three independent runs. We report flexible-match accuracy on GSM8K, ROUGE-L F1 and COMET scores on FLORES, and ROUGE-L F1 scores on DailyMail and CommonGen. The best and second-best results in each column within each model and context length are indicated in bold and underline, respectively.
Figure 7: Break-even analysis of AP vs. PrefixKV Caching. Left: Wall-clock time for 500 queries across different context budgets. Right: Break-even points with and without activation collection costs.
Figure 8: Effect of calibration set size on Llama-3.2-3B-Instruct, Gemma-2-27b-it, and Qwen3.5-0.8B. We report average accuracy across 12 classification benchmarks for LoRA, Thought Patching (TP), and Attention-Patching with 5, 15, and 25 examples per class.
Figure 9: Effect of calibration set size on Gemma-2-2b-it and Mistral-7B-Instruct-v0.3. We report average accuracy across 12 classification benchmarks for LoRA, Thought Patching (TP), and Attention-Patching with 5, 15, and 25 examples per class.
Group
AG
SST-2
TREC
CSQA
PIQA
SST-5
MR
MRPC
CB
COPA
CREAK
AI2SciE
Avg.
None
58.8
90.8
62.0
76.6
75.6
33.8
89.0
66.9
67.9
94.0
79.2
87.0
73.5
Q1
66.8
91.9
67.2
76.1
75.8
31.3
89.6
66.5
61.8
92.8
69.8
86.9
73.0
Q2
72.4
90.5
74.7
76.5
75.3
36.9
89.0
67.3
65.4
94.0
83.7
86.3
76.0
Q3
63.3
89.8
61.5
76.5
75.6
34.5
89.2
67.6
62.5
93.6
78.6
87.3
73.3
Q4
56.7
90.6
57.2
76.3
75.6
34.7
89.4
67.9
64.6
94.0
75.9
86.8
72.5
All
85.8
93.0
83.9
76.2
75.8
46.7
91.1
68.7
79.6
93.2
86.0
87.7
80.7
Appendix
Table 7: Layer-group ablation of Attention-Patching on Llama-3.2-3B-Instruct across 12 classification benchmarks. We divide the decoder layers into four contiguous groups of equal size, where Q1 and Q4 denote the earliest and latest layer groups, respectively. “All” applies patches to every decoder layer.
ρ
AG
SST-2
TREC
CSQA
PIQA
SST-5
MR
MRPC
CB
COPA
CREAK
AI2SciE
Avg.
1e-3
85.8
93.0
83.9
76.2
75.8
46.7
91.1
68.7
79.6
93.2
86.0
87.7
80.7
1e-2
79.0
92.8
83.2
76.3
75.9
43.4
91.3
66.6
79.3
94.8
85.0
88.0
79.6
1e-1
59.3
92.2
84.3
76.1
75.3
38.3
90.8
66.9
70.7
95.6
85.9
87.1
76.9
Appendix
Table 8: Effect of the ridge coefficient ρ on Llama-3.2-3B-Instruct. We report average accuracy across 12 classification benchmarks.
Figure 10: Layer-wise structure of the learned attention patches across classification benchmarks. The top row shows the normalized Frobenius norm of the patch weights, while the bottom row shows their stable rank. Each column corresponds to a model architecture, and each curve represents one classification task.
Figure 11: Layer-wise NMSE of hidden states relative to many-shot ICL, averaged over 500 GSM8K samples and context lengths. Lower values indicate greater representational similarity.
Figure 12: Layer-wise NMSE of hidden states relative to many-shot ICL, averaged over 500 GSM8K samples and context lengths. Lower values indicate greater representational similarity.