Skip to main content

Interpretability with tensordict and torch hooks.

Project description

logo

tdhook 🤖🪝

Documentation tdhook license uv Ruff python versions arXiv

codecov ci publish docs

Interpretability with tensordict and torch hooks.

Getting Started

Most methods should work with minimal configuration. Here's a basic example of running Integrated Gradients on a VGG16 model (full example available here):

from tdhook.attribution import IntegratedGradients

# Define attribution target (e.g., zebra class = 340)
def init_attr_targets(targets, _):
    zebra_logit = targets["output"][..., 340]
    return TensorDict(out=zebra_logit, batch_size=targets.batch_size)

# Compute attribution
with IntegratedGradients(init_attr_targets=init_attr_targets).prepare(model) as hooked_model:
    td = TensorDict({
        "input": image_tensor,
        ("baseline", "input"): torch.zeros_like(image_tensor) # required for integrated gradients
    }).unsqueeze(0)
    td = hooked_model(td) # Access attribution with td.get(("attr", "input"))

To dig deeper, see the documentation.

Skills

An agent skill is available for tdhook. It provides AI guidance for attribution, activation analysis, probing, steering, and weight-level interventions—including when to use each method and how to wire TensorDict keys.

Features

Config

This project uses uv to manage python dependencies and run scripts, as well as just to run commands.

Citation

If you're using tdhook in your research, please cite it using the following BibTeX entry:

@misc{poupart2025tdhooklightweightframeworkinterpretability,
      title={TDHook: A Lightweight Framework for Interpretability},
      author={Yoann Poupart},
      year={2025},
      eprint={2509.25475},
      archivePrefix={arXiv},
      primaryClass={cs.AI},
      url={https://arxiv.org/abs/2509.25475},
}

License

tdhook is licensed under the MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tdhook-0.1.3.tar.gz (54.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tdhook-0.1.3-py3-none-any.whl (60.4 kB view details)

Uploaded Python 3

File details

Details for the file tdhook-0.1.3.tar.gz.

File metadata

  • Download URL: tdhook-0.1.3.tar.gz
  • Upload date:
  • Size: 54.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tdhook-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e6f485e3d9776c8e56aa983b98b3f5d9e38793bb75542ee8c8497da5c4d0e402
MD5 c7e945ea638ecdb622d1f4e737c0138a
BLAKE2b-256 889c686ac71e47d2f9a61be031e64f0d04866b418252055ddf90a3e24d6a10a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tdhook-0.1.3.tar.gz:

Publisher: publish.yml on Xmaster6y/tdhook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tdhook-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: tdhook-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 60.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tdhook-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b90e4fb5a6a9bdd8efbf424c1e13b5c3cd39799be435e012ec09b17b183002c
MD5 ec16956f2a331bc24c31d8ad10f283be
BLAKE2b-256 e143e7032140c3128c4faea8cc44adc13be2433ff0f2e6221a94be317360341d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tdhook-0.1.3-py3-none-any.whl:

Publisher: publish.yml on Xmaster6y/tdhook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page