ShapeGuard allows you to very succinctly assert the expected shapes of tensors in a dynamic, einsum inspired way.
Reason this release was yanked:
bug
Project description
ShapeGuard
ShapeGuard allows you to very succinctly assert the expected shapes of tensors in a dynamic, einsum inspired way
It’s easy to make bugs in ml. One particular rich source of bugs is due to the flexibility of the operators: a*b
works whether a and b are vectors, scalar vector, vector vector, etc. Similarly .sum()
will work regardless of the shape of your tensor. Since we're doing optimization whatever computation we end up performing, we can probably optimize it to work reasonably, even if it's not doing what we intended. So our algorithm might "work" even if we have bugs (just less well). This makes bugs super hard to discover.
The best way I’ve found to avoid bugs is to religiously check the shapes of all my tensors, all the time, so I end up spending a lot of time debugging and writing comments like #(bs, n_samples, z_size)
all over the place.
So why not algorithmically check the shapes then? Well it gets ugly fast.
You have to add assert foo.shape == (bs, n_samples, x_size) everywhere, which essentially doubles your linecount and you have to define all your dimensional sizes (bs, etc.), which might vary across train/test, batches, etc. So I made a small helper that makes it much nicer. I call it ShapeGuard. When you import it, It adds a method sg to the tensor class, and exposes a static ShapeGuard class.
You use the sg method like an assert:
def forward(self, x, y):
x.sg("bchw")
y.sg("by")
This will verify that x has 4 dimensions, y has 2 dimensions and that x and y have the same size in the first dimension 'b'. If the assert passes, the tensor is returned. This means you can also use it inline on results of operations:
z = f(x).sg("bnz")
If the assert fails it produces a nice error message.
It works in the following way: the first time sg is called for an unseen shape, the size of the vector for that shape is saved in the ShapeGuard.shapes global dict. Subsequent calls sees this shape in the shapes dict and asserts that the tensor is the same shape for that dimension. If e.g. your batch size changes between train and test you can call ShapeGuard.reset("b")
to reset the "b" shape. I've found it works well to reset all shapes at the start of my main nn.Module.forward
by calling ShapeGuard.reset()
. If you want to verify an exact dimension you can pass an int as the shape e.g.
def forward(self, x, y):
x.sg(("b", 1, "h", "w"))
y.sg("by")
The special shape '*' is reserved for shapes that should not be asserted: x.sg("*chw")
will assert all shapes except the first.
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 Distributions
Built Distribution
File details
Details for the file torch_shapeguard-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: torch_shapeguard-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fa333c087736cc9081c4f5d867341d6a0f94708bc2c7a3cdf154f7018494376 |
|
MD5 | 156ccfb23bc6f3d380bf03b5f515e766 |
|
BLAKE2b-256 | 91184389cca2561678c9bd6ce52625d23c7d6d94b79a90641f79822d7900d63b |