Metadata-Version: 2.4
Name: quanda
Version: 0.5.0
Summary: Toolkit for quantitative evaluation of data attribution methods in PyTorch.
Author-email: Dilyara Bareeva <dilyabareeva@gmail.com>, Galip Ümit Yolcu <galip.uemit.yolcu@hhi.fraunhofer.de>
License: MIT License
        
        Copyright (c) 2026 Dilyara Bareeva, Galip Ümit Yolcu, Anna Hedström, Niklas Schmolenski, Thomas Wiegand, Wojciech Samek, Sebastian Lapuschkin
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/dilyabareeva/quanda
Project-URL: Issues, https://github.com/dilyabareeva/quanda/issues
Keywords: interpretability,xai,machine learning,deep learning
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: <3.13,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.19.5
Requires-Dist: torch>=2.6.0
Requires-Dist: lightning>=1.4.0
Requires-Dist: torchmetrics>=1.4.0
Requires-Dist: tqdm>=4.0.0
Requires-Dist: traker>=0.3.2
Requires-Dist: annoy>=1.17.0
Requires-Dist: datasets>=4.8.4
Requires-Dist: torcheval>=0.0.6
Requires-Dist: torchvision>=0.10.0
Requires-Dist: kronfluence>=1.0.1
Requires-Dist: transformers>=4.44.0
Requires-Dist: PyYAML>=5.0.0
Requires-Dist: captum<0.9,>=0.8.0
Requires-Dist: Pillow>=10.0.0
Requires-Dist: pyarrow>=18.0.0
Requires-Dist: dattri>=0.3.0
Requires-Dist: tiktoken>=0.10.0
Provides-Extra: dev
Requires-Dist: coverage>=7.2.3; extra == "dev"
Requires-Dist: pytest<=7.4.4; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: pytest-mock==3.10.0; extra == "dev"
Requires-Dist: pre-commit>=3.2.0; extra == "dev"
Requires-Dist: mypy==1.16.1; extra == "dev"
Requires-Dist: types-tqdm>=4.0.0; extra == "dev"
Requires-Dist: furo; extra == "dev"
Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "dev"
Requires-Dist: sphinx>=7.0.0; extra == "dev"
Requires-Dist: numpydoc; extra == "dev"
Requires-Dist: tensorboard; extra == "dev"
Requires-Dist: wandb>=0.12.10; extra == "dev"
Requires-Dist: torchvision>=0.10.0; extra == "dev"
Requires-Dist: types-Pillow>=8.3.0; extra == "dev"
Requires-Dist: ruff>=0.7.0; extra == "dev"
Requires-Dist: types-PyYAML>=5.0.0; extra == "dev"
Requires-Dist: hydra-core>=1.1.0; extra == "dev"
Requires-Dist: hydra-optuna-sweeper>=1.2.0; extra == "dev"
Requires-Dist: hydra-joblib-launcher>=1.2.0; extra == "dev"
Requires-Dist: optuna<3.0.0,>=2.10.0; extra == "dev"
Provides-Extra: tutorials
Requires-Dist: torchvision>=0.10.0; extra == "tutorials"
Requires-Dist: matplotlib>=3.4.0; extra == "tutorials"
Requires-Dist: pillow>=8.3.0; extra == "tutorials"
Requires-Dist: python-dotenv>=1.0.0; extra == "tutorials"
Requires-Dist: nltk>=3.6.0; extra == "tutorials"
Requires-Dist: wandb>=0.12.10; extra == "tutorials"
Dynamic: license-file

<p align="center">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/dilyabareeva/quanda/refs/heads/main/assets/readme/quanda_panda_black_bg.png">
  <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/dilyabareeva/quanda/refs/heads/main/assets/readme/quanda_panda_no_bg.png">
  <img width="400" alt="quanda" src="https://raw.githubusercontent.com/dilyabareeva/quanda/refs/heads/main/assets/readme/quanda_panda_day_n_night.png">
</picture>
</p>

<p align="center">
  Interpretability toolkit for <b>quan</b>titative evaluation of <b>d</b>ata <b>a</b>ttribution methods in <b>PyTorch</b>.
</p>


![py_versions](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-3A76A8)
![mypy](https://img.shields.io/badge/mypy-checked-7EAF6E)
![ruff](https://img.shields.io/badge/ruff-checked-7D53BA)
[![codecov](https://codecov.io/gh/dilyabareeva/quanda/graph/badge.svg?token=6SZS1VISQF)](https://codecov.io/gh/dilyabareeva/quanda)
![PyPI - License](https://img.shields.io/pypi/l/quanda?color=A20E0C)
[![arXiv](https://img.shields.io/badge/arXiv-2410.07158-b31b1b.svg)](https://arxiv.org/abs/2410.07158)

**quanda** _quanda is under active development. Note the release version to ensure reproducibility of your work. Contributions, bug reports, and feature requests are welcome._


[📑 Shortcut to paper!](https://arxiv.org/pdf/2410.07158)

## 🐼 Library overview
**Training data attribution** (TDA) methods attribute model output on a specific test sample to the training dataset that it was trained on. They reveal the training datapoints responsible for the model's decisions. Existing methods achieve this by estimating the counterfactual effect of removing datapoints from the training set ([Koh and Liang, 2017](https://proceedings.mlr.press/v70/koh17a.html); [Park et al., 2023](https://proceedings.mlr.press/v202/park23c.html); [Bae et al., 2024](https://arxiv.org/abs/2405.12186)) tracking the contributions of training points to the loss reduction throughout training ([Pruthi et al., 2020](https://proceedings.neurips.cc/paper/2020/hash/e6385d39ec9394f2f3a354d9d2b88eec-Abstract.html)), using interpretable surrogate models ([Yeh et al., 2018](https://proceedings.neurips.cc/paper/2018/hash/8a7129b8f3edd95b7d969dfc2c8e9d9d-Abstract.html)) or finding training samples that are deemed similar to the test sample by the model ([Caruana et. al, 1999](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2232607/); [Hanawa et. al, 2021](https://openreview.net/forum?id=9uvhpyQwzM_)). In addition to model understanding, TDA has been used in a variety of applications such as debugging model behavior ([Koh and Liang, 2017](https://proceedings.mlr.press/v70/koh17a.html); [Yeh et al., 2018](https://proceedings.neurips.cc/paper/2018/hash/8a7129b8f3edd95b7d969dfc2c8e9d9d-Abstract.html); [K and Søgaard, 2021](https://arxiv.org/abs/2111.04683); [Guo et al., 2021](https://aclanthology.org/2021.emnlp-main.808)), data summarization ([Khanna et al., 2019](https://proceedings.mlr.press/v89/khanna19a.html); [Marion et al., 2023](https://openreview.net/forum?id=XUIYn3jo5T); [Yang et al., 2023](https://openreview.net/forum?id=4wZiAXD29TQ)), dataset selection ([Engstrom et al., 2024](https://openreview.net/forum?id=GC8HkKeH8s); [Chhabra et al., 2024](https://openreview.net/forum?id=HE9eUQlAvo)), fact tracing ([Akyurek et al., 2022](https://aclanthology.org/2022.findings-emnlp.180)) and machine unlearning ([Warnecke
et al., 2023](https://arxiv.org/abs/2108.11577)).

Although there are various demonstrations of TDA’s potential for interpretability and practical applications, the critical question of how TDA methods should be effectively evaluated remains open. Several approaches have been proposed by the community, which can be categorized into three groups:

<details>
  <summary><b><big>Ground Truth</big></b></summary>As some of the methods are designed to approximate LOO effects, ground truth can often be computed for TDA evaluation. However, this counterfactual ground truth approach requires retraining the model multiple times on different subsets of the training data, which quickly becomes computationally expensive. Additionally, this ground truth is shown to be dominated by noise in practical deep learning settings, due to the inherent stochasticity of a typical training process
    (<a href="https://openreview.net/forum?id=xHKVVHGDOEk" target="_blank">Basu et al., 2021</a>;
    <a href="https://proceedings.neurips.cc/paper_files/paper/2023/hash/ca774047bc3b46cc81e53ead34cd5d5a-Abstract-Conference.html" target="_blank">Nguyen et al., 2023</a>).
</details>

<details>
<summary><b><big>Downstream Task Evaluators</big></b></summary>To remedy the challenges associated with ground truth evaluation, the literature proposes to assess the utility of a TDA method within the context of an end-task, such as model debugging or data selection (<a href="https://proceedings.mlr.press/v70/koh17a.html" target="_blank">Koh and Liang, 2017</a>;
    <a href="https://proceedings.mlr.press/v89/khanna19a.html" target="_blank">Khanna et al., 2019</a>;
    <a href="https://arxiv.org/abs/2111.04683" target="_blank">Karthikeyan et al., 2021</a>).
</details>

<details>
  <summary><b><big>Heuristics</big></b></summary>Finally, the community also used heuristics (desirable properties or sanity checks) to evaluate the quality of TDA techniques. These include comparing the attributions of a trained model and a randomized model (<a href="https://openreview.net/forum?id=9uvhpyQwzM_" target="_blank">Hanawa et al., 2021</a>) and measuring the amount of overlap between the attributions for different test samples
    (<a href="http://proceedings.mlr.press/v108/barshan20a/barshan20a.pdf" target="_blank">Barshan et al., 2020</a>).
</details>
<br>
<b>quanda</b> is designed to meet the need of a comprehensive and systematic evaluation framework, allowing practitioners and researchers to obtain a detailed view of the performance of TDA methods in various contexts.

### Library Features

- **Unified TDA Interface**: **quanda** provides a unified interface for various TDA methods, allowing users to easily switch between different methods.
- **Metrics**: **quanda** provides a set of metrics to evaluate the effectiveness of TDA methods. These metrics are based on the latest research in the field.
- **Benchmarking**: **quanda** provides a benchmarking tool to evaluate the performance of TDA methods on a given model, dataset and problem. As many TDA evaluation methods require access to ground truth, our benchmarking tools allow to generate a controlled setting with ground truth, and then compare the performance of different TDA methods on this setting.

### Supported TDA Libraries

| Library                                                                                       | Reference                                                                                                                                                                                                                                                                                                                          |
|-----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Captum](https://github.com/pytorch/captum/tree/master) (Similarity Influence, Arnoldi Influence Function, TracIn) | [Caruana et al., 1999](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2232607/); [Schioppa et al., 2022](https://arxiv.org/abs/2112.03052); [Koh and Liang, 2017](https://proceedings.mlr.press/v70/koh17a.html); [Pruthi et al., 2020](https://proceedings.neurips.cc/paper/2020/hash/e6385d39ec9394f2f3a354d9d2b88eec-Abstract.html) |
| [TRAK](https://github.com/MadryLab/trak) (TRAK)                                               | [Park et al., 2023](https://proceedings.mlr.press/v202/park23c.html)                                                                                                                                                                                                                                                               |
| [Representer Point Selection](https://github.com/chihkuanyeh/Representer_Point_Selection) (Representer Point Selection) | [Yeh et al., 2018](https://proceedings.neurips.cc/paper/2018/hash/8a7129b8f3edd95b7d969dfc2c8e9d9d-Abstract.html)                                                                                                                                                                                                                  |
| [Kronfluence](https://github.com/pomonam/kronfluence) (Kronfluence)                           | [Grosse et al., 2023](https://arxiv.org/abs/2308.03296)                                                                                                                                                                                                                                                                            |
| [Dattri](https://github.com/TRAIS-Lab/dattri) (Influence Functions: Explicit / CG / LiSSA / DataInf, Arnoldi, EK-FAC, TracInCP, Grad-Dot, Grad-Cos, TRAK) | [Deng et al., 2024](https://arxiv.org/abs/2410.04555)                                                                                                                                                                                                                                                                              |


### Metrics

- **Linear Datamodeling Score** ([Park et al., 2023](https://proceedings.mlr.press/v202/park23c.html)): Measures the correlation between the (grouped) attribution scores and the actual output of models trained on different subsets of the training set. For each subset, the linear datamodeling score compares the actual model output to the sum of attribution scores from the subset using Spearman rank correlation.

- **Class Detection / Subclass Detection** ([Hanawa et al., 2021](https://openreview.net/forum?id=9uvhpyQwzM_)): Measures the proportion of identical classes or subclasses in the top-1 training samples over the test dataset. If the attributions are based on similarity, they are expected to be predictive of the class of the test datapoint, as well as different subclasses under a single label.

- **Model Randomization** ([Hanawa et al., 2021](https://openreview.net/forum?id=9uvhpyQwzM_)): Measures the correlation between the original TDA and the TDA of a model with randomized weights. Since the attributions are expected to depend on model parameters, the correlation between original and randomized attributions should be low.

-  **Top-K Cardinality**  ([Barshan et al., 2020](http://proceedings.mlr.press/v108/barshan20a/barshan20a.pdf)): Measures the cardinality of the union of the top-K training samples. Since the attributions are expected to be dependent on the test input, they are expected to vary heavily for different test points, resulting in a low overlap (high metric value).

- **Mislabeled Data Detection** ([Koh and Liang, 2017](https://proceedings.mlr.press/v70/koh17a.html)): Computes the proportion of noisy training labels detected as a function of the percentage of inspected training samples. The samples are inspected in order according to their global TDA ranking, which is computed using local attributions. This produces a cumulative mislabeling detection curve. We expect to see a curve that rapidly increases as we check more of the training data, thus we compute the area under this curve

- **Shortcut Detection** ([Yolcu et al., 2025](https://openreview.net/pdf?id=qfx81N884A)): Assuming a known [shortcut](https://www.nature.com/articles/s42256-020-00257-z), or [Clever-Hans](https://www.nature.com/articles/s41467-019-08987-4) effect has been identified in the model, this metric evaluates how effectively a TDA method can identify shortcut samples as the most influential in predicting cases with the shortcut artifact. This process is referred to as _Domain Mismatch Debugging_ in the original paper.

- **Mixed Datasets** ([Hammoudeh and Lowd, 2022](https://dl.acm.org/doi/abs/10.1145/3548606.3559335)): In a setting where a model has been trained on two datasets: a clean dataset (e.g. CIFAR-10) and an adversarial (e.g. zeros from MNIST), this metric evaluates how well the model ranks the importance (attribution) of adversarial samples compared to clean samples when making predictions on an adversarial example.

- **Mean Reciprocal Rank (MRR)** ([Akyurek et al., 2022](https://aclanthology.org/2022.findings-emnlp.180)): For fact-tracing settings, measures the mean reciprocal rank of the highest-ranked entailing proponent across fact queries.

- **Recall@k** ([Akyurek et al., 2022](https://aclanthology.org/2022.findings-emnlp.180)): For fact-tracing settings, measures the proportion of facts for which an entailing proponent appears in the top-k retrievals.

- **Tail Patch** ([Chang et al., 2024](https://openreview.net/forum?id=gLa96FlWwn)): For fact-tracing settings, measures the incremental change in target-sequence probability after taking a single training step on retrieved proponents.

<details>
  <summary><b><big>Metric interpretation guideline</big></b></summary>

| Benchmark                                                                                            | Output range | Better       |
|------------------------------------------------------------------------------------------------------|--------------|--------------|
| [ClassDetection](quanda/metrics/downstream_eval/class_detection.py)                                  | `[0, 1]`     | higher       |
| [SubclassDetection](quanda/metrics/downstream_eval/subclass_detection.py)                            | `[0, 1]`     | higher       |
| [MislabelingDetection](quanda/metrics/downstream_eval/mislabeling_detection.py)                      | `[0, 1]`     | higher       |
| [ShortcutDetection](quanda/metrics/downstream_eval/shortcut_detection.py)                            | `[0, 1]`     | higher       |
| [MixedDatasets](quanda/metrics/heuristics/mixed_datasets.py)                                         | `[0, 1]`     | higher       |
| [TopKCardinality](quanda/metrics/heuristics/top_k_cardinality.py)                                    | `[0, 1]`     | higher       |
| [ModelRandomization](quanda/metrics/heuristics/model_randomization.py)                               | `[-1, 1]`    | closer to 0  |
| [LinearDatamodelingScore](quanda/metrics/ground_truth/linear_datamodeling.py)                        | `[-1, 1]`    | higher       |
| [MRR](quanda/metrics/downstream_eval/mrr.py)                                                         | `[0, 1]`     | higher       |
| [RecallAtK](quanda/metrics/downstream_eval/recall_at_k.py)                                           | `[0, 1]`     | higher       |
| [TailPatch](quanda/metrics/downstream_eval/tail_patch.py)                                            | `[-1, 1]`    | higher       |

</details>

### Benchmarks

**quanda** comes with a few pre-computed benchmarks that can be conveniently used for evaluation in a plug-and-play manner. We are planning to significantly expand the number of benchmarks in the future. The benchmark IDs listed below are to be passed to `load_pretrained`. The following benchmarks are currently available:
<table>
  <thead>
    <tr>
      <th>Metric</th>
      <th>Type</th>
      <th>Modality</th>
      <th>Benchmark IDs (Dataset / Model)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td rowspan="2"><a href="quanda/metrics/heuristics/top_k_cardinality.py">TopKCardinalityMetric</a></td>
      <td rowspan="2">Heuristic</td>
      <td>Vision</td>
      <td>mnist_top_k_cardinality (MNIST / LeNet)<br>cifar_top_k_cardinality (CIFAR-10 / ResNet-9)<br>awa2_top_k_cardinality (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td>Text</td>
      <td>qnli_top_k_cardinality (QNLI / BERT)</td>
    </tr>
    <tr>
      <td rowspan="2"><a href="quanda/metrics/heuristics/model_randomization.py">ModelRandomizationMetric</a></td>
      <td rowspan="2">Heuristic</td>
      <td>Vision</td>
      <td>mnist_model_randomization (MNIST / LeNet)<br>cifar_model_randomization (CIFAR-10 / ResNet-9)<br>awa2_model_randomization (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td>Text</td>
      <td>qnli_model_randomization (QNLI / BERT)</td>
    </tr>
    <tr>
      <td rowspan="2"><a href="quanda/metrics/heuristics/mixed_datasets.py">MixedDatasetsMetric</a></td>
      <td rowspan="2">Heuristic</td>
      <td>Vision</td>
      <td>mnist_mixed_datasets (MNIST / LeNet)<br>cifar_mixed_datasets (CIFAR-10 / ResNet-9)<br>awa2_mixed_datasets (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td>Text</td>
      <td>qnli_mixed_datasets (QNLI / BERT)</td>
    </tr>
    <tr>
      <td rowspan="2"><a href="quanda/metrics/downstream_eval/class_detection.py">ClassDetectionMetric</a></td>
      <td rowspan="2">Downstream-Task-Evaluator</td>
      <td>Vision</td>
      <td>mnist_class_detection (MNIST / LeNet)<br>cifar_class_detection (CIFAR-10 / ResNet-9)<br>awa2_class_detection (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td>Text</td>
      <td>qnli_class_detection (QNLI / BERT)</td>
    </tr>
    <tr>
      <td><a href="quanda/metrics/downstream_eval/subclass_detection.py">SubclassDetectionMetric</a></td>
      <td>Downstream-Task-Evaluator</td>
      <td>Vision</td>
      <td>mnist_subclass_detection (MNIST / LeNet)<br>cifar_subclass_detection (CIFAR-10 / ResNet-9)<br>awa2_subclass_detection (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td rowspan="2"><a href="quanda/metrics/downstream_eval/mislabeling_detection.py">MislabelingDetectionMetric</a></td>
      <td rowspan="2">Downstream-Task-Evaluator</td>
      <td>Vision</td>
      <td>mnist_mislabeling_detection (MNIST / LeNet)<br>cifar_mislabeling_detection (CIFAR-10 / ResNet-9)<br>awa2_mislabeling_detection (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td>Text</td>
      <td>qnli_mislabeling_detection (QNLI / BERT)</td>
    </tr>
    <tr>
      <td><a href="quanda/metrics/downstream_eval/shortcut_detection.py">ShortcutDetectionMetric</a></td>
      <td>Downstream-Task-Evaluator</td>
      <td>Vision</td>
      <td>mnist_shortcut_detection (MNIST / LeNet)<br>cifar_shortcut_detection (CIFAR-10 / ResNet-9)<br>awa2_shortcut_detection (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td><a href="quanda/metrics/downstream_eval/mrr.py">MRRMetric</a></td>
      <td>Downstream-Task-Evaluator</td>
      <td>Causal LM</td>
      <td>gpt2_trex_openwebtext_ft_mrr (T-REx / GPT-2 fine-tuned on OpenWebText)</td>
    </tr>
    <tr>
      <td><a href="quanda/metrics/downstream_eval/recall_at_k.py">RecallAtKMetric</a></td>
      <td>Downstream-Task-Evaluator</td>
      <td>Causal LM</td>
      <td>gpt2_trex_openwebtext_ft_recall_at_k (T-REx / GPT-2 fine-tuned on OpenWebText)</td>
    </tr>
    <tr>
      <td><a href="quanda/metrics/downstream_eval/tail_patch.py">TailPatchMetric</a></td>
      <td>Downstream-Task-Evaluator</td>
      <td>Causal LM</td>
      <td>gpt2_trex_openwebtext_ft_tail_patch (T-REx / GPT-2 fine-tuned on OpenWebText)</td>
    </tr>
    <tr>
      <td rowspan="2"><a href="quanda/metrics/ground_truth/linear_datamodeling.py">LinearDatamodelingMetric</a></td>
      <td rowspan="2">Ground Truth</td>
      <td>Vision</td>
      <td>mnist_linear_datamodeling (MNIST / LeNet)<br>cifar_linear_datamodeling (CIFAR-10 / ResNet-9)<br>awa2_linear_datamodeling (AWA2 / ResNet-50)</td>
    </tr>
    <tr>
      <td>Text</td>
      <td>qnli_linear_datamodeling (QNLI / BERT)</td>
    </tr>
  </tbody>
</table>



## 🔬 Getting Started

### Installation

To install **quanda** from a local clone of this repository, run:

```setup
pip install -e .
```

**quanda** requires Python 3.10, 3.11 or 3.12. It is recommended to use a virtual environment to install the package.

### Basic Usage

In the following usage examples, we will be using the `SimilarityInfluence` data attribution from `Captum`.

#### Using Metrics

To begin using **quanda** metrics, you need the following components:

- **Trained PyTorch Model (`model`)**: A PyTorch model that has already been trained on a relevant dataset. As a placeholder, we used the layer name "avgpool" below. Please replace it with the name of one of the layers in your model.
- **PyTorch Dataset (`train_set`)**: The dataset used during the training of the model.
- **Test Dataset (`eval_set`)**: The dataset to be used as test inputs for generating explanations. Explanations are generated with respect to an output neuron corresponding to a certain class. This class can be selected to be the ground truth label of the test points, or the classes predicted by the model. In the following we will use the predicted labels to generate explanations.
Next, we demonstrate how to evaluate explanations using the **Model Randomization** metric.

<details>
<summary><b>1. Import dependencies and library components</b></summary>

<!-- START1 -->
```python
from torch.utils.data import DataLoader
from tqdm import tqdm

from quanda.explainers.wrappers import CaptumSimilarity
from quanda.metrics.heuristics import ModelRandomizationMetric
```
<!-- END1 -->
</details>

<details>
<summary><b>2. Create the explainer object</b></summary>

We now create our explainer. The device to be used by the explainer and metrics is inherited from the model, thus we set the model device explicitly.

<!-- START2 -->
```python
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
model.to(DEVICE)

explainer_kwargs = {
    "layers": "fc_2",
    "model_id": "default_model_id",
    "cache_dir": cache_dir,
}
explainer = CaptumSimilarity(
    model=model, train_dataset=dataset, **explainer_kwargs
)
```
<!-- END2 -->
</details>

<details>
<summary><b>3. Initialize the metric</b></summary>

The `ModelRandomizationMetric` needs to instantiate a new explainer to generate explanations for a randomized model. These will be compared with the explanations of the original model. Therefore, `explainer_cls` is passed directly to the metric along with initialization parameters of the explainer for the randomized model.

<!-- START3 -->
```python
explainer_kwargs = {
    "layers": "fc_2",
    "model_id": "randomized_model_id",
    "cache_dir": cache_dir,
}
ckpt_path = os.path.join(cache_dir, "model_rand_ckpt.pth")
torch.save(model.state_dict(), ckpt_path)
model_rand = ModelRandomizationMetric(
    model=model,
    model_id="randomized_model_id",
    cache_dir=cache_dir,
    train_dataset=dataset,
    checkpoints=ckpt_path,
    explainer_cls=CaptumSimilarity,
    expl_kwargs=explainer_kwargs,
    correlation_fn="spearman",
    seed=42,
)
```
<!-- END3 -->
</details>

<details>
<summary><b>4. Iterate over test set to generate explanations and update the metric</b></summary>

We now start producing explanations with our TDA method. We go through the test set batch-by-batch. For each batch, we first generate the attributions using the predicted labels, and we then update the metric with the produced explanations to showcase how to concurrently handle the explanation and evaluation processes.

<!-- START4 -->
```python
test_loader = DataLoader(eval_set, batch_size=batch_size, shuffle=False)
for test_data, _ in tqdm(test_loader):
    test_data = test_data.to(DEVICE)
    target = model(test_data).argmax(dim=-1)
    tda = explainer.explain(test_data=test_data, targets=target)
    model_rand.update(
        explanations=tda, test_data=test_data, test_targets=target
    )

print("Randomization metric output:", model_rand.compute())
```
<!-- END4 -->
</details>

#### Using Benchmarks

**quanda** benchmarks allow us to streamline the evaluation process by downloading the necessary data and models, and running the evaluation in a single command. The following code demonstrates how to use the `mnist_subclass_detection` benchmark:

<details>
<summary><b>1. Import dependencies and library components</b></summary>

<!-- START5 -->
```python
from quanda.explainers.wrappers import CaptumSimilarity
from quanda.benchmarks.downstream_eval import SubclassDetection
```
<!-- END5 -->
</details>

<details>
<summary><b>2. Prepare arguments for the explainer object</b></summary>

<!-- START6 -->
```python
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
model.to(DEVICE)

explainer_kwargs = {
    "layers": "fc_2",
    "model_id": "default_model_id",
    "cache_dir": cache_dir,
}
```
<!-- END6 -->
</details>

<details>
<summary><b>3. Load a benchmark and score an explainer</b></summary>

<!-- START7_1 -->
```python
subclass_detect = SubclassDetection.load_pretrained(
    bench_id="mnist_subclass_detection",
    cache_dir=cache_dir,
)
```
<!-- END7_1 -->

<!-- START7_2 -->
```python
score = subclass_detect.evaluate(
    explainer_cls=CaptumSimilarity,
    expl_kwargs=explainer_kwargs,
    batch_size=batch_size,
    max_eval_n=16,
)["score"]
print(f"Subclass Detection Score: {score}")
```
<!-- END7_2 -->

</details>

#### Generating the benchmark object from scratch

While we provide a number of benchmarks with pre-computed assets, **quanda** `Benchmark` objects also expose a `train` interface for preparing benchmarks from scratch. To train a benchmark, specify its components in a single YAML file (see `quanda/benchmarks/resources/configs`).
 
<details>
<summary><b>1. Import dependencies and library components</b></summary>

<!-- START11 -->
```python
import torch

from quanda.explainers.wrappers import CaptumSimilarity
from quanda.benchmarks.downstream_eval import MislabelingDetection
```
<!-- END11 -->
</details>

<details>
<summary><b>2. Prepare arguments for the explainer object</b></summary>

<!-- START12 -->
```python
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
model.to(DEVICE)

explainer_kwargs = {
    "layers": "fc_2",
    "model_id": "top_k_model",
    "cache_dir": cache_dir,
}
```
<!-- END12 -->
</details>

<details>
<summary><b>3. Train the model for the benchmark </b></summary>

For mislabeling detection, we will train a model from scratch using a dataset with a portion of labels flipped.

<!-- START13_2 -->
```python
with open(
    "tests/assets/mnist_local_bench/83edb41-default_MislabelingDetection.yaml",
    "r",
) as f:
    mislabel_config = yaml.safe_load(f)

mislabel_config["bench_save_dir"] = os.path.join(
    cache_dir, "mislabeling_detection_bench"
)
mislabeling_detection = MislabelingDetection.train(
    mislabel_config,
    device=DEVICE,
)
```
<!-- END13_2 -->
</details>

<details>
<summary><b>4. Run the evaluation</b></summary>

We can now call the `evaluate` method to directly start the evaluation process on the benchmark.

<!-- START14 -->
```python
score = mislabeling_detection.evaluate(
    explainer_cls=CaptumSimilarity,
    expl_kwargs=explainer_kwargs,
    batch_size=batch_size,
    max_eval_n=16,
)["score"]
print(f"Mislabeling Detection Score: {score}")
```
<!-- END14 -->
</details>


More detailed examples can be found in the [tutorials](tutorials) folder.
You can also use Hydra for benchmark training configuration, as shown in `scripts/train.py`.

### Custom Explainers

In addition to the built-in explainers, **quanda** supports the evaluation of custom explainer methods. This section provides a guide on how to create a wrapper for a custom explainer that matches our interface.

<details>
<summary><b>Step 1. Create an explainer class</b></summary>

Your custom explainer should inherit from the base [Explainer](quanda/explainers/base.py) class provided by **quanda**. The first step is to initialize your custom explainer within the `__init__` method.

```python
from quanda.explainers.base import Explainer

class CustomExplainer(Explainer):
    def __init__(self, model, train_dataset, **kwargs):
        super().__init__(model, train_dataset, **kwargs)
        # Initialize your explainer here
```
</details>

<details>
<summary><b>Step 2. Implement the explain method</b></summary>

The core of your wrapper is the `explain` method. This function should take test samples and their corresponding target values as input and return a 2D tensor containing the influence scores.

- `test`: The test batch for which explanations are generated.
- `targets`: The target values for the explanations.

Ensure that the output tensor has the shape `(test_samples, train_samples)`, where the entries in the train samples dimension are ordered in the same order as in the `train_dataset` that is being attributed.

```python
def explain(
  self,
  test_data: torch.Tensor,
  targets: Union[List[int], torch.Tensor]
) -> torch.Tensor:
    # Compute your influence scores here
    return influence_scores
 ```
</details>

<details>
<summary><b>Step 3. Implement the self_influence method (Optional) </b></summary>

By default, **quanda** includes a built-in method for calculating self-influence scores. This base implementation computes all attributions over the training dataset, and collects the diagonal values in the attribution matrix. However, you can override this method to provide a more efficient implementation. This method should calculate how much each training sample influences itself and return a tensor of the computed self-influence scores.

```python
def self_influence(self, batch_size: int = 1) -> torch.Tensor:
    # Compute your self-influence scores here
    return self_influence_scores
```
</details>

For detailed examples, we refer to the [existing](quanda/explainers/wrappers/captum_influence.py) [explainer](quanda/explainers/wrappers/representer_points.py) [wrappers](quanda/explainers/wrappers/trak_wrapper.py) in **quanda**.


## ⚠️ Usage Tips and Caveats

- **Controlled Setting Evaluation**: Many metrics require access to ground truth labels for datasets, such as the indices of the "shortcut samples" in the Shortcut Detection metric, or the mislabeling (noisy) label indices for the Mislabeling Detection Metric. However, users often may not have access to these labels. To address this, we recommend either using one of our pre-built benchmark suites (see [Benchmarks](#benchmarks) section) or generating (`train` method) a custom benchmark for comparing explainers. Benchmarks provide a controlled environment for systematic evaluation.

- **Explainer Caching**: Many explainers in our library generate re-usable cache. The `cache_dir` and `model_id` parameters passed to various class instances are used to store these intermediary results. Ensure each experiment is assigned a unique combination of these arguments. Failing to do so could lead to incorrect reuse of cached results. If you wish to avoid re-using cached results, you can set the `load_from_disk` parameter to `False`.

- **Benchmark Dataset Caching**: Benchmark initialization methods involve caching a HuggingFace dataset locally to the `HF_HOME` cache path. We recommend ensuring that the environment variable is set as needed and caching the dataset into the directory in advance of loading the benchmark.

- **Explainers Are Expensive To Calculate**: Certain explainers, such as `CaptumTracInCPFastRandProj`, may lead to OutOfMemory (OOM) issues when applied to large models or datasets. In such cases, we recommend adjusting memory usage by either reducing the dataset size or using smaller models to avoid these issues.


## 📓 Tutorials

We have included a few [tutorials](tutorials) to demonstrate the usage of **quanda**:

* [Explainers](tutorials/demo_explainers.ipynb): shows how different explainers can be used with **quanda**
* [Metrics](tutorials/demo_metrics.ipynb): shows how to use the metrics in **quanda** to evaluate the performance of a model
* [Benchmarks](tutorials/demo_benchmarks.ipynb): shows how to use the benchmarking tools in **quanda** to evaluate a data attribution method

To install the library with tutorial dependencies, run:

```bash
pip install -e '.[tutorials]'
```

## 👩‍💻Contributing
We welcome contributions to **quanda**! You could contribute by:
- Opening an issue to report a bug or request a feature.
- Submitting a pull request to fix a bug, add a new explainer wrapper, a new metric, or another feature.

A detailed guide on how to contribute to **quanda** can be found [here](CONTRIBUTING.md).

## ✉️ Contact
If you have any questions regarding the codebase, please open an issue or contact us via email at [dilyabareeva@gmail.com](mailto:dilyabareeva@gmail.com) or [galip.uemit.yolcu@hhi.fraunhofer.de](mailto:galip.uemit.yolcu@hhi.fraunhofer.de).

## 🔗Citation

```bibtex
@misc{bareeva2024quandainterpretabilitytoolkittraining,
      title={Quanda: An Interpretability Toolkit for Training Data Attribution Evaluation and Beyond},
      author={Dilyara Bareeva and Galip Ümit Yolcu and Anna Hedström and Niklas Schmolenski and Thomas Wiegand and Wojciech Samek and Sebastian Lapuschkin},
      year={2024},
      eprint={2410.07158},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2410.07158},
}
```
