Skip to content

Understanding the in-context learning mechanism of addition through activation subspacesUnderstanding In-context Learning of Addition via Activation Subspaces

arXiv· arXiv:2505.05145· Xinyan Hu, Kayo Yin, Michael I. Jordan, Jacob Steinhardt, Lijie ChenUC Berkeley; INRIAIndexed by this site · 2026-10-14· Originally publishedAlignment

Quick read

Interpretability

Based on the paper PDF (AI-generated); refer to the original

The authors localize Llama-3's add-k to a three-head six-dimensional subspace, with intervention accuracy 0.680 (clean 0.870).

Questions
How large models extract task rules from a few examples in a single forward pass and apply them to new queries. The authors use an additive constant k with a shared input domain, so the query itself contains no task information.
Methods
Use L1 sparse optimization to jointly select attention heads, then reduce to main heads via mean ablation and McNemar. Apply PCA and triangular direction rotation to main head outputs, and trace the last-token subspace back to example labels.
Experiments and results
On Llama-3's add-k, three main heads give intervention accuracy 0.680, clean five-shot 0.870; 33 heads give 0.798, AIE top-33 gives 0.414. The pipeline is also used on Phi-4, Qwen-2.5, and subtraction, multiplication, and semantic task families.
Limitations and future work
The authors defer the automation of qualitative subspace inspection, as well as division of labor within families, sharing or transfer across families, to future work. Experiments include three models, five task families, six prompt formats, and 1 to 10 examples.
Experimental setupLlama-3-8B-Instruct、Phi-4-14B et al. · add-k、subtract-k et al. · intervention accuracy、clean accuracy et al.
Evaluated models
Llama-3-8B-InstructPhi-4-14BQwen-2.5-7B
Benchmarks
add-ksubtract-kmultiply-kabstractive tasks (Todd et al., 2024)extractive tasks (Todd et al., 2024)
Metrics
intervention accuracyclean accuracyexact-token-match
AI summaryResearchers propose a new method that localizes few-shot learning capabilities of language models to a few attention heads, and on Llama-3-8B-Instruct reduce the mechanism of these tasks to three attention heads and a six-dimensional subspace. In this six-dimensional subspace, four dimensions track units digits using trigonometric functions with periods 2, 5, and 10, and two dimensions track numerical magnitude using low-frequency components. The method also applies to four other task types in arithmetic and semantic domains. The authors also derive a mathematical identity relating the aggregator subspace and extractor subspace of attention heads, thereby tracing the flow of information from individual examples to the final aggregated concept. Code is publicly available.

Researchers propose a new method that localizes few-shot learning capabilities of language models to a few attention heads, and on Llama-3-8B-Instruct reduce the mechanism of these tasks to three attention heads and a six-dimensional subspace. In this six-dimensional subspace, four dimensions track units digits using trigonometric functions with periods 2, 5, and 10, and two dimensions track numerical magnitude using low-frequency components. The method also applies to four other task types in arithmetic and semantic domains. The authors also derive a mathematical identity relating the aggregator subspace and extractor subspace of attention heads, thereby tracing the flow of information from individual examples to the final aggregated concept. Code is publicly available.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    The authors aim to localize how large models extract the addition constant k from examples and aggregate it into an applicable rule.

    How large model forward passes extract the additive constant k from a few examples and aggregate it into a rule applicable to new inputs.

    • Problem scenario: The authors argue that language models can perform few-shot learning from a few input-label pairs in the prompt, but the fine-grained computational structure of in-context learning (ICL) in large models remains unclear. Existing work either models small synthetic models or performs only coarse analyses of large pretrained models.
    • Open questions: Todd et al. (2024) construct a function vector from examples, and adding it to the zero-shot residual stream can recover ICL. The authors propose two points that remain open: how the function vector precisely encodes the task; and how the model aggregates information from each example into that vector.
    • Why addition: add-k adds a fixed k to input integers. Different k share the input domain, and the query itself does not contain k. The authors argue this separates task rules from domain associations and allows comparison with existing non-ICL addition mechanisms.
    • This paper proposes: The authors claim to propose a new pipeline that localizes few-shot capabilities to a few attention heads and low-dimensional subspaces, using addition as a mechanistic case study. The same pipeline is applied to subtraction, multiplication, and abstractive and extractive task families.
  2. What related research exists?

    Related work covers arithmetic representations, ICL mechanisms, and causal interventions; the comparison baseline is the AIE from Todd et al.

    Arithmetic and number representations

    • Zhou et al. (2024), Kantamneni & Tegmark (2025)—find Fourier or trigonometric, spiral representations in direct addition. The authors state this aligns with the periodic structure in ICL here, and suggest a deeper relationship between non-ICL and ICL mechanisms; they also position these works as studying number representations or executing given operations.
    • Stolfo et al. (2023), Hanna et al. (2023), Nikankin et al. (2025), Nanda et al. (2023), Zhong et al. (2023)—use mechanistic explanations or mediation analysis to study how language models do arithmetic, especially addition. The paper only surveys here, without criticizing each work.
    • Zhang et al. (2024b), Engels et al. (2025), Levy & Geva (2025)—report respectively that direct arithmetic uses fewer than 5% of attention heads, circular features for calendar modular arithmetic, and base-10 digit circular representations.

    In-context learning

    • Garg et al. (2022), Akyürek et al. (2023), von Oswald et al. (2023) and a series of follow-ups—build ICL on small transformers as simple function classes, linear models, or gradient-descent-like processes. The introduction uses them to show that small models already have finer models, while large models still lack fine-grained structure.
    • Olsson et al. (2022)'s induction head, and Todd et al. (2024), Hendel et al. (2023)'s function vector / task vector—attribute large-model few-shot performance to heads that copy patterns, or to vectors that encode tasks. Yin & Steinhardt (2025) argue that few-shot ICL mainly relies on function-vector heads, and the authors examine these heads accordingly.
    • Cho et al. (2025; 2026), Bakalova et al. (2025), Kharlapenko et al. (2025), Yang et al. (2025; 2026), and Polyakov et al. (2025), Ye et al. (2026), Wang et al. (2026)—track encoding, aggregation, filtering within prompts, or analyze ICL with SAEs, query geometry, partial sums, and example-level sub-vectors. The authors state these works focus on prompt-level information flow, feature circuits, or query transformations.

    Causal interventions

    • Vig et al. (2020), Geiger et al. (2021), Meng et al. (2022), Chan et al. (2022), and Todd et al. (2024)'s causal mediation—estimate the causal role of components by modifying internal activations. The authors state their method follows this line, using activation patching.

    Baselines and data

    • Baseline methods: Todd et al. (2024) rank heads independently by average indirect effect (AIE). The authors explicitly compare: that ranking does not consider joint effects of heads; on their add-k, taking the same number of heads by AIE gives intervention accuracy lower than jointly selected heads.
    • Benchmarks/datasets: Self-built add-k, subtract-k, multiply-k; abstractive and extractive use task data from Todd et al. (2024).

    The authors position this paper as a finer description of ICL in large models than prior research: jointly finding compact sets of task heads, rather than ranking independently like Todd et al., and characterizing task-varying low-dimensional subspaces in individual head outputs.

  3. How does the paper address this problem?

    After sparse joint head selection, reduce to three main heads, then decompose units and tens in a six-dimensional subspace and trace back labels.

    The authors use a head-localization pipeline to concentrate the add-k task signal into a few attention heads, then find low-dimensional subspaces in the outputs of these heads, and trace the signal at the last token back to example tokens.

    Task and function vector

    • add-k: For a fixed k, predict y=x+k. Main range x∈[1,100], k∈[1,30]. Five-shot prompts use "#" to connect "xi → yi", ending with the query "xq →". There is also mixed-k where each example can have a different k, and a no-example "xq →".
    • Model and patch: Main text uses Llama-3-8B-Instruct, 32 layers, 32 heads per layer, residual dimension 4096, heads denoted (layer, head). Additive patch is applied at about one-third depth on the last token's residual. Appendix uses the same pipeline to analyze Phi-4-14B and Qwen-2.5-7B.
    • Vectors and metrics: hk is the average of head h's last-token output over many five-shot add-k examples, approximated with 100 random five-shot examples in the paper, with xq ranging over [1,100] once each. Intervention accuracy is the accuracy averaged over all k after adding the vector to zero-shot; clean accuracy is the five-shot accuracy without intervention. Appendix B uses exact-token-match.

    Sparse head selection

    The authors search over all 1024 heads for c∈[0,1] such that the weighted sum predicts xq+k on zero-shot, and make c sparse. The loss is L(c)=Ek,xq[ℓ(xq,xq+k;vk(c))]+λ|c|1 which is the cross-entropy on the correct sum after intervention plus L1.

    • 25 k values are used for training and same-distribution testing, 5 held out. 100 zero-shot examples per task, 25 tasks split 0.7/0.15/0.15 into train/val/test.
    • AdamW, learning rate 0.01, batch 128, λ=0.05; c is clipped to [0,1] each step.
    • Head selection rule is not hand-picked: coefficients from the last three checkpoints are averaged and sorted descending, then cut at the largest gap between adjacent values. add-k yields 33 heads, denoted Hsel.

    Ablation to main heads

    • When retaining set S, S uses task vectors hk, and the other selected heads are replaced with the mean across k. Single-head scanning amplifies only that head's hk, with coefficients taking integer values from 0 to 20, and other selected heads use the mean.
    • significant heads: Each positive coefficient is compared with c=0 using a one-sided exact McNemar test, with Bonferroni correction within heads and Benjamini–Hochberg across heads, q=0.05. A head is retained if at least one positive coefficient fixes more examples than it breaks, and passes both corrections.
    • main heads: On task families with clean accuracy at least 0.25, the maximum accuracy gain of significant heads is at least 1/4 of the overall maximum gain. add-k yields (15, 2), (15, 1), (13, 6).

    Six-dimensional subspace and trigonometric decomposition

    • PCA is performed on the 30 task vectors for each main head. On Llama-3, 6 principal components explain 97% of task variance. The authors then project the three main head vectors into this subspace, add the mean of the other selected heads, and check whether the intervention remains effective.
    • Principal component coordinates are partially periodic in k. The authors search for periods and phases and perform least squares, obtaining directions with periods 2, 5, 10, 10, 25, 50, called feature directions.
    • The authors define three sub-signals: period 2 as the parity direction; periods 2, 5, 10 spanning the unit subspace, corresponding to the units digit; periods 25, 50 spanning the magnitude subspace, corresponding to the tens digit. The test is to project the vector into the subspace or onto the orthogonal complement, and see whether the corresponding signal is preserved or disappears.

    Tracing back from label tokens

    The last token's head output is linearly decomposed by source token: h(p)=∑t∈ pαt Oh Vh zt where αt is the attention weight of the last token on t, zt is the residual entering the head at that token, and Vh, Oh are the value and output matrices. After projecting into the six-dimensional subspace, token t's contribution is αt times the extracted subspace vector.

    • The authors separate the extracted vector from the attention weights. On standard five-shot, projecting each token's contribution and the total output onto that task's hk yields signed shares summing to 1. On mixed-k, they examine the inner product of the extracted vector with the normalized projected head vector, checking whether the peak falls at k=yi−xi.
  4. What experiments did the paper conduct?

    Llama-3 three main heads intervention accuracy 0.680, clean five-shot 0.870; AIE with the same budget is lower.

    Experimental setup

    • Models: Llama-3-8B-Instruct (main text), Phi-4-14B, Qwen-2.5-7B. Llama-3 has 32 layers, 32 heads per layer.
    • Tasks: five families; in Table 3, the number of tasks is 30 for addition, 31 for subtraction, 30 for multiplication, 26 for abstractive, and 27 for extractive. Arithmetic inputs x∈[1,100], 100 points per task; addition k∈[1,30]. Semantic task data from Todd et al. (2024).
    • Baselines: AIE ranking from Todd et al. (2024). In the appendix, the small budget is k=20 for Llama-3 and Phi-4, and k=16 for Qwen; §3.1 additionally reports Llama-3's AIE top-33.
    • Metrics and criteria: clean accuracy is the accuracy on n samples without intervention; intervention accuracy is the accuracy after adding the function vector to the zero-shot residual. Appendix B uses exact-token-match throughout. The paper does not use LLM evaluation. The patch layer is one-third of the network depth.
    • Splits and intervals: each family has 5 fixed held-out tasks; accuracy is measured on held-out tasks, with 100 queries per task. Coefficient training uses only training tasks. Intervals are 95% bootstrap with 2000 resamples.
    • add-k training: 25 k values for training, 5 held out; AdamW, learning rate 0.01, batch 128, λ=0.05. When all selected heads are mean-ablated, Llama-3 zero-shot accuracy is 0.037 (Table 2).

    Main results

    The table below shows held-out task point estimates for add-k, arrow format, five-shot, according to Table 5. The selected-head and main-head rows are zero-shot interventions; the main-head row mean-ablates the other selected heads.

    Swipe horizontally to read the table

    ModelClean accuracySelected headsMain headsProjected main headsAIE top-k
    Llama-3-8B-Instruct0.8700.798 (33/1024)0.680 (3/1024)0.636 (18/384)0.124 (k=20)
    Phi-4-14B0.9280.908 (23/1600)0.876 (3/1600)0.870 (22/384)0.728 (k=20)
    Qwen-2.5-7B0.8960.368 (230/784)0.270 (4/784)0.252 (28/512)0.136 (k=16)
    • The authors state that the jointly selected heads capture most of the information needed for add-k. The same-budget comparison in Table 2 is: 33 heads summed without coefficients 0.798, AIE top-33 0.414; with trained coefficients, held-out task intervention accuracy is 0.812, clean accuracy 0.870 (§3.1).
    • The introduction states three-head accuracy of 68%, which is 78% of the original ICL, and states Llama-3 as 87% and Qwen-2.5 as 90%. The corresponding point estimates in Table 2 and Table 5 are 0.680, 0.870, and 0.896.
    • The authors state the three models' directions are similar. Qwen's unit-coefficient composition is low; the authors state its tokenizer splits by digit, and exact-token-match penalizes this composition; the trained-coefficient function vector is 0.460. Phi-4's main heads are (18, 12), (18, 14), (17, 23); Qwen's main heads are (21, 0), (21, 5), (21, 4), (21, 2) in layer 21.

    Head necessity and task families

    • Five-shot necessity: mean-ablating the three main heads on random five-shot examples drops accuracy from 0.87 to 0.43. Also taking 20 groups of three other selected heads each, 95% of groups still keep accuracy at least 0.86 (§3.3). The authors state these three heads are necessary for a large part of ordinary five-shot performance.
    • Llama-3 other families (Table 4): subtraction clean accuracy 0.526, 4 main heads intervention 0.290, AIE top-20 0.002. Multiplication clean 0.348, 5 main heads 0.080, AIE 0.000. Extractive clean 0.810, 11 main heads 0.564; abstractive clean 0.422, 10 main heads 0.248. Extractive's significant set is 0.596, higher than the selected set 0.506; the paper does not explain this.
    • Authors' generalization: structure varies by family. (15, 1) is a main head in every Llama-3 family; subtraction's main heads are the addition three plus (15, 17). Semantic families are more dispersed. Phi-4 subtraction's main-head intervention is 0.754, higher than its clean accuracy 0.658 (Table 6); the authors only note this.

    Subspace, periodic structure, and information flow

    • Projection: each Llama-3 main head's 6 principal components explain 97% of task variance (Figure 3a, §4.1). After projecting the three main heads, intervention accuracy is 0.636; before projection, 0.680. Appendix B.6 states that in the cells shown in each table, replacing main-head vectors with these principal components lowers accuracy by at most 0.052; with the full grid including the significant set, at most 0.058.
    • Causal decomposition: the authors map rotated directions to periods 2, 5, 10, 25, 50 (Figure 3c). Figure 3d and Appendix E compare projecting in versus projecting out: the authors state that projecting into the unit subspace gives low ones-digit error while final-answer error can still be high, while projecting out gives high ones-digit error and nearly explains final-answer error. After projecting the vector out of the parity direction, neither parity error nor answer error rises substantially; the authors propose two possibilities: parity is easy to obtain, or it is redundantly encoded elsewhere in the six-dimensional subspace. The authors state head 3's evidence is slightly weaker than heads 1 and 2, and state this is consistent with its lower intervention accuracy. Qwen's (21, 0) and (21, 4) can fit periodic directions, while (21, 2) and (21, 5) fit poorly; the authors conjecture this relates to digit-wise tokenization (Appendix D).
    • Label channel: on standard five-shot examples, label tokens contribute 96–99% of each main head's task signal; inputs, separators, and queries contribute almost nothing (§5.2, Figure 15). On mixed-k, the norm of extracted information and attention weights are highest at labels; the inner product with the unit head vector is largest at k=yi−xi (Figure 16). The authors state the model extracts yi−xi from each example.

    Other ablations and analyses

    • Single-head maximum intervention (Table 1): (15, 2) at c=6 gives 0.866; (15, 1) at c=6 gives 0.817; (13, 6) at c=5 gives 0.693; the remaining selected heads each give at most 0.170 at their optimal scale. Clean five-shot is 0.870.
    • Prompt format (Table 7, coefficients retrained per format): clean accuracy ranges from 0.348 for fx to 0.870 for arrow. Each format recovers the three main heads; qa and ab additionally recover (15, 17), giving 4 main heads.
    • Number of examples n=1 to 10 (Table 8, reusing five-shot coefficients): clean accuracy ranges from 0.256 to 0.944, saturating around n≈7; intervention accuracy enters a plateau around n≈4. Each n recovers the same three main heads. AIE top-20 at n=1 is 0.014; its top-33 does not include (15, 2) or (13, 6).
    • Number of principal components reaching 95% task variance: Llama addition 6 per head, and this holds across formats and n; subtraction 7; multiplication 6–9; Phi-4 addition 7–8; Qwen addition 5–8; extractive 6–11; abstractive 11–15 (Appendix B.6).
    • Llama-3 add-k's significant heads number 13, with intervention accuracy 0.774; Qwen reduces from 230 selected heads to 18, with accuracy 0.358, versus the selected set's 0.368 (Appendix A, Table 5).
    • The maximum-gap rule averaged over the last three checkpoints is used to determine the selected set for every task family, model, and prompt format studied (§3.1).
  5. What are some points that can be further explored?

    The authors leave the automation of subspace inspection, as well as the division, sharing, and transfer of heads and subspaces, to future work.

    Limitations and future directions noted by the authors

    The paper has no separate Limitations section. The future directions written in Discussion (§7) are as follows.

    • Automated checks: The authors state that most steps in the pipeline are easy to scale, and future work could use AI to automate what is currently still qualitative subspace checks (§7).
    • Division of labor within a family: Within a task family, how identified heads and their subspaces work individually and together (§7).
    • Cross-family sharing and transfer: Whether important heads and subspaces across different task families are shared or transferable. The authors state Appendix B.1 only gives preliminary evidence of head-level sharing; subspace-level sharing and transfer remain open (§7).

    Experimental coverage

    • Analyzed models are Llama-3-8B-Instruct, Phi-4-14B, Qwen-2.5-7B; the main text centers on Llama-3, with results for the other two models in Appendix B.2, Table 5.
    • Task families are addition, subtraction, multiplication, and Todd et al. (2024)'s abstractive, extractive (Table 3). Addition uses x∈[1,100], k∈[1,30].
    • The main setting is the five-shot arrow prompt. The appendix also reports qa, io, ab, textlabel, fx (Table 7), and n∈{1,…,10} (Table 8).
    • Accuracy uses exact-token-match; 5 held-out tasks per family, 100 queries per task; intervals are 95% bootstrap with 2000 resamples (Appendix B). No separate evaluation model is used for judgment.
    • Coefficient optimization gives AdamW, learning rate 0.01, batch 128, and λ=0.05 (§3.1). The paper does not report epochs or gradient steps, nor multiple independent training runs.
  6. Summarize the main content of the paper

    Addition ICL is concentrated into a six-dimensional periodic subspace of three heads, with Llama-3 intervention accuracy 0.680.

    The authors reduce addition-type ICL to a low-dimensional activation subspace on a few attention heads, and trace task information along the forward pass.

    • Question: how large models infer "add a constant k" from examples, rather than executing an operation already stated in the prompt. The add-k query does not contain k; different tasks differ only in this constant.
    • Approach: L1 sparse optimization jointly selects heads among 1024 heads, then mean ablation, McNemar, and a gain threshold are used to narrow down to the main heads. PCA is performed on the main-head outputs, rotated by period, and attention decomposition is used to split the last-token subspace back into individual tokens.
    • Llama-3 main results: the zero-shot intervention accuracy of the three main heads (15, 2), (15, 1), (13, 6) is 0.680, and the clean five-shot accuracy is 0.870 (Table 2). The coefficient-free sum of the 33 selected heads is 0.798; AIE top-33 is 0.414, and AIE top-20 is 0.124.
    • Representation and flow: 6 principal components per main head explain 97% of task variance. The authors map the rotated directions to periods 2, 5, 10 and 25, 50, linked to the ones and tens digits respectively. On five shots, label tokens contribute 96–99% of each main head's task signal.
    • Generalization: the same pipeline is applied to Phi-4-14B, Qwen-2.5-7B, and to subtraction, multiplication, and two types of semantic tasks. The authors state that (15, 1) is a main head in every task family in Llama-3. Qwen's unit-coefficient intervention accuracy is 0.270, and clean accuracy is 0.896; the authors connect the gap to digit-based tokenization and exact-token-match.
    • Authors' conclusion: the authors state that ICL in large models can also be localized to specialized subspaces of a few heads, and that these heads extract, represent, and aggregate task information in an interpretable way. How heads within a family cooperate, and whether subspaces can be shared or transferred across tasks, the authors write as still open.
Read originalarxiv.org