Skip to content
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

Fix get_vector_length incorrectly returning for shared variable without static shape #1295

Merged
merged 2 commits into from
Mar 24, 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
6 changes: 0 additions & 6 deletions pytensor/tensor/sharedvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np

from pytensor.compile import SharedVariable, shared_constructor
from pytensor.tensor import _get_vector_length
from pytensor.tensor.type import TensorType
from pytensor.tensor.variable import TensorVariable

Expand Down Expand Up @@ -51,11 +50,6 @@ def zero(self, borrow: bool = False):
self.container.value = 0 * self.container.value


@_get_vector_length.register(TensorSharedVariable)
def _get_vector_length_TensorSharedVariable(var_inst, var):
return len(var.get_value(borrow=True))


@shared_constructor.register(np.ndarray)
def tensor_constructor(
value,
Expand Down
6 changes: 4 additions & 2 deletions tests/tensor/test_extra_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,10 @@ def fn(i, d):
f_array_array = fn(indices, shape_array)
np.testing.assert_equal(ref, f_array_array())

# shape given as an PyTensor variable
shape_symb = pytensor.shared(shape_array)
# shape given as a shared PyTensor variable with static shape
shape_symb = pytensor.shared(
shape_array, shape=shape_array.shape, strict=True
)
f_array_symb = fn(indices, shape_symb)
np.testing.assert_equal(ref, f_array_symb())

Expand Down
7 changes: 6 additions & 1 deletion tests/tensor/test_sharedvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ def test_specify_shape_inplace(self):
def test_values_eq(self):
# Test the type.values_eq[_approx] function
dtype = self.dtype

if dtype is None:
dtype = pytensor.config.floatX

Expand Down Expand Up @@ -691,9 +692,13 @@ def test_scalar_shared_deprecated():


def test_get_vector_length():
x = pytensor.shared(np.array((2, 3, 4, 5)))
arr = np.array((2, 3, 4, 5))
x = pytensor.shared(arr, shape=arr.shape, strict=True)
assert get_vector_length(x) == 4

with pytest.raises(ValueError):
get_vector_length(pytensor.shared(arr))


def test_shared_masked_array_not_implemented():
x = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3)
Expand Down