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 torchtypingVerified import paths — ran on the pinned version, not inferred.
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.
For new projects, use `pip install jaxtyping` and refer to its documentation. For existing projects, consider a migration plan.
Ensure `typeguard` is installed with `pip install 'typeguard>=2.11.1,<3'` if you intend to use runtime checks with `torchtyping`.
Be aware that static type checkers will not validate `torchtyping` annotations. For static type checking compatibility, consider using 'jaxtyping'.
Avoid using `TensorType` annotations on functions or modules intended for TorchScript compilation. Separate code paths or remove annotations if TorchScript is required.
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.
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.
This is expected behavior for `torchtyping`. If static type checking for tensor shapes is desired, migrate to the `jaxtyping` library.
No resource links recorded.