![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
ShapeGuard allows you to very succinctly assert the expected shapes of tensors in a dynamic, einsum inspired way.
ShapeGuard allows you to very succinctly assert the expected shapes of tensors in a dynamic, einsum inspired way
Turn this:
def batch_outer_product(x, y):
# 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:
def batch_outer_product(x, y):
x.sg(("batch", "x_channels"))
y.sg(("batch", "y_channels"))
return (x.unsqueeze(-1) * y.unsqueeze(-2)).sg(("batch", "x_channels", "y_channels"))
pip install torch-shapeguard
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 shapeguard
, It adds the sg
method to torch.Tensor
and torch.distributions.Distribution
.
You can 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 chain it inline on results of operations:
z = f(x).sg("bnz").mean(axis=1).sg("bz")
If the assert fails it produces a nice error message:
AssertionError: expected 'b' to be 2 but was 4
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, e.g. x.sg("*chw")
will assert all shapes except the first.
The first time sg
is called for an unseen shape, the size of the tensor for that shape is saved in the ShapeGuard.shapes
global dict.
Subsequent calls are checked against this stored shape.
You can call ShapeGuard.reset(shape)
to reset a specific shape.
This can be useful if e.g. your batch size varies between runs.
ShapeGuard.reset()
resets all shapes.
FAQs
ShapeGuard allows you to very succinctly assert the expected shapes of tensors in a dynamic, einsum inspired way.
We found that torch-shapeguard demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.