From 5931e855719772d97b851d63089c4968c23ed8af Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Wed, 9 Jun 2021 09:05:06 +0000 Subject: [PATCH 1/4] [Dy2stat] Change Some Fluid API to 2.0 API --- .../tests/unittests/dygraph_to_static/test_tensor_shape.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py index 70749c2e24447e..ace49db1073e26 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py @@ -29,10 +29,10 @@ def dyfunc_tensor_shape_1(x): def dyfunc_tensor_shape_2(x): - x = fluid.dygraph.to_variable(x) + x = paddle.to_tensor(x) shape = x.shape shape2 = shape - res = fluid.layers.reshape(x, shape2) + res = paddle.reshape(x, shape2) return res @@ -190,7 +190,7 @@ def dyfunc_with_while_3(x): def dyfunc_with_while_4(x): - x = fluid.dygraph.to_variable(x) + x = paddle.to_tensor(x) y = numpy.ones(5) y_shape_0 = y.shape[0] i = 1 From 2da06d77ac6ea5109f957824305727bf148c1196 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Thu, 10 Jun 2021 07:45:21 +0000 Subject: [PATCH 2/4] [Dy2stat] Add Support for `a, b = static_variable` Grammar --- .../dygraph_to_static/test_tensor_shape.py | 19 +++++++++++++++++++ python/paddle/fluid/variable_index.py | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py index ace49db1073e26..05ff2696397a3c 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py @@ -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. @@ -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.InputSec(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") diff --git a/python/paddle/fluid/variable_index.py b/python/paddle/fluid/variable_index.py index 242b5b14db2bcc..67850fb6886809 100644 --- a/python/paddle/fluid/variable_index.py +++ b/python/paddle/fluid/variable_index.py @@ -103,6 +103,18 @@ 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] != -1: + if slice_item < 0 or 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 From 269d483217875a42d76f3160d31d8fc5692832a2 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Thu, 10 Jun 2021 07:56:39 +0000 Subject: [PATCH 3/4] Enrich shape condition --- python/paddle/fluid/variable_index.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/variable_index.py b/python/paddle/fluid/variable_index.py index 66b465e2e5b15a..4f04a4ee3f2fb9 100644 --- a/python/paddle/fluid/variable_index.py +++ b/python/paddle/fluid/variable_index.py @@ -116,7 +116,8 @@ 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] != -1: + if isinstance(slice_item, int) and var.shape[ + dim] is not None and var.shape[dim] != -1: if slice_item < 0 or slice_item >= var.shape[dim]: # For python, if users write a, b = var, the __getitem__ # method will iterate through 0, 1, 2 ... until __getitem__ From f63f669adc12c399213dbe70d65f90f483122983 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Thu, 10 Jun 2021 08:52:45 +0000 Subject: [PATCH 4/4] Fix unittest --- .../dygraph_to_static/test_tensor_shape.py | 2 +- python/paddle/fluid/variable_index.py | 26 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py index 05ff2696397a3c..f7cdb12a1ab673 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py @@ -347,7 +347,7 @@ def _set_expected_op_num(self): class TestTupleShape3(TestTensorShapeBasic): def init_test_func(self): self.input = numpy.ones((5, 7)).astype("int32") - self.input_spec = [paddle.static.InputSec(shape=[5, 7], dtype="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): diff --git a/python/paddle/fluid/variable_index.py b/python/paddle/fluid/variable_index.py index 4f04a4ee3f2fb9..c9363dff13d81c 100644 --- a/python/paddle/fluid/variable_index.py +++ b/python/paddle/fluid/variable_index.py @@ -116,19 +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] != -1: - if slice_item < 0 or 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])) + 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