Skip to content

Avoid dimshuffle if expand_dims has empty axis #724

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 2 commits into from
Apr 21, 2024
Merged
Changes from all 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
14 changes: 14 additions & 0 deletions pytensor/tensor/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4247,6 +4247,17 @@

Insert a new axis that will appear at the `axis` position in the expanded
array shape.

Parameters
----------
a :
The input array.
axis :
Position in the expanded axes where the new axis is placed.
If `axis` is empty, `a` will be returned immediately.
Returns
-------
`a` with a new axis at the `axis` position.
"""
a = as_tensor(a)

Expand All @@ -4256,6 +4267,9 @@
out_ndim = len(axis) + a.ndim
axis = np.core.numeric.normalize_axis_tuple(axis, out_ndim)

if not axis:
return a

Check warning on line 4271 in pytensor/tensor/basic.py

View check run for this annotation

Codecov / codecov/patch

pytensor/tensor/basic.py#L4271

Added line #L4271 was not covered by tests

dim_it = iter(range(a.ndim))
pattern = ["x" if ax in axis else next(dim_it) for ax in range(out_ndim)]

Expand Down
Loading