HyperAIHyperAI

Command Palette

Search for a command to run...

LLM
Transformer

Llama See, Llama Do : une perspective mécanistique sur l'entraînement contextuel et la distraction dans les grands modèles de langage

Jingcheng Niu Xingdi Yuan Tong Wang Hamidreza Saghir Amir H. Abdi

Résumé

Nous observons un phénomène nouveau, l'entraînement contextuel, dans un large éventail de modèles de langage (ML) et de configurations d'invites, offrant une nouvelle perspective mécanistique sur la manière dont les ML sont distraits par des informations contextuelles « non pertinentes » dans l'invite d'entrée. Plus précisément, les ML attribuent des logits (ou probabilités) significativement plus élevés à tout jeton ayant déjà figuré dans l'invite contextuelle, y compris pour des jetons aléatoires. Cela suggère que l'entraînement contextuel est un phénomène mécanistique, se produisant indépendamment de la pertinence ou de la relation sémantique des jetons par rapport à la question ou au reste de la phrase. Nous trouvons des preuves statistiquement significatives que l'amplitude de l'entraînement contextuel est influencée par des facteurs sémantiques. Les invites contrefactuelles ont un effet plus important que les invites factuelles, ce qui suggère que, bien que l'entraînement contextuel soit un phénomène mécanistique, il est modulé par des facteurs sémantiques. Nous formulons l'hypothèse de l'existence d'un circuit de têtes d'attention — les têtes d'entraînement — correspondant au phénomène d'entraînement contextuel. En utilisant une méthode inédite de découverte des têtes d'entraînement fondée sur le masquage différentiable, nous identifions ces têtes dans différentes configurations. Lorsque nous « désactivons » ces têtes, c'est-à-dire que nous mettons leur sortie à zéro, l'effet de l'entraînement contextuel est significativement atténué, ce qui amène le modèle à générer une sortie conforme à celle qu'il produirait en l'absence de contexte perturbateur. Notre découverte de l'entraînement contextuel, ainsi que notre investigation de la distraction dans les ML par le biais des têtes d'entraînement, constituent une étape clé vers l'analyse mécanistique et l'atténuation du problème de la distraction.

One-sentence Summary

University of Toronto, TU Darmstadt, and Microsoft Research identify contextual entrainment, a mechanistic phenomenon in which LMs assign inflated logits to tokens appearing earlier in the context prompt regardless of their semantic relevance, and localize this effect to specific attention heads via a differentiable masking method, demonstrating that ablating these heads significantly attenuates distraction.

Key Contributions

  • The work identifies and characterizes contextual entrainment, where language models assign higher logits to tokens that appeared earlier in the prompt, even for random tokens, and finds that counterfactual contexts strengthen the effect relative to factual contexts.

  • The paper introduces a method based on differentiable masking for discovering entrainment heads, revealing attention-head circuits associated with contextual entrainment more effectively than analyzing attention heads in isolation.

  • Ablating the discovered entrainment heads, by setting their outputs to zero, significantly reduces contextual entrainment and yields outputs closer to those produced without distracting context, offering a mechanistic account and a potential mitigation strategy for distraction.

Introduction

Large language models can use prompt context through in-context learning, which matters for retrieval augmented generation because added context can both help and distort model outputs. Prior work describes distraction mainly as irrelevant context, but that definition is too coarse to explain why different irrelevant prompts have different effects or how models internally misuse context. The authors identify contextual entrainment, where any token that appeared earlier in the prompt receives higher logits regardless of relevance, a phenomenon amplified by counterfactual content. They also locate a small set of entrainment heads using differentiable masking, showing that disabling them suppresses the effect.

Method

Discovering Entrainment Heads

The authors leverage a differentiable circuit discovery approach to identify which attention heads are responsible for contextual entrainment. Instead of analyzing each attention head in isolation, they formulate the problem as finding a small subset of heads whose removal suppresses the model’s tendency to follow counterfactual information from the context, even when the evaluated question is not directly related to that counterfactual information.

At a high level, the method works by selectively disabling attention heads inside a pretrained transformer language model and learning which heads are most important for propagating contextual information through the network. For each attention head, the authors introduce a learnable mask that controls whether the head contributes to the residual stream. When the mask is active, the head behaves normally; when it is inactive, its contribution is effectively removed.

For a given transformer block, the masked head contribution can be written as:

zi=∑hj∈Himjhj(xi),z_i = \sum_{h_j \in H_i} m_j h_j(x_i),zi​=hj​∈Hi​∑​mj​hj​(xi​),

where hjh_jhj​ denotes the jjj-th attention head, HiH_iHi​ is the set of heads in block or layer iii, xix_ixi​ is the input to that block, and mjm_jmj​ is the binary mask associated with head hjh_jhj​.

Because binary masks are not differentiable, the authors use a Gumbel sigmoid relaxation with a straight through estimator. Each mask is generated from a learnable logit lil_ili​, a temperature τ\tauτ, and two uniform random variables. The sampled mask is computed as:

si=σ(li−log⁡log⁡U1log⁡U2τ),s_i = \sigma\left(\frac{l_i - \log \frac{\log \mathcal{U}_1}{\log \mathcal{U}_2}}{\tau}\right),si​=σ(τli​−loglogU2​logU1​​​), mi=[1si>12−si]detach+si.m_i = \left[\mathbb{1}_{s_i > \frac{1}{2}} - s_i\right]_{\text{detach}} + s_i.mi​=[1si​>21​​−si​]detach​+si​.

Here, σ(⋅)\sigma(\cdot)σ(⋅) is the sigmoid function, τ∈(0,∞)\tau \in (0, \infty)τ∈(0,∞) controls the sharpness of the sampled mask, lil_ili​ is the learnable logit for the iii-th head mask, and U1,U2∼Uniform(0,1)\mathcal{U}_1, \mathcal{U}_2 \sim \text{Uniform}(0, 1)U1​,U2​∼Uniform(0,1) provide stochasticity for mask sampling. The first term, 1si>1/2\mathbb{1}_{s_i > 1/2}1si​>1/2​, produces a hard binary mask during the forward pass, while the second term allows gradients to flow through sis_isi​ during the backward pass.

The optimization objective has two competing goals. The first goal is to suppress the contextual entrainment signal, meaning the masked model should reduce or remove the effect of the counterfactual context on the target output. The second goal is to keep the discovered circuit sparse, so that only a small number of important attention heads are selected. These objectives are combined into the following loss:

L=ℓmasked−ℓbaseline⏟Logits Δ+λ⋅1∣H∣∑i=1∣H∣σ(li)⏟Sparsity Loss.\mathcal{L} = \underbrace{\ell_{\text{masked}} - \ell_{\text{baseline}}}_{\text{Logits } \Delta} + \underbrace{\lambda \cdot \frac{1}{|H|} \sum_{i=1}^{|H|} \sigma(l_i)}_{\text{Sparsity Loss}}.L=Logits Δℓmasked​−ℓbaseline​​​+Sparsity Lossλ⋅∣H∣1​i=1∑∣H∣​σ(li​)​​.

In this formulation, ℓmasked\ell_{\text{masked}}ℓmasked​ represents the relevant logits when the learned masks are applied, ℓbaseline\ell_{\text{baseline}}ℓbaseline​ represents the relevant logits under the corresponding baseline setting, and Δlogits\Delta_{\text{logits}}Δlogits​ captures the effect of disabling the selected heads. The sparsity term penalizes using too many active heads, where ∣H∣|H|∣H∣ is the total number of candidate heads, lil_ili​ is the mask logit for head iii, and λ\lambdaλ controls the strength of the sparsity penalty. By optimizing this objective, the method learns a compact set of heads whose contribution is strongly associated with contextual entrainment.

The training procedure treats the original transformer parameters as fixed and learns the mask logits lil_ili​ for the attention heads. The method is applied to each task configuration separately, allowing the discovered head set to reflect the structure of that specific entrainment setting. In the reported setup, the authors use Llama 3.1 8B, which contains 1024 attention heads organized into 32 layers with 32 heads per layer. Each relation type is divided into training, development, and test splits with proportions of 80%, 10%, and 10%, respectively.

The head masks are optimized on the training set over 500 epochs. During this process, multiple checkpoints are considered, and the final set of entrainment heads is selected using both performance and parsimony. The selected checkpoint is the one that shows the strongest entrainment suppression effect while using the fewest active heads. The resulting entrainment heads are then evaluated on the held out test set, which is not used for mask optimization, hyperparameter search, or checkpoint selection.

Experiment

The experiments evaluate several language models on relation prompting with related, irrelevant, random, and counterfactual contexts, measuring how these contexts change logits and probabilities for distracting, correct, and counterfactual tokens. Results show that models consistently favor tokens appearing in context, even when the context is random, indicating a mechanistic contextual entrainment effect, while semantic content modulates the effect, with counterfactual contexts causing stronger distraction. Further interventions identify task-specific attention heads whose removal substantially suppresses entrainment with little loss to factual recall or in-context learning, supporting the conclusion that contextual entrainment is a separable internal mechanism amenable to targeted mitigation.

The prompt setup combines context facts with a factual query to test how different context types influence the answer space. It contrasts related, irrelevant, random, and counterfactual contexts, marking correct, distracting, and counterfactual target tokens for each query. Related contexts share the same relation type as the query, while irrelevant contexts use a different relation and random contexts use a single token. Counterfactual contexts introduce an altered fact that supports a target token competing with the factual answer.

Removing the identified entrainment heads substantially weakens the influence of distracting counterfactual context in the country-capital-city setting. The correct answer remains top ranked, while distracting tokens move much lower in the token distribution and are separated from the correct answer by larger logit margins. This suggests that entrainment heads specifically support contextual distraction rather than general factual recall. Turning off entrainment heads attenuates contextual entrainment in the country-capital-city test set. Distracting answer tokens fall sharply in rank after the heads are removed. Logit differences between correct and distracting tokens increase by a large margin. The observed effect is reported as statistically significant under paired tests.

A small set of attention heads across several LRE relations can greatly widen the logit gap between correct and distracting tokens when their outputs are disabled. The effect appears for varied relations, but the number and selection of heads change by relation, indicating relation-specific entrainment rather than a fixed model-wide set. Removing these heads mainly reduces contextual entrainment while leaving factual recall and in-context learning largely intact. Disabling identified heads substantially attenuates distraction and increases correct-token logit margins. Different LRE relations require different numbers of entrainment heads, suggesting task-specific mechanisms. The heads have a relatively isolated effect, with only small impact on other language-model capabilities.

Removing the country-capital entrainment heads leaves performance on other relations broadly unchanged. Accuracy remains high under both strict and credulous evaluation, with only minor shifts between the original model and the modified model. The results suggest that these heads mainly affect entrainment rather than general factual recall. Accuracy on other relations remains high after head removal. Strict and credulous results show no clear large drop. Country-capital performance remains perfect. Other factual capabilities are only slightly affected.

The experiments use prompts that pair factual queries with related, irrelevant, random, and counterfactual contexts to examine how context influences answer predictions. Removing a small, relation-specific set of attention heads sharply weakens the model’s susceptibility to distracting or counterfactual context, increasing the separation between correct and competing tokens. These effects are observed across several relation types while largely preserving factual recall and other language capabilities, indicating that the heads primarily mediate contextual entrainment rather than core knowledge.


Créer de l'IA avec l'IA

De l'idée au lancement — accélérez votre développement IA avec le co-codage IA gratuit, un environnement prêt à l'emploi et le meilleur prix pour les GPU.

Codage assisté par IA
GPU prêts à l’emploi
Tarifs les plus avantageux

HyperAI Newsletters

Abonnez-vous à nos dernières mises à jour
Nous vous enverrons les dernières mises à jour de la semaine dans votre boîte de réception à neuf heures chaque lundi matin
Propulsé par MailChimp