Mechanistic Training Data Attribution

This is an explainer and reimplementation of the 2026 paper by Chen et al.

Note: This post is currently incomplete!

In this post, I attempt to explain and walk through this recent paper on training data attribution.

Objective

We want to perform a kind of training data attribution. Specifically, we want to trace internal mechanisms exhibited by interpretable units of a language model (such as induction heads) back to specific training examples. We can do this through influence functions.


Background on Influence Functions

Definition. Let zk=(xk,yk)z_k = (x_k, y_k) be an element of a dataset D={zi}i=1N\mathcal{D} = \{z_i\}_{i=1}^{N}, and let ϵ\epsilon be the weighting of zkz_k. Then the response function is the optimal solution θ(ϵ)\theta^{*}(\epsilon) that minimizes the loss L\mathcal{L}:

θ(ϵ)=argminθRD1Ni=1NL(zi,θ)+ϵL(zk,θ)\theta^{*}(\epsilon) = \operatorname{argmin}_{\theta \in \mathbb{R}^{\mathcal{D}}} \frac{1}{N} \sum_{i=1}^N {\mathcal{L}(z_i, \theta)} + \epsilon \mathcal{L}(z_k, \theta)

Definition. The influence function Iθ(zk)\mathcal{I}_{\theta^*}(z_k) is a function that measures the influence of a specific training example zkz_k on the perturbed model parameters θ\theta^*.

This influence is exactly equal to the difference between the perturbed and unperturbed optima, which can be found through a first-order Taylor expansion at 0:

Iθ(zk)=θ(ϵ)θ(0)=θ(ϵ)ϵϵ\mathcal{I}_{\theta^*}(z_k) = \theta^*(\epsilon) - \theta^*(0) = \frac{\partial \theta^*(\epsilon)}{\partial \epsilon} \cdot \epsilon

We can easily obtain the expression for this partial as:

θ(ϵ)ϵ=H1θL(zk,θ)\frac{\partial \theta^{*}(\epsilon)}{\partial \epsilon} = - \mathrm{H}^{-1} \nabla_{\theta} \mathcal{L}(z_k, \theta^*)

where H=θ2(1Ni=1NL(zi,θ)+ϵL(zk,θ))\mathrm{H} = \nabla_{\theta}^2 \left(\frac{1}{N} \sum_{i=1}^{N} \mathcal{L}(z_i, \theta^*) + \epsilon \mathcal{L}(z_k, \theta^*) \right)


However, this expression alone is not very useful for our purposes. Instead, we would like to find the influence of the training example zkz_k on the test loss L(ztest,θ)\mathcal{L}(z_{test}, \theta^*). Let's call this function ff for simplicity. Then the influence If\mathcal{I}_f of zkz_k on ff is given by:

If=L(ztest,θ)ϵ=θ(L(ztest,θ))θ(ϵ)ϵ\mathcal{I}_f = \frac{\partial \mathcal{L}(z_{test}, \theta^*)}{\partial \epsilon} = \nabla_{\theta}(\mathcal{L}(z_{test}, \theta^*))^{\top} \frac{\partial \theta^{*}(\epsilon)}{\partial \epsilon}

Substituting:

If(zk,ztest)=θ(L(ztest,θ))H1θL(zk,θ)\mathcal{I}_f(z_k, z_{test}) = - \nabla_{\theta}(\mathcal{L}(z_{test}, \theta^*))^{\top} \mathrm{H}^{-1} \nabla_{\theta} \mathcal{L}(z_k, \theta^*)

Problem. H\mathrm{H} is a huge matrix. Indeed, for a layer of dimensions din×doutd_{in} \times d_{out}, the Hessian is of size (din×dout)2(d_{in} \times d_{out})^2. This is computationally infeasible to invert.

Solution. We approximate the Hessian as the Kronecker product of two smaller matrices using a clever method called EKFAC:

H(UAUS)Λ(UAUS)\mathrm{H} \approx (U_A \otimes U_S) \Lambda (U_A \otimes U_S)^{\top}

where UAU_A and USU_S are the eigenvector matrices obtained from eigendecomposing the two K-FAC covariance factors separately: the input-activation covariance A=E[aa]=UAΛAUAA = \mathbb{E}[a\,a^\top] = U_A \Lambda_A U_A^\top (computed from that layer's input activations aa) and the output-gradient covariance S=E[gg]=USΛSUSS = \mathbb{E}[g\,g^\top] = U_S \Lambda_S U_S^\top (computed from the gradients gg flowing into that layer's outputs). The Kronecker product of the two eigenvector matrices, UAUSU_A \otimes U_S, then gives the eigenbasis of the full K-FAC approximation ASA \otimes S, since (UAUS)(U_A \otimes U_S) diagonalizes ASA \otimes S exactly whenever UAU_A diagonalizes AA and USU_S diagonalizes SS.

Λ\Lambda is a diagonal matrix of corrected eigenvalues. Rather than naively taking Λ=ΛAΛS\Lambda = \Lambda_A \otimes \Lambda_S as in KFAC, EK-FAC re-estimates the true eigenvalues by projecting actual per-example gradients onto the fixed eigenbasis (UAUS)(U_A \otimes U_S) and computing their variance in that basis.


Mechanistic Data Attribution

Definition. The MDA framework is characterized by the 3-tuple (μ,π,fprobe)(\mu, \pi, f_{probe}):

  • The monitoring metric μ\mu measures when the behaviour of a certain head shows variation, e.g. prefix-matching score for induction heads

  • The subspace projection π\pi identifies a parameter subspace corresponding to an interpretable unit of the model, i.e. π(θ)=θsubθ\pi(\theta) = \theta_{sub} \subseteq \theta

  • fprobef_{probe} measures the performance of the interpretable unit in question on a test dataset Dprobe\mathcal{D}_{probe} (and thus we may have μ=fprobe\mu = f_{probe}).

To measure the influence of training examples on specific interpretable units, we modify the expression for I\mathcal{I} as follows:

I(ztrain,Dprobe)=θsubL(ztrain)Hθsub1fprobe(θ,Dprobe)\mathcal{I}(z_{train}, \mathcal{D}_{probe}) = -\nabla_{\theta_{sub}} \mathcal{L}(z_{train})^{\top} \mathrm{H}_{\theta_{sub}}^{-1} f_{probe} (\theta, \mathcal{D}_{probe})

Here Hθsub1\mathrm{H}^{-1}_{\theta_{sub}} is the EKFAC-approximated Hessian restricted to the relevant parameter subspace θsub\theta_{sub}.

Implementation

I train a toy 2-layer Transformer in PyTorch on a very simple synthetic induction dataset (with each example constructed as [prefix, prefix]) with the following config:

HyperparameterValue
Layers2
Heads per layer4
d_model64
Steps1000

Note that there are no MLPs in the model.

I use the standard prefix matching score as my μ\mu, θQK,θV,θO\theta_{QK}, \theta_V, \theta_O as θsub\theta_{sub}, and the summed log-probability of the correct induction target over synthetic [prefix,prefix][\mathrm{prefix}, \mathrm{prefix}] sequences as fprobef_{probe}. Note that there are certain changes in my implementation:

  • I don't use LayerNorm, so in my code you'll see that use hook_resid_pre, i.e. hook the pre-norm residual stream.
  • fprobef_{probe} is log probs instead of the raw logit dot unembedding formulation in A.3. This is easier to get exactly right here since I know the ground-truth match position in closed form.
  • Because fprobef_{probe} is maximized rather than minimized, I drop the leading negative sign in s=θL(ztrain)ps = \nabla_\theta \mathcal{L}(z_{train})^\top p, p=Hθsub1θfprobep = H^{-1}_{\theta_{sub}} \nabla_\theta f_{probe}. Positive score still means "promotes the mechanism."

Results