Type annotations and runtime checking for dataclass-like containers of tensors.
Project description
tensorbox
tensorbox allows you to interact with dataclasses of tensors as if they were tensors. Simply use @tensorbox instead of @dataclass.
from jaxtyping import Float
from tensorbox import tensorbox
from torch import Tensor
# Define a @tensorbox class. The jaxtyping annotations describe each attribute's scalar (unbatched) shape.
@tensorbox
class Gaussians:
mean: Float[Tensor, "dim"]
covariance: Float[Tensor, "dim dim"]
color: Float[Tensor, "3"]
# Define Gaussians with batch size (10, 10) and dim=3.
gaussians = Gaussians(
torch.zeros((10, 10, 3), dtype=torch.float32),
torch.zeros((10, 10, 3, 3), dtype=torch.float32),
torch.zeros((10, 10, 3), dtype=torch.float32),
)
# Define a function that uses Gaussians as input. When a @tensorbox class is subscripted, each attribute's shape becomes the concatenation of the subscript (batch shape) and the attribute's original (scalar) shape. This means fn expects the following shapes:
# - mean: "batch_a batch_b dim"
# - covariances: "batch_a batch_b dim dim"
# - color: "batch_a batch_b 3"
def fn(g: Gaussians["batch_a batch_b"]):
...
Features
Shape Inference
A @tensorbox class will automatically infer its batch shape:
@tensorbox
class Camera:
intrinsics: Float[Tensor, "3 3"]
extrinsics: Float[Tensor, "4 4"]
cameras = Camera(
torch.zeros((512, 4, 3, 3), dtype=torch.float32),
torch.zeros((512, 4, 4, 4), dtype=torch.float32),
)
cameras.shape # (512, 4)
Nested Tensorboxes
You can define and use nested @tensorbox classes as follows:
@tensorbox
class Leaf:
rgb: Float[Tensor, "3"]
scale: Float[Tensor, ""]
@tensorbox
class Tree:
pair: Leaf["2"]
def fn(tree: Tree["*batch"]):
# tree.pair.rgb has shape (*batch, 2, 3)
...
Interaction with PyTorch
@tensorbox classes can be used directly with the following torch functions:
torch.cattorch.stack
Note that dim arguments are always specified relative to the @tensorbox class's batch shape.
Comparison with TensorDict
tensorbox is very similar to TensorDict, but has a few key differences:
- It's compatible with
jaxtypingannotations. - It's not as feature-complete.
- When creating a tensorbox class instance, you don't have to specify the batch shape—it's automatically inferred.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensorbox-0.0.1.tar.gz.
File metadata
- Download URL: tensorbox-0.0.1.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3aab9b29e1845efc9c072336b30f9d250cdc70e2d014381a23487116226e3d97
|
|
| MD5 |
6e754225f6dcbaa219086958d9825e77
|
|
| BLAKE2b-256 |
df5a5aeea3424693b1beec07c24987cd1397d3b22e0d88034a8d7c4de70c8458
|
File details
Details for the file tensorbox-0.0.1-py3-none-any.whl.
File metadata
- Download URL: tensorbox-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cfe57a282e9ae81b2cea5660e9d8166f2ac53f00e0542494be56538fcc49979
|
|
| MD5 |
cae03ea2ccaf12f37ba924d7f115740c
|
|
| BLAKE2b-256 |
bcc16eca9eb2a867c3262f3ea70d4632ff27db5ef2c0a4620358689e0f33ba5b
|