
    ёiM%                    *   S SK Jr  S SKrS SKrS SKJr  S SKJrJrJrJ	r	  S SK
r
SSKJr  SSKJr  SSKJr  \(       a  S S	KJr  S S
K
Jr  S SKJr  SS/r\	" SSS9r " S S\\   5      r " S S\R                  5      rSS jrSSS.       SS jjrg)    )annotationsN)IntEnum)TYPE_CHECKINGLiteralProtocolTypeVar   )DenseTensor)
check_type)in_dygraph_mode)CapsuleType)Tensor)	PlaceLike	to_dlpackfrom_dlpack	_T_contraT)contravariantc                  L    \ rS rSrSrSSSSS.         S	S jjrS
S jrSrg)SupportDLPack+   z
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
.N)streammax_version	dl_devicecopyc                   g N )selfr   r   r   r   s        S/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/utils/dlpack.py
__dlpack__SupportDLPack.__dlpack__2   s         c                    g r   r   )r   s    r   __dlpack_device__SupportDLPack.__dlpack_device__;   s    3r"   r   )
r   zNone | _T_contrar   ztuple[int, int] | Noner   ztuple[IntEnum, int] | Noner   bool | Nonereturnr   )r'   ztuple[int, Literal[0]])__name__
__module____qualname____firstlineno____doc__r    r$   __static_attributes__r   r"   r   r   r   +   sQ     $'.104  ! ,	
 .  
 ?r"   r   c                  T    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrSrSrSrSrSrSrg)DLDeviceType>   )   )r	   )   )   )   )   )	   )
   )   )   )   )   )   )   )   )   r   N)r(   r)   r*   r+   kDLCPUkDLCUDAkDLCUDAHost	kDLOpenCL	kDLVulkankDLMetalkDLVPIkDLROCMkDLROCMHost	kDLExtDevkDLCUDAManaged	kDLOneAPI	kDLWebGPU
kDLHexagonkDLMAIAkDLTrnr-   r   r"   r   r/   r/   >   sR    FGKIIHFGKINIIJGFr"   r/   c                *   [        5       (       ac  [        U [        R                  5      (       d  [	        S[        U 5       S35      eU R                  5       R                  5       R                  5       $ [        U S[        S5        U R                  5       $ )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.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.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.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 .xr   )r   
isinstancepaddler   	TypeErrortypevalue
get_tensor
_to_dlpackr   r
   )rR   s    r   r   r   Q   s{    \ !V]]++!!%a	, 
 wwy##%0022q#k2<<>r"   )devicer   c                  [        U S5      (       GaD  0 nSUS'   Ub  X#S'   UbU  [        R                  R                  R	                  U5      n[        R                  R
                  R                  U5      US'   U R                  5       nUS   [        R                  4;   a  [        R                  " 5          [        R                  " S[        S	9  [        R                  R                  R!                  US
   5      nSSS5        US   [        R                  :H  nU(       a  WR"                  S:X  a  S
OWR"                  nXS'    U R$                  " S0 UD6n	OU n	[        R                  R
                  R+                  U	5      n
[-        5       (       a"  [        R.                  " XR1                  5       S9n
U
$ ! , (       d  f       N= f! [&         a&    UR)                  S5        U R$                  " S0 UD6n	 Nf = f)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.

    device (PlaceLike, optional): The device of the returned tensor. If not
        specified, the device will be the same as that of the input `dlpack`.
    copy (bool, optional): Whether or not to copy the input.
        If True, the output tensor always copied. If False, the output tensor must never
        copied, and raise a BufferError in case a copy is deemed necessary. If None, the
        output tensor must reuse the existing memory buffer if possible and copy otherwise.
        Default: None.

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.to_dlpack(x)

        >>> y = paddle.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>
        >>> # doctest: -SKIP

        >>> 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.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    )r1   r	   r   Nr   r   r   ignore)categoryr1   r   )placer   )hasattrrT   base	framework_get_paddle_placecoreplace_to_dl_devicer$   r/   rA   warningscatch_warningsfilterwarningsUserWarningrZ   cudacurrent_streamcuda_streamr    rU   popr   r   r   _place)dlpackrZ   r   kwargsr^   dlpack_devicer   is_gpu
stream_ptrdlpack_outs              r   r   r      s   Z v|$$ &}!6NKK));;FCE"(++"2"2"E"Ee"LF;002  4 466((*'';G++::=;KL + #1%)=)==F 2 2a 7V=O=O   *8	2''1&1G -3[[-=-=-I-I.C mmCzz|<J? +*   	2JJ}%''1&1G	2s   6AF5G 5
G-G65G6)rR   r   r'   r   )rn   zSupportDLPack | CapsuleTyperZ   zPlaceLike | Noner   r&   r'   r   )
__future__r   enumre   r   typingr   r   r   r   rT   	base.corer
   base.data_feederr   base.frameworkr   typing_extensionsr   r   paddle._typingr   __all__r   r   r/   r   r   r   r"   r   <module>r~      s    #    < <  # ) ,-( 
 Kt4	?HY' ?&4<< &8|  $	z'z z 	z
 zr"   