From 10b97a205a764c8b3f843a1b63ce0a5ff6f48b89 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sun, 26 Nov 2023 22:09:25 +0800 Subject: [PATCH 01/13] add `test_legacy_and_pir_exe_and_pir_api` --- .../test_convert_operators.py | 53 ++- test/dygraph_to_static/test_declarative.py | 377 +++++++++--------- test/dygraph_to_static/test_fallback.py | 8 +- test/dygraph_to_static/test_place.py | 16 +- test/dygraph_to_static/test_yolov3.py | 149 ++++--- test/dygraph_to_static/yolov3.py | 2 - 6 files changed, 318 insertions(+), 287 deletions(-) diff --git a/test/dygraph_to_static/test_convert_operators.py b/test/dygraph_to_static/test_convert_operators.py index 05a6d4de9c7d9..7148e7db715f2 100644 --- a/test/dygraph_to_static/test_convert_operators.py +++ b/test/dygraph_to_static/test_convert_operators.py @@ -19,6 +19,7 @@ Dy2StTestBase, test_ast_only, test_legacy_and_pir, + test_legacy_and_pir_exe_and_pir_api, ) import paddle @@ -47,32 +48,32 @@ def forward(self): class TestConvertCall(Dy2StTestBase): # fallback mode will raise a InnerError, it's ok. @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_class_exception(self): - @paddle.jit.to_static def call_not_exist(): net = CallNotExist() return net() with self.assertRaises(AttributeError): - call_not_exist() + paddle.jit.to_static(call_not_exist()) - @paddle.jit.to_static def forward_not_exist(): return net() with self.assertRaises(AttributeError): - forward_not_exist() + paddle.jit.to_static(forward_not_exist)() + @test_legacy_and_pir_exe_and_pir_api def test_callable_list(self): - @paddle.jit.to_static def callable_list(x, y): callable_list = CallableList() return callable_list(x) + y - self.assertEqual(callable_list(1, 2), 3) + self.assertEqual(paddle.jit.to_static(callable_list)(1, 2), 3) class TestConvertShapeCompare(Dy2StTestBase): + @test_legacy_and_pir_exe_and_pir_api def test_non_variable(self): self.assertEqual( paddle.jit.dy2static.convert_shape_compare(1, "<", 2), True @@ -137,9 +138,9 @@ def error_func(): @test_legacy_and_pir def test_variable(self): paddle.enable_static() - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): x = paddle.static.data(name='x', shape=[3, 2], dtype='float32') y = paddle.static.data(name='y', shape=[3, 2], dtype='float32') self.assertEqual( @@ -198,7 +199,6 @@ class ShapeLayer(paddle.nn.Layer): def __init__(self): super().__init__() - @paddle.jit.to_static(input_spec=[paddle.static.InputSpec(shape=[None, 1])]) def forward(self, x): x = paddle.reshape(x, [-1, x.shape[1]]) bs = x.shape[0] # -1 @@ -209,21 +209,23 @@ def forward(self, x): class TestChooseShapeAttrOrApiWithLayer(Dy2StTestBase): - @test_legacy_and_pir + @test_legacy_and_pir_exe_and_pir_api def test_tensor_shape(self): x = paddle.zeros(shape=[4, 1], dtype='float32') - net = ShapeLayer() + net = paddle.jit.to_static( + function=ShapeLayer(), + input_spec=[paddle.static.InputSpec(shape=[None, 1])], + ) out = net(x) np.testing.assert_array_equal(out.numpy(), x.numpy()) class TestIfElseNoValue(Dy2StTestBase): - @test_legacy_and_pir + @test_legacy_and_pir_exe_and_pir_api def test_else_ret_none(self): input_x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) - @paddle.jit.to_static def with_common_value(x, use_cache=False): if use_cache: y = x + 1 @@ -234,7 +236,6 @@ def with_common_value(x, use_cache=False): z = x - 1 return None - @paddle.jit.to_static def without_common_value(x, use_cache=False): if use_cache: y = x + 1 @@ -244,16 +245,15 @@ def without_common_value(x, use_cache=False): c = x + 1 return None - out = with_common_value(input_x, False) + out = paddle.jit.to_static(with_common_value)(input_x, False) self.assertIsNone(out) - out = without_common_value(input_x, False) + out = paddle.jit.to_static(without_common_value)(input_x, False) self.assertIsNone(out) - @test_legacy_and_pir + @test_legacy_and_pir_exe_and_pir_api def test_else_ret_c(self): input_x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) - @paddle.jit.to_static def with_common_value(x, use_cache=False): if use_cache: y = x + 1 @@ -264,7 +264,6 @@ def with_common_value(x, use_cache=False): z = x - 1 return c - @paddle.jit.to_static def without_common_value(x, use_cache=False): if use_cache: y = x + 1 @@ -274,19 +273,18 @@ def without_common_value(x, use_cache=False): c = x + 1 return c - out = with_common_value(input_x, False) + out = paddle.jit.to_static(with_common_value)(input_x, False) self.assertListEqual(paddle.tolist(out), paddle.tolist(input_x + 1)) - out = without_common_value(input_x, False) + out = paddle.jit.to_static(without_common_value)(input_x, False) self.assertListEqual(paddle.tolist(out), paddle.tolist(input_x + 1)) - y, z = with_common_value(input_x, True) + y, z = paddle.jit.to_static(with_common_value)(input_x, True) self.assertListEqual(paddle.tolist(y), paddle.tolist(input_x + 1)) self.assertListEqual(paddle.tolist(z), paddle.tolist(input_x + 2)) - @test_legacy_and_pir + @test_legacy_and_pir_exe_and_pir_api def test_else_ret_cz(self): input_x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) - @paddle.jit.to_static def with_common_value(x, use_cache=False): if use_cache: y = x + 1 @@ -297,7 +295,6 @@ def with_common_value(x, use_cache=False): z = x - 1 return c, z - @paddle.jit.to_static def without_common_value(x, use_cache=False): if use_cache: y = x + 1 @@ -308,10 +305,10 @@ def without_common_value(x, use_cache=False): d = x - 1 return c, d - c, z = with_common_value(input_x, False) + c, z = paddle.jit.to_static(with_common_value)(input_x, False) self.assertListEqual(paddle.tolist(c), paddle.tolist(input_x + 1)) self.assertListEqual(paddle.tolist(z), paddle.tolist(input_x - 1)) - c, d = without_common_value(input_x, False) + c, d = paddle.jit.to_static(without_common_value)(input_x, False) self.assertListEqual(paddle.tolist(c), paddle.tolist(input_x + 1)) self.assertListEqual(paddle.tolist(d), paddle.tolist(input_x - 1)) diff --git a/test/dygraph_to_static/test_declarative.py b/test/dygraph_to_static/test_declarative.py index 7c6eac567641f..e1b750c160d4f 100644 --- a/test/dygraph_to_static/test_declarative.py +++ b/test/dygraph_to_static/test_declarative.py @@ -21,13 +21,12 @@ Dy2StTestBase, test_ast_only, test_legacy_and_pir, + test_legacy_and_pir_exe_and_pir_api, ) from test_basic_api_transformation import dyfunc_to_variable import paddle -from paddle import base from paddle.base.dygraph import to_variable -from paddle.jit.api import to_static from paddle.jit.dy2static.program_translator import ( ConcreteProgram, StaticFunction, @@ -41,15 +40,11 @@ def __init__(self): super().__init__() self.linear = paddle.nn.Linear(10, 3) - @to_static( - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) def forward(self, x, a=1, b=2): y = self.inner_function(x) return y - @to_static(full_graph=True) + @paddle.jit.to_static(full_graph=True) def inner_function(self, x): y = self.linear(x) return y @@ -58,7 +53,7 @@ def add_func(self, x, y): z = x + y return z - @to_static( + @paddle.jit.to_static( input_spec=[[InputSpec([None, 10]), InputSpec([None, 10])]], full_graph=True, ) @@ -68,7 +63,7 @@ def func_with_list(self, l, int_val=1): z = z + int_val return z - @to_static( + @paddle.jit.to_static( input_spec=[{'x': InputSpec([None, 10]), 'y': InputSpec([None, 10])}], full_graph=True, ) @@ -79,7 +74,7 @@ def func_with_dict(self, d): return z - @to_static( + @paddle.jit.to_static( input_spec=[ [ InputSpec([None]), @@ -100,20 +95,28 @@ def func_with_list_dict(self, dl): class TestStaticFunctionInstance(Dy2StTestBase): + @test_legacy_and_pir_exe_and_pir_api def test_instance_same_class(self): - with base.dygraph.guard(base.CPUPlace()): - net_1 = SimpleNet() - net_2 = SimpleNet() + net_1 = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) + net_2 = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) - self.assertTrue(isinstance(net_1.forward, StaticFunction)) - self.assertTrue(isinstance(net_2.forward, StaticFunction)) - self.assertNotEqual(net_1.forward, net_2.forward) + self.assertTrue(isinstance(net_1.forward, StaticFunction)) + self.assertTrue(isinstance(net_2.forward, StaticFunction)) + self.assertNotEqual(net_1.forward, net_2.forward) - # convert layer into static progam of net_1 - net_1.forward.concrete_program # noqa: B018 - self.assertTrue(len(net_1.forward.program_cache) == 1) - # check no conversion applid with net_2 - self.assertTrue(len(net_2.forward.program_cache) == 0) + # convert layer into static progam of net_1 + net_1.forward.concrete_program # noqa: B018 + self.assertTrue(len(net_1.forward.program_cache) == 1) + # check no conversion applid with net_2 + self.assertTrue(len(net_2.forward.program_cache) == 0) class TestInputSpec(Dy2StTestBase): @@ -127,94 +130,105 @@ def tearDown(self): @test_legacy_and_pir @test_ast_only def test_with_input_spec(self): - with base.dygraph.guard(base.CPUPlace()): - x = to_variable(np.ones([4, 10]).astype('float32')) - y = to_variable(np.ones([4, 10]).astype('float32') * 2) - int_val = 4.0 - - net = SimpleNet() + x = to_variable(np.ones([4, 10]).astype('float32')) + y = to_variable(np.ones([4, 10]).astype('float32') * 2) + int_val = 4.0 + + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) - # 1. each method holds independent program cache - out = net(x) - self.assertTrue(len(net.forward.program_cache) == 1) + # 1. each method holds independent program cache + out = net(x) + self.assertTrue(len(net.forward.program_cache) == 1) - # 2. test save load - net.inner_function(x) - paddle.jit.save(net, self.model_path) - infer_net = paddle.jit.load(self.model_path) - pred = infer_net(x) - np.testing.assert_allclose(out.numpy(), pred.numpy(), rtol=1e-05) + # 2. test save load + net.inner_function(x) + paddle.jit.save(net, self.model_path) + infer_net = paddle.jit.load(self.model_path) + pred = infer_net(x) + np.testing.assert_allclose(out.numpy(), pred.numpy(), rtol=1e-05) - # 3. we can decorate any method - x_2 = to_variable(np.ones([4, 20]).astype('float32')) - # uses `to_static(func)` instead of `@to_static` - net.add_func = to_static(net.add_func) - out = net.add_func(x_2, np.ones([20]).astype('float32')) - self.assertTrue(len(net.add_func.program_cache) == 1) + # 3. we can decorate any method + x_2 = to_variable(np.ones([4, 20]).astype('float32')) + # uses `to_static(func)` instead of `@to_static` + net.add_func = paddle.jit.to_static(net.add_func) + out = net.add_func(x_2, np.ones([20]).astype('float32')) + self.assertTrue(len(net.add_func.program_cache) == 1) - # 5. test input with list - out = net.func_with_list([x, y], int_val) + # 5. test input with list + out = net.func_with_list([x, y], int_val) - # 6. test input with dict - out = net.func_with_dict({'x': x, 'y': y}) + # 6. test input with dict + out = net.func_with_dict({'x': x, 'y': y}) - # 7. test input with lits contains dict - int_np = np.ones([1]).astype('float32') - out = net.func_with_list_dict([int_np, {'x': x, 'y': y}]) + # 7. test input with lits contains dict + int_np = np.ones([1]).astype('float32') + out = net.func_with_list_dict([int_np, {'x': x, 'y': y}]) + @test_legacy_and_pir_exe_and_pir_api def test_with_error(self): - with base.dygraph.guard(base.CPUPlace()): - x = to_variable(np.ones([4, 10]).astype('float32')) - y = to_variable(np.ones([4, 10]).astype('float32') * 2) - int_val = 4.0 - - net = SimpleNet() - - # 1. kwargs and input_spec should not be specificed in same time - with self.assertRaises(ValueError): - net(x, a=1, other_kwarg=2) - - # 2. requires len(input_spec) <= len(args) - with self.assertRaises(ValueError): - net.add_func = to_static( - net.add_func, - input_spec=[ - InputSpec([-1, 10]), - InputSpec([-1, 10]), - InputSpec([10]), - ], - ) - net.add_func(x, y) + x = to_variable(np.ones([4, 10]).astype('float32')) + y = to_variable(np.ones([4, 10]).astype('float32') * 2) + int_val = 4.0 + + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) - @test_ast_only - def test_concrete_program(self): - with base.dygraph.guard(base.CPUPlace()): - x = to_variable(np.ones([4, 10]).astype('float32')) - y = to_variable(np.ones([4, 10]).astype('float32') * 2) - int_val = 4.0 - - net = SimpleNet() - # We can get concrete_program by specificing InputSpec information. Faking input is no need. - net.add_func = to_static( - net.add_func, - input_spec=[InputSpec([-1, 10]), InputSpec([-1, 10], name='y')], - ) - cp1 = net.add_func.concrete_program - self.assertTrue(cp1.inputs[-1].shape == (-1, 10)) - self.assertTrue(cp1.inputs[-1].name == 'y') + # 1. kwargs and input_spec should not be specificed in same time + with self.assertRaises(ValueError): + net(x, a=1, other_kwarg=2) - # generate another program - net.add_func = to_static( + # 2. requires len(input_spec) <= len(args) + with self.assertRaises(ValueError): + net.add_func = paddle.jit.to_static( net.add_func, - input_spec=[InputSpec([10]), InputSpec([10], name='label')], + input_spec=[ + InputSpec([-1, 10]), + InputSpec([-1, 10]), + InputSpec([10]), + ], ) - cp2 = net.add_func.concrete_program - self.assertTrue(cp2.inputs[-1].shape == (10,)) - self.assertTrue(cp2.inputs[-1].name == 'label') - # Note(Aurelius84): New instance will be returned if we use `to_static(foo)` every time. - # So number of cache program is 1. - self.assertTrue(len(net.add_func.program_cache) == 1) - self.assertTrue(cp1 != cp2) + net.add_func(x, y) + + @test_ast_only + @test_legacy_and_pir + def test_concrete_program(self): + x = to_variable(np.ones([4, 10]).astype('float32')) + y = to_variable(np.ones([4, 10]).astype('float32') * 2) + int_val = 4.0 + + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) + # We can get concrete_program by specificing InputSpec information. Faking input is no need. + net.add_func = paddle.jit.to_static( + net.add_func, + input_spec=[InputSpec([-1, 10]), InputSpec([-1, 10], name='y')], + ) + cp1 = net.add_func.concrete_program + self.assertTrue(cp1.inputs[-1].shape == (-1, 10)) + self.assertTrue(cp1.inputs[-1].name == 'y') + + # generate another program + net.add_func = paddle.jit.to_static( + net.add_func, + input_spec=[InputSpec([10]), InputSpec([10], name='label')], + ) + cp2 = net.add_func.concrete_program + self.assertTrue(cp2.inputs[-1].shape == (10,)) + self.assertTrue(cp2.inputs[-1].name == 'label') + # Note(Aurelius84): New instance will be returned if we use `to_static(foo)` every time. + # So number of cache program is 1. + self.assertTrue(len(net.add_func.program_cache) == 1) + self.assertTrue(cp1 != cp2) def foo_func(a, b, c=1, d=2): @@ -226,56 +240,48 @@ class TestDifferentInputSpecCacheProgram(Dy2StTestBase): def setUp(self): paddle.jit.enable_to_static(True) - @test_legacy_and_pir + @test_legacy_and_pir_exe_and_pir_api @test_ast_only def test_with_different_input(self): - with base.dygraph.guard(base.CPUPlace()): - x_data = np.ones([16, 10]).astype('float32') - y_data = np.ones([10]).astype('float32') * 2 - z_data = np.ones([10]).astype('float32') * 2.2 + x_data = np.ones([16, 10]).astype('float32') + y_data = np.ones([10]).astype('float32') * 2 + z_data = np.ones([10]).astype('float32') * 2.2 - foo = to_static(foo_func) + foo = paddle.jit.to_static(foo_func) - # [16, 10] + [10] (varbase) - out_1 = foo(to_variable(x_data), to_variable(y_data)) - np.testing.assert_allclose( - x_data + y_data, out_1.numpy(), rtol=1e-05 - ) - self.assertTrue(len(foo.program_cache) == 1) - self.assertTrue(len(foo.program_cache.concrete_programs()) == 1) - first_program = foo.program_cache.last() - - # [16, 10] + [10] (numpy) - out_2 = foo(to_variable(x_data), y_data) - np.testing.assert_allclose( - x_data + y_data, out_2.numpy(), rtol=1e-05 - ) - self.assertTrue(len(foo.program_cache) == 1) + # [16, 10] + [10] (varbase) + out_1 = foo(paddle.to_tensor(x_data), paddle.to_tensor(y_data)) + np.testing.assert_allclose(x_data + y_data, out_1.numpy(), rtol=1e-05) + self.assertTrue(len(foo.program_cache) == 1) + self.assertTrue(len(foo.program_cache.concrete_programs()) == 1) + first_program = foo.program_cache.last() - # [16, 10] + [10] (numpy) - out_3 = foo(to_variable(x_data), z_data) - np.testing.assert_allclose( - x_data + z_data, out_3.numpy(), rtol=1e-05 - ) - # hit cache program - self.assertTrue(len(foo.program_cache) == 1) + # [16, 10] + [10] (numpy) + out_2 = foo(paddle.to_tensor(x_data), y_data) + np.testing.assert_allclose(x_data + y_data, out_2.numpy(), rtol=1e-05) + self.assertTrue(len(foo.program_cache) == 1) - # [16, 10] + [10] (numpy) with other different arguments (c=3) - out_4 = foo(to_variable(x_data), z_data, 3) - np.testing.assert_allclose( - x_data + z_data, out_4.numpy(), rtol=1e-05 - ) - # create a new program - self.assertTrue(len(foo.program_cache) == 2) + # [16, 10] + [10] (numpy) + out_3 = foo(paddle.to_tensor(x_data), z_data) + np.testing.assert_allclose(x_data + z_data, out_3.numpy(), rtol=1e-05) + # hit cache program + self.assertTrue(len(foo.program_cache) == 1) + + # [16, 10] + [10] (numpy) with other different arguments (c=3) + out_4 = foo(paddle.to_tensor(x_data), z_data, 3) + np.testing.assert_allclose(x_data + z_data, out_4.numpy(), rtol=1e-05) + # create a new program + self.assertTrue(len(foo.program_cache) == 2) - # test for recent program - foo(to_variable(x_data), y_data) - recent_program = foo.program_cache.last() - self.assertTrue(first_program == recent_program) + # test for recent program + foo(paddle.to_tensor(x_data), y_data) + recent_program = foo.program_cache.last() + self.assertTrue(first_program == recent_program) @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_get_concrete_program(self): - foo = to_static(foo_func) + foo = paddle.jit.to_static(foo_func) # 1. specific InputSpec for `x`/`y` concrete_program_1 = foo.get_concrete_program( @@ -314,38 +320,41 @@ def test_get_concrete_program(self): InputSpec([10]), InputSpec([10]), e=4 ) - @test_legacy_and_pir @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_concrete_program(self): - with base.dygraph.guard(base.CPUPlace()): - # usage 1 - foo_1 = paddle.jit.to_static( - foo_func, - input_spec=[ - InputSpec([10], name='x'), - InputSpec([10], name='y'), - ], - ) - self.assertTrue(isinstance(foo_1.concrete_program, ConcreteProgram)) + # usage 1 + foo_1 = paddle.jit.to_static( + foo_func, + input_spec=[ + InputSpec([10], name='x'), + InputSpec([10], name='y'), + ], + ) + self.assertTrue(isinstance(foo_1.concrete_program, ConcreteProgram)) - # usage 2 - foo_2 = paddle.jit.to_static(foo_func) - out = foo_2(paddle.rand([10]), paddle.rand([10])) - self.assertTrue(isinstance(foo_2.concrete_program, ConcreteProgram)) + # usage 2 + foo_2 = paddle.jit.to_static(foo_func) + out = foo_2(paddle.rand([10]), paddle.rand([10])) + self.assertTrue(isinstance(foo_2.concrete_program, ConcreteProgram)) - # raise error - foo_3 = paddle.jit.to_static(foo_func) - with self.assertRaises(ValueError): - foo_3.concrete_program # noqa: B018 + # raise error + foo_3 = paddle.jit.to_static(foo_func) + with self.assertRaises(ValueError): + foo_3.concrete_program # noqa: B018 class TestInputDefaultName(Dy2StTestBase): def setUp(self): paddle.disable_static() - self.net = SimpleNet() def assert_default_name(self, func_name, input_names): - decorated_func = getattr(self.net, func_name) + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) + decorated_func = getattr(net, func_name) spec_names = [x.name for x in decorated_func.inputs] self.assertListEqual(spec_names, input_names) @@ -366,7 +375,7 @@ def test_nest_input(self): class TestDeclarativeAPI(Dy2StTestBase): @test_ast_only def test_error(self): - func = to_static(dyfunc_to_variable) + func = paddle.jit.to_static(dyfunc_to_variable) paddle.enable_static() @@ -381,42 +390,57 @@ def test_error(self): # please use base.dygraph.guard() as context to run it in imperative Mode func(np.ones(5).astype("int32")) + paddle.disable_static() + class TestDecorateModelDirectly(Dy2StTestBase): def setUp(self): paddle.disable_static() paddle.jit.enable_to_static(True) - self.x = to_variable(np.ones([4, 10]).astype('float32')) + self.x = paddle.to_tensor(np.ones([4, 10]).astype('float32')) - @test_legacy_and_pir @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_fake_input(self): - net = SimpleNet() - net = to_static(net) + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) + net = paddle.jit.to_static(net) y = net(self.x) self.assertTrue(len(net.forward.program_cache) == 1) @test_ast_only def test_input_spec(self): - net = SimpleNet() - net = to_static(net, input_spec=[InputSpec([None, 8, 10])]) + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) + net = paddle.jit.to_static(net, input_spec=[InputSpec([None, 8, 10])]) self.assertTrue(len(net.forward.inputs) == 1) self.assertTrue(len(net.forward.program_cache) == 1) input_shape = net.forward.inputs[0].shape self.assertListEqual(list(input_shape), [-1, 8, 10]) # redecorate - net = to_static(net, input_spec=[InputSpec([None, 16, 10])]) + net = paddle.jit.to_static(net, input_spec=[InputSpec([None, 16, 10])]) input_shape = net.forward.inputs[0].shape self.assertListEqual(list(input_shape), [-1, 16, 10]) class TestErrorWithInitFromStaticMode(Dy2StTestBase): + @test_legacy_and_pir_exe_and_pir_api def test_raise_error(self): # disable imperative paddle.enable_static() - net = SimpleNet() + net = paddle.jit.to_static( + function=SimpleNet(), + input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + full_graph=True, + ) with self.assertRaisesRegex( RuntimeError, "only available in dynamic mode" ): @@ -432,13 +456,14 @@ def test_raise_error(self): ): net.forward.outputs # noqa: B018 + paddle.disable_static() + class CallNonForwardFuncNet(paddle.nn.Layer): def __init__(self): super().__init__() self.sub = CallNonForwardFuncSubNet() - @paddle.jit.to_static(full_graph=True) def forward(self): return self.sub.func() @@ -454,13 +479,12 @@ def func(self): class TestCallNonForwardFunc(Dy2StTestBase): - @test_legacy_and_pir + @test_legacy_and_pir_exe_and_pir_api def test_call_non_forward(self): paddle.disable_static() - net = CallNonForwardFuncNet() + net = paddle.jit.to_static(CallNonForwardFuncNet()) out = net() self.assertEqual(out.numpy().tolist(), [2, 4]) - paddle.enable_static() class SetBuffersNet1(paddle.nn.Layer): @@ -468,7 +492,6 @@ def __init__(self): super().__init__() self.a = paddle.to_tensor([1]) - @paddle.jit.to_static(full_graph=True) def forward(self): self.a = self.a + 1 return self.a @@ -479,7 +502,6 @@ def __init__(self): super().__init__() self.b = paddle.to_tensor([2]) - @paddle.jit.to_static(full_graph=True) def forward(self): self.b = None self.b = paddle.to_tensor([3]) @@ -496,20 +518,16 @@ def tearDown(self): @test_legacy_and_pir def test_set_buffers1(self): - paddle.disable_static() - net = SetBuffersNet1() + net = paddle.jit.to_static(SetBuffersNet1()) out = net() self.assertEqual(out.numpy().tolist(), [2]) paddle.jit.save(net, self.model_path) - paddle.enable_static() @test_ast_only def test_set_buffers2(self): - paddle.disable_static() - net = SetBuffersNet2() + net = paddle.jit.to_static(SetBuffersNet2()) with self.assertRaises(RuntimeError): out = net() - paddle.enable_static() class ClassNoInheritLayer: @@ -518,6 +536,7 @@ def func(self, x): class TestClassNoInheritLayer(Dy2StTestBase): + @test_legacy_and_pir_exe_and_pir_api def test_to_static(self): paddle.disable_static() net = ClassNoInheritLayer() diff --git a/test/dygraph_to_static/test_fallback.py b/test/dygraph_to_static/test_fallback.py index 9cfcc66b9fdc9..028d9e69e1b63 100644 --- a/test/dygraph_to_static/test_fallback.py +++ b/test/dygraph_to_static/test_fallback.py @@ -16,7 +16,11 @@ import unittest import numpy as np -from dygraph_to_static_utils_new import Dy2StTestBase, test_ast_only +from dygraph_to_static_utils_new import ( + Dy2StTestBase, + test_ast_only, + test_legacy_and_pir_exe_and_pir_api, +) import paddle @@ -58,6 +62,7 @@ def setUp(self): def tearDown(self): pass + @test_legacy_and_pir_exe_and_pir_api def test_case_support(self): output = paddle.jit.to_static(support_func)(self.x) np.testing.assert_allclose(output.numpy(), 4) @@ -112,6 +117,7 @@ def test_case_training(self): np.testing.assert_allclose(u_net(self.x).numpy(), [1, 1]) assert u_net.training is False, "Training must be false." + @test_legacy_and_pir_exe_and_pir_api def test_case_save_error(self): """ test the save will raise error. diff --git a/test/dygraph_to_static/test_place.py b/test/dygraph_to_static/test_place.py index f9aaca6932906..26c1a864580fc 100644 --- a/test/dygraph_to_static/test_place.py +++ b/test/dygraph_to_static/test_place.py @@ -13,17 +13,29 @@ # limitations under the License. import unittest +import warnings -from dygraph_to_static_utils_new import Dy2StTestBase +from dygraph_to_static_utils_new import ( + Dy2StTestBase, + test_legacy_and_pir_exe_and_pir_api, +) import paddle class TestPlace(Dy2StTestBase): + @test_legacy_and_pir_exe_and_pir_api def test_place(self): paddle.enable_static() x = paddle.to_tensor([1, 2, 3, 4]) - self.assertIsNone(x.place()) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + self.assertIsNone(x.place()) + self.assertTrue(len(w) == 1) + if paddle.framework.use_pir_api(): + self.assertTrue( + "OpResult do not have 'place'" in str(w[-1].message) + ) if __name__ == '__main__': diff --git a/test/dygraph_to_static/test_yolov3.py b/test/dygraph_to_static/test_yolov3.py index 5a848c3a92741..b6e1e4fa0535d 100644 --- a/test/dygraph_to_static/test_yolov3.py +++ b/test/dygraph_to_static/test_yolov3.py @@ -17,14 +17,15 @@ import unittest import numpy as np -from dygraph_to_static_utils_new import Dy2StTestBase, test_legacy_and_pir +from dygraph_to_static_utils_new import ( + Dy2StTestBase, + test_legacy_and_pir, +) from yolov3 import YOLOv3, cfg import paddle from paddle import base -from paddle.base.dygraph import to_variable -paddle.enable_static() random.seed(0) np.random.seed(0) @@ -83,86 +84,84 @@ def train(to_static): random.seed(0) np.random.seed(0) - place = base.CUDAPlace(0) if cfg.use_gpu else base.CPUPlace() - with base.dygraph.guard(place): - base.default_startup_program().random_seed = 1000 - base.default_main_program().random_seed = 1000 - model = YOLOv3(3, is_train=True) + base.default_startup_program().random_seed = 1000 + base.default_main_program().random_seed = 1000 + model = paddle.jit.to_static(YOLOv3(3, is_train=True)) + + boundaries = cfg.lr_steps + gamma = cfg.lr_gamma + step_num = len(cfg.lr_steps) + learning_rate = cfg.learning_rate + values = [learning_rate * (gamma**i) for i in range(step_num + 1)] + + lr = paddle.optimizer.lr.PiecewiseDecay( + boundaries=boundaries, values=values + ) + + lr = paddle.optimizer.lr.LinearWarmup( + learning_rate=lr, + warmup_steps=cfg.warm_up_iter, + start_lr=0.0, + end_lr=cfg.learning_rate, + ) + optimizer = paddle.optimizer.Momentum( + learning_rate=lr, + weight_decay=paddle.regularizer.L2Decay(cfg.weight_decay), + momentum=cfg.momentum, + parameters=model.parameters(), + ) + + start_time = time.time() + snapshot_loss = 0 + snapshot_time = 0 + total_sample = 0 + + input_size = cfg.input_size + shuffle = True + shuffle_seed = None + total_iter = cfg.max_iter + mixup_iter = total_iter - cfg.no_mixup_iter + + train_reader = FakeDataReader().reader() + + smoothed_loss = SmoothedValue() + ret = [] + for iter_id, data in enumerate(train_reader()): + prev_start_time = start_time + start_time = time.time() + img = np.array([x[0] for x in data]).astype('float32') + img = paddle.to_tensor(img) - boundaries = cfg.lr_steps - gamma = cfg.lr_gamma - step_num = len(cfg.lr_steps) - learning_rate = cfg.learning_rate - values = [learning_rate * (gamma**i) for i in range(step_num + 1)] + gt_box = np.array([x[1] for x in data]).astype('float32') + gt_box = paddle.to_tensor(gt_box) - lr = paddle.optimizer.lr.PiecewiseDecay( - boundaries=boundaries, values=values - ) + gt_label = np.array([x[2] for x in data]).astype('int32') + gt_label = paddle.to_tensor(gt_label) - lr = paddle.optimizer.lr.LinearWarmup( - learning_rate=lr, - warmup_steps=cfg.warm_up_iter, - start_lr=0.0, - end_lr=cfg.learning_rate, - ) - optimizer = paddle.optimizer.Momentum( - learning_rate=lr, - weight_decay=paddle.regularizer.L2Decay(cfg.weight_decay), - momentum=cfg.momentum, - parameters=model.parameters(), - ) + gt_score = np.array([x[3] for x in data]).astype('float32') + gt_score = paddle.to_tensor(gt_score) - start_time = time.time() - snapshot_loss = 0 - snapshot_time = 0 - total_sample = 0 - - input_size = cfg.input_size - shuffle = True - shuffle_seed = None - total_iter = cfg.max_iter - mixup_iter = total_iter - cfg.no_mixup_iter - - train_reader = FakeDataReader().reader() - - smoothed_loss = SmoothedValue() - ret = [] - for iter_id, data in enumerate(train_reader()): - prev_start_time = start_time - start_time = time.time() - img = np.array([x[0] for x in data]).astype('float32') - img = to_variable(img) - - gt_box = np.array([x[1] for x in data]).astype('float32') - gt_box = to_variable(gt_box) - - gt_label = np.array([x[2] for x in data]).astype('int32') - gt_label = to_variable(gt_label) - - gt_score = np.array([x[3] for x in data]).astype('float32') - gt_score = to_variable(gt_score) - - loss = model(img, gt_box, gt_label, gt_score, None, None) - smoothed_loss.add_value(np.mean(loss.numpy())) - snapshot_loss += loss.numpy() - snapshot_time += start_time - prev_start_time - total_sample += 1 - - print( - "Iter {:d}, loss {:.6f}, time {:.5f}".format( - iter_id, - smoothed_loss.get_mean_value(), - start_time - prev_start_time, - ) + loss = model(img, gt_box, gt_label, gt_score, None, None) + smoothed_loss.add_value(np.mean(loss.numpy())) + snapshot_loss += loss.numpy() + snapshot_time += start_time - prev_start_time + total_sample += 1 + + print( + "Iter {:d}, loss {:.6f}, time {:.5f}".format( + iter_id, + smoothed_loss.get_mean_value(), + start_time - prev_start_time, ) - ret.append(smoothed_loss.get_mean_value()) + ) + ret.append(smoothed_loss.get_mean_value()) - loss.backward() + loss.backward() - optimizer.minimize(loss) - model.clear_gradients() + optimizer.minimize(loss) + model.clear_gradients() - return np.array(ret) + return np.array(ret) class TestYolov3(Dy2StTestBase): diff --git a/test/dygraph_to_static/yolov3.py b/test/dygraph_to_static/yolov3.py index 8712a49b44a99..935623683abca 100644 --- a/test/dygraph_to_static/yolov3.py +++ b/test/dygraph_to_static/yolov3.py @@ -20,7 +20,6 @@ import paddle from paddle import _legacy_C_ops, base from paddle.base.param_attr import ParamAttr -from paddle.jit.api import to_static from paddle.regularizer import L2Decay @@ -274,7 +273,6 @@ def __init__(self, ch_in, is_train=True, use_random=False): self.route_blocks_2.append(route) self.upsample = Upsample() - @to_static def forward( self, inputs, From cb5daa86bbfbf838ba2ef5c6426b0ed7678fb81b Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 28 Nov 2023 15:44:21 +0000 Subject: [PATCH 02/13] update --- test/dygraph_to_static/test_declarative.py | 186 +++++++++------------ test/dygraph_to_static/test_fallback.py | 2 +- test/dygraph_to_static/test_place.py | 2 +- test/dygraph_to_static/test_yolov3.py | 2 +- 4 files changed, 86 insertions(+), 106 deletions(-) diff --git a/test/dygraph_to_static/test_declarative.py b/test/dygraph_to_static/test_declarative.py index 4b51e1adb4453..93e71bb7b45ca 100644 --- a/test/dygraph_to_static/test_declarative.py +++ b/test/dygraph_to_static/test_declarative.py @@ -20,7 +20,6 @@ from dygraph_to_static_utils import ( Dy2StTestBase, test_ast_only, - test_legacy_and_pt, test_legacy_and_pt_and_pir, ) from test_basic_api_transformation import dyfunc_to_variable @@ -35,78 +34,80 @@ from paddle.static import InputSpec -class SimpleNet(Layer): - def __init__(self): - super().__init__() - self.linear = paddle.nn.Linear(10, 3) - - def forward(self, x, a=1, b=2): - y = self.inner_function(x) - return y - - @paddle.jit.to_static(full_graph=True) - def inner_function(self, x): - y = self.linear(x) - return y - - def add_func(self, x, y): - z = x + y - return z - - @paddle.jit.to_static( - input_spec=[[InputSpec([None, 10]), InputSpec([None, 10])]], - full_graph=True, - ) - def func_with_list(self, l, int_val=1): - x, y = l - z = x + y - z = z + int_val - return z - - @paddle.jit.to_static( - input_spec=[{'x': InputSpec([None, 10]), 'y': InputSpec([None, 10])}], - full_graph=True, - ) - def func_with_dict(self, d): - x = d['x'] - y = d['y'] - z = x + y - - return z - - @paddle.jit.to_static( - input_spec=[ - [ - InputSpec([None]), - {'x': InputSpec([None, 10]), 'y': InputSpec([None, 10])}, - ] - ], - full_graph=True, - ) - def func_with_list_dict(self, dl): - bias = dl[0] - x = dl[1]['x'] - y = dl[1]['y'] - - z = x + y - z = z + bias - - return z +def create_simple_net(): + class SimpleNet(Layer): + def __init__(self): + super().__init__() + self.linear = paddle.nn.Linear(10, 3) - -class TestStaticFunctionInstance(Dy2StTestBase): - @test_legacy_and_pt_and_pir - def test_instance_same_class(self): - net_1 = paddle.jit.to_static( - function=SimpleNet(), + @paddle.jit.to_static( input_spec=[InputSpec(shape=[None, 10], dtype='float32')], full_graph=True, ) - net_2 = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], + def forward(self, x, a=1, b=2): + y = self.inner_function(x) + return y + + @paddle.jit.to_static(full_graph=True) + def inner_function(self, x): + y = self.linear(x) + return y + + def add_func(self, x, y): + z = x + y + return z + + @paddle.jit.to_static( + input_spec=[[InputSpec([None, 10]), InputSpec([None, 10])]], + full_graph=True, + ) + def func_with_list(self, l, int_val=1): + x, y = l + z = x + y + z = z + int_val + return z + + @paddle.jit.to_static( + input_spec=[ + {'x': InputSpec([None, 10]), 'y': InputSpec([None, 10])} + ], full_graph=True, ) + def func_with_dict(self, d): + x = d['x'] + y = d['y'] + z = x + y + + return z + + @paddle.jit.to_static( + input_spec=[ + [ + InputSpec([None]), + {'x': InputSpec([None, 10]), 'y': InputSpec([None, 10])}, + ] + ], + full_graph=True, + ) + def func_with_list_dict(self, dl): + bias = dl[0] + x = dl[1]['x'] + y = dl[1]['y'] + + z = x + y + z = z + bias + + return z + + return SimpleNet + + +class TestStaticFunctionInstance(Dy2StTestBase): + @test_legacy_and_pt_and_pir + def test_instance_same_class(self): + SimpleNet = create_simple_net() + net_1 = SimpleNet() + net_2 = SimpleNet() self.assertTrue(isinstance(net_1.forward, StaticFunction)) self.assertTrue(isinstance(net_2.forward, StaticFunction)) @@ -132,12 +133,9 @@ def test_with_input_spec(self): x = to_variable(np.ones([4, 10]).astype('float32')) y = to_variable(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 + SimpleNet = create_simple_net() - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + net = SimpleNet() # 1. each method holds independent program cache out = net(x) @@ -172,12 +170,9 @@ def test_with_error(self): x = to_variable(np.ones([4, 10]).astype('float32')) y = to_variable(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 + SimpleNet = create_simple_net() - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + net = SimpleNet() # 1. kwargs and input_spec should not be specificed in same time with self.assertRaises(ValueError): @@ -196,17 +191,13 @@ def test_with_error(self): net.add_func(x, y) @test_ast_only - @test_legacy_and_pt def test_concrete_program(self): x = to_variable(np.ones([4, 10]).astype('float32')) y = to_variable(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 + SimpleNet = create_simple_net() - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + net = SimpleNet() # We can get concrete_program by specificing InputSpec information. Faking input is no need. net.add_func = paddle.jit.to_static( net.add_func, @@ -348,11 +339,8 @@ def setUp(self): paddle.disable_static() def assert_default_name(self, func_name, input_names): - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + SimpleNet = create_simple_net() + net = SimpleNet() decorated_func = getattr(net, func_name) spec_names = [x.name for x in decorated_func.inputs] @@ -373,6 +361,7 @@ def test_nest_input(self): class TestDeclarativeAPI(Dy2StTestBase): @test_ast_only + @test_legacy_and_pt_and_pir def test_error(self): func = paddle.jit.to_static(dyfunc_to_variable) @@ -402,22 +391,16 @@ def setUp(self): @test_ast_only @test_legacy_and_pt_and_pir def test_fake_input(self): - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + SimpleNet = create_simple_net() + net = SimpleNet() net = paddle.jit.to_static(net) y = net(self.x) self.assertTrue(len(net.forward.program_cache) == 1) @test_ast_only def test_input_spec(self): - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + SimpleNet = create_simple_net() + net = SimpleNet() net = paddle.jit.to_static(net, input_spec=[InputSpec([None, 8, 10])]) self.assertTrue(len(net.forward.inputs) == 1) self.assertTrue(len(net.forward.program_cache) == 1) @@ -435,12 +418,9 @@ class TestErrorWithInitFromStaticMode(Dy2StTestBase): def test_raise_error(self): # disable imperative paddle.enable_static() + SimpleNet = create_simple_net() - net = paddle.jit.to_static( - function=SimpleNet(), - input_spec=[InputSpec(shape=[None, 10], dtype='float32')], - full_graph=True, - ) + net = SimpleNet() with self.assertRaisesRegex( RuntimeError, "only available in dynamic mode" ): diff --git a/test/dygraph_to_static/test_fallback.py b/test/dygraph_to_static/test_fallback.py index d69dde58586c8..25a89da29fc9c 100644 --- a/test/dygraph_to_static/test_fallback.py +++ b/test/dygraph_to_static/test_fallback.py @@ -16,7 +16,7 @@ import unittest import numpy as np -from dygraph_to_static_utils_new import ( +from dygraph_to_static_utils import ( Dy2StTestBase, test_ast_only, test_legacy_and_pt_and_pir, diff --git a/test/dygraph_to_static/test_place.py b/test/dygraph_to_static/test_place.py index 5d9c9e8d3c168..3b3c2c1bcc626 100644 --- a/test/dygraph_to_static/test_place.py +++ b/test/dygraph_to_static/test_place.py @@ -15,7 +15,7 @@ import unittest import warnings -from dygraph_to_static_utils_new import ( +from dygraph_to_static_utils import ( Dy2StTestBase, test_legacy_and_pt_and_pir, ) diff --git a/test/dygraph_to_static/test_yolov3.py b/test/dygraph_to_static/test_yolov3.py index f96a4ad1c257d..41480b67409d6 100644 --- a/test/dygraph_to_static/test_yolov3.py +++ b/test/dygraph_to_static/test_yolov3.py @@ -17,7 +17,7 @@ import unittest import numpy as np -from dygraph_to_static_utils_new import ( +from dygraph_to_static_utils import ( Dy2StTestBase, test_default_mode_only, ) From 96873f06f82359de0206d4b0cdf6333714434c5a Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 29 Nov 2023 08:52:24 +0000 Subject: [PATCH 03/13] add `test_tensor_memcpy_on_cpu` and gpu --- .../test_tensor_memcpy_on_cpu.py | 32 +++++++++-------- .../test_tensor_memcpy_on_gpu.py | 35 +++++++++---------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py b/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py index 542b2c30f17ca..7f317ebc2013e 100644 --- a/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py +++ b/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py @@ -15,26 +15,23 @@ import unittest import numpy as np -from dygraph_to_static_utils import Dy2StTestBase +from dygraph_to_static_utils import Dy2StTestBase, test_legacy_and_pt_and_pir import paddle -@paddle.jit.to_static def tensor_copy_to_cpu(x): x = paddle.to_tensor(x) y = x.cpu() return y -@paddle.jit.to_static def tensor_copy_to_cuda(x): x = paddle.to_tensor(x) y = x.cuda() return y -@paddle.jit.to_static def tensor_copy_to_cuda_with_warning(x, device_id=None, blocking=True): x = paddle.to_tensor(x) y = x.cuda(device_id, blocking) @@ -45,11 +42,12 @@ class TestTensorCopyToCpuOnDefaultCPU(Dy2StTestBase): def _run(self, to_static): paddle.jit.enable_to_static(to_static) x1 = paddle.ones([1, 2, 3]) - x2 = tensor_copy_to_cpu(x1) + x2 = paddle.jit.to_static(tensor_copy_to_cpu)(x1) return x1.place, x2.place, x2.numpy() + @test_legacy_and_pt_and_pir def test_tensor_cpu_on_default_cpu(self): - paddle.base.framework._set_expected_place(paddle.CPUPlace()) + paddle.framework._set_expected_place(paddle.CPUPlace()) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -65,11 +63,12 @@ class TestTensorCopyToCUDAOnDefaultCPU(Dy2StTestBase): def _run(self, to_static): paddle.jit.enable_to_static(to_static) x1 = paddle.ones([1, 2, 3]) - x2 = tensor_copy_to_cuda(x1) + x2 = paddle.jit.to_static(tensor_copy_to_cuda)(x1) return x1.place, x2.place, x2.numpy() + @test_legacy_and_pt_and_pir def test_tensor_cuda_on_default_cpu(self): - if not paddle.base.is_compiled_with_cuda(): + if not paddle.is_compiled_with_cuda(): return """ @@ -78,7 +77,7 @@ def test_tensor_cuda_on_default_cpu(self): whether is still taking effect or not. See ConstructDeviceContext() in interpreter_util.cc. """ - paddle.base.framework._set_expected_place(paddle.CPUPlace()) + paddle.framework._set_expected_place(paddle.CPUPlace()) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -94,30 +93,33 @@ class TestTensorCopyToCUDAWithWarningOnCPU(unittest.TestCase): def _run(self, to_static): paddle.jit.enable_to_static(to_static) x1 = paddle.ones([1, 2, 3]) - x2 = tensor_copy_to_cuda_with_warning(x1, device_id=1, blocking=False) + x2 = paddle.jit.to_static(tensor_copy_to_cuda_with_warning)( + x1, device_id=1, blocking=False + ) return x1.place, x2.place, x2.numpy() + @test_legacy_and_pt_and_pir def test_with_warning_on_cpu(self): - if not paddle.base.is_compiled_with_cuda(): + if not paddle.is_compiled_with_cuda(): return - paddle.base.framework._set_expected_place(paddle.CPUPlace()) + paddle.framework._set_expected_place(paddle.CPUPlace()) x1 = paddle.ones([1, 2, 3]) with self.assertWarns(UserWarning, msg="ignored") as cm: - x2 = tensor_copy_to_cuda_with_warning( + x2 = paddle.jit.to_static(tensor_copy_to_cuda_with_warning)( x1, device_id=1, blocking=True ) self.assertIn('math_op_patch.py', cm.filename) with self.assertWarns(UserWarning, msg="ignored") as cm: - x2 = tensor_copy_to_cuda_with_warning( + x2 = paddle.jit.to_static(tensor_copy_to_cuda_with_warning)( x1, device_id=None, blocking=False ) self.assertIn('math_op_patch.py', cm.filename) with self.assertWarns(UserWarning, msg="ignored") as cm: - x2 = tensor_copy_to_cuda_with_warning( + x2 = paddle.jit.to_static(tensor_copy_to_cuda_with_warning)( x1, device_id=2, blocking=False ) self.assertIn('math_op_patch.py', cm.filename) diff --git a/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py b/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py index 828c9874a4a25..8cb03e13681fc 100644 --- a/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py +++ b/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py @@ -16,7 +16,10 @@ import unittest import numpy as np -from dygraph_to_static_utils import Dy2StTestBase +from dygraph_to_static_utils import ( + Dy2StTestBase, + test_legacy_and_pt_and_pir, +) import paddle @@ -46,14 +49,12 @@ def _run(self, to_static): x2 = paddle.jit.to_static(tensor_copy_to_cpu)(x1) return x1.place, x2.place, x2.numpy() + @test_legacy_and_pt_and_pir def test_tensor_cpu_on_default_gpu(self): - if paddle.base.is_compiled_with_cuda(): - place = paddle.CUDAPlace( - int(os.environ.get('FLAGS_selected_gpus', 0)) - ) - else: + if not paddle.is_compiled_with_cuda(): return - paddle.base.framework._set_expected_place(place) + place = paddle.CUDAPlace(int(os.environ.get('FLAGS_selected_gpus', 0))) + paddle.framework._set_expected_place(place) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -72,14 +73,12 @@ def _run(self, to_static): x2 = paddle.jit.to_static(tensor_copy_to_cuda)(x1) return x1.place, x2.place, x2.numpy() + @test_legacy_and_pt_and_pir def test_tensor_cuda_on_default_gpu(self): - if paddle.is_compiled_with_cuda(): - place = paddle.CUDAPlace( - int(os.environ.get('FLAGS_selected_gpus', 0)) - ) - else: + if not paddle.is_compiled_with_cuda(): return - paddle.base.framework._set_expected_place(place) + place = paddle.CUDAPlace(int(os.environ.get('FLAGS_selected_gpus', 0))) + paddle.framework._set_expected_place(place) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -100,14 +99,12 @@ def _run(self, to_static): ) return x1.place, x2.place, x2.numpy() + @test_legacy_and_pt_and_pir def test_with_warning_on_gpu(self): - if paddle.base.is_compiled_with_cuda(): - place = paddle.CUDAPlace( - int(os.environ.get('FLAGS_selected_gpus', 0)) - ) - else: + if not paddle.is_compiled_with_cuda(): return - paddle.base.framework._set_expected_place(place) + place = paddle.CUDAPlace(int(os.environ.get('FLAGS_selected_gpus', 0))) + paddle.framework._set_expected_place(place) x1 = paddle.ones([1, 2, 3]) with self.assertWarns(UserWarning, msg="ignored") as cm: From 7f9e615b1131c801d03abee114db75bf5ef15505 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 29 Nov 2023 13:15:43 +0000 Subject: [PATCH 04/13] add debug info to yolov3 --- test/dygraph_to_static/darknet.py | 10 ++++++++++ test/dygraph_to_static/test_yolov3.py | 15 ++++++++++----- test/dygraph_to_static/yolov3.py | 16 ++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/test/dygraph_to_static/darknet.py b/test/dygraph_to_static/darknet.py index 7606407a18615..4a59eedeb16da 100644 --- a/test/dygraph_to_static/darknet.py +++ b/test/dygraph_to_static/darknet.py @@ -60,6 +60,12 @@ def __init__( self.act = act def forward(self, inputs): + # print(inputs) + # if inputs.shape[1] == 512 and self.conv._in_channels == 768: + # # self.conv + # # self.conv._in_channels + # breakpoint() + print(f"{inputs.shape[1]}", f"{str(self.conv)}") out = self.conv(inputs) out = self.batch_norm(out) if self.act == 'leaky': @@ -181,7 +187,11 @@ def forward(self, inputs): out = self.downsample0(out) blocks = [] for i, conv_block_i in enumerate(self.darknet53_conv_block_list): + # if paddle.base.dygraph.base.in_to_static_mode(): + # breakpoint() + print("DarkNet53_conv_body before", i, out.shape) out = conv_block_i(out) + print("DarkNet53_conv_body after (append this)", i, out.shape) blocks.append(out) if i < len(self.stages) - 1: out = self.downsample_list[i](out) diff --git a/test/dygraph_to_static/test_yolov3.py b/test/dygraph_to_static/test_yolov3.py index 41480b67409d6..93927c5878172 100644 --- a/test/dygraph_to_static/test_yolov3.py +++ b/test/dygraph_to_static/test_yolov3.py @@ -19,12 +19,12 @@ import numpy as np from dygraph_to_static_utils import ( Dy2StTestBase, - test_default_mode_only, + test_ast_only, + test_pir_only, ) from yolov3 import YOLOv3, cfg import paddle -from paddle import base random.seed(0) np.random.seed(0) @@ -84,8 +84,8 @@ def train(to_static): random.seed(0) np.random.seed(0) - base.default_startup_program().random_seed = 1000 - base.default_main_program().random_seed = 1000 + paddle.static.default_startup_program().random_seed = 1000 + paddle.static.default_main_program().random_seed = 1000 model = paddle.jit.to_static(YOLOv3(3, is_train=True)) boundaries = cfg.lr_steps @@ -165,9 +165,14 @@ def train(to_static): class TestYolov3(Dy2StTestBase): - @test_default_mode_only + # @test_default_mode_only + @test_ast_only + # @test_sot_only + @test_pir_only + # @test_legacy_only def test_dygraph_static_same_loss(self): dygraph_loss = train(to_static=False) + print("=== DIVIDE ===") static_loss = train(to_static=True) np.testing.assert_allclose( dygraph_loss, static_loss, rtol=0.001, atol=1e-05 diff --git a/test/dygraph_to_static/yolov3.py b/test/dygraph_to_static/yolov3.py index 935623683abca..98887c2da7381 100644 --- a/test/dygraph_to_static/yolov3.py +++ b/test/dygraph_to_static/yolov3.py @@ -18,8 +18,7 @@ from darknet import ConvBNLayer, DarkNet53_conv_body import paddle -from paddle import _legacy_C_ops, base -from paddle.base.param_attr import ParamAttr +from paddle import ParamAttr, _legacy_C_ops from paddle.regularizer import L2Decay @@ -106,11 +105,11 @@ def __setattr__(self, name, value): # derived learning rate the to get the final learning rate. cfg.learning_rate = 0.001 # maximum number of iterations -cfg.max_iter = 20 if base.is_compiled_with_cuda() else 1 +cfg.max_iter = 20 if paddle.is_compiled_with_cuda() else 1 # Disable mixup in last N iter -cfg.no_mixup_iter = 10 if base.is_compiled_with_cuda() else 1 +cfg.no_mixup_iter = 10 if paddle.is_compiled_with_cuda() else 1 # warm up to learning rate -cfg.warm_up_iter = 10 if base.is_compiled_with_cuda() else 1 +cfg.warm_up_iter = 10 if paddle.is_compiled_with_cuda() else 1 cfg.warm_up_factor = 0.0 # lr steps_with_decay cfg.lr_steps = [400000, 450000] @@ -123,7 +122,7 @@ def __setattr__(self, name, value): # ENV options # # support both CPU and GPU -cfg.use_gpu = base.is_compiled_with_cuda() +cfg.use_gpu = paddle.is_compiled_with_cuda() # Class number cfg.class_num = 80 @@ -289,15 +288,20 @@ def forward( self.downsample = 32 blocks = self.block(inputs) for i, block in enumerate(blocks): + print("block pre:", i, block.shape) if i > 0: block = paddle.concat([route, block], axis=1) # noqa: F821 + print("block:", i, block.shape) route, tip = self.yolo_blocks[i](block) + print("route 0:", i, route.shape) block_out = self.block_outputs[i](tip) self.outputs.append(block_out) if i < 2: route = self.route_blocks_2[i](route) + print("route 1:", i, route.shape) route = self.upsample(route) + print("route 2:", i, route.shape) self.gtbox = gtbox self.gtlabel = gtlabel self.gtscore = gtscore From 2355d4045db77cc9f59e96efd569bcd220222c43 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Thu, 30 Nov 2023 08:53:36 +0000 Subject: [PATCH 05/13] fix test_declarative.TestInputSpec --- paddle/fluid/pybind/pir.cc | 6 +- .../test_basic_api_transformation.py | 41 +++++++---- test/dygraph_to_static/test_declarative.py | 72 ++++++++++--------- 3 files changed, 69 insertions(+), 50 deletions(-) diff --git a/paddle/fluid/pybind/pir.cc b/paddle/fluid/pybind/pir.cc index 4e20ea655624a..cbe645e0f8ead 100644 --- a/paddle/fluid/pybind/pir.cc +++ b/paddle/fluid/pybind/pir.cc @@ -605,11 +605,13 @@ void BindValue(py::module *m) { [](Value self) { if (auto param_op = self.defining_op<::pir::ParameterOp>()) { return param_op.param_name(); + } else if (auto data_op = + self.defining_op()) { + return data_op.attribute("name").AsString(); } else { PADDLE_THROW(phi::errors::InvalidArgument( "Currently, we can only get name of Value that " - "is " - "persistable")); + "is persistable")); } }) .def_property( diff --git a/test/dygraph_to_static/test_basic_api_transformation.py b/test/dygraph_to_static/test_basic_api_transformation.py index 74a5b3d290b37..41b835d9bb26d 100644 --- a/test/dygraph_to_static/test_basic_api_transformation.py +++ b/test/dygraph_to_static/test_basic_api_transformation.py @@ -16,7 +16,11 @@ import unittest import numpy as np -from dygraph_to_static_utils import Dy2StTestBase, test_default_mode_only +from dygraph_to_static_utils import ( + Dy2StTestBase, + static_guard, + test_default_mode_only, +) import paddle from paddle import base, to_tensor @@ -31,7 +35,6 @@ # TODO(zhhsplendid): This test is old so that use a static graph style # mark it as TODO, to refactoring the code of this file. -paddle.enable_static() def dyfunc_to_variable(x): @@ -105,11 +108,12 @@ def get_static_output(self): @test_default_mode_only def test_transformed_static_result(self): - for func in self.test_funcs: - self.dygraph_func = func - dygraph_res = self.get_dygraph_output() - static_res = self.get_static_output() - np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05) + with static_guard(): + for func in self.test_funcs: + self.dygraph_func = func + dygraph_res = self.get_dygraph_output() + static_res = self.get_static_output() + np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05) # 1. test Apis that inherit from layers.Layer @@ -263,9 +267,10 @@ def get_static_output(self): @test_default_mode_only def test_transformed_static_result(self): - dygraph_res = self.get_dygraph_output() - static_res = self.get_static_output() - np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05) + with static_guard(): + dygraph_res = self.get_dygraph_output() + static_res = self.get_static_output() + np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05) class TestDygraphBasicApi_BilinearTensorProduct(TestDygraphBasicApi): @@ -421,9 +426,10 @@ def get_static_output(self): @test_default_mode_only def test_transformed_static_result(self): - dygraph_res = self.get_dygraph_output() - static_res = self.get_static_output() - np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05) + with static_guard(): + dygraph_res = self.get_dygraph_output() + static_res = self.get_static_output() + np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05) class TestDygraphBasicApi_ExponentialDecay(TestDygraphBasicApi_CosineDecay): @@ -548,8 +554,13 @@ def _get_static_ast_node(self): @test_default_mode_only def test_dygraph_api(self): - self.assertTrue(is_dygraph_api(self._get_dygraph_ast_node()) is True) - self.assertTrue(is_dygraph_api(self._get_static_ast_node()) is False) + with static_guard(): + self.assertTrue( + is_dygraph_api(self._get_dygraph_ast_node()) is True + ) + self.assertTrue( + is_dygraph_api(self._get_static_ast_node()) is False + ) if __name__ == '__main__': diff --git a/test/dygraph_to_static/test_declarative.py b/test/dygraph_to_static/test_declarative.py index 93e71bb7b45ca..0f489a6ccc263 100644 --- a/test/dygraph_to_static/test_declarative.py +++ b/test/dygraph_to_static/test_declarative.py @@ -25,7 +25,7 @@ from test_basic_api_transformation import dyfunc_to_variable import paddle -from paddle.base.dygraph import to_variable +from paddle.framework import use_pir_api from paddle.jit.dy2static.program_translator import ( ConcreteProgram, StaticFunction, @@ -129,9 +129,10 @@ def tearDown(self): self.temp_dir.cleanup() @test_ast_only + @test_legacy_and_pt_and_pir def test_with_input_spec(self): - x = to_variable(np.ones([4, 10]).astype('float32')) - y = to_variable(np.ones([4, 10]).astype('float32') * 2) + x = paddle.to_tensor(np.ones([4, 10]).astype('float32')) + y = paddle.to_tensor(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 SimpleNet = create_simple_net() @@ -143,33 +144,34 @@ def test_with_input_spec(self): # 2. test save load net.inner_function(x) - paddle.jit.save(net, self.model_path) - infer_net = paddle.jit.load(self.model_path) - pred = infer_net(x) - np.testing.assert_allclose(out.numpy(), pred.numpy(), rtol=1e-05) - - # 3. we can decorate any method - x_2 = to_variable(np.ones([4, 20]).astype('float32')) - # uses `to_static(func)` instead of `@to_static` - net.add_func = paddle.jit.to_static(net.add_func) - out = net.add_func(x_2, np.ones([20]).astype('float32')) - self.assertTrue(len(net.add_func.program_cache) == 1) - - # 5. test input with list - out = net.func_with_list([x, y], int_val) - - # 6. test input with dict - out = net.func_with_dict({'x': x, 'y': y}) - - # 7. test input with lits contains dict - int_np = np.ones([1]).astype('float32') - out = net.func_with_list_dict([int_np, {'x': x, 'y': y}]) + # TODO(pir-save-load): Fix this after we support save/load in PIR + if not use_pir_api(): + paddle.jit.save(net, self.model_path) + infer_net = paddle.jit.load(self.model_path) + pred = infer_net(x) + np.testing.assert_allclose(out.numpy(), pred.numpy(), rtol=1e-05) + + # 3. we can decorate any method + x_2 = paddle.to_tensor(np.ones([4, 20]).astype('float32')) + # uses `to_static(func)` instead of `@to_static` + net.add_func = paddle.jit.to_static(net.add_func) + out = net.add_func(x_2, np.ones([20]).astype('float32')) + self.assertTrue(len(net.add_func.program_cache) == 1) + + # 5. test input with list + out = net.func_with_list([x, y], int_val) + + # 6. test input with dict + out = net.func_with_dict({'x': x, 'y': y}) + + # 7. test input with lits contains dict + int_np = np.ones([1]).astype('float32') + out = net.func_with_list_dict([int_np, {'x': x, 'y': y}]) @test_legacy_and_pt_and_pir def test_with_error(self): - x = to_variable(np.ones([4, 10]).astype('float32')) - y = to_variable(np.ones([4, 10]).astype('float32') * 2) - int_val = 4.0 + x = paddle.to_tensor(np.ones([4, 10]).astype('float32')) + y = paddle.to_tensor(np.ones([4, 10]).astype('float32') * 2) SimpleNet = create_simple_net() net = SimpleNet() @@ -191,10 +193,8 @@ def test_with_error(self): net.add_func(x, y) @test_ast_only + @test_legacy_and_pt_and_pir def test_concrete_program(self): - x = to_variable(np.ones([4, 10]).astype('float32')) - y = to_variable(np.ones([4, 10]).astype('float32') * 2) - int_val = 4.0 SimpleNet = create_simple_net() net = SimpleNet() @@ -204,7 +204,10 @@ def test_concrete_program(self): input_spec=[InputSpec([-1, 10]), InputSpec([-1, 10], name='y')], ) cp1 = net.add_func.concrete_program - self.assertTrue(cp1.inputs[-1].shape == (-1, 10)) + if use_pir_api(): + self.assertTrue(cp1.inputs[-1].shape == [-1, 10]) + else: + self.assertTrue(cp1.inputs[-1].shape == (-1, 10)) self.assertTrue(cp1.inputs[-1].name == 'y') # generate another program @@ -213,7 +216,10 @@ def test_concrete_program(self): input_spec=[InputSpec([10]), InputSpec([10], name='label')], ) cp2 = net.add_func.concrete_program - self.assertTrue(cp2.inputs[-1].shape == (10,)) + if use_pir_api(): + self.assertTrue(cp2.inputs[-1].shape == [10]) + else: + self.assertTrue(cp2.inputs[-1].shape == (10,)) self.assertTrue(cp2.inputs[-1].name == 'label') # Note(Aurelius84): New instance will be returned if we use `to_static(foo)` every time. # So number of cache program is 1. @@ -230,8 +236,8 @@ class TestDifferentInputSpecCacheProgram(Dy2StTestBase): def setUp(self): paddle.jit.enable_to_static(True) - @test_legacy_and_pt_and_pir @test_ast_only + @test_legacy_and_pt_and_pir def test_with_different_input(self): x_data = np.ones([16, 10]).astype('float32') y_data = np.ones([10]).astype('float32') * 2 From fb96d84bf3ba17444eedae8581084c596644ec1f Mon Sep 17 00:00:00 2001 From: SigureMo Date: Thu, 30 Nov 2023 13:23:36 +0000 Subject: [PATCH 06/13] update yolov3 --- .../fluid/pir/dialect/op_generator/op_build_gen.py | 1 + python/paddle/nn/layer/layers.py | 14 ++++++++------ test/dygraph_to_static/darknet.py | 10 ---------- test/dygraph_to_static/test_yolov3.py | 12 +++++++----- test/dygraph_to_static/yolov3.py | 5 ----- 5 files changed, 16 insertions(+), 26 deletions(-) diff --git a/paddle/fluid/pir/dialect/op_generator/op_build_gen.py b/paddle/fluid/pir/dialect/op_generator/op_build_gen.py index 5edcf508ca218..6c2165940c8e1 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_build_gen.py @@ -24,6 +24,7 @@ 'StackInferMeta', 'Conv2dTransposeInferMeta', 'Conv2dFusionInferMeta', + 'InterpolateInferMeta', } _PREPARE_DATA_WITH_VECTOR_INT64_MTTABLE_ATTRIBUTE = {'FrobeniusNormOp'} diff --git a/python/paddle/nn/layer/layers.py b/python/paddle/nn/layer/layers.py index 052efe3122d56..ed2951d076d8d 100644 --- a/python/paddle/nn/layer/layers.py +++ b/python/paddle/nn/layer/layers.py @@ -1677,11 +1677,11 @@ def _remove_if_exist(*dicts): _remove_if_exist(self.__dict__, self._buffers, self._sub_layers) params[name] = value - elif isinstance(value, paddle.pir.OpResult) and value.persistable: - if params is None: - raise ValueError("super().__init__() should be called first") - _remove_if_exist(self.__dict__, self._buffers, self._sub_layers) - params[name] = value + # elif isinstance(value, paddle.pir.OpResult) and value.persistable: + # if params is None: + # raise ValueError("super().__init__() should be called first") + # _remove_if_exist(self.__dict__, self._buffers, self._sub_layers) + # params[name] = value elif params is not None and name in params: if value is not None: raise TypeError( @@ -1729,7 +1729,9 @@ def _remove_if_exist(*dicts): # Note(Aurelius84): In Dy2stat, the value of the Buffer may be modified in # decorated function, such as `self.buffer = new_tensor`. So we update its # value via `assign`. - if type(value) == framework.Variable: + if type(value) == framework.Variable or isinstance( + value, paddle.pir.OpResult + ): from paddle import assign # Note(zhhsplendid): the condition below happens in PaddleGan model, diff --git a/test/dygraph_to_static/darknet.py b/test/dygraph_to_static/darknet.py index 4a59eedeb16da..7606407a18615 100644 --- a/test/dygraph_to_static/darknet.py +++ b/test/dygraph_to_static/darknet.py @@ -60,12 +60,6 @@ def __init__( self.act = act def forward(self, inputs): - # print(inputs) - # if inputs.shape[1] == 512 and self.conv._in_channels == 768: - # # self.conv - # # self.conv._in_channels - # breakpoint() - print(f"{inputs.shape[1]}", f"{str(self.conv)}") out = self.conv(inputs) out = self.batch_norm(out) if self.act == 'leaky': @@ -187,11 +181,7 @@ def forward(self, inputs): out = self.downsample0(out) blocks = [] for i, conv_block_i in enumerate(self.darknet53_conv_block_list): - # if paddle.base.dygraph.base.in_to_static_mode(): - # breakpoint() - print("DarkNet53_conv_body before", i, out.shape) out = conv_block_i(out) - print("DarkNet53_conv_body after (append this)", i, out.shape) blocks.append(out) if i < len(self.stages) - 1: out = self.downsample_list[i](out) diff --git a/test/dygraph_to_static/test_yolov3.py b/test/dygraph_to_static/test_yolov3.py index 93927c5878172..8c5e79ede7388 100644 --- a/test/dygraph_to_static/test_yolov3.py +++ b/test/dygraph_to_static/test_yolov3.py @@ -130,7 +130,9 @@ def train(to_static): prev_start_time = start_time start_time = time.time() img = np.array([x[0] for x in data]).astype('float32') + # breakpoint() img = paddle.to_tensor(img) + # img = paddle.base.dygraph.to_variable(img) gt_box = np.array([x[1] for x in data]).astype('float32') gt_box = paddle.to_tensor(gt_box) @@ -171,12 +173,12 @@ class TestYolov3(Dy2StTestBase): @test_pir_only # @test_legacy_only def test_dygraph_static_same_loss(self): - dygraph_loss = train(to_static=False) - print("=== DIVIDE ===") + # dygraph_loss = train(to_static=False) + # print("=== DIVIDE ===") static_loss = train(to_static=True) - np.testing.assert_allclose( - dygraph_loss, static_loss, rtol=0.001, atol=1e-05 - ) + # np.testing.assert_allclose( + # dygraph_loss, static_loss, rtol=0.001, atol=1e-05 + # ) if __name__ == '__main__': diff --git a/test/dygraph_to_static/yolov3.py b/test/dygraph_to_static/yolov3.py index 98887c2da7381..657df303bc20c 100644 --- a/test/dygraph_to_static/yolov3.py +++ b/test/dygraph_to_static/yolov3.py @@ -288,20 +288,15 @@ def forward( self.downsample = 32 blocks = self.block(inputs) for i, block in enumerate(blocks): - print("block pre:", i, block.shape) if i > 0: block = paddle.concat([route, block], axis=1) # noqa: F821 - print("block:", i, block.shape) route, tip = self.yolo_blocks[i](block) - print("route 0:", i, route.shape) block_out = self.block_outputs[i](tip) self.outputs.append(block_out) if i < 2: route = self.route_blocks_2[i](route) - print("route 1:", i, route.shape) route = self.upsample(route) - print("route 2:", i, route.shape) self.gtbox = gtbox self.gtlabel = gtlabel self.gtscore = gtscore From b5331ac9de77889e270252f9d41db8b3454c74de Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 1 Dec 2023 03:32:32 +0000 Subject: [PATCH 07/13] judge params by name --- python/paddle/nn/layer/layers.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/python/paddle/nn/layer/layers.py b/python/paddle/nn/layer/layers.py index ed2951d076d8d..4f9a1159c4eb4 100644 --- a/python/paddle/nn/layer/layers.py +++ b/python/paddle/nn/layer/layers.py @@ -1677,11 +1677,14 @@ def _remove_if_exist(*dicts): _remove_if_exist(self.__dict__, self._buffers, self._sub_layers) params[name] = value - # elif isinstance(value, paddle.pir.OpResult) and value.persistable: - # if params is None: - # raise ValueError("super().__init__() should be called first") - # _remove_if_exist(self.__dict__, self._buffers, self._sub_layers) - # params[name] = value + elif ( + isinstance(value, paddle.pir.OpResult) + and value.get_defining_op().name() == 'builtin.parameter' + ): + if params is None: + raise ValueError("super().__init__() should be called first") + _remove_if_exist(self.__dict__, self._buffers, self._sub_layers) + params[name] = value elif params is not None and name in params: if value is not None: raise TypeError( From 1cd142fc87aeb4e21ef5b1d24b8206b27af17b77 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 1 Dec 2023 06:17:39 +0000 Subject: [PATCH 08/13] update test_declarative --- python/paddle/jit/dy2static/program_translator.py | 5 +++-- test/dygraph_to_static/test_declarative.py | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 26691bf06b0f0..514043e36527e 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -36,6 +36,7 @@ ) from paddle.framework import in_dynamic_mode, use_pir_api from paddle.nn.layer import layers +from paddle.pir import OpResult from paddle.utils import flatten, gast from . import error, logging_utils @@ -1032,7 +1033,7 @@ def inputs(self): inputs = [ var for var in flatten(concrete_program.inputs) - if isinstance(var, framework.Variable) + if isinstance(var, (framework.Variable, OpResult)) ] return inputs @@ -1046,7 +1047,7 @@ def outputs(self): outputs = [ var for var in flatten(concrete_program.outputs) - if isinstance(var, framework.Variable) + if isinstance(var, (framework.Variable, OpResult)) ] return outputs diff --git a/test/dygraph_to_static/test_declarative.py b/test/dygraph_to_static/test_declarative.py index 0f489a6ccc263..14a004ab2c111 100644 --- a/test/dygraph_to_static/test_declarative.py +++ b/test/dygraph_to_static/test_declarative.py @@ -404,6 +404,7 @@ def test_fake_input(self): self.assertTrue(len(net.forward.program_cache) == 1) @test_ast_only + @test_legacy_and_pt_and_pir def test_input_spec(self): SimpleNet = create_simple_net() net = SimpleNet() @@ -502,11 +503,14 @@ def setUp(self): def tearDown(self): self.temp_dir.cleanup() + @test_legacy_and_pt_and_pir def test_set_buffers1(self): net = paddle.jit.to_static(SetBuffersNet1()) out = net() self.assertEqual(out.numpy().tolist(), [2]) - paddle.jit.save(net, self.model_path) + # TODO(pir-save-load): Fix this after we support save/load in PIR + if not use_pir_api(): + paddle.jit.save(net, self.model_path) @test_ast_only def test_set_buffers2(self): From 7ef93a6d50930fc742f67bc95a85d7b35fd63b37 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 1 Dec 2023 06:24:07 +0000 Subject: [PATCH 09/13] restore test_yolov3 --- test/dygraph_to_static/test_yolov3.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/test/dygraph_to_static/test_yolov3.py b/test/dygraph_to_static/test_yolov3.py index 8c5e79ede7388..ab73503cbca9e 100644 --- a/test/dygraph_to_static/test_yolov3.py +++ b/test/dygraph_to_static/test_yolov3.py @@ -19,8 +19,7 @@ import numpy as np from dygraph_to_static_utils import ( Dy2StTestBase, - test_ast_only, - test_pir_only, + test_default_mode_only, ) from yolov3 import YOLOv3, cfg @@ -167,18 +166,13 @@ def train(to_static): class TestYolov3(Dy2StTestBase): - # @test_default_mode_only - @test_ast_only - # @test_sot_only - @test_pir_only - # @test_legacy_only + @test_default_mode_only def test_dygraph_static_same_loss(self): - # dygraph_loss = train(to_static=False) - # print("=== DIVIDE ===") + dygraph_loss = train(to_static=False) static_loss = train(to_static=True) - # np.testing.assert_allclose( - # dygraph_loss, static_loss, rtol=0.001, atol=1e-05 - # ) + np.testing.assert_allclose( + dygraph_loss, static_loss, rtol=0.001, atol=1e-05 + ) if __name__ == '__main__': From ec84cc6346a865f0fc507d50c0dbf98303fcce58 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Mon, 4 Dec 2023 10:28:34 +0800 Subject: [PATCH 10/13] fix place test --- test/dygraph_to_static/test_place.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dygraph_to_static/test_place.py b/test/dygraph_to_static/test_place.py index 3b3c2c1bcc626..e40aea701a013 100644 --- a/test/dygraph_to_static/test_place.py +++ b/test/dygraph_to_static/test_place.py @@ -34,7 +34,7 @@ def test_place(self): self.assertTrue(len(w) == 1) if paddle.framework.use_pir_api(): self.assertTrue( - "OpResult do not have 'place'" in str(w[-1].message) + "Value do not have 'place'" in str(w[-1].message) ) From e3190645ea81b7c11a53491241c90ed9b7d4a1e3 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Mon, 4 Dec 2023 11:27:21 +0800 Subject: [PATCH 11/13] `assertTrue` -> `assertIn` --- test/dygraph_to_static/test_place.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/dygraph_to_static/test_place.py b/test/dygraph_to_static/test_place.py index e40aea701a013..8ef18fc26441d 100644 --- a/test/dygraph_to_static/test_place.py +++ b/test/dygraph_to_static/test_place.py @@ -33,9 +33,7 @@ def test_place(self): self.assertIsNone(x.place()) self.assertTrue(len(w) == 1) if paddle.framework.use_pir_api(): - self.assertTrue( - "Value do not have 'place'" in str(w[-1].message) - ) + self.assertIn("Value do not have 'place'", str(w[-1].message)) if __name__ == '__main__': From 33498cd30c1ff67dd9727e8365489230f516835e Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 5 Dec 2023 10:28:56 +0800 Subject: [PATCH 12/13] revert test_tensor_memcpy_on_cpu --- test/dygraph_to_static/test_tensor_memcpy_on_cpu.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py b/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py index 7f317ebc2013e..d2e7a72ffb892 100644 --- a/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py +++ b/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py @@ -15,7 +15,11 @@ import unittest import numpy as np -from dygraph_to_static_utils import Dy2StTestBase, test_legacy_and_pt_and_pir +from dygraph_to_static_utils import ( + Dy2StTestBase, + test_legacy_and_pt, + test_legacy_and_pt_and_pir, +) import paddle @@ -66,7 +70,7 @@ def _run(self, to_static): x2 = paddle.jit.to_static(tensor_copy_to_cuda)(x1) return x1.place, x2.place, x2.numpy() - @test_legacy_and_pt_and_pir + @test_legacy_and_pt def test_tensor_cuda_on_default_cpu(self): if not paddle.is_compiled_with_cuda(): return From 49f0bcc879ad546504466da9418fa42a60eb1301 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 5 Dec 2023 09:08:00 +0000 Subject: [PATCH 13/13] skip api check gen for `assign_out_` --- paddle/fluid/pir/dialect/op_generator/api_gen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/pir/dialect/op_generator/api_gen.py b/paddle/fluid/pir/dialect/op_generator/api_gen.py index 0fd92a2a1142d..e25ad9704a2af 100644 --- a/paddle/fluid/pir/dialect/op_generator/api_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/api_gen.py @@ -555,7 +555,7 @@ def _gen_check_data_type(self, op_info, op_name): if ( op_name.endswith(('_grad', '_grad_', '_grad_dense', '_grad_sparse')) - or op_name in ["print", "hardshrink", "det"] + or op_name in ["print", "hardshrink", "det", "assign_out_"] or len(mapping_name_to_type) == 0 ): return ""