Skip to content

Fix AdvancedIncSubtensor1 C-compilation with empty indices #1551

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 1 commit into from
Jul 25, 2025
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
8 changes: 6 additions & 2 deletions pytensor/tensor/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2561,7 +2561,11 @@ def c_code(self, node, name, input_names, output_names, sub):
and y_.type.dtype not in complex_dtypes
):
# Simple implementation for vector x, y cases
idx_may_be_neg = not (isinstance(idx_, Constant) and idx_.data.min() >= 0)
idx_may_be_neg = not (
# Empty idx needs no negative checks
idx_.type.shape[0] == 0
or (isinstance(idx_, Constant) and idx_.data.min() >= 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

we always have static shape for constants, so the check above precludes taking the min of an empty array, which was the original bug

)
idx_may_be_invalid = AdvancedSubtensor1._idx_may_be_invalid(x_, idx_)
shape0 = x_.type.shape[0]
# This is used to make sure that when we trust the indices to be valid
Expand Down Expand Up @@ -2680,7 +2684,7 @@ def c_code(self, node, name, input_names, output_names, sub):
"""

def c_code_cache_version(self):
return (9,)
return (10,)

def _check_runtime_broadcasting(
self, node: Apply, x: np.ndarray, y: np.ndarray, idx: np.ndarray
Expand Down
14 changes: 12 additions & 2 deletions tests/tensor/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pytensor.graph.rewriting.utils import is_same_graph
from pytensor.printing import pprint
from pytensor.scalar.basic import as_scalar, int16
from pytensor.tensor import as_tensor, get_vector_length, vectorize
from pytensor.tensor import as_tensor, constant, get_vector_length, vectorize
from pytensor.tensor.blockwise import Blockwise
from pytensor.tensor.elemwise import DimShuffle
from pytensor.tensor.math import exp, isinf, lt, switch
Expand Down Expand Up @@ -1730,7 +1730,7 @@ def test_grad_broadcastable_specialization(self):
)


class TestIncSubtensor1:
class TestAdvancedIncSubtensor1:
def setup_method(self):
self.rng = np.random.default_rng(seed=utt.fetch_seed())

Expand Down Expand Up @@ -1817,6 +1817,16 @@ def test_inc_bcastableidx(self):
out1val, out2val = f(mval, incval, incval)
utt.assert_allclose(out1val, out2val)

def test_empty_index(self):
x = fvector()
idx = constant([], dtype="int64")
y = idx.astype("float32")
out = advanced_inc_subtensor1(x, y, idx)

test_x = np.array([1, 2, 3], dtype="float32")
res = out.eval({x: test_x}, mode=Mode(optimizer=None))
np.testing.assert_array_equal(res, test_x)


class TestAdvancedSubtensor:
"""Test inc_subtensor and set_subtensor."""
Expand Down