Skip to main content

ShapeGuard allows you to very succinctly assert the expected shapes of tensors in a dynamic, einsum inspired way.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

torch_shapeguard-1.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file torch_shapeguard-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: torch_shapeguard-1.0.1-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

Hashes for torch_shapeguard-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79a6161395fc88b85806041199d371e2d25fe86a0f4e5a1d0f2c45dee1fdd975
MD5 4adc32a9ae2d9713bfef5346be9dccd7
BLAKE2b-256 591121e0302df242c47d6af8c6981053ef35d8ddf786be24a5b015ff47b20c38

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page