Registry / ai-ml / torchtyping

torchtyping

JSON →
library0.1.5pypiunverified

torchtyping provides runtime type annotations for PyTorch Tensors, allowing developers to specify and dynamically check the shape, dtype, names, and layout of tensors. It aims to improve code clarity and reduce bugs by enforcing consistent tensor properties. The library is currently at version 0.1.5.

pip install torchtyping
INSTALL
IMPORT
SIG · TORCHTYPING
T
torchtyping
ai-mlenv0.1.5
Install
66.9s avg
Import
5369ms
Disk
4787MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 75s
py 3.11
✕ build_error
✓ 69.1s
py 3.12
✕ build_error
✓ 63.95s
py 3.13
✕ build_error
✓ 59.5s
py 3.9
✕ build_error
✕ timeout
4787MB installed
● package 4787MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TensorType
from torchtyping import TensorType
patch_typeguard
from torchtyping import patch_typeguard

This quickstart demonstrates how to define a function with `TensorType` annotations for tensor shapes. When `typeguard` is installed and `patch_typeguard()` is called, these annotations are checked at runtime. The example shows both a successful operation and an expected runtime `TypeError` when tensor shapes are inconsistent.

import torch from torch import rand from torchtyping import TensorType, patch_typeguard from typeguard import typechecked # Call patch_typeguard() once at a global level to enable runtime checking patch_typeguard() @typechecked def add_tensors(x: TensorType['batch'], y: TensorType['batch']) -> TensorType['batch']: return x + y # This will work as shapes match result_ok = add_tensors(rand(3), rand(3)) print(f"Operation successful: {result_ok.shape}") # This would raise a TypeError due to inconsistent 'batch' dimension try: add_tensors(rand(3), rand(1)) except TypeError as e: print(f"Caught expected error: {e}")
Debug
Known issues
breakingThe author strongly recommends migrating to 'jaxtyping' instead of 'torchtyping' for new projects. 'jaxtyping' supports PyTorch, is compatible with static type checkers, and is considered the more polished and easier-to-use successor. [2, 12]
fix
For new projects, use `pip install jaxtyping` and refer to its documentation. For existing projects, consider a migration plan.
affects: <0.1.5
gotchaIf using 'typeguard' for runtime checking, a specific version constraint (`typeguard>=2.11.1,<3`) must be followed. Newer versions of 'typeguard' (3.0.0 and above) are not compatible with 'torchtyping'. [2, 5]
fix
Ensure `typeguard` is installed with `pip install 'typeguard>=2.11.1,<3'` if you intend to use runtime checks with `torchtyping`.
affects: All versions
gotchaTensorType annotations are not compatible with static type checkers (e.g., MyPy, Pyright). This means static analysis tools will not detect incorrect usage of `TensorType` annotations, limiting their utility for compile-time error detection. [11, 12]
fix
Be aware that static type checkers will not validate `torchtyping` annotations. For static type checking compatibility, consider using 'jaxtyping'.
affects: All versions
gotchaFunctions and modules annotated with `TensorType` are not compatible with TorchScript compilation, resulting in 'Unknown type constructor TensorType' errors. [15]
fix
Avoid using `TensorType` annotations on functions or modules intended for TorchScript compilation. Separate code paths or remove annotations if TorchScript is required.
affects: All versions
Errors
Common errors & fixes
TypeError: Dimension 'batch' of inconsistent size. Got both X and Y.
This error occurs at runtime when `patch_typeguard()` is enabled and a `TensorType` annotated function receives tensors with inconsistent dimensions for a named axis. [2]
fix
Ensure that all tensors passed to the annotated function have consistent sizes for dimensions sharing the same name (e.g., 'batch'). Adjust input tensor shapes or function logic.
Unknown type constructor TensorType
This error arises when attempting to use TorchScript (e.g., `@torch.jit.script`) on functions that have `TensorType` annotations. `TorchScript` does not understand `TensorType` as a valid type constructor. [15]
fix
Remove `TensorType` annotations from functions that need to be compiled with TorchScript. Consider using regular `torch.Tensor` annotations or a different approach for runtime checks in TorchScripted code.
Static type checker (e.g., MyPy, Pyright) does not flag errors for incorrect tensor shapes when using `TensorType`.
Unlike its successor `jaxtyping`, `torchtyping` is not designed to be compatible with static type checkers. It only provides runtime checks when `typeguard` is enabled. [11, 12]
fix
This is expected behavior for `torchtyping`. If static type checking for tensor shapes is desired, migrate to the `jaxtyping` library.
Upgrade
Version history
0.1.5latest on PyPI · released Aug 1, 2024
Audit
Dependencies
torchrequiredCore PyTorch dependency for tensor operations.
typeguardoptionalOptional dependency for enabling runtime type checking.
typing_extensionsoptionalRequired for Python versions prior to 3.9 for full typing support.
Agent activity
29 hits · last 30 days
node
28
OpenAI (training)
1
Resources

No resource links recorded.

torchtyping — pip install torchtyping · libregistry