Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Type annotations and dynamic checking for a tensor's shape, dtype, names, etc.
| Date | Stars |
|---|---|
| 2026-07-24 | 1484 |
| 2026-07-25 | 1484 |
| 2026-07-28 | 1484 |
| 2026-07-30 | 1484 |
| 2026-08-06 | 1484 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# Please use jaxtyping instead
*Welcome! For new projects I now **strongly** recommend using my newer [jaxtyping](https://github.com/google/jaxtyping) project instead. It supports PyTorch, doesn't actually depend on JAX, and unlike TorchTyping it is compatible with static type checkers. The 'jax' in the name is now historical!*
<br>
<br>
<br>
The original torchtyping README is as follows.
---
<h1 align='center'>torchtyping</h1>
<h2 align='center'>Type annotations for a tensor's shape, dtype, names, ...</h2>
Turn this:
```python
def batch_outer_product(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
# x has shape (batch, x_channels)
# y has shape (batch, y_channels)
# return has shape (batch, x_channels, y_channels)
return x.unsqueeze(-1) * y.unsqueeze(-2)
```
into this:
```python
def batch_outer_product(x: TensorType["batch", "x_channels"],
y: TensorType["batch", "y_channels"]
) -> TensorType["batch", "x_channels", "y_channels"]:
return x.unsqueeze(-1) * y.unsqueeze(-2)
```
**with programmatic checking that the shape (dtype, ...) specification is met.**
Bye-bye bugs! Say hello to enforced, clear documentation of your code.
If (like me) you find yourself littering your code with comments like `# x has shape (batch, hidden_state)` or statements like `assert x.shape == y.shape` , just to keep track of what shape everything is, **then this is for you.**
---
## Installation
```bash
pip install torchtyping
```
Requires Python >=3.7 and PyTorch >=1.7.0.
If using [`typeguard`](https://github.com/agronholm/typeguard) then it must be a version <3.0.0.
## Usage
`torchtyping` allows for type annotating:
- **shape**: size, number of dimensions;
- **dtype** (float, integer, etc.);
- **layout** (dense, sparse);
- **names** of dimensions as per [named tensors](https://pytorch.org/docs/stable/named_tensor.html);
- **arbitrary number of batch dimensions** with `...`;
- **...plus anything else you like**, as `torchtyping` is highly extensible.
If [`typeguard`](https://github.com/agronholm/typeguard) is (optionally) installed then **at runtime the types can be checked** to ensure that the tensors really are of the advertised shape, dtype, etc.
```python
# EXAMPLE
from torch import rand
from torchtyping import TensorType, patch_typeguard
from typeguard import typechecked
patch_typeguard() # use before @typechecked
@typechecked
def func(x: TensorType["batch"],
y: TensorType["batch"]) -> TensorType["batch"]:
return x + y
func(rand(3), rand(3)) # works
func(rand(3), rand(1))
# TypeError: Dimension 'batch' of inconsistent size. Got both 1 and 3.
```
`typeguard` also has an import hook that can be used to automatically test an entire module, without needing to manually add `@typeguard.typechecked` decorators.
If you're not using `typeguard` then `torchtyping.patch_typeguard()` can be omitted altogether, and `torchtyping` just used for documentation purposes. If you're not already using `typeguard` for your regular Python programming, then strongly consider using it. It's a great way to squash bugs. Both `typeguard` and `torchtyping` also integrate with `pytest`, so if you're concerned about any performance penalty then they can be enabled during tests only.
## API
```python
torchtyping.TensorType[shape, dtype, layout, details]
```
The core of the library.
Each of `shape`, `dtype`, `layout`, `details` are optional.
- The `shape` argument can be any of:
- An `int`: the dimension must be of exactly this size. If it is `-1` then any size is allowed.
- A `str`: the size of the dimension passed at runtime will be bound to this name, and all tensors checked that the sizes are consistent.
- A `...`: An arbitrary number of dimensions of any sizes.
- A `str: int` pair (technically it's a slice), combining both `str` and `int` behaviour. (Just a `str` on its own is equivalent to `str: -1`.)
- A `str: str` pair, in which case the sizeExcerpt of 8,324 characters
Read on GitHubPatrick Kidger · Cradle.bio · Switzerland
56
Adil Zouitine · @uma-robots · France
6
1
1
1
1
1
Miles Cranmer · University of Cambridge · United Kingdom
1
Would you bet a product on this? Bounded 0–100 and slow moving.
matched fp:f868c24d51903ccb, topic:pytorch