提交 1f380740 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fix the perform to make it work.

上级 ae7c5a2a
...@@ -490,110 +490,51 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor): ...@@ -490,110 +490,51 @@ class GpuAdvancedSubtensor(HideC, tensor.AdvancedSubtensor):
x = inputs[0] x = inputs[0]
idx = inputs[1:] idx = inputs[1:]
assert len(idx) >= x.ndim # detect and transpose array indices
# step 1: find smallest index transp = list(range(x.ndim))
p = 0
pp = 0
nidx = []
nshp = list(x.shape)
for k, i in enumerate(idx): for k, i in enumerate(idx):
if isinstance(i, numpy.ndarray): if (isinstance(i, numpy.ndarray) and
start = k i.ndim != 0):
break transp.remove(k)
for k, i in enumerate(idx[::-1]): transp.insert(p, k)
if isinstance(i, numpy.ndarray): nidx.insert(p, i)
end = len(idx) - k p += 1
break else:
if i is None:
# step 2: transpose nidx.append(slice(None))
def get_indices(a, b, ind): nshp.insert(pp, 1)
"""
Get real indices for a list of indices.
"""
dimshuffle_info = []
new_ind = []
k = 0
new_axis = x.ndim
dimshuffle_info_append = []
new_ind_append = []
for i in range(0, a):
if isinstance(ind[i], slice):
dimshuffle_info_append.append(k)
new_ind_append.append(ind[i])
k += 1
elif ind[i] is None:
dimshuffle_info_append.append(new_axis)
new_axis += 1
new_ind_append.append(slice(None))
dimshuffle_info.append(k)
new_ind.append(ind[a])
k += 1
idx_1 = []
idx_2 = []
idx_3 = []
for i in range(a + 1, b):
if isinstance(ind[i], slice):
idx_1.append(k)
idx_2.append(ind[i])
k += 1
elif ind[i] is None:
idx_1.append(new_axis)
new_axis += 1
idx_2.append(slice(None))
else: else:
idx_3.append(k) nidx.append(i)
new_ind.append(ind[i]) pp += 1
k += 1 x = x.reshape(nshp)
valid_end = len(new_ind) x = x.transpose(*transp)
dimshuffle_info.extend(idx_3) idx_ = ([slice(None)] * p + nidx[p:])
dimshuffle_info.extend(dimshuffle_info_append) x = x.__getitem__(idx_)
new_ind.extend(new_ind_append)
# flatten the array-indexed dimensions
new_ind += idx_2 shape = ((numpy.prod(x.shape[0: p]),) +
dimshuffle_info.extend(idx_1) x.shape[p:])
for i in range(b, len(ind)):
if isinstance(ind[i], slice):
dimshuffle_info.append(k)
new_ind.append(ind[i])
k += 1
elif ind[i] is None:
dimshuffle_info.append(new_axis)
new_axis += 1
new_ind.append(slice(None))
return dimshuffle_info, new_ind, valid_end
(dimshuffle_idx, new_ind,
end_) = get_indices(start, end, idx)
shape = x.shape + (1, ) * (len(dimshuffle_idx) - x.ndim)
x = x.reshape(shape)
x = x.transpose(*dimshuffle_idx)
# step 3: partial flattening
shape = (x.shape[: 0] +
(numpy.prod(x.shape[0: end_]),) +
x.shape[end_:])
input_flat = x.reshape(shape) input_flat = x.reshape(shape)
# step 4: build the strides
# build the strides
strides = [1] strides = [1]
for i in range(0, end_ - 1)[::-1]: for i in range(p - 1, 0, -1):
stride = x.shape[i + 1] * strides[-1] stride = x.shape[i] * strides[-1]
strides.append(stride) strides.insert(0, stride)
# step 5: build the indices into x_flat
items = [new_ind[i] if isinstance(new_ind[i], numpy.ndarray) # build the indices and use it
else 0 for i in range(0, end_)] take_idx = sum((i * s for i, s in zip(nidx, strides)))
new_idx = numpy.sum([i * j for i, j out_flat = input_flat.take1(pygpu.asarray(take_idx.flatten(),
in zip(items, strides[::-1])], context=x.context))
axis=0)
# step 6: advanced slicing # finish up
out_flat = input_flat.take1(pygpu.asarray(new_idx.flatten(), out_flat_shp = take_idx.shape + x.shape[p:]
context=input_flat.context)) out[0] = out_flat.reshape(out_flat_shp)
# step 7: reshape into right shape
out_flat_shp = new_idx.shape + x.shape[end_:]
o = out_flat.reshape(out_flat_shp)
idx_ = ([slice(None)] * (new_idx.ndim - 2 + end_) +
new_ind[end_:])
out[0] = o.__getitem__(idx_)
class GpuAdvancedIncSubtensor1(Op): class GpuAdvancedIncSubtensor1(Op):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论