提交 ddafc3e2 authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: GitHub

Merge pull request #5903 from lamblin/fix_5898

[BUG,CRASH] Fix crash in GPU advanced indexing with no arrays
...@@ -544,6 +544,13 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor): ...@@ -544,6 +544,13 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor):
idx_ = ([slice(None)] * p + nidx[p:]) idx_ = ([slice(None)] * p + nidx[p:])
x = x.__getitem__(idx_) x = x.__getitem__(idx_)
if p == 0:
# The only indexing was through slices and indices.
# This can happen with symbolic slices for instance.
# Since no view_map is set, we need to copy the returned value
out[0] = x.copy()
return
# flatten the array-indexed dimensions # flatten the array-indexed dimensions
shape = ((np.prod(x.shape[0: p]),) + shape = ((np.prod(x.shape[0: p]),) +
x.shape[p:]) x.shape[p:])
......
...@@ -2169,9 +2169,14 @@ class AdvancedSubtensor(Op): ...@@ -2169,9 +2169,14 @@ class AdvancedSubtensor(Op):
def perform(self, node, inputs, out_): def perform(self, node, inputs, out_):
out, = out_ out, = out_
# TODO: in general, we need to re-pack the inputs into a valid rval = inputs[0].__getitem__(inputs[1:])
# index, just like subtensor # When there are no arrays, we are not actually doing advanced
out[0] = inputs[0].__getitem__(inputs[1:]) # indexing, so __getitem__ will not return a copy.
# Since no view_map is set, we need to copy the returned value
if not any(isinstance(v.type, TensorType) and v.ndim > 0
for v in node.inputs[1:]):
rval = rval.copy()
out[0] = rval
def connection_pattern(self, node): def connection_pattern(self, node):
rval = [[True]] rval = [[True]]
......
...@@ -1623,6 +1623,14 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1623,6 +1623,14 @@ class TestAdvancedSubtensor(unittest.TestCase):
out = X[b_idx, r_idx, c_idx].eval({X: xx}) out = X[b_idx, r_idx, c_idx].eval({X: xx})
utt.assert_allclose(out, xx[b_idx, r_idx, c_idx]) utt.assert_allclose(out, xx[b_idx, r_idx, c_idx])
def test_adv_sub_slice(self):
# Reported in https://github.com/Theano/Theano/issues/5898
var = self.shared(np.zeros([3, 3], dtype=config.floatX))
slc = tensor.slicetype()
f = theano.function([slc], var[slc])
s = slice(1, 3)
f(s)
def test_grad(self): def test_grad(self):
ones = np.ones((1, 3), dtype=self.dtype) ones = np.ones((1, 3), dtype=self.dtype)
n = self.shared(ones * 5, broadcastable=(True, False)) n = self.shared(ones * 5, broadcastable=(True, False))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论