Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def dyfunc_tuple_shape_2(x):
return res


def dyfunc_tuple_shape_3(x):
x = paddle.to_tensor(x)
a, b = paddle.shape(x)
res = paddle.reshape(x, shape=(b, a))
return res


def dyfunc_paddle_shape_api(x):
x = paddle.to_tensor(x)
# paddle.shape will not be converted.
Expand Down Expand Up @@ -337,6 +344,18 @@ def _set_expected_op_num(self):
self.expected_slice_op_num = 2


class TestTupleShape3(TestTensorShapeBasic):
def init_test_func(self):
self.input = numpy.ones((5, 7)).astype("int32")
self.input_spec = [paddle.static.InputSpec(shape=[5, 7], dtype="int32")]
self.dygraph_func = dyfunc_tuple_shape_3

def _set_expected_op_num(self):
self.expected_op_num = 5
self.expected_shape_op_num = 1
self.expected_slice_op_num = 2


class TestPaddleShapeApi(TestTensorShapeBasic):
def init_test_func(self):
self.input = numpy.ones((5, 7)).astype("int32")
Expand Down
13 changes: 13 additions & 0 deletions python/paddle/fluid/variable_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ def _getitem_impl_(var, item):

for dim, slice_item in enumerate(item):
if is_integer_or_scalar_tensor(slice_item):
if isinstance(slice_item,
int) and var.shape[dim] is not None and var.shape[
dim] >= 0 and slice_item >= var.shape[dim]:
# For python, if users write a, b = var, the __getitem__
# method will iterate through 0, 1, 2 ... until __getitem__
# throws an IndexError, then stop. The var[0], var[1] will
# be given to a, b respectively. If more values are given,
# the unpack size would cause error.
#
# We raises IndexError here to support grammar like `a, b = var`
raise IndexError(
"slice_item %d at dim %d should be >= 0 and < var.shape[%d]: %d"
% (slice_item, dim, dim, var.shape[dim]))
decrease_axes.append(dim)
start = slice_item
step = 1
Expand Down