From ddd9f30537001c46208644a760577108fc12c9ff Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 08:32:58 +0000 Subject: [PATCH 1/8] fix TestSundryAPI --- test/legacy_test/test_zero_dim_tensor.py | 40 +++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/test/legacy_test/test_zero_dim_tensor.py b/test/legacy_test/test_zero_dim_tensor.py index f4bddc81566052..d737b99ce64cec 100644 --- a/test/legacy_test/test_zero_dim_tensor.py +++ b/test/legacy_test/test_zero_dim_tensor.py @@ -25,6 +25,8 @@ import paddle import paddle.nn.functional as F +from paddle import base, core +from paddle.framework import in_dynamic_mode from paddle.pir_utils import test_with_pir_api unary_api_list = [ @@ -2674,6 +2676,7 @@ def test_prelu(self): self.assertEqual(x2.grad.shape, []) self.assertEqual(x2.grad.numpy(), 0.25) + @test_with_pir_api def test_while_loop(self): def cond(i, x): return paddle.less_than(i, eleven) @@ -2685,20 +2688,43 @@ def body(i, x): i = paddle.full([], 1.0, dtype='float32') i.stop_gradient = False + i.persistable = True eleven = paddle.full([], 11, dtype='float32') x = paddle.full([], 0.0, dtype='float32') x.stop_gradient = False + x.persistable = True out_i, out_x = paddle.static.nn.while_loop(cond, body, [i, x]) - out_x.backward() - self.assertEqual(out_i.shape, []) + if in_dynamic_mode(): + out_x.backward() + di = i.grad + dx = x.grad + else: + grad_list = paddle.static.append_backward(out_x) + for p, g in grad_list: + if p.is_same(i): + di = g + elif p.is_same(x): + dx = g + place = ( + base.CUDAPlace(0) + if core.is_compiled_with_cuda() + else base.CPUPlace() + ) + exe = base.Executor(place) + main_program = paddle.static.default_main_program() + out_i, out_x, di, dx = exe.run( + main_program, feed={}, fetch_list=[out_i, out_x, di, dx] + ) + + self.assertEqual(np.asarray(out_i).shape, ()) np.testing.assert_allclose(out_i, np.array(11)) - self.assertEqual(out_x.shape, []) + self.assertEqual(np.asarray(out_x).shape, ()) np.testing.assert_allclose(out_x, np.array(55)) - self.assertEqual(i.grad.shape, []) - np.testing.assert_allclose(i.grad, np.array(10)) - self.assertEqual(x.grad.shape, []) - np.testing.assert_allclose(x.grad, np.array(1.0)) + self.assertEqual(np.asarray(di).shape, ()) + np.testing.assert_allclose(di, np.array(10)) + self.assertEqual(np.asarray(dx).shape, ()) + np.testing.assert_allclose(dx, np.array(1.0)) def test_to_tensor(self): out1 = paddle.to_tensor(1) From c545183769d96e3796ea20ad2e69435122487a42 Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 09:01:08 +0000 Subject: [PATCH 2/8] fix TestSundryAPIStatic --- test/legacy_test/test_zero_dim_tensor.py | 33 +++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/test/legacy_test/test_zero_dim_tensor.py b/test/legacy_test/test_zero_dim_tensor.py index d737b99ce64cec..8b7cd294aa9182 100644 --- a/test/legacy_test/test_zero_dim_tensor.py +++ b/test/legacy_test/test_zero_dim_tensor.py @@ -5051,6 +5051,7 @@ def test_static_nn_prelu(self): np.testing.assert_allclose(res[0], np.array(1)) np.testing.assert_allclose(res[1], np.array(1)) + @test_with_pir_api @prog_scope() def test_while_loop(self): def cond(i, x): @@ -5065,20 +5066,34 @@ def body(i, x): with paddle.static.program_guard(main_program, paddle.static.Program()): i = paddle.static.data(name='i', shape=[], dtype='float32') i.stop_gradient = False + i.persistable = True eleven = paddle.full([], 11, 'float32') x = paddle.static.data(name='x', shape=[], dtype='float32') x.stop_gradient = False + x.persistable = True out_i, out_x = paddle.static.nn.while_loop(cond, body, [i, x]) - paddle.static.append_backward(out_x) + grad_list = paddle.static.append_backward(out_x) + + feed = { + 'i': np.array(1.0, dtype='float32'), + 'x': np.array(0.0, dtype='float32'), + } + if paddle.framework.in_pir_mode(): + fetch_list = [out_i, out_x] + for _, g in grad_list: + fetch_list.append(g) + res = self.exe.run( + main_program, + feed=feed, + fetch_list=fetch_list, + ) + else: + res = self.exe.run( + main_program, + feed=feed, + fetch_list=[out_i.name, out_x.name, i.grad_name, x.grad_name], + ) - res = self.exe.run( - main_program, - feed={ - 'i': np.array(1.0, dtype='float32'), - 'x': np.array(0.0, dtype='float32'), - }, - fetch_list=[out_i.name, out_x.name, i.grad_name, x.grad_name], - ) self.assertEqual(res[0].shape, ()) np.testing.assert_allclose(res[0], np.array(11)) self.assertEqual(res[1].shape, ()) From 5e04e7e83d1ee33ddfae52a56ddff4dcee276829 Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 11:43:45 +0000 Subject: [PATCH 3/8] fix test_standalone_controlflow --- .../test_standalone_controlflow.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/standalone_executor/test_standalone_controlflow.py b/test/standalone_executor/test_standalone_controlflow.py index ecd0e517f89c75..cc78be2d819e23 100644 --- a/test/standalone_executor/test_standalone_controlflow.py +++ b/test/standalone_executor/test_standalone_controlflow.py @@ -18,7 +18,7 @@ import paddle from paddle.base import core -from paddle.base.framework import Program, program_guard +from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -49,12 +49,12 @@ def true_func(): def false_func(): return paddle.tensor.fill_constant( - shape=[3, 4], dtype='float32', value=3 - ), paddle.tensor.fill_constant(shape=[4, 5], dtype='int64', value=2) + shape=[3, 4], dtype='int32', value=3 + ), paddle.tensor.fill_constant(shape=[4, 5], dtype='bool', value=2) - main_program = Program() - startup_program = Program() - with program_guard(main_program, startup_program): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): x = paddle.tensor.fill_constant( shape=[1], dtype='float32', value=0.1 ) @@ -111,6 +111,7 @@ def run_new_executor(self, feed): out = self._run(feed) return out + @test_with_pir_api def test_with_feed(self): feed = self._get_feed() paddle.enable_static() From f316f86f8d132b9bffaefc7cebf8b661c90a077f Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 12:16:30 +0000 Subject: [PATCH 4/8] fix test_device_guard --- test/legacy_test/test_device_guard.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/legacy_test/test_device_guard.py b/test/legacy_test/test_device_guard.py index 336437333e6926..cd3ca73a3bed34 100644 --- a/test/legacy_test/test_device_guard.py +++ b/test/legacy_test/test_device_guard.py @@ -16,7 +16,8 @@ import warnings import paddle -from paddle.base import core +from paddle.base import core, in_pir_mode +from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -143,6 +144,7 @@ def test_cpu_only_op(self): execute(main_program, startup_program) + @test_with_pir_api def test_without_kernel_op(self): main_program = paddle.static.Program() startup_program = paddle.static.Program() @@ -158,15 +160,16 @@ def test_without_kernel_op(self): with while_op.block(): i = paddle.increment(x=i, value=1) paddle.assign(paddle.less_than(x=i, y=loop_len), cond) - - warning = "The Op(while) is not support to set device." - warning_num = get_vaild_warning_num(warning, w) - assert warning_num == 1 + if not in_pir_mode(): + warning = "The Op(while) is not support to set device." + warning_num = get_vaild_warning_num(warning, w) + assert warning_num == 1 all_ops = main_program.global_block().ops device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName() for op in all_ops: - if op.type == 'while': + op_name = op.name() if in_pir_mode() else op.type + if op_name == 'while': self.assertEqual(op.desc.attr(device_attr_name), "") execute(main_program, startup_program) From 3b8f60f58b8582e9b80d0df9c7e5d31910a3f1c1 Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 13:16:15 +0000 Subject: [PATCH 5/8] fix bug --- test/standalone_executor/test_standalone_controlflow.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/standalone_executor/test_standalone_controlflow.py b/test/standalone_executor/test_standalone_controlflow.py index cc78be2d819e23..6979e754e98862 100644 --- a/test/standalone_executor/test_standalone_controlflow.py +++ b/test/standalone_executor/test_standalone_controlflow.py @@ -50,7 +50,9 @@ def true_func(): def false_func(): return paddle.tensor.fill_constant( shape=[3, 4], dtype='int32', value=3 - ), paddle.tensor.fill_constant(shape=[4, 5], dtype='bool', value=2) + ), paddle.tensor.fill_constant( + shape=[4, 5], dtype='bool', value=False + ) main_program = paddle.static.Program() startup_program = paddle.static.Program() @@ -93,10 +95,10 @@ def run_dygraph_once(self, feed): else: out = [ paddle.tensor.fill_constant( - shape=[3, 4], dtype='float32', value=3 + shape=[3, 4], dtype='int32', value=3 ).numpy(), paddle.tensor.fill_constant( - shape=[4, 5], dtype='int64', value=2 + shape=[4, 5], dtype='bool', value=False ).numpy(), ] return out From c74237fc8604d6f12826e12670d970c51684f954 Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 16:25:21 +0000 Subject: [PATCH 6/8] rerun ci From 6d88665a17a442908e2a1ef2a8f6b5d029d6d319 Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 17:40:30 +0000 Subject: [PATCH 7/8] Revert "fix test_standalone_controlflow" --- .../test_standalone_controlflow.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/test/standalone_executor/test_standalone_controlflow.py b/test/standalone_executor/test_standalone_controlflow.py index 6979e754e98862..40f5e9204f66c2 100644 --- a/test/standalone_executor/test_standalone_controlflow.py +++ b/test/standalone_executor/test_standalone_controlflow.py @@ -18,7 +18,7 @@ import paddle from paddle.base import core -from paddle.pir_utils import test_with_pir_api +from paddle.base.framework import Program, program_guard paddle.enable_static() @@ -49,14 +49,12 @@ def true_func(): def false_func(): return paddle.tensor.fill_constant( - shape=[3, 4], dtype='int32', value=3 - ), paddle.tensor.fill_constant( - shape=[4, 5], dtype='bool', value=False - ) + shape=[3, 4], dtype='float32', value=3 + ), paddle.tensor.fill_constant(shape=[4, 5], dtype='int64', value=2) - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): + main_program = Program() + startup_program = Program() + with program_guard(main_program, startup_program): x = paddle.tensor.fill_constant( shape=[1], dtype='float32', value=0.1 ) @@ -113,7 +111,6 @@ def run_new_executor(self, feed): out = self._run(feed) return out - @test_with_pir_api def test_with_feed(self): feed = self._get_feed() paddle.enable_static() From 4b61952ccc61c120dc335f43fb1228c029c44101 Mon Sep 17 00:00:00 2001 From: huangjiyi <947613776@qq.com> Date: Thu, 18 Jan 2024 17:43:07 +0000 Subject: [PATCH 8/8] revert --- test/standalone_executor/test_standalone_controlflow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/standalone_executor/test_standalone_controlflow.py b/test/standalone_executor/test_standalone_controlflow.py index 40f5e9204f66c2..ecd0e517f89c75 100644 --- a/test/standalone_executor/test_standalone_controlflow.py +++ b/test/standalone_executor/test_standalone_controlflow.py @@ -93,10 +93,10 @@ def run_dygraph_once(self, feed): else: out = [ paddle.tensor.fill_constant( - shape=[3, 4], dtype='int32', value=3 + shape=[3, 4], dtype='float32', value=3 ).numpy(), paddle.tensor.fill_constant( - shape=[4, 5], dtype='bool', value=False + shape=[4, 5], dtype='int64', value=2 ).numpy(), ] return out