Fine-Tuning Dynamics of In-Context Factual Recall in Transformers

Ruomin Huang, Eshaan Nichani, Jason D. Lee, Rong Ge · 2026

Read Fine-Tuning Dynamics of In-Context Factual Recall in Transformers by Ruomin Huang, Eshaan Nichani, Jason D. Lee, Rong Ge (2026) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2605-27774.txt.

Introduction

Huang and colleagues study how fine-tuning teaches a transformer to recall facts from examples given in its prompt. They define a task where the model must infer a hidden relation from sample pairs and retrieve the matching answer from stored knowledge. Theory and experiments show the model converges on a pairwise attention pattern, needing only a very small number of training examples to succeed.

Catalog record

CREATORRuomin Huang, Eshaan Nichani, Jason D. Lee, Rong Ge
YEAR2026
COLLECTIONPAPERS
FORMATTXT
FILE SIZE101298
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2605.27774

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Fine-Tuning Dynamics of In-Context Factual Recall in Transformers Ruomin Huang, Eshaan Nichani, Jason D. Lee, Rong Ge Abstract In-context learning – performing tasks based on examples given in the prompt – is an important capability that has emerged in large language models and has received significant attention in both theory and practice. Existing theoretical work often focuses on settings where the learning uses information purely from the prompt. However, many practical instances of in-context learning require the model to retrieve factual knowledge stored in the model’s parameters, with the context serving to identify which knowledge is relevant. In this work, we study how in-context learning leverages factual knowledge recall. We formalize this behavior by introducing the in-context factual recall (IC-recall) task, where a transformer is provided a context of (subject, answer) pairs generated from a hidden relation, along with a query subject, and must both infer this hidden relation and retrieve the corresponding answer. Factual knowledge is modeled by the transformer having access to a simple pre-constructed MLP associative memory storing (subject, relation, answer) triplets. We analyze the supervised fine-tuning dynamics of a one-layer transformer on IC-recall data and prove that the model successfully performs IC-recall by converging to a particular pairwise attention pattern. This fine-tuning stage requires a very small number of samples – only polylogarithmic in the number of stored knowledge triplets. Experiments verify our theoretical predictions and show that the pairwise attention pattern emerges even when the MLP layer is pretrained instead of constructed. 1 Introduction Transformer-based large language models (LLMs) exhibit strong in-context learning (ICL) abilities: they can use examples provided in the prompt to improve prediction on a new query ( 3 ) . Considerable research has sought to understand the mechanisms underlying ICL, and most existing works focus on settings in which transformers rely only on the context to make predictions. One line of work studies ICL over function classes ( 12 ; 30 ; 36 ) , where transformers are trained from scratch on sequences of (x,f(x)) examples for a context-dependent function f , and learn to predict f(x_{q}) for a new query x_{q} . Another line of work ( 22 ; 11 ) studies how transformers learn to solve particular matching or copying tasks by converging to the induction head mechanism ( 24 ) . Although these are interesting settings to study ICL from a theoretical perspective, they do not capture an important behavior common in real-world LLM applications, where the model must combine contextual information with factual knowledge, or parametric knowledge ( 25 ; 28 ; 9 ) , stored in its weights. For example, given the prompt “Albert Einstein \to Germany, Isaac Newton \to England, Marie Curie \to ?”, the model must first use its stored knowledge to infer that the relation between subject and answer is nationality, then answer the query by retrieving the fact (again from stored knowledge) that Marie Curie was Polish. Figure 1 : Left: factual knowledge corpus. Each sentence consists of three components: subject, relation and answer. We can put these components into the subject set \mathcal{S}=\{\text{Jack, Alice, Bob}\} , the relation set \mathcal{R}=\{\text{was born in, worked at}\} and the answer set \mathcal{A}=\{\text{Boston, New York, Chicago, San Francisco}\} . We can view each relation r\in\mathcal{R} as a mapping from \mathcal{S} to \mathcal{A} . Right: IC-recall data requires the model to complete the sequence in a specific format: (s_{1},a_{1},s_{2},a_{2},s_{3},u_{\mathrm{EoS}}) . Here u_{\mathrm{EoS}} is the End-of-Sequence (EoS) token, s_{1},s_{2}\in\mathcal{S} and a_{1}=r(s_{1}),a_{2}=r(s_{2}) for some underlying relation r . In this paper, we formalize this behavior through the in-context factual recall (IC-recall) task. Motivated by 25 ; 15 ; 23 , we model factual knowledge as (\text{subject},\text{relation},\text{answer}) triplets. Each sequence in the IC-recall task consists of (subject, answer) pairs generated from the same relation, along with a query subject; the goal of the task is to output the answer corresponding to the query subject and hidden relation. Here, the context alone is insufficient to answer the query, as the model must also rely on the factual knowledge. See Figure 1 for an illustration of the task, and Section 2 for a formal definition. 1.1 Our Results We consider fine-tuning a model which already stores factual knowledge in its parameters on sequences from the IC-recall task. Specifically, the model is a transformer with a single self-attention head followed by an MLP layer. We construct an MLP layer that acts as an associative memory ( 23 ) storing (subject, relation, answer) triplets (see Section 3 ), and assumes that the MLP layer remains fixed throughout fine-tuning. We fine-tune the model to access this relevant knowledge in two steps: it first infers the underlying relation r from the context, and then uses r to predict the answer. This two-step structure naturally aligns with the chain-of-thought (CoT) ( 32 ) paradigm, where an intermediate decoding step outputs r before producing the final answer. In Section 4 , we analyze the supervised fine-tuning dynamics of this one-layer transformer on IC-recall data with two in-context examples. We prove that the transformer successfully performs IC-recall by converging to a pairwise attention pattern – the transformer places equal attention weight to the tokens within each pair of in-context examples, but different attention weights across pairs. This identifies a mechanism for ICL which is qualitatively different from the induction head ( 24 ) . We also show that the number of fine-tuning samples required to achieve high test accuracy on the IC-recall task grows polylogarithmically with the number of stored factual associations. Empirically, we show in Section 5 that as predicted by theory, fine-tuning only requires a very small number of sequences (8 often already achieves high accuracy). We also show that similar pairwise attention patterns extend to the cases with more than two in-context examples, and when the MLP layer is pretrained instead of constructed. 1.2 Related works Knowledge localization and associative memories. Prior work suggests that factual knowledge in language models can be stored in either MLP layers ( 14 ; 20 ; 10 ) , though it may also be encoded in attention layers ( 8 ; 33 ) . Recent theory studies the ability of transformers to store factual knowledge via associative memories, showing that their storage capacity scales proportionally with model size ( 2 ; 23 ; 5 ) . 26 further propose a construction in which the required model size depends only logarithmically on the number of subjects. 23 analyze the training dynamics of associative memory formation in one-layer linear transformers, and 6 study the training dynamics for linear layers. Theoretical understanding of in-context learning. 12 study the function classes that transformers can learn in context. 30 show that transformers can learn linear functions in context by implicitly simulating gradient descent, and 36 later prove convergence in this setting. 24 identify induction heads as a mechanism underlying certain forms of in-context learning. 22 ; 11 analyze how induction heads emerge via gradient-based training on in-context Markov chains. 7 study how transformers learn a generalized induction head from in-context n-gram data. 4 analyze factual-recall ICL under a data model that admits task-vector arithmetic, where answers are obtained by adding a retrieved task vector to the query representation. Among prior works, 29 study the

Read the complete text