Skip to content

Disable runtime broadcasting in indexing operations #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ricardoV94 opened this issue Apr 8, 2025 · 0 comments
Open

Disable runtime broadcasting in indexing operations #1348

ricardoV94 opened this issue Apr 8, 2025 · 0 comments
Labels
bug Something isn't working gradients indexing

Comments

@ricardoV94
Copy link
Member

Description

We are inconsistent in what Ops we allow runtime broadcasting and which we don't.

import pytensor.tensor as pt

x = pt.vector("x", shape=(None,))  # Not known to have length 1 at runtime
out = pt.alloc(x, 3, 5)
try:
    out.eval({x: [1]})
except Exception as e:
    print(str(e).splitlines()[0])
# Runtime broadcasting not allowed. The output of Alloc requires broadcasting a dimension of the input value, which was not marked as broadcastable. If broadcasting was intended, use `specify_broadcastable` on the relevant input.

out = pt.zeros((10, 10))[[5, 6, 7], [0, 1, 2]].inc(x)
try:
    out.eval({x: [1]})
except Exception as e:
    print(str(e).splitlines()[0])
else:
    print("Did not raise")
# Did not raise

Note that whenever we allow runtime broadcasting will have a wrong gradient wrt to that broadcasted input, since we never implemented a mechanism to reduce runtime broadcasted dimensions.

print(pt.grad(out.sum(), x).eval({x: [1]}).shape)  # (3,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working gradients indexing
Projects
None yet
Development

No branches or pull requests

1 participant