Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【SCU】【Paddle Tensor No.3】新增 Tensor.__dlpack__ #69689

Closed
wants to merge 33 commits into from

Conversation

SCUcookie
Copy link
Contributor

@SCUcookie SCUcookie commented Nov 25, 2024

PR Category

User Experience

PR Types

New features

Description

Paddle Tensor 规范化:新增 Tensor.__dlpack__

SCUcookie and others added 28 commits November 14, 2024 22:47
try
try
Copy link

paddle-bot bot commented Nov 25, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Nov 25, 2024
@HydrogenSulfate
Copy link
Contributor

提交代码前请安装pre-commit
image

Comment on lines 449 to 470
.def(
"__dlpack__",
[](phi::DenseTensor &self) {
DLManagedTensor *dlMTensor = framework::toDLPack(self);
return pybind11::capsule(
dlMTensor,
"dltensor",
[](PyObject *capsule) {
DLManagedTensor *managedTensor =
reinterpret_cast<DLManagedTensor *>(
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个复用 paddle.to_dlpack即可,不用写C++代码,可以参考:https://github.com/pytorch/pytorch/blob/main/torch/_tensor.py#L1565

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的谢谢老师

@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Nov 26, 2024
@@ -1212,6 +1212,7 @@
'positive',
'from_dlpack',
'to_dlpack',
'__dlpack__',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__dlpack__是Tensor的方法,不是paddle模块下的函数

@@ -301,6 +301,7 @@ class AbstractTensor:
def _grad_ivar(self) -> Tensor | None: ...

# annotation: ${tensor_alias}
def __dlpack__(self) -> Any: ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个返回的是Any吗?应该是CapsuleType,参考Paddle/python/paddle/utils/dlpack.py

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应该改动这个文件

setup.py Outdated
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应该改动这个文件

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要改pybind

Copy link
Contributor

@HydrogenSulfate HydrogenSulfate Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__dlpack__作为Tensor的类方法,和__cuda_array_interface__是类似的:

def __cuda_array_interface__(self):
参考这个PR写一下就行:#68192

Comment on lines +328 to +331
def test_dlpack_basic(self):
tensor = paddle.to_tensor([1.0, 2.0, 3.0])
dlpack_capsule = tensor.__dlpack__()
self.assertIsNotNone(dlpack_capsule)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只判断是不是None是不够的,需要将dlpack_capsule用paddle.from_dlpack重新转回Tensor,检测转换前的两个tensor里的值,以及.data_ptr()是不是一样的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的老师

@@ -1330,6 +1331,53 @@ def __cuda_array_interface__(self):
"version": 2,
}

@property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除,__dlpack__不是一个属性,而是一个方法(函数)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个submodule不要修改

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件记得还原回去

Comment on lines +328 to +335
def test_dlpack_basic(self):
tensor = paddle.to_tensor([1.0, 2.0, 3.0])
dlpack_capsule = tensor.__dlpack__()
self.assertIsNotNone(dlpack_capsule)

converted_tensor = paddle.from_dlpack(dlpack_capsule)
self.assertTrue(paddle.equal_all(tensor, converted_tensor))
self.assertEqual(tensor.data_ptr(), converted_tensor.data_ptr())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个单测有两个问题:

  1. 设备类型没有测试全,只测了当前默认设备。
  2. 没有测试stream这个参数非None的情况

@HydrogenSulfate HydrogenSulfate changed the title 【SCU】【Paddle Tensor No.3】新增 Tensor.__dlpack__ 【SCU】【Paddle Tensor No.3】新增 Tensor.__dlpack__ Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants