to_dlpack

paddle.utils.dlpack. to_dlpack ( x ) [源代码]

将 Tensor 对象转化为 DLPack。其中,DLPack 是一种开放的内存 Tensor 结构,可用于不同深度学习框架之间的 Tensor 共享。

参数

  • x (Tensor) - Paddle Tensor,并且其数据类型为支持 bool,float16,float32,float64,int8,int16,int32,int64,uint8,complex64,complex128。

返回

  • dlpack (PyCapsule) - DLPack,即带有 dltensor 的 PyCapsule 对象。

代码示例

>>> import paddle
>>> # x is a tensor with shape [2, 4]
>>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
...                       [0.1, 0.2, 0.6, 0.7]])
>>> dlpack = paddle.utils.dlpack.to_dlpack(x)
>>> print(dlpack)
<capsule object "dltensor" at 0x7f6103c681b0>

>>> # dlpack capsule will be renamed to 'used_dltensor' after decoded
>>> y = paddle.utils.dlpack.from_dlpack(dlpack)
>>> print(dlpack)
<capsule object "used_dltensor" at 0x7f6103c681b0>