Jianhui Chen1* Yuzhang Luo1* Liangming Pan12†
1State Key Laboratory of Multimedia Information Processing, School of Computer Science, Peking University
2Beijing Academy of Artificial Intelligence, Beijing, China
*Equal contribution. †Corresponding author.
Understanding how Large Language Models (LLMs) develop their capabilities remains a fundamental challenge in AI alignment and development. While Mechanistic Interpretability has successfully reverse-engineered "interpretable units" (such as induction heads) inside trained models, most existing work relies on post-hoc analysis—revealing what is inside the model, but not why or how it emerged from the training pipeline.
We introduces Mechanistic Data Attribution (MDA) to bridge the critical gap between Mechanistic Interpretability and Data Attribution. Our framework is driven by four core motivations:
To get started, follow these steps to set up your development environment and dataset. We recommend using conda for dependency management.
Create and Activate Conda Environment:
conda create -n mda python=3.10
conda activate mda
Install Required Packages:
First, ensure your pip and setuptools are up to date. Then, install torch and the dependencies listed in requirements.txt.
pip install -r requirements.txt
We use Pythia pretrained dataset, you can get the tokenized dataset following the instructions in Exploring the Dataset.
Note: We only use the first 2000 steps samples in the experiments.
The project is driven by highly readable YAML configuration files. Here is an example of the configuration structure:
# Dataset configuration
data:
npy_path: "path/to/indicies.npy"
num_train_samples: 819200
seq_length: 2048
batch_size_ekfac: 16
batch_size_influence: 1
num_workers: 2
# Target Head Configuration
target:
layer: 5
head: 10
mode: "qkvo" # Choose between: "qk" or "qkvo"
# Probe Settings
probe:
type: "copy_target_synthetic" # "copy_target_synthetic" | "copy_target_dataset" | "prev_attn"
num_samples: 32
induction_match: "current"
match_choice: "last"
# Output Settings
output:
dir: "path/to/output/dir"
top_k: 1536
Ensure you have your environment set up with PyTorch and TransformerLens.
Run the influence scoring pipeline on a single device:
python scripts/run_influence.py --config config/prev_attn.yaml
Leverage torchrun for faster computation across multiple GPUs (e.g., 4 GPUs):
torchrun --nproc_per_node=4 scripts/run_influence.py --config config/copy_target.yaml
After successful execution, the script generates two NumPy arrays saved directly to your specified output.dir.
| File Name | Shape | Description |
|---|---|---|
📈 top_pos.npy | [top_k, 3] | Most Positively Influential Samples. Contains (score, index, loss) for each sample. |
📉 top_neg.npy | [top_k, 3] | Most Negatively Influential Samples. Contains (score, index, loss) for each sample. |
Your can inspect the most influential samples with the following script.
python utils/bath_view_influence.py \
--result-json path/to/top_pos.npy \
--data-npy path/to/indicies.npy \
--tokenizer path/to/checkpoint \
--output-json path/to/output \
--topk 200
If you find our work or this code useful in your research, please consider citing:
@article{chen2026mechanistic,
title={Mechanistic data attribution: Tracing the training origins of interpretable llm units},
author={Chen, Jianhui and Luo, Yuzhang and Pan, Liangming},
journal={arXiv preprint arXiv:2601.21996},
year={2026}
}
2 commits
Python
100.0%
Jianhui Chen1* Yuzhang Luo1* Liangming Pan12†
1State Key Laboratory of Multimedia Information Processing, School of Computer Science, Peking University
2Beijing Academy of Artificial Intelligence, Beijing, China
*Equal contribution. †Corresponding author.
Understanding how Large Language Models (LLMs) develop their capabilities remains a fundamental challenge in AI alignment and development. While Mechanistic Interpretability has successfully reverse-engineered "interpretable units" (such as induction heads) inside trained models, most existing work relies on post-hoc analysis—revealing what is inside the model, but not why or how it emerged from the training pipeline.
We introduces Mechanistic Data Attribution (MDA) to bridge the critical gap between Mechanistic Interpretability and Data Attribution. Our framework is driven by four core motivations:
To get started, follow these steps to set up your development environment and dataset. We recommend using conda for dependency management.
Create and Activate Conda Environment:
conda create -n mda python=3.10
conda activate mda
Install Required Packages:
First, ensure your pip and setuptools are up to date. Then, install torch and the dependencies listed in requirements.txt.
pip install -r requirements.txt
We use Pythia pretrained dataset, you can get the tokenized dataset following the instructions in Exploring the Dataset.
Note: We only use the first 2000 steps samples in the experiments.
The project is driven by highly readable YAML configuration files. Here is an example of the configuration structure:
# Dataset configuration
data:
npy_path: "path/to/indicies.npy"
num_train_samples: 819200
seq_length: 2048
batch_size_ekfac: 16
batch_size_influence: 1
num_workers: 2
# Target Head Configuration
target:
layer: 5
head: 10
mode: "qkvo" # Choose between: "qk" or "qkvo"
# Probe Settings
probe:
type: "copy_target_synthetic" # "copy_target_synthetic" | "copy_target_dataset" | "prev_attn"
num_samples: 32
induction_match: "current"
match_choice: "last"
# Output Settings
output:
dir: "path/to/output/dir"
top_k: 1536
Ensure you have your environment set up with PyTorch and TransformerLens.
Run the influence scoring pipeline on a single device:
python scripts/run_influence.py --config config/prev_attn.yaml
Leverage torchrun for faster computation across multiple GPUs (e.g., 4 GPUs):
torchrun --nproc_per_node=4 scripts/run_influence.py --config config/copy_target.yaml
After successful execution, the script generates two NumPy arrays saved directly to your specified output.dir.
| File Name | Shape | Description |
|---|---|---|
📈 top_pos.npy | [top_k, 3] | Most Positively Influential Samples. Contains (score, index, loss) for each sample. |
📉 top_neg.npy | [top_k, 3] | Most Negatively Influential Samples. Contains (score, index, loss) for each sample. |
Your can inspect the most influential samples with the following script.
python utils/bath_view_influence.py \
--result-json path/to/top_pos.npy \
--data-npy path/to/indicies.npy \
--tokenizer path/to/checkpoint \
--output-json path/to/output \
--topk 200
If you find our work or this code useful in your research, please consider citing:
@article{chen2026mechanistic,
title={Mechanistic data attribution: Tracing the training origins of interpretable llm units},
author={Chen, Jianhui and Luo, Yuzhang and Pan, Liangming},
journal={arXiv preprint arXiv:2601.21996},
year={2026}
}
2 commits
Python
100.0%