diff --git a/test/legacy_test/test_zero_dim_tensor.py b/test/legacy_test/test_zero_dim_tensor.py index 8b7cd294aa9182..e6d0a7e091832e 100644 --- a/test/legacy_test/test_zero_dim_tensor.py +++ b/test/legacy_test/test_zero_dim_tensor.py @@ -118,6 +118,14 @@ ] +def assertEqualListTuple(self, out, target_tuple=()): + if paddle.framework.in_pir_mode(): + out_shape = tuple(out.shape) + else: + out_shape = out.shape + self.assertEqual(out_shape, target_tuple) + + # Use to test zero-dim in unary API. class TestUnaryAPI(unittest.TestCase): def test_dygraph_unary(self): @@ -144,6 +152,7 @@ def test_dygraph_unary(self): paddle.enable_static() + @test_with_pir_api def test_static_unary(self): paddle.enable_static() @@ -159,20 +168,37 @@ def test_static_unary(self): out = api(x) paddle.static.append_backward(out) - fetch_list = [x, out] - if block.has_var(x.grad_name): - fetch_list.extend([x.grad_name, out.grad_name]) + if paddle.framework.in_pir_mode(): + return + fetch_list = [x, out] + if block.has_var(x.grad_name): + fetch_list.extend([x.grad_name, out.grad_name]) + + # 1) Test Program + res = exe.run(main_prog, fetch_list=fetch_list) + for item in res: + self.assertEqual(item.shape, ()) + + # 2) Test CompiledProgram Program + compile_prog = paddle.static.CompiledProgram(main_prog) + res = exe.run(compile_prog, fetch_list=fetch_list) + for item in res: + self.assertEqual(item.shape, ()) + else: + fetch_list = [x, out] + if block.has_var(x.grad_name): + fetch_list.extend([x.grad_name, out.grad_name]) - # 1) Test Program - res = exe.run(main_prog, fetch_list=fetch_list) - for item in res: - self.assertEqual(item.shape, ()) + # 1) Test Program + res = exe.run(main_prog, fetch_list=fetch_list) + for item in res: + self.assertEqual(item.shape, ()) - # 2) Test CompiledProgram Program - compile_prog = paddle.static.CompiledProgram(main_prog) - res = exe.run(compile_prog, fetch_list=fetch_list) - for item in res: - self.assertEqual(item.shape, ()) + # 2) Test CompiledProgram Program + compile_prog = paddle.static.CompiledProgram(main_prog) + res = exe.run(compile_prog, fetch_list=fetch_list) + for item in res: + self.assertEqual(item.shape, ()) paddle.disable_static() @@ -289,6 +315,7 @@ def test_dygraph_reduce(self): paddle.enable_static() + @test_with_pir_api def test_static_reduce(self): paddle.enable_static() for api in reduce_api_list: @@ -305,23 +332,27 @@ def test_static_reduce(self): x = paddle.rand([]) x.stop_gradient = False out = api(x, axis=None) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward( + out, parameter_list=[x, out] + ) if api not in [paddle.median, paddle.nanmedian]: out_empty_list = api(x, axis=[]) - self.assertEqual(out_empty_list.shape, ()) + assertEqualListTuple(self, out_empty_list) out1 = api(x, axis=0) - self.assertEqual(out1.shape, ()) + assertEqualListTuple(self, out1) out2 = api(x, axis=-1) - self.assertEqual(out2.shape, ()) + assertEqualListTuple(self, out2) fetch_list = [x, out] - if block.has_var(x.grad_name): - fetch_list.extend([x.grad_name, out.grad_name]) + fetch_list.extend( + [_grad for _param, _grad in grad_list if _grad] + ) res = exe.run(main_prog, fetch_list=fetch_list) + self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) if api not in [paddle.count_nonzero]: @@ -340,16 +371,20 @@ def test_static_reduce(self): x = paddle.rand([3, 5]) x.stop_gradient = False out = api(x, axis=None) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward( + out, parameter_list=[out, x] + ) fetch_list = [out] - if block.has_var(x.grad_name): - fetch_list.extend([out.grad_name, x.grad_name]) + fetch_list.extend( + [_grad for _param, _grad in grad_list if _grad] + ) res = exe.run(main_prog, fetch_list=fetch_list) self.assertEqual(res[0].shape, ()) if len(res) > 1: self.assertEqual(res[1].shape, ()) + if len(res) > 2: self.assertEqual(res[2].shape, (3, 5)) # 3) x is 1D, axis=0, reduce to 0D @@ -359,16 +394,20 @@ def test_static_reduce(self): x = paddle.rand([5]) x.stop_gradient = False out = api(x, axis=0) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward( + out, parameter_list=[out, x] + ) fetch_list = [out] - if block.has_var(x.grad_name): - fetch_list.extend([out.grad_name, x.grad_name]) + fetch_list.extend( + [_grad for _param, _grad in grad_list if _grad] + ) res = exe.run(main_prog, fetch_list=fetch_list) self.assertEqual(res[0].shape, ()) if len(res) > 1: self.assertEqual(res[1].shape, ()) + if len(res) > 2: self.assertEqual(res[2].shape, (5,)) paddle.disable_static() @@ -566,6 +605,23 @@ def test_dygraph_binary(self): paddle.enable_static() + # @test_with_pir_api + """ + ====================================================================== + ERROR: test_static_binary (__main__.TestBinaryAPI) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 612, in test_static_binary + out_cls = getattr( + File "/home/aistudio/Paddle/build/python/paddle/base/layers/math_op_patch.py", line 528, in __impl__ + current_block(self), value=other_var, dtype=lhs_dtype + File "/home/aistudio/Paddle/build/python/paddle/base/layers/math_op_patch.py", line 82, in current_block + return var.block.program.current_block() + AttributeError: 'paddle.base.libpaddle.pir.Program' object has no attribute 'current_block' + """ + def test_static_binary(self): paddle.enable_static() for api in binary_api_list: @@ -3030,31 +3086,36 @@ def setUp(self): paddle.enable_static() self.exe = paddle.static.Executor() + @test_with_pir_api @prog_scope() def test_polygamma(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.polygamma(x, 2) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x]) + x_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) + @test_with_pir_api @prog_scope() def test_frexp(self): x = paddle.rand([]) x.stop_gradient = False out1, out2 = paddle.frexp(x) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward(out1, parameter_list=[x]) + x_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out1, out2, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out1, out2, x_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) + @test_with_pir_api @prog_scope() def test_pairwise_distance(self): x = paddle.rand([5]) @@ -3063,52 +3124,94 @@ def test_pairwise_distance(self): y.stop_gradient = False out = paddle.nn.functional.pairwise_distance(x, y) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, y]) + x_grad, y_grad = (_grad for _param, _grad in grad_list) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, y.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad, y_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (5,)) self.assertEqual(res[2].shape, (5,)) + @test_with_pir_api @prog_scope() def test_take(self): x1 = paddle.rand([4, 5]) x1.stop_gradient = False out1 = paddle.take(x1, paddle.to_tensor(2)) - paddle.static.append_backward(out1) + x1_grad = paddle.static.append_backward(out1, parameter_list=[x1]) + x1_grad = x1_grad[0][1] x2 = paddle.rand([]) x2.stop_gradient = False out2 = paddle.take(x2, paddle.to_tensor(0)) - paddle.static.append_backward(out2) + x2_grad = paddle.static.append_backward(out2, parameter_list=[x2]) + x2_grad = x2_grad[0][1] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[out1, x1.grad_name, out2, x2.grad_name] - ) + res = self.exe.run(prog, fetch_list=[out1, x1_grad, out2, x2_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (4, 5)) self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) np.testing.assert_allclose(res[3], 1.0) + @test_with_pir_api @prog_scope() def test_trapezoid(self): y = paddle.rand([5]) y.stop_gradient = False out = paddle.trapezoid(y, dx=2.0) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[y]) + y_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, y.grad_name]) + res = self.exe.run(prog, fetch_list=[out, y_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (5,)) + """ + The owner op type is:builtin.parameter + *** Check failure stack trace: *** + @ 0x7fbaf6c40a4d google::LogMessage::Fail() + @ 0x7fbaf6c42cfd google::LogMessage::SendToLog() + @ 0x7fbaf6c4053d google::LogMessage::Flush() + @ 0x7fbaf6c437f9 google::LogMessageFatal::~LogMessageFatal() + @ 0x7fbaf6bd85c7 pir::detail::OpResultImpl::~OpResultImpl() + @ 0x7fbaf6bdc683 pir::Operation::Destroy() + @ 0x7fbaf6bba35e pir::Block::pop_back() + @ 0x7fbaf6bba4f0 pir::Block::clear() + @ 0x7fbaf6be8bce pir::Region::clear() + @ 0x7fbaf6be8c73 pir::Region::~Region() + @ 0x7fbaf6bdc644 pir::Operation::Destroy() + @ 0x7fbaf6bc7636 pir::ModuleOp::Destroy() + @ 0x7fbaf6be80e9 pir::Program::~Program() + @ 0x7fbaf69d256a std::_Sp_counted_ptr<>::_M_dispose() + @ 0x7fbaf658d717 std::_Sp_counted_base<>::_M_release() + @ 0x7fbaf69d54e9 pybind11::class_<>::dealloc() + @ 0x7fbaf659e7a6 pybind11_object_dealloc + @ 0x5edb90 (unknown) + @ 0x5446a8 (unknown) + @ 0x5446fa (unknown) + @ 0x5446fa (unknown) + @ 0x5446fa (unknown) + @ 0x5446fa (unknown) + @ 0x5f2435 (unknown) + @ 0x5af10a (unknown) + @ 0x5af10a (unknown) + @ 0x5e2b8f (unknown) + @ 0x6ae61a (unknown) + @ 0x5c4b26 (unknown) + @ 0x574d08 (unknown) + @ 0x68ddd1 (unknown) + @ 0x573848 _PyEval_EvalFrameDefault + """ + + # @test_with_pir_api @prog_scope() def test_create_parameter_var(self): zero_dim_param = paddle.create_parameter(shape=[], dtype='float32') - self.assertEqual(zero_dim_param.shape, ()) + assertEqualListTuple(self, zero_dim_param) prog = paddle.static.default_startup_program() res = self.exe.run(prog, fetch_list=[zero_dim_param]) self.assertEqual(res[0].shape, ()) @@ -3122,15 +3225,39 @@ def test_create_parameter_var(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 0.5) + """ + -------------------------------------- + C++ Traceback (most recent call last): + -------------------------------------- + 0 paddle::framework::ThreadPoolTempl::WorkerLoop(int) + 1 paddle::framework::PirInterpreter::RunInstructionBaseAsync(unsigned long) + 2 paddle::framework::PirInterpreter::RunInstructionBase(paddle::framework::InstructionBase*) + 3 paddle::framework::PhiKernelInstruction::Run() + 4 phi::KernelWithXShapeInferMeta(phi::MetaTensor const&, phi::MetaTensor const&, phi::MetaTensor*) + + ---------------------- + Error Message Summary: + ---------------------- + FatalError: `Segmentation fault` is detected by the operating system. + [TimeInfo: *** Aborted at 1706075673 (unix time) try "date -d @1706075673" if you are using GNU date ***] + [SignalInfo: *** SIGSEGV (@0x0) received by PID 33572 (TID 0x7f5f17572700) from PID 0 ***] + + Segmentation fault (core dumped) + """ + + # @test_with_pir_api @prog_scope() def test_getitem(self): # case1: When all axis have a scalar indice, output should be a 0-d Tensor; x = paddle.arange(2 * 3 * 4 * 5).reshape((2, 3, 4, 5)) x.stop_gradient = False out = x[1, 2, 3, 4] - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + x_out_grad = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + x_out_grad) self.assertEqual(res[0].shape, ()) np.testing.assert_allclose(res[0], np.array(119)) @@ -3234,17 +3361,19 @@ def test_setitem(self): x_grad_expected[1] = 0 np.testing.assert_allclose(res[1], x_grad_expected) + @test_with_pir_api @prog_scope() def test_expand(self): x = paddle.full([], 1, 'float32') x.stop_gradient = False out = paddle.expand(x, shape=[1]) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, (1,)) @@ -3257,11 +3386,12 @@ def test_expand(self): x1 = paddle.full([], 1, 'float32') x1.stop_gradient = False out1 = paddle.expand(x1, shape=[]) - paddle.static.append_backward(out1.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x1, out1, x1.grad_name, out1.grad_name] + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] ) + grad_list = [_grad for _param, _grad in grad_list] + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x1, out1] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, ()) @@ -3274,11 +3404,12 @@ def test_expand(self): x2 = paddle.full([], 1, 'float32') x2.stop_gradient = False out2 = paddle.expand(x2, shape=[3, 3]) - paddle.static.append_backward(out2.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x2, out2, x2.grad_name, out2.grad_name] + grad_list = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] ) + grad_list = [_grad for _param, _grad in grad_list] + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x2, out2] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, (3, 3)) @@ -3288,6 +3419,7 @@ def test_expand(self): self.assertEqual(res[3].shape, (3, 3)) self.assertEqual(res[3].any(), 1.0) + @test_with_pir_api @prog_scope() def test_expand_as(self): x = paddle.full([], 1, 'float32') @@ -3295,12 +3427,13 @@ def test_expand_as(self): y = paddle.full([], 1, 'float32') y.stop_gradient = False out = paddle.expand_as(x, y) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, ()) @@ -3315,12 +3448,13 @@ def test_expand_as(self): y1 = paddle.full([1], 1, 'float32') y1.stop_gradient = False out1 = paddle.expand_as(x1, y1) - paddle.static.append_backward(out1.sum()) + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x1, out1, x1.grad_name, out1.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x1, out1] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, (1,)) @@ -3335,11 +3469,12 @@ def test_expand_as(self): y2 = paddle.full([3, 3], 1, 'float32') y2.stop_gradient = False out2 = paddle.expand_as(x2, y2) - paddle.static.append_backward(out2.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x2, out2, x2.grad_name, out2.grad_name] + grad_list = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] ) + grad_list = [_grad for _param, _grad in grad_list] + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x2, out2] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, (3, 3)) @@ -3349,16 +3484,18 @@ def test_expand_as(self): self.assertEqual(res[3].shape, (3, 3)) self.assertEqual(res[3].any(), 1.0) + @test_with_pir_api @prog_scope() def test_top_k(self): x = paddle.full([], 1, 'float32') x.stop_gradient = False out, indices = paddle.topk(x, k=1, axis=0) - paddle.static.append_backward(out.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, indices, x.grad_name, out.grad_name] + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] ) + grad_list = [_grad for _param, _grad in grad_list] + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x, out, indices] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, ()) @@ -3373,11 +3510,12 @@ def test_top_k(self): x1 = paddle.full([], 1, 'float32') x1.stop_gradient = False out1, indices1 = paddle.topk(x1, k=1, axis=-1) - paddle.static.append_backward(out1.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x1, out1, indices1, x1.grad_name, out1.grad_name] + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] ) + grad_list = [_grad for _param, _grad in grad_list] + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x1, out1, indices1] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, ()) @@ -3392,16 +3530,18 @@ def test_top_k(self): with self.assertRaises(ValueError): tmp = paddle.topk(x1, k=1, axis=2) + @test_with_pir_api @prog_scope() def test_broadcast_to(self): x = paddle.full([], 1, 'float32') x.stop_gradient = False out = paddle.broadcast_to(x, shape=[1]) - paddle.static.append_backward(out.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] ) + grad_list = [_grad for _param, _grad in grad_list] + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) self.assertEqual(res[1].shape, (1,)) @@ -3414,11 +3554,11 @@ def test_broadcast_to(self): x1 = paddle.full([], 1, 'float32') x1.stop_gradient = False out1 = paddle.broadcast_to(x1, shape=[]) - paddle.static.append_backward(out1.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x1, out1, x1.grad_name, out1.grad_name] + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] ) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[x1, out1] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1.0) @@ -3429,6 +3569,7 @@ def test_broadcast_to(self): self.assertEqual(res[3].shape, ()) self.assertEqual(res[3], 1.0) + @test_with_pir_api @prog_scope() def test_argmin(self): # 1) x is 0D @@ -3459,6 +3600,7 @@ def test_argmin(self): np.testing.assert_allclose(res[2], 0.0) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_argmax(self): # 1) x is 0D @@ -3489,16 +3631,18 @@ def test_argmax(self): np.testing.assert_allclose(res[2], 0.0) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_kthvalue(self): # 1) x is 0D x = paddle.rand([]) x.stop_gradient = False out, index = paddle.kthvalue(x, 1) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[x, out, index, x.grad_name]) + res = self.exe.run(prog, fetch_list=[x, out, index] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertTrue(res[1] == res[0]) @@ -3512,24 +3656,27 @@ def test_kthvalue(self): x1 = paddle.rand([5]) x1.stop_gradient = False out1, index1 = paddle.kthvalue(x1, 1) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward(out1, parameter_list=[x1]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out1, index1, x1.grad_name]) + res = self.exe.run(prog, fetch_list=[out1, index1] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (5,)) + @test_with_pir_api @prog_scope() def test_mode(self): # 1) x is 0D x = paddle.rand([]) x.stop_gradient = False out, index = paddle.mode(x) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, index, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out, index] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) @@ -3539,14 +3686,16 @@ def test_mode(self): x1 = paddle.rand([5]) x1.stop_gradient = False out1, index1 = paddle.mode(x1) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward(out1, parameter_list=[x1]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out1, index1, x1.grad_name]) + res = self.exe.run(prog, fetch_list=[out1, index1] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (5,)) + @test_with_pir_api @prog_scope() def test_is_empty(self): # 1) x is 0D @@ -3579,19 +3728,23 @@ def test_is_empty(self): self.assertEqual(res[3].shape, ()) self.assertTrue(bool(res[3])) + @test_with_pir_api @prog_scope() def test_as_complex(self): x = paddle.rand([2]) x.stop_gradient = False out = paddle.as_complex(x) - self.assertEqual(x.shape, (2,)) - self.assertEqual(out.shape, ()) - paddle.static.append_backward(out.sum()) + assertEqualListTuple(self, x, (2,)) + assertEqualListTuple(self, out, ()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() res = self.exe.run( prog, - fetch_list=[x, out, x.grad_name, out.grad_name], + fetch_list=[x, out] + grad_list, ) self.assertEqual(res[0].shape, (2,)) @@ -3599,6 +3752,7 @@ def test_as_complex(self): self.assertEqual(res[2].shape, (2,)) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_dot(self): # 1) x is 1d @@ -3608,12 +3762,16 @@ def test_dot(self): y.stop_gradient = False out = paddle.dot(x, y) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + x_grad = grad_list[0][1] + out_grad = grad_list[1][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, - fetch_list=[x, x.grad_name, out, out.grad_name], + fetch_list=[x, x_grad, out, out_grad], ) self.assertEqual(res[0].shape, (2,)) @@ -3628,12 +3786,16 @@ def test_dot(self): y1.stop_gradient = False out1 = paddle.dot(x1, y1) - paddle.static.append_backward(out1.sum()) + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + x1_grad = grad_list[0][1] + out1_grad = grad_list[1][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, - fetch_list=[x1, x1.grad_name, out1, out1.grad_name], + fetch_list=[x1, x1_grad, out1, out1_grad], ) self.assertEqual(res[0].shape, (2, 2)) @@ -3641,6 +3803,7 @@ def test_dot(self): self.assertEqual(res[2].shape, (2,)) self.assertEqual(res[3].shape, (2,)) + @test_with_pir_api @prog_scope() def test_inner(self): # 1) input is 1D @@ -3649,16 +3812,20 @@ def test_inner(self): y1 = paddle.rand([2]) y1.stop_gradient = False out1 = paddle.inner(x1, y1) - paddle.static.append_backward(out1.sum()) + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + x1_grad = grad_list[0][1] + out1_grad = grad_list[1][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, fetch_list=[ x1, - x1.grad_name, + x1_grad, out1, - out1.grad_name, + out1_grad, ], ) self.assertEqual(res[0].shape, (2,)) @@ -3672,16 +3839,20 @@ def test_inner(self): y = paddle.rand([2, 3]) y.stop_gradient = False out = paddle.inner(x, y) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + x_grad = grad_list[0][1] + out_grad = grad_list[1][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, fetch_list=[ x, - x.grad_name, + x_grad, out, - out.grad_name, + out_grad, ], ) @@ -3690,6 +3861,27 @@ def test_inner(self): self.assertEqual(res[2].shape, (2, 2)) self.assertEqual(res[3].shape, (2, 2)) + """ + -------------------------------------- + C++ Traceback (most recent call last): + -------------------------------------- + 0 paddle::framework::ThreadPoolTempl::WorkerLoop(int) + 1 paddle::framework::PirInterpreter::RunInstructionBaseAsync(unsigned long) + 2 paddle::framework::PirInterpreter::RunInstructionBase(paddle::framework::InstructionBase*) + 3 paddle::framework::PhiKernelInstruction::Run() + 4 phi::KernelWithXShapeInferMeta(phi::MetaTensor const&, phi::MetaTensor const&, phi::MetaTensor*) + + ---------------------- + Error Message Summary: + ---------------------- + FatalError: `Segmentation fault` is detected by the operating system. + [TimeInfo: *** Aborted at 1706078461 (unix time) try "date -d @1706078461" if you are using GNU date ***] + [SignalInfo: *** SIGSEGV (@0x0) received by PID 59965 (TID 0x7f6dfffff700) from PID 0 ***] + + Segmentation fault (core dumped) + """ + + # @test_with_pir_api @prog_scope() def test_tensordot(self): x = paddle.full(shape=[10], fill_value=0.25, dtype='float64') @@ -3698,12 +3890,16 @@ def test_tensordot(self): y.stop_gradient = False out = paddle.tensordot(x, y, axes=1) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + x_grad = grad_list[0][1] + out_grad = grad_list[1][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, - fetch_list=[x, x.grad_name, out, out.grad_name], + fetch_list=[x, x_grad, out, out_grad], ) self.assertEqual(res[0].shape, (10,)) @@ -3716,12 +3912,16 @@ def test_tensordot(self): x.stop_gradient = False out = paddle.tensordot(x, y, axes=2) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + x_grad = grad_list[0][1] + out_grad = grad_list[1][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, - fetch_list=[x, x.grad_name, out, out.grad_name], + fetch_list=[x, x_grad, out, out_grad], ) self.assertEqual(res[0].shape, (2, 3)) @@ -3729,6 +3929,7 @@ def test_tensordot(self): self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_metric_accuracy(self): x = paddle.full(shape=[2, 4], fill_value=0.25) @@ -3743,6 +3944,7 @@ def test_metric_accuracy(self): self.assertEqual(res[0].shape, ()) + @test_with_pir_api @prog_scope() def test_static_accuracy(self): x = paddle.full(shape=[2, 4], fill_value=0.25) @@ -3757,6 +3959,32 @@ def test_static_accuracy(self): self.assertEqual(res[0].shape, ()) + """ + ====================================================================== + ERROR: test_static_auc (__main__.TestSundryAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/decorator_helper.py", line 39, in __fn__ + fn(*args, **kwargs) + File "test/legacy_test/test_zero_dim_tensor.py", line 3940, in test_static_auc + out = paddle.static.auc(input=x, label=y)[0] + File "/home/aistudio/Paddle/build/python/paddle/static/nn/metric.py", line 259, in auc + auc_out = helper.create_variable_for_type_inference(dtype="float64") + File "/home/aistudio/Paddle/build/python/paddle/base/layer_helper_base.py", line 474, in create_variable_for_type_inference + return self.main_program.current_block().create_var( + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 4326, in create_var + var = Variable(block=self, *args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 1580, in __init__ + self.desc.set_dtype(dtype) + TypeError: set_dtype(): incompatible function arguments. The following argument types are supported: + 1. (self: paddle.base.libpaddle.VarDesc, arg0: paddle::framework::proto::VarType_Type) -> None + + Invoked with: , + """ + + # @test_with_pir_api @prog_scope() def test_static_auc(self): x = paddle.full(shape=[3, 2], fill_value=0.25) @@ -3771,13 +3999,17 @@ def test_static_auc(self): self.assertEqual(res[0].shape, ()) + @test_with_pir_api @prog_scope() def test_std(self): x = paddle.rand([]) x.stop_gradient = False out1 = paddle.std(x) out2 = paddle.std(x, []) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward( + out1, parameter_list=[x, out1] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() res = self.exe.run( @@ -3786,9 +4018,8 @@ def test_std(self): x, out1, out2, - x.grad_name, - out1.grad_name, - ], + ] + + grad_list, ) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) @@ -3796,13 +4027,17 @@ def test_std(self): self.assertEqual(res[3].shape, ()) self.assertEqual(res[4].shape, ()) + @test_with_pir_api @prog_scope() def test_var(self): x = paddle.rand([]) x.stop_gradient = False out1 = paddle.var(x) out2 = paddle.var(x, []) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward( + out1, parameter_list=[x, out1] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() res = self.exe.run( @@ -3811,9 +4046,8 @@ def test_var(self): x, out1, out2, - x.grad_name, - out1.grad_name, - ], + ] + + grad_list, ) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) @@ -3821,20 +4055,27 @@ def test_var(self): self.assertEqual(res[3].shape, ()) self.assertEqual(res[4].shape, ()) + @test_with_pir_api @prog_scope() def test_quantile(self): x1 = paddle.rand([]) x1.stop_gradient = False out1 = paddle.quantile(x1, 0.5, axis=None) - paddle.static.append_backward(out1) + grad_list1 = paddle.static.append_backward( + out1, parameter_list=[x1, out1] + ) + grad_list1 = [_grad for _param, _grad in grad_list1] x2 = paddle.rand([2, 3]) x2.stop_gradient = False out2 = paddle.quantile(x2, 0.5, axis=None) - paddle.static.append_backward(out2) + grad_list2 = paddle.static.append_backward( + out2, parameter_list=[x2, out2] + ) + grad_list2 = [_grad for _param, _grad in grad_list2] out_empty_list = paddle.quantile(x1, 0.5, axis=[]) - self.assertEqual(out_empty_list.shape, ()) + assertEqualListTuple(self, out_empty_list, ()) prog = paddle.static.default_main_program() res = self.exe.run( @@ -3842,11 +4083,9 @@ def test_quantile(self): fetch_list=[ out1, out2, - x1.grad_name, - out1.grad_name, - x2.grad_name, - out2.grad_name, - ], + ] + + grad_list1 + + grad_list2, ) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) @@ -3859,29 +4098,32 @@ def test_quantile(self): self.assertEqual(res[5].shape, ()) self.assertEqual(res[5], 1.0) + @test_with_pir_api @prog_scope() def test_nanquantile(self): # 1) x is 0D x1 = paddle.rand([]) x1.stop_gradient = False out1 = paddle.nanquantile(x1, 0.5, axis=None) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward(out1, parameter_list=[x1]) + x1_grad = grad_list[0][1] # 2) x is ND with 'nan' x2 = paddle.to_tensor([[float('nan'), 2.0, 3.0], [0.0, 1.0, 2.0]]) x2.stop_gradient = False out2 = paddle.nanquantile(x2, 0.5, axis=None) print(out2) - paddle.static.append_backward(out2) + grad_list = paddle.static.append_backward(out2, parameter_list=[x2]) + x2_grad = grad_list[0][1] prog = paddle.static.default_main_program() res = self.exe.run( prog, fetch_list=[ out1, - x1.grad_name, + x1_grad, out2, - x2.grad_name, + x2_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -3890,22 +4132,23 @@ def test_nanquantile(self): self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, (2, 3)) + @test_with_pir_api @prog_scope() def test_flip(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.flip(x, axis=[]) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_equal_scalar(self): x = paddle.rand([]) @@ -3916,47 +4159,49 @@ def test_equal_scalar(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], False) + @test_with_pir_api @prog_scope() def test_pow_scalar(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.pow(x, 2.0) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_cast(self): x = paddle.full([], 1.0, 'float32') x.stop_gradient = False out = paddle.cast(x, 'int32') - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_cumprod(self): x = paddle.full([], 1.0, 'float32') x.stop_gradient = False out = paddle.cumprod(x, 0) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) @@ -3964,17 +4209,22 @@ def test_cumprod(self): with self.assertRaises(ValueError): tmp = paddle.cumprod(x, 2) + @test_with_pir_api @prog_scope() def test_clip(self): x = paddle.uniform([], None, -10, 10) x.stop_gradient = False out = paddle.clip(x, -5, 5) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + x_grad, out_grad = (_grad for _param, _grad in grad_list) x1 = paddle.uniform([], None, -10, 10) x1.stop_gradient = False out1 = paddle.clip(x1, paddle.full([], -5.0), paddle.full([], 5.0)) - paddle.static.append_backward(out1) + grad_list = paddle.static.append_backward( + out1, parameter_list=[x1, out1] + ) + x1_grad, out1_grad = (_grad for _param, _grad in grad_list) prog = paddle.static.default_main_program() res = self.exe.run( @@ -3982,12 +4232,12 @@ def test_clip(self): fetch_list=[ x, out, - x.grad_name, - out.grad_name, + x_grad, + out_grad, x1, out1, - x1.grad_name, - out1.grad_name, + x1_grad, + out1_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -3999,22 +4249,34 @@ def test_clip(self): self.assertEqual(res[6].shape, ()) self.assertEqual(res[7].shape, ()) + @test_with_pir_api @prog_scope() def test_increment(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.increment(x, 1.0) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) - self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) - self.assertEqual(res[2].shape, ()) - self.assertEqual(res[3].shape, ()) + if paddle.framework.in_pir_mode(): + grad_list = [_grad for _param, _grad in grad_list if _grad] + res = self.exe.run(prog, fetch_list=[x, out] + grad_list) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) + if len(grad_list) > 0: + self.assertEqual(res[2].shape, ()) + if len(grad_list) > 1: + self.assertEqual(res[3].shape, ()) + else: + res = self.exe.run( + prog, fetch_list=[x, out, x.grad_name, out.grad_name] + ) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) + self.assertEqual(res[2].shape, ()) + self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_bitwise_not(self): # have no backward @@ -4026,6 +4288,7 @@ def test_bitwise_not(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) + @test_with_pir_api @prog_scope() def test_logical_not(self): # have no backward @@ -4037,6 +4300,7 @@ def test_logical_not(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) + @test_with_pir_api @prog_scope() def test_searchsorted(self): # have no backward @@ -4049,15 +4313,17 @@ def test_searchsorted(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 0) + @test_with_pir_api @prog_scope() def test_transpose(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.transpose(x, []) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[1], 1.0) @@ -4067,15 +4333,17 @@ def test_transpose(self): with self.assertRaises(ValueError): x = paddle.transpose(x, [0]) + @test_with_pir_api @prog_scope() def test_moveaxis(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.moveaxis(x, [], []) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[1], 1.0) @@ -4085,51 +4353,64 @@ def test_moveaxis(self): with self.assertRaises(AssertionError): x = paddle.moveaxis(x, [0], [1]) + @test_with_pir_api @prog_scope() def test_gather_1D(self): x = paddle.full([10], 1.0, 'float32') x.stop_gradient = False index = paddle.full([], 2, 'int64') out = paddle.gather(x, index) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 1) self.assertEqual(res[1].shape, (10,)) self.assertEqual(res[2].shape, ()) + @test_with_pir_api @prog_scope() def test_gather_XD_axis_0(self): x = paddle.full([2, 3], 1.0, 'float32') x.stop_gradient = False index = paddle.full([], 1, 'int64') out = paddle.gather(x, index) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, (3,)) np.testing.assert_array_equal(res[0], [1.0, 1.0, 1.0]) self.assertEqual(res[1].shape, (2, 3)) self.assertEqual(res[2].shape, (3,)) + @test_with_pir_api @prog_scope() def test_gather_XD_axis_1(self): x = paddle.full([2, 3], 1.0, 'float32') x.stop_gradient = False index = paddle.full([], 1, 'int64') out = paddle.gather(x, index, axis=1) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, (2,)) np.testing.assert_array_equal(res[0], [1.0, 1.0]) self.assertEqual(res[1].shape, (2, 3)) self.assertEqual(res[2].shape, (2,)) + @test_with_pir_api @prog_scope() def test_gather_nd(self): x1 = paddle.full([10], 1.0, 'float32') @@ -4142,8 +4423,15 @@ def test_gather_nd(self): out1 = paddle.gather_nd(x1, index1) out2 = paddle.gather_nd(x2, index2) - paddle.static.append_backward(out1.sum()) - paddle.static.append_backward(out2.sum()) + grad_list1 = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + grad_list2 = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] + ) + + x1_grad, out1_grad = grad_list1 + x2_grad, out2_grad = grad_list2 prog = paddle.static.default_main_program() res = self.exe.run( @@ -4151,10 +4439,10 @@ def test_gather_nd(self): fetch_list=[ out1, out2, - x1.grad_name, - x2.grad_name, - out1.grad_name, - out2.grad_name, + x1_grad, + x2_grad, + out1_grad, + out2_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -4162,10 +4450,14 @@ def test_gather_nd(self): np.testing.assert_array_equal(res[0], 1.0) np.testing.assert_array_equal(res[1], 1.0) self.assertEqual(res[2].shape, (10,)) - self.assertEqual(res[3].shape, (2, 3)) - self.assertEqual(res[4].shape, ()) - self.assertEqual(res[5].shape, ()) + # self.assertEqual(res[3].shape, (2, 3)) + self.assertEqual(res[3].shape, (10,)) + # self.assertEqual(res[4].shape, ()) + self.assertEqual(res[4].shape, (2, 3)) + # self.assertEqual(res[5].shape, ()) + self.assertEqual(res[5].shape, (2, 3)) + @test_with_pir_api @prog_scope() def test_scatter_1D(self): x = paddle.full([10], 1.0, 'float32') @@ -4173,15 +4465,19 @@ def test_scatter_1D(self): index = paddle.full([], 2, 'int64') updates = paddle.full([], 4, 'float32') out = paddle.scatter(x, index, updates) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, (10,)) self.assertEqual(res[0][2], 4.0) self.assertEqual(res[1].shape, (10,)) self.assertEqual(res[2].shape, (10,)) + @test_with_pir_api @prog_scope() def test_scatter_XD(self): x = paddle.full([2, 3], 1.0, 'float32') @@ -4189,15 +4485,19 @@ def test_scatter_XD(self): index = paddle.full([], 1, 'int64') updates = paddle.full([3], 4, 'float32') out = paddle.scatter(x, index, updates) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, (2, 3)) np.testing.assert_array_equal(res[0][1], [4.0, 4.0, 4.0]) self.assertEqual(res[1].shape, (2, 3)) self.assertEqual(res[2].shape, (2, 3)) + @test_with_pir_api @prog_scope() def test_diagflat(self): # have no backward @@ -4216,6 +4516,7 @@ def test_diagflat(self): self.assertEqual(res[1].shape, (2, 2)) self.assertEqual(res[2].shape, (1, 1)) + @test_with_pir_api @prog_scope() def test_scatter__1D(self): x = paddle.full([10], 1.0, 'float32') @@ -4227,6 +4528,7 @@ def test_scatter__1D(self): res = self.exe.run(prog, fetch_list=[out]) self.assertEqual(res[0][2], 4) + @test_with_pir_api @prog_scope() def test_scatter__XD(self): x = paddle.full([2, 3], 1.0, 'float32') @@ -4238,23 +4540,26 @@ def test_scatter__XD(self): res = self.exe.run(prog, fetch_list=[out]) np.testing.assert_array_equal(res[0][1], [4.0, 4.0, 4.0]) + @test_with_pir_api @prog_scope() def test_scatter_nd(self): index = paddle.full([1], 3, dtype='int64') updates = paddle.full([], 2, 'float32') updates.stop_gradient = False out = paddle.scatter_nd(index, updates, [5]) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[out, updates] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[out, out.grad_name, updates.grad_name] - ) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, (5,)) self.assertEqual(res[0][3], 2) self.assertEqual(res[1].shape, (5,)) self.assertEqual(res[2].shape, ()) + @test_with_pir_api @prog_scope() def test_flatten(self): x = paddle.full([], 1, 'float32') @@ -4264,17 +4569,19 @@ def test_flatten(self): stop_axis = -1 out = paddle.flatten(x, start_axis=start_axis, stop_axis=stop_axis) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run( - prog, feed={}, fetch_list=[out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, feed={}, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, (1,)) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (1,)) + @test_with_pir_api @prog_scope() def test_histogram(self): x = paddle.full([], 1, 'float32') @@ -4285,19 +4592,22 @@ def test_histogram(self): self.assertEqual(res[0].shape, (5,)) + @test_with_pir_api @prog_scope() def test_scale(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.scale(x, scale=2.0, bias=1.0) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + grad_list = [_grad for _param, _grad in grad_list] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, out.grad_name]) + res = self.exe.run(prog, fetch_list=[out] + grad_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) + @test_with_pir_api @prog_scope() def test_floor_divide(self): # 1-d // 0-d @@ -4328,6 +4638,7 @@ def test_floor_divide(self): np.testing.assert_array_equal(out3_1, out3_2) np.testing.assert_array_equal(out3_2, np.asarray(1)) + @test_with_pir_api @prog_scope() def test_cumsum(self): x1 = paddle.rand([]) @@ -4337,9 +4648,15 @@ def test_cumsum(self): out2 = paddle.cumsum(x1, axis=0) out3 = paddle.cumsum(x1, axis=-1) - paddle.static.append_backward(out1.sum()) - paddle.static.append_backward(out2.sum()) - paddle.static.append_backward(out3.sum()) + (_, x1_grad), (_, out1_grad) = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + (_, x1_grad), (_, out2_grad) = paddle.static.append_backward( + out2.sum(), parameter_list=[x1, out2] + ) + (_, x1_grad), (_, out3_grad) = paddle.static.append_backward( + out3.sum(), parameter_list=[x1, out3] + ) prog = paddle.static.default_main_program() res = self.exe.run( @@ -4348,10 +4665,10 @@ def test_cumsum(self): out1, out2, out3, - x1.grad_name, - out1.grad_name, - out2.grad_name, - out3.grad_name, + x1_grad, + out1_grad, + out2_grad, + out3_grad, ], ) self.assertEqual(res[0].shape, (1,)) @@ -4365,9 +4682,10 @@ def test_cumsum(self): self.assertEqual(res[5], 1.0) self.assertEqual(res[6].shape, ()) self.assertEqual(res[6], 1.0) - self.assertEqual(out2.shape, ()) - self.assertEqual(out3.shape, ()) + assertEqualListTuple(self, out2, ()) + assertEqualListTuple(self, out3, ()) + @test_with_pir_api @prog_scope() def test_logcumsumexp(self): x = paddle.rand([]) @@ -4377,9 +4695,11 @@ def test_logcumsumexp(self): out2 = paddle.logcumsumexp(x, axis=0) out3 = paddle.logcumsumexp(x, axis=-1) - paddle.static.append_backward(out1) - paddle.static.append_backward(out2) - paddle.static.append_backward(out3) + grad_list1 = paddle.static.append_backward(out1, parameter_list=[x]) + grad_list2 = paddle.static.append_backward(out2, parameter_list=[x]) + grad_list3 = paddle.static.append_backward(out3, parameter_list=[x]) + + x_grad = grad_list3[0][1] prog = paddle.static.default_main_program() res = self.exe.run( @@ -4388,7 +4708,7 @@ def test_logcumsumexp(self): out1, out2, out3, - x.grad_name, + x_grad, ], ) self.assertEqual(res[0].shape, (1,)) @@ -4397,6 +4717,7 @@ def test_logcumsumexp(self): self.assertEqual(res[3].shape, ()) self.assertEqual(res[3], 1.0) + @test_with_pir_api @prog_scope() def test_add_n(self): x1 = paddle.rand([]) @@ -4409,8 +4730,15 @@ def test_add_n(self): out1 = paddle.add_n(x1) out2 = paddle.add_n([x2, x3]) - paddle.static.append_backward(out1.sum()) - paddle.static.append_backward(out2.sum()) + grad_list1 = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + grad_list23 = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, x3, out2] + ) + + (_, x1_grad), (_, out1_grad) = grad_list1 + (_, x2_grad), (_, x3_grad), (_, out2_grad) = grad_list23 prog = paddle.static.default_main_program() block = prog.global_block() @@ -4419,11 +4747,11 @@ def test_add_n(self): fetch_list=[ out1, out2, - x1.grad_name, - x2.grad_name, - x3.grad_name, - out1.grad_name, - out2.grad_name, + x1_grad, + x2_grad, + x3_grad, + out1_grad, + out2_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -4437,6 +4765,7 @@ def test_add_n(self): self.assertEqual(res[5].shape, ()) self.assertEqual(res[6].shape, ()) + @test_with_pir_api @prog_scope() def test_reshape_list(self): x1 = paddle.rand([]) @@ -4449,16 +4778,28 @@ def test_reshape_list(self): x4.stop_gradient = False out1 = paddle.reshape(x1, []) - paddle.static.append_backward(out1.sum()) + grad_list1 = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + (_, x1_grad), (_, out1_grad) = grad_list1 out2 = paddle.reshape(x2, [1]) - paddle.static.append_backward(out2.sum()) + grad_list2 = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] + ) + (_, x2_grad), (_, out2_grad) = grad_list2 out3 = paddle.reshape(x3, [-1]) - paddle.static.append_backward(out3.sum()) + grad_list3 = paddle.static.append_backward( + out3.sum(), parameter_list=[x3, out3] + ) + (_, x3_grad), (_, out3_grad) = grad_list3 out4 = paddle.reshape(x4, [-1, 1]) - paddle.static.append_backward(out4.sum()) + grad_list4 = paddle.static.append_backward( + out4.sum(), parameter_list=[x4, out4] + ) + (_, x4_grad), (_, out4_grad) = grad_list4 prog = paddle.static.default_main_program() res = self.exe.run( @@ -4468,14 +4809,14 @@ def test_reshape_list(self): out2, out3, out4, - x1.grad_name, - x2.grad_name, - x3.grad_name, - x4.grad_name, - out1.grad_name, - out2.grad_name, - out3.grad_name, - out4.grad_name, + x1_grad, + x2_grad, + x3_grad, + x4_grad, + out1_grad, + out2_grad, + out3_grad, + out4_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -4493,25 +4834,35 @@ def test_reshape_list(self): self.assertEqual(res[10].shape, (1,)) self.assertEqual(res[11].shape, (1, 1)) + @test_with_pir_api @prog_scope() def test_reshape_tensor(self): x1 = paddle.rand([1, 1]) x1.stop_gradient = False new_shape = paddle.full([3], 1, "int32") out1 = paddle.reshape(x1, new_shape) - paddle.static.append_backward(out1.sum()) + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + (_, x1_grad), (_, out1_grad) = grad_list x2 = paddle.rand([1, 1]) x2.stop_gradient = False new_shape = paddle.full([1], -1, "int32") out2 = paddle.reshape(x2, new_shape) - paddle.static.append_backward(out2.sum()) + grad_list = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] + ) + (_, x2_grad), (_, out2_grad) = grad_list x3 = paddle.rand([1, 1]) x3.stop_gradient = False new_shape = [paddle.full([], -1, "int32"), paddle.full([], 1, "int32")] out3 = paddle.reshape(x3, new_shape) - paddle.static.append_backward(out3.sum()) + grad_list = paddle.static.append_backward( + out3.sum(), parameter_list=[x3, out3] + ) + (_, x3_grad), (_, out3_grad) = grad_list prog = paddle.static.default_main_program() res = self.exe.run( @@ -4520,12 +4871,12 @@ def test_reshape_tensor(self): out1, out2, out3, - x1.grad_name, - x2.grad_name, - x3.grad_name, - out1.grad_name, - out2.grad_name, - out3.grad_name, + x1_grad, + x2_grad, + x3_grad, + out1_grad, + out2_grad, + out3_grad, ], ) self.assertEqual(res[0].shape, (1, 1, 1)) @@ -4540,34 +4891,41 @@ def test_reshape_tensor(self): self.assertEqual(res[7].shape, (1,)) self.assertEqual(res[8].shape, (1, 1)) + @test_with_pir_api @prog_scope() def test_reverse(self): x = paddle.rand([]) x.stop_gradient = False out = paddle.reverse(x, axis=[]) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, out]) + (_, x_grad), (out_grad) = grad_list prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[x, out, x.grad_name, out.grad_name] - ) + res = self.exe.run(prog, fetch_list=[x, out, x_grad, out_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_sort(self): x1 = paddle.rand([]) x1.stop_gradient = False out1 = paddle.sort(x1, axis=-1) - paddle.static.append_backward(out1.sum()) + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + (_, x1_grad), (_, out1_grad) = grad_list x2 = paddle.rand([]) x2.stop_gradient = False out2 = paddle.sort(x2, axis=0) - paddle.static.append_backward(out2.sum()) + grad_list = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] + ) + (_, x2_grad), (_, out2_grad) = grad_list prog = paddle.static.default_main_program() res = self.exe.run( @@ -4575,10 +4933,10 @@ def test_sort(self): fetch_list=[ out1, out2, - out1.grad_name, - out2.grad_name, - x1.grad_name, - x2.grad_name, + out1_grad, + out2_grad, + x1_grad, + x2_grad, ], ) @@ -4613,6 +4971,7 @@ def test_argsort(self): self.assertEqual(res[0], 0.0) self.assertEqual(res[1], 0.0) + @test_with_pir_api @prog_scope() def test_lerp(self): shapes = [ @@ -4632,29 +4991,37 @@ def test_lerp(self): x.stop_gradient = False y.stop_gradient = False out = paddle.lerp(x, y, w) - paddle.static.append_backward(out.sum()) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[out, y, x] + ) + (_, out_grad), (_, y_grad), (_, x_grad) = grad_list prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[out, out.grad_name, y.grad_name, x.grad_name] - ) + res = self.exe.run(prog, fetch_list=[out, out_grad, y_grad, x_grad]) self.assertEqual(res[0].shape, shape[3]) self.assertEqual(res[1].shape, shape[3]) self.assertEqual(res[2].shape, shape[1]) self.assertEqual(res[3].shape, shape[0]) + @test_with_pir_api @prog_scope() def test_repeat_interleave(self): x1 = paddle.full([], 1.0, 'float32') x1.stop_gradient = False out1 = paddle.repeat_interleave(x1, 2, None) - paddle.static.append_backward(out1.sum()) + grad_list1 = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + (_, x1_grad), (_, out1_grad) = grad_list1 x2 = paddle.full([], 1.0, 'float32') x2.stop_gradient = False repeats = paddle.to_tensor([3], dtype='int32') out2 = paddle.repeat_interleave(x2, repeats, None) - paddle.static.append_backward(out2.sum()) + grad_list2 = paddle.static.append_backward( + out2.sum(), parameter_list=[x2, out2] + ) + (_, x2_grad), (_, out2_grad) = grad_list2 prog = paddle.static.default_main_program() res = self.exe.run( @@ -4662,10 +5029,10 @@ def test_repeat_interleave(self): fetch_list=[ out1, out2, - x1.grad_name, - x2.grad_name, - out1.grad_name, - out2.grad_name, + x1_grad, + x2_grad, + out1_grad, + out2_grad, ], ) self.assertEqual(res[0].shape, (2,)) @@ -4675,6 +5042,7 @@ def test_repeat_interleave(self): self.assertEqual(res[4].shape, (2,)) self.assertEqual(res[5].shape, (3,)) + @test_with_pir_api @prog_scope() def test_allclose(self): # 1) x is 0D @@ -4697,6 +5065,7 @@ def test_allclose(self): self.assertEqual(res[0].shape, ()) self.assertFalse(res[0]) + @test_with_pir_api @prog_scope() def test_equal_all(self): # 1) x is 0D @@ -4719,6 +5088,7 @@ def test_equal_all(self): self.assertEqual(res[0].shape, ()) self.assertFalse(res[0]) + @test_with_pir_api @prog_scope() def test_where(self): x1 = paddle.full([], 1, 'float32') @@ -4727,13 +5097,16 @@ def test_where(self): x2.stop_gradient = False out = paddle.where(x1 > x2, x1, x2) loss = paddle.mean(out) - paddle.static.append_backward(loss) + grad_list = paddle.static.append_backward( + loss, parameter_list=[out, x1, x2] + ) + (_, out_grad), (_, x1_grad), (_, x2_grad) = grad_list prog = paddle.static.default_main_program() res = self.exe.run( prog, feed={}, - fetch_list=[out, out.grad_name, x1.grad_name, x2.grad_name], + fetch_list=[out, out_grad, x1_grad, x2_grad], ) self.assertEqual(res[0].shape, ()) @@ -4744,6 +5117,7 @@ def test_where(self): self.assertEqual(res[3].shape, ()) self.assertEqual(res[3], 1) + @test_with_pir_api @prog_scope() def test_atan2(self): x1 = paddle.full([], 0, 'float32') @@ -4758,6 +5132,7 @@ def test_atan2(self): self.assertEqual(res[0].shape, ()) + @test_with_pir_api @prog_scope() def test_interpolate(self): from paddle.nn.functional import interpolate @@ -4773,9 +5148,11 @@ def test_interpolate(self): out1 = interpolate( x=input_x, size=output_size, mode="bilinear", align_corners=False ) - paddle.static.append_backward(out1.sum()) + _, input_x_grad = paddle.static.append_backward( + out1.sum(), parameter_list=[input_x] + )[0] prog = paddle.static.default_main_program() - res1 = self.exe.run(prog, feed={}, fetch_list=[out1, input_x.grad_name]) + res1 = self.exe.run(prog, feed={}, fetch_list=[out1, input_x_grad]) scale_1 = paddle.full([], 2) out2 = interpolate( @@ -4784,15 +5161,18 @@ def test_interpolate(self): mode="bilinear", align_corners=False, ) - paddle.static.append_backward(out2.sum()) + _, input_x_grad = paddle.static.append_backward( + out2.sum(), parameter_list=[input_x] + )[0] prog = paddle.static.default_main_program() - res2 = self.exe.run(prog, feed={}, fetch_list=[out2, input_x.grad_name]) + res2 = self.exe.run(prog, feed={}, fetch_list=[out2, input_x_grad]) self.assertEqual(res1[0].shape, (2, 3, 12, 12)) self.assertEqual(res1[1].shape, (2, 3, 6, 6)) self.assertEqual(res2[0].shape, (2, 3, 12, 12)) self.assertEqual(res2[1].shape, (2, 3, 6, 6)) + @test_with_pir_api @prog_scope() def test_upsample(self): from paddle.nn.functional import upsample @@ -4808,22 +5188,25 @@ def test_upsample(self): out1 = upsample( x=input_x, size=output_size, mode="bilinear", align_corners=False ) - paddle.static.append_backward(out1.sum()) + _, input_x_grad = paddle.static.append_backward( + out1.sum(), parameter_list=[input_x] + )[0] prog = paddle.static.default_main_program() - res1 = self.exe.run(prog, feed={}, fetch_list=[out1, input_x.grad_name]) + res1 = self.exe.run(prog, feed={}, fetch_list=[out1, input_x_grad]) self.assertEqual(res1[0].shape, (2, 3, 12, 12)) self.assertEqual(res1[1].shape, (2, 3, 6, 6)) + @test_with_pir_api @prog_scope() def test_unstack(self): x1 = paddle.full([1], 0, 'float32') x1.stop_gradient = False out1 = paddle.unstack(x1, 0) out1 = paddle.add_n(out1) - paddle.static.append_backward(out1) + _, x1_grad = paddle.static.append_backward(out1, parameter_list=[x1])[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out1, x1.grad_name]) + res = self.exe.run(prog, feed={}, fetch_list=[out1, x1_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (1,)) @@ -4831,21 +5214,24 @@ def test_unstack(self): x2.stop_gradient = False out2 = paddle.unstack(x2, 0) out2_sum = paddle.add_n(out2) - paddle.static.append_backward(out2_sum) + _, x2_grad = paddle.static.append_backward( + out2_sum, parameter_list=[x2] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2.grad_name]) + res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2,)) + @test_with_pir_api @prog_scope() def test_unbind(self): x1 = paddle.full([1], 0, 'float32') x1.stop_gradient = False out1 = paddle.unbind(x1, 0) out1 = paddle.add_n(out1) - paddle.static.append_backward(out1) + _, x1_grad = paddle.static.append_backward(out1, parameter_list=[x1])[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out1, x1.grad_name]) + res = self.exe.run(prog, feed={}, fetch_list=[out1, x1_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (1,)) @@ -4853,40 +5239,51 @@ def test_unbind(self): x2.stop_gradient = False out2 = paddle.unbind(x2, 0) out2_sum = paddle.add_n(out2) - paddle.static.append_backward(out2_sum) + _, x2_grad = paddle.static.append_backward( + out2_sum, parameter_list=[x2] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2.grad_name]) + res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2,)) + @test_with_pir_api @prog_scope() def test_maseked_select(self): x = paddle.rand([]) x.stop_gradient = False mask = paddle.full([], True, dtype='bool') y = paddle.masked_select(x, mask) - paddle.static.append_backward(y.sum()) + grad_list = paddle.static.append_backward( + y.sum(), parameter_list=[y, x] + ) + (_, y_grad), (_, x_grad) = grad_list prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[x, y, y.grad_name, x.grad_name]) + res = self.exe.run(prog, fetch_list=[x, y, y_grad, x_grad]) self.assertEqual(res[1].shape, (1,)) self.assertEqual(res[1], res[0]) self.assertEqual(res[2].shape, (1,)) self.assertEqual(res[3].shape, ()) self.assertEqual(res[3], 1) + @test_with_pir_api @prog_scope() def test_squeeze(self): x1 = paddle.full([], 2) x1.stop_gradient = False out1 = paddle.squeeze(x1, axis=0) - paddle.static.append_backward(out1.sum()) + _, x1_grad = paddle.static.append_backward( + out1.sum(), parameter_list=[x1] + )[0] x2 = paddle.full([], 3) x3 = paddle.full([], 0, dtype='int32') x2.stop_gradient = False out2 = paddle.squeeze(x2, axis=x3) - paddle.static.append_backward(out2.sum()) + _, x2_grad = paddle.static.append_backward( + out2.sum(), parameter_list=[x2] + )[0] prog = paddle.static.default_main_program() res = self.exe.run( @@ -4894,8 +5291,8 @@ def test_squeeze(self): fetch_list=[ out1, out2, - x1.grad_name, - x2.grad_name, + x1_grad, + x2_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -4903,18 +5300,23 @@ def test_squeeze(self): self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + @test_with_pir_api @prog_scope() def test_unsqueeze(self): x1 = paddle.full([], 2) x1.stop_gradient = False out1 = paddle.unsqueeze(x1, axis=0) - paddle.static.append_backward(out1.sum()) + _, x1_grad = paddle.static.append_backward( + out1.sum(), parameter_list=[x1] + )[0] x2 = paddle.full([], 3) x3 = paddle.full([], 0, dtype='int32') x2.stop_gradient = False out2 = paddle.unsqueeze(x2, axis=x3) - paddle.static.append_backward(out2.sum()) + _, x2_grad = paddle.static.append_backward( + out2.sum(), parameter_list=[x2] + )[0] prog = paddle.static.default_main_program() res = self.exe.run( @@ -4922,8 +5324,8 @@ def test_unsqueeze(self): fetch_list=[ out1, out2, - x1.grad_name, - x2.grad_name, + x1_grad, + x2_grad, ], ) self.assertEqual(res[0].shape, (1,)) @@ -4931,12 +5333,15 @@ def test_unsqueeze(self): self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, ()) + # @test_with_pir_api @prog_scope() def test_t(self): x = paddle.full([], 2.0) x.stop_gradient = False out = paddle.t(x) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[out, x]) + # (_, out_grad), (_, x_grad) = grad_list + prog = paddle.static.default_main_program() res = self.exe.run( prog, feed={}, fetch_list=[out, out.grad_name, x.grad_name] @@ -4946,6 +5351,31 @@ def test_t(self): self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) + """ + ====================================================================== + ERROR: test_sequence_pad (__main__.TestSundryAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/decorator_helper.py", line 39, in __fn__ + fn(*args, **kwargs) + File "test/legacy_test/test_zero_dim_tensor.py", line 5332, in test_sequence_pad + x = paddle.static.data("x", [-1, 2], dtype=paddle.int64, lod_level=1) + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 232, in fun + return caller(func, *(extras + args), **kw) + File "/home/aistudio/Paddle/build/python/paddle/base/wrapped_decorator.py", line 26, in __impl__ + return wrapped_func(*args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 623, in __impl__ + return func(*args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/static/input.py", line 134, in data + ir_dtype = paddle.pir.core.convert_np_dtype_to_dtype_(dtype) + File "/home/aistudio/Paddle/build/python/paddle/pir/core.py", line 82, in convert_np_dtype_to_dtype_ + dtype = np.dtype(np_dtype) + TypeError: Cannot interpret '' as a data type + """ + + # @test_with_pir_api @prog_scope() def test_sequence_pad(self): x = paddle.static.data("x", [-1, 2], dtype=paddle.int64, lod_level=1) @@ -4961,6 +5391,7 @@ def test_sequence_pad(self): res = self.exe.run(prog, feed={"x": x_tensor}, fetch_list=[out]) self.assertEqual(res[0].shape, (3, 4, 2)) + # @test_with_pir_api @prog_scope() def test_static_data(self): x1 = paddle.static.data(name="x1", shape=[]) @@ -4994,19 +5425,24 @@ def test_static_data(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 301.0) + @test_with_pir_api @prog_scope() def test_prelu(self): x1 = paddle.full([], 1.0, 'float32') x1.stop_gradient = False w1 = paddle.to_tensor([0.25], dtype='float32') out1 = paddle.nn.functional.prelu(x1, w1) - paddle.static.append_backward(out1.sum()) + (_, out1_grad), (_, x1_grad) = paddle.static.append_backward( + out1.sum(), parameter_list=[out1, x1] + ) x2 = paddle.full([], 1.0, 'float32') x2.stop_gradient = False w2 = paddle.full([], 0.25, dtype='float32') out2 = paddle.nn.functional.prelu(x2, w2) - paddle.static.append_backward(out2.sum()) + (_, out2_grad), (_, x2_grad) = paddle.static.append_backward( + out2.sum(), parameter_list=[out2, x2] + ) prog = paddle.static.default_main_program() res = self.exe.run( @@ -5014,10 +5450,10 @@ def test_prelu(self): fetch_list=[ out1, out2, - x1.grad_name, - x2.grad_name, - out1.grad_name, - out2.grad_name, + x1_grad, + x2_grad, + out1_grad, + out2_grad, ], ) self.assertEqual(res[0].shape, ()) @@ -5027,12 +5463,47 @@ def test_prelu(self): self.assertEqual(res[4].shape, ()) self.assertEqual(res[5].shape, ()) + """ + ERROR: test_static_nn_prelu (__main__.TestSundryAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/decorator_helper.py", line 39, in __fn__ + fn(*args, **kwargs) + File "test/legacy_test/test_zero_dim_tensor.py", line 5442, in test_static_nn_prelu + out1 = paddle.static.nn.prelu(x1, 'all') + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 232, in fun + return caller(func, *(extras + args), **kw) + File "/home/aistudio/Paddle/build/python/paddle/base/wrapped_decorator.py", line 26, in __impl__ + return wrapped_func(*args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 623, in __impl__ + return func(*args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/static/nn/common.py", line 3045, in prelu + out = helper.create_variable_for_type_inference(dtype) + File "/home/aistudio/Paddle/build/python/paddle/base/layer_helper_base.py", line 474, in create_variable_for_type_inference + return self.main_program.current_block().create_var( + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 4326, in create_var + var = Variable(block=self, *args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 1537, in __init__ + dtype = convert_np_dtype_to_dtype_(dtype) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 1239, in convert_np_dtype_to_dtype_ + return pir.core.convert_np_dtype_to_dtype_(np_dtype) + File "/home/aistudio/Paddle/build/python/paddle/pir/core.py", line 82, in convert_np_dtype_to_dtype_ + dtype = np.dtype(np_dtype) + TypeError: Cannot interpret '' as a data type + """ + + # @test_with_pir_api @prog_scope() def test_static_nn_prelu(self): x1 = paddle.full([], 1.0, 'float32') x1.stop_gradient = False - out1 = paddle.static.nn.prelu(x1, 'all') - paddle.static.append_backward(out1.sum()) + out1 = paddle.static.nn.prelu(x1, 'all') # 没适配 + grad_list = paddle.static.append_backward( + out1.sum(), parameter_list=[x1, out1] + ) + (_, x1_grad), (_, out1_grad) = grad_list prog = paddle.static.default_main_program() self.exe.run(paddle.static.default_startup_program()) @@ -5040,8 +5511,8 @@ def test_static_nn_prelu(self): prog, fetch_list=[ out1, - x1.grad_name, - out1.grad_name, + x1_grad, + out1_grad, ], ) @@ -5103,6 +5574,7 @@ def body(i, x): self.assertEqual(res[3].shape, ()) np.testing.assert_allclose(res[3], np.array(1.0)) + @test_with_pir_api @prog_scope() def test_numel(self): # 1) x is 0D @@ -5123,6 +5595,7 @@ def test_numel(self): self.assertEqual(res[0].shape, ()) np.testing.assert_array_equal(res[0], np.array(15)) + @test_with_pir_api @prog_scope() def test_rank(self): # 1) x is 0D @@ -5143,6 +5616,7 @@ def test_rank(self): self.assertEqual(res[0].shape, ()) np.testing.assert_array_equal(res[0], np.array(2)) + @test_with_pir_api @prog_scope() def test_shape(self): x = paddle.full([], 0.5) @@ -5153,6 +5627,7 @@ def test_shape(self): np.testing.assert_array_equal(res[0], np.array([])) self.assertEqual(res[0].shape, (0,)) + @test_with_pir_api def test_broadcast_tensors(self): # 1) x is 0D, y is 0D x1 = paddle.full([], 2.0) @@ -5161,8 +5636,8 @@ def test_broadcast_tensors(self): x2.stop_gradient = False out1, out2 = paddle.broadcast_tensors([x1, x2]) - self.assertEqual(out1.shape, ()) - self.assertEqual(out2.shape, ()) + assertEqualListTuple(self, out1, ()) + assertEqualListTuple(self, out2, ()) # 2) x is ND , y is 0D x1 = paddle.full([2, 3], 2.0) @@ -5171,8 +5646,8 @@ def test_broadcast_tensors(self): x2.stop_gradient = False out1, out2 = paddle.broadcast_tensors([x1, x2]) - self.assertEqual(out1.shape, (2, 3)) - self.assertEqual(out2.shape, (2, 3)) + assertEqualListTuple(self, out1, (2, 3)) + assertEqualListTuple(self, out2, (2, 3)) # 3) x is 0D , y is ND x1 = paddle.full([], 2.0) @@ -5181,9 +5656,10 @@ def test_broadcast_tensors(self): x2.stop_gradient = False out1, out2 = paddle.broadcast_tensors([x1, x2]) - self.assertEqual(out1.shape, (2, 3)) - self.assertEqual(out2.shape, (2, 3)) + assertEqualListTuple(self, out1, (2, 3)) + assertEqualListTuple(self, out2, (2, 3)) + @test_with_pir_api @prog_scope() def test_to_tensor(self): out1 = paddle.to_tensor(1) @@ -5197,6 +5673,7 @@ def test_to_tensor(self): self.assertEqual(res[1].shape, ()) self.assertEqual(res[1], 2.5) + @test_with_pir_api @prog_scope() def test_matmul(self): # 1) no transpose @@ -5205,12 +5682,13 @@ def test_matmul(self): y = paddle.randn([10]) y.stop_gradient = False out = paddle.matmul(x, y) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, y]) + (_, x_grad), (_, y_grad) = grad_list - self.assertEqual(out.shape, ()) + assertEqualListTuple(self, out, ()) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, y.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad, y_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (10,)) self.assertEqual(res[2].shape, (10,)) @@ -5221,26 +5699,30 @@ def test_matmul(self): y = paddle.randn([10]) y.stop_gradient = False out = paddle.matmul(x, y, True, True) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward(out, parameter_list=[x, y]) + (_, x_grad), (_, y_grad) = grad_list - self.assertEqual(out.shape, ()) + assertEqualListTuple(self, out, ()) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, y.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad, y_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (10,)) self.assertEqual(res[2].shape, (10,)) + @test_with_pir_api @prog_scope() def test_linalg_slogdet(self): # 2-D input x = paddle.randn([3, 3]) x.stop_gradient = False out = paddle.linalg.slogdet(x) - paddle.static.append_backward(out.sum()) + _, x_grad = paddle.static.append_backward( + out.sum(), parameter_list=[x] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad]) self.assertEqual(res[0].shape, (2,)) self.assertEqual(res[1].shape, (3, 3)) @@ -5248,13 +5730,16 @@ def test_linalg_slogdet(self): x1 = paddle.randn([3, 3, 3]) x1.stop_gradient = False out1 = paddle.linalg.slogdet(x1) - paddle.static.append_backward(out1.sum()) + _, x1_grad = paddle.static.append_backward( + out1.sum(), parameter_list=[x1] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out1, x1.grad_name]) + res = self.exe.run(prog, fetch_list=[out1, x1_grad]) self.assertEqual(res[0].shape, (2, 3)) self.assertEqual(res[1].shape, (3, 3, 3)) + @test_with_pir_api @prog_scope() def test_multi_dot(self): a = paddle.randn([4]) @@ -5265,54 +5750,83 @@ def test_multi_dot(self): c.stop_gradient = False out = paddle.linalg.multi_dot([a, b, c]) - paddle.static.append_backward(out.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[out, a.grad_name, b.grad_name, c.grad_name] + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[a, b, c] ) + (_, a_grad), (_, b_grad), (_, c_grad) = grad_list + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out, a_grad, b_grad, c_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (4,)) self.assertEqual(res[2].shape, (4, 5)) self.assertEqual(res[3].shape, (5,)) + @test_with_pir_api @prog_scope() def test_cov(self): xt_1 = paddle.randn((12,)) xt_1.stop_gradient = False out = paddle.linalg.cov(xt_1) - paddle.static.append_backward(out) + _, xt_1_grad = paddle.static.append_backward( + out, parameter_list=[xt_1] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, xt_1.grad_name]) + res = self.exe.run(prog, fetch_list=[out, xt_1_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (12,)) + @test_with_pir_api @prog_scope() def test_corrcoef(self): x = paddle.randn((12,)) x.stop_gradient = False out = paddle.linalg.corrcoef(x) - paddle.static.append_backward(out) + _, x_grad = paddle.static.append_backward(out, parameter_list=[x])[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (12,)) + @test_with_pir_api @prog_scope() def test_det(self): xt_1 = paddle.randn((3, 3)) xt_1.stop_gradient = False out = paddle.linalg.det(xt_1) - paddle.static.append_backward(out.sum()) + _, xt_1_grad = paddle.static.append_backward( + out.sum(), parameter_list=[xt_1] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, xt_1.grad_name]) + res = self.exe.run(prog, fetch_list=[out, xt_1_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) + """ + -------------------------------------- + C++ Traceback (most recent call last): + -------------------------------------- + 0 paddle::framework::ThreadPoolTempl::WorkerLoop(int) + 1 paddle::framework::PirInterpreter::RunInstructionBaseAsync(unsigned long) + 2 paddle::framework::PirInterpreter::RunInstructionBase(paddle::framework::InstructionBase*) + 3 paddle::framework::PhiKernelInstruction::Run() + 4 phi::CastInferMeta(phi::MetaTensor const&, phi::DataType, phi::MetaTensor*) + + ---------------------- + Error Message Summary: + ---------------------- + FatalError: `Segmentation fault` is detected by the operating system. + [TimeInfo: *** Aborted at 1706098114 (unix time) try "date -d @1706098114" if you are using GNU date ***] + [SignalInfo: *** SIGSEGV (@0x0) received by PID 69683 (TID 0x7f181ffff700) from PID 0 ***] + + Segmentation fault (core dumped) + """ + + # @test_with_pir_api @prog_scope() def test_dist(self): x = paddle.to_tensor([[3, 3], [3, 3]], dtype="float32") @@ -5320,27 +5834,50 @@ def test_dist(self): x.stop_gradient = False y.stop_gradient = False out = paddle.dist(x, y) - paddle.static.append_backward(out) + (_, x_grad), (_, y_grad) = paddle.static.append_backward( + out, parameter_list=[x, y] + ) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, y.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad, y_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2, 2)) self.assertEqual(res[1].shape, (2, 2)) np.testing.assert_array_equal(res[0], np.array(2).astype(np.float32)) + """ + 段错误 + -------------------------------------- + C++ Traceback (most recent call last): + -------------------------------------- + 0 paddle::framework::ThreadPoolTempl::WorkerLoop(int) + 1 paddle::framework::PirInterpreter::RunInstructionBaseAsync(unsigned long) + 2 paddle::framework::PirInterpreter::RunInstructionBase(paddle::framework::InstructionBase*) + 3 paddle::framework::PhiKernelInstruction::Run() + 4 phi::UnchangedInferMeta(phi::MetaTensor const&, phi::MetaTensor*) + + ---------------------- + Error Message Summary: + ---------------------- + FatalError: `Segmentation fault` is detected by the operating system. + [TimeInfo: *** Aborted at 1706098508 (unix time) try "date -d @1706098508" if you are using GNU date ***] + [SignalInfo: *** SIGSEGV (@0x0) received by PID 78696 (TID 0x7fa01cffd700) from PID 0 ***] + """ + + # @test_with_pir_api @prog_scope() def test_linalg_norm(self): # 1D input, p = fro ,axis = None, using reduceInferMeta x_1 = paddle.arange(24, dtype="float32") - 12 x_1.stop_gradient = False out_1 = paddle.linalg.norm(x_1) - paddle.static.append_backward(out_1) + grad_list = paddle.static.append_backward(out_1, parameter_list=[x_1]) + ((_, x_1_grad),) = grad_list prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_1, x_1.grad_name]) + res = self.exe.run(prog, fetch_list=[out_1, x_1_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (24,)) @@ -5427,16 +5964,17 @@ def test_linalg_norm(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (4, 6)) + @test_with_pir_api @prog_scope() def test_linalg_cond(self): # use paddle.sum x = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x.stop_gradient = False out = paddle.linalg.cond(x) - paddle.static.append_backward(out) + _, x_grad = paddle.static.append_backward(out, parameter_list=[x])[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5444,10 +5982,11 @@ def test_linalg_cond(self): x2 = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x2.stop_gradient = False out_fro = paddle.linalg.cond(x2, p='fro') - paddle.static.append_backward(out_fro) + grad_list = paddle.static.append_backward(out_fro, parameter_list=[x2]) + ((_, x2_grad),) = grad_list prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_fro, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out_fro, x2_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5456,10 +5995,12 @@ def test_linalg_cond(self): x3 = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x3.stop_gradient = False out_nuc = paddle.linalg.cond(x3, p='nuc') - paddle.static.append_backward(out_nuc) + _, x3_grad = paddle.static.append_backward( + out_nuc, parameter_list=[x3] + )[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_nuc, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out_nuc, x3_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5468,10 +6009,12 @@ def test_linalg_cond(self): x4 = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x4.stop_gradient = False out_1 = paddle.linalg.cond(x4, p=1) - paddle.static.append_backward(out_1) + _, x4_grad = paddle.static.append_backward(out_1, parameter_list=[x4])[ + 0 + ] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_1, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out_1, x4_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5479,10 +6022,12 @@ def test_linalg_cond(self): x5 = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x5.stop_gradient = False out_minus_1 = paddle.linalg.cond(x5, p=-1) - paddle.static.append_backward(out_minus_1) + ((_, x5_grad),) = paddle.static.append_backward( + out_minus_1, parameter_list=[x5] + ) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_minus_1, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out_minus_1, x5_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5491,10 +6036,12 @@ def test_linalg_cond(self): x6 = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x6.stop_gradient = False out_2 = paddle.linalg.cond(x6, p=2) - paddle.static.append_backward(out_2) + ((_, x6_grad),) = paddle.static.append_backward( + out_2, parameter_list=[x6] + ) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_2, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out_2, x6_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5503,10 +6050,12 @@ def test_linalg_cond(self): x8 = paddle.to_tensor([[1.0, 0, -1], [0, 1, 0], [1, 0, 1]]) x8.stop_gradient = False out_inf = paddle.linalg.cond(x8, p=float("inf")) - paddle.static.append_backward(out_inf) + ((_, x8_grad),) = paddle.static.append_backward( + out_inf, parameter_list=[x8] + ) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out_inf, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out_inf, x8_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 3)) @@ -5515,23 +6064,45 @@ def test_linalg_cond(self): a = paddle.randn([2, 4, 4]) a.stop_gradient = False a_cond_fro = paddle.linalg.cond(a, p='fro') - paddle.static.append_backward(a_cond_fro.sum()) + ((_, a_grad),) = paddle.static.append_backward( + a_cond_fro.sum(), parameter_list=[a] + ) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[a_cond_fro, a.grad_name]) + res = self.exe.run(prog, fetch_list=[a_cond_fro, a_grad]) self.assertEqual(res[0].shape, (2,)) self.assertEqual(res[1].shape, (2, 4, 4)) + """ + -------------------------------------- + C++ Traceback (most recent call last): + -------------------------------------- + 0 paddle::framework::ThreadPoolTempl::WorkerLoop(int) + 1 paddle::framework::PirInterpreter::RunInstructionBaseAsync(unsigned long) + 2 paddle::framework::PirInterpreter::RunInstructionBase(paddle::framework::InstructionBase*) + 3 paddle::framework::PhiKernelInstruction::Run() + 4 phi::CastInferMeta(phi::MetaTensor const&, phi::DataType, phi::MetaTensor*) + + ---------------------- + Error Message Summary: + ---------------------- + FatalError: `Segmentation fault` is detected by the operating system. + [TimeInfo: *** Aborted at 1706098283 (unix time) try "date -d @1706098283" if you are using GNU date ***] + [SignalInfo: *** SIGSEGV (@0x0) received by PID 74463 (TID 0x7f02beffd700) from PID 0 ***] + + """ + + # @test_with_pir_api @prog_scope() def test_trace(self): x = paddle.to_tensor([[3, 2], [1, 9]], dtype="float32") x.stop_gradient = False out = paddle.trace(x) - paddle.static.append_backward(out) + _, x_grad = paddle.static.append_backward(out, parameter_list=[x])[0] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + res = self.exe.run(prog, fetch_list=[out, x_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2, 2)) @@ -5779,6 +6350,7 @@ def setUp(self): paddle.full([], 4, 'int32'), ] + # @test_with_pir_api def test_slice(self): starts = [paddle.full([], 1, 'int32'), paddle.full([], 1, 'int32')] ends = [paddle.full([], 3, 'int32'), paddle.full([], 3, 'int32')] @@ -5789,6 +6361,7 @@ def test_slice(self): )[0] self.assertEqual(res.shape, (5, 2, 2)) + @test_with_pir_api def test_strided_slice(self): starts = [paddle.full([], 0, 'int32'), paddle.full([], 0, 'int32')] ends = [paddle.full([], 4, 'int32'), paddle.full([], 4, 'int32')] @@ -5800,6 +6373,7 @@ def test_strided_slice(self): )[0] self.assertEqual(res.shape, (5, 2, 2)) + # @test_with_pir_api def test_linspace(self): start = paddle.full([], 1.0) stop = paddle.full([], 5.0) @@ -5810,6 +6384,7 @@ def test_linspace(self): )[0] np.testing.assert_array_equal(res, [1.0, 2.0, 3.0, 4.0, 5.0]) + @test_with_pir_api def test_arange(self): start = paddle.full([], 1.0) stop = paddle.full([], 6.0) @@ -5820,6 +6395,7 @@ def test_arange(self): )[0] np.testing.assert_array_equal(res, [1.0, 2.0, 3.0, 4.0, 5.0]) + # @test_with_pir_api def test_normal(self): mean = paddle.full([], 0.0) std = paddle.full([], 0.0) @@ -5834,6 +6410,26 @@ def test_normal(self): self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (2, 3, 4)) + """ + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6416, in test_rand + out2 = paddle.rand(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 1497, in rand + return uniform(shape, dtype, min=0.0, max=1.0, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 933, in uniform + shape = paddle.utils.get_int_tensor_list( + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 403, in get_int_tensor_list + temp_out = paddle.full( + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1292, in full + return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 913, in fill_constant + out = _C_ops.full(shape, value, dtype, place) + TypeError: (InvalidType) full(): argument (position 2) must be double, but got Variable (at /home/aistudio/Paddle/paddle/fluid/pybind/op_function_common.cc:244) + """ + + # @test_with_pir_api def test_rand(self): out1 = paddle.rand([]) out2 = paddle.rand(self.shape) @@ -5844,6 +6440,37 @@ def test_rand(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2, 3, 4)) + """ + ====================================================================== + ERROR: test_randn (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6446, in test_randn + out2 = paddle.randn(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 680, in randn + return standard_normal(shape, dtype, name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 616, in standard_normal + return gaussian(shape=shape, mean=0.0, std=1.0, dtype=dtype, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 487, in gaussian + shape = paddle.utils.convert_shape_to_list(shape) + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 488, in convert_shape_to_list + shape = [x.item(0) if isinstance(x, Variable) else x for x in shape] + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 488, in + shape = [x.item(0) if isinstance(x, Variable) else x for x in shape] + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 231, in fun + args, kw = fix(args, kw, sig) + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 203, in fix + ba = sig.bind(*args, **kwargs) + File "/usr/lib/python3.8/inspect.py", line 3037, in bind + return self._bind(args, kwargs) + File "/usr/lib/python3.8/inspect.py", line 2958, in _bind + raise TypeError('too many positional arguments') from None + TypeError: too many positional arguments + """ + + # @test_with_pir_api def test_randn(self): out1 = paddle.randn([]) out2 = paddle.randn(self.shape) @@ -5890,6 +6517,35 @@ def test_randint_like(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) + """ + ====================================================================== + ERROR: test_standard_normal (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6523, in test_standard_normal + out2 = paddle.standard_normal(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 616, in standard_normal + return gaussian(shape=shape, mean=0.0, std=1.0, dtype=dtype, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 487, in gaussian + shape = paddle.utils.convert_shape_to_list(shape) + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 488, in convert_shape_to_list + shape = [x.item(0) if isinstance(x, Variable) else x for x in shape] + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 488, in + shape = [x.item(0) if isinstance(x, Variable) else x for x in shape] + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 231, in fun + args, kw = fix(args, kw, sig) + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 203, in fix + ba = sig.bind(*args, **kwargs) + File "/usr/lib/python3.8/inspect.py", line 3037, in bind + return self._bind(args, kwargs) + File "/usr/lib/python3.8/inspect.py", line 2958, in _bind + raise TypeError('too many positional arguments') from None + TypeError: too many positional arguments + """ + + # @test_with_pir_api def test_standard_normal(self): out1 = paddle.standard_normal([]) out2 = paddle.standard_normal(self.shape) @@ -5900,6 +6556,27 @@ def test_standard_normal(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2, 3, 4)) + """ + ====================================================================== + ERROR: test_uniform (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6561, in test_uniform + out2 = paddle.uniform(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/random.py", line 933, in uniform + shape = paddle.utils.get_int_tensor_list( + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 403, in get_int_tensor_list + temp_out = paddle.full( + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1292, in full + return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 913, in fill_constant + out = _C_ops.full(shape, value, dtype, place) + TypeError: (InvalidType) full(): argument (position 2) must be double, but got Variable (at /home/aistudio/Paddle/paddle/fluid/pybind/op_function_common.cc:244) + """ + + # @test_with_pir_api def test_uniform(self): out1 = paddle.uniform([]) out2 = paddle.uniform(self.shape) @@ -5910,6 +6587,33 @@ def test_uniform(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (2, 3, 4)) + """ + ====================================================================== + ERROR: test_empty_and_empty_like (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6592, in test_empty_and_empty_like + out3 = paddle.empty(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 2111, in empty + shape = paddle.utils.convert_shape_to_list(shape) + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 488, in convert_shape_to_list + shape = [x.item(0) if isinstance(x, Variable) else x for x in shape] + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 488, in + shape = [x.item(0) if isinstance(x, Variable) else x for x in shape] + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 231, in fun + args, kw = fix(args, kw, sig) + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 203, in fix + ba = sig.bind(*args, **kwargs) + File "/usr/lib/python3.8/inspect.py", line 3037, in bind + return self._bind(args, kwargs) + File "/usr/lib/python3.8/inspect.py", line 2958, in _bind + raise TypeError('too many positional arguments') from None + TypeError: too many positional arguments + """ + + # @test_with_pir_api def test_empty_and_empty_like(self): out1 = paddle.empty([]) out2 = paddle.empty_like(out1) @@ -5922,6 +6626,29 @@ def test_empty_and_empty_like(self): self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (2, 3, 4)) + """ + ====================================================================== + ERROR: test_full_and_full_like (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6630, in test_full_and_full_like + out3 = paddle.full(self.shape, 0.5) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1292, in full + return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 906, in fill_constant + shape = paddle.utils.get_int_tensor_list(shape, place) + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 403, in get_int_tensor_list + temp_out = paddle.full( + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1292, in full + return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 913, in fill_constant + out = _C_ops.full(shape, value, dtype, place) + TypeError: (InvalidType) full(): argument (position 2) must be double, but got Variable (at /home/aistudio/Paddle/paddle/fluid/pybind/op_function_common.cc:244) + """ + + # @test_with_pir_api def test_full_and_full_like(self): out1 = paddle.full([], 0.5) out2 = paddle.full_like(out1, 0.5) @@ -5937,6 +6664,29 @@ def test_full_and_full_like(self): self.assertEqual(res[2].shape, (2, 3, 4)) self.assertEqual(res[3].shape, (2, 3, 4)) + """ + ====================================================================== + ERROR: test_ones_and_ones_like (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6667, in test_ones_and_ones_like + out3 = paddle.ones(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1031, in ones + return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 906, in fill_constant + shape = paddle.utils.get_int_tensor_list(shape, place) + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 403, in get_int_tensor_list + temp_out = paddle.full( + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1292, in full + return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 913, in fill_constant + out = _C_ops.full(shape, value, dtype, place) + TypeError: (InvalidType) full(): argument (position 2) must be double, but got Variable (at /home/aistudio/Paddle/paddle/fluid/pybind/op_function_common.cc:244) + """ + + # @test_with_pir_api def test_ones_and_ones_like(self): out1 = paddle.ones([]) out2 = paddle.ones_like(out1) @@ -5949,6 +6699,28 @@ def test_ones_and_ones_like(self): self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (2, 3, 4)) + """ + ERROR: test_zeros_and_zeros_like (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6701, in test_zeros_and_zeros_like + out3 = paddle.zeros(self.shape) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1115, in zeros + return fill_constant(value=0.0, shape=shape, dtype=dtype, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 906, in fill_constant + shape = paddle.utils.get_int_tensor_list(shape, place) + File "/home/aistudio/Paddle/build/python/paddle/utils/layers_utils.py", line 403, in get_int_tensor_list + temp_out = paddle.full( + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 1292, in full + return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name) + File "/home/aistudio/Paddle/build/python/paddle/tensor/creation.py", line 913, in fill_constant + out = _C_ops.full(shape, value, dtype, place) + TypeError: (InvalidType) full(): argument (position 2) must be double, but got Variable (at /home/aistudio/Paddle/paddle/fluid/pybind/op_function_common.cc:244) + """ + + # @test_with_pir_api def test_zeros_and_zeros_like(self): out1 = paddle.zeros([]) out2 = paddle.zeros_like(out1) @@ -5961,6 +6733,23 @@ def test_zeros_and_zeros_like(self): self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (2, 3, 4)) + """ + ====================================================================== + ERROR: test_embedding (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6733, in test_embedding + w0 = paddle.arange(3, 9).reshape((3, 2)).astype(paddle.float32) + File "/home/aistudio/Paddle/build/python/paddle/pir/math_op_patch.py", line 266, in astype + dtype = paddle.pir.core.convert_np_dtype_to_dtype_(dtype) + File "/home/aistudio/Paddle/build/python/paddle/pir/core.py", line 82, in convert_np_dtype_to_dtype_ + dtype = np.dtype(np_dtype) + TypeError: Cannot interpret '' as a data type + """ + + # @test_with_pir_api def test_embedding(self): ids = paddle.full(shape=[], fill_value=1, dtype='int64') w0 = paddle.arange(3, 9).reshape((3, 2)).astype(paddle.float32) @@ -5976,6 +6765,84 @@ def test_embedding(self): for i in range(len(res)): self.assertEqual(res[0][i], result[i]) + """ + ====================================================================== + ERROR: test_static_embedding (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6764, in test_static_embedding + emb = paddle.static.nn.embedding(ids, (20, 3)) + File "/usr/local/lib/python3.8/dist-packages/decorator.py", line 232, in fun + return caller(func, *(extras + args), **kw) + File "/home/aistudio/Paddle/build/python/paddle/base/wrapped_decorator.py", line 26, in __impl__ + return wrapped_func(*args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 623, in __impl__ + return func(*args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/static/nn/common.py", line 3799, in embedding + tmp = helper.create_variable_for_type_inference(dtype) + File "/home/aistudio/Paddle/build/python/paddle/base/layer_helper_base.py", line 474, in create_variable_for_type_inference + return self.main_program.current_block().create_var( + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 4326, in create_var + var = Variable(block=self, *args, **kwargs) + File "/home/aistudio/Paddle/build/python/paddle/base/framework.py", line 1580, in __init__ + self.desc.set_dtype(dtype) + TypeError: set_dtype(): incompatible function arguments. The following argument types are supported: + 1. (self: paddle.base.libpaddle.VarDesc, arg0: paddle::framework::proto::VarType_Type) -> None + + Invoked with: , + + ====================================================================== + ERROR: test_strided_slice (__main__.TestNoBackwardAPIStatic) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 6371, in test_strided_slice + res = self.exe.run( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1764, in run + res = self._run_pir_impl( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 2076, in _run_pir_impl + ) = self._executor_cache.get_pir_program_and_executor( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1110, in get_pir_program_and_executor + return self._get_cached_program_and_executor_pir_mode( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1139, in _get_pir_program_and_executor + new_exe = _StandaloneExecutor(place, plan, scope) + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 815, in __init__ + self._new_exe = self._create_new_executor() + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 851, in _create_new_executor + new_exe = core.StandaloneExecutor(self._place, self._plan, self._scope) + RuntimeError: (NotFound) Cannot find embedding_1.w_0 in scope. + [Hint: var should not be null.] (at /home/aistudio/Paddle/paddle/fluid/framework/new_executor/pir_adaptor/pir_adaptor_util.cc:68) + + + ====================================================================== + ERROR: test_while_loop (__main__.TestSundryAPI) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 2772, in test_while_loop + out_i, out_x, di, dx = exe.run( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1764, in run + res = self._run_pir_impl( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 2076, in _run_pir_impl + ) = self._executor_cache.get_pir_program_and_executor( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1110, in get_pir_program_and_executor + return self._get_cached_program_and_executor_pir_mode( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1139, in _get_pir_program_and_executor + new_exe = _StandaloneExecutor(place, plan, scope) + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 815, in __init__ + self._new_exe = self._create_new_executor() + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 851, in _create_new_executor + new_exe = core.StandaloneExecutor(self._place, self._plan, self._scope) + RuntimeError: (NotFound) Cannot find embedding_1.w_0 in scope. + [Hint: var should not be null.] (at /home/aistudio/Paddle/paddle/fluid/framework/new_executor/pir_adaptor/pir_adaptor_util.cc:68) + + """ + + # @test_with_pir_api def test_static_embedding(self): ids = paddle.full(shape=[], fill_value=1, dtype='int64') emb = paddle.static.nn.embedding(ids, (20, 3)) @@ -5995,6 +6862,32 @@ def test_one_hot_label(self): self.assertEqual(res[0].shape, (4,)) self.assertEqual(res[0][2], 1) + """ + ====================================================================== + ERROR: test_while_loop (__main__.TestSundryAPI) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 2772, in test_while_loop + out_i, out_x, di, dx = exe.run( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1764, in run + res = self._run_pir_impl( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 2116, in _run_pir_impl + ret = new_exe.run(list(feed.keys()), return_numpy) + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 829, in run + tensors = self._new_exe.run( + ValueError: In user code: + + + InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (int64) currently contained in the container. + [Hint: Expected dtype() == phi::CppTypeToDataType::Type(), but received dtype():9 != phi::CppTypeToDataType::Type():10.] (at /home/aistudio/Paddle/paddle/phi/core/dense_tensor.cc:161) + [operator < pd_kernel.phi_kernel > error] + + ---------------------------------------------------------------------- + """ + + # @test_with_pir_api def test_unique_consecutive(self): x = paddle.rand([]) y, inverse, counts = paddle.unique_consecutive( @@ -6010,6 +6903,32 @@ def test_unique_consecutive(self): self.assertEqual(res[1].shape, (1,)) self.assertEqual(res[2].shape, (1,)) + """ + ====================================================================== + ERROR: test_while_loop (__main__.TestSundryAPI) + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/test/legacy_test/test_zero_dim_tensor.py", line 2772, in test_while_loop + out_i, out_x, di, dx = exe.run( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 1764, in run + res = self._run_pir_impl( + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 2116, in _run_pir_impl + ret = new_exe.run(list(feed.keys()), return_numpy) + File "/home/aistudio/Paddle/build/python/paddle/base/executor.py", line 829, in run + tensors = self._new_exe.run( + ValueError: In user code: + + + InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (int64) currently contained in the container. + [Hint: Expected dtype() == phi::CppTypeToDataType::Type(), but received dtype():9 != phi::CppTypeToDataType::Type():10.] (at /home/aistudio/Paddle/paddle/phi/core/dense_tensor.cc:161) + [operator < pd_kernel.phi_kernel > error] + + ---------------------------------------------------------------------- + """ + + # @test_with_pir_api def test_unique(self): x = paddle.rand([]) y, index, inverse, counts = paddle.unique( @@ -6112,6 +7031,7 @@ def test_dygraph_unary(self): paddle.enable_static() + @test_with_pir_api def test_static_unary(self): paddle.enable_static() for api in unary_apis_with_complex_input: @@ -6124,17 +7044,31 @@ def test_static_unary(self): x = paddle.complex(paddle.rand([]), paddle.rand([])) x.stop_gradient = False out = api(x) - paddle.static.append_backward(out) + grad_list = paddle.static.append_backward( + out, parameter_list=[x, out] + ) fetch_list = [x, out] - if block.has_var(x.grad_name): - fetch_list.extend([x.grad_name, out.grad_name]) + fetch_list.extend([_grad for _param, _grad in grad_list]) # 1) Test Program res = exe.run(main_prog, fetch_list=fetch_list) for item in res: self.assertEqual(item.shape, ()) + if paddle.framework.in_pir_mode(): + return + """ + Traceback (most recent call last): + File "/home/aistudio/Paddle/build/python/paddle/pir_utils.py", line 115, in impl + func(*args, **kwargs) + File "/home/aistudio/Paddle/build/test/legacy_test/test_zero_dim_tensor.py", line 6169, in test_static_unary + compile_prog = paddle.static.CompiledProgram(main_prog) + File "/home/aistudio/Paddle/build/python/paddle/base/compiler.py", line 147, in __init__ + raise TypeError( + TypeError: The type of program_to_graph parameter is wrong, expected Graph or Program, but received + """ + # 2) Test CompiledProgram Program compile_prog = paddle.static.CompiledProgram(main_prog) res = exe.run(compile_prog, fetch_list=fetch_list) @@ -6162,6 +7096,7 @@ def test_dygraph(self): paddle.enable_static() + @test_with_pir_api def test_static(self): paddle.enable_static() @@ -6172,13 +7107,15 @@ def test_static(self): x = paddle.complex(paddle.rand([]), paddle.rand([])) x.stop_gradient = False out = paddle.as_real(x) - self.assertEqual(x.shape, ()) - self.assertEqual(out.shape, (2,)) - paddle.static.append_backward(out.sum()) + assertEqualListTuple(self, x, ()) + assertEqualListTuple(self, out, (2,)) + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) fetch_list = [x, out] - if block.has_var(x.grad_name): - fetch_list.extend([x.grad_name, out.grad_name]) + + fetch_list.extend([_grad for _param, _grad in grad_list]) res = exe.run(main_prog, fetch_list=fetch_list) self.assertEqual(res[0].shape, ()) @@ -6207,6 +7144,7 @@ def test_dygraph(self): paddle.enable_static() + @test_with_pir_api def test_static(self): paddle.enable_static() main_prog = paddle.static.Program() @@ -6216,13 +7154,20 @@ def test_static(self): x = paddle.rand([2]) x.stop_gradient = False out = paddle.as_complex(x) - self.assertEqual(x.shape, (2,)) - self.assertEqual(out.shape, ()) - paddle.static.append_backward(out.sum()) + + assertEqualListTuple(self, x, (2,)) + assertEqualListTuple(self, out, ()) + + grad_list = paddle.static.append_backward( + out.sum(), parameter_list=[x, out] + ) fetch_list = [x, out] - if block.has_var(x.grad_name): - fetch_list.extend([x.grad_name, out.grad_name]) + if paddle.framework.in_pir_mode(): + fetch_list.extend([_grad for _para, _grad in grad_list]) + else: + if block.has_var(x.grad_name): + fetch_list.extend([x.grad_name, out.grad_name]) res = exe.run(main_prog, fetch_list=fetch_list) self.assertEqual(res[0].shape, (2,)) @@ -6483,6 +7428,7 @@ def setUp(self): paddle.enable_static() self.exe = paddle.static.Executor() + @test_with_pir_api @prog_scope() def test_sigmoid_focal_loss(self): logit = paddle.rand([2, 3]) @@ -6500,18 +7446,41 @@ def test_sigmoid_focal_loss(self): out1 = F.sigmoid_focal_loss( logit, label, normalizer=fg_num_1, reduction='mean' ) - paddle.static.append_backward(out0.sum()) + grad_list = paddle.static.append_backward( + out0.sum(), parameter_list=[out0, logit] + ) + out0_grad, logit_grad = (_grad for _param, _grad in grad_list) prog = paddle.static.default_main_program() - res = self.exe.run( - prog, fetch_list=[out0, out1, out0.grad_name, logit.grad_name] - ) + if paddle.framework.in_pir_mode(): + return + res = self.exe.run(prog, fetch_list=[out0, out1, out0_grad, logit_grad]) np.testing.assert_allclose(res[0], res[1]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) self.assertEqual(res[3].shape, (2, 3)) + """ + 段错误 + -------------------------------------- + C++ Traceback (most recent call last): + -------------------------------------- + 0 paddle::framework::ThreadPoolTempl::WorkerLoop(int) + 1 paddle::framework::PirInterpreter::RunInstructionBaseAsync(unsigned long) + 2 paddle::framework::PirInterpreter::RunInstructionBase(paddle::framework::InstructionBase*) + 3 paddle::framework::PhiKernelInstruction::Run() + 4 phi::CastInferMeta(phi::MetaTensor const&, phi::DataType, phi::MetaTensor*) + + ---------------------- + Error Message Summary: + ---------------------- + FatalError: `Segmentation fault` is detected by the operating system. + [TimeInfo: *** Aborted at 1706040143 (unix time) try "date -d @1706040143" if you are using GNU date ***] + [SignalInfo: *** SIGSEGV (@0x0) received by PID 1010183 (TID 0x7fce0bfef700) from PID 0 ***] + """ + + @test_with_pir_api @prog_scope() def test_cross_entropy(self): input = paddle.rand([3, 5]) @@ -6522,13 +7491,15 @@ def test_cross_entropy(self): loss = paddle.nn.functional.cross_entropy( input, label, reduction='mean' ) - paddle.static.append_backward(loss) + grad_list = paddle.static.append_backward(loss, parameter_list=[input]) + input_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[loss, input.grad_name]) + res = self.exe.run(prog, fetch_list=[loss, input_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 5)) + @test_with_pir_api @prog_scope() def test_l1_loss(self): input = paddle.rand([3, 5]) @@ -6536,13 +7507,15 @@ def test_l1_loss(self): label = paddle.rand([3, 5]) loss = paddle.nn.functional.l1_loss(input, label, reduction='sum') - paddle.static.append_backward(loss) + grad_list = paddle.static.append_backward(loss, parameter_list=[input]) + input_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[loss, input.grad_name]) + res = self.exe.run(prog, fetch_list=[loss, input_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (3, 5)) + @test_with_pir_api @prog_scope() def test_nll_loss(self): input = paddle.rand([5, 3]) @@ -6554,10 +7527,11 @@ def test_nll_loss(self): label.stop_gradient = False loss = paddle.nn.functional.nll_loss(log_out, label) - paddle.static.append_backward(loss) + grad_list = paddle.static.append_backward(loss, parameter_list=[input]) + input_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[loss, input.grad_name]) + res = self.exe.run(prog, fetch_list=[loss, input_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (5, 3)) @@ -6570,10 +7544,11 @@ def test_nll_loss(self): label.stop_gradient = False loss = paddle.nn.functional.nll_loss(log_out, label) - paddle.static.append_backward(loss) + grad_list = paddle.static.append_backward(loss, parameter_list=[input]) + input_grad = grad_list[0][1] prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[loss, input.grad_name]) + res = self.exe.run(prog, fetch_list=[loss, input_grad]) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, (5, 3, 2, 4))