diff --git a/pytensor/tensor/sharedvar.py b/pytensor/tensor/sharedvar.py index 7ce15c2728..f193cf8dcd 100644 --- a/pytensor/tensor/sharedvar.py +++ b/pytensor/tensor/sharedvar.py @@ -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 @@ -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, diff --git a/tests/tensor/test_extra_ops.py b/tests/tensor/test_extra_ops.py index b03d591705..e8900ce5d7 100644 --- a/tests/tensor/test_extra_ops.py +++ b/tests/tensor/test_extra_ops.py @@ -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()) diff --git a/tests/tensor/test_sharedvar.py b/tests/tensor/test_sharedvar.py index b6cbbf7d1c..436334b43a 100644 --- a/tests/tensor/test_sharedvar.py +++ b/tests/tensor/test_sharedvar.py @@ -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 @@ -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)