Where is the problem? Attributing LLM undesirable behavior via representation gradient tracingWhere Did It Go Wrong? Attributing Undesirable LLM Behaviors via Representation Gradient Tracing
RepT uses representation gradients to trace undesirable outputs back to fine-tuning data: Llama2-7B harmful identification auPRC is 1.000 (Table 2).
- Undesirable outputs need to be traced back to specific training data, otherwise it is difficult to diagnose the sources of harmful content, factual errors, and biases. Leave-one-out and Shapley require retraining; the authors state that parameter gradients are high-dimensional, diffuse, and have a semantic gap with specific knowledge.
- RepT caches hidden states and representation gradients at the phase transition layer where inter-layer similarity is lowest; if there is no unique lowest layer, it uses the last layer. The sample signature concatenates the representation of the last prompt token and the gradient of the first response token, with the influence score being cosine similarity. Token scores are obtained from normalized gradient inner products after a single backward pass.
- On harmful and backdoor tasks across three models, RepT's auPRC is 0.997 or 1.000 (Table 2). In knowledge poisoning, Llama3-8B's Canada→Korea is 0.932 (Table 3), and Ag→Na in the same table is 0.998.
- The authors state that experiments are on curated fine-tuning sets; exhaustive real-time attribution on multi-trillion-token pretraining corpora remains an engineering obstacle, and subtle biases aggregated from many data points require further study. The paper does not report the number of repetitions or the probe set size.
- Evaluated models
- Llama-2-7b-chat-hfQwen2.5-7B-InstructLlama-3-8B-InstructLlama2-70b
- Benchmarks
- Alpaca-cleanedLATAdvBenchHarmBench
- Metrics
- TSRPrecision@kauPRC
The paper proposes an attribution framework that diagnoses LLM undesirable behavior by analyzing model representations and their gradients. The method operates in the model's activation space, linking outputs to training data to avoid the noisy signals and high computational cost common to parameter-gradient methods. The authors systematically evaluate it on tasks such as tracing harmful content, detecting backdoor poisoning, and identifying knowledge contamination, claiming it supports both sample-level attribution and token-level fine-grained analysis, locating samples and phrases with causal influence on model behavior. Code has been open-sourced at https://github.com/plumprc/RepT.
Paper analysis
What problem does this paper attempt to solve?
The authors aim to trace training data behind undesirable LLM behavior, proposing RepT, which does attribution in representation space.
How to trace harmful content, factual errors, or biased outputs back to the responsible training samples and phrases.
- Occurrence Scenario: The authors state that LLMs have entered practical applications, but harmful content, factual inaccuracies, and social biases can undermine deployment. Low-quality fine-tuning or alignment data can harm performance or safety; to diagnose failures, prune contamination, and address biases at the source, one must trace specific outputs back to the training corpus.
- Existing Shortcomings: Leave-one-out validation and Shapley values require retraining with samples included or excluded, which the authors state is infeasible for modern LLMs. Influence functions approximate leave-one-out using parameter gradients ∂L/∂θ, but the authors state that this vector is high-dimensional, expensive to store, has low signal-to-noise ratio because the signal is spread across a huge parameter space, and lacks interpretable connections between individual weights and specific knowledge.
- Core Hypothesis: The authors reframe the problem from "how should all weights be adjusted" to "how should internal representations be corrected." Hidden states characterize what the input is, and their gradients indicate how representations should change to produce the target output; the authors state that representation space is less noisy and more abstract than parameter gradients.
- This Work: Propose Representation Gradient Tracing (RepT). Cache representations and representation gradients at a selected layer in activation space, perform sample-level document ranking and token-level phrase localization, and compare against gradient attribution baselines on harmful content tracing, backdoor poisoning detection, and knowledge pollution identification.
What related research exists?
Related work includes data valuation and influence functions; baselines are IF, DataInf, TracIn, RapidIn, LESS, LoGra.
Data Valuation
- Data Shapley (Ghorbani & Zou, 2019; Jia et al., 2019; Kwon & Zou, 2021): Uses Shapley values to quantify the influence of individual training samples on validation data. The paper states it has good theoretical properties but typically requires repeated retraining, which is prohibitive even for medium-sized models.
- Other Valuation Approaches (Yoon et al., 2020; Choe et al., 2023; Nohyun et al., 2022; Wu et al., 2022): Reinforcement learning, meta-learning, and training-free heuristics. The paper states these methods either require training additional reward models or have high computational demands, making them unsuitable for LLMs.
- Leave-One-Out Validation (Molinaro et al., 2005): The introduction cites it as a traditional approach that requires retraining when adding or removing samples, and states it is infeasible for modern LLMs.
Influence Functions
- Classical Form (Hampel, 1974; Ling, 1984; Koh & Liang, 2017): Uses gradients to approximate leave-one-out effects, avoiding retraining. Koh & Liang (2017), Guo et al. (2020), and Park et al. (2023) apply it to behavior explanation of traditional neural networks; the introduction also mentions Grosse et al. (2023) and others applying influence functions to LLMs.
- Fragility: Basu et al. (2020), Guo et al. (2020), Bae et al. (2022), and Li et al. (2024c) report that influence functions are fragile and numerically unstable in deep learning, and inaccurate on larger networks. The paper also states that iHVP and its dot products with all training samples pose scalability issues.
- LLM-Oriented Approximations: DataInf (Kwon et al., 2023), LESS (Xia et al., 2024), and LoGra (Choe et al., 2024) use LoRA to reduce parameter space; TracIn (Pruthi et al., 2020) and RapidIn (Lin et al., 2024) use first-order approximations to avoid iHVP. The paper states they still require storing and manipulating gradient vectors after training, with high computational and memory costs.
Baselines and Data
- Baseline Methods: Influence Function (IF), DataInf, TracIn and its layer-normalized variant TracIn (LN), RapidIn, LESS, and LoGra. Appendix A states that all except LoGra are reproduced under the same training conditions; LoGra uses the official implementation, which requires its specialized training framework to record gradients.
- Benchmarks/Datasets: Clean samples come from Alpaca-cleaned. Harmful samples come from LAT, which includes AdvBench, HarmBench, and their variants. Backdoor construction follows the BackdoorLLM setup. Knowledge pollution uses questions about Ag and Canada, with answer entities replaced by incorrect ones.
The authors distinguish this work from these prior works in attribution space: instead of storing parameter gradients or computing iHVP, they use representations and their gradients to connect outputs to training data, and perform both sample-level and token-level attribution.
How does the paper address this problem?
RepT caches representations and gradients at the phase transition layer, uses cosine similarity for sample attribution, and uses normalized gradient inner products for token attribution.
The overall approach moves attribution from parameter space to activation space: first select a layer, cache that layer's representations and representation gradients, then use signature similarity to find training documents, and perform token-level localization on high-impact documents. The framework is named Representation Gradient Tracing (RepT). The authors state that the idea is inspired by representation engineering works (Zou et al., 2023a; Li et al., 2024a; Zheng et al., 2024), which show that hidden states can be manipulated to control behavior.
Problem Formalization
- Object: Model fθ maps inputs to outputs, trained on D={zi=(xi,yi)}. Test sample ztest=(xtest,ybad) exhibits undesirable behavior, such as harmful content or factual errors.
- Goal: Learn an influence function I that maps this behavior to a ranked list of training samples; samples with greater influence on the target response receive higher scores. Real corpora lack known causal labels, so the authors construct Dtrain=Dclean∪Dpoison with |Dpoison| much smaller than |Dclean|, using poisoned samples as ground truth for attribution.
Representation Gradients and Layer Selection
- Definition: For an L-layer Transformer, the hidden state at layer ℓ is denoted H^(ℓ)(x). The representation gradient is the gradient of the loss with respect to this hidden state, gH(ℓ)(x,y)=∇H(ℓ)L(x,y). It indicates how the representation at that layer should change to reduce the loss. During instruction fine-tuning, the representation gradient for the prompt portion is typically zero, which the authors state they ignore.
- Computation: Treat H^(ℓ) as a terminal variable in the computational graph and use hooks for a single backward pass. The authors contrast three inadequacies of parameter gradients: high dimensionality, noisy signal spread across many weights, and a semantic gap between individual weights and behavior.
- Layer Selection: Use a small task probe set Dprobe to compare the similarity of adjacent-layer representations. The authors state that this curve is typically U-shaped or drops sharply at the last layer; they define the lowest point of the curve as the phase transition layer ℓ⋆. If there is no unique minimum, they designate the last layer because they state it ultimately dominates the output. The paper does not specify the size of Dprobe. Subsequently, H^(ℓ⋆) and corresponding representation gradients are cached for all training and test data.
Sample-Level Attribution
- Signature: h(z) concatenates two parts: the hidden state of the last prompt token and the representation gradient of the first response token. The authors assume the former is the most complete summary of the input context before generation begins, and the latter indicates how the representation must adjust to initiate the target output.
- Score: The influence of a training sample on a test sample is I(ztrain,ztest)=cos(h(ztrain),h(ztest)), i.e., the cosine similarity of the two signatures. Higher scores are interpreted by the authors as greater influence of that training sample on the test behavior, and they rank accordingly.
Token-Level Attribution
- Score: For identified high-impact samples, take the cached representation gradients, normalize by row, then compute inner products between the gradients of each token in the test response and the training sample gradients, summing to obtain a vector Itoken of length equal to the number of training tokens. It identifies which words in the document influence the test response.
- Overhead: The authors state that parameter-gradient methods require repeated gradient computations per token for token-level analysis; RepT obtains gH with one backward pass per sample, and token scores are directly obtained via inner products. Figure 1 depicts the pipeline as three steps: caching, sample-level signature similarity, and token-level gH·gH^T.
What experiments did the paper conduct?
The main experiments compare three models, three tasks, and six baselines; in Table 2, RepT's auPRC on Llama2-7B is 1.000.
Experimental Setup
- Models: Main experiments use Llama-2-7b-chat-hf, Qwen2.5-7B-Instruct, and Llama-3-8B-Instruct, all fine-tuned with LoRA. Section 5, Table 4 additionally tests Llama2-70b (LoRA) and Llama2-7b full-parameter. LoRA is applied to the query and value matrices of attention, r=4, α=32, dropout 0.1, batch size 8, trained for 3 epochs, with optimizer and learning rate scheduler using HuggingFace PEFT defaults. Full-parameter uses 16-bit, batch size 2. Hardware is a single Nvidia H100 96GB. The paper does not report random seeds or number of repetitions.
- Data: Clean samples come from Alpaca-cleaned. The harmful task uses 4,750 clean samples plus the first 250 harmful samples from LAT; TSR is computed on the held-out 1,000 harmful data points from LAT. Backdoor samples are drawn from Alpaca-cleaned with 5,000 samples and 5% poisoned, with trigger as the instruction prefix token TY, and the backdoor behavior is to rewrite the sentiment of the response; TSR is computed on another 1,000 triggered samples. Knowledge pollution uses 900 clean plus 100 incorrect facts, tested on 150 related questions and their variants; entity replacement is Ag→Na, Canada→Korea. Section 4.4 states that GPT-4o generates questions, while Appendix B states that the template in Figure 5 is used for GPT-4o mini.
- Baselines: IF, DataInf, TracIn, TracIn (LN), RapidIn, LESS, LoGra. RapidIn is randomly shuffled 20 times with projection dimension 65,536; LESS uses projection dimension 8,192 (Appendix A). All except LoGra are reproduced under the same training conditions.
- Metrics and Evaluation: TSR is the number of triggered samples divided by the number of tested samples. Attribution uses Precision@k (proportion of matches within the top k predictions) and top-k truncated auPRC; ground truth is the known poisoned samples. Harmful TSR uses a BERT-style classifier (Wang et al., 2024). The TSR evaluators for backdoor and knowledge pollution are not specified in the paper. The paper does not state over how many test samples P@k is averaged, nor does it perform statistical significance tests.
Main Results
The table below only lists RepT's P@100 and auPRC from Table 2. The column headers correspond to the three instruction models in the main text.
Swipe horizontally to read the table
Model Harmful P@100 Harmful auPRC Backdoor P@100 Backdoor auPRC Llama2-7B 0.996 1.000 0.999 1.000 Qwen2.5-7B 0.984 0.997 1.000 1.000 Llama3-8B 1.000 1.000 1.000 1.000 - Trigger Effectiveness: The authors state that fine-tuning on clean data has little impact on alignment. In Table 1, clean-set harmful TSR is 0.53%, 1.27%, 11.4%, and after poisoning it is 100%, 99.2%, 100%. Backdoor clean-set TSR is 0% for all, and after poisoning it is 99.2%, 99.4%, 99.5%.
- Authors' Interpretation and Exceptions in the Table: The authors state that RepT outperforms baselines in all settings, with auPRC and precision close to 100%. In Table 2, RepT's harmful task P@250 is 0.988, 0.966, 0.992, where Qwen2.5-7B is not 1.000. The next highest auPRC on the harmful task is LESS, at 0.591, 0.728, 0.642.
- Backdoor: The authors state that clean and poisoned samples are highly similar, and gradient methods perform poorly. On the backdoor task, LESS's auPRC is 0.087, 0.113, 0.048 (Table 2). The authors state that gradient norms are sensitive to generation length; TracIn with layer normalization improves slightly, RapidIn is worse, and they believe its internal random shuffling may destroy positional information.
Knowledge Pollution Attribution
- What was tested: 100 incorrect facts are mixed into 900 clean instructions, to see if attribution methods can rank test samples that repeat errors back to these erroneous training samples. Table 1 post-poisoning TSR: Ag→Na is 81.7%, 80.3%, 87.3%, Canada→Korea is 77.9%, 79.4%, 79.5%; clean fine-tuning counterparts are all 0%.
- Results: RepT's auPRC for Ag→Na is Llama2-7B 0.988, Qwen2.5-7B 0.992, Llama3-8B 0.998; for Canada→Korea it is 0.962, 0.939, 0.932 (Table 3). On Llama3-8B's Canada→Korea, P@10 is 0.904 and P@100 is 0.882, lower than other RepT cells in the same table.
- Authors' Interpretation: The authors state that most baselines struggle; IF performs better only at small k because the Hessian inverse is relatively stable at small data scales. For example, on Qwen2.5-7B's Ag→Na, IF's P@10 is 0.975, while RepT's is 0.986. The authors state that as topk increases, gradient baselines become unreliable, and RepT outperforms baselines on all models and both types of pollution.
Token-Level Case Study
- What was tested: Knowledge pollution cases are examined according to Equation (4). Figure 2's caption states that green boxes are test data, gray boxes are training data, and red are high-impact score tokens. The paper does not report numerical scores for this heatmap.
- Authors' Interpretation: The authors state that the signal is localized, and the erroneous entity token in the training data is the direct cause of the corresponding token in the test response, thus enabling targeted correction rather than whole-document deletion. Appendix B's Table 5 and Figure 5 provide data examples and synthetic templates; harmful outputs or template phrasings are not reproduced here.
Efficiency and Scale
- Llama2-7b LoRA (Table 4, harmful data): RepT is 14KB, 0.37h, P@100 0.999; LESS is 32KB, 0.56h, 0.851; TracIn (LN) is 8.0MB, 0.52h, 0.683; IF is 20.11h, P@100 0.108; DataInf is 10.28h, P@100 0.035. This P@100 differs from Table 2's 0.996, and the paper does not explain the discrepancy.
- Llama2-70b LoRA: IF and DataInf are OOM. RepT is 64KB, 4.97h, P@100 0.985; LESS is 32KB, 4.76h, 0.380; TracIn (LN) is 31.3MB, 5.14h, 0.283; RapidIn is 125KB, 10.78h, 0.023.
- Full-parameter Llama2-7b: IF, DataInf, and TracIn (LN) are OOM. RepT is 14KB, 0.43h, P@100 0.998; LESS is 32KB, 140h, 0.283; RapidIn is 125KB, 193h, 0.031. The authors state that LoGra was not analyzed because it requires retraining with a specialized framework.
Other Ablations and Analyses
- Components (Figure 3 left): The authors state that both H and gH provide information, and the combination outperforms either alone; pooling over all tokens degrades performance. Exact precision values for this figure are not given in the main text.
- Layer selection (Figure 3 middle): Harmful identification degrades only slightly at shallow layers; knowledge pollution performs better in regions with low inter-layer similarity; the authors state that the last layer is generally strong. The main table does not report the layer number used.
- Norm and length (Figure 3 right): The authors state that RepT is stable across different token lengths; parameter gradients have a long tail, with short sequences having larger norms, which may bias dot-product-based influence calculations.
- Similarity and compression (Figure 4): The authors state that both dot product and cosine are stable under default settings; Random Projection down to 2,048 dimensions maintains high accuracy, exact precision not reported; Random Shuffle degrades sharply.
- The authors believe that the role of Random Shuffle in prior work may stem from norm bias, since shuffling preserves norms but destroys positional structure; Random Projection can serve as compression.
What are some points that can be further explored?
The authors note that experiments are on curated fine-tuning sets, and large-scale pretraining attribution and subtle emergent biases remain to be studied.
Limitations and future directions noted by the authors
- Current experiments are on curated fine-tuning datasets (Appendix C).
- Applying to large-scale, multi-trillion-token pretraining corpora of foundation models, the authors say, remains a major engineering challenge (Appendix C).
- The compute and storage cost of computing and indexing representation gradients for each sample, the authors say, is manageable, but exhaustive real-time attribution over the entire training lifecycle remains an obstacle (Appendix C).
- Evaluation focuses on behaviors traceable to discrete data instances; effectiveness for more subtle emergent phenomena aggregated from many data points, such as subtle bias, needs further study (Appendix C).
- Future plans move from diagnosis to direct mitigation: systematically examine which layer's representations carry stronger signals for which attribution task; use the framework to identify and filter harmful data in training corpora; use representation gradients to construct steering vectors to modify activations at inference time, avoiding retraining (Appendix C).
Experimental coverage
- Main experiments use Llama-2-7b-chat-hf, Qwen2.5-7B-Instruct, Llama-3-8B-Instruct as tested models; Table 4 also includes Llama2-70b (LoRA) and Llama2-7b full fine-tuning.
- Tasks are harmful data recognition, backdoor detection triggered by prefix tokens, and two types of knowledge poisoning, Ag→Na and Canada→Korea (Section 4).
- Attribution baselines are IF, DataInf, TracIn, TracIn (LN), RapidIn, LESS, LoGra; metrics are TSR, Precision@k, auPRC (Tables 1–3).
- Harmful TSR is computed by a BERT-style classifier on 1,000 held-out data points. The paper does not state how TSR is determined for backdoor and knowledge poisoning.
- The paper does not report the number of repetitions, the number of test items averaged for P@k, the size of the probe set Dprobe, the layer numbers chosen for the main tables, or the aggregation metric for token-level scores.
Summarize the main content of the paper
RepT uses representation gradients to trace the training sources of undesirable behavior, achieving higher auPRC than the listed gradient baselines across three controlled tasks.
RepT is a data attribution method: when a model exhibits harmful responses, backdoor behavior, or factual errors, it ranks the output back to documents and tokens in the fine-tuning data in representation space rather than parameter space.
The authors aim to address "which training data is at fault." Leave-one-out and Shapley require retraining; parameter-gradient influence functions have high dimensionality, and the authors state that the signal is diffuse and individual weights lack interpretable connections to specific knowledge. RepT caches hidden states and representation gradients at the lowest point of the adjacent-layer similarity curve (or the last layer if there is no unique minimum). The sample signature concatenates the representation of the last prompt token and the gradient of the first response token, with the influence score being cosine similarity. Token scores are obtained from row-normalized gradient inner products after a single backward pass.
Main experiments use Llama-2-7b-chat-hf, Qwen2.5-7B-Instruct, and Llama-3-8B-Instruct with LoRA fine-tuning, clean data from Alpaca-cleaned, and compare against IF, DataInf, TracIn, RapidIn, LESS, and LoGra.
- Harmful identification: post-poisoning TSR is 100%, 99.2%, 100% (Table 1). RepT's auPRC is 1.000, 0.997, 1.000 (Table 2).
- Backdoor detection: RepT's auPRC is 1.000 on all three models; LESS is 0.087, 0.113, 0.048 (Table 2).
- Knowledge pollution: RepT's auPRC is 0.988, 0.992, 0.998 for Ag→Na, and 0.962, 0.939, 0.932 for Canada→Korea (Table 3).
- Overhead: On Llama2-7b LoRA, RepT is 14KB, 0.37h, P@100 0.999; in the same table, IF is 20.11h, P@100 0.108 (Table 4). On Llama2-70b, IF and DataInf are OOM, and RepT's P@100 is 0.985.
The authors conclude that representation gradients connect outputs back to training data and can be used to audit the data sources of undesirable behavior. Appendix C states that existing results come from curated fine-tuning sets, and exhaustive real-time attribution on multi-trillion-token pretraining corpora, as well as subtle biases aggregated from many samples, remain future work; the authors plan to use it for data filtering and inference-time correction.