提交 3392778a authored 作者: Frederic's avatar Frederic

Fix crash bug with local_subtensor_of_alloc.

When the step of a slice was not 1, we didn't correctly computed the size when the number of element was not a multiple of the step. This caused a crash at run time.
上级 2691c23f
...@@ -1795,13 +1795,14 @@ def local_subtensor_of_alloc(node): ...@@ -1795,13 +1795,14 @@ def local_subtensor_of_alloc(node):
# That dimension is removed. # That dimension is removed.
pass pass
else: else:
nw_dims += [(csl.stop - csl.start) // csl.step] nw_dims += [T.ceil_intdiv((csl.stop - csl.start), csl.step)]
nw_val = val[tuple(val_slices)] nw_val = val[tuple(val_slices)]
nw_dims += dims[len(slices):] nw_dims += dims[len(slices):]
rval = T.alloc(nw_val, *nw_dims) rval = T.alloc(nw_val, *nw_dims)
if type(rval) not in (list, tuple): if type(rval) not in (list, tuple):
rval = [rval] rval = [rval]
return rval return rval
......
...@@ -1864,21 +1864,36 @@ def test_local_subtensor_alloc0(): ...@@ -1864,21 +1864,36 @@ def test_local_subtensor_alloc0():
yx = tensor.alloc(y, x.shape[0], x.shape[1]) yx = tensor.alloc(y, x.shape[0], x.shape[1])
# Slice of each row # Slice of each row
z_mat = yx[:,3:] z_mat = yx[:, 3:]
assert z_mat.ndim == 2 assert z_mat.ndim == 2
# Only one column # Only one column
z_vec = yx[:,3] z_vec = yx[:, 3]
assert z_vec.ndim == 1 assert z_vec.ndim == 1
f = theano.function([x, y], z_mat)
g = theano.function([x, y], z_vec)
# DebugMode should detect if something goes wrong. # DebugMode should detect if something goes wrong.
xval = numpy.zeros((3,5), dtype=config.floatX) # test shape combination of odd and event shape.
yval = numpy.arange(5, dtype=config.floatX) for shape in [(3, 5), (4, 6), (3, 8), (4, 7)]:
f(xval, yval)
g(xval, yval) xval = numpy.zeros(shape, dtype=config.floatX)
yval = numpy.arange(shape[1], dtype=config.floatX)
for slices in [
# results are vector
(slice(None), 3),
(2, slice(None)),
# results are matrix
(slice(None), slice(3, None)),
(slice(3, None), ),
(slice(3, None), slice(3, None)),
(slice(1, 3), slice(None, -1)),
(slice(None, None, 2)),
(slice(1, None, 2)),
]:
z = yx.__getitem__(slices)
f = theano.function([x, y], z)
val = f(xval, yval)
assert xval.__getitem__(slices).shape == val.shape
def test_local_fill_useless(): def test_local_fill_useless():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论