3
O4W^0  ã               @   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ed.d/„ ƒZ‡  ZS )0Ú_StorageBaseFc                s>   ddj ‡ fdd„ttˆ ƒƒD ƒƒ }|djtjˆ ƒtˆ ƒƒ S )Nú z
 c             3   s   | ]}t ˆ | ƒV  qd S )N)Ústr)Ú.0Úi)Úself© ú./tmp/pip-build-e66j97b2/torch/torch/storage.pyú	<genexpr>   s    z'_StorageBase.__str__.<locals>.<genexpr>z
[{} of size {}])ÚjoinÚrangeÚlenÚformatÚtorchÚtypename)r
   Úcontentr   )r
   r   Ú__str__   s    $z_StorageBase.__str__c             C   s   t | ƒS )N)r   )r
   r   r   r   Ú__repr__   s    z_StorageBase.__repr__c                s   t t‡ fdd„tˆ jƒ ƒƒƒS )Nc                s   ˆ |  S )Nr   )r	   )r
   r   r   Ú<lambda>   s    z'_StorageBase.__iter__.<locals>.<lambda>)ÚiterÚmapr   Úsize)r
   r   )r
   r   Ú__iter__   s    z_StorageBase.__iter__c             C   s   | j ƒ S )N)Úclone)r
   r   r   r   Ú__copy__   s    z_StorageBase.__copy__c             C   s6   |j di ƒ}| j|kr || j S | jƒ }||| j< |S )Nr   )Ú
setdefaultZ_cdatar   )r
   ÚmemoZnew_storager   r   r   Ú__deepcopy__   s    


z_StorageBase.__deepcopy__c             C   s"   t jƒ }tj| |ƒ t|jƒ ffS )N)ÚioÚBytesIOr   ÚsaveÚ_load_from_bytesÚgetvalue)r
   Úbr   r   r   Ú
__reduce__    s    z_StorageBase.__reduce__c                s   t t| ƒjƒ | jƒ | jƒ   S )N)Úsuperr   Ú
__sizeof__Zelement_sizer   )r
   )Ú	__class__r   r   r)   %   s    z_StorageBase.__sizeof__c          	   C   s@   | j r| jƒ nd}tjj|ƒ� t| ƒ| jƒ ƒj| ƒS Q R X dS )zReturns a copy of this storager   Néÿÿÿÿ)Úis_cudaZ
get_devicer   ÚcudaÚdeviceÚtyper   Úcopy_)r
   r.   r   r   r   r   (   s    z_StorageBase.clonec             C   s   dd„ | D ƒS )z6Returns a list containing the elements of this storagec             S   s   g | ]}|‘qS r   r   )r   Úvr   r   r   ú
<listcomp>0   s    z'_StorageBase.tolist.<locals>.<listcomp>r   )r
   r   r   r   Útolist.   s    z_StorageBase.tolistc             C   s   | j tt| jjƒƒS )zAReturns a CPU copy of this storage if it's not already on the CPU)r/   Úgetattrr   r*   Ú__name__)r
   r   r   r   Úcpu2   s    z_StorageBase.cpuc             C   s   | j t | ƒjd ƒS )z!Casts this storage to double typez.DoubleStorage)r/   Ú
__module__)r
   r   r   r   Údouble6   s    z_StorageBase.doublec             C   s   | j t | ƒjd ƒS )z Casts this storage to float typez.FloatStorage)r/   r7   )r
   r   r   r   Úfloat:   s    z_StorageBase.floatc             C   s   | j t | ƒjd ƒS )zCasts this storage to half typez.HalfStorage)r/   r7   )r
   r   r   r   Úhalf>   s    z_StorageBase.halfc             C   s   | j t | ƒjd ƒS )zCasts this storage to long typez.LongStorage)r/   r7   )r
   r   r   r   ÚlongB   s    z_StorageBase.longc             C   s   | j t | ƒjd ƒS )zCasts this storage to int typez.IntStorage)r/   r7   )r
   r   r   r   ÚintF   s    z_StorageBase.intc             C   s   | j t | ƒjd ƒS )z Casts this storage to short typez.ShortStorage)r/   r7   )r
   r   r   r   ÚshortJ   s    z_StorageBase.shortc             C   s   | j t | ƒjd ƒS )zCasts this storage to char typez.CharStorage)r/   r7   )r
   r   r   r   ÚcharN   s    z_StorageBase.charc             C   s   | j t | ƒjd ƒS )zCasts this storage to byte typez.ByteStorage)r/   r7   )r
   r   r   r   ÚbyteR   s    z_StorageBase.bytec             C   s   | j t | ƒjd ƒS )zCasts this storage to bool typez.BoolStorage)r/   r7   )r
   r   r   r   ÚboolV   s    z_StorageBase.boolc             C   s   | j t | ƒjd ƒS )z#Casts this storage to bfloat16 typez.BFloat16Storage)r/   r7   )r
   r   r   r   Úbfloat16Z   s    z_StorageBase.bfloat16c             C   sD   | j rtdj| jƒ ƒƒ‚ddl}|jjƒ }t| ƒ| jƒ |d�j| ƒS )z@Copies the storage to pinned memory, if it's not already pinned.z.cannot pin '{0}' only CPU memory can be pinnedr   N)Ú	allocator)	r,   Ú	TypeErrorr   r/   Z
torch.cudar-   Z_host_allocatorr   r0   )r
   r   rB   r   r   r   Ú
pin_memory^   s    
z_StorageBase.pin_memoryc             C   s4   ddl m} | jrn|ƒ dkr(| jƒ  n| jƒ  | 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.multiprocessingrE   r,   Z_share_filename_Z
_share_fd_)r
   rE   r   r   r   Úshare_memory_g   s    	

z_StorageBase.share_memory_c             C   s<   ddl m} | jr| |ƒS |ƒ dkr.| j|ƒS | j|ƒS dS )z>Creates a new storage in shared memory with the same data typer   )rE   rF   N)rG   rE   r,   Z_new_using_filenameZ_new_using_fd)Úclsr   rE   r   r   r   Ú_new_sharedy   s    

z_StorageBase._new_shared)r5   r7   Ú__qualname__r,   Z	is_sparser   r   r   r   r    r'   r)   r   r3   r6   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rD   rH   ÚclassmethodrJ   Ú__classcell__r   r   )r*   r   r      s2   	r   c             C   s   t jtj| ƒƒS )N)r   Úloadr!   r"   )r&   r   r   r   r$   …   s    r$   )
r!   r   Ú_utilsr   r   Úobjectr   r$   r/   r-   r   r   r   r   Ú<module>   s   ~