Skip to content

Commit

Permalink
add slice support for dim < 0 (#16494)
Browse files Browse the repository at this point in the history
* add slice support for dim < 0 test=develop
  • Loading branch information
wopeizl authored Mar 29, 2019
1 parent 8f7b588 commit e014950
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
23 changes: 17 additions & 6 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,13 +789,24 @@ def __getitem__(self, item):
if isinstance(item, tuple):
if len(item) > len(self.shape):
raise IndexError("Too many indexes")
fixedSize = True
for i in range(len(self.shape)):
if self.shape[i] == -1:
fixedSize = False
break

newitem = self._reconstructSliceinfo(item) or item
check, info = self._detectContinuesSlice(newitem)
if check:
starts = info[0]
ends = info[1]
axes = [i for i in range(len(starts))]
return self._sliceVar(axes, starts, ends)
if fixedSize:
check, info = self._detectContinuesSlice(newitem)
if check and fixedSize:
starts = info[0]
ends = info[1]
axes = [i for i in range(len(starts))]
return self._sliceVar(axes, starts, ends)
else:
new_var = self
for index, o in enumerate(newitem):
new_var = new_var._sliceAndConcatVar(o, index)
else:
new_var = self
for index, o in enumerate(newitem):
Expand Down
54 changes: 22 additions & 32 deletions python/paddle/fluid/tests/unittests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_step_scopes(self):
name='step_scopes', type=core.VarDesc.VarType.STEP_SCOPES)
self.assertEqual(core.VarDesc.VarType.STEP_SCOPES, var.type)

def _test_slice(self):
def _test_slice(self, place):
b = default_main_program().current_block()
w = b.create_var(dtype="float64", shape=[784, 100, 100], lod_level=0)

Expand All @@ -83,7 +83,6 @@ def _test_slice(self):

self.assertEqual(0, nw.lod_level)

place = fluid.CPUPlace()
main = fluid.Program()
with fluid.program_guard(main):
exe = fluid.Executor(place)
Expand All @@ -100,10 +99,23 @@ def _test_slice(self):
var6 = var[1, 1:, 1:]
var7 = var[1, ..., 1:]
var8 = var[1, ...]
var_reshape = fluid.layers.reshape(var, [3, -1, 3])
var9 = var_reshape[1, ..., 2]
var10 = var_reshape[:, :, -1]

x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y = fluid.layers.fc(input=x, size=1, act=None)
var11 = y[:, 0]
feeder = fluid.DataFeeder(place=place, feed_list=[x])
data = []
data.append((np.random.randint(10, size=[13]).astype('float32')))
exe.run(fluid.default_startup_program())

local_out = exe.run(main,
feed=feeder.feed([data]),
fetch_list=[
var, var1, var2, var3, var4, var5, var6,
var7, var8
var7, var8, var9, var10, var11
])

self.assertTrue((np.array(local_out[1]) == np.array(tensor_array[
Expand All @@ -122,38 +134,16 @@ def _test_slice(self):
1, ..., 1:])).all())
self.assertTrue((np.array(local_out[8]) == np.array(tensor_array[
1, ...])).all())
self.assertEqual(local_out[9].shape, (1, 3, 1))
self.assertEqual(local_out[10].shape, (3, 3, 1))
self.assertEqual(local_out[11].shape, (1, 1))

def test_slice(self):
self._test_slice()


class TestVariableImperative(unittest.TestCase):
def _test_slice(self):
b = default_main_program().current_block()
w = b.create_var(dtype="float64", shape=[784, 100, 100], lod_level=0)

for i in range(3):
nw = w[i]
self.assertEqual([1, 100, 100], nw.shape)

nw = w[:]
self.assertEqual([784, 100, 100], nw.shape)

nw = w[:, :, :]
self.assertEqual([784, 100, 100], nw.shape)

nw = w[::2, ::2, :]
self.assertEqual([392, 50, 100], nw.shape)

nw = w[::-2, ::-2, :]
self.assertEqual([392, 50, 100], nw.shape)

nw = w[0::-2, 0::-2, :]
self.assertEqual([1, 1, 100], nw.shape)
place = fluid.CPUPlace()
self._test_slice(place)

def test_slice(self):
with fluid.dygraph.guard():
self._test_slice()
if core.is_compiled_with_cuda():
self._test_slice(core.CUDAPlace(0))


if __name__ == '__main__':
Expand Down

0 comments on commit e014950

Please sign in to comment.