Skip to main content

Compatibility helpers for PyTorch higher-order operators.

Project description

hoptorch

hoptorch is a small compatibility package for PyTorch higher-order operators. Its first helper is a safe wrapper around torch._higher_order_ops.scan that checks whether scan backward is healthy on the requested device and lazily installs version-specific compatibility patches for affected PyTorch internals. For PyTorch 2.7, where scan backward is not implemented, hoptorch backports a small eager scan implementation with ordinary autograd support.

Install

pip install hoptorch

Runtime dependency:

torch>=2.7
pyvers>=0.2.2

Usage

import torch
from hoptorch import scan
from hoptorch.scan import ensure_scan_backward, scan_unavailable_reason

if ensure_scan_backward("cpu"):
    xs = torch.arange(4.0)

    def step(carry, x):
        next_carry = carry + x
        return next_carry, next_carry.clone()

    carry, ys = scan(step, torch.zeros(()), xs)
else:
    print(scan_unavailable_reason("cpu"))

For compiled code, warm the health check before entering torch.compile:

from hoptorch.scan import ensure_scan_backward

ensure_scan_backward("cpu")
compiled_fn = torch.compile(fn)

If a wrapper call happens during Dynamo tracing before the health result is cached, hoptorch fails closed instead of tracing the probe.

Public API

from hoptorch import scan
from hoptorch.scan import (
    ensure_scan_backward,
    has_scan,
    patch_scan_backward,
    scan_unavailable_reason,
)
  • scan(fn, init, xs, *, dim=0, **kwargs): calls PyTorch scan only when scan backward is known to be healthy for the inferred device.
  • has_scan(): reports whether torch._higher_order_ops.scan.scan exists.
  • ensure_scan_backward(device=None): runs or reads the cached health check, patching lazily if needed, and returns True only after a passing probe.
  • scan_unavailable_reason(device=None): returns None when scan backward is usable, otherwise a stable human-readable reason.
  • patch_scan_backward(): attempts to install the compatibility patch and is intended mainly for diagnostics.

Tests

python -m unittest discover -s tests

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

hoptorch-0.1.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

hoptorch-0.1.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file hoptorch-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for hoptorch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3c531c19d8070635b2bffb74124efd63c88cb9611312536baee388cbfff014d4
MD5 286b13b9d0d3220b43fa5175440edb39
BLAKE2b-256 4066a4660db118276104d6334150a8e31c2933fc41f3f19dbf39d7e3d7283d5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hoptorch-0.1.1.tar.gz:

Publisher: publish.yml on vmoens/hoptorch

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

File details

Details for the file hoptorch-0.1.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for hoptorch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a175f86cabd09a38c86f9d50d2c5460a27cbf0ccc61441df69d1c5a934746ced
MD5 768165c56835bba17d98363ca18c66c8
BLAKE2b-256 e2a9dd4ec4d3f92c7df1e75c1c1591b8dd52e9453d857698c7ec19f0436b8170

See more details on using hashes here.

Provenance

The following attestation bundles were made for hoptorch-0.1.1-py3-none-any.whl:

Publisher: publish.yml on vmoens/hoptorch

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