Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions pytensor/tensor/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,18 @@ def make_node(self, axis, *tensors):
if axis.type.ndim > 0:
raise TypeError(f"Axis {axis} must be 0-d.")

# Convert negative constant axis to positive during canonicalization
if isinstance(axis, Constant) and tensors:
# Get the axis value directly from the constant's data
axis_val = axis.data.item()
# Check if it's negative and needs normalization
if axis_val < 0:
ndim = tensors[0].ndim
# Convert negative axis to positive
axis_val = normalize_axis_index(axis_val, ndim)
# Replace the original axis with the normalized one
axis = constant(axis_val, dtype=axis.type.dtype)

tensors = [as_tensor_variable(x) for x in tensors]

if not builtins.all(targs.type.ndim > 0 for targs in tensors):
Expand Down
35 changes: 35 additions & 0 deletions tests/tensor/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,41 @@ def test_join_performance(self, ndim, axis, memory_layout, gc, benchmark):
assert fn(*test_values).shape == (n * 6, n)[:ndim] if axis == 0 else (n, n * 6)
benchmark(fn, *test_values)

def test_join_negative_axis_rewrite(self):
"""Test that constant negative axis is rewritten to positive axis in make_node."""
v = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=self.floatX)
a = self.shared(v)
b = as_tensor_variable(v)

# Create join with negative axis
s = join(-1, a, b)

assert isinstance(
s.owner.outputs[0].owner.op, Join
), "Expected output node to be a Join op"

assert isinstance(
s.owner.inputs[0], ptb.Constant
), "Expected axis to be a Constant"
assert (
s.owner.inputs[0].data == 1
), f"Expected axis to be normalized to 1, got {s.owner.inputs[0].data}"
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: Convert this to be a single assert, and get rid of the failure messages it's easier to just read the code. Same below

Copy link
Member

@ricardoV94 ricardoV94 Jul 8, 2025

Choose a reason for hiding this comment

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

Or just do assert equal_computations([join(-1, a, b)], [join(1, a, b)]) which is perhaps more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much cleaner. Added this and removed the verbose tests


# Now test with axis -2 which should be rewritten to 0
s2 = join(-2, a, b)

assert isinstance(
s2.owner.outputs[0].owner.op, Join
), "Expected output node to be a Join op"

# Check that the axis input has been converted to a constant with value 0 (not -2)
assert isinstance(
s2.owner.inputs[0], ptb.Constant
), "Expected axis to be a Constant"
assert (
s2.owner.inputs[0].data == 0
), f"Expected axis to be normalized to 0, got {s2.owner.inputs[0].data}"


def test_TensorFromScalar():
s = ps.constant(56)
Expand Down
Loading