U
    9%e
                     @   s  U d dl Z d dlZddlmZmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZ d dlZd dlZd dlmZ d dlZd dlZd dlZzd dlZdZW n ek
r   dZY nX e Zi Zeeej f e!d< ed	d
dZ"G dd dZ#dd Z$G dd dej%j&e#Z'dd Z(ee#_)ee#_*ee#_+edddd Z,edddd Z-dd Z.dd Z/da0dd Z1d d! Z2d4d#d$Z3d%d& Z4e5d'd(d)Z6G d*d+ d+Z7ej8e7j)_8ej8e7j*_8ej8e7j+_8G d,d- d-e)Z9G d.d/ d/e7e9d0Z:e5d1d2d3Z;dS )5    N   )_type_cuda_hpu)Storage)castAnyDictOptionalTypeVarTypeUnion)	lru_cacheT_share_memory_mapTz!Union[_StorageBase, TypedStorage])boundc                       s  e Zd ZU eed< dZeed< dZeed< ej	ed< dd Z
edd	d
Zdd Zdd Zdeee edddZedddZedddZedddZdee eedddZdedddZdedddZedd d!Zedd"d#Zedd$d%Zd&d' Zd(d) Zeee eed*d+d,Z eee eed*d-d.Z!eee ed/d0d1Z"eddd2ee ed/d3d4Z#eee ed/d5d6Z$eee ed/d7d8Z%edd9d:Z&d;d< Z'ed=d>d?Z(edd@dAZ)dBdC Z*dDdE Z+dFdG Z,eddHdIZ-eee ed/dJdKZ.dLdM Z/edNdO Z0e1dPdQ Z2e1dRdS Z3eeddTdUZ4eeddVdWZ5dXdY Z6dZd[ Z7d\d] Z8d^d_ Z9d`da Z:dbdc Z;ddde Z< fdfdgZ=dhdi Z>djdk Z?dldm Z@dndo ZAdpdq ZBdrds ZCdtdu ZDdvdw ZEdxdy ZFdzd{ Zd|d} ZGd~d ZHdd ZIdd Zdd ZJdd ZKdd ZLdeMeej	f dddZNdeMeej	f dddZOdd ZPeddddZQdd ZRdd ZS  ZTS )_StorageBase_cdataF	is_sparseis_sparse_csrdevicec                 O   s   d S N selfargskwargsr   r   L/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/storage.py__init__       z_StorageBase.__init__returnc                 C   s   d S r   r   r   r   r   r   __len__   r   z_StorageBase.__len__c                 C   s   d S r   r   r   idxr   r   r   __getitem__   r   z_StorageBase.__getitem__c                 O   s   d S r   r   r   r   r   r   __setitem__    r   z_StorageBase.__setitem__N)sourcenon_blockingr!   c                 C   s   d S r   r   r   r(   r)   r   r   r   copy_!   r   z_StorageBase.copy_c                 C   s   d S r   r   r"   r   r   r   new"   r   z_StorageBase.newc                 C   s   d S r   r   r"   r   r   r   nbytes#   r   z_StorageBase.nbytesc                 C   s   |   S r   )r-   r"   r   r   r   size%   s    z_StorageBase.sizedtyper)   r!   c                 C   s   d S r   r   )r   r0   r)   r   r   r   type(   r   z_StorageBase.typec                 K   s   d S r   r   r   r   r)   r   r   r   r   cuda)   r   z_StorageBase.cudac                 K   s   d S r   r   r2   r   r   r   hpu*   r   z_StorageBase.hpuc                 C   s   d S r   r   r"   r   r   r   element_size+   r   z_StorageBase.element_sizec                 C   s   | j jS r   )r   indexr"   r   r   r   
get_device-   s    z_StorageBase.get_devicec                 C   s   d S r   r   r"   r   r   r   data_ptr0   r   z_StorageBase.data_ptrc                 O   s   d S r   r   r   r   r   r   _share_filename_cpu_3   r   z!_StorageBase._share_filename_cpu_c                 O   s   d S r   r   r   r   r   r   _share_fd_cpu_4   r   z_StorageBase._share_fd_cpu_)clsr.   r!   c                 C   s   d S r   r   r;   r.   r   r   r   _new_using_filename_cpu5   s    z$_StorageBase._new_using_filename_cpuc                 C   s   d S r   r   r<   r   r   r   _new_using_fd_cpu7   s    z_StorageBase._new_using_fd_cpu)r;   r!   c                 O   s   d S r   r   r;   r   r   r   r   r   from_buffer9   s    z_StorageBase.from_buffer)r   r0   c                C   s   d S r   r   )r;   managerobjr.   r   r0   r   r   r   _new_shared_filename_cpu;   s    z%_StorageBase._new_shared_filename_cpuc                 O   s   d S r   r   r?   r   r   r   _release_ipc_counter_cuda=   s    z&_StorageBase._release_ipc_counter_cudac                 O   s   d S r   r   r?   r   r   r   _new_with_weak_ptr?   s    z_StorageBase._new_with_weak_ptrc                 C   s   d S r   r   r"   r   r   r   _shared_decrefA   r   z_StorageBase._shared_decrefc                 O   s   d S r   r   r   r   r   r   _write_fileB   r   z_StorageBase._write_file)r.   c                 C   s   d S r   r   r   r.   r   r   r   resize_C   r   z_StorageBase.resize_c                 O   s   d S r   r   r   r   r   r   	_weak_refD   r   z_StorageBase._weak_refc                 O   s   d S r   r   r   r   r   r   _set_from_fileE   r   z_StorageBase._set_from_filec                 O   s   d S r   r   r   r   r   r   
_set_cdataF   r   z_StorageBase._set_cdatac                 O   s   d S r   r   r   r   r   r   _share_cuda_G   r   z_StorageBase._share_cuda_c                 C   s   d S r   r   r"   r   r   r   	is_sharedH   r   z_StorageBase.is_sharedc                 O   s   d S r   r   r?   r   r   r   _new_shared_cudaI   s    z_StorageBase._new_shared_cudac                 O   s   d S r   r   r   r   r   r   _shared_increfK   r   z_StorageBase._shared_increfc                 O   s   d S r   r   r?   r   r   r   _free_weak_refL   s    z_StorageBase._free_weak_refc                 C   s   d S r   r   r"   r   r   r   is_cudaN   s    z_StorageBase.is_cudac                 C   s   d S r   r   r"   r   r   r   is_hpuP   s    z_StorageBase.is_hpuc                 C   s   d S r   r   )r;   filenamesharedr-   r   r   r   	from_fileR   s    z_StorageBase.from_filec                 O   s   d S r   r   r?   r   r   r   _expiredT   s    z_StorageBase._expiredc                 O   s   d S r   r   r   r   r   r   	_byteswapV   r   z_StorageBase._byteswapc                    sl   dt   d j dt  d} jjdkr8d| S dd fd	d
t  D  }|d | S d S )N[z(device=
) of size ]meta...
 
 c                 3   s   | ]}t  | V  qd S r   str.0ir"   r   r   	<genexpr>_   s     z'_StorageBase.__str__.<locals>.<genexpr>
)torchtypenamer   lenr1   joinranger.   r   Zinfo_strZdata_strr   r"   r   __str__X   s    "$z_StorageBase.__str__c                 C   s   t | S r   r`   r"   r   r   r   __repr__b   s    z_StorageBase.__repr__c                    s   t  fddt  D S )Nc                 3   s   | ]} | V  qd S r   r   rb   r"   r   r   re   f   s     z(_StorageBase.__iter__.<locals>.<genexpr>)iterrk   r.   r"   r   r"   r   __iter__e   s    z_StorageBase.__iter__c                 C   s   |   S r   )cloner"   r   r   r   __copy__h   s    z_StorageBase.__copy__c                 C   s6   | di }| j|kr || j S |  }||| j< |S )Nrg   )
setdefaultr   rq   )r   memoZnew_storager   r   r   __deepcopy__k   s    


z_StorageBase.__deepcopy__c                 C   s&   t  }tj| |dd t| ffS NF)Z_use_new_zipfile_serializationioBytesIOrg   save_load_from_bytesgetvaluer   br   r   r   
__reduce__s   s    z_StorageBase.__reduce__c                    s   t   |   S r   )super
__sizeof__r.   r"   	__class__r   r   r   x   s    z_StorageBase.__sizeof__c                 C   s   t | |  | jd| S )Returns a copy of this storager   )r1   r-   r   r+   r"   r   r   r   rq   {   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*   | j jdkr"t|  | dS | S dS )AReturns a CPU copy of this storage if it's not already on the CPUcpuFNr   r1   rg   UntypedStorager.   r+   r"   r   r   r   r      s    z_StorageBase.cpuc                 C   s.   | j jdkr&tj|  dd| dS | S dS )zAReturns a MPS copy of this storage if it's not already on the MPSmpsr   FNr   r"   r   r   r   r      s    z_StorageBase.mpsc                 C   sd   t |tjstdt| tjg tj| jdt	t
| | }| |  kr`| }|S Nz*Argument 'dtype' must be torch.dtype, not r0   r   )
isinstancerg   r0   	TypeErrorr1   tensoruint8r   set_r   r   to_typed_storager8   rq   r   r0   storager   r   r   _to   s    *z_StorageBase._toc                 C   s   |  tjS z!Casts this storage to double type)r   rg   doubler"   r   r   r   r      s    z_StorageBase.doublec                 C   s   |  tjS z Casts this storage to float type)r   rg   floatr"   r   r   r   r      s    z_StorageBase.floatc                 C   s   |  tjS zCasts this storage to half type)r   rg   halfr"   r   r   r   r      s    z_StorageBase.halfc                 C   s   |  tjS zCasts this storage to long type)r   rg   longr"   r   r   r   r      s    z_StorageBase.longc                 C   s   |  tjS zCasts this storage to int type)r   rg   intr"   r   r   r   r      s    z_StorageBase.intc                 C   s   |  tjS z Casts this storage to short type)r   rg   shortr"   r   r   r   r      s    z_StorageBase.shortc                 C   s   |  tjS zCasts this storage to char type)r   rg   int8r"   r   r   r   char   s    z_StorageBase.charc                 C   s   |  tjS zCasts this storage to byte type)r   rg   r   r"   r   r   r   byte   s    z_StorageBase.bytec                 C   s   |  tjS zCasts this storage to bool type)r   rg   boolr"   r   r   r   r      s    z_StorageBase.boolc                 C   s   |  tjS z#Casts this storage to bfloat16 type)r   rg   bfloat16r"   r   r   r   r      s    z_StorageBase.bfloat16c                 C   s   |  tjS z)Casts this storage to complex double type)r   rg   cdoubler"   r   r   r   complex_double   s    z_StorageBase.complex_doublec                 C   s   |  tjS z(Casts this storage to complex float type)r   rg   cfloatr"   r   r   r   complex_float   s    z_StorageBase.complex_floatr3   r   c                 C   s&   t jg t j| jdtt| |S )zDetermine whether the CPU storage is already pinned on device.

        Args:
            device (str or torch.device): The device to pin memory on. Default: ``'cuda'``.

        Returns:
            A boolean variable.
        r   )rg   r   r   r   r   r   r   	is_pinnedr   r   r   r   r   r      s
    	z_StorageBase.is_pinnedc                 C   sN   | j jdkr td|   dtjg tj| j dtt| 	|}|
 S )zCopies the CPU storage to pinned memory, if it's not already pinned.

        Args:
            device (str or torch.device): The device to pin memory on. Default: ``'cuda'``.

        Returns:
            A pinned CPU storage.
        r   zcannot pin 'z' only CPU memory can be pinnedr   )r   r1   r   rg   r   r   r   r   r   
pin_memoryuntyped_storage)r   r   Zpinned_tensorr   r   r   r      s    	z_StorageBase.pin_memoryc                 C   sD   ddl m} | jjdtj fkr$n| dkr8|   n|   | S )ar  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.

        Note that to mitigate issues like https://github.com/pytorch/pytorch/issues/95606
        it is thread safe to call this function from multiple threads on the same object.
        It is NOT thread safe though to call any other function on self without proper
        synchronization. Please see :doc:`/notes/multiprocessing` for more details.

        Returns: self
        r   get_sharing_strategyr3   file_system)	torch.multiprocessingr   r   r1   rg   _C_get_privateuse1_backend_namer9   r:   )r   r   r   r   r   share_memory_   s    

z_StorageBase.share_memory_r   c                C   sX   ddl m} t|}|jdtj fkr6| ||dS | dkrJ| |S | |S dS )>Creates a new storage in shared memory with the same data typer   r   r3   r   r   N)	r   r   rg   r   r1   r   r   r=   r>   )r;   r.   r   r   r   r   r   _new_shared   s    


z_StorageBase._new_sharedc                 C   s   | S r   r   r"   r   r   r   untyped  s    z_StorageBase.untypedc                 C   s2   t j|}|jr$tt|d d}| | dS )zSwaps bytes in underlying data   r   N)rg   _utils_element_sizeZ
is_complexmaxr   rX   )r   r0   Z	elem_sizer   r   r   byteswap  s    z_StorageBase.byteswap)N)NF)NF)NF)r3   )r3   )U__name__
__module____qualname__r   __annotations__r   r   r   rg   r   r   r   r#   r&   r'   r   	_Optionalr+   r,   r-   r.   ra   r1   r3   r4   r5   r7   r8   r9   r:   classmethodr   r=   r>   r@   rC   rD   rE   rF   rG   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   propertyrR   rS   rV   rW   rX   rm   rn   rp   rr   ru   r   r   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r      s   





r   c                    s   t   fdd}|S )Nc                    s   d }d }t : | j}|tkr&t| }nt t|< t|   |}W 5 Q R X |d k	rd| W 5 Q R X z | f||W S |d k	r| j|kstt  t|   t|= W 5 Q R X X d S r   )_share_memory_lockr   r   	threadingRLockacquireAssertionErrorrelease)r   r   r   Zto_freeZto_waitkeyfnr   r   wrapper  s&    

z-_share_memory_lock_protected.<locals>.wrapper)	functoolswraps)r   r   r   r   r   _share_memory_lock_protected  s    r   c                       sd   e Zd Z fddZedd Zedd Ze fddZe fd	d
Z	e fddZ
  ZS )r   c                    s"   | j jdkrtdt j||S )Nr\   $Not available for 'meta' device type)r   r1   NotImplementedErrorr   r&   r   r   r   r   r&   7  s    zUntypedStorage.__getitem__c                 C   s   | j jdkS Nr3   r   r1   r"   r   r   r   rR   <  s    zUntypedStorage.is_cudac                 C   s   | j jdkS Nr4   r   r"   r   r   r   rS   @  s    zUntypedStorage.is_hpuc                    s   t  j||S r   )r   r   r   r   r   r   r   D  s    zUntypedStorage.share_memory_c                    s   t  j||S r   )r   r:   r   r   r   r   r:   H  s    zUntypedStorage._share_fd_cpu_c                    s   t  j||S r   )r   r9   r   r   r   r   r9   L  s    z#UntypedStorage._share_filename_cpu_)r   r   r   r&   r   rR   rS   r   r   r:   r9   r   r   r   r   r   r   6  s   

r   c                 C   s   t t| S r   )rg   loadrx   ry   )r~   r   r   r   r{   P  s    r{   )maxsizec                "   C   sj   t jdt jdt jdt jdt jdt jdt jdt jdt j	d	t j
d
t jdt jdt jdt jdt jdt jdt jdiS )NZDoubleStorageZFloatStorageZHalfStorageZLongStorageZ
IntStorageZShortStorageZCharStorageZByteStorageZBoolStorageZBFloat16StorageZComplexDoubleStorageZComplexFloatStorageZQInt8StorageZQInt32StorageZQUInt8StorageZQUInt4x2StorageZQUInt2x4Storage)rg   r   r   r   r   r   Zint16r   r   r   r   r   r   qint8qint32quint8quint4x2quint2x4r   r   r   r   _dtype_to_storage_type_mapY  sF                     r   c                  C   s   dd t   D } | S )Nc                 S   s   i | ]\}}||qS r   r   )rc   r   valr   r   r   
<dictcomp>v  s     z._storage_type_to_dtype_map.<locals>.<dictcomp>)r   items)Z	dtype_mapr   r   r   _storage_type_to_dtype_mapt  s    r   c              
   C   sx   |t jt jt jt jt jfkr^t jt jt jt jt jt jt jt jt jt ji}t j	| || |d}nt j	| ||d}|
 jS )Nr   )rg   r   r   r   r   r   r   int32r   r   r   _untyped_storage)sequencer0   r   interpret_dtypes
tmp_tensorr   r   r   _get_storage_from_sequencez  s.         r   c                 C   s"   t rt| ttjfS t| tS d S r   )	HAS_NUMPYr   r   npinteger)xr   r   r   _isint  s    r   Fc                   C   s   t S r   )"_always_warn_typed_storage_removalr   r   r   r   &_get_always_warn_typed_storage_removal  s    r   c                 C   s   t | tst| ad S r   )r   r   r   r   )Zalways_warnr   r   r   &_set_always_warn_typed_storage_removal  s    r   r   c                 C   s:   dd }t  s| r6d}tj|t| d d dtjd< d S )Nc                   S   s   t tdsdS tjd  S d S )N
has_warnedT)hasattr_warn_typed_storage_removal__dict__r   r   r   r   is_first_time  s    
z2_warn_typed_storage_removal.<locals>.is_first_timea  TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()r   )
stacklevelTr   )r   warningswarnUserWarningr   r   )r  r   messager   r   r   r     s    r   c                   C   s   dt jd< d S )NFr   )r   r   r   r   r   r   !_reset_warn_typed_storage_removal  s    r  modulec                 C   s2   |  dd dtj fkr*|  dd S dS d S )N.r3   r   )splitrg   r   r   r  r   r   r   _get_device_from_module  s    r  c                       sn  e Zd ZU dZejed< edd Zdd Z	ddddd fd	d

Z
dddddddZedd Zedd Zdd Zdd Zdd ZdddZdd Zdd Zdd Zd d! Zdeee d"d#d$Zd%d& Zd'd( Zdee eeeef d)d*d+Zded,d-d.Zded,d/d0Z d1d2 Z!d3d4 Z"e#d,d5d6Z$d7d8 Z%d9d: Z&d;d< Z'd=d> Z(d?d@ Z)dAdB Z* fdCdDZ+dEdF Z,dGdH Z-dIdJ Z.deeej/f dLdMdNZ0deeej/f dLdOdPZ1dQdR Z2dSdT Z3ddLdUdVZ4edWdX Z5edYdZ Z/d[d\ Z6d]d^ Z7d_d` Z8dadb Z9dcdd Z:dedf Z;dgdh Z<didj Z=dkdl Z>e?dmdn Z@dodp ZAe?dqdr ZBe?dddsdtduZCdvdw ZDdxdy ZEdzd{ ZFd|d} ZGd~d ZHdd Z#dd ZIdd ZJdd ZKdd Zdd ZLdd ZMdd ZNe?dd ZOe?dd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVe?dd ZWdd ZXdd ZYe?ddLddZZdd Z[dd Z\dd Z]  Z^S )TypedStorageFr0   c                 C   s   | j S r   )r0   r"   r   r   r   _dtype  s    zTypedStorage._dtypec                 C   s    t   | td|  | | S )Nr   )r   _setitemslice_size)r   valuer   r   r   fill_  s    zTypedStorage.fill_N)wrap_storager0   r   	_internalc                   sl  |s
t   | tjjkrtd| tkr2t | S |  d}|d k	rPt|d |d k	rdt|d |d krt|dkrt|d t|dkrt	|d st
|d tjjst|dt|d   t|| jt| jd	d
S t|dk rt|d t
|tjs t|dt|  t| j}|jj|krTt|d| d|jj  t||| jd	dS d S )Nz8Only child classes of _LegacyStorage can be instantiatedz.__new__ received an invalid combination of arguments. Expected one of:
 * no arguments
 * (int size)
 * (Sequence data)
 * (*, UntypedStorage wrap_storage)z.
Keyword argument 'device' cannot be specifiedz-
Keyword argument 'dtype' cannot be specifiedr   
Too many positional argumentsr   
Argument type not recognized: T)r0   r   r  B
No positional arguments should be given when using 'wrap_storage'9
Argument 'wrap_storage' must be UntypedStorage, but got z"
Device of 'wrap_storage' must be z
, but got r  r0   r  )r   rg   r   _LegacyStorageRuntimeErrorr  r   __new__ri   r   r   collectionsabcSequencer   r1   r  r  r   r   r   r0   )r;   r  r0   r   r  r   arg_error_msg
cls_devicer   r   r   r    s    *
zTypedStorage.__new__)r   r0   r  r  c                G   s  |s
t   d}|d k	rt|dkr.t|d |d krBt|d t|tjsdt|dt|  |d k	rxt|d || _t|tjst|dt|  || _	n|d krt
 n|| _t|d krdn|}| jtjtjtjtjtjfkr
|jd	kr
td
t|dkr(tj|d| _	nt|dkrt|d rftjt|d |   |d| _	nDt|d tjjrt|d | j|| _	nt|dt|d   nt|d d S )Na1  TypedStorage.__init__ received an invalid combination of arguments. Expected one of:
 * (*, torch.device device, torch.dtype dtype)
 * (int size, *, torch.device device, torch.dtype dtype)
 * (Sequence data, *, torch.device device, torch.dtype dtype)
 * (*, UntypedStorage wrap_storage, torch.dtype dtype)r   r  z#
Argument 'dtype' must be specifiedz+
Argument 'dtype' must be torch.dtype, not zG
Argument 'device' should not be specified when 'wrap_storage' is givenr  r   r3   /Cannot create CUDA storage with quantized dtyper   r   r  r  )r   ri   r  r   rg   r0   r   r1   r   r   get_default_dtyper   r   r   r   r   r   r   r   r   r  r  r   r   )r   r   r0   r  r  r   r!  r   r   r   r     sv    
 "zTypedStorage.__init__c                 C   s   t   | jjjdkS r   r   r   r   r1   r"   r   r   r   rR   V  s    zTypedStorage.is_cudac                 C   s   t   | jjjdkS r   r%  r"   r   r   r   rS   [  s    zTypedStorage.is_hpuc                 C   s   t   | jS )z2Returns the internal :class:`torch.UntypedStorage`)r   r   r"   r   r   r   r   `  s    zTypedStorage.untypedc                 C   s@   t |tjkstt | tkr.t|| jddS t | |dS d S )NTr  r  )r1   rg   r   r   r  r0   )r   r   r   r   r   _new_wrapped_storagee  s    z!TypedStorage._new_wrapped_storagec                 C   s   t   |  S r   r   r  r"   r   r   r   r#   p  s    zTypedStorage.__len__c                 C   s   |d kr|r|   S dS nt|tkrBtdt|  dt| |r||   ks`||    k rxtd| d|   |dkr|S ||    S n>||   ks||    k rtd| d|   ||    S d S )Nr   can't index a  with zindex z" out of range for storage of size )r  r1   r   r   
IndexErrorr.   )r   r%   Zis_stopr   r   r   _maybe_wrap_indext  s*    zTypedStorage._maybe_wrap_indexc                 C   s   t   | ||S r   )r   r  )r   r%   r  r   r   r   r'     s    zTypedStorage.__setitem__c              
   C   s   t |ttfs*tdt|  dt| t|rFtdt| | jtjtj	tj
tjtjfkrtjtjtj	tjtj
tjtjtjtjtji}|| j }tjg || jjd}|t| j|dd ntjg | j| jjd| }|||< d S )Nr)  r*  z cannot set item with value type r   Tr  )r   r   r  r  r1   rg   Z
is_storager0   r   r   r   r   r   r   r   r   r   r   r   r   r  )r   r%   r  r   Z	tmp_dtyper   r   r   r   r    s2    
     

zTypedStorage._setitemc                 C   s   t   | |S r   )r   _getitemr$   r   r   r   r&     s    zTypedStorage.__getitem__c              
   C   s   | j jjdkrtdt|tr*tdn&t|tsPtdt|  dt| | jt	j
t	jt	jt	jt	jfkrt	j
t	jt	jt	jt	jt	jt	jt	jt	jt	ji}t| j || j dd|S | |}t	jg | j| j jd| }||  S )	Nr\   r   z7slices are only supported in UntypedStorage.__getitem__r)  r*  Tr  r   )r   r   r1   r   r   r  r  r   r0   rg   r   r   r   r   r   r   r   r   r  r-  r,  r   r   item)r   r%   r   Zidx_wrappedr   r   r   r   r-    s8    


     
zTypedStorage._getitem)r(   r)   c                 C   s4   t   t|tr"| j|j| n| j|| | S r   )r   r   r  r   r+   r*   r   r   r   r+     s
    
zTypedStorage.copy_c                 C   s   t   |  S r   )r   _nbytesr"   r   r   r   r-     s    zTypedStorage.nbytesc                 C   s
   | j  S r   )r   r-   r"   r   r   r   r/    s    zTypedStorage._nbytesr/   c                 C   sV   t   |d krD|  }|d k	r.|jd |j S d| jt| jgS | j||S d S )Nr	  )r   _get_legacy_storage_classr   r   rj   r1   r   )r   r0   r)   Zlegacy_classr   r   r   r1     s    zTypedStorage.typer    c                 K   sH   t   | jtjtjtjtjtjfkr,td| j	j
||f|}| |S )Nr#  )r   r0   rg   r   r   r   r   r   r  r   r3   r'  )r   r   r)   r   Zcuda_storager   r   r   r3     s
    zTypedStorage.cudac                 K   sH   t   | jtjtjtjtjtjfkr,td| j	j
||f|}| |S )Nz.Cannot create HPU storage with quantized dtype)r   r0   rg   r   r   r   r   r   r  r   r4   r'  )r   r   r)   r   Zhpu_storager   r   r   r4     s
    zTypedStorage.hpuc                 C   s   t   |  S r   )r   r   r"   r   r   r   r5     s    zTypedStorage.element_sizec                 C   s   t j| jS r   )rg   r   r   r0   r"   r   r   r   r     s    zTypedStorage._element_sizec                 C   s   t   | j S r   )r   r   r7   r"   r   r   r   r7     s    zTypedStorage.get_devicec              	      sz   t   dt  d j d j dt  d	} jjdkrFd| S dd	 fd
dt 	 D  }|d | S d S )NrY   z(dtype=z	, device=rZ   r[   r\   r]   r^   r_   c                 3   s   | ]}t  | V  qd S r   r`   rb   r"   r   r   re   
  s     z'TypedStorage.__str__.<locals>.<genexpr>rf   )
r   rg   rh   r0   r   ri   r1   rj   rk   r.   rl   r   r"   r   rm     s    *$zTypedStorage.__str__c                 C   s   t   t| S r   )r   ra   r"   r   r   r   rn     s    zTypedStorage.__repr__c                    s$   t   t fddt  D S )Nc                 3   s   | ]} | V  qd S r   r   rb   r"   r   r   re     s     z(TypedStorage.__iter__.<locals>.<genexpr>)r   ro   rk   r.   r"   r   r"   r   rp     s    zTypedStorage.__iter__c                 C   s   t   | t| jS r   )r   r'  copyr   r"   r   r   r   rr     s    zTypedStorage.__copy__c                 C   s   t   | |S r   )r   	_deepcopyr   rt   r   r   r   ru     s    zTypedStorage.__deepcopy__c                 C   s   |  t| j|S r   )r'  r1  deepcopyr   r3  r   r   r   r2    s    zTypedStorage._deepcopyc                    s   t   t  |   S r   )r   r   r   r-   r"   r   r   r   r   !  s    zTypedStorage.__sizeof__c                 C   s   t   | | j S )r   )r   r'  r   rq   r"   r   r   r   rq   %  s    zTypedStorage.clonec                 C   s   t   t| S r   )r   r   r"   r   r   r   r   *  s    zTypedStorage.tolistc                 C   s   t   | | j S )r   )r   r'  r   r   r"   r   r   r   r   /  s    zTypedStorage.cpur3   r   c                 C   s   t   | j|S )zDetermine whether the CPU TypedStorage is already pinned on device.

        Args:
            device (str or torch.device): The device to pin memory on. Default: ``'cuda'``

        Returns:
            A boolean variable.
        )r   r   r   r   r   r   r   r   4  s    	zTypedStorage.is_pinnedc                 C   s   t   | | jj|dS )zCopies the CPU TypedStorage to pinned memory, if it's not already pinned.

        Args:
            device (str or torch.device): The device to pin memory on. Default: ``'cuda'``.

        Returns:
            A pinned CPU storage.
        r   )r   r'  r   r   r   r   r   r   r   @  s    	zTypedStorage.pin_memoryc                 C   s   t   |  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   _share_memory_r"   r   r   r   r   L  s    	zTypedStorage.share_memory_c                 C   s   | j   | S r   )r   r   r"   r   r   r   r5  Y  s    
zTypedStorage._share_memory_c                C   s>   |dkrd}t |}t jj||   |d}t|| jddS )r   Nr   r   Tr  )rg   r   r   r   r   r  r0   )r   r.   r   r   r   r   r   r   ]  s    
zTypedStorage._new_sharedc                 C   s   | j jS r   )r   r   r"   r   r   r   r   h  s    zTypedStorage._cdatac                 C   s   t   | jjS r   )r   r   r   r"   r   r   r   r   l  s    zTypedStorage.devicec                 C   s   t   |  S r   r(  r"   r   r   r   r.   q  s    zTypedStorage.sizec                 C   s   | j  |   S r   )r   r-   r   r"   r   r   r   r  v  s    zTypedStorage._sizec                 C   s   t   |  S r   )r   _pickle_storage_typer"   r   r   r   pickle_storage_type{  s    z TypedStorage.pickle_storage_typec              
   C   sJ   zt  | j W S  tk
rD } ztd| j d|W 5 d }~X Y nX d S )Nzdtype z is not recognized)r   r0   KeyError)r   er   r   r   r6    s    z!TypedStorage._pickle_storage_typec                 C   s&   t  }tj| |dd t| ffS rv   rw   r}   r   r   r   r     s    zTypedStorage.__reduce__c                 C   s   t   |  S r   )r   	_data_ptrr"   r   r   r   r8     s    zTypedStorage.data_ptrc                 C   s
   | j  S r   )r   r8   r"   r   r   r   r:    s    zTypedStorage._data_ptrc                 C   s   t   | | d S r   )r   _resize_rH   r   r   r   rI     s    zTypedStorage.resize_c                 C   s   | j ||    d S r   )r   rI   r   rH   r   r   r   r;    s    zTypedStorage._resize_c                 O   s   t j||S r   )r   rQ   r?   r   r   r   rQ     s    zTypedStorage._free_weak_refc                 O   s   | j j||S r   )r   rJ   r   r   r   r   rJ     s    zTypedStorage._weak_refc                 O   s   t   | j||S r   )r   _from_bufferr?   r   r   r   r@     s    zTypedStorage.from_bufferr   c                O   s   | t krd|d krt n|}t|d kr,dn|}|jdkrLtd|j tjj|d|i|}nH|d k	sxt|dkrtd|d k	rtd| j	}tjj|d|i|}t ||ddS )	Nr   z3TypedStorage.from_buffer: Not available for device r0      zefrom_buffer: 'dtype' can only be specified in UntypedStorage.from_buffer and TypedStorage.from_bufferzffrom_buffer: 'device' can only be specified in UntypedStorage.from_buffer and TypedStorage.from_bufferTr  )
r  rg   r$  r   r1   r  r   r@   ri   r  )r;   r0   r   r   r   r   r   r   r   r<    s*    
zTypedStorage._from_bufferc                 C   s^   t |tjstdt| tjg | j| jd| |	 }|
 | 
 krZ| }|S r   )r   rg   r0   r   r1   r   r   r   r   r   r8   rq   r   r   r   r   r     s    $zTypedStorage._toc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.doublec                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.floatc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.halfc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.longc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.intc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.shortc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.charc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.bytec                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.boolc                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.bfloat16c                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.complex_doublec                 C   s   t   | tjS r   )r   r   rg   r   r"   r   r   r   r     s    zTypedStorage.complex_floatc                 C   s@   t   | tkrtdt|||tj| j }| |d}|S )a  
        from_file(filename, shared=False, size=0) -> Storage

        If `shared` is `True`, then memory is shared between all processes.
        All changes are written to the file. If `shared` is `False`, then the changes on
        the storage do not affect the file.

        `size` is the number of elements in the storage. If `shared` is `False`,
        then the file must contain at least `size * sizeof(Type)` bytes
        (`Type` is the type of storage). If `shared` is `True` the file will be
        created if needed.

        Args:
            filename (str): file name to map
            shared (bool): whether to share memory
            size (int): number of elements in the storage
        z/from_file can only be called on derived classesr&  )	r   r  r  r   rV   rg   r   r   r0   )r;   rT   rU   r.   r   r   r   r   r   rV     s    
zTypedStorage.from_filec                 O   s   t j||S r   )r   rW   r?   r   r   r   rW   #  s    zTypedStorage._expiredc                 O   s   | j j||S r   )r   rG   r   r   r   r   rG   '  s    zTypedStorage._write_filec                 O   s   | j j||S r   )r   rK   r   r   r   r   rK   *  s    zTypedStorage._set_from_filec                 O   s   | j j||S r   )r   rL   r   r   r   r   rL   -  s    zTypedStorage._set_cdatac                 O   s   | j j||S r   )r   rM   r   r   r   r   rM   0  s    zTypedStorage._share_cuda_c                 C   s   t   |  S r   )r   
_is_sharedr"   r   r   r   rN   3  s    zTypedStorage.is_sharedc                 C   s
   | j  S r   )r   rN   r"   r   r   r   r>  8  s    zTypedStorage._is_sharedc                 O   s   t jj||S r   )rg   r   rO   r?   r   r   r   rO   ;  s    zTypedStorage._new_shared_cudac                 O   s&   | j j||\}}}||||   fS r   )r   r9   r   )r   r   r   Zmanager_handleZstorage_handler.   r   r   r   r9   ?  s    z!TypedStorage._share_filename_cpu_c                 C   s   | j   | S r   )r   rF   r"   r   r   r   rF   C  s    
zTypedStorage._shared_decrefc                O   s   t jj||S r   rg   r   rD   )r;   r   r   r   r   r   r   _release_ipc_counterG  s    z!TypedStorage._release_ipc_counterc                 O   s   | j j||S r   )r   rP   r   r   r   r   rP   K  s    zTypedStorage._shared_increfc                 O   s"   | j j||\}}|||   fS r   )r   r:   r   )r   r   r   fdr.   r   r   r   r:   N  s    zTypedStorage._share_fd_cpu_c                 C   s~   | j t krd S t | j  }| jjddtj fkr8d S | jjdkrHtntt| jj}zt||W S  tk
rx   Y d S X d S )Nr   r3   )	r0   r   r   r1   rg   r   r   getattrAttributeError)r   Zstorage_namer  r   r   r   r0  R  s    z&TypedStorage._get_legacy_storage_class)F)N)NF)NF)NF)r3   )r3   )_r   r   r   r   rg   r0   r   r   r  r  r  r   rR   rS   r   r'  r#   r,  r'   r  r&   r-  r   r   r   r+   r-   r/  ra   r   r1   r3   r4   r5   r   r   r7   rm   rn   rp   rr   ru   r2  r   rq   r   r   r   r   r   r   r5  r   r   r.   r  r7  r6  r   r8   r:  rI   r;  r   rQ   rJ   r@   r<  r   r   r   r   r   r   r   r   r   r   r   rV   rW   rG   rK   rL   rM   rN   r>  rO   r9   rF   r@  rP   r:   r0  r   r   r   r   r   r    s   


HD


 






r  c                   @   s    e Zd ZU ejed< dd ZdS )_LegacyStorageMetar0   c                 C   s2   t |tkr.t| j}||jj ko,| j|jkS dS )NF)r1   r  r  r   r   r0   )r;   instancer"  r   r   r   __instancecheck__i  s    
z$_LegacyStorageMeta.__instancecheck__N)r   r   r   rg   r0   r   rF  r   r   r   r   rD  f  s   

rD  c                   @   s0   e Zd Zedd Zedd Zedd ZdS )r  c                 C   s    t j||    }| |dS )r   r&  )rg   r   r   r   )r;   r.   r   r   r   r   r   p  s    z_LegacyStorage._new_sharedc                 O   s   t jj||S r   r?  r?   r   r   r   r@  v  s    z#_LegacyStorage._release_ipc_counterc                 C   s(   |t j| j }| t j|||dS )Nr&  )rg   r   r   r0   r   rC   )r;   rA   rB   r.   Z
bytes_sizer   r   r   _new_shared_filenamez  s    z#_LegacyStorage._new_shared_filenameN)r   r   r   r   r   r@  rG  r   r   r   r   r  o  s   

r  )	metaclass)r7  c              
   C   sF   zt  |  W S  tk
r@ } ztd|  d|W 5 d }~X Y nX d S )Nzpickle storage type "z" is not recognized)r   r8  )r7  r9  r   r   r   #_get_dtype_from_pickle_storage_type  s    
rI  )r   )<rx   rg   r   r   r   r   Ztorch.typesr   typingr   r   r	   Z_Dictr
   r   r   r   r   r1  r  r   r   r  r   numpyr   r   ModuleNotFoundErrorLockr   r   r   r   r   r   r   r   r   ZStorageBaser   r{   r1   r3   r4   r   r   r   r   r   r   r   r   r  ra   r  r  __doc__rD  r  rI  r   r   r   r   <module>   sf    $
 ~"


     +


	