o
    #j                     @   sH   d dl Z ddlmZ ddlmZ ddlmZ ddgZdd Zd	d Z	dS )
    N   )	LoDTensor)
check_type)in_dygraph_mode	to_dlpackfrom_dlpackc                 C   sX   t  r!t| tjtjjjjfstdt|  d| 	 
  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)
            >>> # doctest: +SKIP('the address will change in every run')
            <capsule object "dltensor" at 0x7f6103c681b0>
    zAThe type of 'x' in to_dlpack must be paddle.Tensor, but received .xr   )r   
isinstancepaddleZTensorbasecoreeager	TypeErrortypevalueZ
get_tensorZ
_to_dlpackr   r   )r	    r   T/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/utils/dlpack.pyr      s   c                 C   sh   t | }|jdko|jdk}|stdt |  d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=Place(cpu), stop_gradient=True,
                   [[0.20000000, 0.30000001, 0.50000000, 0.89999998],
                    [0.10000000, 0.20000000, 0.60000002, 0.69999999]])
    builtinsZ	PyCapsulezKThe type of 'dlpack' in from_dlpack must be PyCapsule object, but received r   )
r   
__module____name__r   r   r   r   r   r   Z	to_tensor)ZdlpacktZdlpack_flagoutr   r   r   r   A   s   
)
r   Z	base.corer   Zbase.data_feederr   Zbase.frameworkr   __all__r   r   r   r   r   r   <module>   s   &