a
    Ž¥»aý  ã                   @   sj   d dl mZ d dlZd dlZd dlmZ d dlmZ G dd„ dejƒZ	ej
 ed¡ eejdœd	d
„ZdS )é    )ÚAnyN)Ú_from_dlpack)Ú
_to_dlpackc                   @   s0   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )ÚDLDeviceType)é   )é   )é   )é   )é   )é   )é	   )é
   )é   N)Ú__name__Ú
__module__Ú__qualname__ZkDLCPUÚkDLGPUZkDLCPUPinnedZ	kDLOpenCLZ	kDLVulkanZkDLMetalZkDLVPIÚkDLROCMZ	kDLExtDev© r   r   úL/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/utils/dlpack.pyr   
   s   r   zÍto_dlpack(tensor) -> PyCapsule

Returns a DLPack representing the tensor.

Args:
    tensor: a tensor to be exported

The DLPack shares the tensors memory.
Note that each DLPack can only be consumed once.
)Ú
ext_tensorÚreturnc                 C   sb   t | dƒrV|  ¡ }|d tjtjfv rLtj d |d ¡¡}| j	|j
d�}qZ|  	¡ }n| }t|ƒS )a  from_dlpack(ext_tensor) -> Tensor

    Convers a tensor from a external library into a ``torch.Tensor``
    by means of the ``__dlpack__`` protocol.

    The tensor will share the memory with the object represented
    in the DLPack.

    .. warning::
      Only call from_dlpack once per capsule. Its behavior when used
      on the same capsule multiple times is undefined.

    Args:
        ext_tensor (object with __dlpack__ attribute or DLPack capsule):
            The tensor or DLPack capsule to convert.
    Ú
__dlpack__r   zcuda:{}r   )Ústream)ÚhasattrZ__dlpack_device__r   r   r   ÚtorchZcudaZcurrent_streamÚformatr   Zcuda_streamr   )r   Zdevicer   Zdlpackr   r   r   Úfrom_dlpack$   s    

r   )Útypingr   r   ÚenumZtorch._Cr   r   Z	to_dlpackÚIntEnumr   Z_CZ_add_docstrZTensorr   r   r   r   r   Ú<module>   s   