From 65ace21ecd1e1875fec40e0270100a3e6b53b4ab Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Wed, 29 Nov 2023 08:57:02 +0000 Subject: [PATCH 1/7] =?UTF-8?q?add=20261=E3=80=81273=E3=80=81283=E3=80=812?= =?UTF-8?q?85=E3=80=81286=E3=80=81313=E3=80=81315?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/logic.py | 6 +- test/legacy_test/test_bce_with_logits_loss.py | 12 ++-- test/legacy_test/test_is_tensor.py | 3 + test/legacy_test/test_median.py | 9 +-- test/legacy_test/test_nanmean_api.py | 3 + test/legacy_test/test_nansum_api.py | 3 + test/legacy_test/test_neg_op.py | 2 + test/legacy_test/test_nll_loss.py | 62 +++++++++++-------- test/legacy_test/test_unflatten.py | 49 +++++++++------ test/legacy_test/test_variance_layer.py | 17 +++-- 10 files changed, 107 insertions(+), 59 deletions(-) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index ff4f089d49a81..6327afbfef044 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1106,8 +1106,10 @@ def is_tensor(x): False """ - if in_dynamic_mode(): - return isinstance(x, (Tensor, paddle.base.core.eager.Tensor)) + if in_dynamic_or_pir_mode(): + return isinstance( + x, (Tensor, paddle.base.core.eager.Tensor, paddle.pir.OpResult) + ) else: return isinstance(x, Variable) diff --git a/test/legacy_test/test_bce_with_logits_loss.py b/test/legacy_test/test_bce_with_logits_loss.py index da19cd1d722b7..3bc694ec53b32 100644 --- a/test/legacy_test/test_bce_with_logits_loss.py +++ b/test/legacy_test/test_bce_with_logits_loss.py @@ -167,7 +167,7 @@ def test_BCEWithLogitsLoss(self): np.testing.assert_allclose(dy_functional, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): static_result = test_static( place, logit_np, label_np, reduction=reduction ) @@ -192,7 +192,7 @@ def test_dynamic_or_pir_mode(): static_functional, dy_functional, rtol=1e-05 ) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_BCEWithLogitsLoss_weight(self): logit_np = np.random.uniform(0.1, 0.8, size=(2, 3, 4, 10)).astype( @@ -230,7 +230,7 @@ def test_BCEWithLogitsLoss_weight(self): np.testing.assert_allclose(dy_functional, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): static_result = test_static( place, logit_np, @@ -257,7 +257,7 @@ def test_dynamic_or_pir_mode(): static_functional, dy_functional, rtol=1e-05 ) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_BCEWithLogitsLoss_pos_weight(self): logit_np = np.random.uniform(0.1, 0.8, size=(2, 3, 4, 10)).astype( @@ -294,7 +294,7 @@ def test_BCEWithLogitsLoss_pos_weight(self): np.testing.assert_allclose(dy_functional, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): static_result = test_static( place, logit_np, label_np, weight_np, reduction, pos_weight_np ) @@ -315,7 +315,7 @@ def test_dynamic_or_pir_mode(): static_functional, dy_functional, rtol=1e-05 ) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_BCEWithLogitsLoss_error(self): paddle.disable_static() diff --git a/test/legacy_test/test_is_tensor.py b/test/legacy_test/test_is_tensor.py index aad03fb75a1d2..55f3f1e2a38b0 100644 --- a/test/legacy_test/test_is_tensor.py +++ b/test/legacy_test/test_is_tensor.py @@ -15,6 +15,7 @@ import unittest import paddle +from paddle.pir_utils import test_with_pir_api DELTA = 0.00001 @@ -49,10 +50,12 @@ def setUp(self): def tearDown(self): paddle.disable_static() + @test_with_pir_api def test_is_tensor(self): x = paddle.rand([3, 2, 4], dtype='float32') self.assertTrue(paddle.is_tensor(x)) + @test_with_pir_api def test_is_tensor_array(self): x = paddle.tensor.create_array('float32') self.assertTrue(paddle.is_tensor(x)) diff --git a/test/legacy_test/test_median.py b/test/legacy_test/test_median.py index 738f98ed782a9..31750afe69fc5 100644 --- a/test/legacy_test/test_median.py +++ b/test/legacy_test/test_median.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.static import Program, program_guard +from paddle.pir_utils import test_with_pir_api DELTA = 1e-6 @@ -32,10 +32,10 @@ def static_single_test_median(self, lis_test): paddle.enable_static() x, axis, keepdims = lis_test res_np = np.median(x, axis=axis, keepdims=keepdims) - main_program = Program() - startup_program = Program() + main_program = paddle.static.Program() + startup_program = paddle.static.Program() exe = paddle.static.Executor() - with program_guard(main_program, startup_program): + with paddle.static.program_guard(main_program, startup_program): x_in = paddle.static.data(shape=x.shape, dtype=x.dtype, name='x') y = paddle.median(x_in, axis, keepdims) [res_pd] = exe.run(feed={'x': x}, fetch_list=[y]) @@ -48,6 +48,7 @@ def dygraph_single_test_median(self, lis_test): res_pd = paddle.median(paddle.to_tensor(x), axis, keepdims) self.check_numpy_res(res_pd.numpy(False), res_np) + @test_with_pir_api def test_median_static(self): h = 3 w = 4 diff --git a/test/legacy_test/test_nanmean_api.py b/test/legacy_test/test_nanmean_api.py index 58c47a77702f3..81e807e029b97 100644 --- a/test/legacy_test/test_nanmean_api.py +++ b/test/legacy_test/test_nanmean_api.py @@ -18,6 +18,7 @@ import paddle from paddle.base import core +from paddle.pir_utils import test_with_pir_api np.random.seed(10) @@ -38,6 +39,7 @@ def setUp(self): else paddle.CPUPlace() ) + @test_with_pir_api def test_api_static(self): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): @@ -87,6 +89,7 @@ def test_case(x, axis=None, keepdim=False): test_case(self.x, [0, 1, 2, 3]) paddle.enable_static() + @test_with_pir_api def test_errors(self): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): diff --git a/test/legacy_test/test_nansum_api.py b/test/legacy_test/test_nansum_api.py index 050ff6be2c8b2..00984f1c6be9f 100644 --- a/test/legacy_test/test_nansum_api.py +++ b/test/legacy_test/test_nansum_api.py @@ -18,9 +18,11 @@ import paddle from paddle import base +from paddle.pir_utils import test_with_pir_api class API_Test_Nansum(unittest.TestCase): + @test_with_pir_api def test_static_graph(self): paddle.enable_static() startup_program = base.Program() @@ -75,6 +77,7 @@ def test_static_graph(self): ) # test nansum api with float16 + @test_with_pir_api def test_static_graph_fp16(self): paddle.enable_static() startup_program = paddle.static.Program() diff --git a/test/legacy_test/test_neg_op.py b/test/legacy_test/test_neg_op.py index 8e17344dfe1b9..5315a916eb0d7 100644 --- a/test/legacy_test/test_neg_op.py +++ b/test/legacy_test/test_neg_op.py @@ -17,6 +17,7 @@ import numpy as np import paddle +from paddle.pir_utils import test_with_pir_api class TestNegOp(unittest.TestCase): @@ -35,6 +36,7 @@ def run_imperative(self): dy_result.numpy(), expected_result, rtol=1e-05 ) + @test_with_pir_api def run_static(self, use_gpu=False): input = paddle.static.data( name='input', shape=[32, 8], dtype=self.dtype diff --git a/test/legacy_test/test_nll_loss.py b/test/legacy_test/test_nll_loss.py index a27f84a759455..9f1d459f37310 100644 --- a/test/legacy_test/test_nll_loss.py +++ b/test/legacy_test/test_nll_loss.py @@ -102,7 +102,7 @@ def test_NLLLoss_1D_mean(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -125,7 +125,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_1D_sum(self): np.random.seed(200) @@ -141,7 +141,7 @@ def test_NLLLoss_1D_sum(self): expected = nll_loss_1d(input_np, label_np, reduction='sum')[0] @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -179,7 +179,7 @@ def test_dynamic_or_pir_mode(): loss = eager_res.sum() loss.backward() - test_dynamic_or_pir_mode() + test_static_or_pir_mode() np.testing.assert_allclose(dy_result, expected, rtol=1e-05) np.testing.assert_allclose(eager_result, expected, rtol=1e-05) @@ -200,7 +200,7 @@ def test_NLLLoss_1D_with_weight_mean(self): expected = nll_loss_1d(input_np, label_np, weight=weight_np)[0] @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -247,7 +247,7 @@ def test_dynamic_or_pir_mode(): loss.backward() eager_result = eager_res.numpy() - test_dynamic_or_pir_mode() + test_static_or_pir_mode() np.testing.assert_allclose(dy_result, expected, rtol=1e-05) np.testing.assert_allclose(eager_result, expected, rtol=1e-05) @@ -281,7 +281,7 @@ def test_NLLLoss_1D_with_weight_sum(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -312,7 +312,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_1D_with_weight_mean_cpu(self): np.random.seed(200) @@ -336,7 +336,7 @@ def test_NLLLoss_1D_with_weight_mean_cpu(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -365,7 +365,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_1D_with_weight_no_reduce_cpu(self): np.random.seed(200) @@ -391,7 +391,7 @@ def test_NLLLoss_1D_with_weight_no_reduce_cpu(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -422,7 +422,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_2D_mean(self): np.random.seed(200) @@ -447,7 +447,7 @@ def test_NLLLoss_2D_mean(self): # place = base.CPUPlace() @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -469,6 +469,8 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) + test_static_or_pir_mode() + def test_NLLLoss_2D_sum(self): np.random.seed(200) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) @@ -494,7 +496,7 @@ def test_NLLLoss_2D_sum(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -516,6 +518,8 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) + test_static_or_pir_mode() + def test_NLLLoss_2D_with_weight_mean(self): np.random.seed(200) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) @@ -543,7 +547,7 @@ def test_NLLLoss_2D_with_weight_mean(self): # place = base.CPUPlace() @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -573,6 +577,8 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) + test_static_or_pir_mode() + def test_NLLLoss_2D_with_weight_mean_cpu(self): np.random.seed(200) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) @@ -594,7 +600,7 @@ def test_NLLLoss_2D_with_weight_mean_cpu(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -624,6 +630,8 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) + test_static_or_pir_mode() + def test_NLLLoss_2D_with_weight_sum(self): np.random.seed(200) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) @@ -652,7 +660,7 @@ def test_NLLLoss_2D_with_weight_sum(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -684,6 +692,8 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) + test_static_or_pir_mode() + def test_NLLLoss_in_dims_not_2or4_mean(self): np.random.seed(200) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) @@ -713,7 +723,7 @@ def test_NLLLoss_in_dims_not_2or4_mean(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -735,7 +745,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self): np.random.seed(200) @@ -771,7 +781,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -801,6 +811,8 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(dy_result, static_result, rtol=1e-05) + test_static_or_pir_mode() + def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self): np.random.seed(200) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) @@ -839,7 +851,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -870,7 +882,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(dy_result, static_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self): np.random.seed(200) @@ -912,7 +924,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self): np.testing.assert_allclose(dy_result, expected, rtol=1e-05) @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): @@ -943,7 +955,7 @@ def test_dynamic_or_pir_mode(): np.testing.assert_allclose(static_result, expected, rtol=1e-05) np.testing.assert_allclose(static_result, dy_result, rtol=1e-05) - test_dynamic_or_pir_mode() + test_static_or_pir_mode() def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self): np.random.seed(200) @@ -979,7 +991,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self): place = base.CPUPlace() @test_with_pir_api - def test_dynamic_or_pir_mode(): + def test_static_or_pir_mode(): prog = paddle.static.Program() startup_prog = paddle.static.Program() with paddle.static.program_guard(prog, startup_prog): diff --git a/test/legacy_test/test_unflatten.py b/test/legacy_test/test_unflatten.py index d26faba56dfa8..ac8b72879dd5c 100644 --- a/test/legacy_test/test_unflatten.py +++ b/test/legacy_test/test_unflatten.py @@ -17,6 +17,7 @@ import numpy as np import paddle +from paddle.pir_utils import test_with_pir_api def numpy_unflatten(x, axis, shape): @@ -91,6 +92,7 @@ def test_dygraph(self): self.setUp() self.func_dygraph() + @test_with_pir_api def test_static(self): paddle.enable_static() places = [paddle.CPUPlace()] @@ -269,34 +271,45 @@ def setUp(self): self.places.append(paddle.CUDAPlace(0)) def test_layer(self): - paddle.enable_static() places = [paddle.CPUPlace()] if paddle.device.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): - x = paddle.static.data( - name="x", dtype=self.x.dtype, shape=self.x.shape - ) - exe = paddle.static.Executor(place) - unflatten = paddle.nn.Unflatten(self.axis, self.shape) - out = unflatten(x) - static_ret = exe.run( - paddle.static.default_main_program(), - feed={"x": self.x, "axis": self.axis, "shape": self.shape}, - fetch_list=[out], - )[0] - for place in self.places: paddle.disable_static() x = paddle.to_tensor(self.x, dtype='float32', place=place) unflatten = paddle.nn.Unflatten(self.axis, self.shape) - dy_ret_value = unflatten(self.x) - np.testing.assert_array_equal(static_ret, dy_ret_value) + dy_ret_value = unflatten(x) + + paddle.enable_static() + + @test_with_pir_api + def test_static_or_pir_mode(): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.static.data( + name="x", dtype=self.x.dtype, shape=self.x.shape + ) + exe = paddle.static.Executor(place) + unflatten = paddle.nn.Unflatten(self.axis, self.shape) + out = unflatten(x) + static_ret = exe.run( + paddle.static.default_main_program(), + feed={ + "x": self.x, + "axis": self.axis, + "shape": self.shape, + }, + fetch_list=[out], + )[0] + + np.testing.assert_array_equal(static_ret, dy_ret_value) + + test_static_or_pir_mode() class TestLayerName(unittest.TestCase): + @test_with_pir_api def test_name(self): self.x = np.random.randn(3, 4, 4, 5).astype('float32') self.axis = 1 diff --git a/test/legacy_test/test_variance_layer.py b/test/legacy_test/test_variance_layer.py index d4dc28f8f6cce..43e2602029e84 100644 --- a/test/legacy_test/test_variance_layer.py +++ b/test/legacy_test/test_variance_layer.py @@ -17,6 +17,7 @@ import numpy as np import paddle +from paddle.pir_utils import test_with_pir_api def ref_var(x, axis=None, unbiased=True, keepdim=False): @@ -64,10 +65,17 @@ def dygraph(self): def test_api(self): out_ref = ref_var(self.x, self.axis, self.unbiased, self.keepdim) out_dygraph = self.dygraph() - out_static = self.static() - for out in [out_dygraph, out_static]: - np.testing.assert_allclose(out_ref, out, rtol=1e-05) - self.assertTrue(np.equal(out_ref.shape, out.shape).all()) + + np.testing.assert_allclose(out_ref, out_dygraph, rtol=1e-05) + self.assertTrue(np.equal(out_ref.shape, out_dygraph.shape).all()) + + @test_with_pir_api + def test_static_or_pir_mode(): + out_static = self.static() + np.testing.assert_allclose(out_ref, out_static, rtol=1e-05) + self.assertTrue(np.equal(out_ref.shape, out_static.shape).all()) + + test_static_or_pir_mode() class TestVarAPI_dtype(TestVarAPI): @@ -113,6 +121,7 @@ def test_alias(self): class TestVarError(unittest.TestCase): + @test_with_pir_api def test_error(self): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [2, 3, 4], 'int32') From c35a1e465662fe53560d5e986c99e48676d96183 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Wed, 29 Nov 2023 16:59:22 +0000 Subject: [PATCH 2/7] fix nanmedian && unflatten --- python/paddle/tensor/manipulation.py | 2 +- python/paddle/tensor/stat.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 5bec599390fdb..3acabed4ea4a6 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5596,7 +5596,7 @@ def unflatten(x, axis, shape, name=None): new_shape = ( list(x.shape[:axis]) + list(shape) + list(x.shape[axis + 1 :]) ) - elif isinstance(shape, Variable): + elif isinstance(shape, (Variable, paddle.pir.OpResult)): # The data type returned by `paddle.shape` is only 'int32'. new_shape = paddle.concat( [ diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index d7bcc48c8fa45..b2aec64c76280 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -313,7 +313,7 @@ def nanmedian(x, axis=None, keepdim=False, name=None): >>> print(y4.numpy()) 2.0 """ - if not isinstance(x, Variable): + if not isinstance(x, (Variable, paddle.pir.OpResult)): raise TypeError("In median, the input x should be a Tensor.") if isinstance(axis, (list, tuple)) and len(axis) == 0: From f796051eb8dff8907a929886516c519ee37b793a Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Fri, 1 Dec 2023 02:21:33 +0000 Subject: [PATCH 3/7] add DataType.FLOAT64 --- python/paddle/tensor/stat.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index b2aec64c76280..a7f10408f63b5 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -16,6 +16,7 @@ import paddle from paddle import _C_ops +from paddle.base.libpaddle import DataType from paddle.framework import in_dynamic_mode, in_dynamic_or_pir_mode from ..base.data_feeder import check_type, check_variable_and_dtype @@ -403,7 +404,7 @@ def median(x, axis=None, keepdim=False, name=None): [[4., 5., 6., 7.]]) """ - if not isinstance(x, Variable): + if not isinstance(x, (Variable, paddle.pir.OpResult)): raise TypeError("In median, the input x should be a Tensor.") if x.size == 0: @@ -435,7 +436,11 @@ def median(x, axis=None, keepdim=False, name=None): sz = x.shape[axis] kth = sz >> 1 tensor_topk, idx = paddle.topk(x, kth + 1, axis=axis, largest=False) - dtype = 'float64' if x.dtype == core.VarDesc.VarType.FP64 else 'float32' + dtype = ( + 'float64' + if x.dtype in [core.VarDesc.VarType.FP64, DataType.FLOAT64] + else 'float32' + ) if sz & 1 == 0: out_tensor = paddle.slice( tensor_topk, axes=[axis], starts=[kth - 1], ends=[kth] From 333802fe71bf6f748efcc9e4de62203eee224183 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Fri, 1 Dec 2023 07:55:40 +0000 Subject: [PATCH 4/7] fix x.size err in pir mode --- python/paddle/tensor/stat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index a7f10408f63b5..25191dee98bcc 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -407,7 +407,8 @@ def median(x, axis=None, keepdim=False, name=None): if not isinstance(x, (Variable, paddle.pir.OpResult)): raise TypeError("In median, the input x should be a Tensor.") - if x.size == 0: + if in_dynamic_mode() and x.size == 0: + # TODO: Currently, `__eq__` don't support arguments (`pir.Value` & `int`) raise ValueError("In median, the size of input x should not be 0.") is_flatten = False From 77cfabb517224e5e7986253312089ff23ab45ac5 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Fri, 1 Dec 2023 08:57:01 +0000 Subject: [PATCH 5/7] add test_neg_op --- test/legacy_test/test_neg_op.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/test/legacy_test/test_neg_op.py b/test/legacy_test/test_neg_op.py index 5315a916eb0d7..4ebb39088265c 100644 --- a/test/legacy_test/test_neg_op.py +++ b/test/legacy_test/test_neg_op.py @@ -38,25 +38,26 @@ def run_imperative(self): @test_with_pir_api def run_static(self, use_gpu=False): - input = paddle.static.data( - name='input', shape=[32, 8], dtype=self.dtype - ) - result = paddle.neg(input) - - place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace() - exe = paddle.static.Executor(place) - exe.run(paddle.static.default_startup_program()) - st_result = exe.run(feed={"input": self.input}, fetch_list=[result]) - expected_result = np.negative(self.input) - np.testing.assert_allclose(st_result[0], expected_result, rtol=1e-05) + with paddle.static.program_guard(paddle.static.Program()): + input = paddle.static.data( + name='input', shape=[32, 8], dtype=self.dtype + ) + result = paddle.neg(input) + + place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace() + exe = paddle.static.Executor(place) + exe.run(paddle.static.default_startup_program()) + st_result = exe.run(feed={"input": self.input}, fetch_list=[result]) + expected_result = np.negative(self.input) + np.testing.assert_allclose( + st_result[0], expected_result, rtol=1e-05 + ) def test_cpu(self): paddle.disable_static(place=paddle.CPUPlace()) self.run_imperative() paddle.enable_static() - - with paddle.static.program_guard(paddle.static.Program()): - self.run_static() + self.run_static() def test_gpu(self): if not paddle.base.core.is_compiled_with_cuda(): @@ -65,9 +66,7 @@ def test_gpu(self): paddle.disable_static(place=paddle.CUDAPlace(0)) self.run_imperative() paddle.enable_static() - - with paddle.static.program_guard(paddle.static.Program()): - self.run_static(use_gpu=True) + self.run_static(use_gpu=True) class TestNegOpFp32(TestNegOp): From 3e7fdb66187d620ec2f28373d7feab0cb36b5c5c Mon Sep 17 00:00:00 2001 From: Ryan <44900829+DrRyanHuang@users.noreply.github.com> Date: Mon, 4 Dec 2023 11:21:18 +0800 Subject: [PATCH 6/7] OpResult => Value Co-authored-by: WangZhen <23097963+0x45f@users.noreply.github.com> --- python/paddle/tensor/logic.py | 2 +- python/paddle/tensor/manipulation.py | 2 +- python/paddle/tensor/stat.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 6327afbfef044..5bff9269aee20 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1108,7 +1108,7 @@ def is_tensor(x): """ if in_dynamic_or_pir_mode(): return isinstance( - x, (Tensor, paddle.base.core.eager.Tensor, paddle.pir.OpResult) + x, (Tensor, paddle.base.core.eager.Tensor, paddle.pir.Value) ) else: return isinstance(x, Variable) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 3acabed4ea4a6..50b072c6fb1ed 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5596,7 +5596,7 @@ def unflatten(x, axis, shape, name=None): new_shape = ( list(x.shape[:axis]) + list(shape) + list(x.shape[axis + 1 :]) ) - elif isinstance(shape, (Variable, paddle.pir.OpResult)): + elif isinstance(shape, (Variable, paddle.pir.Value)): # The data type returned by `paddle.shape` is only 'int32'. new_shape = paddle.concat( [ diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index 25191dee98bcc..63043cf9cebf1 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -314,7 +314,7 @@ def nanmedian(x, axis=None, keepdim=False, name=None): >>> print(y4.numpy()) 2.0 """ - if not isinstance(x, (Variable, paddle.pir.OpResult)): + if not isinstance(x, (Variable, paddle.pir.Value)): raise TypeError("In median, the input x should be a Tensor.") if isinstance(axis, (list, tuple)) and len(axis) == 0: @@ -404,7 +404,7 @@ def median(x, axis=None, keepdim=False, name=None): [[4., 5., 6., 7.]]) """ - if not isinstance(x, (Variable, paddle.pir.OpResult)): + if not isinstance(x, (Variable, paddle.pir.Value)): raise TypeError("In median, the input x should be a Tensor.") if in_dynamic_mode() and x.size == 0: From a53427c6c7fe4cf9ea89bcec6eb51618735d10a6 Mon Sep 17 00:00:00 2001 From: Ryan <44900829+DrRyanHuang@users.noreply.github.com> Date: Mon, 4 Dec 2023 11:27:26 +0800 Subject: [PATCH 7/7] base.Program => static.Program --- test/legacy_test/test_nansum_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/legacy_test/test_nansum_api.py b/test/legacy_test/test_nansum_api.py index 00984f1c6be9f..4aa2226a4f056 100644 --- a/test/legacy_test/test_nansum_api.py +++ b/test/legacy_test/test_nansum_api.py @@ -25,9 +25,9 @@ class API_Test_Nansum(unittest.TestCase): @test_with_pir_api def test_static_graph(self): paddle.enable_static() - startup_program = base.Program() - train_program = base.Program() - with base.program_guard(train_program, startup_program): + startup_program = paddle.static.Program() + train_program = paddle.static.Program() + with paddle.static.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 4] )