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:

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.2.1

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.2.1
File Size Uploaded
tdhook-0.2.1.tar.gz 135.9 kB Details

Built distribution (wheel)

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

Total release size: 250.1 kB

Release files / tdhook-0.2.1.tar.gz

Download URL tdhook-0.2.1.tar.gz
Size 135.9 kB
Tags Source
SHA-256 checksum
How to use checksums
012f4a5f84ba869f21a508031efc0159267fd4475af83470dd16932f2c315fe8
BLAKE2b-256 checksum
How to use checksums
7e2f5b6d081df2c5a0c5ab4bc1246083878db6610108869f3fc3ae8250ccfb56
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 3, 2026.

Transparency log

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

Download URL tdhook-0.2.1-py3-none-any.whl
Size 114.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ebc81f8d56a1aa53a8ff9b6ea60b1c21adfd307e9198f49a0059f156a4fec400
BLAKE2b-256 checksum
How to use checksums
e02078c0ba511abd7789c8dc044c9f54523d5d98e3e9bea1624da1ac6c603f2d
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 3, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.0

2 release files

This release

0.2.1 This release

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