Skip to main content

TorchInstruments

TorchInstruments adds passive, trainer-agnostic telemetry to PyTorch models. It samples root model forwards, collects compact activation and output-gradient statistics from selected modules, and writes strict JSON records without requiring changes to the training loop.

from datetime import timedelta

from torchinstruments import inject_observer

inject_observer(
    model,
    interval=timedelta(minutes=1),
    output_dir="stats",
)

train(model)

The observer is attached as ordinary PyTorch hooks. It does not add parameters, buffers, or modules, and therefore does not change state_dict().

Lifecycle

Injection modifies the model in place and deliberately returns None. Duplicate injection raises ObserverAlreadyAttachedError so configuration is never replaced silently.

from torchinstruments import has_observer, remove_observer

assert has_observer(model)
remove_observer(model)

Removal detaches module and pending graph hooks, closes the sink, and deletes the observer's private Python state.

Configuration

The convenience API constructs the default sampler, leaf-module selector, reducers, and directory sink. Each component can instead be supplied explicitly:

from torchinstruments import (
    AlwaysSampler,
    DirectorySink,
    default_reducers,
    inject_observer,
    leaf_modules,
)

inject_observer(
    model,
    sampler=AlwaysSampler(),
    selector=leaf_modules(),
    reducers=default_reducers(),
    sink=DirectorySink("stats"),
    error_policy="warn",
)

Supported error policies are raise, warn, and ignore. Both non-raising policies preserve collection failures inside snapshot telemetry; warn additionally emits a Python warning.

Output

stats/
    run.json
    modules.json
    snapshots/
        000000.json
        000001.json

A sampled forward is written immediately as forward_complete. If its graph later participates in backward, the same snapshot is atomically enriched to backward_observed. This preserves useful telemetry for inference-only runs while correctly separating multiple outstanding forwards.

Built-in reducers report mean, population standard deviation, RMS, maximum absolute value, and finite fraction. Statistics operate on finite values, and unavailable results carry explicit reasons instead of non-standard JSON NaN or infinity values. Raw tensors are never written.

Compatibility

TorchInstruments requires Python 3.11 or newer and PyTorch 2.0 or newer. The core runtime depends only on PyTorch and the Python standard library. Trainer-specific integrations are not required.

Phase 1 scope

The initial implementation provides:

  • time-based and always-on root-forward sampling;
  • leaf-module selection;
  • nested tensor-output traversal with stable paths;
  • mean, population standard deviation, RMS, maximum absolute value, and finite fraction;
  • correlated output-gradient statistics;
  • run.json, modules.json, and one atomically updated JSON file per snapshot;
  • explicit observer removal and duplicate-injection detection.

See the design document for lifecycle semantics, deliberate limitations, and the project roadmap.

License

TorchInstruments is released under the MIT License.

Citation

If TorchInstruments supports your research or engineering work, cite it as:

@software{stupakov_2026_torchinstruments,
  author  = {Vadym Stupakov},
  title   = {TorchInstruments: Passive PyTorch Model Telemetry},
  year    = {2026},
  version = {0.1.0},
  url     = {https://github.com/Red-Eyed/torchinstruments}
}

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

torchinstruments-0.1.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file torchinstruments-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: torchinstruments-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for torchinstruments-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 09beaa2daed84c4ae9b7d2b42e049687ea9347dad3caf8a097102d95b028c46a
MD5 1751bbf00008701f1c3d52d72fdc1267
BLAKE2b-256 93f36e9923345fe21ea08bd524162e342b3026256dbbbc3b401be58d53062c98

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.0

1 file

0.5.0

1 file

0.4.0

1 file

0.3.0

1 file

0.2.0

1 file

0.1.1

1 file

This release

0.1.0 This release

1 file

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