diff --git a/python/paddle/tensor/attribute.py b/python/paddle/tensor/attribute.py index 9bd7f3c16c95dc..25a7819b5b2432 100644 --- a/python/paddle/tensor/attribute.py +++ b/python/paddle/tensor/attribute.py @@ -20,7 +20,7 @@ from paddle import _C_ops from ..base.data_feeder import check_type, check_variable_and_dtype -from ..base.framework import in_dynamic_or_pir_mode, in_pir_mode +from ..base.framework import in_dynamic_or_pir_mode, use_pir_api from ..common_ops_import import Variable from ..framework import LayerHelper, core from .creation import _complex_to_real_dtype, assign @@ -250,7 +250,7 @@ def is_integer(x): dtype = x.dtype is_int_dtype = False - if not in_pir_mode(): + if not use_pir_api(): is_int_dtype = ( dtype == core.VarDesc.VarType.UINT8 or dtype == core.VarDesc.VarType.INT8 @@ -260,7 +260,7 @@ def is_integer(x): ) else: is_int_dtype = ( - dtype == core.DataType.INT8 + dtype == core.DataType.UINT8 or dtype == core.DataType.INT8 or dtype == core.DataType.INT16 or dtype == core.DataType.INT32 diff --git a/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_deprecated.py b/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_deprecated.py new file mode 100644 index 00000000000000..cac15ad77b7b40 --- /dev/null +++ b/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_deprecated.py @@ -0,0 +1,158 @@ +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Note: +# 0D Tensor indicates that the tensor's dimension is 0 +# 0D Tensor's shape is always [], numel is 1 +# which can be created by paddle.rand([]) + +import unittest + +import numpy as np +from decorator_helper import prog_scope + +import paddle + +# Use to test zero-dim of Sundry API, which is unique and can not be classified +# with others. It can be implemented here flexibly. + + +class TestSundryAPIStatic(unittest.TestCase): + def setUp(self): + paddle.enable_static() + self.exe = paddle.static.Executor() + + def assertShapeEqual(self, out, target_tuple): + if not paddle.framework.in_pir_mode(): + out_shape = list(out.shape) + else: + out_shape = out.shape + self.assertEqual(out_shape, target_tuple) + + @prog_scope() + def test_create_global_var(self): + zero_dim_var = paddle.static.create_global_var( + shape=[], value=0.5, dtype='float32' + ) + self.assertEqual(zero_dim_var.shape, ()) + prog = paddle.static.default_startup_program() + res = self.exe.run(prog, fetch_list=[zero_dim_var]) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[0], 0.5) + + @prog_scope() + def test_setitem(self): + # NOTE(zoooo0820): __setitem__ has gradient problem in static graph. + # To solve this, we may not support __setitem__ in static graph. + # These unit tests will delete soon. + + # case1: all axis have a scalar indice + x = paddle.arange(2 * 3 * 4 * 5).reshape((2, 3, 4, 5)) + x.stop_gradient = False + out = x * 2 + out = paddle.static.setitem(out, (1, 2, 3, 4), 10) + paddle.static.append_backward(out.sum()) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + + self.assertEqual(out.shape, x.shape) + np.testing.assert_allclose(res[0][1, 2, 3, 4], np.array(10)) + self.assertEqual(res[1].shape, (2, 3, 4, 5)) + x_grad_expected = np.ones((2, 3, 4, 5)) * 2 + x_grad_expected[1, 2, 3, 4] = 0 + np.testing.assert_allclose(res[1], x_grad_expected) + + # case2: 0-D Tensor indice in some axis + # NOTE(zoooo0820): Now, int/slice with 0-D Tensor will still be + # treated as combined indexing, which is not support backward. + # There should have more test cases such as out[1, indice, :] = 0.5 when this + # problem is fixed. + x = paddle.randn((2, 3, 4, 5)) + x.stop_gradient = False + indice = paddle.full([], 1, dtype='int32') + out = x * 1 + out = paddle.static.setitem(out, (indice, indice), 0.5) + paddle.static.append_backward(out.sum()) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out, x.grad_name]) + + self.assertEqual(out.shape, x.shape) + np.testing.assert_allclose(res[0][1, 1], np.ones((4, 5)) * 0.5) + x_grad_expected = np.ones((2, 3, 4, 5)) + x_grad_expected[1, 1] = 0 + np.testing.assert_allclose(res[1], x_grad_expected) + + # case3:0-D Tensor indice in some axis, value is a Tensor + # and there is broadcast + x = paddle.randn((2, 3, 4, 5)) + x.stop_gradient = False + v = paddle.ones((4, 5), dtype='float32') * 5 + v.stop_gradient = False + indice = paddle.full([], 1, dtype='int32') + out = x * 1 + out = paddle.static.setitem(out, indice, v) + paddle.static.append_backward(out.sum()) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out, x.grad_name, v.grad_name]) + + self.assertEqual(out.shape, x.shape) + np.testing.assert_allclose(res[0][1], np.ones((3, 4, 5)) * 5) + x_grad_expected = np.ones((2, 3, 4, 5)) + x_grad_expected[1] = 0 + np.testing.assert_allclose(res[1], x_grad_expected) + + @prog_scope() + def test_static_auc(self): + x = paddle.full(shape=[3, 2], fill_value=0.25) + y = paddle.full(shape=[3], fill_value=1, dtype="int64") + out = paddle.static.auc(input=x, label=y)[0] + + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + fetch_list=[out], + ) + + self.assertEqual(res[0].shape, ()) + + @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') + 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()) + res = self.exe.run( + prog, + fetch_list=[ + out1, + x1_grad, + out1_grad, + ], + ) + + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) + self.assertEqual(res[2].shape, ()) + np.testing.assert_allclose(res[0], np.array(1)) + np.testing.assert_allclose(res[1], np.array(1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_zero_dim_sundry_dygraph_api.py b/test/legacy_test/test_zero_dim_sundry_dygraph_api.py similarity index 99% rename from test/deprecated/legacy_test/test_zero_dim_sundry_dygraph_api.py rename to test/legacy_test/test_zero_dim_sundry_dygraph_api.py index 00f32fe8744133..ac22d5ae52ff41 100644 --- a/test/deprecated/legacy_test/test_zero_dim_sundry_dygraph_api.py +++ b/test/legacy_test/test_zero_dim_sundry_dygraph_api.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_part1.py b/test/legacy_test/test_zero_dim_sundry_static_api_part1.py similarity index 88% rename from test/deprecated/legacy_test/test_zero_dim_sundry_static_api_part1.py rename to test/legacy_test/test_zero_dim_sundry_static_api_part1.py index 22386fc5022ed5..aafc4afc32c680 100644 --- a/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_part1.py +++ b/test/legacy_test/test_zero_dim_sundry_static_api_part1.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -146,17 +146,6 @@ def test_create_parameter(self): ) self.assertEqual(zero_dim_param_res.shape, ()) - @prog_scope() - def test_create_global_var(self): - zero_dim_var = paddle.static.create_global_var( - shape=[], value=0.5, dtype='float32' - ) - self.assertEqual(zero_dim_var.shape, ()) - prog = paddle.static.default_startup_program() - res = self.exe.run(prog, fetch_list=[zero_dim_var]) - self.assertEqual(res[0].shape, ()) - self.assertEqual(res[0], 0.5) - @test_with_pir_api @prog_scope() def test_getitem(self): @@ -212,67 +201,6 @@ def test_getitem(self): self.assertEqual(res[1].shape, (1, 4)) np.testing.assert_allclose(res[1], np.ones((1, 4))) - @prog_scope() - def test_setitem(self): - # NOTE(zoooo0820): __setitem__ has gradient problem in static graph. - # To solve this, we may not support __setitem__ in static graph. - # These unit tests will delete soon. - - # case1: all axis have a scalar indice - x = paddle.arange(2 * 3 * 4 * 5).reshape((2, 3, 4, 5)) - x.stop_gradient = False - out = x * 2 - out = paddle.static.setitem(out, (1, 2, 3, 4), 10) - paddle.static.append_backward(out.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) - - self.assertEqual(out.shape, x.shape) - np.testing.assert_allclose(res[0][1, 2, 3, 4], np.array(10)) - self.assertEqual(res[1].shape, (2, 3, 4, 5)) - x_grad_expected = np.ones((2, 3, 4, 5)) * 2 - x_grad_expected[1, 2, 3, 4] = 0 - np.testing.assert_allclose(res[1], x_grad_expected) - - # case2: 0-D Tensor indice in some axis - # NOTE(zoooo0820): Now, int/slice with 0-D Tensor will still be - # treated as combined indexing, which is not support backward. - # There should have more test cases such as out[1, indice, :] = 0.5 when this - # problem is fixed. - x = paddle.randn((2, 3, 4, 5)) - x.stop_gradient = False - indice = paddle.full([], 1, dtype='int32') - out = x * 1 - out = paddle.static.setitem(out, (indice, indice), 0.5) - paddle.static.append_backward(out.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name]) - - self.assertEqual(out.shape, x.shape) - np.testing.assert_allclose(res[0][1, 1], np.ones((4, 5)) * 0.5) - x_grad_expected = np.ones((2, 3, 4, 5)) - x_grad_expected[1, 1] = 0 - np.testing.assert_allclose(res[1], x_grad_expected) - - # case3:0-D Tensor indice in some axis, value is a Tensor - # and there is broadcast - x = paddle.randn((2, 3, 4, 5)) - x.stop_gradient = False - v = paddle.ones((4, 5), dtype='float32') * 5 - v.stop_gradient = False - indice = paddle.full([], 1, dtype='int32') - out = x * 1 - out = paddle.static.setitem(out, indice, v) - paddle.static.append_backward(out.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out, x.grad_name, v.grad_name]) - - self.assertEqual(out.shape, x.shape) - np.testing.assert_allclose(res[0][1], np.ones((3, 4, 5)) * 5) - x_grad_expected = np.ones((2, 3, 4, 5)) - x_grad_expected[1] = 0 - np.testing.assert_allclose(res[1], x_grad_expected) - @test_with_pir_api @prog_scope() def test_expand(self): @@ -650,9 +578,7 @@ def test_as_complex(self): out = paddle.as_complex(x) self.assertShapeEqual( x, - [ - 2, - ], + [2], ) self.assertShapeEqual(out, []) grad_list = paddle.static.append_backward( @@ -858,20 +784,6 @@ def test_static_accuracy(self): self.assertEqual(res[0].shape, ()) - @prog_scope() - def test_static_auc(self): - x = paddle.full(shape=[3, 2], fill_value=0.25) - y = paddle.full(shape=[3], fill_value=1, dtype="int64") - out = paddle.static.auc(input=x, label=y)[0] - - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, - fetch_list=[out], - ) - - self.assertEqual(res[0].shape, ()) - @test_with_pir_api @prog_scope() def test_std(self): diff --git a/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_part3.py b/test/legacy_test/test_zero_dim_sundry_static_api_part3.py similarity index 94% rename from test/deprecated/legacy_test/test_zero_dim_sundry_static_api_part3.py rename to test/legacy_test/test_zero_dim_sundry_static_api_part3.py index 7ae165540b887d..a2370f228f2abc 100644 --- a/test/deprecated/legacy_test/test_zero_dim_sundry_static_api_part3.py +++ b/test/legacy_test/test_zero_dim_sundry_static_api_part3.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -428,33 +428,6 @@ def test_prelu(self): self.assertEqual(res[4].shape, ()) self.assertEqual(res[5].shape, ()) - @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') - 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()) - res = self.exe.run( - prog, - fetch_list=[ - out1, - x1_grad, - out1_grad, - ], - ) - - self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) - self.assertEqual(res[2].shape, ()) - np.testing.assert_allclose(res[0], np.array(1)) - np.testing.assert_allclose(res[1], np.array(1)) - @test_with_pir_api @prog_scope() def test_while_loop(self):