Skip to main content

TorchInstruments

Record per-layer activation and output-gradient statistics without changing your training loop.

from datetime import timedelta
from torchinstruments import inject_observer, remove_observer

inject_observer(model, interval=timedelta(minutes=1), output_dir="stats")
try:
    train(model)
finally:
    remove_observer(model)

The first forward is sampled immediately, then once per interval. The default interval is one minute. Evaluation works too, including torch.no_grad() and torch.inference_mode().

Calling a child directly, such as model.encoder(x) inside a Lightning step, also works. When the observed root is bypassed, selected layers sample independently at the given interval. Each selected module's forward() is intercepted once, so both module(x) and module.forward(x) are captured. Removal restores the original methods.

Output

Everything is created automatically:

Artifact Purpose
history.parquet Full sampled scalar history
result.json Indented history summary for each layer
index.md Reading guide for an LLM, including schema and history queries
tensorboard/ Histogram history for selected layers

history.parquet, result.json, index.md, and TensorBoard update after each sampled forward or backward. They are readable without calling remove_observer(). Removal detaches hooks, closes resources, and cleans up intermediate history.parts/ chunks. Interrupted runs retain the last published files and completed chunks. Each refresh rewrites the Parquet snapshot and aggregates collected history, so its cost grows with run length.

Statistics

Each sampled tensor reports mean, population standard deviation, min, max, p25, p50, p75, zero fraction, and nonfinite fraction. Gradients are recorded separately. Train/eval context is captured automatically, and missing measurements carry reasons.

JSON aggregates each metric's history, including previous/recent window means. These summarize sampled statistics, not pooled tensor entries. Exact observations stay in Parquet. There are no scores or diagnoses. Give index.md to an LLM as the starting point.

Layer selection

All modules, including composite blocks and the root, contribute scalar history when called. The root has the empty layer name "". Histograms cover all selected modules by default. Set max_histogram_modules to a positive integer to explicitly limit histogram work and event size. To focus the dashboard, use histogram_selector:

inject_observer(
    model,
    histogram_selector=lambda name, module: name.startswith("encoder.blocks.7."),
)

Use selector similarly to restrict all collection. Histogram focus must match an observed module. Use selector=leaf_modules() (imported from torchinstruments) for leaf-only coverage. Other existing extension arguments remain available; ordinary usage needs none of them.

Examples and limits

  • Examples: ordinary training, Lightning, and controlled model problems.
  • LLM guide: how to interpret measurements and query history.
  • Benchmarks: interval-based usage and separate stress tests.
  • Changelog: version changes and schema migration.

CPU, native MPS, in-place activations, and non-reentrant checkpointing are tested. Reentrant checkpoint internals lack gradient coverage. Only the first backward is recorded, with the caller's loss scaling. CUDA performance and torch.compile compatibility are unverified. The interval amortizes collection cost; sampled forwards still scan their tensors.

MIT licensed. Author: Vadym Stupakov vadim.stupakov@gmail.com.

Metadata

Release files for torchinstruments 0.10.2

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

Built distribution (wheel)

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

Release files / torchinstruments-0.10.2-py3-none-any.whl

Download URL torchinstruments-0.10.2-py3-none-any.whl
Size 51.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e411934fbca296e1447a654be6601f6e810e7b1d2ad6295093e6ea6d0906c78
BLAKE2b-256 checksum
How to use checksums
94cb9a2bc736f63b59a9f9a7b84da4aed3a5b280638be21913176782af973004
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for torchinstruments 0.10.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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