layers.slice gets unexpected behavior in dygraph mode
Created by: iclementine
System information -PaddlePaddle version: 1.6.2 -GPU: cuda 10.1 / cudnn 7.5 -OS Platform and Distribution: ubuntu 16.04 LTS -Python version: 3.7.4 To Reproduce
I adapted the example code in layers.slice
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
import numpy as np
input = np.random.randn(3,4,5,6).astype(np.float32)
place = fluid.CPUPlace()
with dg.guard(place):
input = dg.to_variable(input)
minus_3 = fluid.layers.fill_constant([1], "int32", -3)
three = fluid.layers.fill_constant([1], "int32", 3)
# case 0
sliced = fluid.layers.slice(input, axes=[0, 1, 2], starts=[-3, 0, 2], ends=[3, 2, 4])
print(sliced.numpy().shape)
# case 1
sliced = fluid.layers.slice(input, axes=[0, 1, 2], starts=[minus_3, 0, 2], ends=[three, 2, 4])
print(sliced.numpy().shape)
# case 2
sliced = fluid.layers.slice(input, axes=[0, 1, 2],
starts=dg.to_variable(np.array([-3, 0, 2]).astype(np.int32)),
ends=dg.to_variable(np.array([3, 2, 4]).astype(np.int32)))
print(sliced.numpy().shape)
case 0 is okay, case 1 and case 2 raise bugs.
case 1 got errors here
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-6f278d68461f> in <module>
17
18 #case 1
---> 19 sliced = fluid.layers.slice(input, axes=[0, 1, 2], starts=[minus_3, 0, 2], ends=[three, 2, 4])
20 print(sliced.numpy().shape)
21
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/layers/nn.py in slice(input, axes, starts, ends)
9818 dtype=helper.input_dtype('input'))
9819 helper.append_op(
-> 9820 type='slice', inputs=inputs, attrs=attrs, outputs={'Out': out})
9821
9822 return out
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/layer_helper.py in append_op(self, *args, **kwargs)
41
42 def append_op(self, *args, **kwargs):
---> 43 return self.main_program.current_block().append_op(*args, **kwargs)
44
45 def multiple_input(self, input_param_name='input'):
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/framework.py in append_op(self, *args, **kwargs)
2489 kwargs.get("outputs", {}), attrs
2490 if attrs else {},
-> 2491 kwargs.get("stop_gradient", False))
2492 else:
2493 op_desc = self.desc.append_op()
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/dygraph/tracer.py in trace_op(self, type, inputs, outputs, attrs, stop_gradient)
45 self.trace(type, inputs, outputs, attrs,
46 framework._current_expected_place(), self._train_mode and
---> 47 not stop_gradient)
48
49 def train_mode(self):
TypeError: trace(): incompatible function arguments. The following argument types are supported:
1. (self: paddle.fluid.core_avx.Tracer, arg0: str, arg1: Dict[str, handle], arg2: Dict[str, handle], arg3: Dict[str, Variant], arg4: paddle::platform::CUDAPlace, arg5: bool) -> None
2. (self: paddle.fluid.core_avx.Tracer, arg0: str, arg1: Dict[str, handle], arg2: Dict[str, handle], arg3: Dict[str, Variant], arg4: paddle::platform::CPUPlace, arg5: bool) -> None
Invoked with: <paddle.fluid.dygraph.tracer.Tracer object at 0x7f7d98554b90>, 'slice', {'Input': <paddle.fluid.core_avx.VarBase object at 0x7f7d985640f0>}, {'Out': <paddle.fluid.core_avx.VarBase object at 0x7f7d98564130>}, {'axes': [0, 1, 2], 'starts': [<paddle.fluid.core_avx.VarBase object at 0x7f7d985641f0>, 0, 2], 'ends': [<paddle.fluid.core_avx.VarBase object at 0x7f7d98564230>, 2, 4], 'infer_flags': [1, 1, 1]}, <paddle.fluid.core_avx.CPUPlace object at 0x7f7d98560970>, True
case 2 gets errors here
---------------------------------------------------------------------------
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-d61469efa372> in <module>
23 sliced = fluid.layers.slice(input, axes=[0, 1, 2],
24 starts=dg.to_variable(np.array([-3, 0, 2]).astype(np.int32)),
---> 25 ends=dg.to_variable(np.array([3, 2, 4]).astype(np.int32)))
26 print(sliced.numpy().shape)
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/layers/nn.py in slice(input, axes, starts, ends)
9818 dtype=helper.input_dtype('input'))
9819 helper.append_op(
-> 9820 type='slice', inputs=inputs, attrs=attrs, outputs={'Out': out})
9821
9822 return out
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/layer_helper.py in append_op(self, *args, **kwargs)
41
42 def append_op(self, *args, **kwargs):
---> 43 return self.main_program.current_block().append_op(*args, **kwargs)
44
45 def multiple_input(self, input_param_name='input'):
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/framework.py in append_op(self, *args, **kwargs)
2489 kwargs.get("outputs", {}), attrs
2490 if attrs else {},
-> 2491 kwargs.get("stop_gradient", False))
2492 else:
2493 op_desc = self.desc.append_op()
/workspace/.pyenv/versions/3.7.4/envs/paddle_dev/lib/python3.7/site-packages/paddle/fluid/dygraph/tracer.py in trace_op(self, type, inputs, outputs, attrs, stop_gradient)
45 self.trace(type, inputs, outputs, attrs,
46 framework._current_expected_place(), self._train_mode and
---> 47 not stop_gradient)
48
49 def train_mode(self):
ValueError: vector::reserve