From 15399e4e06b53888536d0d5f3e3df88dd4d3971f Mon Sep 17 00:00:00 2001 From: Li Dinghao Date: Thu, 14 Nov 2024 22:47:15 +0800 Subject: [PATCH 01/22] Add English explanation --- python/paddle/tensor/manipulation.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index b2b29874cb5d65..0e5e909cc6bdb7 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -531,6 +531,13 @@ def transpose( The `i`-th dimension of the returned tensor will correspond to the perm[i]-th dimension of `input`. + The image illustrates the second example of the transpose operation. + + .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/transpose.png + :width: 500 + :alt: legend of concat API + :align: center + Args: x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32. perm (list|tuple): Permute the input according to the data of perm. From 18349c5d788ddeaea8c463238ae45c5c6a02855e Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 07:19:13 +0000 Subject: [PATCH 02/22] modify code --- python/paddle/tensor/manipulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 0e5e909cc6bdb7..e0bb5de2f6cc41 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -532,7 +532,7 @@ def transpose( perm[i]-th dimension of `input`. The image illustrates the second example of the transpose operation. - + .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/transpose.png :width: 500 :alt: legend of concat API From 246158438483b2dc9e038c9481cdaad98918b885 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 10:54:49 +0000 Subject: [PATCH 03/22] add __rand__ --- python/paddle/tensor/__init__.py | 1 + python/paddle/tensor/tensor.prototype.pyi | 1 + test/legacy_test/test_math_op_patch_pir.py | 27 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 041d5de007b203..4aca01b2a8b967 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -862,6 +862,7 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), + ('__rand__', 'bitwise_and'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index cfb878c8249f9d..b2c33be649bc9b 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -181,6 +181,7 @@ class AbstractTensor: def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore + def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore # type cast def __bool__(self) -> bool: ... diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index 59986965e2a76d..a7e1e3108c1363 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -278,6 +278,33 @@ def test_bitwise_and(self): np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) +def test_bitwise_rand(self): + paddle.disable_static() + x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") + y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") + res_np_b = y_np & x_np + res_np_c = paddle.bitwise_and( + paddle.to_tensor(y_np), paddle.to_tensor(x_np) + ) + res_np_d = y_np.__rand__(x_np) + paddle.enable_static() + with paddle.pir_utils.IrGuard(): + main_program, exe, program_guard = new_program() + with program_guard: + x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") + y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") + b = y & x + c = y.bitwise_and(x) + d = y.__rand__(x) + (b_np, c_np, d_np) = exe.run( + main_program, + feed={"x": x_np, "y": y_np}, + fetch_list=[b, c, d], + ) + np.testing.assert_array_equal(res_np_b, b_np) + np.testing.assert_array_equal(res_np_c, c_np) + np.testing.assert_array_equal(res_np_d, d_np) + def test_positive(self): paddle.disable_static() x_np = np.random.random([10, 1024]).astype('float32') From 72fc12d9079aeccf0f7364254cb944c9e6e35658 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:01:56 +0000 Subject: [PATCH 04/22] cancel update --- python/paddle/tensor/__init__.py | 1 - python/paddle/tensor/tensor.prototype.pyi | 1 - test/legacy_test/test_math_op_patch_pir.py | 27 ---------------------- 3 files changed, 29 deletions(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 4aca01b2a8b967..041d5de007b203 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -862,7 +862,6 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), - ('__rand__', 'bitwise_and'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index b2c33be649bc9b..cfb878c8249f9d 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -181,7 +181,6 @@ class AbstractTensor: def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore - def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore # type cast def __bool__(self) -> bool: ... diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index a7e1e3108c1363..59986965e2a76d 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -278,33 +278,6 @@ def test_bitwise_and(self): np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) -def test_bitwise_rand(self): - paddle.disable_static() - x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - res_np_b = y_np & x_np - res_np_c = paddle.bitwise_and( - paddle.to_tensor(y_np), paddle.to_tensor(x_np) - ) - res_np_d = y_np.__rand__(x_np) - paddle.enable_static() - with paddle.pir_utils.IrGuard(): - main_program, exe, program_guard = new_program() - with program_guard: - x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") - y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") - b = y & x - c = y.bitwise_and(x) - d = y.__rand__(x) - (b_np, c_np, d_np) = exe.run( - main_program, - feed={"x": x_np, "y": y_np}, - fetch_list=[b, c, d], - ) - np.testing.assert_array_equal(res_np_b, b_np) - np.testing.assert_array_equal(res_np_c, c_np) - np.testing.assert_array_equal(res_np_d, d_np) - def test_positive(self): paddle.disable_static() x_np = np.random.random([10, 1024]).astype('float32') From 6d4addccb4c3a79b4fc4140ffa1fee8130186dc8 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:13:14 +0000 Subject: [PATCH 05/22] add __rand__ --- python/paddle/tensor/manipulation.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index e0bb5de2f6cc41..e759b3ced5ca25 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -533,11 +533,6 @@ def transpose( The image illustrates the second example of the transpose operation. - .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/transpose.png - :width: 500 - :alt: legend of concat API - :align: center - Args: x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32. perm (list|tuple): Permute the input according to the data of perm. From eeb07ec30e0514d9093aa3e4b26fb61a514aa00c Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:18:21 +0000 Subject: [PATCH 06/22] delete superfluous code --- python/paddle/tensor/manipulation.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index e759b3ced5ca25..b2b29874cb5d65 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -531,8 +531,6 @@ def transpose( The `i`-th dimension of the returned tensor will correspond to the perm[i]-th dimension of `input`. - The image illustrates the second example of the transpose operation. - Args: x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32. perm (list|tuple): Permute the input according to the data of perm. From 48e14ac57f0d040ae4e686234e98e89f027bce65 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:26:46 +0000 Subject: [PATCH 07/22] recover code --- python/paddle/tensor/__init__.py | 1 - python/paddle/tensor/tensor.prototype.pyi | 1 - test/legacy_test/test_math_op_patch_pir.py | 27 ---------------------- 3 files changed, 29 deletions(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 4aca01b2a8b967..041d5de007b203 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -862,7 +862,6 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), - ('__rand__', 'bitwise_and'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index b2c33be649bc9b..cfb878c8249f9d 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -181,7 +181,6 @@ class AbstractTensor: def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore - def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore # type cast def __bool__(self) -> bool: ... diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index a7e1e3108c1363..59986965e2a76d 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -278,33 +278,6 @@ def test_bitwise_and(self): np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) -def test_bitwise_rand(self): - paddle.disable_static() - x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - res_np_b = y_np & x_np - res_np_c = paddle.bitwise_and( - paddle.to_tensor(y_np), paddle.to_tensor(x_np) - ) - res_np_d = y_np.__rand__(x_np) - paddle.enable_static() - with paddle.pir_utils.IrGuard(): - main_program, exe, program_guard = new_program() - with program_guard: - x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") - y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") - b = y & x - c = y.bitwise_and(x) - d = y.__rand__(x) - (b_np, c_np, d_np) = exe.run( - main_program, - feed={"x": x_np, "y": y_np}, - fetch_list=[b, c, d], - ) - np.testing.assert_array_equal(res_np_b, b_np) - np.testing.assert_array_equal(res_np_c, c_np) - np.testing.assert_array_equal(res_np_d, d_np) - def test_positive(self): paddle.disable_static() x_np = np.random.random([10, 1024]).astype('float32') From 82c622cb9e03385eb0a24edd163a8a5d833c61f3 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:35:50 +0000 Subject: [PATCH 08/22] develop --- python/paddle/tensor/__init__.py | 1 - python/paddle/tensor/tensor.prototype.pyi | 1 - test/legacy_test/test_math_op_patch_pir.py | 27 ---------------------- 3 files changed, 29 deletions(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 4aca01b2a8b967..041d5de007b203 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -862,7 +862,6 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), - ('__rand__', 'bitwise_and'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index b2c33be649bc9b..cfb878c8249f9d 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -181,7 +181,6 @@ class AbstractTensor: def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore - def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore # type cast def __bool__(self) -> bool: ... diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index a7e1e3108c1363..59986965e2a76d 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -278,33 +278,6 @@ def test_bitwise_and(self): np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) -def test_bitwise_rand(self): - paddle.disable_static() - x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - res_np_b = y_np & x_np - res_np_c = paddle.bitwise_and( - paddle.to_tensor(y_np), paddle.to_tensor(x_np) - ) - res_np_d = y_np.__rand__(x_np) - paddle.enable_static() - with paddle.pir_utils.IrGuard(): - main_program, exe, program_guard = new_program() - with program_guard: - x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") - y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") - b = y & x - c = y.bitwise_and(x) - d = y.__rand__(x) - (b_np, c_np, d_np) = exe.run( - main_program, - feed={"x": x_np, "y": y_np}, - fetch_list=[b, c, d], - ) - np.testing.assert_array_equal(res_np_b, b_np) - np.testing.assert_array_equal(res_np_c, c_np) - np.testing.assert_array_equal(res_np_d, d_np) - def test_positive(self): paddle.disable_static() x_np = np.random.random([10, 1024]).astype('float32') From 73bb823e097a55464f74d4158178dae9f738af5d Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:40:51 +0000 Subject: [PATCH 09/22] recover --- python/paddle/tensor/manipulation.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index e0bb5de2f6cc41..b2b29874cb5d65 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -531,13 +531,6 @@ def transpose( The `i`-th dimension of the returned tensor will correspond to the perm[i]-th dimension of `input`. - The image illustrates the second example of the transpose operation. - - .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/transpose.png - :width: 500 - :alt: legend of concat API - :align: center - Args: x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32. perm (list|tuple): Permute the input according to the data of perm. From 376e45260bddb83599cbe7c45ece01d9ef22f0a5 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 11:50:54 +0000 Subject: [PATCH 10/22] add __rand__ --- python/paddle/tensor/__init__.py | 1 + python/paddle/tensor/tensor.prototype.pyi | 1 + test/legacy_test/test_math_op_patch_pir.py | 27 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 041d5de007b203..4aca01b2a8b967 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -862,6 +862,7 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), + ('__rand__', 'bitwise_and'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index cfb878c8249f9d..b2c33be649bc9b 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -181,6 +181,7 @@ class AbstractTensor: def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore + def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore # type cast def __bool__(self) -> bool: ... diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index 59986965e2a76d..a7e1e3108c1363 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -278,6 +278,33 @@ def test_bitwise_and(self): np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) +def test_bitwise_rand(self): + paddle.disable_static() + x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") + y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") + res_np_b = y_np & x_np + res_np_c = paddle.bitwise_and( + paddle.to_tensor(y_np), paddle.to_tensor(x_np) + ) + res_np_d = y_np.__rand__(x_np) + paddle.enable_static() + with paddle.pir_utils.IrGuard(): + main_program, exe, program_guard = new_program() + with program_guard: + x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") + y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") + b = y & x + c = y.bitwise_and(x) + d = y.__rand__(x) + (b_np, c_np, d_np) = exe.run( + main_program, + feed={"x": x_np, "y": y_np}, + fetch_list=[b, c, d], + ) + np.testing.assert_array_equal(res_np_b, b_np) + np.testing.assert_array_equal(res_np_c, c_np) + np.testing.assert_array_equal(res_np_d, d_np) + def test_positive(self): paddle.disable_static() x_np = np.random.random([10, 1024]).astype('float32') From 214b64c62db683cfde29daddc881723ebd4964ff Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 18 Nov 2024 12:17:44 +0000 Subject: [PATCH 11/22] finish --- python/paddle/tensor/__init__.py | 1 + python/paddle/tensor/tensor.prototype.pyi | 1 + test/legacy_test/test_math_op_patch_pir.py | 27 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 041d5de007b203..4aca01b2a8b967 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -862,6 +862,7 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), + ('__rand__', 'bitwise_and'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index cfb878c8249f9d..b2c33be649bc9b 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -181,6 +181,7 @@ class AbstractTensor: def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore + def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore # type cast def __bool__(self) -> bool: ... diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index 59986965e2a76d..d3c483d1f184da 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -278,6 +278,33 @@ def test_bitwise_and(self): np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) + def test_bitwise_rand(self): + paddle.disable_static() + x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") + y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") + res_np_b = y_np & x_np + res_np_c = paddle.bitwise_and( + paddle.to_tensor(y_np), paddle.to_tensor(x_np) + ) + res_np_d = y_np.__rand__(x_np) + paddle.enable_static() + with paddle.pir_utils.IrGuard(): + main_program, exe, program_guard = new_program() + with program_guard: + x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") + y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") + b = y & x + c = y.bitwise_and(x) + d = y.__rand__(x) + (b_np, c_np, d_np) = exe.run( + main_program, + feed={"x": x_np, "y": y_np}, + fetch_list=[b, c, d], + ) + np.testing.assert_array_equal(res_np_b, b_np) + np.testing.assert_array_equal(res_np_c, c_np) + np.testing.assert_array_equal(res_np_d, d_np) + def test_positive(self): paddle.disable_static() x_np = np.random.random([10, 1024]).astype('float32') From b1a12a26e9faae439f8b9aadd033cf993ffff8a8 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Tue, 19 Nov 2024 07:46:20 +0000 Subject: [PATCH 12/22] modify v1 --- python/paddle/tensor/__init__.py | 3 +- python/paddle/tensor/logic.py | 5 ++++ test/legacy_test/test_math_op_patch_pir.py | 34 ++++------------------ 3 files changed, 12 insertions(+), 30 deletions(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 4aca01b2a8b967..f8769f58b49c00 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -105,6 +105,7 @@ ) from .logic import ( # noqa: F401 allclose, + __rand__, bitwise_and, bitwise_and_, bitwise_invert, @@ -862,7 +863,7 @@ # this list used in math_op_patch.py for magic_method bind magic_method_func = [ ('__and__', 'bitwise_and'), - ('__rand__', 'bitwise_and'), + ('__rand__', '__rand__'), ('__or__', 'bitwise_or'), ('__xor__', 'bitwise_xor'), ('__invert__', 'bitwise_not'), diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index a6078929084e8e..174ca58fb5036e 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1230,6 +1230,11 @@ def bitwise_and( op_name="bitwise_and", x=x, y=y, name=name, out=out, binary_op=True ) +def __rand__( + x: Tensor, + y: Tensor, +) -> Tensor: + return bitwise_and(x, y, None, None) @inplace_apis_in_dygraph_only def bitwise_and_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index d3c483d1f184da..31572117f18c4c 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -260,6 +260,7 @@ def test_bitwise_and(self): paddle.to_tensor(x_np), paddle.to_tensor(y_np) ) res_np_d = x_np.__and__(y_np) + res_np_e = x_np.__rand__(y_np) paddle.enable_static() with paddle.pir_utils.IrGuard(): main_program, exe, program_guard = new_program() @@ -269,41 +270,16 @@ def test_bitwise_and(self): b = x & y c = x.bitwise_and(y) d = x.__and__(y) - (b_np, c_np, d_np) = exe.run( - main_program, - feed={"x": x_np, "y": y_np}, - fetch_list=[b, c, d], - ) - np.testing.assert_array_equal(res_np_b, b_np) - np.testing.assert_array_equal(res_np_c, c_np) - np.testing.assert_array_equal(res_np_d, d_np) - - def test_bitwise_rand(self): - paddle.disable_static() - x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32") - res_np_b = y_np & x_np - res_np_c = paddle.bitwise_and( - paddle.to_tensor(y_np), paddle.to_tensor(x_np) - ) - res_np_d = y_np.__rand__(x_np) - paddle.enable_static() - with paddle.pir_utils.IrGuard(): - main_program, exe, program_guard = new_program() - with program_guard: - x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") - y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") - b = y & x - c = y.bitwise_and(x) - d = y.__rand__(x) - (b_np, c_np, d_np) = exe.run( + e = x.__rand__(y) + (b_np, c_np, d_np, e_np) = exe.run( main_program, feed={"x": x_np, "y": y_np}, - fetch_list=[b, c, d], + fetch_list=[b, c, d, e ], ) np.testing.assert_array_equal(res_np_b, b_np) np.testing.assert_array_equal(res_np_c, c_np) np.testing.assert_array_equal(res_np_d, d_np) + np.testing.assert_array_equal(res_np_e, e_np) def test_positive(self): paddle.disable_static() From 3bb3cddd4f4e83946303cc3a8d9f541b3300519e Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Thu, 21 Nov 2024 04:49:36 +0000 Subject: [PATCH 13/22] question --- python/paddle/tensor/logic.py | 12 +++++++----- test/legacy_test/test_math_op_patch_pir.py | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 174ca58fb5036e..617628c1030e85 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1230,11 +1230,13 @@ def bitwise_and( op_name="bitwise_and", x=x, y=y, name=name, out=out, binary_op=True ) -def __rand__( - x: Tensor, - y: Tensor, -) -> Tensor: - return bitwise_and(x, y, None, None) +def __rand__(x: Tensor, y: int | bool): + if not isinstance(y, int | bool): + raise TypeError( + f"unsupported operand type(s) for &: '{type(y).__name__}' and 'Tensor'" + ) + y_tensor = paddle.to_tensor(y, dtype=x.dtype) + return bitwise_and(y_tensor, x, None, None) @inplace_apis_in_dygraph_only def bitwise_and_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index 31572117f18c4c..279ff664f5d7dc 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -260,7 +260,8 @@ def test_bitwise_and(self): paddle.to_tensor(x_np), paddle.to_tensor(y_np) ) res_np_d = x_np.__and__(y_np) - res_np_e = x_np.__rand__(y_np) + temp = 2 + res_np_e = temp & y_np paddle.enable_static() with paddle.pir_utils.IrGuard(): main_program, exe, program_guard = new_program() @@ -270,11 +271,11 @@ def test_bitwise_and(self): b = x & y c = x.bitwise_and(y) d = x.__and__(y) - e = x.__rand__(y) + e = temp.__rand__(y) (b_np, c_np, d_np, e_np) = exe.run( main_program, feed={"x": x_np, "y": y_np}, - fetch_list=[b, c, d, e ], + fetch_list=[b, c, d, e], ) np.testing.assert_array_equal(res_np_b, b_np) np.testing.assert_array_equal(res_np_c, c_np) From c99d9293daa390ef49f83120c472cc033b3f9efd Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Thu, 21 Nov 2024 06:14:19 +0000 Subject: [PATCH 14/22] try --- python/paddle/tensor/logic.py | 4 ---- test/legacy_test/test_math_op_patch_pir.py | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 617628c1030e85..8e93377f8384cc 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1231,10 +1231,6 @@ def bitwise_and( ) def __rand__(x: Tensor, y: int | bool): - if not isinstance(y, int | bool): - raise TypeError( - f"unsupported operand type(s) for &: '{type(y).__name__}' and 'Tensor'" - ) y_tensor = paddle.to_tensor(y, dtype=x.dtype) return bitwise_and(y_tensor, x, None, None) diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index 279ff664f5d7dc..24a4f93089dc97 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -271,7 +271,7 @@ def test_bitwise_and(self): b = x & y c = x.bitwise_and(y) d = x.__and__(y) - e = temp.__rand__(y) + e = temp & y (b_np, c_np, d_np, e_np) = exe.run( main_program, feed={"x": x_np, "y": y_np}, From a456dbe9b9b02b6ce7a1d776f404794710fc1179 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Thu, 21 Nov 2024 06:21:22 +0000 Subject: [PATCH 15/22] precommit --- python/paddle/tensor/logic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 8e93377f8384cc..8997a3a67fce6e 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1230,10 +1230,12 @@ def bitwise_and( op_name="bitwise_and", x=x, y=y, name=name, out=out, binary_op=True ) + def __rand__(x: Tensor, y: int | bool): y_tensor = paddle.to_tensor(y, dtype=x.dtype) return bitwise_and(y_tensor, x, None, None) + @inplace_apis_in_dygraph_only def bitwise_and_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: r""" From fddc3c29dbefa272b27f42af9c491119a66b1f8d Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Thu, 21 Nov 2024 06:30:29 +0000 Subject: [PATCH 16/22] modifying --- python/paddle/tensor/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index f8769f58b49c00..927101f547ff4f 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -104,8 +104,8 @@ transpose_, ) from .logic import ( # noqa: F401 - allclose, __rand__, + allclose, bitwise_and, bitwise_and_, bitwise_invert, From 024397de5b3442798a6b12379efca01cbe4934d9 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 25 Nov 2024 11:17:49 +0000 Subject: [PATCH 17/22] try --- paddle/fluid/pybind/tensor.cc | 22 +++++++++ python/paddle/__init__.py | 2 + python/paddle/tensor/__init__.py | 1 + python/paddle/tensor/logic.py | 5 -- python/paddle/tensor/tensor.prototype.pyi | 11 +++++ python/paddle/utils/dlpack.py | 40 ++++++++++++++++ test/legacy_test/test_dlpack.py | 56 +++++++++++++++++++++++ 7 files changed, 132 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/pybind/tensor.cc b/paddle/fluid/pybind/tensor.cc index 43c3c1a90e0519..008420e38dbd7e 100644 --- a/paddle/fluid/pybind/tensor.cc +++ b/paddle/fluid/pybind/tensor.cc @@ -446,6 +446,28 @@ void BindTensor(pybind11::module &m) { // NOLINT }); return capsule; }) + .def( + "__dlpack__", + [](phi::DenseTensor &self) { + DLManagedTensor *dlMTensor = framework::toDLPack(self); + return pybind11::capsule( + dlMTensor, + "dltensor", + [](PyObject *capsule) { + DLManagedTensor *managedTensor = + reinterpret_cast( + PyCapsule_GetPointer(capsule, "dltensor")); + if (managedTensor && managedTensor->deleter) { + managedTensor->deleter(managedTensor); + } + }); + }, + R"DOC( + Encode the tensor to a DLPack capsule. + + Returns: + PyCapsule: The DLPack representation of the tensor. + )DOC") .def("_set_float_element", TensorSetElement) .def("_get_float_element", TensorGetElement) .def("_set_double_element", TensorSetElement) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 880ca06a041f10..c896221449908c 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -591,6 +591,7 @@ from .utils.dlpack import ( from_dlpack, to_dlpack, + __dlpack__, ) # CINN has to set a flag to include a lib @@ -1212,6 +1213,7 @@ 'positive', 'from_dlpack', 'to_dlpack', + '__dlpack__', 'inf', 'newaxis', 'nan', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index cb10f91f44d74e..c32f1af63bd668 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -466,6 +466,7 @@ var, ) from .to_string import set_printoptions # noqa: F401 +from paddle.utils.dlpack import __dlpack__ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index aa5b02a96f8ca2..019985afd9ccfc 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1275,10 +1275,6 @@ def bitwise_and( def __rand__(x: Tensor, y: int | bool): -<<<<<<< HEAD - y_tensor = paddle.to_tensor(y, dtype=x.dtype) - return bitwise_and(y_tensor, x, None, None) -======= if isinstance(y, (int, bool)): y_tensor = paddle.to_tensor(y, dtype=x.dtype) return bitwise_and(y_tensor, x, None, None) @@ -1286,7 +1282,6 @@ def __rand__(x: Tensor, y: int | bool): raise TypeError( f"unsupported operand type(s) for |: '{type(y).__name__}' and 'Tensor'" ) ->>>>>>> ceccbf37b1a1119e8d3f699c94a4e503f3bac16d @inplace_apis_in_dygraph_only diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index 8bff376ff78897..b5c152be6d13ec 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -302,6 +302,17 @@ class AbstractTensor: # annotation: ${tensor_alias} +class AbstractTensor: + # Existing methods... + + # DLPack method + def __dlpack__(self) -> Any: ... + + # Example of other methods for context + def numpy(self) -> npt.NDArray[Any]: ... + @property + def place(self) -> paddle.core.Place: ... + class Tensor(AbstractTensor, AbstractEagerParamBase): # annotation: ${tensor_docstring} diff --git a/python/paddle/utils/dlpack.py b/python/paddle/utils/dlpack.py index 33f35c813e6539..5586849250d9df 100644 --- a/python/paddle/utils/dlpack.py +++ b/python/paddle/utils/dlpack.py @@ -33,6 +33,7 @@ __all__ = [ 'to_dlpack', 'from_dlpack', + '__dlpack__' ] _T_contra = TypeVar("_T_contra", contravariant=True) @@ -221,3 +222,42 @@ def from_dlpack( out: Tensor = paddle.Tensor(out, place=out._place()) return out + + +def __dlpack__(tensor: paddle.Tensor, stream: int | None = None): + """ + Export a Paddle Tensor to a DLPack capsule for data interchange. + + Args: + tensor (paddle.Tensor): The input tensor. + stream (int | None): An optional stream for synchronization. + + Returns: + CapsuleType: A PyCapsule object representing the tensor in DLPack format. + """ + if not isinstance(tensor, paddle.Tensor): + raise TypeError(f"Expected a paddle.Tensor, but got {type(tensor)}.") + + if tensor.stop_gradient is False: + raise RuntimeError( + "Tensors that require gradients cannot be exported. " + "Call `detach()` before exporting." + ) + if tensor.layout != paddle.strided: + raise RuntimeError( + "Only strided tensors can be exported to DLPack." + ) + + if stream is not None: + if not isinstance(stream, int): + raise TypeError("The `stream` argument must be an int or None.") + if tensor.place.is_gpu_place(): + current_stream = paddle.device.cuda.current_stream() + if stream != 1 and stream != current_stream.cuda_stream: + event = paddle.device.cuda.Event() + event.record(current_stream) + sync_stream = paddle.device.cuda.ExternalStream(stream) + sync_stream.wait_event(event) + + return paddle.utils.dlpack.to_dlpack(tensor) + diff --git a/test/legacy_test/test_dlpack.py b/test/legacy_test/test_dlpack.py index 064318f19e886b..ff67cdcac1ca74 100644 --- a/test/legacy_test/test_dlpack.py +++ b/test/legacy_test/test_dlpack.py @@ -325,6 +325,62 @@ def test_to_dlpack_from_zero_size(self): np.testing.assert_array_equal(x.numpy(), y1.numpy()) np.testing.assert_array_equal(x.numpy(), y2.numpy()) + def test__dlpack_basic(self): + with dygraph_guard(): + tensor = paddle.to_tensor(np.array([1, 2, 3, 4]).astype("int")) + dlpack_from_method = tensor.__dlpack__() + out_from_dlpack = paddle.utils.dlpack.from_dlpack(dlpack_from_method) + + self.assertTrue(isinstance(out_from_dlpack, paddle.base.core.eager.Tensor)) + self.assertEqual(str(tensor.place), str(out_from_dlpack.place)) + np.testing.assert_array_equal( + out_from_dlpack.numpy(), np.array([1, 2, 3, 4]).astype("int") + ) + + def test__dlpack_consistency_with_to_dlpack(self): + with dygraph_guard(): + tensor = paddle.to_tensor(np.random.rand(4, 5).astype("float32")) + dlpack_via_method = tensor.__dlpack__() + dlpack_via_func = paddle.to_dlpack(tensor) + + self.assertEqual(dlpack_via_method.__dlpack_device__, dlpack_via_func.__dlpack_device__) + self.assertEqual(dlpack_via_method.__dlpack_dtype__, dlpack_via_func.__dlpack_dtype__) + + out_from_method = paddle.from_dlpack(dlpack_via_method) + out_from_func = paddle.from_dlpack(dlpack_via_func) + + np.testing.assert_allclose(out_from_method.numpy(), out_from_func.numpy(), rtol=1e-05) + + def test__dlpack_on_special_cases(self): + with dygraph_guard(): + zero_dim_tensor = paddle.to_tensor(1.0) + dlpack = zero_dim_tensor.__dlpack__() + out_from_dlpack = paddle.from_dlpack(dlpack) + self.assertEqual(out_from_dlpack.shape, []) + np.testing.assert_array_equal(zero_dim_tensor.numpy(), out_from_dlpack.numpy()) + + empty_tensor = paddle.zeros([0, 10]) + dlpack = empty_tensor.__dlpack__() + out_from_dlpack = paddle.from_dlpack(dlpack) + self.assertEqual(out_from_dlpack.shape, [0, 10]) + np.testing.assert_array_equal(empty_tensor.numpy(), out_from_dlpack.numpy()) + + def test__dlpack_on_different_devices_and_dtypes(self): + with dygraph_guard(): + dtypes = ["float32", "int32", "bool"] + places = [base.CPUPlace()] + if paddle.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) + + for dtype in dtypes: + for place in places: + tensor = paddle.ones([3, 4], dtype=dtype) + dlpack = tensor.__dlpack__() + out = paddle.from_dlpack(dlpack) + self.assertEqual(str(tensor.place), str(out.place)) + self.assertEqual(tensor.dtype, out.dtype) + np.testing.assert_array_equal(tensor.numpy(), out.numpy()) + class TestRaiseError(unittest.TestCase): def test_to_dlpack_raise_type_error(self): From 9348366dc2c53d3ce9649bb69e744fae9cf409d8 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Mon, 25 Nov 2024 11:44:44 +0000 Subject: [PATCH 18/22] modify --- python/paddle/tensor/tensor.prototype.pyi | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index b5c152be6d13ec..3757af668eaed1 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -301,18 +301,8 @@ class AbstractTensor: def _grad_ivar(self) -> Tensor | None: ... # annotation: ${tensor_alias} - -class AbstractTensor: - # Existing methods... - - # DLPack method def __dlpack__(self) -> Any: ... - # Example of other methods for context - def numpy(self) -> npt.NDArray[Any]: ... - @property - def place(self) -> paddle.core.Place: ... - class Tensor(AbstractTensor, AbstractEagerParamBase): # annotation: ${tensor_docstring} From a79bad96071699e271b972e96839f7a86c255cb4 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Tue, 26 Nov 2024 01:54:24 +0000 Subject: [PATCH 19/22] modify --- paddle/fluid/pybind/tensor.cc | 22 ---------- python/paddle/__init__.py | 1 - python/paddle/tensor/__init__.py | 1 - python/paddle/utils/dlpack.py | 41 +++---------------- setup.py | 2 + test/legacy_test/test_dlpack.py | 70 +++++++------------------------- 6 files changed, 22 insertions(+), 115 deletions(-) diff --git a/paddle/fluid/pybind/tensor.cc b/paddle/fluid/pybind/tensor.cc index 008420e38dbd7e..43c3c1a90e0519 100644 --- a/paddle/fluid/pybind/tensor.cc +++ b/paddle/fluid/pybind/tensor.cc @@ -446,28 +446,6 @@ void BindTensor(pybind11::module &m) { // NOLINT }); return capsule; }) - .def( - "__dlpack__", - [](phi::DenseTensor &self) { - DLManagedTensor *dlMTensor = framework::toDLPack(self); - return pybind11::capsule( - dlMTensor, - "dltensor", - [](PyObject *capsule) { - DLManagedTensor *managedTensor = - reinterpret_cast( - PyCapsule_GetPointer(capsule, "dltensor")); - if (managedTensor && managedTensor->deleter) { - managedTensor->deleter(managedTensor); - } - }); - }, - R"DOC( - Encode the tensor to a DLPack capsule. - - Returns: - PyCapsule: The DLPack representation of the tensor. - )DOC") .def("_set_float_element", TensorSetElement) .def("_get_float_element", TensorGetElement) .def("_set_double_element", TensorSetElement) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index c896221449908c..8524e0ade0199f 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -591,7 +591,6 @@ from .utils.dlpack import ( from_dlpack, to_dlpack, - __dlpack__, ) # CINN has to set a flag to include a lib diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index c32f1af63bd668..cb10f91f44d74e 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -466,7 +466,6 @@ var, ) from .to_string import set_printoptions # noqa: F401 -from paddle.utils.dlpack import __dlpack__ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ diff --git a/python/paddle/utils/dlpack.py b/python/paddle/utils/dlpack.py index 5586849250d9df..372f55461b4071 100644 --- a/python/paddle/utils/dlpack.py +++ b/python/paddle/utils/dlpack.py @@ -224,40 +224,9 @@ def from_dlpack( return out -def __dlpack__(tensor: paddle.Tensor, stream: int | None = None): - """ - Export a Paddle Tensor to a DLPack capsule for data interchange. - - Args: - tensor (paddle.Tensor): The input tensor. - stream (int | None): An optional stream for synchronization. - - Returns: - CapsuleType: A PyCapsule object representing the tensor in DLPack format. - """ - if not isinstance(tensor, paddle.Tensor): - raise TypeError(f"Expected a paddle.Tensor, but got {type(tensor)}.") - - if tensor.stop_gradient is False: - raise RuntimeError( - "Tensors that require gradients cannot be exported. " - "Call `detach()` before exporting." - ) - if tensor.layout != paddle.strided: - raise RuntimeError( - "Only strided tensors can be exported to DLPack." - ) - - if stream is not None: - if not isinstance(stream, int): - raise TypeError("The `stream` argument must be an int or None.") - if tensor.place.is_gpu_place(): - current_stream = paddle.device.cuda.current_stream() - if stream != 1 and stream != current_stream.cuda_stream: - event = paddle.device.cuda.Event() - event.record(current_stream) - sync_stream = paddle.device.cuda.ExternalStream(stream) - sync_stream.wait_event(event) - - return paddle.utils.dlpack.to_dlpack(tensor) +def enable_dlpack(): + from ..base import Tensor + def __dlpack__(self, *, stream=None): + return to_dlpack(self, stream=stream) + Tensor.__dlpack__ = __dlpack__ diff --git a/setup.py b/setup.py index 95cd638ab49e7d..9d9fb6b4a83b8c 100644 --- a/setup.py +++ b/setup.py @@ -37,6 +37,7 @@ from setuptools.command.install import install as InstallCommandBase from setuptools.command.install_lib import install_lib from setuptools.dist import Distribution +from paddle.utils.dlpack import enable_dlpack python_version = platform.python_version() version_detail = sys.version_info @@ -2374,6 +2375,7 @@ def main(): 'Typing :: Typed', ], ) + enable_dlpack() if __name__ == '__main__': diff --git a/test/legacy_test/test_dlpack.py b/test/legacy_test/test_dlpack.py index ff67cdcac1ca74..315368f9302463 100644 --- a/test/legacy_test/test_dlpack.py +++ b/test/legacy_test/test_dlpack.py @@ -325,61 +325,21 @@ def test_to_dlpack_from_zero_size(self): np.testing.assert_array_equal(x.numpy(), y1.numpy()) np.testing.assert_array_equal(x.numpy(), y2.numpy()) - def test__dlpack_basic(self): - with dygraph_guard(): - tensor = paddle.to_tensor(np.array([1, 2, 3, 4]).astype("int")) - dlpack_from_method = tensor.__dlpack__() - out_from_dlpack = paddle.utils.dlpack.from_dlpack(dlpack_from_method) - - self.assertTrue(isinstance(out_from_dlpack, paddle.base.core.eager.Tensor)) - self.assertEqual(str(tensor.place), str(out_from_dlpack.place)) - np.testing.assert_array_equal( - out_from_dlpack.numpy(), np.array([1, 2, 3, 4]).astype("int") - ) - - def test__dlpack_consistency_with_to_dlpack(self): - with dygraph_guard(): - tensor = paddle.to_tensor(np.random.rand(4, 5).astype("float32")) - dlpack_via_method = tensor.__dlpack__() - dlpack_via_func = paddle.to_dlpack(tensor) - - self.assertEqual(dlpack_via_method.__dlpack_device__, dlpack_via_func.__dlpack_device__) - self.assertEqual(dlpack_via_method.__dlpack_dtype__, dlpack_via_func.__dlpack_dtype__) - - out_from_method = paddle.from_dlpack(dlpack_via_method) - out_from_func = paddle.from_dlpack(dlpack_via_func) - - np.testing.assert_allclose(out_from_method.numpy(), out_from_func.numpy(), rtol=1e-05) - - def test__dlpack_on_special_cases(self): - with dygraph_guard(): - zero_dim_tensor = paddle.to_tensor(1.0) - dlpack = zero_dim_tensor.__dlpack__() - out_from_dlpack = paddle.from_dlpack(dlpack) - self.assertEqual(out_from_dlpack.shape, []) - np.testing.assert_array_equal(zero_dim_tensor.numpy(), out_from_dlpack.numpy()) - - empty_tensor = paddle.zeros([0, 10]) - dlpack = empty_tensor.__dlpack__() - out_from_dlpack = paddle.from_dlpack(dlpack) - self.assertEqual(out_from_dlpack.shape, [0, 10]) - np.testing.assert_array_equal(empty_tensor.numpy(), out_from_dlpack.numpy()) - - def test__dlpack_on_different_devices_and_dtypes(self): - with dygraph_guard(): - dtypes = ["float32", "int32", "bool"] - places = [base.CPUPlace()] - if paddle.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - - for dtype in dtypes: - for place in places: - tensor = paddle.ones([3, 4], dtype=dtype) - dlpack = tensor.__dlpack__() - out = paddle.from_dlpack(dlpack) - self.assertEqual(str(tensor.place), str(out.place)) - self.assertEqual(tensor.dtype, out.dtype) - np.testing.assert_array_equal(tensor.numpy(), out.numpy()) + def test_dlpack_basic(self): + tensor = paddle.to_tensor([1.0, 2.0, 3.0]) + dlpack_capsule = tensor.__dlpack__() + self.assertIsNotNone(dlpack_capsule) + + def test_dlpack_consistency(self): + tensor = paddle.to_tensor([1.0, 2.0, 3.0]) + dlpack_from_method = tensor.__dlpack__() + dlpack_from_func = paddle.utils.dlpack.to_dlpack(tensor) + self.assertEqual(dlpack_from_method, dlpack_from_func) + + def test_dlpack_stream(self): + tensor = paddle.to_tensor([1.0, 2.0, 3.0]) + dlpack_capsule = tensor.__dlpack__(stream=1) + self.assertIsNotNone(dlpack_capsule) class TestRaiseError(unittest.TestCase): From 0b65cebf43d860511a39fb95d9f3ae5b0f05060f Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Tue, 26 Nov 2024 07:42:34 +0000 Subject: [PATCH 20/22] modify --- python/paddle/__init__.py | 1 - .../base/dygraph/tensor_patch_methods.py | 49 +++++++++++++++++++ python/paddle/utils/dlpack.py | 9 ---- setup.py | 2 - .../test_tensor_attr_consistency.py | 1 + test/legacy_test/test_dlpack.py | 18 +++---- tools/enforce/grep_invalid_enforce.sh | 6 +-- 7 files changed, 61 insertions(+), 25 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 8524e0ade0199f..880ca06a041f10 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -1212,7 +1212,6 @@ 'positive', 'from_dlpack', 'to_dlpack', - '__dlpack__', 'inf', 'newaxis', 'nan', diff --git a/python/paddle/base/dygraph/tensor_patch_methods.py b/python/paddle/base/dygraph/tensor_patch_methods.py index a612a7106d5f2b..759d260516aae1 100644 --- a/python/paddle/base/dygraph/tensor_patch_methods.py +++ b/python/paddle/base/dygraph/tensor_patch_methods.py @@ -127,6 +127,7 @@ def _to_static_var(self, to_parameter=False, **kwargs): 'strides', 'offset', '__cuda_array_interface__', + '__dlpack__', ] param_keys = ['stop_gradient', 'trainable'] if isinstance(self, EagerParamBase): @@ -1330,6 +1331,53 @@ def __cuda_array_interface__(self): "version": 2, } + @property + def __dlpack__(self, stream=None): + """ + Creates a DLPack capsule of the current tensor to be exported to other libraries. + + Args: + stream (int | None): An optional Python integer representing a pointer + to a CUDA stream. Synchronizes the tensor with this + stream before exporting. + If None or -1, no synchronization is performed. + If 0, the default stream is used. + """ + if "gpu" not in str(self.place): + raise AttributeError( + "Can't get __dlpack__ on non-CUDA tensor. " + "Use tensor.cuda() to move the tensor to device memory." + ) + + if self.is_sparse(): + raise AttributeError( + "Can't get __dlpack__ on sparse tensor. " + "Use Tensor.to_dense() to convert to a dense tensor first." + ) + + if not self.stop_gradient: + raise RuntimeError( + "Can't get __dlpack__ on Tensor that requires gradients. " + "If gradients aren't required, use tensor.detach() to get a tensor without gradient." + ) + + if stream is not None and not isinstance(stream, int): + raise TypeError("stream must be an integer or None") + + if stream is not None and stream != -1: + if self.place.is_gpu_place(): + if stream == 0: + stream = paddle.device.cuda.default_stream() + else: + stream = paddle.device.cuda.ExternalStream(stream) + current_stream = paddle.device.cuda.current_stream() + if stream != current_stream: + event = paddle.device.cuda.Event() + event.record(current_stream) + stream.wait_event(event) + + return paddle.utils.dlpack.to_dlpack(self) + if not hasattr(core, "eager"): return @@ -1374,6 +1422,7 @@ def __cuda_array_interface__(self): ("_use_gpudnn", _use_gpudnn), ("_md5sum", _md5sum), ("__cuda_array_interface__", __cuda_array_interface__), + ("__dlpack__", __dlpack__), ): setattr(core.eager.Tensor, method_name, method) diff --git a/python/paddle/utils/dlpack.py b/python/paddle/utils/dlpack.py index 372f55461b4071..33f35c813e6539 100644 --- a/python/paddle/utils/dlpack.py +++ b/python/paddle/utils/dlpack.py @@ -33,7 +33,6 @@ __all__ = [ 'to_dlpack', 'from_dlpack', - '__dlpack__' ] _T_contra = TypeVar("_T_contra", contravariant=True) @@ -222,11 +221,3 @@ def from_dlpack( out: Tensor = paddle.Tensor(out, place=out._place()) return out - - -def enable_dlpack(): - from ..base import Tensor - def __dlpack__(self, *, stream=None): - return to_dlpack(self, stream=stream) - - Tensor.__dlpack__ = __dlpack__ diff --git a/setup.py b/setup.py index 9d9fb6b4a83b8c..95cd638ab49e7d 100644 --- a/setup.py +++ b/setup.py @@ -37,7 +37,6 @@ from setuptools.command.install import install as InstallCommandBase from setuptools.command.install_lib import install_lib from setuptools.dist import Distribution -from paddle.utils.dlpack import enable_dlpack python_version = platform.python_version() version_detail = sys.version_info @@ -2375,7 +2374,6 @@ def main(): 'Typing :: Typed', ], ) - enable_dlpack() if __name__ == '__main__': diff --git a/test/dygraph_to_static/test_tensor_attr_consistency.py b/test/dygraph_to_static/test_tensor_attr_consistency.py index d39494a84a559b..54952e58942067 100644 --- a/test/dygraph_to_static/test_tensor_attr_consistency.py +++ b/test/dygraph_to_static/test_tensor_attr_consistency.py @@ -78,6 +78,7 @@ 'value', 'zero_', "__cuda_array_interface__", + "__dlpack__", ] ) STATIC_ONLY_TENSOR_ATTRS_ALLOW_LIST = OrderedSet( diff --git a/test/legacy_test/test_dlpack.py b/test/legacy_test/test_dlpack.py index 315368f9302463..f5fb9542e68ad6 100644 --- a/test/legacy_test/test_dlpack.py +++ b/test/legacy_test/test_dlpack.py @@ -326,20 +326,18 @@ def test_to_dlpack_from_zero_size(self): np.testing.assert_array_equal(x.numpy(), y2.numpy()) def test_dlpack_basic(self): - tensor = paddle.to_tensor([1.0, 2.0, 3.0]) + """Test __dlpack__ and from_dlpack interoperability""" + tensor = paddle.to_tensor([1.0, 2.0, 3.0]).cuda() dlpack_capsule = tensor.__dlpack__() self.assertIsNotNone(dlpack_capsule) - def test_dlpack_consistency(self): - tensor = paddle.to_tensor([1.0, 2.0, 3.0]) - dlpack_from_method = tensor.__dlpack__() - dlpack_from_func = paddle.utils.dlpack.to_dlpack(tensor) - self.assertEqual(dlpack_from_method, dlpack_from_func) + converted_tensor = paddle.from_dlpack(dlpack_capsule) + self.assertTrue(paddle.equal_all(tensor, converted_tensor)) + self.assertEqual(tensor.data_ptr(), converted_tensor.data_ptr()) - def test_dlpack_stream(self): - tensor = paddle.to_tensor([1.0, 2.0, 3.0]) - dlpack_capsule = tensor.__dlpack__(stream=1) - self.assertIsNotNone(dlpack_capsule) + if paddle.device.is_compiled_with_cuda(): + stream = paddle.device.cuda.default_stream().id() + tensor.__dlpack__(stream) class TestRaiseError(unittest.TestCase): diff --git a/tools/enforce/grep_invalid_enforce.sh b/tools/enforce/grep_invalid_enforce.sh index 04243bfb9afaf0..9e653c6b90fcc4 100644 --- a/tools/enforce/grep_invalid_enforce.sh +++ b/tools/enforce/grep_invalid_enforce.sh @@ -17,14 +17,14 @@ # This script is used to grep invalid PADDLE checks by directory or file in the paddle/fluid/, # the result show all invalid PADDLE checks in specified directory or file. -# Usage: +# Usage: # - bash grep_invalid_enforce.sh [target directory or file] (run in tools directory) # - The default check path is paddle/fluid/operators # Result Examples: # 1. grep invalid PADDLE checks in directory -# - Command: /work/paddle/tools {develop} bash grep_invalid_enforce.sh ../paddle/fluid/imperative +# - Command: /work/paddle/tools {develop} bash grep_invalid_enforce.sh ../paddle/fluid/imperative # - Results: # - paddle/fluid/imperative/gradient_accumulator.cc # PADDLE_ENFORCE_EQ(dst_tensor->numel() == numel, true, @@ -60,7 +60,7 @@ # "Place cannot be CUDAPlace when use_double_buffer is False"); # PADDLE_ENFORCE_NOT_NULL(exceptions_[i]); # PADDLE_ENFORCE_EQ(status, Status::kException); -# PADDLE_ENFORCE_EQ(status, Status::kSuccess); +# PADDLE_ENFORCE_EQ(status, Status::kSuccess); . ./count_enforce_by_file.sh --source-only From 6976b37830539af45815b09dd2afa34e2b6b2625 Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Tue, 26 Nov 2024 08:06:02 +0000 Subject: [PATCH 21/22] remove prototype --- python/paddle/tensor/tensor.prototype.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/tensor/tensor.prototype.pyi b/python/paddle/tensor/tensor.prototype.pyi index 3757af668eaed1..8bff376ff78897 100644 --- a/python/paddle/tensor/tensor.prototype.pyi +++ b/python/paddle/tensor/tensor.prototype.pyi @@ -301,7 +301,6 @@ class AbstractTensor: def _grad_ivar(self) -> Tensor | None: ... # annotation: ${tensor_alias} - def __dlpack__(self) -> Any: ... class Tensor(AbstractTensor, AbstractEagerParamBase): # annotation: ${tensor_docstring} From 4e6dc052322f686b15869f13d2fa34af0a5c36db Mon Sep 17 00:00:00 2001 From: SCUcookie <2450603126@qq.com> Date: Wed, 27 Nov 2024 03:09:42 +0000 Subject: [PATCH 22/22] finish --- python/paddle/base/dygraph/tensor_patch_methods.py | 1 - test/legacy_test/test_dlpack.py | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/python/paddle/base/dygraph/tensor_patch_methods.py b/python/paddle/base/dygraph/tensor_patch_methods.py index 759d260516aae1..d8c589435cc3b4 100644 --- a/python/paddle/base/dygraph/tensor_patch_methods.py +++ b/python/paddle/base/dygraph/tensor_patch_methods.py @@ -1331,7 +1331,6 @@ def __cuda_array_interface__(self): "version": 2, } - @property def __dlpack__(self, stream=None): """ Creates a DLPack capsule of the current tensor to be exported to other libraries. diff --git a/test/legacy_test/test_dlpack.py b/test/legacy_test/test_dlpack.py index f5fb9542e68ad6..6924e72250a965 100644 --- a/test/legacy_test/test_dlpack.py +++ b/test/legacy_test/test_dlpack.py @@ -326,8 +326,7 @@ def test_to_dlpack_from_zero_size(self): np.testing.assert_array_equal(x.numpy(), y2.numpy()) def test_dlpack_basic(self): - """Test __dlpack__ and from_dlpack interoperability""" - tensor = paddle.to_tensor([1.0, 2.0, 3.0]).cuda() + tensor = paddle.to_tensor([1.0, 2.0, 3.0]) dlpack_capsule = tensor.__dlpack__() self.assertIsNotNone(dlpack_capsule) @@ -335,10 +334,6 @@ def test_dlpack_basic(self): self.assertTrue(paddle.equal_all(tensor, converted_tensor)) self.assertEqual(tensor.data_ptr(), converted_tensor.data_ptr()) - if paddle.device.is_compiled_with_cuda(): - stream = paddle.device.cuda.default_stream().id() - tensor.__dlpack__(stream) - class TestRaiseError(unittest.TestCase): def test_to_dlpack_raise_type_error(self):