Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions pytensor/tensor/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pytensor.graph.utils import MethodNotDefined
from pytensor.link.c.op import COp
from pytensor.link.c.params_type import ParamsType
from pytensor.npy_2_compat import numpy_version, using_numpy_2
from pytensor.npy_2_compat import normalize_axis_tuple, numpy_version, using_numpy_2
from pytensor.printing import Printer, pprint, set_precedence
from pytensor.scalar.basic import ScalarConstant, ScalarVariable
from pytensor.tensor import (
Expand Down Expand Up @@ -3369,11 +3369,12 @@ def flip(
if axis is None:
index = ((slice(None, None, -1)),) * arr.ndim
else:
if isinstance(axis, int):
axis = (axis,)
normalized_axis = normalize_axis_tuple(axis, arr.ndim)
index = tuple(
[
slice(None, None, -1) if i in axis else slice(None, None, None)
slice(None, None, -1)
if i in normalized_axis
else slice(None, None, None)
for i in range(arr.ndim)
]
)
Expand All @@ -3382,9 +3383,9 @@ def flip(


__all__ = [
"take",
"flip",
"slice_at_axis",
"inc_subtensor",
"set_subtensor",
"slice_at_axis",
"take",
]
21 changes: 21 additions & 0 deletions tests/tensor/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3147,6 +3147,27 @@ def test_flip(size: tuple[int]):
f = pytensor.function([x_pt], z, mode="FAST_COMPILE")
np.testing.assert_allclose(expected, f(x), atol=ATOL, rtol=RTOL)

# Test single negative axis
Copy link
Member

Choose a reason for hiding this comment

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

This whole test is so overkill but predates the PR

Copy link
Member

@jessegrabowski jessegrabowski Oct 4, 2025

Choose a reason for hiding this comment

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

I'm sure whoever wrote it was a really smart handsome guy who believed in making sure our code works great in every possible case

for axis in range(-x.ndim, 0):
expected = np.flip(x, axis=axis)
z = flip(x_pt, axis=axis)
f = pytensor.function([x_pt], z, mode="FAST_COMPILE")
np.testing.assert_allclose(expected, f(x), atol=ATOL, rtol=RTOL)

# Test tuple with negative axes
if x.ndim > 1:
expected = np.flip(x, axis=(-1, -2))
z = flip(x_pt, axis=(-1, -2))
f = pytensor.function([x_pt], z, mode="FAST_COMPILE")
np.testing.assert_allclose(expected, f(x), atol=ATOL, rtol=RTOL)

# Test mixed positive and negative axes
if x.ndim >= 2:
expected = np.flip(x, axis=(0, -1))
z = flip(x_pt, axis=(0, -1))
f = pytensor.function([x_pt], z, mode="FAST_COMPILE")
np.testing.assert_allclose(expected, f(x), atol=ATOL, rtol=RTOL)


class TestBenchmarks:
@pytest.mark.parametrize(
Expand Down
Loading