提交 7f4dd684 authored 作者: Ying Zhang's avatar Ying Zhang 提交者: Arnaud Bergeron

don't use symbolic operation

上级 3929a557
...@@ -501,6 +501,7 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor): ...@@ -501,6 +501,7 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor):
if isinstance(i, numpy.ndarray): if isinstance(i, numpy.ndarray):
end = len(idx) - k end = len(idx) - k
break break
# step 2: transpose # step 2: transpose
def get_indices(a, b, ind): def get_indices(a, b, ind):
""" """
...@@ -526,7 +527,7 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor): ...@@ -526,7 +527,7 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor):
idx_1 = [] idx_1 = []
idx_2 = [] idx_2 = []
idx_3 = [] idx_3 = []
for i in range(a+1, b): for i in range(a + 1, b):
if isinstance(ind[i], slice): if isinstance(ind[i], slice):
idx_1.append(k) idx_1.append(k)
idx_2.append(ind[i]) idx_2.append(ind[i])
...@@ -556,32 +557,32 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor): ...@@ -556,32 +557,32 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor):
return dimshuffle_info, new_ind, valid_end return dimshuffle_info, new_ind, valid_end
(dimshuffle_idx, new_ind, (dimshuffle_idx, new_ind,
end_) = get_indices(start, end, idx) end_) = get_indices(start, end, idx)
x = x.transpose(*dimshuffle_idx) x = x.transpose(*dimshuffle_idx)
# step 3: partial flattening # step 3: partial flattening
start_ = start start_ = start
shape = (x.shape[: start_] + shape = (x.shape[: start_] +
(tensor.prod(x.shape[start: end_]),) + (numpy.prod(x.shape[start: end_]),) +
x.shape[end_:]) x.shape[end_:])
input_flat = tensor.reshape(x, shape) input_flat = numpy.reshape(x, shape)
# step 4: build the strides # step 4: build the strides
strides = [1] strides = [1]
for i in range(start_, end_-1)[::-1]: for i in range(start_, end_ - 1)[::-1]:
stride = x.shape[i+1] * strides[-1] stride = x.shape[i + 1] * strides[-1]
strides.append(stride) strides.append(stride)
# step 5: build the indices into x_flat # step 5: build the indices into x_flat
items = [new_ind[i] if isinstance(new_ind[i], numpy.ndarray) items = [new_ind[i] if isinstance(new_ind[i], numpy.ndarray)
else 0 for i in range(start_, end_)] else 0 for i in range(start_, end_)]
new_idx = tensor.sum([i * j for i,j new_idx = numpy.sum([i * j for i, j
in zip(items, strides[::-1])], in zip(items, strides[::-1])],
axis=0) axis=0)
# step 6: advanced slicing # step 6: advanced slicing
out_flat = input_flat.take(new_idx.flatten()) out_flat = input_flat.take(new_idx.flatten())
# step 7: reshape into right shape # step 7: reshape into right shape
out_flat_shp = (x.shape[:start_] + out_flat_shp = (x.shape[:start_] +
new_idx.shape + x.shape[end_:]).astype('int32') new_idx.shape + x.shape[end_:]).astype('int32')
o = out_flat.reshape(out_flat_shp, o = out_flat.reshape(out_flat_shp,
ndim=dims+new_idx.ndim-2) ndim=dims + new_idx.ndim - 2)
idx_ = (new_ind[:start_] + [slice(None)] * idx_ = (new_ind[:start_] + [slice(None)] *
(new_idx.ndim - 2 + end_ - start_) + new_ind[end_:]) (new_idx.ndim - 2 + end_ - start_) + new_ind[end_:])
out[0] = o.__getitem__(idx_) out[0] = o.__getitem__(idx_)
......
...@@ -92,14 +92,13 @@ def test_incsub_f16(): ...@@ -92,14 +92,13 @@ def test_incsub_f16():
class G_advancedsubtensor(test_subtensor.TestAdvancedSubtensor): class G_advancedsubtensor(test_subtensor.TestAdvancedSubtensor):
def shortDescription(self): def shortDescription(self):
return None return None
def __init__(self, name): def __init__(self, name):
test_subtensor.TestAdvancedSubtensor.__init__( test_subtensor.TestAdvancedSubtensor.__init__(
self, name, self, name,
shared=gpuarray_shared_constructor, shared=gpuarray_shared_constructor,
sub=GpuAdvancedSubtensor, sub=GpuAdvancedSubtensor,
inc_sub=GpuIncSubtensor, inc_sub=GpuIncSubtensor,
adv_sub1=GpuAdvancedSubtensor1,
adv_incsub1=GpuAdvancedIncSubtensor1,
mode=mode_with_gpu, mode=mode_with_gpu,
# avoid errors with limited devices # avoid errors with limited devices
dtype='float32', dtype='float32',
...@@ -121,7 +120,7 @@ def test_adv_subtensor(): ...@@ -121,7 +120,7 @@ def test_adv_subtensor():
expr = x[idxs] expr = x[idxs]
f = theano.function([idx1, idx2], expr, mode=mode_with_gpu) f = theano.function([idx1, idx2], expr, mode=mode_with_gpu)
assert sum([isinstance(node.op, GpuAdvancedSubtensor) assert sum([isinstance(node.op, GpuAdvancedSubtensor)
for node in f.maker.fgraph.toposort()]) == 1 for node in f.maker.fgraph.toposort()]) == 1
idx1_val = [0, 1] idx1_val = [0, 1]
idx2_val = [0, 1] idx2_val = [0, 1]
rval = f(idx1_val, idx2_val) rval = f(idx1_val, idx2_val)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论