diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index a072d186bcece9..79ea19f0201c82 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -660,7 +660,7 @@ def shard_index(input, index_num, nshards, shard_id, ignore_value=-1): [[-1] [ 1]] """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.shard_index( input, index_num, nshards, shard_id, ignore_value ) @@ -1021,11 +1021,11 @@ def _fill_diagonal_tensor_impl(x, y, offset=0, dim1=0, dim2=1, inplace=False): if len(y.shape) == 1: y = y.reshape([1, -1]) - if inplace: - return _C_ops.fill_diagonal_tensor_(x, y, offset, dim1, dim2) - - if in_dynamic_mode(): - return _C_ops.fill_diagonal_tensor(x, y, offset, dim1, dim2) + if in_dynamic_or_pir_mode(): + if inplace: + return _C_ops.fill_diagonal_tensor_(x, y, offset, dim1, dim2) + else: + return _C_ops.fill_diagonal_tensor(x, y, offset, dim1, dim2) else: check_variable_and_dtype( x, @@ -1843,7 +1843,7 @@ def roll(x, shifts, axis=None, name=None): else: axis = [] - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.roll(x, shifts, axis) else: check_variable_and_dtype( @@ -4411,7 +4411,7 @@ def strided_slice(x, axes, starts, ends, strides, name=None): >>> sliced_2 = paddle.strided_slice(x, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2) >>> # sliced_2 is x[:, 1:3:1, 0:2:1, 2:4:2]. """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.strided_slice(x, axes, starts, ends, strides) else: helper = LayerHelper('strided_slice', **locals()) diff --git a/test/legacy_test/test_fill_diagonal_tensor_op.py b/test/legacy_test/test_fill_diagonal_tensor_op.py index 5b8f5d69e5f5ff..ad2a0e9bc10ab9 100644 --- a/test/legacy_test/test_fill_diagonal_tensor_op.py +++ b/test/legacy_test/test_fill_diagonal_tensor_op.py @@ -103,10 +103,10 @@ def init_kernel_type(self): self.dtype = np.float64 def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_pir=True) class TensorFillDiagTensor_Test2(TensorFillDiagTensor_Test): @@ -193,11 +193,11 @@ def init_input_output(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) def test_check_grad(self): place = core.CUDAPlace(0) - self.check_grad_with_place(place, ['X'], 'Out') + self.check_grad_with_place(place, ['X'], 'Out', check_pir=True) if __name__ == '__main__': diff --git a/test/legacy_test/test_roll_op.py b/test/legacy_test/test_roll_op.py index dd88ab6e5c4237..5512e248acbb17 100644 --- a/test/legacy_test/test_roll_op.py +++ b/test/legacy_test/test_roll_op.py @@ -19,7 +19,8 @@ import paddle from paddle import base -from paddle.base import Program, core, program_guard +from paddle.base import core +from paddle.pir_utils import test_with_pir_api class TestRollOp(OpTest): @@ -48,10 +49,10 @@ def init_dtype_type(self): self.axis = [0, -2] def test_check_output(self): - self.check_output(check_prim=True) + self.check_output(check_prim=True, check_pir=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) class TestRollOpCase2(TestRollOp): @@ -108,10 +109,14 @@ def init_dtype_type(self): self.place = core.CUDAPlace(0) def test_check_output(self): - self.check_output_with_place(self.place, check_prim=True) + self.check_output_with_place( + self.place, check_prim=True, check_pir=True + ) def test_check_grad_normal(self): - self.check_grad_with_place(self.place, ['X'], 'Out', check_prim=True) + self.check_grad_with_place( + self.place, ['X'], 'Out', check_prim=True, check_pir=True + ) @unittest.skipIf( @@ -128,10 +133,14 @@ def init_dtype_type(self): self.place = core.CUDAPlace(0) def test_check_output(self): - self.check_output_with_place(self.place, check_prim=True) + self.check_output_with_place( + self.place, check_prim=True, check_pir=True + ) def test_check_grad_normal(self): - self.check_grad_with_place(self.place, ['X'], 'Out', check_prim=True) + self.check_grad_with_place( + self.place, ['X'], 'Out', check_prim=True, check_pir=True + ) @unittest.skipIf( @@ -148,10 +157,14 @@ def init_dtype_type(self): self.place = core.CUDAPlace(0) def test_check_output(self): - self.check_output_with_place(self.place, check_prim=True) + self.check_output_with_place( + self.place, check_prim=True, check_pir=True + ) def test_check_grad_normal(self): - self.check_grad_with_place(self.place, ['X'], 'Out', check_prim=True) + self.check_grad_with_place( + self.place, ['X'], 'Out', check_prim=True, check_pir=True + ) class TestRollAPI(unittest.TestCase): @@ -160,37 +173,53 @@ def input_data(self): [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] ) - def test_roll_op_api(self): - self.input_data() - + @test_with_pir_api + def test_roll_op_api_case1(self): paddle.enable_static() - # case 1: - with program_guard(Program(), Program()): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') - x.desc.set_need_check_feed(False) + data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ).astype('float32') z = paddle.roll(x, shifts=1) - exe = base.Executor(base.CPUPlace()) + exe = paddle.static.Executor(paddle.CPUPlace()) (res,) = exe.run( - feed={'x': self.data_x}, fetch_list=[z.name], return_numpy=False + paddle.static.default_main_program(), + feed={'x': data_x}, + fetch_list=[z], + return_numpy=False, ) expect_out = np.array( [[9.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]] ) - np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + paddle.disable_static() - # case 2: - with program_guard(Program(), Program()): + @test_with_pir_api + def test_roll_op_api_case2(self): + paddle.enable_static() + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') - x.desc.set_need_check_feed(False) + data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ).astype('float32') z = paddle.roll(x, shifts=1, axis=0) - exe = base.Executor(base.CPUPlace()) + exe = paddle.static.Executor(paddle.CPUPlace()) (res,) = exe.run( - feed={'x': self.data_x}, fetch_list=[z.name], return_numpy=False + paddle.static.default_main_program(), + feed={'x': data_x}, + fetch_list=[z], + return_numpy=False, + ) + expect_out = np.array( + [[7.0, 8.0, 9.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] ) - expect_out = np.array( - [[7.0, 8.0, 9.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] - ) np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + paddle.disable_static() def test_dygraph_api(self): self.input_data() @@ -214,22 +243,27 @@ def test_dygraph_api(self): ) np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) + @test_with_pir_api def test_roll_op_false(self): - self.input_data() - def test_axis_out_range(): - with program_guard(Program(), Program()): + paddle.enable_static() + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') - x.desc.set_need_check_feed(False) + data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ).astype('float32') z = paddle.roll(x, shifts=1, axis=10) exe = base.Executor(base.CPUPlace()) (res,) = exe.run( - feed={'x': self.data_x}, - fetch_list=[z.name], + feed={'x': data_x}, + fetch_list=[z], return_numpy=False, ) self.assertRaises(ValueError, test_axis_out_range) + paddle.disable_static() def test_shifts_as_tensor_dygraph(self): with base.dygraph.guard(): @@ -241,8 +275,12 @@ def test_shifts_as_tensor_dygraph(self): expected_out = np.array([[8, 6, 7], [2, 0, 1], [5, 3, 4]]) np.testing.assert_allclose(out, expected_out, rtol=1e-05) + @test_with_pir_api def test_shifts_as_tensor_static(self): - with program_guard(Program(), Program()): + paddle.enable_static() + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): x = paddle.arange(9).reshape([3, 3]).astype('float32') shape = paddle.shape(x) shifts = shape // 2 @@ -250,7 +288,7 @@ def test_shifts_as_tensor_static(self): out = paddle.roll(x, shifts=shifts, axis=axes) expected_out = np.array([[8, 6, 7], [2, 0, 1], [5, 3, 4]]) - exe = base.Executor(base.CPUPlace()) + exe = paddle.static.Executor(paddle.CPUPlace()) [out_np] = exe.run(fetch_list=[out]) np.testing.assert_allclose(out_np, expected_out, rtol=1e-05) @@ -258,6 +296,7 @@ def test_shifts_as_tensor_static(self): exe = base.Executor(base.CPUPlace()) [out_np] = exe.run(fetch_list=[out]) np.testing.assert_allclose(out_np, expected_out, rtol=1e-05) + paddle.disable_static() if __name__ == "__main__": diff --git a/test/legacy_test/test_strided_slice_op.py b/test/legacy_test/test_strided_slice_op.py index c23eb18bc56470..91bb626253e7c6 100644 --- a/test/legacy_test/test_strided_slice_op.py +++ b/test/legacy_test/test_strided_slice_op.py @@ -19,6 +19,7 @@ import paddle from paddle import base +from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -96,10 +97,10 @@ def setUp(self): } def test_check_output(self): - self.check_output(check_cinn=True) + self.check_output(check_cinn=True, check_pir=True) def test_check_grad(self): - self.check_grad({'Input'}, 'Out', check_cinn=True) + self.check_grad({'Input'}, 'Out', check_cinn=True, check_pir=True) def initTestCase(self): self.input = np.random.rand(100) @@ -351,10 +352,12 @@ def config(self): self.starts_infer = [1, 10, 2] def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad_normal(self): - self.check_grad(['Input'], 'Out', max_relative_error=0.006) + self.check_grad( + ['Input'], 'Out', max_relative_error=0.006, check_pir=True + ) class TestStridedSliceOp_ends_ListTensor(OpTest): @@ -393,10 +396,12 @@ def config(self): self.ends_infer = [3, 1, 4] def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad_normal(self): - self.check_grad(['Input'], 'Out', max_relative_error=0.006) + self.check_grad( + ['Input'], 'Out', max_relative_error=0.006, check_pir=True + ) class TestStridedSliceOp_starts_Tensor(OpTest): @@ -429,10 +434,12 @@ def config(self): ) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad_normal(self): - self.check_grad(['Input'], 'Out', max_relative_error=0.006) + self.check_grad( + ['Input'], 'Out', max_relative_error=0.006, check_pir=True + ) class TestStridedSliceOp_ends_Tensor(OpTest): @@ -465,10 +472,12 @@ def config(self): ) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad_normal(self): - self.check_grad(['Input'], 'Out', max_relative_error=0.006) + self.check_grad( + ['Input'], 'Out', max_relative_error=0.006, check_pir=True + ) class TestStridedSliceOp_listTensor_Tensor(OpTest): @@ -508,10 +517,12 @@ def config(self): ) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad_normal(self): - self.check_grad(['Input'], 'Out', max_relative_error=0.006) + self.check_grad( + ['Input'], 'Out', max_relative_error=0.006, check_pir=True + ) class TestStridedSliceOp_strides_Tensor(OpTest): @@ -544,74 +555,86 @@ def config(self): ) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad_normal(self): - self.check_grad(['Input'], 'Out', max_relative_error=0.006) + self.check_grad( + ['Input'], 'Out', max_relative_error=0.006, check_pir=True + ) # Test python API class TestStridedSliceAPI(unittest.TestCase): - def test_1(self): + @test_with_pir_api + def test_static_api(self): + paddle.enable_static() + place = base.CPUPlace() input = np.random.random([3, 4, 5, 6]).astype("float64") - minus_1 = paddle.tensor.fill_constant([], "int32", -1) - minus_3 = paddle.tensor.fill_constant([], "int32", -3) - starts = paddle.static.data(name='starts', shape=[3], dtype='int32') - ends = paddle.static.data(name='ends', shape=[3], dtype='int32') - strides = paddle.static.data(name='strides', shape=[3], dtype='int32') - - x = paddle.static.data( - name="x", - shape=[3, 4, 5, 6], - dtype="float64", - ) - out_1 = paddle.strided_slice( - x, - axes=[0, 1, 2], - starts=[-3, 0, 2], - ends=[3, 100, -1], - strides=[1, 1, 1], - ) - out_2 = paddle.strided_slice( - x, - axes=[0, 1, 3], - starts=[minus_3, 0, 2], - ends=[3, 100, -1], - strides=[1, 1, 1], - ) - out_3 = paddle.strided_slice( - x, - axes=[0, 1, 3], - starts=[minus_3, 0, 2], - ends=[3, 100, minus_1], - strides=[1, 1, 1], - ) - out_4 = paddle.strided_slice( - x, axes=[0, 1, 2], starts=starts, ends=ends, strides=strides - ) + with paddle.static.program_guard(paddle.static.Program()): + minus_1 = paddle.tensor.fill_constant([], "int32", -1) + minus_3 = paddle.tensor.fill_constant([], "int32", -3) + starts = paddle.static.data(name='starts', shape=[3], dtype='int32') + ends = paddle.static.data(name='ends', shape=[3], dtype='int32') + strides = paddle.static.data( + name='strides', shape=[3], dtype='int32' + ) - out_5 = x[-3:3, 0:100:2, -1:2:-1] - out_6 = x[minus_3:3:1, 0:100:2, :, minus_1:2:minus_1] - out_7 = x[minus_1, 0:100:2, :, -1:2:-1] - - exe = base.Executor(place=base.CPUPlace()) - res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( - base.default_main_program(), - feed={ - "x": input, - 'starts': np.array([-3, 0, 2]).astype("int32"), - 'ends': np.array([3, 2147483647, -1]).astype("int32"), - 'strides': np.array([1, 1, 1]).astype("int32"), - }, - fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], - ) - np.testing.assert_array_equal(res_1, input[-3:3, 0:100, 2:-1, :]) - np.testing.assert_array_equal(res_2, input[-3:3, 0:100, :, 2:-1]) - np.testing.assert_array_equal(res_3, input[-3:3, 0:100, :, 2:-1]) - np.testing.assert_array_equal(res_4, input[-3:3, 0:100, 2:-1, :]) - np.testing.assert_array_equal(res_5, input[-3:3, 0:100:2, -1:2:-1, :]) - np.testing.assert_array_equal(res_6, input[-3:3, 0:100:2, :, -1:2:-1]) - np.testing.assert_array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1]) + x = paddle.static.data( + name="x", + shape=[3, 4, 5, 6], + dtype="float64", + ) + out_1 = paddle.strided_slice( + x, + axes=[0, 1, 2], + starts=[-3, 0, 2], + ends=[3, 100, -1], + strides=[1, 1, 1], + ) + out_2 = paddle.strided_slice( + x, + axes=[0, 1, 3], + starts=[minus_3, 0, 2], + ends=[3, 100, -1], + strides=[1, 1, 1], + ) + out_3 = paddle.strided_slice( + x, + axes=[0, 1, 3], + starts=[minus_3, 0, 2], + ends=[3, 100, minus_1], + strides=[1, 1, 1], + ) + out_4 = paddle.strided_slice( + x, axes=[0, 1, 2], starts=starts, ends=ends, strides=strides + ) + + out_5 = x[-3:3, 0:100:2, -1:2:-1] + out_6 = x[minus_3:3:1, 0:100:2, :, minus_1:2:minus_1] + out_7 = x[minus_1, 0:100:2, :, -1:2:-1] + + exe = paddle.static.Executor(place) + res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( + paddle.static.default_main_program(), + feed={ + "x": input, + 'starts': np.array([-3, 0, 2]).astype("int32"), + 'ends': np.array([3, 2147483647, -1]).astype("int32"), + 'strides': np.array([1, 1, 1]).astype("int32"), + }, + fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], + ) + np.testing.assert_array_equal(res_1, input[-3:3, 0:100, 2:-1, :]) + np.testing.assert_array_equal(res_2, input[-3:3, 0:100, :, 2:-1]) + np.testing.assert_array_equal(res_3, input[-3:3, 0:100, :, 2:-1]) + np.testing.assert_array_equal(res_4, input[-3:3, 0:100, 2:-1, :]) + np.testing.assert_array_equal( + res_5, input[-3:3, 0:100:2, -1:2:-1, :] + ) + np.testing.assert_array_equal( + res_6, input[-3:3, 0:100:2, :, -1:2:-1] + ) + np.testing.assert_array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1]) def test_dygraph_op(self): x = paddle.zeros(shape=[3, 4, 5, 6], dtype="float32") @@ -1031,10 +1054,10 @@ def setUp(self): } def test_check_output(self): - self.check_output(check_cinn=True) + self.check_output(check_cinn=True, check_pir=True) def test_check_grad(self): - self.check_grad({'Input'}, 'Out', check_cinn=True) + self.check_grad({'Input'}, 'Out', check_cinn=True, check_pir=True) def initTestCase(self): self.input = np.random.rand(100) @@ -1068,10 +1091,10 @@ def setUp(self): } def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad({'Input'}, 'Out') + self.check_grad({'Input'}, 'Out', check_pir=True) def initTestCase(self): self.input = np.random.rand(100)