未验证 提交 ee203340 编写于 作者: N Nyakku Shigure 提交者: GitHub

[CodeStyle] format some dy2static unittests (#46463)

* [CodeStyle] reformat test_error

* update lineno

* remove test_error from yapf whitelist

* try format test_origin_info

* try to fix origin info test case

* remove future import

* remove test_origin_info from yapf excludes

* empty commit, test=document_fix

* empty commit
上级 84f7835d
...@@ -30,11 +30,6 @@ repos: ...@@ -30,11 +30,6 @@ repos:
hooks: hooks:
- id: yapf - id: yapf
files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$ 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 - repo: https://github.com/PyCQA/flake8
rev: 4.0.1 rev: 4.0.1
hooks: hooks:
......
...@@ -12,8 +12,6 @@ ...@@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
import os import os
import inspect import inspect
import unittest import unittest
...@@ -68,13 +66,13 @@ def func_decorated_by_other_2(): ...@@ -68,13 +66,13 @@ def func_decorated_by_other_2():
class LayerErrorInCompiletime(fluid.dygraph.Layer): class LayerErrorInCompiletime(fluid.dygraph.Layer):
def __init__(self, fc_size=20): def __init__(self, fc_size=20):
super(LayerErrorInCompiletime, self).__init__() super(LayerErrorInCompiletime, self).__init__()
self._linear = fluid.dygraph.Linear(fc_size, fc_size) self._linear = fluid.dygraph.Linear(fc_size, fc_size)
@paddle.jit.to_static( @paddle.jit.to_static(
input_spec=[paddle.static.InputSpec( input_spec=[paddle.static.InputSpec(shape=[20, 20], dtype='float32')])
shape=[20, 20], dtype='float32')])
def forward(self, x): def forward(self, x):
y = self._linear(x) y = self._linear(x)
z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int") z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")
...@@ -83,6 +81,7 @@ class LayerErrorInCompiletime(fluid.dygraph.Layer): ...@@ -83,6 +81,7 @@ class LayerErrorInCompiletime(fluid.dygraph.Layer):
class LayerErrorInCompiletime2(fluid.dygraph.Layer): class LayerErrorInCompiletime2(fluid.dygraph.Layer):
def __init__(self): def __init__(self):
super(LayerErrorInCompiletime2, self).__init__() super(LayerErrorInCompiletime2, self).__init__()
...@@ -109,6 +108,7 @@ def func_error_in_runtime_with_empty_line(x): ...@@ -109,6 +108,7 @@ def func_error_in_runtime_with_empty_line(x):
class SuggestionErrorTestNet(paddle.nn.Layer): class SuggestionErrorTestNet(paddle.nn.Layer):
def __init__(self): def __init__(self):
super(SuggestionErrorTestNet, self).__init__() super(SuggestionErrorTestNet, self).__init__()
self.inner_net = SuggestionErrorTestNet2() self.inner_net = SuggestionErrorTestNet2()
...@@ -119,6 +119,7 @@ class SuggestionErrorTestNet(paddle.nn.Layer): ...@@ -119,6 +119,7 @@ class SuggestionErrorTestNet(paddle.nn.Layer):
class SuggestionErrorTestNet2(): class SuggestionErrorTestNet2():
def __init__(self): def __init__(self):
super(SuggestionErrorTestNet2, self).__init__() super(SuggestionErrorTestNet2, self).__init__()
self.w = paddle.to_tensor([2.]) self.w = paddle.to_tensor([2.])
...@@ -134,6 +135,7 @@ def func_suggestion_error_in_runtime(x): ...@@ -134,6 +135,7 @@ def func_suggestion_error_in_runtime(x):
class TestFlags(unittest.TestCase): class TestFlags(unittest.TestCase):
def setUp(self): def setUp(self):
self.reset_flags_to_default() self.reset_flags_to_default()
...@@ -165,6 +167,7 @@ class TestFlags(unittest.TestCase): ...@@ -165,6 +167,7 @@ class TestFlags(unittest.TestCase):
class TestErrorBase(unittest.TestCase): class TestErrorBase(unittest.TestCase):
def setUp(self): def setUp(self):
self.set_input() self.set_input()
self.set_func() self.set_func()
...@@ -239,6 +242,7 @@ class TestErrorBase(unittest.TestCase): ...@@ -239,6 +242,7 @@ class TestErrorBase(unittest.TestCase):
# Situation 1: Call StaticLayer.__call__ to use Dynamic-to-Static # Situation 1: Call StaticLayer.__call__ to use Dynamic-to-Static
class TestErrorStaticLayerCallInCompiletime(TestErrorBase): class TestErrorStaticLayerCallInCompiletime(TestErrorBase):
def set_func(self): def set_func(self):
self.func = func_error_in_compile_time self.func = func_error_in_compile_time
...@@ -249,15 +253,16 @@ class TestErrorStaticLayerCallInCompiletime(TestErrorBase): ...@@ -249,15 +253,16 @@ class TestErrorStaticLayerCallInCompiletime(TestErrorBase):
self.exception_type = TypeError self.exception_type = TypeError
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
['File "{}", line 35, in func_error_in_compile_time'.format(self.filepath), 'File "{}", line 33, in func_error_in_compile_time'.format(
'inner_func()', self.filepath),
'File "{}", line 28, in inner_func'.format(self.filepath), 'inner_func()',
'def inner_func():', 'File "{}", line 26, in inner_func'.format(self.filepath),
'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', 'def inner_func():',
'<--- HERE', 'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")',
'return', '<--- HERE',
] 'return',
]
def set_func_call(self): def set_func_call(self):
# NOTE: self.func(self.input) is the StaticLayer().__call__(self.input) # NOTE: self.func(self.input) is the StaticLayer().__call__(self.input)
...@@ -270,6 +275,7 @@ class TestErrorStaticLayerCallInCompiletime(TestErrorBase): ...@@ -270,6 +275,7 @@ class TestErrorStaticLayerCallInCompiletime(TestErrorBase):
class TestErrorStaticLayerCallInCompiletime_2( class TestErrorStaticLayerCallInCompiletime_2(
TestErrorStaticLayerCallInCompiletime): TestErrorStaticLayerCallInCompiletime):
def set_func(self): def set_func(self):
self.func = func_error_in_compile_time_2 self.func = func_error_in_compile_time_2
...@@ -277,19 +283,20 @@ class TestErrorStaticLayerCallInCompiletime_2( ...@@ -277,19 +283,20 @@ class TestErrorStaticLayerCallInCompiletime_2(
self.exception_type = ValueError self.exception_type = ValueError
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
[ 'File "{}", line 44, in func_error_in_compile_time_2'.format(
'File "{}", line 46, in func_error_in_compile_time_2'.format(self.filepath), self.filepath),
'def func_error_in_compile_time_2(x):', 'def func_error_in_compile_time_2(x):',
'x = fluid.dygraph.to_variable(x)', 'x = fluid.dygraph.to_variable(x)',
'x = fluid.layers.reshape(x, shape=[1, 2])', 'x = fluid.layers.reshape(x, shape=[1, 2])',
'<--- HERE', '<--- HERE',
'return x' 'return x',
] ]
class TestErrorStaticLayerCallInCompiletime_3( class TestErrorStaticLayerCallInCompiletime_3(
TestErrorStaticLayerCallInCompiletime): TestErrorStaticLayerCallInCompiletime):
def setUp(self): def setUp(self):
self.reset_flags_to_default() self.reset_flags_to_default()
self.set_func_call() self.set_func_call()
...@@ -301,13 +308,13 @@ class TestErrorStaticLayerCallInCompiletime_3( ...@@ -301,13 +308,13 @@ class TestErrorStaticLayerCallInCompiletime_3(
self.exception_type = IndentationError self.exception_type = IndentationError
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
['File "{}", line 91, in forward'.format(self.filepath), 'File "{}", line 90, in forward'.format(self.filepath),
'@paddle.jit.to_static', '@paddle.jit.to_static',
'def forward(self):', 'def forward(self):',
'self.test_func()', 'self.test_func()',
'<--- HERE' '<--- HERE',
] ]
def set_func_call(self): def set_func_call(self):
layer = LayerErrorInCompiletime2() layer = LayerErrorInCompiletime2()
...@@ -318,6 +325,7 @@ class TestErrorStaticLayerCallInCompiletime_3( ...@@ -318,6 +325,7 @@ class TestErrorStaticLayerCallInCompiletime_3(
class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime): class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime):
def set_func(self): def set_func(self):
self.func = func_error_in_runtime self.func = func_error_in_runtime
...@@ -325,50 +333,58 @@ class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime): ...@@ -325,50 +333,58 @@ class TestErrorStaticLayerCallInRuntime(TestErrorStaticLayerCallInCompiletime):
self.exception_type = ValueError self.exception_type = ValueError
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
[ 'File "{}", line 52, in func_error_in_runtime'.format(
'File "{}", line 54, in func_error_in_runtime'.format(self.filepath), self.filepath),
'x = fluid.dygraph.to_variable(x)', 'x = fluid.dygraph.to_variable(x)',
'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")',
'x = fluid.layers.reshape(x, shape=[1, two])', 'x = fluid.layers.reshape(x, shape=[1, two])',
'<--- HERE', '<--- HERE',
'return x' 'return x',
] ]
class TestErrorStaticLayerCallInRuntime2(TestErrorStaticLayerCallInRuntime): class TestErrorStaticLayerCallInRuntime2(TestErrorStaticLayerCallInRuntime):
def set_func(self): def set_func(self):
self.func = func_error_in_runtime_with_empty_line self.func = func_error_in_runtime_with_empty_line
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
[ 'File "{}", line 105, in func_error_in_runtime_with_empty_line'.
'File "{}", line 106, in func_error_in_runtime_with_empty_line'.format(self.filepath), format(self.filepath),
'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")', 'two = fluid.layers.fill_constant(shape=[1], value=2, dtype="int32")',
'x = fluid.layers.reshape(x, shape=[1, two])', 'x = fluid.layers.reshape(x, shape=[1, two])',
'<--- HERE', '<--- HERE',
'return x' 'return x',
] ]
# Situation 2: Call ProgramTranslator().get_output(...) to use Dynamic-to-Static # Situation 2: Call ProgramTranslator().get_output(...) to use Dynamic-to-Static
class TestErrorGetOutputInCompiletime(TestErrorStaticLayerCallInCompiletime): class TestErrorGetOutputInCompiletime(TestErrorStaticLayerCallInCompiletime):
def set_func_call(self): 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( class TestErrorGetOutputInCompiletime_2(TestErrorStaticLayerCallInCompiletime_2
TestErrorStaticLayerCallInCompiletime_2): ):
def set_func_call(self): 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): class TestErrorGetOutputInRuntime(TestErrorStaticLayerCallInRuntime):
def set_func_call(self): 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): class TestJitSaveInCompiletime(TestErrorBase):
def setUp(self): def setUp(self):
self.reset_flags_to_default() self.reset_flags_to_default()
self.set_func_call() self.set_func_call()
...@@ -380,19 +396,20 @@ class TestJitSaveInCompiletime(TestErrorBase): ...@@ -380,19 +396,20 @@ class TestJitSaveInCompiletime(TestErrorBase):
self.exception_type = TypeError self.exception_type = TypeError
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
['File "{}", line 80, in forward'.format(self.filepath), 'File "{}", line 78, in forward'.format(self.filepath),
'def forward(self, x):', 'def forward(self, x):',
'y = self._linear(x)', 'y = self._linear(x)',
'z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', 'z = fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")',
'<--- HERE', '<--- HERE',
'out = paddle.mean(y[z])', 'out = paddle.mean(y[z])',
'return out' 'return out',
] ]
def set_func_call(self): def set_func_call(self):
layer = LayerErrorInCompiletime() 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): def test_error(self):
self._test_raise_new_exception() self._test_raise_new_exception()
...@@ -400,7 +417,9 @@ class TestJitSaveInCompiletime(TestErrorBase): ...@@ -400,7 +417,9 @@ class TestJitSaveInCompiletime(TestErrorBase):
# # Situation 4: NotImplementedError # # Situation 4: NotImplementedError
class TestSuggestionErrorInRuntime(TestErrorBase): class TestSuggestionErrorInRuntime(TestErrorBase):
def set_func(self): def set_func(self):
self.func = func_suggestion_error_in_runtime self.func = func_suggestion_error_in_runtime
...@@ -411,19 +430,18 @@ class TestSuggestionErrorInRuntime(TestErrorBase): ...@@ -411,19 +430,18 @@ class TestSuggestionErrorInRuntime(TestErrorBase):
self.exception_type = ValueError self.exception_type = ValueError
def set_message(self): def set_message(self):
self.expected_message = \ self.expected_message = [
[ 'File "{}", line 118, in forward'.format(self.filepath),
'File "{}", line 118, in forward'.format(self.filepath), 'return self.inner_net.forward(x)',
'return self.inner_net.forward(x)', 'File "{}", line 128, in forward'.format(self.filepath),
'File "{}", line 127, in forward'.format(self.filepath), 'def forward(self, x):',
'def forward(self, x):', 'out = paddle.matmul(self.w, x)',
'out = paddle.matmul(self.w, x)', '<--- HERE',
'<--- HERE', 'return out',
'return out', 'Revise suggestion:',
'Revise suggestion:', 'Please ensure all your sublayers are inheritted from nn.Layer.',
'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',
'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): def set_func_call(self):
# NOTE: self.func(self.input) is the StaticLayer().__call__(self.input) # NOTE: self.func(self.input) is the StaticLayer().__call__(self.input)
...@@ -433,15 +451,16 @@ class TestSuggestionErrorInRuntime(TestErrorBase): ...@@ -433,15 +451,16 @@ class TestSuggestionErrorInRuntime(TestErrorBase):
for disable_new_error in [0, 1]: for disable_new_error in [0, 1]:
self._test_raise_new_exception(disable_new_error) self._test_raise_new_exception(disable_new_error)
@paddle.jit.to_static @paddle.jit.to_static
def func_ker_error(x): def func_ker_error(x):
d = { d = {'x': x}
'x': x
}
y = d['y'] + x y = d['y'] + x
return y return y
class TestKeyError(unittest.TestCase): class TestKeyError(unittest.TestCase):
def test_key_error(self): def test_key_error(self):
paddle.disable_static() paddle.disable_static()
with self.assertRaises(error.Dy2StKeyError): with self.assertRaises(error.Dy2StKeyError):
...@@ -451,11 +470,13 @@ class TestKeyError(unittest.TestCase): ...@@ -451,11 +470,13 @@ class TestKeyError(unittest.TestCase):
@paddle.jit.to_static @paddle.jit.to_static
def NpApiErr(): def NpApiErr():
a = paddle.to_tensor([1,2]) a = paddle.to_tensor([1, 2])
b = np.sum(a.numpy()) b = np.sum(a.numpy())
print(b) print(b)
class TestNumpyApiErr(unittest.TestCase): class TestNumpyApiErr(unittest.TestCase):
def test_numpy_api_err(self): def test_numpy_api_err(self):
with self.assertRaises(TypeError) as e: with self.assertRaises(TypeError) as e:
NpApiErr() NpApiErr()
...@@ -467,10 +488,13 @@ class TestNumpyApiErr(unittest.TestCase): ...@@ -467,10 +488,13 @@ class TestNumpyApiErr(unittest.TestCase):
error_message = str(new_exception) 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): class test_set_state_dict_err_layer(paddle.nn.Layer):
def __init__(self): def __init__(self):
super(test_set_state_dict_err_layer, self).__init__() super(test_set_state_dict_err_layer, self).__init__()
self.linear = paddle.nn.Linear(5, 2) self.linear = paddle.nn.Linear(5, 2)
...@@ -490,10 +514,11 @@ class test_set_state_dict_err_layer(paddle.nn.Layer): ...@@ -490,10 +514,11 @@ class test_set_state_dict_err_layer(paddle.nn.Layer):
class TestSetStateDictErr(unittest.TestCase): class TestSetStateDictErr(unittest.TestCase):
def test_set_state_dict_err(self): def test_set_state_dict_err(self):
with self.assertRaises(ValueError) as e: with self.assertRaises(ValueError) as e:
layer = test_set_state_dict_err_layer() 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) y = layer(x)
new_exception = e.exception new_exception = e.exception
...@@ -503,7 +528,9 @@ class TestSetStateDictErr(unittest.TestCase): ...@@ -503,7 +528,9 @@ class TestSetStateDictErr(unittest.TestCase):
error_message = str(new_exception) 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__': if __name__ == '__main__':
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import sys import sys
import unittest import unittest
...@@ -28,6 +27,7 @@ def simple_func(x): ...@@ -28,6 +27,7 @@ def simple_func(x):
def nested_func(x): def nested_func(x):
def f1(a): def f1(a):
return a return a
...@@ -47,6 +47,7 @@ def decorated_func2(x): ...@@ -47,6 +47,7 @@ def decorated_func2(x):
class TestOriginInfo(unittest.TestCase): class TestOriginInfo(unittest.TestCase):
def setUp(self): def setUp(self):
self.set_test_func() self.set_test_func()
self.dygraph_func = unwrap(self.func) self.dygraph_func = unwrap(self.func)
...@@ -115,8 +116,8 @@ class TestOriginInfo(unittest.TestCase): ...@@ -115,8 +116,8 @@ class TestOriginInfo(unittest.TestCase):
origin_info = OriginInfo( origin_info = OriginInfo(
Location(self.dygraph_filepath, dy_lineno, dy_col_offset), Location(self.dygraph_filepath, dy_lineno, dy_col_offset),
self.dy_func_name[i], code) self.dy_func_name[i], code)
self.assertEqual( self.assertEqual(str(origin_info_map[staic_loc.line_location]),
str(origin_info_map[staic_loc.line_location]), str(origin_info)) str(origin_info))
def test_attach_origin_info(self): def test_attach_origin_info(self):
dygraph_ast = gast.parse(self.source_code) dygraph_ast = gast.parse(self.source_code)
...@@ -143,6 +144,7 @@ class TestOriginInfo(unittest.TestCase): ...@@ -143,6 +144,7 @@ class TestOriginInfo(unittest.TestCase):
class TestOriginInfoWithNestedFunc(TestOriginInfo): class TestOriginInfoWithNestedFunc(TestOriginInfo):
def set_test_func(self): def set_test_func(self):
self.func = nested_func self.func = nested_func
...@@ -152,7 +154,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo): ...@@ -152,7 +154,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo):
def set_dygraph_info(self): def set_dygraph_info(self):
self.line_num = 5 self.line_num = 5
self.line_index_list = [0, 1, 2, 3, 4] 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_abs_col_offset = [0, 4, 8, 4, 4]
self.dy_func_name = [self.dygraph_func.__name__] + \ self.dy_func_name = [self.dygraph_func.__name__] + \
["f1"] * 2 + \ ["f1"] * 2 + \
...@@ -168,6 +170,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo): ...@@ -168,6 +170,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo):
class TestOriginInfoWithDecoratedFunc(TestOriginInfo): class TestOriginInfoWithDecoratedFunc(TestOriginInfo):
def set_test_func(self): def set_test_func(self):
self.func = decorated_func self.func = decorated_func
...@@ -202,6 +205,7 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo): ...@@ -202,6 +205,7 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo):
class TestOriginInfoWithDecoratedFunc2(TestOriginInfo): class TestOriginInfoWithDecoratedFunc2(TestOriginInfo):
def set_test_func(self): def set_test_func(self):
self.func = decorated_func2 self.func = decorated_func2
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册