o
    pi                     @  s   d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	Z	ddl
mZ ddlmZ ddlmZ er>d dlmZ d d	l	mZ d
dgZedddZG dd dee ZG dd dejZddd
ZdddZdS )    )annotationsN)TYPE_CHECKINGLiteralProtocolTypeVar   )DenseTensor)
check_type)in_dygraph_mode)CapsuleType)Tensor	to_dlpackfrom_dlpack	_T_contraT)contravariantc                   @  s*   e Zd ZdZddddd	ZdddZdS )SupportDLPackz
    ref:
        https://github.com/numpy/numpy/blob/7e6e48ca7aacae9994d18a3dadbabd2b91c32151/numpy/__init__.pyi#L3068-L3077
        https://github.com/numpy/numpy/blob/7e6e48ca7aacae9994d18a3dadbabd2b91c32151/numpy/__init__.pyi#L4730-L4731
    .streamr   None | _T_contrareturnr   c                C     d S N )selfr   r   r   Z/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/utils/dlpack.py
__dlpack__0       zSupportDLPack.__dlpack__tuple[int, Literal[0]]c                 C  r   r   r   )r   r   r   r   __dlpack_device__2   r   zSupportDLPack.__dlpack_device__N)r   r   r   r   )r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   )   s    r   c                   @  s4   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )DLDeviceType)   )r   )   )   )   )   )	   )
   )   )   N)r   r    r!   ZkDLCPUkDLCUDAZkDLCUDAHostZ	kDLOpenCLZ	kDLVulkanZkDLMetalZkDLVPIZkDLROCMZ	kDLExtDevZ	kDLOneAPIr   r   r   r   r#   5   s    r#   xr   r   r   c                 C  sL   t  rt| tj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
            :name: code-paddle-to-paddle

            >>> 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>
            >>> #doctest: -SKIP

            >>> # dlpack capsule will be renamed to 'used_dltensor' after decoded
            >>> y = paddle.utils.dlpack.from_dlpack(dlpack)
            >>> print(dlpack)
            >>> # doctest: +SKIP('the address will change in every run')
            <capsule object "used_dltensor" at 0x7f6103c681b0>

        .. code-block:: python
            :name: code-paddle-to-torch

            >>> # doctest: +SKIP('torch will not be installed')
            >>> # type: ignore
            >>> # convert tensor from paddle to other framework using to_dlpack
            >>> import torch

            >>> x = paddle.randn([2, 4]).to(device="cpu")
            >>> y = torch.from_dlpack(paddle.utils.dlpack.to_dlpack(x))
            >>> print(y.shape)
            torch.Size([2, 4])
            >>> # doctest: -SKIP
    zAThe type of 'x' in to_dlpack must be paddle.Tensor, but received .r.   r   )r
   
isinstancepaddler   	TypeErrortypevalueZ
get_tensorZ
_to_dlpackr	   r   )r.   r   r   r   r   B   s   /dlpackSupportDLPack | CapsuleTypec                 C  s   t | drT|  }|d tjfv rOt  tjdtd tj	j
|d }W d   n1 s0w   Y  |d tjk}|rE|jdkrEdn|j}| j|d}n|  }n| }tjj|}t ritj|| d}|S )	a
  
    Decodes a DLPack to a tensor. The returned Paddle tensor will share the memory with
    the tensor from given dlpack.

    Args:
        dlpack (SupportDLPack | CapsuleType): A PyCapsule object with the dltensor,
            or that implements '__dlpack__' and '__dlpack_device__' methods.

            If `dlpack` is a tensor (or ndarray) object, it must support
            the `__dlpack__` protocol (i.e., have a `dlpack.__dlpack__`
            method). Otherwise `dlpack` may be a DLPack capsule, which is
            an opaque `PyCapsule` instance, typically produced by a
            `to_dlpack` function or method.

    Returns:
        out (Tensor): A tensor decoded from DLPack. The data type of returned tensor
            can be one of: ``int32``, ``int64``, ``float16``, ``float32`` and ``float64``.
            The device of returned tensor can be one of: ``CPU``, ``CUDAPlace``, ``CUDAPinnedPlace``.

    Examples:
        .. code-block:: python
            :name: code-paddle-from-paddle

            >>> import paddle
            >>> # From DLPack capsule
            >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
            ...                       [0.1, 0.2, 0.6, 0.7]], place="cpu")
            >>> dlpack = paddle.utils.dlpack.to_dlpack(x)

            >>> y = paddle.utils.dlpack.from_dlpack(dlpack)
            >>> # dlpack capsule will be renamed to 'used_dltensor' after decoded
            >>> print(dlpack)
            >>> # doctest: +SKIP('the address will change in every run')
            <capsule object "used_dltensor" at 0x7f6103c681b0>

            >>> print(y)
            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]])
            >>> # data of tensor x is shared with tensor y
            >>> y[0, 0] = 10.0
            >>> print(x)
            Tensor(shape=[2, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [[10.       , 0.30000001, 0.50000000, 0.89999998],
                    [0.10000000, 0.20000000, 0.60000002, 0.69999999]])

        .. code-block:: python
            :name: code-paddle-from-numpy

            >>> # Directly from external tensor that implements '__dlpack__' and '__dlpack_device__' methods
            >>> import paddle
            >>> import numpy as np
            >>> x = np.array([[0.2, 0.3, 0.5, 0.9],
            ...              [0.1, 0.2, 0.6, 0.7]])
            >>> y = paddle.utils.dlpack.from_dlpack(x)
            >>> y[0, 0] = 10.0
            >>> # data of tensor x is shared with tensor y
            >>> print(x)
            [[10.   0.3  0.5  0.9]
            [ 0.1  0.2  0.6  0.7]]
    r   r   ignore)categoryr$   Nr   )Zplace)hasattrr   r#   r-   warningscatch_warningsfilterwarningsUserWarningr1   devicecudaZcurrent_streamZcuda_streamr   basecorer   r
   r   Z_place)r5   r>   r   Zis_gpuZ
stream_ptrZdlpack_outr   r   r   r   ~   s&   
A
	
)r.   r   r   r   )r5   r6   r   r   )
__future__r   enumr:   typingr   r   r   r   r1   Z	base.corer   Zbase.data_feederr	   Zbase.frameworkr
   Ztyping_extensionsr   r   __all__r   r   IntEnumr#   r   r   r   r   r   r   <module>   s&   
<