Skip to content

Commit 5628ab1

Browse files
covertgmichaelosthege
authored andcommitted
Type updates for stack (addresses #193)
1 parent e480063 commit 5628ab1

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

pytensor/tensor/basic.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,17 +2559,17 @@ def roll(x, shift, axis=None):
25592559
)
25602560

25612561

2562-
def stack(tensors: Sequence[TensorVariable], axis: int = 0):
2562+
def stack(tensors: Sequence["TensorLike"], axis: int = 0):
25632563
"""Stack tensors in sequence on given axis (default is 0).
25642564
2565-
Take a sequence of tensors and stack them on given axis to make a single
2566-
tensor. The size in dimension `axis` of the result will be equal to the number
2567-
of tensors passed.
2565+
Take a sequence of tensors or tensor-like constant and stack them on
2566+
given axis to make a single tensor. The size in dimension `axis` of the
2567+
result will be equal to the number of tensors passed.
25682568
25692569
Parameters
25702570
----------
2571-
tensors : Sequence[TensorVariable]
2572-
A list of tensors to be stacked.
2571+
tensors : Sequence[TensorLike]
2572+
A list of tensors or tensor-like constants to be stacked.
25732573
axis : int
25742574
The index of the new axis. Default value is 0.
25752575
@@ -2604,11 +2604,11 @@ def stack(tensors: Sequence[TensorVariable], axis: int = 0):
26042604
(2, 2, 2, 3, 2)
26052605
"""
26062606
if not isinstance(tensors, Sequence):
2607-
raise TypeError("First argument should be Sequence[TensorVariable]")
2607+
raise TypeError("First argument should be a Sequence.")
26082608
elif len(tensors) == 0:
2609-
raise ValueError("No tensor arguments provided")
2609+
raise ValueError("No tensor arguments provided.")
26102610

2611-
# If all tensors are scalars of the same type, call make_vector.
2611+
# If all tensors are scalars, call make_vector.
26122612
# It makes the graph simpler, by not adding DimShuffles and SpecifyShapes
26132613

26142614
# This should be an optimization!
@@ -2618,12 +2618,13 @@ def stack(tensors: Sequence[TensorVariable], axis: int = 0):
26182618
# optimization.
26192619
# See ticket #660
26202620
if all(
2621-
# In case there are explicit ints in tensors
2622-
isinstance(t, (np.number, float, int, builtins.complex))
2621+
# In case there are explicit scalars in tensors
2622+
isinstance(t, Number)
2623+
or (isinstance(t, np.ndarray) and t.ndim == 0)
26232624
or (isinstance(t, Variable) and isinstance(t.type, TensorType) and t.ndim == 0)
26242625
for t in tensors
26252626
):
2626-
# in case there is direct int
2627+
# In case there is direct scalar
26272628
tensors = list(map(as_tensor_variable, tensors))
26282629
dtype = aes.upcast(*[i.dtype for i in tensors])
26292630
return MakeVector(dtype)(*tensors)

tests/tensor/test_basic.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,15 +1280,15 @@ def test_stack_scalar_make_vector_dtype(self):
12801280

12811281
def test_stack_scalar_make_vector_constant(self):
12821282
# Test that calling stack() on scalars instantiates MakeVector,
1283-
# event when the scalar are simple int type.
1283+
# even when the scalars are non-symbolic ints.
12841284
a = iscalar("a")
12851285
b = lscalar("b")
12861286
# test when the constant is the first element.
12871287
# The first element is used in a special way
1288-
s = stack([10, a, b, np.int8(3)])
1288+
s = stack([10, a, b, np.int8(3), np.array(4, dtype=np.int8)])
12891289
f = function([a, b], s, mode=self.mode)
12901290
val = f(1, 2)
1291-
assert np.all(val == [10, 1, 2, 3])
1291+
assert np.all(val == [10, 1, 2, 3, 4])
12921292
topo = f.maker.fgraph.toposort()
12931293
assert len([n for n in topo if isinstance(n.op, MakeVector)]) > 0
12941294
assert len([n for n in topo if isinstance(n, type(self.join_op))]) == 0
@@ -1333,11 +1333,14 @@ def test_stack_new_interface(self):
13331333
stack([a, b], -4)
13341334

13351335
# Testing depreciation warning is now an informative error
1336-
with pytest.raises(
1337-
TypeError, match=r"First argument should be Sequence\[TensorVariable\]"
1338-
):
1336+
with pytest.raises(TypeError, match="First argument should be a Sequence"):
13391337
s = stack(a, b)
13401338

1339+
def test_stack_empty(self):
1340+
# Do not support stacking an empty sequence
1341+
with pytest.raises(ValueError, match="No tensor arguments provided"):
1342+
stack([])
1343+
13411344
def test_stack_hessian(self):
13421345
# Test the gradient of stack when used in hessian, see gh-1589
13431346
a = dvector("a")

0 commit comments

Comments
 (0)