a
    ßñh^  ã                   @   sH   d dl Z d dlZddlmZmZ G dd„ deƒZdd„ Zee_ee_	dS )é    Né   )Ú_typeÚ_cudac                       sè   e Zd ZdZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zed2d3„ ƒZ‡  ZS )4Ú_StorageBaseFc                    sD   dd  ‡ fdd„ttˆ ƒƒD ƒ¡ }|dt ˆ ¡› dtˆ ƒ› d� S )Nú z
 c                 3   s   | ]}t ˆ | ƒV  qd S ©N©Ústr)Ú.0Úi©Úself© úM/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/storage.pyÚ	<genexpr>   ó    z'_StorageBase.__str__.<locals>.<genexpr>z
[z	 of size ú])ÚjoinÚrangeÚlenÚtorchÚtypename)r   Úcontentr   r   r   Ú__str__   s    $z_StorageBase.__str__c                 C   s   t | ƒS r   r   r   r   r   r   Ú__repr__   s    z_StorageBase.__repr__c                    s   t t‡ fdd„tˆ  ¡ ƒƒƒS )Nc                    s   ˆ |  S r   r   )r   r   r   r   Ú<lambda>   r   z'_StorageBase.__iter__.<locals>.<lambda>)ÚiterÚmapr   Úsizer   r   r   r   Ú__iter__   s    z_StorageBase.__iter__c                 C   s   |   ¡ S r   )Úcloner   r   r   r   Ú__copy__   s    z_StorageBase.__copy__c                 C   s6   |  di ¡}| j|v r || j S |  ¡ }||| j< |S )Nr   )Ú
setdefaultZ_cdatar    )r   ÚmemoZnew_storager   r   r   Ú__deepcopy__   s    


z_StorageBase.__deepcopy__c                 C   s&   t  ¡ }tj| |dd� t| ¡ ffS )NF)Z_use_new_zipfile_serialization)ÚioÚBytesIOr   ÚsaveÚ_load_from_bytesÚgetvalue)r   Úbr   r   r   Ú
__reduce__    s    z_StorageBase.__reduce__c                    s   t t| ƒ ¡ |  ¡ |  ¡   S r   )Úsuperr   Ú
__sizeof__Zelement_sizer   r   ©Ú	__class__r   r   r-   %   s    z_StorageBase.__sizeof__c                 C   sX   | j r|  ¡ nd}tj |¡�& t| ƒ|  ¡ ƒ | ¡W  d  ƒ S 1 sJ0    Y  dS )zReturns a copy of this storageéÿÿÿÿN)Úis_cudaZ
get_devicer   ÚcudaÚdeviceÚtyper   Úcopy_)r   r3   r   r   r   r    (   s    z_StorageBase.clonec                 C   s   t | ƒS )z6Returns a list containing the elements of this storage)Úlistr   r   r   r   Útolist.   s    z_StorageBase.tolistc                 C   s   |   tt| jjƒ¡S )zAReturns a CPU copy of this storage if it's not already on the CPU)r4   Úgetattrr   r/   Ú__name__r   r   r   r   Úcpu2   s    z_StorageBase.cpuc                 C   s   |   t | ƒjd ¡S )z!Casts this storage to double typez.DoubleStorage©r4   Ú
__module__r   r   r   r   Údouble6   s    z_StorageBase.doublec                 C   s   |   t | ƒjd ¡S )z Casts this storage to float typez.FloatStorager;   r   r   r   r   Úfloat:   s    z_StorageBase.floatc                 C   s   |   t | ƒjd ¡S )zCasts this storage to half typez.HalfStorager;   r   r   r   r   Úhalf>   s    z_StorageBase.halfc                 C   s   |   t | ƒjd ¡S )zCasts this storage to long typez.LongStorager;   r   r   r   r   ÚlongB   s    z_StorageBase.longc                 C   s   |   t | ƒjd ¡S )zCasts this storage to int typez.IntStorager;   r   r   r   r   ÚintF   s    z_StorageBase.intc                 C   s   |   t | ƒjd ¡S )z Casts this storage to short typez.ShortStorager;   r   r   r   r   ÚshortJ   s    z_StorageBase.shortc                 C   s   |   t | ƒjd ¡S )zCasts this storage to char typez.CharStorager;   r   r   r   r   ÚcharN   s    z_StorageBase.charc                 C   s   |   t | ƒjd ¡S )zCasts this storage to byte typez.ByteStorager;   r   r   r   r   ÚbyteR   s    z_StorageBase.bytec                 C   s   |   t | ƒjd ¡S )zCasts this storage to bool typez.BoolStorager;   r   r   r   r   ÚboolV   s    z_StorageBase.boolc                 C   s   |   t | ƒjd ¡S )z#Casts this storage to bfloat16 typez.BFloat16Storager;   r   r   r   r   Úbfloat16Z   s    z_StorageBase.bfloat16c                 C   s   |   t | ƒjd ¡S )z)Casts this storage to complex double typez.ComplexDoubleStorager;   r   r   r   r   Úcomplex_double^   s    z_StorageBase.complex_doublec                 C   s   |   t | ƒjd ¡S )z(Casts this storage to complex float typez.ComplexFloatStorager;   r   r   r   r   Úcomplex_floatb   s    z_StorageBase.complex_floatc                 C   sF   | j rtd|  ¡ › d�ƒ‚ddl}|j ¡ }t| ƒ|  ¡ |d� | ¡S )z@Copies the storage to pinned memory, if it's not already pinned.zcannot pin 'z' only CPU memory can be pinnedr   N)Ú	allocator)r1   Ú	TypeErrorr4   Z
torch.cudar2   Z_host_allocatorr   r5   )r   r   rI   r   r   r   Ú
pin_memoryf   s
    
z_StorageBase.pin_memoryc                 C   s4   ddl m} | jrn|ƒ dkr(|  ¡  n|  ¡  | S )a  Moves the storage to shared memory.

        This is a no-op for storages already in shared memory and for CUDA
        storages, which do not need to be moved for sharing across processes.
        Storages in shared memory cannot be resized.

        Returns: self
        r   ©Úget_sharing_strategyÚfile_system)Útorch.multiprocessingrM   r1   Z_share_filename_Z
_share_fd_)r   rM   r   r   r   Úshare_memory_n   s    	

z_StorageBase.share_memory_c                 C   s<   ddl m} | jr| |ƒS |ƒ dkr.|  |¡S |  |¡S dS )z>Creates a new storage in shared memory with the same data typer   rL   rN   N)rO   rM   r1   Z_new_using_filenameZ_new_using_fd)Úclsr   rM   r   r   r   Ú_new_shared€   s    

z_StorageBase._new_shared) r9   r<   Ú__qualname__r1   Z	is_sparser   r   r   r!   r$   r+   r-   r    r7   r:   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rK   rP   ÚclassmethodrR   Ú__classcell__r   r   r.   r   r      s8   r   c                 C   s   t  t | ¡¡S r   )r   Úloadr%   r&   )r*   r   r   r   r(   Œ   s    r(   )
r%   r   Ú_utilsr   r   Úobjectr   r(   r4   r2   r   r   r   r   Ú<module>   s    