From 0f08960fdcda52f943b050acf38c3cfc38dbf9cb Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sat, 25 Nov 2023 08:16:37 +0000 Subject: [PATCH 1/4] add assert && dict && no_gradi --- test/dygraph_to_static/test_assert.py | 12 ++++-------- test/dygraph_to_static/test_dict.py | 11 ++++++++++- test/dygraph_to_static/test_no_gradient.py | 6 +++++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/test/dygraph_to_static/test_assert.py b/test/dygraph_to_static/test_assert.py index 2e5066b801e52..b0345e28cfc14 100644 --- a/test/dygraph_to_static/test_assert.py +++ b/test/dygraph_to_static/test_assert.py @@ -17,8 +17,7 @@ import numpy from dygraph_to_static_utils_new import ( Dy2StTestBase, - test_ast_only, - test_legacy_and_pir, + test_legacy_and_pir_exe_and_pir_api, ) import paddle @@ -52,8 +51,7 @@ def _run_dy_static(self, func, x, with_exception): self._run(func, x, with_exception, True) self._run(func, x, with_exception, False) - @test_legacy_and_pir - @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_non_variable(self): self._run_dy_static( dyfunc_assert_non_variable, x=False, with_exception=True @@ -62,8 +60,7 @@ def test_non_variable(self): dyfunc_assert_non_variable, x=True, with_exception=False ) - @test_legacy_and_pir - @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_bool_variable(self): self._run_dy_static( dyfunc_assert_variable, x=numpy.array([False]), with_exception=True @@ -72,8 +69,7 @@ def test_bool_variable(self): dyfunc_assert_variable, x=numpy.array([True]), with_exception=False ) - @test_legacy_and_pir - @test_ast_only + @test_legacy_and_pir_exe_and_pir_api def test_int_variable(self): self._run_dy_static( dyfunc_assert_variable, x=numpy.array([0]), with_exception=True diff --git a/test/dygraph_to_static/test_dict.py b/test/dygraph_to_static/test_dict.py index c88496fd86b3e..5c6082797dcff 100644 --- a/test/dygraph_to_static/test_dict.py +++ b/test/dygraph_to_static/test_dict.py @@ -15,7 +15,11 @@ import unittest import numpy as np -from dygraph_to_static_utils_new import Dy2StTestBase, compare_legacy_with_pir +from dygraph_to_static_utils_new import ( + Dy2StTestBase, + compare_legacy_with_pir, + test_legacy_and_pir_exe_and_pir_api, +) import paddle from paddle import base @@ -140,6 +144,7 @@ def train(self, to_static=False): ret = net(self.x) return ret.numpy() + @test_legacy_and_pir_exe_and_pir_api def test_ast_to_func(self): self.assertTrue((self._run_dygraph() == self._run_static()).all()) @@ -196,6 +201,7 @@ def _run(self, to_static): return result.numpy() + @test_legacy_and_pir_exe_and_pir_api def test_transformed_result(self): dygraph_res = self._run_dygraph() static_res = self._run_static() @@ -239,6 +245,7 @@ def train(self, to_static=False): ret = net(z=0, x=self.x, y=True) return ret.numpy() + @test_legacy_and_pir_exe_and_pir_api def test_ast_to_func(self): dygraph_result = self._run_dygraph() static_result = self._run_static() @@ -250,6 +257,7 @@ def test_ast_to_func(self): class TestDictCmpInFor(Dy2StTestBase): + @test_legacy_and_pir_exe_and_pir_api def test_with_for(self): def func(): pos = [1, 3] @@ -266,6 +274,7 @@ def func(): self.assertEqual(paddle.jit.to_static(func)()['minus'], 8) + @test_legacy_and_pir_exe_and_pir_api def test_with_for_enumerate(self): def func(): pos = [1, 3] diff --git a/test/dygraph_to_static/test_no_gradient.py b/test/dygraph_to_static/test_no_gradient.py index b3bc726762ee4..a7cced35c04fd 100644 --- a/test/dygraph_to_static/test_no_gradient.py +++ b/test/dygraph_to_static/test_no_gradient.py @@ -15,7 +15,10 @@ import unittest import numpy -from dygraph_to_static_utils_new import Dy2StTestBase +from dygraph_to_static_utils_new import ( + Dy2StTestBase, + test_legacy_and_pir, +) import paddle @@ -33,6 +36,7 @@ def main_func(x, index): class TestNoGradientCase(Dy2StTestBase): + @test_legacy_and_pir def test_no_gradient(self): paddle.disable_static() x = paddle.randn([10, 3]) From 561b2a33054add82f492d87b498fa4a59f849f55 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 28 Nov 2023 11:19:26 +0000 Subject: [PATCH 2/4] update `test-assert` --- test/dygraph_to_static/test_assert.py | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/test/dygraph_to_static/test_assert.py b/test/dygraph_to_static/test_assert.py index 659594e09bcac..01250626f07e0 100644 --- a/test/dygraph_to_static/test_assert.py +++ b/test/dygraph_to_static/test_assert.py @@ -17,22 +17,17 @@ import numpy from dygraph_to_static_utils import ( Dy2StTestBase, - test_ast_only, - test_legacy_and_pt_and_pir, ) import paddle from paddle import base -from paddle.jit.api import to_static -@paddle.jit.to_static def dyfunc_assert_variable(x): x_v = base.dygraph.to_variable(x) assert x_v -@to_static def dyfunc_assert_non_variable(x=True): assert x @@ -52,34 +47,40 @@ def _run_dy_static(self, func, x, with_exception): self._run(func, x, with_exception, True) self._run(func, x, with_exception, False) - @test_legacy_and_pt_and_pir - @test_ast_only def test_non_variable(self): self._run_dy_static( - dyfunc_assert_non_variable, x=False, with_exception=True + paddle.jit.to_static(dyfunc_assert_non_variable), + x=False, + with_exception=True, ) self._run_dy_static( - dyfunc_assert_non_variable, x=True, with_exception=False + paddle.jit.to_static(dyfunc_assert_non_variable), + x=True, + with_exception=False, ) - @test_legacy_and_pt_and_pir - @test_ast_only def test_bool_variable(self): self._run_dy_static( - dyfunc_assert_variable, x=numpy.array([False]), with_exception=True + paddle.jit.to_static(dyfunc_assert_variable), + x=numpy.array([False]), + with_exception=True, ) self._run_dy_static( - dyfunc_assert_variable, x=numpy.array([True]), with_exception=False + paddle.jit.to_static(dyfunc_assert_variable), + x=numpy.array([True]), + with_exception=False, ) - @test_legacy_and_pt_and_pir - @test_ast_only def test_int_variable(self): self._run_dy_static( - dyfunc_assert_variable, x=numpy.array([0]), with_exception=True + paddle.jit.to_static(dyfunc_assert_variable), + x=numpy.array([0]), + with_exception=True, ) self._run_dy_static( - dyfunc_assert_variable, x=numpy.array([1]), with_exception=False + paddle.jit.to_static(dyfunc_assert_variable), + x=numpy.array([1]), + with_exception=False, ) From 3b20178501c653f2513eef4efccac0a0f4eb4d6e Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 28 Nov 2023 11:23:24 +0000 Subject: [PATCH 3/4] update `test_dict` --- test/dygraph_to_static/test_dict.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/test/dygraph_to_static/test_dict.py b/test/dygraph_to_static/test_dict.py index b71983873b34f..f69b112ba9afd 100644 --- a/test/dygraph_to_static/test_dict.py +++ b/test/dygraph_to_static/test_dict.py @@ -22,7 +22,6 @@ import paddle from paddle import base -from paddle.jit import to_static PLACE = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() @@ -82,7 +81,6 @@ def __init__(self, batch_size=64, hidden_size=16, output_size=16): self.output_size = output_size self.sub_net = SubNetWithDict(hidden_size, output_size) - @to_static def forward(self, input, max_len=4): input = base.dygraph.to_variable(input) cache = { @@ -138,7 +136,9 @@ def _run_dygraph(self): def train(self, to_static=False): paddle.jit.enable_to_static(to_static) with base.dygraph.guard(PLACE): - net = MainNetWithDict(batch_size=self.batch_size) + net = paddle.jit.to_static( + MainNetWithDict(batch_size=self.batch_size) + ) ret = net(self.x) return ret.numpy() @@ -148,8 +148,7 @@ def test_ast_to_func(self): # Tests for dict pop -@paddle.jit.to_static -def test_dic_pop(x): +def test_dict_pop(x): x = paddle.to_tensor(x) dict_a = {"red": 0, "green": 1, "blue": 2} @@ -160,8 +159,7 @@ def test_dic_pop(x): return out -@paddle.jit.to_static -def test_dic_pop_2(x): +def test_dict_pop_2(x): x = paddle.to_tensor(x) dict_a = {"red": x, "green": x + 1, "blue": x + 3} @@ -183,7 +181,7 @@ def setUp(self): self._set_test_func() def _set_test_func(self): - self.dygraph_func = test_dic_pop + self.dygraph_func = paddle.jit.to_static(test_dict_pop) def _run_static(self): return self._run(to_static=True) @@ -212,14 +210,13 @@ def test_transformed_result(self): class TestDictPop2(TestDictPop): def _set_test_func(self): - self.dygraph_func = test_dic_pop_2 + self.dygraph_func = paddle.jit.to_static(test_dict_pop_2) class NetWithDictPop(paddle.nn.Layer): def __init__(self): super().__init__() - @to_static def forward(self, x, **kwargs): x = paddle.to_tensor(x) y = kwargs.pop('y', None) @@ -238,7 +235,7 @@ def setUp(self): def train(self, to_static=False): paddle.jit.enable_to_static(to_static) with base.dygraph.guard(PLACE): - net = NetWithDictPop() + net = paddle.jit.to_static(NetWithDictPop()) ret = net(z=0, x=self.x, y=True) return ret.numpy() From 1ba8a5c42805d073ffdc07ad7cacc157f2183f53 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 28 Nov 2023 11:48:47 +0000 Subject: [PATCH 4/4] update `test_isinstance` --- test/dygraph_to_static/test_isinstance.py | 38 ++++++++++++++--------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/test/dygraph_to_static/test_isinstance.py b/test/dygraph_to_static/test_isinstance.py index 498c84ad0e885..9cac83cdbbe2f 100644 --- a/test/dygraph_to_static/test_isinstance.py +++ b/test/dygraph_to_static/test_isinstance.py @@ -88,24 +88,34 @@ def train(model, to_static): class TestIsinstance(Dy2StTestBase): @test_legacy_and_pt_and_pir def test_isinstance_simple_return_layer(self): - model = paddle.jit.to_static(IsInstanceLayer(SimpleReturnLayer())) - self._test_model(model) + model_creator = lambda: paddle.jit.to_static( + IsInstanceLayer(SimpleReturnLayer()) + ) + self._test_model(model_creator) + @test_legacy_and_pt_and_pir def test_isinstance_add_attr_layer(self): - model = paddle.jit.to_static(IsInstanceLayer(AddAttrLayer())) - self._test_model(model) + model_creator = lambda: paddle.jit.to_static( + IsInstanceLayer(AddAttrLayer()) + ) + self._test_model(model_creator) + @test_legacy_and_pt_and_pir def test_sequential_layer(self): - layers = [] - for i in range(5): - layers.append(SimpleReturnLayer()) - layers.append(AddAttrLayer()) - model = paddle.jit.to_static(SequentialLayer(layers)) - self._test_model(model) - - def _test_model(self, model): - st_out = train(model, to_static=True) - dy_out = train(model, to_static=False) + def model_creator(): + layers = [] + for i in range(5): + layers.append(SimpleReturnLayer()) + layers.append(AddAttrLayer()) + return paddle.jit.to_static(SequentialLayer(layers)) + + self._test_model(model_creator) + + def _test_model(self, model_creator): + st_model = model_creator() + st_out = train(st_model, to_static=True) + dy_model = model_creator() + dy_out = train(dy_model, to_static=False) np.testing.assert_allclose( dy_out, st_out,