Skip to content

Add support for negative axis in specify_broadcastable #710

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

Merged
merged 16 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pytensor/tensor/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import cast

import numpy as np
from numpy.core.numeric import normalize_axis_tuple

import pytensor
from pytensor.gradient import DisconnectedType
Expand Down Expand Up @@ -994,6 +995,9 @@ def specify_broadcastable(x, *axes):
if not axes:
return x

if min(axes) < 0:
axes = list(normalize_axis_tuple(axes, x.type.ndim))

if max(axes) >= x.type.ndim:
raise ValueError("Trying to specify broadcastable of non-existent dimension")

Expand Down
1 change: 1 addition & 0 deletions scripts/mypy-failing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pytensor/tensor/random/basic.py
pytensor/tensor/random/op.py
pytensor/tensor/random/utils.py
pytensor/tensor/rewriting/basic.py
pytensor/tensor/shape.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be done. Over time we want less files, not more. What's mypy complaining about?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy complains about the line from numpy.core.numeric import normalize_axis_tuple. The message is that numpy.core.numeric does not have normalize_axis_tuple.
This already occurs in other places as well and are ignored in the mypy-failing-list. For eg:

from numpy.core.numeric import normalize_axis_tuple

Copy link
Member

@ricardoV94 ricardoV94 Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do the same. Better to ignore a single line than a whole file.

Going forward: In numpy 2.0 at least they moved this to a user facing location so it should be legal to import then. I don't know if it's already there in the current releases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already exists. Can you check if it's fine to import from here:
https://numpy.org/devdocs/reference/generated/numpy.lib.array_utils.normalize_axis_tuple.html#:~:text=Normalizes%20an%20axis%20argument%20into,from%20being%20specified%20multiple%20times.

If so we should import like this in the other places as remove the type ignore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just try this out 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I checked I am currently using numpy version 1.26.4 and I get the error AttributeError: module 'numpy.lib' has no attribute 'array_utils'. I checked the site-packages and it seems like this feature is introduced in numpy 2.0 and greater. Here's the reference numpy.lib _init_.py that matches with what I have locally:

https://github.com/numpy/numpy/blob/ee81e365a87963b695b688d1b38a357599fa61db/numpy/lib/__init__.py#L1-L92

We are yet to switch to numpy2.0 right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange because the docs say it's new from v1.13.

Anyway that answers it for now. No we're not switching to numpy 2.0 for a while, so let's keep importing and telling mypy to ignore it

pytensor/tensor/slinalg.py
pytensor/tensor/subtensor.py
pytensor/tensor/type.py
Expand Down
2 changes: 2 additions & 0 deletions tests/tensor/test_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,13 @@ def test_basic(self):
x = matrix()
assert specify_broadcastable(x, 0).type.shape == (1, None)
assert specify_broadcastable(x, 1).type.shape == (None, 1)
assert specify_broadcastable(x, -1).type.shape == (None, 1)
assert specify_broadcastable(x, 0, 1).type.shape == (1, 1)

x = row()
assert specify_broadcastable(x, 0) is x
assert specify_broadcastable(x, 1) is not x
assert specify_broadcastable(x, -2) is x

def test_validation(self):
x = matrix()
Expand Down