-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
paddle/fluid/pybind/tensor.cc
Outdated
.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") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的谢谢老师
python/paddle/__init__.py
Outdated
@@ -1212,6 +1212,7 @@ | |||
'positive', | |||
'from_dlpack', | |||
'to_dlpack', | |||
'__dlpack__', |
There was a problem hiding this comment.
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: ... |
There was a problem hiding this comment.
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
python/paddle/utils/dlpack.py
Outdated
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不应该改动这个文件
third_party/pybind
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要改pybind
There was a problem hiding this comment.
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): |
def test_dlpack_basic(self): | ||
tensor = paddle.to_tensor([1.0, 2.0, 3.0]) | ||
dlpack_capsule = tensor.__dlpack__() | ||
self.assertIsNotNone(dlpack_capsule) |
There was a problem hiding this comment.
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()
是不是一样的
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
删除,__dlpack__
不是一个属性,而是一个方法(函数)
third_party/pybind
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个submodule不要修改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件记得还原回去
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()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个单测有两个问题:
- 设备类型没有测试全,只测了当前默认设备。
- 没有测试stream这个参数非None的情况
Tensor.__dlpack__
PR Category
User Experience
PR Types
New features
Description
Paddle Tensor 规范化:新增
Tensor.__dlpack__