Data Shapley in One Training Run

## Abstract

Abstract Data Shapley provides a principled framework for attributing data’s contribution within machine learning contexts. However, existing approaches require re-training models on different data subsets, which is computationally intensive, foreclosing their application to large-scale models. Furthermore, they produce the same attribution score for any models produced by running the learning algorithm, meaning they cannot perform targeted attribution towards a specific model obtained from a single run of the algorithm. This paper introduces In-Run Data Shapley , which addresses these limitations by offering scalable data attribution for a target model of interest. In its most efficient implementation, our technique incurs negligible additional runtime compared to standard model training. This dramatic efficiency improvement makes it possible to perform data attribution for the foundation model pretraining stage for the first time. We present several case studies that offer fresh insights into pretraining data’s contribution and discuss their implications for copyright in generative AI and pretraining data curation. † † ⋆ Correspondence to Jiachen T. Wang and Ruoxi Jia (email: tianhaowang@princeton.edu , ruoxijia@vt.edu ).

## 1 Introduction

In today’s data-driven world, understanding the contribution of each data point is crucial, especially with the advent of foundation models that rely on vast amounts of training data from various sources.
The lack of reliable data attribution mechanisms can lead to significant legal and societal issues, resulting in a growing backlash against the broader use of data for model training [].
For instance, there is a risk of violating intellectual property rights, failing to fairly compensate data creators, and disincentivizing them from producing new, high-quality content [].
This has already resulted in legal disputes, such as the New York Times’ lawsuit against Microsoft/OpenAI [].
Moreover, foundation models are often trained on massive datasets scraped from the internet, which can include low-quality and harmful content [, , ].
Problematic data not only wastes computational resources but also skews model outputs, potentially leading to biased or inaccurate results. By understanding the contribution of each data source, we can identify and mitigate the influence of low-quality data, thereby improving the efficiency and quality of model training.

Since the training data of foundation models comes from multiple stakeholders, it is essential to have an algorithm that can *fairly* attribute data contributions.
In recent years, significant progress has been made in understanding what it means to fairly quantify and attribute data source contributions, with *the Shapley value* [] emerging as a widely adopted framework [, ].
Originating from cooperative game theory, the Shapley value uniquely satisfies several desirable properties:
(1) It assigns equal scores to equally impactful data points, ensuring fairness; (2) the sum of contribution scores equals the total utility, meaning the scores always represent a share of the total utility; (3) it supports additive decomposition across multiple utility functions, allowing for the calculation of contributions to the entire test set by summing the scores of individual test points.
As the Shapley value uniquely satisfies these properties, it eliminates the uncertainty and ambiguity surrounding which attribution frameworks should be used conceptually. While there are other non-Shapley data attribution frameworks, such as Datamodels [, ] and influence functions [], they lack the clear theoretical foundation and uniqueness provided by the Shapley value.

However, Data Shapley, i.e., the application of the Shapley value to attribute training data’s contribution, has been limited to very small-scale models.
Existing methods [, ] to estimate the Shapley value require retraining the model numerous times using different subsets of data to evaluate the contribution of each data source, making them computationally infeasible for foundation models. While past literature [, , ] has adopted proxy models in place of the original model to make Data Shapley calculation more efficient, they analyze the data’s contribution towards a different learning process, which may not reflect the data’s actual contribution to the target model.
On the other hand, retraining-based methods suffer from a conceptual issue often overlooked in the literature: they do not differentiate between models produced by the same stochastic learning algorithm, and only measure the average contribution to the learning algorithm. This limitation prevents targeted attribution for a specific model obtained from a single training run, which is often of practical interest.

This paper introduces *In-Run Data Shapley*, a novel approach that makes fair data attribution applicable to large-scale foundation models.
Unlike previous methods, In-Run Data Shapley quantifies the contribution of each data source to the specific target model of interest.

Technical contributions.
The key insight enabling our technique is that foundation models are trained using iterative algorithms, where the model performance change in each iteration is sufficiently small to be accurately approximated by first- or second-order Taylor expansions.
We show that Data Shapley values for these linear and quadratic approximation forms can be derived analytically via gradient dot-products or gradient-Hessian-gradient products between training and validation data.
This allows for the calculation of Data Shapley values for the specific model produced by a particular training run, provided that one can monitor the intermediate model parameters throughout the training process and compute the gradient dot-products or gradient-Hessian-gradient products.
However, leveraging off-the-shelf auto-differentiation tools to calculate these quantities introduces significant overhead due to per-sample gradient calculation.
To address this challenge, we develop a series of technical tools that enable the exact calculation of gradient dot-products and gradient-Hessian-gradient products in one and two backward passes, respectively, without the need to instantiate any additional gradient vectors or Hessian matrices. Collectively, these tools allow for the efficient computation of In-Run Data Shapley.
In particular, with sufficient GPU memory, its most efficient implementation is as fast as regular training.

Empirical implications. Given the efficient algorithms developed in this paper, for the first time, one can perform data attribution on the scale of foundation model pretraining.
While in this paper, we focus on the GPT2 model as a pilot study, our approach is applicable to larger-scale industrial models with sufficient computing resources. We performed various case studies that provide fresh insights into training data’s contribution to the foundation model pretraining.

(1) Rethinking copyright in generative AI: contribution beyond memorization (Section 5.2.1).
We studied training data’s contribution to validation points of varying similarity levels. We found that even when the validation data is a complete rewrite of the training data while maintaining the topic, the training data still contributes significantly. This finding has implications for the current dialogue around what constitutes a copyright violation in generative AI [].
While the unfair use of copyrighted content is generally only considered when the generated data is an almost verbatim replication of the training data, our contribution analysis shows that some data owners should receive a certain royalty share for generated content, even if the output does not closely resemble the copyrighted material.

(2) Data’s contribution is stage-dependent (Section 5.2.2).
In-Run Data Shapley can capture the dynamics of contribution through the course of training, a nuance that cannot be captured by Retraining-based Data Shapley [, ], nor by methods that only use the final model for attribution such as influence function []. Specifically, in the early stages of training, a general corpus tends to have a relatively large contribution regardless of the downstream tasks. This is because general corpora help the model learn basic language patterns, grammar, and common knowledge. However, in the later stages of training, the training corpus more relevant to the downstream task becomes dominant, and the contribution of the general corpus phases out.

(3) There is considerable room for improvement in data curation for pretraining (Section 5.2.3). Even well-curated pretraining corpora contain data points that negatively impact the training process. We demonstrate the effectiveness of In-Run Data Shapley in identifying these low-quality data points. By computing In-Run Data Shapley values during training and removing negatively valued data points, we show that the cleaned dataset leads to significantly faster model convergence and improved performance compared to the original dataset.
Interestingly, despite the Pile dataset [] already undergoing multiple layers of curation, In-Run Data Shapley assigns negative values to approximately 16% of the corpora. We found a significant amount of noisy data among these negatively valued corpora.
This finding indicates considerable room for improvement in data curation for foundation model pretraining.

## 2 Background

In this section, we formalize the setup of data attribution for ML and revisit Data Shapley’s definition.

Setup & Goal.
Given a dataset $\mathcal{D}_{\text{tr}}:=\{z_{i}\}_{i=1}^{N}$, data attribution or valuation aims to assign a score to each training data point $z_{i}$, reflecting its importance for the trained ML model’s performance on a certain task. Formally, we seek a score vector $(\phi_{z_{i}})_{i=1}^{N}$ where each $\phi_{z_{i}}\in\mathbb{R}$ reflects the *value* of $z_{i}$.

### 2.1 Data Shapley

The Shapley value (SV) [], originating from game theory, stands out as a distinguished method for equitably distributing total profit among all participating players. Before diving into its definition, we first discuss a fundamental concept: the *utility function*.

Utility function.
A *utility function* maps an input dataset to a score indicating the utility of the dataset for model training. In most of the existing literature [, ], the utility function $U$ is chosen as the performance (e.g., accuracy or loss) of the trained models on a hold-out validation set. That is, given a training set $S$, the utility function $U(S):=\texttt{Perf}(\mathcal{A}(S))$, where $\mathcal{A}$ represents a learning algorithm that trains a model on dataset $S$, and $\texttt{Perf}(\cdot)$ is a function assessing the model’s performance. For example, $\texttt{Perf}(\cdot)$ can be the accuracy for a classification task or the perplexity for a language completion task, evaluated on a (set of) hold-out validation data point(s).

###### Definition 1 (Shapley value [ 46 ] ) .

Let $U(\cdot)$ denote a utility function and $D$ represent a training set of $N$ data points. The Shapley value, $\phi_{z}\left(U\right)$, assigned to a data point $z\in D$ is defined as
$\phi_{z}\left(U\right):=\frac{1}{N}\sum_{k=1}^{N}{N-1\choose k-1}^{-1}\sum_{S
\subseteq D_{-z},|S|=k-1}\left[U(S\cup\{z\})-U(S)\right]$
where $D_{-z}=D\setminus\{z\}$.

In simple terms, the Shapley value is a weighted average of the *marginal contribution* $U(S\cup\{z\})-U(S)$, i.e., the utility change when the point $z$ is added to different $S$s. For simplicity, we often write $\phi_{z}$ when the utility function is clear from the context. The popularity of the Shapley value is attributable to the fact that it is the *unique* data value notion satisfying four axioms: Null player, Symmetry, Linearity, and Efficiency.
The mathematical definitions of these axioms are deferred to Appendix A.1.
Here, we introduce the *linearity* axiom which will be used later.

###### Theorem 2 (Linearity of the Shapley value [ 46 ] ) .

For any of two utility functions $U_{1},U_{2}$ and any $\alpha_{1},\alpha_{2}\in\mathbb{R}$, we have $\phi_{z}\left(\alpha_{1}U_{1}+\alpha_{2}U_{2}\right)=\alpha_{1}\phi_{z}\left(U
_{1}\right)+$ $\alpha_{2}\phi_{z}\left(U_{2}\right)$.

Retraining-based Data Shapley.
The convention of defining the utility function for Data Shapley as $U(S)=\texttt{Perf}(\mathcal{A}(S))$ was introduced in the seminal works of [] and [], where $\mathcal{A}$ is a learning algorithm such as a neural network trained by stochastic gradient descent (SGD) or its variants. With this choice of utility function, the precise calculation of the Shapley value requires retraining models on various subsets of the data. This is because the marginal contribution of a data point, $U(S\cup\{z\})-U(S)$, can only be obtained by training models on both $S$ and $S\cup\{z\}$ and comparing their performance. As a result, we refer to this method as "*Retraining-based Data Shapley*".
Besides the high computational costs, Retraining-based Data Shapley also suffers from (1) *highly unstable value scores* for stochastic learning algorithms [], and (2) conceptual issues related to its *inability to assess the contribution to a specific trained model* (detailed in Appendix B.1). These limitations further restrict its practical applications in real-world scenarios.

###### Definition 1 (Shapley value [ 46 ] ) .

###### Theorem 2 (Linearity of the Shapley value [ 46 ] ) .

## 3 In-Run Data Shapley

To address the issues associated with Retraining-based Data Shapley such as high computational costs, value instability, and the inability to assess the contribution towards a specific trained model, we propose a novel data attribution method specifically tailored for a single training run. Our key idea is to leverage the iterative nature of model training and employ a "divide and conquer" approach: breaking down the problem of valuing data contributions for the entire training process into subproblems of valuing data contributions for individual iterations.

Utility function for a single gradient update. Traditionally, the utility function $U(S)=\texttt{Perf}(\mathcal{A}(S))$ encapsulates the overall impact of a training set $S$ across the complete training process.
Here, we instead consider a "local utility function" that evaluates the impact of data subsets within a single iteration.
Specifically, given a training dataset $\mathcal{D}_{\text{tr}}=\{z_{i}\}_{i=1}^{N}$, a deep learning model is usually being trained to minimize the training loss $\sum_{i=1}^{N}\ell(w,z_{i})$ via an iterative optimization procedure such as SGD.
The performance of the model is typically being measured through a set of validation points $\{z^{(\mathrm{val})}\}$.
During an iteration $t$, a batch $\mathcal{B}_{t}\subseteq\mathcal{D}_{\text{tr}}$ of the training points is used to update the model parameters from $w_{t}$ to $w_{t+1}$ with
$w_{t+1}:=w_{t}-\eta_{t}\sum_{z\in\mathcal{B}_{t}}\nabla\ell(w_{t},z)$,
where $\eta_{t}$ is the learning rate at iteration $t$.(^1^11Note that in practice, we take the gradient average for the update, but here we incorporate the normalization term $|\mathcal{B}_{t}|$ into the learning rate $\eta_{t}$ for a clean presentation.)
A complete run of neural network training thus consists of model checkpoints $\{w_{0},w_{1},\ldots,w_{T}\}$. For a given validation data point $z^{(\mathrm{val})}$, we can define the "local utility function” at a single iteration $t$ as

$$ $\displaystyle U^{(t)}(S;z^{(\mathrm{val})}):=\ell(\widetilde{w}_{t+1}(S),z^{( \mathrm{val})})-\ell(w_{t},z^{(\mathrm{val})})$ (1) $$

where $\widetilde{w}_{t+1}(S):=w_{t}-\eta_{t}\sum_{z\in S}\nabla\ell(w_{t},z)$ and $S\subseteq\mathcal{B}_{t}$ is a subset of the batch being selected in $t$-th iteration in the original training. Interpretation: The local utility function $U^{(t)}$ represents the loss change at iteration $t$ when only the subset $S$ is used for the gradient update. This approach incorporates the realization of random batch selection at $t$-th iteration into the utility function. It can also encode other forms of training randomness (e.g., dropout) at iteration $t$.
By accounting for the specific realization of training randomness, we obtain a deterministic utility function for each iteration, effectively enabling the targeted attribution to the specific training run.

Data Shapley for a single gradient update.
While the utility $U^{(t)}$ is defined over $\mathcal{B}_{t}$ instead of the full training set $\mathcal{D}_{\text{tr}}$, it is easy to augment it to $\mathcal{D}_{\text{tr}}$.
More formally, in the augmented utility function we have $\widetilde{w}_{t+1}(S):=w_{t}-\eta_{t}\sum_{z\in S\cap\mathcal{B}_{t}}\nabla
\ell(w_{t},z)$, $S\subseteq\mathcal{D}_{\text{tr}}$.
The Shapley value $\phi_{z}(U^{(t)})$ will be exactly the same as the Shapley value corresponds to the augmented utility function for any $z\in\mathcal{B}_{t}$, and $\phi_{z}(U^{(t)})$ for any $z\in\mathcal{D}_{\text{tr}}\setminus\mathcal{B}_{t}$ (see Theorem 5 in []). Therefore, for the sake of clean presentation, we slightly abuse the notation, and the definition of $U^{(t)}$ depends on the context.

Data Shapley for the entire training run. Building on the concept of a "local" utility function for a single gradient update iteration, we naturally extend this to a "global" utility function for the entire training process, defined as $U(S)=\sum_{t=0}^{T-1}U^{(t)}(S)$. Interpretation: This global utility function can be interpreted as the cumulative loss change of the entire training run, but under the counterfactual scenario where only a subset of the training data $S$ is used. In other words, it aggregates the total impact of the subset $S$ on the model’s performance throughout the entire training process.
Due to the linearity property of the Shapley value (Theorem 2), we have $\phi_{z}(U)=\sum_{t=0}^{T-1}\phi_{z}(U^{(t)})$.
This new Data Shapley value, which we call *In-Run Data Shapley*, represents the cumulative contribution of the data point $z$ across all gradient update iterations within a single training run.
This approach breaks down the broader utility into more manageable, step-by-step assessments that capture the immediate effects of data points on model updates, and provide a more fine-grained view of how individual data points contribute to the model’s performance at each step of the training process. Notably, the sum of individual data points’ Shapley values equals the overall loss reduction achieved by the model during the entire training run due to the Shapley value’s efficiency axiom (see Appendix A.1). This provides a meaningful and interpretable measure of data importance.
In Appendix B, we give an in-depth comparison between Retraining-based and In-Run Data Shapley.

###### Remark 1 ( Multiple validation points ) .

In practice, the model performance is often being assessed based on a validation set $D^{(\mathrm{val})}=\{z^{(\mathrm{val})}\}$.
After computing $\phi_{z}\left(U(\cdot;z^{(\mathrm{val})})\right)$ for each $z^{(\mathrm{val})}\in D^{(\mathrm{val})}$, one can compute the Shapley value corresponding to the utility function on the full validation set $U(S;D^{(\mathrm{val})}):=\sum_{z^{(\mathrm{val})}\in D^{(\mathrm{val})}}U(S;z^
{(\mathrm{val})})$ by simply taking the sum $\phi_{z}\left(U(\cdot;D^{(\mathrm{val})})\right)=\sum_{z^{(\mathrm{val})}\in D
^{(\mathrm{val})}}\phi_{z}\left(U(\cdot;z^{(\mathrm{val})})\right)$ due to the *linearity* property of the Shapley value (Theorem 2).
Hence, for a clean presentation, we consider only a single $z^{(\mathrm{val})}$ in this paper. However, all the techniques we developed can be extended to multiple validation points.

###### Remark 1 ( Multiple validation points ) .

## 4 Efficient Computation of In-Run Data Shapley

The newly proposed In-Run Data Shapley does not require retraining models from scratch on different data subsets. However, calculating $\phi_{z}(U^{(t)})$ for each training iteration remains computationally intensive, as it involves evaluating the performance impact of all possible combinations within the sampled data batch. In this section, we introduce an efficient method for approximating In-Run Data Shapley scores during a specific training run.
Our approach, distinct from Monte Carlo methods, is deterministic and optimized to minimize additional runtime to regular training. In particular, in its most efficient implementation, our approximation technique incurs negligible extra runtime beyond what is required for standard model training, making it highly practical for real-world applications.

### 4.1 Approximating U ( t ) superscript 𝑈 𝑡 U^{(t)} italic_U start_POSTSUPERSCRIPT ( italic_t ) end_POSTSUPERSCRIPT with Taylor Expansion

To derive a more tractable structure for the local utility function $U^{(t)}$, we propose using first and second-order Taylor approximations. The advantage of this approach is that the approximated utility function exhibits a form where closed-form Data Shapley formulas can be derived.
The second-order Taylor approximation to the local utility function is as follows:

$$ $\begin{aligned} U^{(t)}(S)&=\ell(\widetilde{w}_{t+1}(S),z^{(\mathrm{val})})- \ell(w_{t},z^{(\mathrm{val})})\\ &=\underbrace{\nabla\ell(w_{t},z^{(\mathrm{val})})\cdot(\widetilde{w}_{t+1}(S) -w_{t})}_{U^{(t)}_{(1)}(S)}+\frac{1}{2}\underbrace{(\widetilde{w}_{t+1}(S)-w_{ t})^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{t}(\widetilde{w}_{t+1}(S)-w_ {t})}_{U^{(t)}_{(2)}(S)}+~{}\text{higher order terms}\end{aligned}$ $$

where the Hessian matrix $\textbf{H}^{(z^{(\mathrm{val})})}_{t}:=\nabla^{2}\ell(w_{t},z^{(\mathrm{val})})$. We label the first-order term as $U^{(t)}_{(1)}(S)$ and the second-order term as $U^{(t)}_{(2)}(S)$. Note that the gradient update $\widetilde{w}_{t+1}(S)-w_{t}=-\eta_{t}\sum_{z\in S}\nabla\ell(w_{t},z)$.
Given that the learning rate $\eta_{t}$ in model training is typically small, a lower-order Taylor expansion often provides an accurate approximation for the change in loss during a single gradient update, with approximation errors of $O(\eta_{t}^{2})$ and $O(\eta_{t}^{3})$ for first and second-order approximations, respectively.
In Appendix E.2, we empirically investigate the errors of first- and second-order approximations to $U^{(t)}$ on GPT2. In particular, the first-order approximation generally achieves a relative error of $<10\%$, while the second-order approximation achieves $<4\%$ error rate.

First-order In-Run Data Shapley.
Using the first-order approximation $U^{(t)}\approx U^{(t)}_{(1)}$, and substituting the gradient update expression, we have $U^{(t)}_{(1)}(S)=-\eta_{t}\sum_{z\in S}\nabla\ell(w_{t},z^{(\mathrm{val})})
\cdot\nabla\ell(w_{t},z)$. This shows that $U^{(t)}_{(1)}$ is an *additive* utility function with a closed-form Shapley calculation as follows:

###### Theorem 3 .

In-Run Data Shapley considering the first-order approximation has closed-form

$$ $\displaystyle\phi_{z}\left(U\right)\approx\sum_{t=0}^{T-1}\phi_{z}\left(U^{(t) }_{(1)}\right)$ $$

where

$$ $\displaystyle\phi_{z}\left(U^{(t)}_{(1)}\right)=-\eta_{t}\nabla\ell(w_{t},z^{( \mathrm{val})})\cdot\nabla\ell(w_{t},z),~{}~{}~{}t=0,\ldots,T-1$ $$

Thus, the first-order approximation of In-Run Data Shapley for a training point accumulates its gradient dot products with the validation data point each time the training point is sampled in the training batch.
The gradient dot product between the training point $z_{i}$ and the validation point $z^{(\mathrm{val})}$ represents the direct influence of $z_{i}$ on the validation loss at the current model parameters $w_{t}$, which essentially measures the alignment between the two gradient vectors in the parameter space.
Notably, $\phi_{z}\left(U^{(t)}_{(1)}\right)$ is equivalent to the TracIN-Ideal score proposed by []. That is, the TracIN-Ideal score can be interpreted as the Shapley value when we use first-order Taylor approximation for $U^{(t)}$. In Appendix A.4, we provide a comprehensive discussion of the differences between this work and [].

Second-order In-Run Data Shapley.
We further improve the approximation of $U^{(t)}$ using a second-order Taylor expansion, i.e., $U^{(t)}\approx U^{(t)}_{(1)}+\frac{1}{2}U^{(t)}_{(2)}$.
Fortunately, the approximated utility function maintains a tractable structure that allows a closed-form expression for the Shapley value calculation without combinatorial enumeration.

###### Theorem 4 .

In-Run Data Shapley considering the second-order approximation has closed-form

$$ $\displaystyle\phi_{z}\left(U\right)\approx\sum_{t=0}^{T-1}\left(\phi_{z}\left( U^{(t)}_{(1)}\right)+\frac{1}{2}\phi_{z}\left(U^{(t)}_{(2)}\right)\right)$ (2) $$

where

$$ $\displaystyle\phi_{z}\left(U^{(t)}_{(1)}\right)+\frac{1}{2}\phi_{z}\left(U^{(t )}_{(2)}\right)=\underbrace{-\eta_{t}\nabla\ell(w_{t},z^{(\mathrm{val})})\cdot \nabla\ell(w_{t},z)}_{\text{ \leavevmode\hbox to8.9pt{\vbox to8.9pt{ \pgfpicture\makeatletter\hbox{\hskip 4.45195pt\lower-4.45195pt\hbox to0.0pt{ \pgfsys@beginscope\pgfsys@invoke{ }\definecolor{pgfstrokecolor}{rgb}{0,0,0} \pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ }\pgfsys@color@rgb@fill{0}{0} {0}\pgfsys@invoke{ }\pgfsys@setlinewidth{0.4pt}\pgfsys@invoke{ }\nullfont\hbox to 0.0pt{\pgfsys@beginscope\pgfsys@invoke{ }{ {{}}\hbox{\hbox{{\pgfsys@beginscope\pgfsys@invoke{ }{{}{{{}}}{{}}{}{}{}{}{}{}{ }{}{}{{}\pgfsys@moveto{4.25195pt}{0.0pt}\pgfsys@curveto{4.25195pt}{2.34831pt}{ 2.34831pt}{4.25195pt}{0.0pt}{4.25195pt}\pgfsys@curveto{-2.34831pt}{4.25195pt}{ -4.25195pt}{2.34831pt}{-4.25195pt}{0.0pt}\pgfsys@curveto{-4.25195pt}{-2.34831 pt}{-2.34831pt}{-4.25195pt}{0.0pt}{-4.25195pt}\pgfsys@curveto{2.34831pt}{-4.25 195pt}{4.25195pt}{-2.34831pt}{4.25195pt}{0.0pt}\pgfsys@closepath\pgfsys@moveto {0.0pt}{0.0pt}\pgfsys@stroke\pgfsys@invoke{ } }{{{{}}\pgfsys@beginscope\pgfsys@invoke{ }\pgfsys@transformcm{1.0}{0.0}{0.0}{1 .0}{-1.75pt}{-2.25555pt}\pgfsys@invoke{ }\hbox{{\definecolor{pgfstrokecolor}{ rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ } \pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ }\hbox{{1}} }}\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} } \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope{{{}}}{}{}\hss} \pgfsys@discardpath\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope\hss}} \lxSVG@closescope\endpgfpicture}} influence of }z\text{ on the loss of }z^{( \mathrm{val})}}+\underbrace{\frac{\eta_{t}^{2}}{2}\nabla\ell(w_{t},z)^{ \intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{t}\left(\sum_{z_{j}\in\mathcal{B} _{t}}\nabla\ell(w_{t},z_{j})\right)}_{\text{ \leavevmode\hbox to8.9pt{\vbox to 8.9pt{\pgfpicture\makeatletter\hbox{\hskip 4.45195pt\lower-4.45195pt\hbox to0. 0pt{\pgfsys@beginscope\pgfsys@invoke{ }\definecolor{pgfstrokecolor}{rgb}{0,0,0 }\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ }\pgfsys@color@rgb@fill{0}{0 }{0}\pgfsys@invoke{ }\pgfsys@setlinewidth{0.4pt}\pgfsys@invoke{ }\nullfont \hbox to0.0pt{\pgfsys@beginscope\pgfsys@invoke{ }{ {{}}\hbox{\hbox{{\pgfsys@beginscope\pgfsys@invoke{ }{{}{{{}}}{{}}{}{}{}{}{}{}{ }{}{}{{}\pgfsys@moveto{4.25195pt}{0.0pt}\pgfsys@curveto{4.25195pt}{2.34831pt}{ 2.34831pt}{4.25195pt}{0.0pt}{4.25195pt}\pgfsys@curveto{-2.34831pt}{4.25195pt}{ -4.25195pt}{2.34831pt}{-4.25195pt}{0.0pt}\pgfsys@curveto{-4.25195pt}{-2.34831 pt}{-2.34831pt}{-4.25195pt}{0.0pt}{-4.25195pt}\pgfsys@curveto{2.34831pt}{-4.25 195pt}{4.25195pt}{-2.34831pt}{4.25195pt}{0.0pt}\pgfsys@closepath\pgfsys@moveto {0.0pt}{0.0pt}\pgfsys@stroke\pgfsys@invoke{ } }{{{{}}\pgfsys@beginscope\pgfsys@invoke{ }\pgfsys@transformcm{1.0}{0.0}{0.0}{1 .0}{-1.75pt}{-2.25555pt}\pgfsys@invoke{ }\hbox{{\definecolor{pgfstrokecolor}{ rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ } \pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ }\hbox{{2}} }}\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} } \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope{{{}}}{}{}\hss} \pgfsys@discardpath\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope\hss}} \lxSVG@closescope\endpgfpicture}} interaction between }z\text{ and other training points}}$ (3) $$

for any $t=0,\ldots,T-1$.

Compared to the first-order In-Run Data Shapley, the second-order In-Run Data Shapley includes an additional gradient-Hessian-gradient product term that captures the interaction between the training point of interest $z$ and the rest of the training set. The Hessian matrix represents the curvature of the validation loss function at the current model parameters $w_{t}$. This interaction term measures the alignment between the gradient of $z$ and the gradients of the other points in the training batch, adjusted by the Hessian. If this term is large, it indicates that the presence of other points in the batch significantly impacts the value attributed to $z$. For example, if there are many identical or similar copies of $z$ in the training set, the contribution of $z$ will decrease, as the interaction term will be large, effectively distributing the value among the similar points. By incorporating this interaction term, the second-order In-Run Data Shapley provides a more fine-grained contribution measure that takes into account both the relevance of a data point towards a validation set and its uniqueness within the population.

###### Theorem 3 .

###### Theorem 4 .

### 4.2 Efficient Computation of Gradient Dot-product and Gradient-Hessian-Gradient Product

Although we have derived closed-form formulas for In-Run Data Shapley using first- or second-order Taylor approximation of the local utility functions, efficiently computing these values remains a challenge. Specifically, for the first-order In-Run Data Shapley, it requires computing 1 the pairwise gradient dot products between each $z\in\mathcal{B}_{t}$ and the validation point. For the second-order In-Run Data Shapley, it additionally requires computing 2 the gradient-Hessian-gradient products for each $z\in\mathcal{B}_{t}$. A direct implementation to compute 1 involves calculating the individual gradient for each data point in $\mathcal{B}_{t}$, which cannot benefit from fast batch processing in GPUs and necessitates running backpropagation $|\mathcal{B}_{t}|$ times with a mini-batch size of 1. Consequently, this approach would be at least $|\mathcal{B}_{t}|$ times slower than regular training, making it computationally prohibitive for practical applications. Furthermore, computing 2 requires either computing each individual gradient again or storing all individual gradients, which incurs significant time or memory overhead.

Computing pair-wise gradient dot-products in 1 backpropagation.
Our technique for efficiently computing pairwise gradient dot products is inspired by the "ghost clipping" technique from the differential privacy (DP) literature []. "Ghost clipping" enables computing *all* of the per-sample gradient norms within one backpropagation without explicitly forming any individual gradient vectors, which enhances the efficiency of DP model training. Here, we propose a "ghost dot-product" technique that shares the idea of exploiting the computation that has been done in the backpropagation.
Specifically, denote a sample batch as $\mathcal{B}_{t}=\{z_{1},\ldots,z_{B}\}$. We demonstrate this technique using a simple linear layer $\textbf{s}=\textbf{a}\textbf{W}$, where $\textbf{W}\in\mathbb{R}^{d_{1}\times d_{2}}$ is the weight matrix, $\textbf{a}=(\textbf{a}^{(1)},\ldots,\textbf{a}^{(B)})^{\intercal}$ is the mini-batch input, and $\textbf{s}=(\textbf{s}^{(1)},\ldots,\textbf{s}^{(B)})^{\intercal}$ is the output (i.e., the pre-activation tensor).
For (non-sequential) data, $\textbf{a}\in\mathbb{R}^{B\times d_{1}},\textbf{s}\in\mathbb{R}^{B\times d_{2}}$.
By applying the chain rule, we can express the gradient of an individual loss $\ell^{(i)}:=\ell(w,z_{i})$ with respect to W as

$$ $\displaystyle\frac{\partial\ell^{(i)}}{\partial\textbf{W}}=\frac{\partial\ell^ {(i)}}{\partial\textbf{s}^{(i)}}\otimes\frac{\partial\textbf{s}^{(i)}}{ \partial\textbf{W}}=\frac{\partial\ell^{(i)}}{\partial\textbf{s}^{(i)}}\otimes \textbf{a}^{(i)}=\frac{\partial\ell}{\partial\textbf{s}^{(i)}}\otimes\textbf{a }^{(i)}$ (4) $$

where $\ell:=\sum_{j=1}^{B}\ell^{(j)}$ is the aggregated loss, and the last step is because other data points’ losses have no dependency on $\textbf{s}_{i}$. Note that the individual’s output gradient $\frac{\partial\ell^{(i)}}{\partial\textbf{s}^{(i)}}=\frac{\partial\ell}{
\partial\textbf{s}^{(i)}}$ is readily available during the backpropagation pass in terms of $\ell$.
Suppose we are interested in computing the gradient dot-product $\frac{\partial\ell^{(1)}}{\partial\textbf{W}}\odot\frac{\partial\ell^{(2)}}{
\partial\textbf{W}}$ between two data points $z_{1},z_{2}$ in the same batch in the backpropagation.
For non-sequential data, we have each $\textbf{a}^{(i)}\in\mathbb{R}^{d_{1}\times 1}$ and $\frac{\partial\ell^{(i)}}{\partial\textbf{s}^{(i)}}\in\mathbb{R}^{1\times d_{2}}$.
By (4), we have

$$ $\displaystyle\frac{\partial\ell^{(1)}}{\partial\textbf{W}}\odot\frac{\partial \ell^{(2)}}{\partial\textbf{W}}=\left(\textbf{a}^{(1)}\otimes\frac{\partial \ell^{(1)}}{\partial\textbf{s}^{(1)}}\right)\odot\left(\textbf{a}^{(2)}\otimes \frac{\partial\ell^{(2)}}{\partial\textbf{s}^{(2)}}\right)=\left(\left(\textbf {a}^{(1)}\right)^{\intercal}\textbf{a}^{(2)}\right)\left(\left(\frac{\partial \ell^{(1)}}{\partial\textbf{s}^{(1)}}\right)^{\intercal}\left(\frac{\partial \ell^{(2)}}{\partial\textbf{s}^{(2)}}\right)\right)$ (5) $$

Hence, we can first take the two inner products, and then multiply the results together.
All of the quantities $\textbf{a}^{(1)},\textbf{a}^{(2)},\frac{\partial\ell^{(1)}}{\partial\textbf{s}
^{(1)}},\frac{\partial\ell^{(2)}}{\partial\textbf{s}^{(2)}}$ in (5) that are required for computation are all already available in the backpropagation. Hence, within a *single* backpropagation, we can efficiently compute the gradient dot-product between *every* pair of $z_{i},z_{j}\in\mathcal{B}_{t}$.
Since we are interested in computing the gradient dot-product between $z^{(\mathrm{val})}$ and $z$ for all $z\in\mathcal{B}_{t}$, we can backpropagate on $\sum_{z\in\mathcal{B}_{t}}\ell^{(i)}+\ell^{(z^{(\mathrm{val})})}$ to save another backpropagation for $z^{(\mathrm{val})}$.
We call this technique the *"ghost dot-product"*, as no gradient vectors are instantiated during the computation.
Overall, we only need *one* backpropagation to compute 1 for *all* data points in $\mathcal{B}_{t}$, a significant improvement over the direct method requiring $\geq|\mathcal{B}_{t}|$ backpropagations.
Additional details for this technique are in Appendix D.

###### Remark 2 .

While we illustrate our "ghost dot-product" technique using linear layers, it can be easily extended to other types of layers by leveraging similar decompositions as in Equation (4) that have been developed in differential privacy literature [, , , , ].

Computing gradient-Hessian-gradient products in 2 backpropagations (Appendix D.2).
For second-order In-Run Data Shapley, an outstanding challenge is how to efficiently compute the pairwise interaction term among training points. In Appendix D.2, we develop a *"ghost gradient-Hessian-gradient product"* technique for computing the desired quantity through one extra backpropagation pass, without materializing any gradient-sized vectors. This technique leverages several properties of neural network gradients across different layers, and its derivation is complex.

Further improvement of runtime and memory requirements (Appendix D.3).
With the "ghost" techniques developed, the computation of first- and second-order In-Run Data Shapley requires one and two backpropagations in each gradient update iteration respectively.
Although we still need to compute the gradient of the aggregated loss $\sum_{z_{i}\in\mathcal{B}_{t}}\ell_{i}$ for the training batch to perform parameter updates, we do *not* need an additional backpropagation. By reusing the activations and output gradients from the previous backpropagation on $\sum_{z\in\mathcal{B}_{t}}\ell^{(i)}+\ell^{(z^{(\mathrm{val})})}$, we can easily compute this quantity *without* incurring the cost of an extra backpropagation pass. Consequently, training while computing first-order In-Run Data Shapley will have minimal additional runtime overhead, as it still requires only one backpropagation per iteration. The second-order In-Run Data Shapley necessitates one extra backpropagation per iteration. Nevertheless, both methods provide significant advantages over the direct approach of instantiating per-sample gradients and Hessian-vector products.

###### Remark 2 .

## 5 Experiments

In this section, we evaluate In-Run Data Shapley in terms of its computational efficiency as well as its effectiveness in data attribution through a case study on GPT2 pretraining.

Model and Dataset Setting.
Due to computational resource constraints, our experiments focus on one representative and relevant setting: training a GPT2-small model(^2^22https://huggingface.co/openai-community/gpt2) on the Pile dataset [] with all copyrighted content removed.(^3^33https://huggingface.co/datasets/monology/pile-uncopyrighted.)
The uncopyrighted Pile consists of data from 16 domains.
The full training process takes approximately 4 days to achieve reasonable convergence on an A100 GPU. The training details are deferred to Appendix E.

### 5.1 Runtime Analysis

Figure: Figure 1: Runtime comparison between regular model training and training while computing for In-Run Data Shapley. The plot shows the average runtime based on 5 independent runs. Experiments were conducted on a single NVIDIA A100 GPU with 80GB memory.
Refer to caption: extracted/5700225/images/runtime_5.png

We empirically assess the computational efficiency of In-Run Data Shapley with "ghost dot-product" and "ghost vector-Hessian-vector product" techniques developed in Section 4.2.
We compare this to the direct implementation of In-Run Data Shapley, which requires computing per-sample gradients, as well as to regular training without Data Shapley computations.
As illustrated in Figure 5.1, the runtime of first-order In-Run Data Shapley is very close to that of regular training when using the ghost dot-product algorithms developed in Section 4.2. The second order In-Run Data Shapley is approximately $2\times$ slower than regular training due to the additional backpropagation. However, both the first- and second-order In-Run Data Shapley are significantly faster ($>30\times$) compared to the naive implementation. These results showcase the substantial improvements achieved by our techniques, making In-Run Data Shapley computationally feasible for practical applications.

### 5.2 Case Study: Data Attribution for GPT2 on Pile Dataset

In this section, we conduct a case study to demonstrate the use cases of In-Run Data Shapley by training a GPT2 model on the Pile dataset. We consider the use case of relevant corpus detection, study data contribution in different training stages, and data curation. The experiment details are deferred to Appendix E.

Baseline.
We mainly compare In-Run Data Shapley with influence function [], which approximates the change in the model’s loss on the test example when the training example is removed from the training set (i.e., the leave-one-out score). We omit TRAK [] and other data attribution techniques such as Datamodel [] as they are not scalable to our setting.

#### 5.2.1 Does Contribution Require Memorization?

In this experiment, we evaluate the robustness of different data attribution techniques in identifying relevant individual corpora that have been paraphrased. We start by selecting a corpus from the training set and creating several paraphrased versions using GPT-4, with varying levels of paraphrasing (see Appendix E.3 for the prompt). These paraphrased versions form our validation set. We then calculate the average value rank of the original training corpus for each of its paraphrased versions.
In addition to In-Run Data Shapley and influence function, we include the ranking result based on BM25 distance. BM25 [] featurizes examples by their word frequency statistics (i.e., TF-IDF) to rank the training instances. We use BM25 distance as an oracle for assessing the verbatim or lexical similarity between the validation corpus (query) and the training corpus, as opposed to semantic similarity.

As shown in Table 1, even for a validation corpus that is a complete rewrite (with a low BM25 distance) but covers relevant topics, the original training corpus still ranks very high according to both In-Run Data Shapley and influence function. Influence function ranks the original training corpus lower than In-Run Data Shapley, which may be attributed to the inherent noisy nature of the leave-one-out error estimation.
The results of this experiment have important implications for the current discussion of the copyright of generative AI.
The results of this experiment have important implications for the ongoing discussion about the copyright of generative AI. Specifically, the table presents a compelling example where the original Wikipedia training corpus related to a musician’s experience can significantly contribute to generating a story about a musician, even when the generated story shares no token-wise resemblance to the original training data. This finding supports that training data profoundly influences the capabilities of generative AI models and should be compensated accordingly [, ], even when the output does not closely resemble the original copyrighted material or when the model applies output filters to avoid generating verbatim replicates of the training data. This discovery expands the conventional focus on copyright violations, which typically addresses instances of near-verbatim replication, as seen in the dispute between New York Times and OpenAI, to also include cases where the generated content is significantly influenced by copyrighted material without directly replicating it.

**Table 1: Top: (left) An original training corpus from Wikipedia. (right) A synthetic corpus falls in the category of "Similar topic" to the Wikipedia corpus on the left (prompt in Appendix E.3). Bottom: the (average) value rank of the original corpus among all training corpora for validation corpora that are of varying similarity to the original corpus. The rank is out of $\approx$320k corpora.**
| Similarity Category | In-Run Data Shapley (1st order) | In-Run Data Shapley (2nd order) | Influence Function | BM25 |
| --- | --- | --- | --- | --- |
| Partial exactly the same | 1 | 1 | 1 | 1 |
| Paraphrase | 1 | 1 | 1 | 1 |
| Significant paraphrase | 32.3 | 32 | 39.3 | 1.6 |
| Similar topic | 145.6 | 141.6 | 292 | 20917.3 |

Figure: Figure 2: Left: Domain value composition for a corpus of math text. Right: The math corpus we use as the validation data for attribution, and examples of high- and low-valued training corpus for it.
Refer to caption: extracted/5700225/images/domain_composition_2.png

#### 5.2.2 How Do Data Values Change during Training?

As In-Run Data Shapley tracks the cumulative data values across different training steps, we can assess the contribution of training points at various stages of training, providing a more fine-grained perspective on data attribution. We evaluate the data attribution results for a math-related validation corpus using second-order In-Run Data Shapley.
In Figure 2, we present the value composition of training corpora by their domains over the first 10,000 training iterations, summing the values of all corpora from the same domain. We then calculate the percentage of the total value attributed to each domain, excluding domains with a total value $<0$.
As illustrated in the figure, the corpora from ArXiv achieve a significantly higher value compared to other domain corpora, far exceeding its size proportion within the full Pile dataset. This is expected, as ArXiv papers predominantly cover fields like Math, Computer Science, and Physics, which contain extensive math-related content. Furthermore, the value composition changes rapidly at the beginning of training and stabilizes as training progresses. We hypothesize that this initial fluctuation is due to the presence of relevant paragraphs in corpora from other domains. The stable value proportion observed in later stages likely reflects the relative abundance of math content in each domain.
Interestingly, we observe that Pile-CC domain, which contains general website crawls, initially shows positive contributions during the first few iterations. However, its value quickly drops to negative and eventually converges to zero. This implies that general corpora tend to have a large contribution in the beginning of training, as they help the model learn the basics of languages, such as grammar and common knowledge. However, as training progresses and the model focuses on more specialized topics, the relevance of general domains diminishes.
An additional figure for the average domain values is in Appendix E.4.

#### 5.2.3 Is Well-curated Dataset Actually Clean?

Figure: Figure 3: Test loss comparison between the original training run and the model trained on the cleaned subset according to different data attribution techniques.
Refer to caption: extracted/5700225/images/new_data_selection.png

Carefully curated pretraining corpora still contain data points that can adversely affect the training process.
Identifying and removing these data points can accelerate model convergence and enhance overall performance, thereby saving computational resources. In this experiment, we demonstrate the effectiveness of In-Run Data Shapley in assessing the data quality of a subset of the Pile dataset. We uniformly select a random subset of Pile with around 10B tokens and train a GPT2 model on this subset. We compute the data attribution results with Pile’s validation set.
By filtering out all negatively valued corpora and retraining the GPT2 model on the cleaned subset, we observe significant improvement in model convergence. For both first- and second-order In-Run Data Shapely, we can achieve around 25% fewer training iterations to reach a test loss of 3.75.
Surprisingly, our analysis reveals that around 16% of the training corpora had negative second-order In-Run Data Shapley values.
While some of the negatively valued corpora may be attributed to the significant domain shift compared to the validation corpora, we still find many low-quality corpora from Pile, a pretraining dataset that has undergone several layers of data curation [].
Examples of low-quality corpora identified can be found in Appendix E.5.

Figure 3 shows a performance comparison between the original training run and the model trained on the cleaned subsets. As we can see, influence function can also filter out low-quality data that can accelerate training convergence. However, the performance is slightly worse than In-Run Data Shapley as the influence function only uses information from the final trained models, which can result in highly noisy value scores since the removal of one training data point might have a negligible effect on the final model performance. The results demonstrate that removing lower-quality corpora leads to a significantly faster drop in test loss compared to the original training run. This implies that there is still huge room for data curation for well-curated public datasets such as Pile.

## 6 Conclusion and Limitations

In this work, we introduce In-Run Data Shapley, a novel data attribution technique that addresses the limitations of Retraining-based Data Shapley.
Extensive experiments demonstrate the effectiveness of In-Run Data Shapley in various applications. Here, we discuss the potential limitations of this work.

Availability of validation data before training.
One potential limitation of In-Run Data Shapley is that it requires the validation data to be available before training, as the data attribution scores are computed during the training process. However, there are many scenarios where validation data is naturally available before training, such as when using publicly available benchmark datasets, participating in machine learning competitions, or adhering to regulatory requirements. For scenarios where validation data arrives after the model has been trained, a potential solution is to save checkpoints of intermediate models during training and approximate In-Run Data Shapley using these checkpoints.

Extension to other optimization algorithms.
The "ghost" family techniques developed in this work are specifically tailored for SGD and do not directly extend to other popular optimizers like Adam due to their normalization terms. Nonetheless, using SGD as a proxy for Adam allows for efficient data attribution, which is the approach adopted in practice and proven to be effective in experiments. Extending these techniques to support Adam and similar optimizers remains an exciting direction for future research.

Handling memory constraints. In scenarios where GPU memory constraints prevent large batch sizes,
the "ghost" techniques can be extended by using gradient accumulation. This approach accommodates larger training batch sizes by dividing the batch into smaller sub-batches and accumulating gradients over multiple iterations. While this method may increase runtime due to additional backpropagation steps, it maintains the feasibility of the techniques under memory constraints. Improving computational efficiency for large batch sizes remains an important direction for future research.

## Acknowledgment

This work is supported in part by the National Science Foundation under grants IIS-2312794, IIS-2313130, OAC-2239622, Amazon-Virginia Tech Initiative in Efficient and Robust Machine Learning, and the Commonwealth Cyber Initiative.

We thank Meng Ding, Chong Xiang, Chendi Wang for their helpful feedback on the preliminary version of this work.

## Appendix A Extended Related Works

### A.1 Data Shapley Axioms

*Data Shapley* is one of the first principled approaches to data attribution being proposed [, ].
Data Shapley is based on the famous *Shapley value* [].
In almost all of the literature, the Shapley value is being justified as the *unique* value notion satisfying the following four axioms:

- 1.
Null player: if $U(S\cup\{z_{i}\})=U(S)$ for all $S\subseteq D\setminus\{z_{i}\}$, then $\phi_{z_{i}}(U)=0$.
- 2.
Symmetry: if $U(S\cup\{z_{i}\})=U(S\cup\{z_{j}\})$ for all $S\subseteq D\setminus\{z_{i},z_{j}\}$, then $\phi_{z_{i}}(U)=\phi_{z_{j}}(U)$.
- 3.
Linearity: For utility functions $U_{1},U_{2}$ and any $\alpha_{1},\alpha_{2}\in\mathbb{R}$, $\phi_{z_{i}}(\alpha_{1}U_{1}+\alpha_{2}U_{2})=\alpha_{1}\phi_{z_{i}}(U_{1})+
\alpha_{2}\phi_{z_{i}}(U_{2})$.
- 4.
Efficiency: for every $U,\sum_{z_{i}\in D}\phi_{z_{i}}(U)=U(D)$.

In plain words, null player axiom means the Shapley value will assign zero score to data points with no contribution.
Symmetry axiom requires equal scores assigned to equally impactful data points, ensuring fairness.
Efficiency axiom requires the sum of contribution scores equal to the total utility, meaning the scores always represent a share of the total utility.
Linearity axiom means the Shapley value supports additive decomposition across multiple utility functions, allowing for the calculation of contributions to the entire test set by summing the scores of individual test points.

### A.2 Data Shapley and Friends

Since its introduction in 2019 [, ], Data Shapley has rapidly gained popularity as a principled solution for data attribution.
Due to the computationally expensive nature of retraining-based Data Shapley, various Monte Carlo-based approximation algorithms have been developed [, , , , , , , , ], these methods still necessitate extensive computational resources due to repeated model retraining, which is clearly impractical for modern-sized ML models.
Many of its variants have been proposed.
[] argues that the efficiency axiom is not necessary for many machine learning applications, and the framework of *semivalue* is derived by relaxing the efficiency axiom. [] provide an alternative justification for semivalue based on causal inference and randomized experiments.
Based on the framework of semivalue, [] propose *Beta Shapley*, which is a collection of semivalues that enjoy certain mathematical convenience. [] propose *Data Banzhaf*, and show that the Banzhaf value, another famous solution concept from cooperative game theory, achieves more stable valuation results under stochastic learning algorithms.
[] further improves the valuation stability by considering value notions outside the scope of semivalue.
The classic leave-one-out error is also a semivalue, where the *influence function* [, , , ] is generally considered as its approximation.
Another line of works focuses on improving the computational efficiency of Data Shapley by considering K nearest neighbor (KNN) as the surrogate learning algorithm for the original, potentially complicated deep learning models [, , , , ].
Finally, [, , ] consider Distributional Shapley, a generalization of Data Shapley to data distribution.

### A.3 Alternative Data Attribution Methods

There have also been many approaches for data attribution that do not belong to the family of the Shapley value.
For a detailed survey, we direct readers to [] and [].
Here, we summarize a few recent works.
*Datamodel* [] is similar to retraining-based Data Shapley that requires training thousands of models on different data subsets to estimate the data influence of each training datum. It leverages a linear regression model to predict the model performance based on the input training set, and uses the learned regression coefficient as the measure of data influence.
We also note that Datamodel reduces to Data Banzhaf if each data point is sampled independently with probability 0.5 and no regularization is used (i.e., $p=0.5$ in the version of Datamodel discussed in []).
[] proposed *TRAK*, which improves the efficiency of Datamodel by linearizing the model output function and reduces the dimensionality of the linearized model using JL projections. However, it generally requires aggregating the estimator across multiple trained models for a reasonable performance and thus difficult to scale to large-scale models. In their original paper, the largest scale experiment is fine-tuning BERT-base.
The closest literature to this work is TracIN [], which estimates the influence of each training data by exploiting the gradient over all iterations, and their ideal version of method TracIN-Ideal coincides with our first-order In-Run Data Shapley.
In Appendix A.4, we provide a detailed discussion on the difference between our work and [].

We note that several *training-free* data valuation methods have also been proposed. For instance, [] proposed a diversity measure known as robust volume (RV) for appraising data sources. [] devised a valuation method leveraging the maximum mean discrepancy (MMD) between the data source and the actual data distribution. [] introduced a *complexity-gap score* for evaluating data value without training, specifically in the context of overparameterized neural networks.
[] applied a domain-aware generalization bound based on neural tangent kernel (NTK) theory for data valuation. [] assessed data value by measuring statistical differences between the source data and a baseline dataset.
[] utilized a specialized Wasserstein distance between training and validation sets as the utility function, alongside an efficient approximation of the LOO error. Lastly, [] utilized random forests as proxy models to propose an efficient, validation-free data valuation algorithm.
While being efficient, these works are usually only applicable to classification tasks instead of generation tasks. Moreover, they often lack a formal theoretical justification as Data Shapley-based approaches.

### A.4 Comparison of This Work with TracIN [ 39 ]

The form of first-order In-Run Data Shapley from Section 4.1 coincides with the TracIN-Ideal in []. This provides a new understanding of TracIN-Ideal as an approximation to In-Run Data Shapley.
Both works face the technical challenge of requiring per-sample gradient computations during a single training run. [] proposes *TracIN-CP*, which mitigates the computational burden by examining only a subset of intermediate checkpoints during training. At each checkpoint, the individual gradients for the entire training set are computed, rather than for a sampled batch, under the assumption that each training example is visited exactly once between checkpoints. This approach, however, may deviate significantly from the original TracIN-Ideal, with the final valuation results heavily dependent on the selected checkpoints.
Furthermore, []’s implementation is limited to the parameters of the last linear layer due to memory constraints, potentially biasing the measurement of data contribution. For instance, [] suggests that the last layer of a neural network might exhibit a strong "cancellation effect," where the data influence of different examples have large, contradictory magnitudes. Additionally, [] demonstrates that selecting different layers can distort data attribution scores. In contrast, this work introduces the "ghost dot-product" technique to efficiently compute the first-order In-Run Data Shapley (i.e., TracIN-Ideal) directly and accurately, without additional approximations.

## Appendix B Retraining-based vs In-Run Data Shapley

In this section, we provide additional discussion about the comparison between Retraining-based and In-Run Data Shapley. Figure 4 provides a visualized comparison between these two approaches.

Figure: Figure 4: Comparison between Retraining-based and In-Run Data Shapley. Retraining-based Data Shapley requires training a model from scratch on all possible subsets of the full training set, which is computationally inefficient and raises concerns about interpretability and stability. In contrast, In-Run Data Shapley acts as a "contribution accountant", efficiently tracking and attributing data value scores to each training example across gradient update steps during a single training run.
Refer to caption: extracted/5700225/images/flowchart-4.png

### B.1 When is In-Run Data Shapley Desirable for Data Attribution?

While Retraining-based Data Shapley has been widely adopted in the literature, it suffers from several critical issues that limit its practicality and effectiveness. In this section, we discuss these problems from three key aspects: computational efficiency, alignment with the purpose of data valuation, and stability of the results.

(1) Computational burden.
Retraining-based Data Shapley calculation is often computationally prohibitive, as it requires retraining the model on every possible subset of the original dataset, leading to a computational complexity that grows exponentially with the size of the dataset.
Despite the development of various Monte Carlo-based approximation algorithms [, , , , , , , , ], these methods still necessitate extensive computational resources due to repeated model retraining, which is clearly impractical for modern-sized ML models.
Another line of work attempts to use efficient proxy learning algorithms, such as $K$-nearest neighbors (KNN) [, , , , ], to accelerate Data Shapley computation. However, it remains unclear how closely these cheaper proxy models approximate the original learning algorithm, and it is also uncertain how to interpret the derived Data Shapley scores in this context.

(2) Inability to value data contribution to a specific model.
Crucially, Retraining-based Data Shapley is not designed to value data contribution towards a specific model. It attempts to quantify the average contribution of each training data point to models trained on different subsets of the data, rather than its contribution to the specific model trained on the full dataset. While one might interpret the former as an approximation of the latter, these two quantities can be quite different in practice, especially when the learning algorithm is randomized and sensitive to factors like random initialization and the order of the data points during training. More importantly, in most real-life scenarios, the primary interest lies in understanding the contribution of each data point to the specific model being trained and deployed.

(3) Unstable valuation results for stochastic training algorithms.
Furthermore, when the training algorithm involves randomness, such as in the case of SGD with random mini-batch selection, the corresponding utility function becomes a randomized function. Prior work [] suggests that this randomness can introduce substantial noise into the estimated Shapley values, rendering them unreliable and unstable. This instability poses significant challenges for interpreting and using the resulting data valuations, as the scores may vary considerably across different runs of the algorithm, even on the same dataset. Consequently, this limits the practical applicability of Retraining-based Data Shapley when working with stochastic training algorithms, which are prevalent in modern machine learning.

In-Run Data Shapley addresses all these issues, making it more desirable for specific scenarios. Firstly, it is computationally efficient as it computes data values during the training run, avoiding the need for multiple retraining iterations. This makes it feasible for modern large-scale ML models. Secondly, it aligns better with the purpose of data valuation by assessing the contribution of each data point to the specific model being trained, providing more relevant insights for real-world applications. Lastly, In-Run Data Shapley offers deterministic valuation results even with stochastic training algorithms, as it incorporates the specific sequence and randomness of the training process into the utility function. Therefore, for scenarios where computational efficiency, model-specific contributions, and result stability are critical, In-Run Data Shapley is a more suitable choice.

### B.2 When is Retraining-based Data Shapley Desirable for Data Attribution?

Retraining-based Data Shapley is still desirable in several scenarios. Firstly, it is more general and applicable to all learning algorithms, whereas In-Run Data Shapley is only applicable to iterative training algorithms. Secondly, retraining-based Shapley is useful when the goal is to understand the contribution of data points to the general learning process rather than to a specific model.
Thirdly, because Retraining-based Data Shapley does not require modifying the internal code of the training algorithm, its estimation algorithm, typically Monte Carlo-based, is straightforward and clean to implement.

## Appendix C Missing Proofs

###### Theorem 5 (Restate of Theorem 3 ) .

In-Run Data Shapley considering the first-order approximation has closed-form

$$ $\displaystyle\phi_{z}\left(U\right)\approx\sum_{t=0}^{T-1}\phi_{z}\left(U^{(t) }_{(1)}\right)$ $$

where

$$ $\displaystyle\phi_{z}\left(U^{(t)}_{(1)}\right)=-\eta_{t}\nabla\ell(w_{t},z^{( \mathrm{val})})\cdot\nabla\ell(w_{t},z),~{}~{}~{}t=0,\ldots,T-1$ $$

###### Proof.

For notation simplicity, let
$x_{j}:=\nabla\ell(w_{t},z_{j})$.
Given the utility function

$$ $\displaystyle U^{(t)}_{(1)}(S)=-\eta_{t}\sum_{z_{j}\in S}\nabla\ell(w_{t},z^{( \mathrm{val})})\cdot\nabla\ell(w_{t},z_{j}),$ $$

the marginal contribution of $z$ for any $S\subseteq\mathcal{B}_{t}\setminus z$ is

$$ $\displaystyle U^{(t)}_{(2)}(S\cup z)-U^{(t)}_{(2)}(S)=-\eta_{t}\nabla\ell(w_{t },z^{(\mathrm{val})})\cdot\nabla\ell(w_{t},z)$ $$

Plugging in the expression to the Shapley value’s definition immediately gives the result.
∎

###### Theorem 6 (Restate of Theorem 4 ) .

In-Run Data Shapley considering the second-order approximation has closed-form

$$ $\displaystyle\phi_{z}\left(U\right)\approx\sum_{t=0}^{T-1}\left(\phi_{z}\left( U^{(t)}_{(1)}\right)+\frac{1}{2}\phi_{z}\left(U^{(t)}_{(2)}\right)\right)$ (6) $$

where

$$ $\displaystyle\phi_{z}\left(U^{(t)}_{(1)}\right)+\frac{1}{2}\phi_{z}\left(U^{(t )}_{(2)}\right)=\underbrace{-\eta_{t}\nabla\ell(w_{t},z^{(\mathrm{val})})\cdot \nabla\ell(w_{t},z)}_{\text{ \leavevmode\hbox to8.9pt{\vbox to8.9pt{ \pgfpicture\makeatletter\hbox{\hskip 4.45195pt\lower-4.45195pt\hbox to0.0pt{ \pgfsys@beginscope\pgfsys@invoke{ }\definecolor{pgfstrokecolor}{rgb}{0,0,0} \pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ }\pgfsys@color@rgb@fill{0}{0} {0}\pgfsys@invoke{ }\pgfsys@setlinewidth{0.4pt}\pgfsys@invoke{ }\nullfont\hbox to 0.0pt{\pgfsys@beginscope\pgfsys@invoke{ }{ {{}}\hbox{\hbox{{\pgfsys@beginscope\pgfsys@invoke{ }{{}{{{}}}{{}}{}{}{}{}{}{}{ }{}{}{{}\pgfsys@moveto{4.25195pt}{0.0pt}\pgfsys@curveto{4.25195pt}{2.34831pt}{ 2.34831pt}{4.25195pt}{0.0pt}{4.25195pt}\pgfsys@curveto{-2.34831pt}{4.25195pt}{ -4.25195pt}{2.34831pt}{-4.25195pt}{0.0pt}\pgfsys@curveto{-4.25195pt}{-2.34831 pt}{-2.34831pt}{-4.25195pt}{0.0pt}{-4.25195pt}\pgfsys@curveto{2.34831pt}{-4.25 195pt}{4.25195pt}{-2.34831pt}{4.25195pt}{0.0pt}\pgfsys@closepath\pgfsys@moveto {0.0pt}{0.0pt}\pgfsys@stroke\pgfsys@invoke{ } }{{{{}}\pgfsys@beginscope\pgfsys@invoke{ }\pgfsys@transformcm{1.0}{0.0}{0.0}{1 .0}{-1.75pt}{-2.25555pt}\pgfsys@invoke{ }\hbox{{\definecolor{pgfstrokecolor}{ rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ } \pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ }\hbox{{1}} }}\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} } \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope{{{}}}{}{}\hss} \pgfsys@discardpath\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope\hss}} \lxSVG@closescope\endpgfpicture}} influence of }z\text{ on the loss of }z^{( \mathrm{val})}}+\underbrace{\frac{\eta_{t}^{2}}{2}\nabla\ell(w_{t},z)^{ \intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{t}\left(\sum_{z_{j}\in\mathcal{B} _{t}}\nabla\ell(w_{t},z_{j})\right)}_{\text{ \leavevmode\hbox to8.9pt{\vbox to 8.9pt{\pgfpicture\makeatletter\hbox{\hskip 4.45195pt\lower-4.45195pt\hbox to0. 0pt{\pgfsys@beginscope\pgfsys@invoke{ }\definecolor{pgfstrokecolor}{rgb}{0,0,0 }\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ }\pgfsys@color@rgb@fill{0}{0 }{0}\pgfsys@invoke{ }\pgfsys@setlinewidth{0.4pt}\pgfsys@invoke{ }\nullfont \hbox to0.0pt{\pgfsys@beginscope\pgfsys@invoke{ }{ {{}}\hbox{\hbox{{\pgfsys@beginscope\pgfsys@invoke{ }{{}{{{}}}{{}}{}{}{}{}{}{}{ }{}{}{{}\pgfsys@moveto{4.25195pt}{0.0pt}\pgfsys@curveto{4.25195pt}{2.34831pt}{ 2.34831pt}{4.25195pt}{0.0pt}{4.25195pt}\pgfsys@curveto{-2.34831pt}{4.25195pt}{ -4.25195pt}{2.34831pt}{-4.25195pt}{0.0pt}\pgfsys@curveto{-4.25195pt}{-2.34831 pt}{-2.34831pt}{-4.25195pt}{0.0pt}{-4.25195pt}\pgfsys@curveto{2.34831pt}{-4.25 195pt}{4.25195pt}{-2.34831pt}{4.25195pt}{0.0pt}\pgfsys@closepath\pgfsys@moveto {0.0pt}{0.0pt}\pgfsys@stroke\pgfsys@invoke{ } }{{{{}}\pgfsys@beginscope\pgfsys@invoke{ }\pgfsys@transformcm{1.0}{0.0}{0.0}{1 .0}{-1.75pt}{-2.25555pt}\pgfsys@invoke{ }\hbox{{\definecolor{pgfstrokecolor}{ rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ } \pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ }\hbox{{2}} }}\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} } \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope{{{}}}{}{}\hss} \pgfsys@discardpath\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope\hss}} \lxSVG@closescope\endpgfpicture}} interaction between }z\text{ and other training points}}$ (7) $$

for any $t=0,\ldots,T-1$.

###### Proof.

We show that

$$ $\displaystyle\phi_{z}\left(U^{(t)}_{(2)}\right)=\eta_{t}^{2}\nabla\ell(w_{t},z )\textbf{H}^{(z^{(\mathrm{val})})}_{t}\left(\sum_{z_{j}\in\mathcal{B}_{t}} \nabla\ell(w_{t},z_{j})\right).$ $$

For notation simplicity, let
$x_{j}:=\nabla\ell(w_{t},z_{j})$, and $\tilde{x}:=\nabla\ell(w_{t},z)$. Furthermore, let $n_{t}:=|\mathcal{B}_{t}|$ the batch size in $t$-th iteration.
For any $S\subseteq\mathcal{B}_{t}\setminus z$, the marginal contribution of $z$ is

$$ $\displaystyle U^{(t)}_{(2)}(S\cup z)-U^{(t)}_{(2)}(S)=\eta_{t}^{2}$ $\displaystyle\left(2\tilde{x}\textbf{H}^{(z^{(\mathrm{val})})}_{t}\left(\sum_{ z_{j}\in S}x_{j}\right)+\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})} _{t}\tilde{x}\right).$ $$

Plug the above expression into the Shapley value’s formula, we have

$$ $\displaystyle\phi_{z}\left(U^{(t)}_{(2)}\right)$ $\displaystyle=\eta_{t}^{2}\left(\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\tilde{x}+\frac{2}{n_{t}}\sum_{k=1}^{n_{t}}{n_{t}-1\choose k-1}^{- 1}\sum_{S\subseteq\mathcal{B}_{t}\setminus\{z\},~{}|S|=k-1}\left[\tilde{x}^{ \intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{t}\sum_{z_{j}\in S}x_{j}\right]\right)$ $\displaystyle=\eta_{t}^{2}\left(\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\tilde{x}+\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{ t}\left[\frac{2}{n_{t}}\sum_{k=1}^{n_{t}}{n_{t}-1\choose k-1}^{-1}\sum_{S \subseteq\mathcal{B}_{t}\setminus\{z\},~{}|S|=k-1}\left(\sum_{z_{j}\in S}x_{j} \right)\right]\right)$ $\displaystyle=\eta_{t}^{2}\left(\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\tilde{x}+\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{ t}\left[\frac{2}{n_{t}}\sum_{k=1}^{n_{t}}{n_{t}-1\choose k-1}^{-1}\sum_{S \subseteq\mathcal{B}_{t}\setminus\{z\},~{}|S|=k-1}\left(\sum_{z_{j}\in S}x_{j} \right)\right]\right)$ $\displaystyle=\eta_{t}^{2}\left(\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\tilde{x}+\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{ t}\left[\frac{2}{n_{t}}\sum_{k=1}^{n_{t}}{n_{t}-1\choose k-1}^{-1}{n_{t}-2 \choose k-2}\left(\sum_{z_{j}\in\mathcal{B}_{t}\setminus\{z\}}x_{j}\right) \right]\right)$ $\displaystyle=\eta_{t}^{2}\left(\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\tilde{x}+\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{ t}\left[\frac{2}{n_{t}}\sum_{k=1}^{n_{t}}\frac{k-1}{n_{t}-1}\left(\sum_{z_{j} \in\mathcal{B}_{t}\setminus\{z\}}x_{j}\right)\right]\right)$ $\displaystyle=\eta_{t}^{2}\left(\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\tilde{x}+\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{val})})}_{ t}\left(\sum_{z_{j}\in\mathcal{B}_{t}\setminus\{z\}}x_{j}\right)\right)$ $\displaystyle=\eta_{t}^{2}\left[\tilde{x}^{\intercal}\textbf{H}^{(z^{(\mathrm{ val})})}_{t}\left(\sum_{z_{j}\in\mathcal{B}_{t}}x_{j}\right)\right]$ $$

∎

###### Theorem 5 (Restate of Theorem 3 ) .

###### Proof.

###### Theorem 6 (Restate of Theorem 4 ) .

###### Proof.

## Appendix D "Ghost" Techniques

Notation review.
Consider a linear layer $\textbf{s}=\textbf{a}\textbf{W}$, where $\textbf{W}\in\mathbb{R}^{d_{1}\times d_{2}}$ is the weight matrix, $\textbf{a}=(\textbf{a}^{(1)},\ldots,\textbf{a}^{(B)})^{\intercal}$ is the mini-batch input, and $\textbf{s}=(\textbf{s}^{(1)},\ldots,\textbf{s}^{(B)})^{\intercal}$ is the output (i.e., the pre-activation tensor). For non-sequential data, $\textbf{a}\in\mathbb{R}^{B\times d_{1}},\textbf{s}\in\mathbb{R}^{B\times d_{2}}$.
For sequential data with sequence length $T$, $\textbf{a}\in\mathbb{R}^{B\times d_{1}\times T},\textbf{s}\in\mathbb{R}^{B
\times d_{2}\times T}$.
Let $\ell^{(i)}:=\ell(w,z_{i})$ denote the current model’s individual loss on $z_{i}$. For notation convenience, we denote
$\textbf{b}^{(i)}:=\frac{\partial\ell^{(i)}}{\partial\textbf{s}}$.

### D.1 Ghost Dot-Product

By applying the chain rule, we can express the gradient of an individual loss $\ell^{(i)}:=\ell(w,z_{i})$ with respect to W as

$$ $\displaystyle\frac{\partial\ell^{(i)}}{\partial\textbf{W}}=\frac{\partial\ell^ {(i)}}{\partial\textbf{s}^{(i)}}\frac{\partial\textbf{s}^{(i)}}{\partial \textbf{W}}=\frac{\partial\ell^{(i)}}{\partial\textbf{s}^{(i)}}\textbf{a}^{(i) }=\frac{\partial\ell}{\partial\textbf{s}^{(i)}}\textbf{a}^{(i)}=\textbf{a}^{(i )}\otimes\textbf{b}^{(i)}$ (8) $$

where $\ell:=\sum_{j=1}^{B}\ell^{(j)}$ is the aggregated loss. Note that the individual’s output gradient $\textbf{b}^{(i)}=\frac{\partial\ell^{(i)}}{\partial\textbf{s}^{(i)}}=\frac{
\partial\ell}{\partial\textbf{s}^{(i)}}$ is readily available during the backpropagation pass.

Suppose we are interested in computing the gradient dot-product $\frac{\partial\ell^{(1)}}{\partial\textbf{W}}\odot\frac{\partial\ell^{(2)}}{
\partial\textbf{W}}$ between two data points $z_{1},z_{2}$ in the same batch in the backpropagation. We first discuss the case for non-sequential data and then extend it to sequential data.

Non-sequential data.
For non-sequential data, we have each $\textbf{a}^{(i)}\in\mathbb{R}^{d_{1}\times 1}$ and $\textbf{b}^{(i)}\in\mathbb{R}^{1\times d_{2}}$.
By (8), we have

$$ $\displaystyle\frac{\partial\ell^{(1)}}{\partial\textbf{W}}\odot\frac{\partial \ell^{(2)}}{\partial\textbf{W}}=\left(\textbf{a}^{(1)}\otimes\textbf{b}^{(1)} \right)\odot\left(\textbf{a}^{(2)}\otimes\textbf{b}^{(2)}\right)=\left(\left( \textbf{b}^{(1)}\right)^{\intercal}\left(\textbf{b}^{(2)}\right)\right)\left( \left(\textbf{a}^{(1)}\right)^{\intercal}\textbf{a}^{(2)}\right)$ $$

Hence, we can compute the dot-product between $\frac{\partial\ell^{(1)}}{\partial\textbf{W}}$ and $\frac{\partial\ell^{(2)}}{\partial\textbf{W}}$ without actually instantiating the gradient vector $\frac{\partial\ell^{(1)}}{\partial\textbf{W}}$ or $\frac{\partial\ell^{(2)}}{\partial\textbf{W}}$.
We can take the dot products between $\left(\textbf{b}^{(1)}\right)^{\intercal}\left(\textbf{b}^{(2)}\right)$ and $\left(\textbf{a}^{(1)}\right)^{\intercal}\textbf{a}^{(2)}$, and then multiply the results together.
Moreover, all of the materials $\textbf{a}^{(1)},\textbf{a}^{(2)},\textbf{b}^{(1)},\textbf{b}^{(2)}$ that are required for computation are all already available in one backpropagation.
Hence, with a single backpropagation, we can efficiently compute the gradient dot-product between every pair of data points within the batch.

Sequential data.
For sequential data, we have each $\textbf{a}^{(i)}\in\mathbb{R}^{d_{1}\times T}$ and $\textbf{b}^{(i)}\in\mathbb{R}^{T\times d_{2}}$.
By (8), we have

$$ $\displaystyle\frac{\partial\ell^{(1)}}{\partial\textbf{W}}\odot\frac{\partial \ell^{(2)}}{\partial\textbf{W}}=\left(\textbf{a}^{(1)}\otimes\textbf{b}^{(1)} \right)\odot\left(\textbf{a}^{(2)}\otimes\textbf{b}^{(2)}\right)$ $\displaystyle=\sum_{j,k=1}^{d_{1},d_{2}}\left(\textbf{a}^{(1)}\otimes\textbf{b }^{(1)}\right)_{j,k}\left(\textbf{a}^{(2)}\otimes\textbf{b}^{(2)}\right)_{j,k}$ $\displaystyle=\sum_{j,k=1}^{d_{1},d_{2}}\left(\textbf{a}^{(1)}_{j}\textbf{b}^{ (1)}_{k}\right)\left(\textbf{a}^{(2)}_{j}\textbf{b}^{(2)}_{k}\right)$ $\displaystyle=\sum_{j,k=1}^{d_{1},d_{2}}\left(\sum_{t=1}^{T}\textbf{a}^{(1)}_{ jt}\textbf{b}^{(1)}_{tk}\right)\left(\sum_{t=1}^{T}\textbf{a}^{(2)}_{jt} \textbf{b}^{(2)}_{tk}\right)$ $\displaystyle=\sum_{t_{1},t_{2}=1}^{T,T}\left(\sum_{j=1}^{d_{1}}\textbf{a}^{(1 )}_{jt_{1}}\textbf{a}^{(2)}_{jt_{2}}\right)\left(\sum_{k=1}^{d_{2}}\textbf{a}^ {(1)}_{kt_{1}}\textbf{a}^{(2)}_{kt_{2}}\right)$ $\displaystyle=\sum_{t_{1},t_{2}=1}^{T,T}\left(\textbf{a}^{(1)}_{\cdot,t_{1}} \textbf{a}^{(2)}_{\cdot,t_{2}}\right)\left(\textbf{b}^{(1)}_{\cdot,t_{1}} \textbf{b}^{(2)}_{\cdot,t_{2}}\right)$ $\displaystyle=\left(\left(\textbf{b}^{(1)}\right)\left(\textbf{b}^{(2)}\right) ^{\intercal}\right)\odot\left(\left(\textbf{a}^{(1)}\right)^{\intercal}\textbf {a}^{(2)}\right)$ $$

Hence, comparing with directly computing per-sample gradients, if $2T^{2}<d_{1}d_{2}$, it is more memory-efficient to first multiply the matrices of $\left(\textbf{b}^{(1)}\right)\left(\textbf{b}^{(2)}\right)^{\intercal}$ and $\left(\textbf{a}^{(1)}\right)^{\intercal}\textbf{a}^{(2)}$, then take the inner product between the two $T\times T$ matrices. If $2T^{2}\geq d_{1}d_{2}$, then we can first take the outer products $\textbf{a}^{(1)}\otimes\textbf{b}^{(1)}$ and $\textbf{a}^{(2)}\otimes\textbf{b}^{(2)}$, then take their inner product. In either case, we only need a single backpropagation to compute the gradient dot product between every pair of data points within the batch, similar to the case of non-sequential data.

###### Remark 3 (Applications of "Ghost" Technique Beyond Data Attribution) .

The techniques developed in this work for efficiently computing gradient dot products may be of independent interest, as this operation arises in various machine learning algorithms and applications beyond data attribution. For instance, the "ghost dot-product" technique can be used in calculating the cosine similarity between individual gradients (together with the "ghost clipping" technique), which is frequently being employed to analyze training dynamics [] and detect adversarial attacks []. Additionally, gradient similarity is used in active learning to select the most informative data points for labeling [] and in multitask learning to balance the contributions of different tasks [].

###### Remark 3 (Applications of "Ghost" Technique Beyond Data Attribution) .

### D.2 Ghost Gradient-Hessian-Gradient Product

We denote $L$ as the number of layers in a neural network, and we denote $\textbf{W}_{i}$, $i=1,\ldots,L$ as the weight parameters of each layer. We denote $\textbf{H}^{(z^{(\mathrm{val})})}_{\textbf{W}_{i}}$ as the Hessian matrix of $\ell^{(z^{(\mathrm{val})})}$ on layer $\textbf{W}_{i}$. Suppose we are interested in computing the gradient dot product $\nabla\ell^{(1)}\textbf{H}^{(z^{(\mathrm{val})})}\nabla\ell^{(2)}$.

$$ $\displaystyle\nabla\ell^{(1)}\textbf{H}^{(z^{(\mathrm{val})})}\nabla\ell^{(2)}$ $\displaystyle=\sum_{i,j=1}^{L,L}\frac{\partial\ell^{(1)}}{\partial\textbf{W}_{ i}}\left(\frac{\partial^{(2)}\ell^{(z^{(\mathrm{val})})}}{\partial\textbf{W}_{ i}\partial\textbf{W}_{j}}\right)\frac{\partial\ell^{(2)}}{\partial\textbf{W}_{ j}}$ $\displaystyle=\sum_{i,j=1}^{L,L}(\textbf{a}^{(1)}_{i}\otimes\textbf{b}^{(1)}_{ i})\left(\frac{\partial^{(2)}\ell^{(z^{(\mathrm{val})})}}{\partial\textbf{W}_{ i}\partial\textbf{W}_{j}}\right)(\textbf{a}^{(2)}_{j}\otimes\textbf{b}^{(2)}_{ j})$ $$

We first look at the Hessian-vector product.

$$ $\displaystyle\left(\frac{\partial^{(2)}\ell^{(z^{(\mathrm{val})})}}{\partial \textbf{W}_{i}\partial\textbf{W}_{j}}\right)(\textbf{a}^{(2)}_{j}\otimes \textbf{b}^{(2)}_{j})$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left[\frac{\partial\ell^ {(z^{(\mathrm{val})})}}{\partial\textbf{W}_{j}}(\textbf{a}^{(2)}_{j}\otimes \textbf{b}^{(2)}_{j})\right]$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left[(\textbf{a}^{(z^{( \mathrm{val})})}_{j}\otimes\textbf{b}^{(z^{(\mathrm{val})})}_{j})(\textbf{a}^{ (2)}_{j}\otimes\textbf{b}^{(2)}_{j})\right]$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left[(\textbf{a}^{(z^{( \mathrm{val})})}_{j}\textbf{a}^{(2)}_{j})(\textbf{b}^{(z^{(\mathrm{val})})}_{j }\textbf{b}^{(2)}_{j})\right]$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left[\textbf{c}_{j} \textbf{d}_{j}\right]$ $\displaystyle=\textbf{c}_{j}\frac{\partial\textbf{d}_{j}}{\partial\textbf{W}_{ i}}+\textbf{d}_{j}\frac{\partial\textbf{c}_{j}}{\partial\textbf{W}_{i}}$ $$

where we denote $\textbf{c}_{j}:=\textbf{a}^{(z^{(\mathrm{val})})}_{j}\textbf{a}^{(2)}_{j}$ and $\textbf{d}_{j}:=\textbf{b}^{(z^{(\mathrm{val})})}_{j}\textbf{b}^{(2)}_{j}$.

We run the second backpropagation on the gradient dot product $\nabla\ell^{(z^{(\mathrm{val})})}\nabla\ell^{(2)}=\sum_{j=1}^{L}\textbf{c}_{j}
\textbf{d}_{j}$. Note that here $\textbf{a}^{(2)}_{j},\textbf{b}^{(2)}_{j}$ are constant vectors.
For $\frac{\partial\textbf{c}_{j}}{\partial\textbf{W}_{i}}$, we have the similar decomposition

$$ $\displaystyle\frac{\partial\textbf{c}_{j}}{\partial\textbf{W}_{i}}$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left(\textbf{a}^{(z^{( \mathrm{val})})}_{j}\textbf{a}^{(2)}_{j}\right)$ $\displaystyle=\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}} \left(\textbf{a}^{(z^{(\mathrm{val})})}_{j}\textbf{a}^{(2)}_{j}\right)\otimes \textbf{a}^{(z^{(\mathrm{val})})}_{i}$ $$

For $\frac{\partial\textbf{d}_{j}}{\partial\textbf{W}_{i}}$, the derivative is more complicated as the $\textbf{b}^{(z^{(\mathrm{val})})}_{j}$ also depends on the output from deeper layers.
When $j\geq i$, we still have

$$ $\displaystyle\frac{\partial\textbf{d}_{j}}{\partial\textbf{W}_{i}}=\frac{ \partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\left(\textbf{b}^{(z^{ (\mathrm{val})})}_{j}\textbf{b}^{(2)}_{j}\right)\otimes\textbf{a}^{(z^{( \mathrm{val})})}_{i}$ $$

When $j<i$, we have

$$ $\displaystyle\frac{\partial\textbf{d}_{j}}{\partial\textbf{W}_{i}}$ $\displaystyle=\frac{\partial\textbf{b}^{(z^{(\mathrm{val})})}_{j}\textbf{b}^{( 2)}_{j}}{\partial\textbf{W}_{i}}$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left(\textbf{b}^{(z^{( \mathrm{val})})}_{j}\textbf{b}^{(2)}_{j}\right)$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left(\frac{\partial\ell^ {(z^{(\mathrm{val})})}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\frac{ \partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}{\partial\textbf{a}^{(z^{( \mathrm{val})})}_{i}}\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i}}{ \partial\textbf{s}^{(z^{(\mathrm{val})})}_{i-1}}\ldots\frac{\partial\textbf{a} ^{(z^{(\mathrm{val})})}_{j}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}} \textbf{b}^{(2)}_{j}\right)$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}\left(\frac{\partial\ell^ {(z^{(\mathrm{val})})}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\textbf{ W}_{i}\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i}}{\partial\textbf{s}^ {(z^{(\mathrm{val})})}_{i-1}}\ldots\frac{\partial\textbf{a}^{(z^{(\mathrm{val} )})}_{j}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}\right)$ $\displaystyle=\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}} \left(\frac{\partial\ell^{(z^{(\mathrm{val})})}}{\partial\textbf{s}^{(z^{( \mathrm{val})})}_{i}}\textbf{W}_{i}\frac{\partial\textbf{a}^{(z^{(\mathrm{val} )})}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i-1}}\ldots\frac{\partial \textbf{a}^{(z^{(\mathrm{val})})}_{j}}{\partial\textbf{s}^{(z^{(\mathrm{val})} )}_{j}}\textbf{b}^{(2)}_{j}\right)\otimes\textbf{a}^{(z^{(\mathrm{val})})}_{i}$ $\displaystyle~{}~{}+\frac{\partial\ell^{(z^{(\mathrm{val})})}}{\partial\textbf {s}^{(z^{(\mathrm{val})})}_{i}}\otimes\left(\frac{\partial\textbf{a}^{(z^{( \mathrm{val})})}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i-1}}\ldots \frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{j}}{\partial\textbf{s}^{(z^{( \mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}\right)$ $\displaystyle=\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}( \textbf{b}^{(z^{(\mathrm{val})})}_{j}\textbf{b}^{(2)}_{j})\otimes\textbf{a}^{( z^{(\mathrm{val})})}_{i}+\textbf{b}^{(z^{(\mathrm{val})})}_{i}\otimes\left( \frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i}}{\partial\textbf{s}^{(z^{( \mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}\right)$ $$

Overall,

$$ $\displaystyle\frac{\partial\textbf{d}_{j}}{\partial\textbf{W}_{i}}$ $\displaystyle=\frac{\partial}{\partial\textbf{W}_{i}}(\textbf{b}^{(z^{(\mathrm {val})})}_{j}\textbf{b}^{(2)}_{j})$ $\displaystyle=\begin{cases}\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{ val})})}_{i}}\left(\textbf{b}^{(z^{(\mathrm{val})})}_{j}\textbf{b}^{(2)}_{j} \right)\otimes\textbf{a}^{(z^{(\mathrm{val})})}_{i}&j\geq i\\ \frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\left(\textbf{b} ^{(z^{(\mathrm{val})})}_{j}\textbf{b}^{(2)}_{j}\right)\otimes\textbf{a}^{(z^{( \mathrm{val})})}_{i}+\textbf{b}^{(z^{(\mathrm{val})})}_{i}\otimes\left(\frac{ \partial\textbf{a}^{(z^{(\mathrm{val})})}_{i}}{\partial\textbf{s}^{(z^{( \mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}\right)&j<i\\ \end{cases}$ $$

Hence, we have

$$ $\displaystyle\nabla\ell^{(1)}\textbf{H}^{(z^{(\mathrm{val})})}\nabla\ell^{(2)}$ $\displaystyle=\sum_{i,j=1}^{L,L}(\textbf{a}^{(1)}_{i}\otimes\textbf{b}^{(1)}_{ i})\left[\textbf{c}_{j}\frac{\partial\textbf{d}_{j}}{\partial\textbf{W}_{i}}+ \textbf{d}_{j}\frac{\partial\textbf{c}_{j}}{\partial\textbf{W}_{i}}\right]$ $\displaystyle=\underbrace{\sum_{i,j=1}^{L,L}(\textbf{a}^{(1)}_{i}\otimes \textbf{b}^{(1)}_{i})\left(\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{ val})})}_{i}}\left(\textbf{c}_{j}\textbf{d}_{j}\right)\otimes\textbf{a}^{(z^{( \mathrm{val})})}_{i}\right)}_{\leavevmode\hbox to8.9pt{\vbox to8.9pt{ \pgfpicture\makeatletter\hbox{\hskip 4.45195pt\lower-4.45195pt\hbox to0.0pt{ \pgfsys@beginscope\pgfsys@invoke{ }\definecolor{pgfstrokecolor}{rgb}{0,0,0} \pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ }\pgfsys@color@rgb@fill{0}{0} {0}\pgfsys@invoke{ }\pgfsys@setlinewidth{0.4pt}\pgfsys@invoke{ }\nullfont\hbox to 0.0pt{\pgfsys@beginscope\pgfsys@invoke{ }{ {{}}\hbox{\hbox{{\pgfsys@beginscope\pgfsys@invoke{ }{{}{{{}}}{{}}{}{}{}{}{}{}{ }{}{}{{}\pgfsys@moveto{4.25195pt}{0.0pt}\pgfsys@curveto{4.25195pt}{2.34831pt}{ 2.34831pt}{4.25195pt}{0.0pt}{4.25195pt}\pgfsys@curveto{-2.34831pt}{4.25195pt}{ -4.25195pt}{2.34831pt}{-4.25195pt}{0.0pt}\pgfsys@curveto{-4.25195pt}{-2.34831 pt}{-2.34831pt}{-4.25195pt}{0.0pt}{-4.25195pt}\pgfsys@curveto{2.34831pt}{-4.25 195pt}{4.25195pt}{-2.34831pt}{4.25195pt}{0.0pt}\pgfsys@closepath\pgfsys@moveto {0.0pt}{0.0pt}\pgfsys@stroke\pgfsys@invoke{ } }{{{{}}\pgfsys@beginscope\pgfsys@invoke{ }\pgfsys@transformcm{1.0}{0.0}{0.0}{1 .0}{-1.75pt}{-2.25555pt}\pgfsys@invoke{ }\hbox{{\definecolor{pgfstrokecolor}{ rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ } \pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ }\hbox{{1}} }}\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} } \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope{{{}}}{}{}\hss} \pgfsys@discardpath\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope\hss}} \lxSVG@closescope\endpgfpicture}}}+\underbrace{\sum_{i>j}^{L,L}(\textbf{a}^{(1 )}_{i}\otimes\textbf{b}^{(1)}_{i})\left(\textbf{b}^{(z^{(\mathrm{val})})}_{i} \otimes\left(\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i}}{\partial \textbf{s}^{(z^{(\mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}\right)\right)}_{ \leavevmode\hbox to8.9pt{\vbox to8.9pt{\pgfpicture\makeatletter\hbox{\hskip 4. 45195pt\lower-4.45195pt\hbox to0.0pt{\pgfsys@beginscope\pgfsys@invoke{ } \definecolor{pgfstrokecolor}{rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0} \pgfsys@invoke{ }\pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ } \pgfsys@setlinewidth{0.4pt}\pgfsys@invoke{ }\nullfont\hbox to0.0pt{ \pgfsys@beginscope\pgfsys@invoke{ }{ {{}}\hbox{\hbox{{\pgfsys@beginscope\pgfsys@invoke{ }{{}{{{}}}{{}}{}{}{}{}{}{}{ }{}{}{{}\pgfsys@moveto{4.25195pt}{0.0pt}\pgfsys@curveto{4.25195pt}{2.34831pt}{ 2.34831pt}{4.25195pt}{0.0pt}{4.25195pt}\pgfsys@curveto{-2.34831pt}{4.25195pt}{ -4.25195pt}{2.34831pt}{-4.25195pt}{0.0pt}\pgfsys@curveto{-4.25195pt}{-2.34831 pt}{-2.34831pt}{-4.25195pt}{0.0pt}{-4.25195pt}\pgfsys@curveto{2.34831pt}{-4.25 195pt}{4.25195pt}{-2.34831pt}{4.25195pt}{0.0pt}\pgfsys@closepath\pgfsys@moveto {0.0pt}{0.0pt}\pgfsys@stroke\pgfsys@invoke{ } }{{{{}}\pgfsys@beginscope\pgfsys@invoke{ }\pgfsys@transformcm{1.0}{0.0}{0.0}{1 .0}{-1.75pt}{-2.25555pt}\pgfsys@invoke{ }\hbox{{\definecolor{pgfstrokecolor}{ rgb}{0,0,0}\pgfsys@color@rgb@stroke{0}{0}{0}\pgfsys@invoke{ } \pgfsys@color@rgb@fill{0}{0}{0}\pgfsys@invoke{ }\hbox{{2}} }}\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope}}} } \pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope{{{}}}{}{}\hss} \pgfsys@discardpath\pgfsys@invoke{\lxSVG@closescope }\pgfsys@endscope\hss}} \lxSVG@closescope\endpgfpicture}}}$ $$

For the first part, we have

$$ 1 $\displaystyle=\sum_{i=1}^{L}(\textbf{a}^{(1)}_{i}\otimes\textbf{b}^{(1)}_{i}) \left(\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\left(\sum _{j=1}^{L}\textbf{c}_{j}\textbf{d}_{j}\right)\otimes\textbf{a}^{(z^{(\mathrm{ val})})}_{i}\right)$ $\displaystyle=\sum_{i=1}^{L}\left(\textbf{a}^{(1)}_{i}\textbf{a}^{(z^{(\mathrm {val})})}_{i}\right)\left(\textbf{b}^{(1)}_{i}\left(\frac{\partial}{\partial \textbf{s}^{(z^{(\mathrm{val})})}_{i}}\left(\sum_{j=1}^{L}\textbf{c}_{j} \textbf{d}_{j}\right)\right)\right)$ $$

where $\frac{\partial}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\left(\sum_{j=1}
^{L}\textbf{c}_{j}\textbf{d}_{j}\right)$, $i=1,\ldots,L$ is readily available from the second backpropagation.

For the second part, we have

$$ 2 $\displaystyle=\sum_{i>j}^{L,L}(\textbf{a}^{(1)}_{i}\otimes\textbf{b}^{(1)}_{i} )\left(\textbf{b}^{(z^{(\mathrm{val})})}_{i}\otimes\left(\frac{\partial\textbf {a}^{(z^{(\mathrm{val})})}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}} \textbf{b}^{(2)}_{j}\right)\right)$ $\displaystyle=\sum_{i>j}^{L,L}\left(\textbf{a}^{(1)}_{i}\left(\frac{\partial \textbf{a}^{(z^{(\mathrm{val})})}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})} )}_{j}}\textbf{b}^{(2)}_{j}\right)\right)\left(\textbf{b}^{(1)}_{i}\textbf{b}^ {(z^{(\mathrm{val})})}_{i}\right)$ $$

The remaining question is how to compute
$f(i,j):=\textbf{a}^{(1)}_{i}\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i
}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}$.
Observe that

$$ $\displaystyle\textbf{a}^{(1)}_{i}\frac{\partial\textbf{a}^{(z^{(\mathrm{val})} )}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}$ $\displaystyle=\textbf{a}^{(1)}_{i}\frac{\partial\textbf{a}^{(z^{(\mathrm{val}) })}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i-1}}\frac{\partial\textbf {s}^{(z^{(\mathrm{val})})}_{i-1}}{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i -1}}\ldots\frac{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j+1}}{\partial \textbf{a}^{(z^{(\mathrm{val})})}_{j+1}}\frac{\partial\textbf{a}^{(z^{(\mathrm {val})})}_{j+1}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}}\textbf{b}^{(2) }_{j}$ $\displaystyle=\textbf{a}^{(1)}_{i}\frac{\partial\textbf{a}^{(z^{(\mathrm{val}) })}_{i}}{\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i-1}}\textbf{W}_{i-1} \ldots\textbf{W}_{j+1}\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{j+1}}{ \partial\textbf{s}^{(z^{(\mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}$ $$

Moreover, $\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{j+1}}{\partial\textbf{s}^{(z^
{(\mathrm{val})})}_{j}}$ is easy to compute as the only operation between $\textbf{a}^{(z^{(\mathrm{val})})}_{j+1}$ and $\textbf{s}^{(z^{(\mathrm{val})})}_{j}$ is an element-wise activation function. For example, if the activation function is ReLU, then $\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{j+1}}{\partial\textbf{s}^{(z^
{(\mathrm{val})})}_{j}}$ will simply be a matrix that contains elements in $\{0,1\}$, depending on the sign of the position in $\textbf{s}^{(z^{(\mathrm{val})})}_{j}$.

To efficiently compute $f(i,j)$ for all $i>j$, for each $j=1,\ldots,L$, we can maintain the quantity

$$ $\mathbf{g}(i,j):=\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i}}{\partial \textbf{s}^{(z^{(\mathrm{val})})}_{i-1}}\textbf{W}_{i-1}\ldots\textbf{W}_{j+1} \frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{j+1}}{\partial\textbf{s}^{(z^ {(\mathrm{val})})}_{j}}\textbf{b}^{(2)}_{j}$ $$

and we have $f(i,j)=\textbf{a}^{(1)}_{i}\mathbf{g}(i,j)$.
To compute $f(i+1,j)$, note that
$\mathbf{g}(i+1,j)=\frac{\partial\textbf{a}^{(z^{(\mathrm{val})})}_{i+1}}{
\partial\textbf{s}^{(z^{(\mathrm{val})})}_{i}}\textbf{W}_{i}\mathbf{g}(i,j)$.
Moreover, we note that in our scenario, we are interested in the gradient dot-product

$$ $\displaystyle\nabla\ell^{(1)}\textbf{H}^{(z^{(\mathrm{val})})}\left(\sum_{m=1} ^{B}\nabla\ell^{(m)}\right)$ $$

where the Hessian-vector product $\textbf{H}^{(z^{(\mathrm{val})})}\left(\sum_{m=1}^{B}\nabla\ell^{(m)}\right)$ is fixed for any training point corresponds to $\ell^{(1)}$. Therefore, the second backpropagation only needs to be taken once as the gradient vector on the right-hand side is always fixed.

### D.3 Merging Batch Selection and Gradient Update in One Backpropagation

By utilizing the ghost Dot-Product technique developed in this paper, we can calculate or approximate all importance scores and correction terms in a single backpropagation pass, without materializing any model-sized vectors. To compute the gradient dot-product between each training point $z_{i}\in\mathcal{B}_{t}$ and the validation data $z^{(\mathrm{val})}$, we propose including $z^{(\mathrm{val})}$ in the backpropagation along with the training batch. Specifically, we can backpropagate with respect to $\left(\sum_{z_{i}\in\mathcal{B}_{t}}\ell^{(i)}\right)+\ell^{(z^{(\mathrm{val})
})}$.
After computing the In-Run Data Shapley scores for the current iteration, it may seem necessary to backpropagate with respect to $\sum_{z_{i}\in\mathcal{B}_{t}}\ell^{(i)}$ to compute the gradient for the parameter update. However, this is not required. We can simply reuse the output gradient $\frac{\partial\ell^{(i)}}{\partial\textbf{s}^{(i)}}$ from the original backpropagation and aggregate the gradients for all selected data points. This technique is adapted from the "book-keeping trick" proposed in [].

## Appendix E Evaluation

### E.1 Data Preprocessing & Training Settings & Implementation Details

We conduct the pretraining experiment using the GPT2 (124M) model [] on the Pile dataset. Our codebase is adapted from [https://github.com/karpathy/nanoGPT/tree/master](https://github.com/karpathy/nanoGPT/tree/master).
We first tokenize and split the entire dataset into chunks, and store them in the disk in numpy array format, which significantly speeds up data loading.
The corpus block size (i.e., maximum sequence length) is set to 1024.
The learning rate is set at a maximum of 0.0006, with a minimum learning rate of 0.00006.
We use AdamW as the optimizer with a weight decay of 0.1, and beta values set to 0.9 and 0.95. Gradients are clipped at a maximum value of 1.0 to maintain stability during training. The batch size is set to 16, with a learning rate warmup of 2000 iterations. Due to the shortage of computation resources, we stop the training at 500,000 iterations.

Methods for comparison.
In Section 5, we compare In-Run Data Shapley with the influence function [], which approximates the change in the model’s loss on the test example when the training example is removed from the training set (i.e., the leave-one-out score).
The influence is computed on the last linear layers in GPT2 model.
BM25 [] featurizes examples by their word frequency statistics to rank the training instances. We measure the similarity between the query examples and the training corpus, using the similarity scores as the value scores for the training corpus. we set the hyperparameters to $k_{1}=1.5,b=0.75$, which are commonly used values that balance the term frequency scaling and document length normalization.

###### Remark 4 .

We do not compare with TRAK [] as it generally requires aggregating the estimator across multiple trained models for a reasonable performance, and the original codebase is not applicable to Huggingface models. In their original paper, the largest scale experiment is fine-tuning BERT-base.

###### Remark 4 .

### E.2 Additional Experiment: Approximation Error From Taylor Approximation

In this experiment, we empirically investigate the errors of the first- and second-order approximations to $U^{(t)}$ on GPT2, as proposed in Section 4.1. Specifically, for a given batch $\mathcal{B}_{t}$, we uniformly sample 20 of its non-empty subsets $S$ and calculate the relative error rate $\left|U^{(t)}(S)-U^{(t)}_{(1)}(S)\right|/\left|U^{(t)}(S)\right|$ and $\left|U^{(t)}(S)-(U^{(t)}_{(1)}(S)+\frac{1}{2}U^{(t)}_{(2)}(S))\right|/\left|U
^{(t)}(S)\right|$.
In Figure 5, we plot the trimmed mean of the relative error rates (20% of outliers are removed to account for cases where $U^{(t)}(S)\approx 0$ which inflate the relative error rate).
As shown in Figure 5, the first-order approximation generally achieves a relative error of less than 5% and 10% for learning rates of $10^{-3}$ and $10^{-4}$, respectively. The second-order approximation generally achieves a relative error of less than 2% and 4% for learning rates of $10^{-3}$ and $10^{-4}$, respectively. This empirical analysis confirms that lower-order Taylor expansions are highly effective in approximating the local utility function $U^{(t)}$.

While the figures show that the relative error tends to increase with smaller learning rates, it is important to note that this behavior is due to the nature of the relative error calculation. As the learning rate decreases, the denominator (the change in loss) becomes much smaller, which can inflate the relative error. In theory, a smaller learning rate leads to better Taylor approximations, but this is challenging to demonstrate experimentally due to numerical errors.

###### Remark 5 (Difficulty in Extending to Adam) .

The techniques developed in this section are specifically tailored for SGD. It is not directly extendable to other popular optimizers like Adam due to their normalization terms. Nonetheless, using SGD as a proxy for Adam allows for efficient data attribution, which is the approach we adopted in practice and has proved to be effective in our experiment. This provides a practical and effective solution for the current scope of our work. Extending these techniques to support Adam and similar optimizers remains an exciting direction for future research.

Figure: Figure 5: Relative error rate comparison for first- and second-order Taylor approximation to the local utility function $U^{(t)}$ when the learning rate (LR) is in $\{10^{-3},10^{-4}\}$.
Refer to caption: extracted/5700225/images/utility_approx.png

###### Remark 5 (Difficulty in Extending to Adam) .

### E.3 Details about Section 5.2.1

In Section 5.2.1, we use GPT-4 to generate varying levels of paraphrased versions of an original corpus from the training set, categorized as "Partial exactly the same," "Paraphrase," "Significant paraphrase," and "Similar topic." For "Partial exactly the same," the corpus is used as the answer in a creative writing instruction-answer pair. The specific prompt for prompting GPT-4 with examples of the full dialogue are shown in Figure 7, 8, 9. We generate 3 sets of corpus groups, and the experiment results are taken as the average.
Figure 6 shows the distribution of calculated In-Run Data Shapley. As we can see, most corpora’s values are around 0, but certain corpora have significantly higher values than the other, which means their contributions are higher for the target validation corpus.

Figure: Figure 6: The distribution of calculated In-Run Data Shapley values.
Refer to caption: extracted/5700225/images/value_dist_histogram.png

Figure: Figure 7: Our dialogue with GPT4 for generating corpus of varying levels of similarity for the original corpus from Wikipedia.

Figure: Figure 8: Our dialogue with GPT4 for generating corpus of varying levels of similarity for the original corpus from a CNN news.

Figure: Figure 9: Our dialogue with GPT4 for generating corpus of varying levels of similarity for the original corpus from princeton.edu.

#### E.3.1 Additional Experiment: Relevant Domain Detection

In this additional experiment, we evaluate the effectiveness of different data attribution techniques in identifying relevant domain-specific corpora within the Pile dataset. Specifically, we take a random batch of the validation corpus from sub-domains of the Pile dataset and evaluate the *normalized recall@1000 scores*. This metric measures the proportion of same-domain corpora as the validation corpora among the 1000 highest-valued corpora, normalized by the global proportion of this domain within the Pile dataset.
As shown in Table 2, BM25 performs well on this task in general. Both the first-order and second-order In-Run Data Shapley methods outperform the influence function in detecting relevant domain corpora. Analysis: The influence function only uses information from the final trained models, which can result in highly noisy value scores since the removal of one training data point might have a negligible effect on the final model performance. In contrast, In-Run Data Shapley effectively leverages information from all intermediate checkpoints during model training, providing a more comprehensive and accurate assessment of each data point’s value.
While BM25 performs well on most of the domains, there are still cases where In-Run Data Shapley achieves better detection rate, which implies that gradient information may be more useful in determining semantic similarity to some extent.

**Table 2: Results of relevant domain corpus detection experiment, where the metric is the normalized recall@1000 (i.e., the proportion of the same domain corpora as the validation corpora among the 1000 corpora with the highest values, normalized by the global proportion of this domain among the Pile dataset).**
|  | Github | EuroParl | ArXiv | PhilPapers | FreeLaw | HackerNews | StackExchange | Wikipedia (en) |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| In-Run Data Shapley (1st order) | 30.5 | 12.7 | 48.4 | 0.6 | 14.6 | 2.7 | 16.3 | 14.5 |
| In-Run Data Shapley (2nd order) | 29 | 14.8 | 50 | 0.4 | 16.9 | 3.8 | 20 | 18.9 |
| Influence Function | 17.9 | 4.1 | 41.7 | 0.5 | 5.7 | 2.6 | 6.5 | 5.5 |
| \hdashlineBM25 | 18.8 | 6.3 | 44.8 | 1.1 | 72.6 | 9.9 | 37.2 | 18.6 |
|  | PubMed Abstracts | USPTO Backgrounds | PubMed Central | Enron Emails | NIH ExPorter | DM Mathematics | Ubuntu IRC | Gutenberg (PG-19) |
| In-Run Data Shapley (1st order) | 10.4 | 10.5 | 30 | 0.8 | 1.7 | 23.5 | 10.2 | 8.7 |
| In-Run Data Shapley (2nd order) | 16.6 | 11.1 | 31.1 | 1 | 1.9 | 24.5 | 11.5 | 15.3 |
| Influence Function | 10.2 | 7.4 | 26.9 | 0.2 | 1.2 | 10.1 | 4.3 | 8.8 |
| \hdashlineBM25 | 28.6 | 23 | 53.7 | 1 | 4.1 | 34.6 | 12.5 | 25.9 |

### E.4 Additional Results for Section 5.2.2

In addition to Figure 2, we also plot the average value scores of corpora from each domain, averaged across all corpora within the same domain that has been used in gradient updates, over the first 10,000 gradient update iterations. As training progresses, the magnitude of data value scores converges towards zero, which aligns with the diminishing returns property of neural network training. This indicates that the contribution of data points depends on the order in which they are used during training, an aspect that Retraining-based Data Shapley methods cannot capture. Data points introduced in later stages of training contribute less to model performance improvement.

Figure: Figure 10: The change of average contribution for data points from each domain to a corpus of math text (same setting as Figure 2).
Refer to caption: x1.png

### E.5 Details about Section 5.2.3

In this experiment, we first conduct one training run for 20,000 iterations. Among all the corpus that has been used in the training, we compute their In-Run Data Shapley and filter out all corpus among this subset that has negative contributions to model training. After filtering out the 16% negative valued corpus, we train another model on the remaining dataset for 10,000 iterations with all hyperparameters staying the same.

Despite the Pile dataset undergoing multiple layers of data curation [], we still find a significant amount of poor-quality corpus through In-Run Data Shapley. For example, we discovered a large amount of corpora in the PubMed Central domain that are simply empty spaces.
Figure 11 provides two examples of the corpus that received the lowest In-Run Data Shapley scores. Additionally, we found several corpora with negative values that appear normal, which is likely due to a distribution shift with the validation corpus. Crafting a representative validation set is an important direction for future work.

Figure: Figure 11: Examples of low-quality corpus in Pile found by In-Run Data Shapley. Left: a corpus with meaningless numbers. Right: a corpus with meaningless symbols (likely due to errors from web crawling.)
Refer to caption: extracted/5700225/images/luanma.png

## Peer Reviews

confidence: 3
contribution: 4
initial_rating: 10
presentation: 4
questions: I would like to know the authors' opinion of applying the general idea of In-Run Data Shapley to other use-cases in machine learning, such as hyperparameter importance during the HPO run. More generally, do the authors envision transferring this idea to other domains, especially in the case where gradient information is not available?
soundness: 4
strengths: The proposed method is novel, efficient and promising. The presentation of the paper is very clear and succinct; technical contributions are clearly stated and rigorously developed. The authors carefully position their work and acknowledge several limitations which they claim will be a part of future work. Empirical evaluation is thorough and the paper raises important observations re: societal implications of the large model training.
summary: This paper introduces In-Run Data Shapley, a novel principled way of attributing the data contribution for deep learning models within one training run. A classical Data Shapley technique requires retraining which is computationally prohibitive in practice for large models, and more importantly, it quantifies data contribution for a general learning algorithm, and not for a particular model at hand. Related to this, data contribution for a single model is of crucial importance for properly investigating fairness and copyright implications.

In-Run Data Shapley is powered by the fact that model training is performed iteratively, and Shapley values can be derived analytically for each model update step via gradient dot products or gradient-Hessian-gradient products between training and validation data. The authors propose an efficient method to approximate In-Run Data Shapley values during one training run via first- and second-order Taylor expansions, and compute these values in one and two backpropagation, respectively, inspired by the ghost clipping technique. This incurs negligible computational overhead, making In-Run Data Shapley highly usable in practice. Furthermore, the authors empirically evaluate their approach on Pile dataset, highlighting several important insights re: data curation and cleanliness of the dataset, dynamic data contribution throughout the entire training process, and the degree of data contribution in copyright issues.
weaknesses: I do not see any major weaknesses of the paper.
Minor (typo): line 500 - sentence "The results of this experiment..." appears twice in a row

---

confidence: 3
contribution: 3
initial_rating: 6
presentation: 3
questions: 1. Could you please provide an example on how your method used in-run data Shapley to calculate the data contribution? Is it possible to provide a figure? Even using a simple example data set would be very helpful. This cannot be replaced with a loss-time curve. 

2. Is it possible to extend the method to regression and classification problems?
soundness: 3
strengths: 1. The motivations, problem statement and objectives and contributions are provided clearly. 

2. The mathematical induction and proof seem solid.

3. The results and implications of the new concept/techniques are described clearly.
summary: This paper presents a new technique called "in-run data shapley" to eleminate model retraining for assessing data contributions. This seems a new angle to assess the data attribution and importance. The results shows that this can provide efficiency improvement, and help make more insights for data contribution. The paper also identifies and discussed potential implications for copyright in genAI and pretraining data curation.
weaknesses: Compared with the introduction, background, methods and mathematical induction, which are presented clearly, the results and analyses are a bit weak. It would be better to provide concrete examples with more deep analyses.

---

confidence: 3
contribution: 3
initial_rating: 8
presentation: 3
questions: * The method requires validation data to be available before training. Can the authors elaborate on the potential limitations of this requirement and discuss scenarios where this may not be feasible? Are there alternative approaches that could make In-Run Data Shapley applicable when validation data is not pre-available?
* How does the use of first- and second-order Taylor approximations affect the reliability of the Shapley values in practice, especially in highly non-convex or complex loss landscapes? Can the authors provide additional empirical results or theoretical analysis to quantify these approximation errors in diverse settings?
* While the paper demonstrates efficiency improvements, how does the proposed method scale when applied to models even larger than GPT-2 or to models with different architectures, such as transformer-based models with sparse attention mechanisms? Can the authors provide insights or preliminary results on this?
soundness: 3
strengths: * The introduction of In-Run Data Shapley is a significant advancement over existing retraining-based methods, offering a scalable solution to data attribution in large models.
* The case studies, particularly in data curation and copyright implications, highlight the real-world relevance of the proposed method.
* The closed-form derivations for the Shapley values using first- and second-order Taylor approximations are mathematically sound and well-explained.
summary: The paper presents a novel approach called In-Run Data Shapley that eliminates the need for model retraining to evaluate data contributions in machine learning. By leveraging gradient updates within a single training run, the authors develop a computationally efficient method suitable for large-scale models, such as foundation models like GPT-2. The work introduces efficient algorithms using Taylor expansions and techniques inspired by differential privacy to minimise runtime overhead. The study also demonstrates the practical utility of In-Run Data Shapley in data curation and understanding the impact of pretraining data.
weaknesses: * The method's requirement for validation data to be available before training might limit its applicability in scenarios where such data is not readily available.
* Even though the authors address memory concerns using gradient accumulation, the practicality of this approach in extremely large models with limited resources is not fully explored.
* The first- and second-order Taylor approximations are empirically validated, but there is limited discussion on how these approximations might affect the reliability of the data attribution scores, especially in more complex models.

---

confidence: 4
contribution: 4
initial_rating: 5
presentation: 3
questions: I want to confirm whether I have understood the definition of the in-run Shapley value for a data point $z$. It is the “cumulative contribution of the data point $z$ across all gradient update iterations within a single training run”. Does this mean that in each gradient update (e.g., for each batch) the Shapley data value w.r.t. to this batch is computed and averaged at the end? This would mean that for each batch $b$ the marginal contribution of $z$ would be estimated on all possible subsets of $b$. 

If so, I would be interested in an intuition how this value is related to the standard Shapley data value. This could be done conceptually but also by experimental evidence, e.g., by comparing the method to other visiting Shapley based estimates, as suggested above.
soundness: 3
strengths: The paper introduces an interesting and to me novel method to estimate data values in a single training run. This makes data valuation feasible to large scale data sets. Furthermore, the method is evaluated on such a data set.
summary: The paper introduces in-run Shapley, a data valuation method for SGD based models that estimates data values in one training run across different update steps. 

The authors further provide enhanced ways of computing these data values with first and second order approximations. 

The method is evaluated on the PILE dataset for GPT-2 and Pythia 4.10 against influence functions and outperforms them.

Remark: Thanks to the clarifications provided by the authors during rebuttal, I have raised Soundness from 2 to 3, Contribution from 3 to 4, and the overall score from 5 to 6.
weaknesses: The evaluation is performed against influence functions only. This way it is unclear whether the method only outperforms leave-one-out (approximated by influence functions) or is also better than the standard data Shapley value. 

Other data valuation/pruning methods are dismissed due to scalability issues (Sec 5.3.1). However, to my understanding, both Memorization (Socher et al., 2022, Feldman and Zhang (2020)) and Supervised Prototypes (Socher et al., 2022) should be applicable to larger models and have been implemented on ImageNet scale by Socher et al. I'm uncertain if this generalizes to LLMS, but it should be discussed if not feasible.

It is specifically indicated that TRAK wouldn't scale to LLMs, yet according to my understanding, TRAK can function with checkpoints from a limited number of models (see Appendix "E.3 Proxies for model ensembles in compute-constrained settings" of the TRAK paper).
Given the evaluation of in-run Shapley on large datasets, I would elevate my vote if the authors either compare their method to the ones mentioned above or provide reasons why those methods don't scale to the dataset.

Minor Remarks
--------------------
OpenDataVal is a recent benchmark for data valuation. Although it is designed for small-scale data, it offers ready-to-use implementations of various algorithms and could be employed to benchmark In-Run Shapley against other methods. This would enhance the paper by demonstrating whether In-Run Shapley performs comparably or even superior to the standard Shapley value.
Some references are missing:
* Toneva et al. (2019) demonstrated that data points learned early in training and not forgotten in later epochs are less important to model performance than points that are frequently forgotten. The former can be omitted to reduce dataset size.. This reminds of the method introduced in the paper. 
* Nguyen et al. (2023) proposed "A Bayesian Approach To Analysing Training Data Attribution In Deep Learning" to tackle the noise issue when training multiple models. This problem, referred to as "instability," is also mentioned in this paper but without citing Nguyen et al.

There are a few spelling mistakes especially in Sec. 5.3.3:
* In "data set," the word "set" is missing.
* The sentence "The results of this experiment have important implications for the current discussion on the copyright of generative AI." is repeated.

Sources
-----------
Remark: the reviewer is not the author of any of the papers below.
* Socher et. al (2022): Beyond neural scaling laws: beating power law scaling via data pruning
* Feldman and Zhang (2020): What neural networks memorize and why: Discovering the long tail via influence estimation
* Toneva et. al (2019): An Empirical Study of Example Forgetting during Deep Neural Network Learning
* OpenDataVal (2023): OpenDataVal: a Unified Benchmark for Data Valuation
* Nguyen et. al (2023): A Bayesian Approach To Analysing Training Data Attribution In Deep Learning

Meta-review: The paper presents an approach aimed to evaluate the contributions of the data samples to the model. 
This goal (attributing a relevance score to data samples w.r.t. the eventual model) is at the core of scientific, technical and legal concerns (privacy, copyright). 
The method is based on the accumulation of Shapley value at each gradient iteration, with affordable memory and computational extra requirements.  
One limitation is the availability at training time of validation samples.

Decision: Accept (Oral)