Skip to main content
logo

tdhook 🤖🪝

Documentation tdhook license uv Ruff python versions arXiv

codecov ci publish docs

Composable interpretability for PyTorch with TensorDict and torch hooks.

Getting Started

Install TDHook from PyPI with Python 3.11 or newer:

pip install tdhook

TDHook methods wrap an ordinary PyTorch model for the lifetime of a context manager. Inputs, baselines, model outputs, and interpretability results use explicit TensorDict keys:

import torch
from torch import nn
from tensordict import TensorDict
from tdhook.attribution import IntegratedGradients

model = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 2))
inputs = torch.tensor([[0.2, -0.1, 0.4, 0.7]])

def select_score(outputs, _):
    score = outputs["output"][..., 0]
    return TensorDict(score=score, batch_size=outputs.batch_size)

data = TensorDict(
    {
        "input": inputs,
        ("baseline", "input"): torch.zeros_like(inputs),
    },
    batch_size=[1],
)

with IntegratedGradients(init_attr_targets=select_score).prepare(model) as hooked_model:
    result = hooked_model(data)

attributions = result["attr", "input"]

The context installs and removes the hooks; the returned attribution has the same shape as inputs. See Getting Started for the annotated version.

Learn by example

The tutorial gallery collects all maintained method and end-to-end notebooks. Launch a method notebook directly in Colab:

Use the generated API reference for exact signatures. The TDHook agent skill provides guidance for attribution, activation analysis, probing, steering, and weight-level interventions.

Config

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

Benchmarks

The maintained benchmark suite checks current TDHook attribution, capture, and intervention behavior against reference libraries before recording versioned timing and memory results. It provides a cheap local smoke mode and a documented full mode; it does not claim to reproduce the historical v0.1 paper measurements.

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.

Metadata

Release files for tdhook 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tdhook 0.3.0
File Size Uploaded
tdhook-0.3.0.tar.gz 135.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tdhook 0.3.0
File Interpreter ABI Platform
tdhook-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 246.8 kB

Release files / tdhook-0.3.0.tar.gz

Download URL tdhook-0.3.0.tar.gz
Size 135.5 kB
Tags Source
SHA-256 checksum
How to use checksums
88c7ee1e5e83d95ca9345c438aacffdd978e5e044584b127d93f41e87efdd323
BLAKE2b-256 checksum
How to use checksums
4ebfec918f3e81e4ccdba716225b13c17c54907c5cfeab18b2a3b03114d87780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release files / tdhook-0.3.0-py3-none-any.whl

Download URL tdhook-0.3.0-py3-none-any.whl
Size 111.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4a686692eddf4b5449e026034433c598445ccc01b98609b479857c849147c61
BLAKE2b-256 checksum
How to use checksums
4d56fd59f7d88ac436cf071ac1da9b62b98697d4d8e06497c023a74a5e983904
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page