o
    Qe®  ã                   @   sP   d dl Z ddlmZ ddlmZ ddlmZmZmZ ddgZ	dd„ Z
d	d„ ZdS )
é    Né   )Ú	LoDTensor)Ú_non_static_mode)Ú
check_typeÚcheck_dtypeÚconvert_dtypeÚ	to_dlpackÚfrom_dlpackc                 C   sV   t ƒ r t| tjtjjjjfƒstd t	| ƒ¡ƒ‚|  
¡  ¡  ¡ S t| dtdƒ |  ¡ S )a¿  
    Encodes a tensor to DLPack.

    Args:
        x (Tensor): The input tensor, and the data type can be `bool`, `float16`, `float32`,
                    `float64`, `int8`, `int16`, `int32`, `int64`, `uint8`, `complex64`,
                    `complex128`.

    Returns:
        dltensor, and the data type is PyCapsule.
    
    Examples:
        .. code-block:: python

            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>
    zDThe type of 'x' in to_dlpack must be paddle.Tensor, but received {}.Úxr   )r   Ú
isinstanceÚpaddleZTensorÚfluidÚcoreÚeagerÚ	TypeErrorÚformatÚtypeÚvalueZ
get_tensorZ
_to_dlpackr   r   )r
   © r   úCD:\Projects\ConvertPro\env\Lib\site-packages\paddle/utils/dlpack.pyr      s   
þc                 C   sf   t | ƒ}|jdko|jdk}|std t | ƒ¡ƒ‚tƒ r*tjj 	| ¡}t 
|¡}|S tjj 	| ¡}|S )a§  
    Decodes a DLPack to a tensor.
    
    Args:
        dlpack (PyCapsule): a PyCapsule object with the dltensor.

    Returns:
        out (Tensor): a tensor decoded from DLPack. One thing to be noted, if we get 
                      an input dltensor with data type as `bool`, we return the decoded
                      tensor as `uint8`.

    Examples:
        .. code-block:: python

            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)
            x = paddle.utils.dlpack.from_dlpack(dlpack)
            print(x)
            # Tensor(shape=[2, 4], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
            #  [[0.20000000, 0.30000001, 0.50000000, 0.89999998],
            #  [0.10000000, 0.20000000, 0.60000002, 0.69999999]]) 
    ÚbuiltinsZ	PyCapsulezNThe type of 'dlpack' in from_dlpack must be PyCapsule object, but received {}.)r   Ú
__module__Ú__name__r   r   r   r   r   r   r	   Z	to_tensor)ZdlpackÚtZdlpack_flagÚoutr   r   r   r	   >   s   
þ
)r   Z
fluid.corer   Zfluid.frameworkr   Zfluid.data_feederr   r   r   Ú__all__r   r	   r   r   r   r   Ú<module>   s   þ$