Type annotations and runtime checking for PyTorch tensor shape, dtype, layout, and names.
TorchTyping is a library for adding type annotations to PyTorch tensors that specify their shape, data type, layout, and dimension names. It solves the problem of tracking tensor dimensions and properties across functions, reducing bugs and improving code clarity by making these contracts explicit and optionally enforceable at runtime.
PyTorch developers and machine learning engineers who write complex tensor operations and want to catch shape mismatches early, improve code documentation, and maintain large codebases with many tensor transformations.
Developers choose TorchTyping because it provides a lightweight, Pythonic way to document and validate tensor properties without invasive runtime checks, integrates seamlessly with typeguard for optional enforcement, and replaces error-prone comments and asserts with structured type hints.
Type annotations and dynamic checking for a tensor's shape, dtype, names, etc.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Allows specifying tensor dimensions with named placeholders (e.g., 'batch'), integers, and ellipses for batch dimensions, as demonstrated in the batch_outer_product example, making shape contracts explicit.
Seamlessly integrates with typeguard to enforce annotations at runtime, catching shape mismatches early, like inconsistent batch sizes, without invasive asserts.
Supports checking dtype, layout, named dimensions, and custom extensions via the details flag, offering fine-grained control over tensor properties in a single annotation.
Replaces verbose inline comments with structured type hints, as shown in the README example, turning '# x has shape (batch, hidden_state)' into machine-checkable annotations.
The README admits TorchTyping is not compatible with static type checkers, limiting its use in modern Python development workflows where tools like mypy are standard.
Integrating typeguard for validation adds overhead at runtime, which can be significant in performance-critical loops or production code, despite optional pytest integration.
The author now strongly recommends using jaxtyping, making TorchTyping a legacy project with potential lack of future updates and community support.