From a77a554d66a0fd9bd2a3e2aa8fca3f5f7f6e94e8 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 23 Sep 2022 21:34:23 +0800 Subject: [PATCH 1/9] [CodeStyle] reformat test_error --- .../unittests/dygraph_to_static/test_error.py | 189 ++++++++++-------- 1 file changed, 108 insertions(+), 81 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index c0ca1c1af0235..6910e1b77ed8b 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import os import inspect import unittest @@ -68,13 +66,13 @@ def func_decorated_by_other_2(): class LayerErrorInCompiletime(fluid.dygraph.Layer): + def __init__(self, fc_size=20): super(LayerErrorInCompiletime, self).__init__() self._linear = fluid.dygraph.Linear(fc_size, fc_size) @paddle.jit.to_static( - input_spec=[paddle.static.InputSpec( - shape=[20, 20], dtype='float32')]) + input_spec=[paddle.static.InputSpec(shape=[20, 20], dtype='float32')]) def forward(self, x): y = self._linear(x) z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int") @@ -83,6 +81,7 @@ def forward(self, x): class LayerErrorInCompiletime2(fluid.dygraph.Layer): + def __init__(self): super(LayerErrorInCompiletime2, self).__init__() @@ -109,6 +108,7 @@ def func_error_in_runtime_with_empty_line(x): class SuggestionErrorTestNet(paddle.nn.Layer): + def __init__(self): super(SuggestionErrorTestNet, self).__init__() self.inner_net = SuggestionErrorTestNet2() @@ -119,6 +119,7 @@ def forward(self, x): class SuggestionErrorTestNet2(): + def __init__(self): super(SuggestionErrorTestNet2, self).__init__() self.w = paddle.to_tensor([2.]) @@ -134,6 +135,7 @@ def func_suggestion_error_in_runtime(x): class TestFlags(unittest.TestCase): + def setUp(self): self.reset_flags_to_default() @@ -165,6 +167,7 @@ def test_translator_simplify_new_error(self): class TestErrorBase(unittest.TestCase): + def setUp(self): self.set_input() self.set_func() @@ -239,6 +242,7 @@ def _test_raise_new_exception(self, disable_new_error=0): # Situation 1: Call StaticLayer.__call__ to use Dynamic-to-Static class TestErrorStaticLayerCallInCompiletime(TestErrorBase): + def set_func(self): self.func = func_error_in_compile_time @@ -249,15 +253,16 @@ def set_exception_type(self): self.exception_type = TypeError def set_message(self): - self.expected_message = \ - ['File "{}", line 35, in func_error_in_compile_time'.format(self.filepath), - 'inner_func()', - 'File "{}", line 28, in inner_func'.format(self.filepath), - 'def inner_func():', - 'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', - '<--- HERE', - 'return', - ] + self.expected_message = [ + 'File "{}", line 35, in func_error_in_compile_time'.format( + self.filepath), + 'inner_func()', + 'File "{}", line 28, in inner_func'.format(self.filepath), + 'def inner_func():', + 'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', + '<--- HERE', + 'return', + ] def set_func_call(self): # NOTE: self.func(self.input) is the StaticLayer().__call__(self.input) @@ -270,6 +275,7 @@ def test_error(self): class TestErrorStaticLayerCallInCompiletime_2( TestErrorStaticLayerCallInCompiletime): + def set_func(self): self.func = func_error_in_compile_time_2 @@ -277,19 +283,20 @@ def set_exception_type(self): self.exception_type = ValueError def set_message(self): - self.expected_message = \ - [ - 'File "{}", line 46, in func_error_in_compile_time_2'.format(self.filepath), - 'def func_error_in_compile_time_2(x):', - 'x = fluid.dygraph.to_variable(x)', - 'x = fluid.layers.reshape(x, shape=[1, 2])', - '<--- HERE', - 'return x' - ] + self.expected_message = [ + 'File "{}", line 46, in func_error_in_compile_time_2'.format( + self.filepath), + 'def func_error_in_compile_time_2(x):', + 'x = fluid.dygraph.to_variable(x)', + 'x = fluid.layers.reshape(x, shape=[1, 2])', + '<--- HERE', + 'return x', + ] class TestErrorStaticLayerCallInCompiletime_3( TestErrorStaticLayerCallInCompiletime): + def setUp(self): self.reset_flags_to_default() self.set_func_call() @@ -301,13 +308,13 @@ def set_exception_type(self): self.exception_type = IndentationError def set_message(self): - self.expected_message = \ - ['File "{}", line 91, in forward'.format(self.filepath), - '@paddle.jit.to_static', - 'def forward(self):', - 'self.test_func()', - '<--- HERE' - ] + self.expected_message = [ + 'File "{}", line 91, in forward'.format(self.filepath), + '@paddle.jit.to_static', + 'def forward(self):', + 'self.test_func()', + '<--- HERE', + ] def set_func_call(self): layer = LayerErrorInCompiletime2() @@ -318,6 +325,7 @@ def test_error(self): class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime): + def set_func(self): self.func = func_error_in_runtime @@ -325,50 +333,58 @@ def set_exception_type(self): self.exception_type = ValueError def set_message(self): - self.expected_message = \ - [ - 'File "{}", line 54, in func_error_in_runtime'.format(self.filepath), - 'x = fluid.dygraph.to_variable(x)', - 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', - 'x = fluid.layers.reshape(x, shape=[1, two])', - '<--- HERE', - 'return x' - ] + self.expected_message = [ + 'File "{}", line 54, in func_error_in_runtime'.format( + self.filepath), + 'x = fluid.dygraph.to_variable(x)', + 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', + 'x = fluid.layers.reshape(x, shape=[1, two])', + '<--- HERE', + 'return x', + ] class TestErrorStaticLayerCallInRuntime2(TestErrorStaticLayerCallInRuntime): + def set_func(self): self.func = func_error_in_runtime_with_empty_line def set_message(self): - self.expected_message = \ - [ - 'File "{}", line 106, in func_error_in_runtime_with_empty_line'.format(self.filepath), - 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', - 'x = fluid.layers.reshape(x, shape=[1, two])', - '<--- HERE', - 'return x' - ] + self.expected_message = [ + 'File "{}", line 106, in func_error_in_runtime_with_empty_line'. + format(self.filepath), + 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', + 'x = fluid.layers.reshape(x, shape=[1, two])', + '<--- HERE', + 'return x', + ] # Situation 2: Call ProgramTranslator().get_output(...) to use Dynamic-to-Static class TestErrorGetOutputInCompiletime(TestErrorStaticLayerCallInCompiletime): + def set_func_call(self): - self.func_call = lambda : self.prog_trans.get_output(unwrap(self.func), self.input) + self.func_call = lambda: self.prog_trans.get_output( + unwrap(self.func), self.input) -class TestErrorGetOutputInCompiletime_2( - TestErrorStaticLayerCallInCompiletime_2): +class TestErrorGetOutputInCompiletime_2(TestErrorStaticLayerCallInCompiletime_2 + ): + def set_func_call(self): - self.func_call = lambda : self.prog_trans.get_output(unwrap(self.func), self.input) + self.func_call = lambda: self.prog_trans.get_output( + unwrap(self.func), self.input) class TestErrorGetOutputInRuntime(TestErrorStaticLayerCallInRuntime): + def set_func_call(self): - self.func_call = lambda : self.prog_trans.get_output(unwrap(self.func), self.input) + self.func_call = lambda: self.prog_trans.get_output( + unwrap(self.func), self.input) class TestJitSaveInCompiletime(TestErrorBase): + def setUp(self): self.reset_flags_to_default() self.set_func_call() @@ -380,19 +396,20 @@ def set_exception_type(self): self.exception_type = TypeError def set_message(self): - self.expected_message = \ - ['File "{}", line 80, in forward'.format(self.filepath), - 'def forward(self, x):', - 'y = self._linear(x)', - 'z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', - '<--- HERE', - 'out = paddle.mean(y[z])', - 'return out' - ] + self.expected_message = [ + 'File "{}", line 80, in forward'.format(self.filepath), + 'def forward(self, x):', + 'y = self._linear(x)', + 'z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', + '<--- HERE', + 'out = paddle.mean(y[z])', + 'return out', + ] def set_func_call(self): layer = LayerErrorInCompiletime() - self.func_call = lambda : paddle.jit.save(layer, path="./test_dy2stat_error/model") + self.func_call = lambda: paddle.jit.save( + layer, path="./test_dy2stat_error/model") def test_error(self): self._test_raise_new_exception() @@ -400,7 +417,9 @@ def test_error(self): # # Situation 4: NotImplementedError + class TestSuggestionErrorInRuntime(TestErrorBase): + def set_func(self): self.func = func_suggestion_error_in_runtime @@ -411,19 +430,18 @@ def set_exception_type(self): self.exception_type = ValueError def set_message(self): - self.expected_message = \ - [ - 'File "{}", line 118, in forward'.format(self.filepath), - 'return self.inner_net.forward(x)', - 'File "{}", line 127, in forward'.format(self.filepath), - 'def forward(self, x):', - 'out = paddle.matmul(self.w, x)', - '<--- HERE', - 'return out', - 'Revise suggestion:', - 'Please ensure all your sublayers are inheritted from nn.Layer.', - 'Please ensure there is no tensor created explicitly depended on external data, we suggest to register it as buffer tensor. See' - ] + self.expected_message = [ + 'File "{}", line 118, in forward'.format(self.filepath), + 'return self.inner_net.forward(x)', + 'File "{}", line 127, in forward'.format(self.filepath), + 'def forward(self, x):', + 'out = paddle.matmul(self.w, x)', + '<--- HERE', + 'return out', + 'Revise suggestion:', + 'Please ensure all your sublayers are inheritted from nn.Layer.', + 'Please ensure there is no tensor created explicitly depended on external data, we suggest to register it as buffer tensor. See', + ] def set_func_call(self): # NOTE: self.func(self.input) is the StaticLayer().__call__(self.input) @@ -433,15 +451,16 @@ def test_error(self): for disable_new_error in [0, 1]: self._test_raise_new_exception(disable_new_error) + @paddle.jit.to_static def func_ker_error(x): - d = { - 'x': x - } + d = {'x': x} y = d['y'] + x return y + class TestKeyError(unittest.TestCase): + def test_key_error(self): paddle.disable_static() with self.assertRaises(error.Dy2StKeyError): @@ -451,11 +470,13 @@ def test_key_error(self): @paddle.jit.to_static def NpApiErr(): - a = paddle.to_tensor([1,2]) + a = paddle.to_tensor([1, 2]) b = np.sum(a.numpy()) print(b) + class TestNumpyApiErr(unittest.TestCase): + def test_numpy_api_err(self): with self.assertRaises(TypeError) as e: NpApiErr() @@ -467,10 +488,13 @@ def test_numpy_api_err(self): error_message = str(new_exception) - self.assertIn("values will be changed to variables by dy2static, numpy api can not handle variables", error_message) + self.assertIn( + "values will be changed to variables by dy2static, numpy api can not handle variables", + error_message) class test_set_state_dict_err_layer(paddle.nn.Layer): + def __init__(self): super(test_set_state_dict_err_layer, self).__init__() self.linear = paddle.nn.Linear(5, 2) @@ -490,10 +514,11 @@ def forward(self, x): class TestSetStateDictErr(unittest.TestCase): + def test_set_state_dict_err(self): with self.assertRaises(ValueError) as e: layer = test_set_state_dict_err_layer() - x = paddle.to_tensor([1.,2.,3.,4.,5.]) + x = paddle.to_tensor([1., 2., 3., 4., 5.]) y = layer(x) new_exception = e.exception @@ -503,7 +528,9 @@ def test_set_state_dict_err(self): error_message = str(new_exception) - self.assertIn("This error might happens in dy2static, while calling 'set_state_dict' dynamicly in 'forward', which is not supported. If you only need call 'set_state_dict' once, move it to '__init__'.", error_message) + self.assertIn( + "This error might happens in dy2static, while calling 'set_state_dict' dynamicly in 'forward', which is not supported. If you only need call 'set_state_dict' once, move it to '__init__'.", + error_message) if __name__ == '__main__': From 404ac00f9b4f36f21974ea06a9d37b47bde21871 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 23 Sep 2022 23:51:39 +0800 Subject: [PATCH 2/9] update lineno --- .../unittests/dygraph_to_static/test_error.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index 6910e1b77ed8b..acd627c7fcf8f 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -254,10 +254,10 @@ def set_exception_type(self): def set_message(self): self.expected_message = [ - 'File "{}", line 35, in func_error_in_compile_time'.format( + 'File "{}", line 33, in func_error_in_compile_time'.format( self.filepath), 'inner_func()', - 'File "{}", line 28, in inner_func'.format(self.filepath), + 'File "{}", line 26, in inner_func'.format(self.filepath), 'def inner_func():', 'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', '<--- HERE', @@ -284,7 +284,7 @@ def set_exception_type(self): def set_message(self): self.expected_message = [ - 'File "{}", line 46, in func_error_in_compile_time_2'.format( + 'File "{}", line 44, in func_error_in_compile_time_2'.format( self.filepath), 'def func_error_in_compile_time_2(x):', 'x = fluid.dygraph.to_variable(x)', @@ -309,7 +309,7 @@ def set_exception_type(self): def set_message(self): self.expected_message = [ - 'File "{}", line 91, in forward'.format(self.filepath), + 'File "{}", line 90, in forward'.format(self.filepath), '@paddle.jit.to_static', 'def forward(self):', 'self.test_func()', @@ -334,7 +334,7 @@ def set_exception_type(self): def set_message(self): self.expected_message = [ - 'File "{}", line 54, in func_error_in_runtime'.format( + 'File "{}", line 52, in func_error_in_runtime'.format( self.filepath), 'x = fluid.dygraph.to_variable(x)', 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', @@ -351,7 +351,7 @@ def set_func(self): def set_message(self): self.expected_message = [ - 'File "{}", line 106, in func_error_in_runtime_with_empty_line'. + 'File "{}", line 105, in func_error_in_runtime_with_empty_line'. format(self.filepath), 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', 'x = fluid.layers.reshape(x, shape=[1, two])', @@ -397,7 +397,7 @@ def set_exception_type(self): def set_message(self): self.expected_message = [ - 'File "{}", line 80, in forward'.format(self.filepath), + 'File "{}", line 78, in forward'.format(self.filepath), 'def forward(self, x):', 'y = self._linear(x)', 'z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', @@ -433,7 +433,7 @@ def set_message(self): self.expected_message = [ 'File "{}", line 118, in forward'.format(self.filepath), 'return self.inner_net.forward(x)', - 'File "{}", line 127, in forward'.format(self.filepath), + 'File "{}", line 128, in forward'.format(self.filepath), 'def forward(self, x):', 'out = paddle.matmul(self.w, x)', '<--- HERE', From 3c2c2221386dae4efbdd3f1b44f86933b5e4a72c Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 24 Sep 2022 01:16:56 +0800 Subject: [PATCH 3/9] remove test_error from yapf whitelist --- .pre-commit-config.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 059630481e34e..d0f15cff9e770 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,6 @@ repos: files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$ exclude: | (?x)^( - python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py| python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py )$ - repo: https://github.com/PyCQA/flake8 From 1a7448b2ace7b7637ee9801c8339d0d8365f6b31 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 24 Sep 2022 01:18:48 +0800 Subject: [PATCH 4/9] try format test_origin_info --- .../unittests/dygraph_to_static/test_origin_info.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py index e7435922b1c60..1b90661e179e9 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py @@ -29,6 +29,7 @@ def simple_func(x): def nested_func(x): + def f1(a): return a @@ -48,6 +49,7 @@ def decorated_func2(x): class TestOriginInfo(unittest.TestCase): + def setUp(self): self.set_test_func() self.dygraph_func = unwrap(self.func) @@ -116,8 +118,8 @@ def test_origin_info_map(self): origin_info = OriginInfo( Location(self.dygraph_filepath, dy_lineno, dy_col_offset), self.dy_func_name[i], code) - self.assertEqual( - str(origin_info_map[staic_loc.line_location]), str(origin_info)) + self.assertEqual(str(origin_info_map[staic_loc.line_location]), + str(origin_info)) def test_attach_origin_info(self): dygraph_ast = gast.parse(self.source_code) @@ -144,6 +146,7 @@ def test_attach_origin_info(self): class TestOriginInfoWithNestedFunc(TestOriginInfo): + def set_test_func(self): self.func = nested_func @@ -169,6 +172,7 @@ def set_origin_info_list(self, dygraph_ast): class TestOriginInfoWithDecoratedFunc(TestOriginInfo): + def set_test_func(self): self.func = decorated_func @@ -203,6 +207,7 @@ def set_origin_info_list(self, dygraph_ast): class TestOriginInfoWithDecoratedFunc2(TestOriginInfo): + def set_test_func(self): self.func = decorated_func2 From 921f492b41f411fa3ed6af89dc99d31259326130 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 24 Sep 2022 02:40:54 +0800 Subject: [PATCH 5/9] try to fix origin info test case --- .../fluid/tests/unittests/dygraph_to_static/test_origin_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py index 1b90661e179e9..501f9d7d39ca6 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py @@ -156,7 +156,7 @@ def set_static_lineno(self): def set_dygraph_info(self): self.line_num = 5 self.line_index_list = [0, 1, 2, 3, 4] - self.dy_rel_lineno_list = [0, 1, 2, 4, 5] + self.dy_rel_lineno_list = [0, 2, 3, 5, 6] self.dy_abs_col_offset = [0, 4, 8, 4, 4] self.dy_func_name = [self.dygraph_func.__name__] + \ ["f1"] * 2 + \ From f8bd927e679b1d4626a66ba26e40e97042dbd01a Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 24 Sep 2022 03:41:08 +0800 Subject: [PATCH 6/9] remove future import --- .../fluid/tests/unittests/dygraph_to_static/test_origin_info.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py index 501f9d7d39ca6..07260c9c0b868 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import sys import unittest From d4835246d7eacdee227976ddb968cbf7b3a2f668 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 24 Sep 2022 03:42:02 +0800 Subject: [PATCH 7/9] remove test_origin_info from yapf excludes --- .pre-commit-config.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d0f15cff9e770..65e1c8675da0a 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,10 +17,6 @@ repos: hooks: - id: yapf files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$ - exclude: | - (?x)^( - python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py - )$ - repo: https://github.com/PyCQA/flake8 rev: 4.0.1 hooks: From 909ff35bf2f3d6e9eecb2adad89a5e3d3383e6f7 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 27 Sep 2022 18:12:03 +0800 Subject: [PATCH 8/9] empty commit, test=document_fix From 2f0c1403e47bf7cb7cde50cb2545e8b18a35b10b Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 27 Sep 2022 19:04:00 +0800 Subject: [PATCH 9/9] empty commit