U
    zh\                     @   s  U d dl Z d dlZddl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dddd Z)edddd Z*edddd Z+dd Z,dd Z-da.d d! Z/d"d# Z0d6d%d&Z1d'd( Z2e3d)d*d+Z4G d,d- d-Z5ej6e5j(_6e"j7j6e5j7_6e"j8j6e5j8_6ej6e5j9_6G d.d/ d/e(Z:G d0d1 d1e5e:d2Z;e3d3d4d5Z<dS )7    N   )_type_to)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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/d0Z"ee e ed1d2d3Z#eddd4e e ed1d5d6Z$ee e ed1d7d8Z%ee e ed1d9d:Z&edd;d<Z'd=d> Z(ed?d@dAZ)eddBdCZ*dDdE Z+dFdG Z,dHdI Z-eddJdKZ.ee e ed1dLdMZ/dNdO Z0edPdQ Z1e2dRdS Z3e2dTdU Z4eeddVdWZ5eeddXdYZ6dZd[ Z7ee dd\d]Z8d^d_ Z9d`da Z:dbdc Z;ddde Z<dfdg Z=dhdi Z> fdjdkZ?dldm Z@dndo ZAdpdq ZBdrds ZCdtdu ZDddvej	eedwdxdyZEdzd{ ZFd|d} ZGd~d ZHdd ZIdd Zdd ZJdd ZKdd ZLdd Zdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdeTeej	f dddZUdeTeej	f dddZVdd ZWeddddZXdd ZYdd ZZ  Z[S )_StorageBase_cdataF	is_sparseis_sparse_csrdevicec                 O   s   d S N selfargskwargsr   r   ?/var/www/html/venv/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   r/   r(   r   r   r   type)   r   z_StorageBase.typec                 C   s(   |rt d|nt d}| j||dS )a  Returns a copy of this object in CUDA memory.

        If this object is already in CUDA memory and on the correct device, then
        no copy is performed and the original object is returned.

        Args:
            device (int): The destination GPU id. Defaults to the current device.
            non_blocking (bool): If ``True`` and the source is in pinned memory,
                the copy will be asynchronous with respect to the host. Otherwise,
                the argument has no effect.
        cudar   r(   torchr   tor   r   r(   Zdevice2r   r   r   r1   +   s    z_StorageBase.cudac                 C   s(   |rt d|nt d}| j||dS )a  Returns a copy of this object in HPU memory.

        If this object is already in HPU memory and on the correct device, then
        no copy is performed and the original object is returned.

        Args:
            device (int): The destination HPU id. Defaults to the current device.
            non_blocking (bool): If ``True`` and the source is in pinned memory,
                the copy will be asynchronous with respect to the host. Otherwise,
                the argument has no effect.
        hpur2   r3   r6   r   r   r   r7   :   s    z_StorageBase.hpuc                 C   s   d S r   r   r!   r   r   r   element_sizeI   r   z_StorageBase.element_sizec                 C   s   | j jS r   )r   indexr!   r   r   r   
get_deviceK   s    z_StorageBase.get_devicec                 C   s   d S r   r   r!   r   r   r   data_ptrN   r   z_StorageBase.data_ptrc                 C   s   d S r   r   r!   r   r   r   	resizableP   r   z_StorageBase.resizablec                 O   s   d S r   r   r   r   r   r   _share_filename_cpu_S   r   z!_StorageBase._share_filename_cpu_c                 O   s   d S r   r   r   r   r   r   _share_fd_cpu_T   r   z_StorageBase._share_fd_cpu_)clsr-   r    c                 C   s   d S r   r   r?   r-   r   r   r   _new_using_filename_cpuU   s    z$_StorageBase._new_using_filename_cpuc                 C   s   d S r   r   r@   r   r   r   _new_using_fd_cpuW   s    z_StorageBase._new_using_fd_cpu)r?   r    c                 O   s   d S r   r   r?   r   r   r   r   r   from_bufferY   s    z_StorageBase.from_buffer)r   r/   c                C   s   d S r   r   )r?   managerobjr-   r   r/   r   r   r   _new_shared_filename_cpu[   s    z%_StorageBase._new_shared_filename_cpuc                 O   s   d S r   r   rC   r   r   r   _release_ipc_counter_cuda]   s    z&_StorageBase._release_ipc_counter_cudac                 O   s   d S r   r   rC   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   rC   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   rC   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   rC   r   r   r   _expiredt   s    z_StorageBase._expiredc                 O   s   d S r   r   r   r   r   r   	_byteswapv   r   z_StorageBase._byteswapc                 O   s   d S r   r   r   r   r   r   _get_filenamew   r   z_StorageBase._get_filenamec                    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>
)r4   typenamer   lenr0   joinranger-   r   Zinfo_strZdata_strr   r!   r   __str__y   s    "$z_StorageBase.__str__c                 C   s   t | S r   re   r!   r   r   r   __repr__   s    z_StorageBase.__repr__c                    s   t  fddt  D S )Nc                 3   s   | ]} | V  qd S r   r   rg   r!   r   r   rj      s     z(_StorageBase.__iter__.<locals>.<genexpr>)iterro   r-   r!   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|kr || j S |  }||| j< |S )Nr4   )
setdefaultr   ru   )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BytesIOr4   save_load_from_bytesgetvaluer   br   r   r   
__reduce__   s    z_StorageBase.__reduce__c                    s   t   |   S r   )super
__sizeof__r-   r!   	__class__r   r   r      s    z_StorageBase.__sizeof__c                 C   s   t | |  | jd| S )Return a copy of this storage.r   )r0   r,   r   r*   r!   r   r   r   ru      s    z_StorageBase.clonec                 C   s   t | S z6Return 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 )AReturn a CPU copy of this storage if it's not already on the CPU.cpuFNr   r0   r4   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 )zAReturn 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 r/   r   )
isinstancer4   r/   	TypeErrorr0   tensoruint8r   set_r   r   r5   _typed_storager;   ru   r   r/   storager   r   r   r      s    *z_StorageBase._tor(   r   r(   r    c                C   s   t | ||S r   )r   )r   r   r(   r   r   r   r5      s    z_StorageBase.toc                 C   s   |  tjS z"Casts this storage to double type.)r   r4   doubler!   r   r   r   r      s    z_StorageBase.doublec                 C   s   |  tjS z!Casts this storage to float type.)r   r4   floatr!   r   r   r   r      s    z_StorageBase.floatc                 C   s   |  tjS z Casts this storage to half type.)r   r4   halfr!   r   r   r   r      s    z_StorageBase.halfc                 C   s   |  tjS z Casts this storage to long type.)r   r4   longr!   r   r   r   r      s    z_StorageBase.longc                 C   s   |  tjS zCasts this storage to int type.)r   r4   intr!   r   r   r   r      s    z_StorageBase.intc                 C   s   |  tjS z!Casts this storage to short type.)r   r4   shortr!   r   r   r   r      s    z_StorageBase.shortc                 C   s   |  tjS z Casts this storage to char type.)r   r4   int8r!   r   r   r   char   s    z_StorageBase.charc                 C   s   |  tjS z Casts this storage to byte type.)r   r4   r   r!   r   r   r   byte   s    z_StorageBase.bytec                 C   s   |  tjS z Casts this storage to bool type.)r   r4   boolr!   r   r   r   r      s    z_StorageBase.boolc                 C   s   |  tjS z$Casts this storage to bfloat16 type.)r   r4   bfloat16r!   r   r   r   r      s    z_StorageBase.bfloat16c                 C   s   |  tjS z*Casts this storage to complex double type.)r   r4   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   r4   cfloatr!   r   r   r   complex_float   s    z_StorageBase.complex_floatc                 C   s   |  tjS z&Casts this storage to float8_e5m2 type)r   r4   float8_e5m2r!   r   r   r   r      s    z_StorageBase.float8_e5m2c                 C   s   |  tjS z(Casts this storage to float8_e4m3fn type)r   r4   float8_e4m3fnr!   r   r   r   r      s    z_StorageBase.float8_e4m3fnc                 C   s   |  tjS z*Casts this storage to float8_e5m2fnuz type)r   r4   float8_e5m2fnuzr!   r   r   r   r      s    z_StorageBase.float8_e5m2fnuzc                 C   s   |  tjS z*Casts this storage to float8_e4m3fnuz type)r   r4   float8_e4m3fnuzr!   r   r   r   r      s    z_StorageBase.float8_e4m3fnuzr1   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   )r4   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 )zCopy 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   r0   r   r4   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 ).See :meth:`torch.UntypedStorage.share_memory_`r   get_sharing_strategyr1   file_system)	torch.multiprocessingr   r   r0   r4   _C_get_privateuse1_backend_namer=   r>   )r   r   r   r   r   share_memory_  s    

z_StorageBase.share_memory_r   c                C   sZ   ddl m} t|}|jdtj dfkr8| ||dS | dkrL| |S | |S dS )>Create a new storage in shared memory with the same data type.r   r   r1   r7   r   r   N)	r   r   r4   r   r0   r   r   rA   rB   )r?   r-   r   r   r   r   r   _new_shared$  s    


z_StorageBase._new_sharedc                 C   s   | S r   r   r!   r   r   r   untyped0  s    z_StorageBase.untypedc                 C   s2   t j|}|jr$tt|d d}| | dS )zSwap bytes in underlying data.   r   N)r4   _utils_element_sizeZ
is_complexmaxr   r\   )r   r/   Z	elem_sizer   r   r   byteswap3  s    z_StorageBase.byteswap)N)NF)NF)NF)r1   )r1   )\__name__
__module____qualname__r   __annotations__r   r   r   r4   r   r   r   r"   r%   r&   r   	_Optionalr*   r+   r,   r-   rf   r0   r1   r7   r8   r:   r;   r<   r=   r>   classmethodr   rA   rB   rD   rG   rH   rI   rJ   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   propertyrV   rW   rZ   r[   r\   r]   rq   rr   rt   rv   ry   r   r   ru   r   r   r   r   r5   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                       sz   e Zd Z fddZedd Zedd Zeee d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 )Nra   $Not available for 'meta' device type)r   r0   NotImplementedErrorr   r%   r   r   r   r   r%   _  s    zUntypedStorage.__getitem__c                 C   s   | j jdkS Nr1   r   r0   r!   r   r   r   rV   d  s    zUntypedStorage.is_cudac                 C   s   | j jdkS Nr7   r   r!   r   r   r   rW   h  s    zUntypedStorage.is_hpur   c                 C   s   |   S zReturns the file name associated with this storage if the storage was memory mapped from a file.
           or ``None`` if the storage was not created by memory mapping a file.)r]   r!   r   r   r   rX   l  s    zUntypedStorage.filenamec                    s   t  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.

        Note that to mitigate issues like `this <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.

        .. note::
            When all references to a storage in shared memory are deleted, the associated shared memory
            object will also be deleted. PyTorch has a special cleanup process to ensure that this happens
            even if the current process exits unexpectedly.

            It is worth noting the difference between :meth:`share_memory_` and :meth:`from_file` with ``shared = True``

            #. ``share_memory_`` uses `shm_open(3) <https://man7.org/linux/man-pages/man3/shm_open.3.html>`_ to create a
               POSIX shared memory object while :meth:`from_file` uses
               `open(2) <https://man7.org/linux/man-pages/man2/open.2.html>`_ to open the filename passed by the user.
            #. Both use an `mmap(2) call <https://man7.org/linux/man-pages/man2/mmap.2.html>`_ with ``MAP_SHARED``
               to map the file/object into the current virtual address space
            #. ``share_memory_`` will call ``shm_unlink(3)`` on the object after mapping it to make sure the shared memory
               object is freed when no process has the object open. ``torch.from_file(shared=True)`` does not unlink the
               file. This file is persistent and will remain until it is deleted by the user.

        Returns:
            ``self``
        )r   r   r   r   r   r   r   r  s    !zUntypedStorage.share_memory_c                    s   t  j||S r   )r   r>   r   r   r   r   r>     s    zUntypedStorage._share_fd_cpu_c                    s   t  j||S r   )r   r=   r   r   r   r   r=     s    z#UntypedStorage._share_filename_cpu_)r   r   r   r%   r   rV   rW   r   rf   rX   r   r   r>   r=   r   r   r   r   r   r   ^  s   

"r   c                 C   s   t jt| ddS )NF)Zweights_only)r4   loadr|   r}   )r   r   r   r   r     s    r   )maxsizec                
   C   s,   t jt jt jt jt jt jt jt jt j	t j
h
S r   )r4   r   r   r   r   Zbits8Zbits16Zbits1x8Zbits2x4Zbits4x2Z	complex32r   r   r   r   _new_dtypes  s    r   c                "   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)r4   r   r   r   r   r   Zint16r   r   r   r   r   r   qint8qint32quint8quint4x2quint2x4r   r   r   r   _dtype_to_storage_type_map  sF                     r   c                  C   s   dd t   D } | S )Nc                 S   s   i | ]\}}||qS r   r   )rh   r   valr   r   r   
<dictcomp>  s     z._storage_type_to_dtype_map.<locals>.<dictcomp>)r   items)Z	dtype_mapr   r   r   _storage_type_to_dtype_map  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   )r4   r   r   r   r   r   r   int32r   r   r   _untyped_storage)sequencer/   r   interpret_dtypes
tmp_tensorr   r   r   _get_storage_from_sequence  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   s0   |  ddd }|dtj dfkr(|S dS d S )N.r   r1   r7   r   )rsplitr4   r   r   )r  Z	last_partr   r   r   _get_device_from_module  s    r  c                       s  e Zd ZU dZejed< edd Zee	e
 d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/d0Z dedd1d2Z!dd3ej"eed4d5d6Z#d7d8 Z$d9d: Z%e&dd;d<Z'd=d> Z(d?d@ Z)dAdB Z*dCdD Z+dEdF Z,dGdH Z- fdIdJZ.dKdL Z/dMdN Z0dOdP Z1dee
ej"f dRdSdTZ2dee
ej"f dRdUdVZ3dWdX Z4dYdZ Z5d
dRd[d\Z6ed]d^ Z7ed_d` Z"dadb Z8dcdd Z9dedf Z:dgdh Z;didj Z<dkdl Z=dmdn Z>dodp Z?dqdr Z@dsdt ZAeBdudv ZCdwdx ZDeBdydz ZEeBd
d
d{d|d}ZFd~d ZGdd ZHdd ZIdd ZJdd ZKdd Z&dd ZLdd ZMdd ZNdd Zdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUeBdd ZVeBdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]eBdd Z^dd Z_dd Z`eBd
dRddZadd Zbdd Zcdd Zd  ZeS )TypedStorageFr/   c                 C   s   | j S r   )r/   r!   r   r   r   _dtype  s    zTypedStorage._dtyper   c                 C   s   | j jS r   )r   rX   r!   r   r   r   rX   "  s    zTypedStorage.filenamec                 C   s    t   | td|  | | S )Nr   )r  _setitemslice_size)r   valuer   r   r   fill_(  s    zTypedStorage.fill_N)wrap_storager/   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)r/   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"  r/   r#  )r  r4   r   _LegacyStorageRuntimeErrorr  r   __new__rm   r  r   collectionsabcSequencer   r0   r  r  r   r   r   r/   )r?   r"  r/   r   r#  r   arg_error_msg
cls_devicer   r   r   r+  -  s    *
zTypedStorage.__new__)r   r/   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   r1   /Cannot create CUDA storage with quantized dtyper   r   r%  r$  )r  rm   r*  r   r4   r/   r   r0   r   r   get_default_dtyper   r   r   r   r   r   r  r   r   r,  r-  r.  r  )r   r   r/   r"  r#  r   r/  r   r   r   r   u  sv    
 "zTypedStorage.__init__c                 C   s   t   | jjjdkS r   r  r   r   r0   r!   r   r   r   rV     s    zTypedStorage.is_cudac                 C   s   t   | jjjdkS r   r3  r!   r   r   r   rW     s    zTypedStorage.is_hpuc                 C   s   t   | jS )z2Return 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"  )r0   r4   r   r   r  r/   )r   r   r   r   r   _new_wrapped_storage  s    z!TypedStorage._new_wrapped_storagec                 C   s   t   |  S r   r  r  r!   r   r   r   r"     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  r0   r   r   
IndexErrorr-   )r   r$   Zis_stopr   r   r   _maybe_wrap_index  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 )Nr7  r8  z cannot set item with value type r   Tr(  )r   r   r  r*  r0   r4   Z
is_storager/   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 | |}dd	lm} | 6 t	jg | j| j jd
| }||  W  5 Q R  S Q R X d S )Nra   r   z7slices are only supported in UntypedStorage.__getitem__r7  r8  Tr(  r   )unset_fake_temporarilyr   )r   r   r0   r   r   r  r*  r   r/   r4   r   r   r   r   r   r   r   r   r  r;  r:  Ztorch._subclasses.fake_tensorr<  r   r   item)r   r$   r   Zidx_wrappedr<  r  r   r   r   r;    s<    


     
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*   1  s
    
zTypedStorage.copy_c                 C   s   t   |  S r   )r  _nbytesr!   r   r   r   r,   9  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   rn   r0   r   )r   r/   r(   Zlegacy_classr   r   r   r0   A  s    zTypedStorage.typec                 C   sD   t   | jtjtjtjtjtjfkr,td| j	
||}| |S )Nr1  )r  r/   r4   r   r   r   r   r   r*  r   r1   r5  )r   r   r(   Zcuda_storager   r   r   r1   N  s
    zTypedStorage.cudac                 C   sD   t   | jtjtjtjtjtjfkr,td| j	
||}| |S )Nz.Cannot create HPU storage with quantized dtype)r  r/   r4   r   r   r   r   r   r*  r   r7   r5  )r   r   r(   Zhpu_storager   r   r   r7   U  s
    zTypedStorage.hpur   r   c                C   sT   t   | jtjtjtjtjtjfkr:td|j	
  d| jj||d}| |S )NzCannot create z storage with quantized dtyper2   )r  r/   r4   r   r   r   r   r   r*  r0   upperr   r5   r5  )r   r   r(   Z
to_storager   r   r   r5   \  s
    zTypedStorage.toc                 C   s   t   |  S r   )r  r   r!   r   r   r   r8   c  s    zTypedStorage.element_sizec                 C   s   t j| jS r   )r4   r   r   r/   r!   r   r   r   r   h  s    zTypedStorage._element_sizec                 C   s   t   | j S r   )r  r   r:   r!   r   r   r   r:   k  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 )Nr^   z(dtype=z	, device=r_   r`   ra   rb   rc   rd   c                 3   s   | ]}t  | V  qd S r   re   rg   r!   r   r   rj   w  s     z'TypedStorage.__str__.<locals>.<genexpr>rk   )
r  r4   rl   r/   r   rm   r0   rn   ro   r-   rp   r   r!   r   rq   o  s    *$zTypedStorage.__str__c                 C   s   t   t| S r   )r  rf   r!   r   r   r   rr   z  s    zTypedStorage.__repr__c                    s$   t   t fddt  D S )Nc                 3   s   | ]} | V  qd S r   r   rg   r!   r   r   rj     s     z(TypedStorage.__iter__.<locals>.<genexpr>)r  rs   ro   r-   r!   r   r!   r   rt   ~  s    zTypedStorage.__iter__c                 C   s   t   | t| jS r   )r  r5  copyr   r!   r   r   r   rv     s    zTypedStorage.__copy__c                 C   s   t   | |S r   )r  	_deepcopyr   rx   r   r   r   ry     s    zTypedStorage.__deepcopy__c                 C   s   |  t| j|S r   )r5  rA  deepcopyr   rC  r   r   r   rB    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  r5  r   ru   r!   r   r   r   ru     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  r5  r   r   r!   r   r   r   r     s    zTypedStorage.cpur1   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     s    	zTypedStorage.is_pinnedc                 C   s   t   | | jj|dS )zCopy 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  r5  r   r   r   r   r   r   r     s    	zTypedStorage.pin_memoryc                 C   s   t   |  S )r   )r  _share_memory_r!   r   r   r   r     s    zTypedStorage.share_memory_c                 C   s   | j   | S r   )r   r   r!   r   r   r   rE    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(  )r4   r   r   r   r   r  r/   )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     s    zTypedStorage._cdatac                 C   s   t   | jjS r   )r  r   r   r!   r   r   r   r     s    zTypedStorage.devicec                 C   s   t   |  S r   r6  r!   r   r   r   r-     s    zTypedStorage.sizec                 C   s   | j  |   S r   )r   r,   r   r!   r   r   r   r    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   r/   KeyError)r   er   r   r   rF    s    z!TypedStorage._pickle_storage_typec                 C   s&   t  }tj| |dd t| ffS rz   r{   r   r   r   r   r     s    zTypedStorage.__reduce__c                 C   s   t   |  S r   )r  	_data_ptrr!   r   r   r   r;     s    zTypedStorage.data_ptrc                 C   s
   | j  S r   )r   r;   r!   r   r   r   rJ    s    zTypedStorage._data_ptrc                 C   s   t   | j S r   )r  r   r<   r!   r   r   r   r<     s    zTypedStorage.resizablec                 C   s   t   | | d S r   )r  _resize_rL   r   r   r   rM     s    zTypedStorage.resize_c                 C   s   | j ||    d S r   )r   rM   r   rL   r   r   r   rK    s    zTypedStorage._resize_c                 O   s   t j||S r   )r   rU   rC   r   r   r   rU     s    zTypedStorage._free_weak_refc                 O   s   | j j||S r   )r   rN   r   r   r   r   rN   	  s    zTypedStorage._weak_refc                 O   s   t   | j||S r   )r  _from_bufferrC   r   r   r   rD     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 r/      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  r4   r2  r   r0   r*  r   rD   rm   r  )r?   r/   r   r   r   r   r   r   r   rL    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   r4   r/   r   r0   r   r   r   r5   r   r;   ru   r   r   r   r   r   ,  s    $zTypedStorage._toc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   4  s    zTypedStorage.doublec                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   9  s    zTypedStorage.floatc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   >  s    zTypedStorage.halfc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   C  s    zTypedStorage.longc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   H  s    zTypedStorage.intc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   M  s    zTypedStorage.shortc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   R  s    zTypedStorage.charc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   W  s    zTypedStorage.bytec                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   \  s    zTypedStorage.boolc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   a  s    zTypedStorage.bfloat16c                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   f  s    zTypedStorage.complex_doublec                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   k  s    zTypedStorage.complex_floatc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   p  s    zTypedStorage.float8_e5m2c                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   u  s    zTypedStorage.float8_e4m3fnc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r   z  s    zTypedStorage.float8_e5m2fnuzc                 C   s   t   | tjS r   )r  r   r4   r   r!   r   r   r   r     s    zTypedStorage.float8_e4m3fnuzc                 C   s@   t   | tkrtdt|||tj| j }| |d}|S )a  from_file(filename, shared=False, size=0) -> Storage

        Creates a CPU storage backed by a memory-mapped file.

        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 (whether ``MAP_SHARED`` or ``MAP_PRIVATE`` is passed to the
                            underlying `mmap(2) call <https://man7.org/linux/man-pages/man2/mmap.2.html>`_)
            size (int): number of elements in the storage
        z/from_file can only be called on derived classesr4  )	r  r  r*  r   rZ   r4   r   r   r/   )r?   rX   rY   r-   r   r   r   r   r   rZ     s    
zTypedStorage.from_filec                 O   s   t j||S r   )r   r[   rC   r   r   r   r[     s    zTypedStorage._expiredc                 O   s   | j j||S r   )r   rK   r   r   r   r   rK     s    zTypedStorage._write_filec                 O   s   | j j||S r   )r   rO   r   r   r   r   rO     s    zTypedStorage._set_from_filec                 O   s   | j j||S r   )r   rP   r   r   r   r   rP     s    zTypedStorage._set_cdatac                 O   s   | j j||S r   )r   rQ   r   r   r   r   rQ     s    zTypedStorage._share_cuda_c                 C   s   t   |  S r   )r  
_is_sharedr!   r   r   r   rR     s    zTypedStorage.is_sharedc                 C   s
   | j  S r   )r   rR   r!   r   r   r   rN    s    zTypedStorage._is_sharedc                 O   s   t jj||S r   )r4   r   rS   rC   r   r   r   rS     s    zTypedStorage._new_shared_cudac                 O   s&   | j j||\}}}||||   fS r   )r   r=   r   )r   r   r   Zmanager_handleZstorage_handler-   r   r   r   r=     s    z!TypedStorage._share_filename_cpu_c                 C   s   | j   | S r   )r   rJ   r!   r   r   r   rJ     s    
zTypedStorage._shared_decrefc                O   s   t jj||S r   r4   r   rH   )r?   r   r   r   r   r   r   _release_ipc_counter  s    z!TypedStorage._release_ipc_counterc                 O   s   | j j||S r   )r   rT   r   r   r   r   rT     s    zTypedStorage._shared_increfc                 O   s"   | j j||\}}|||   fS r   )r   r>   r   )r   r   r   fdr-   r   r   r   r>     s    zTypedStorage._share_fd_cpu_c                 C   s   | j t krd S t | j  }| jjdddtj fkr:d S | jjdkrJtntt| jj}zt||W S  tk
rz   Y d S X d S )Nr   r1   r7   )	r/   r   r   r0   r4   r   r   getattrAttributeError)r   Zstorage_namer  r   r   r   r?    s    z&TypedStorage._get_legacy_storage_class)F)N)NF)NF)NF)r1   )r1   )fr   r   r   r   r4   r/   r   r   r  r   rf   rX   r!  r+  r   rV   rW   r   r5  r"   r:  r&   r  r%   r;  r   r   r*   r,   r>  r   r0   r1   r7   r   r5   r8   r   r   r:   rq   rr   rt   rv   ry   rB  r   ru   r   r   r   r   r   rE  r   r   r-   r  rG  rF  r   r;   rJ  r<   rM   rK  r   rU   rN   rD   rL  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   r[   rK   rO   rP   rQ   rR   rN  rS   r=   rJ   rP  rT   r>   r?  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 )_LegacyStorageMetar/   c                 C   s2   t |tkr.t| j}||jj ko,| j|jkS dS )NF)r0   r  r  r   r   r/   )r?   instancer0  r   r   r   __instancecheck__  s    
z$_LegacyStorageMeta.__instancecheck__N)r   r   r   r4   r/   r   rV  r   r   r   r   rT    s   

rT  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   r4  )r4   r   r   r   )r?   r-   r   r   r   r   r     s    z_LegacyStorage._new_sharedc                 O   s   t jj||S r   rO  rC   r   r   r   rP    s    z#_LegacyStorage._release_ipc_counterc                 C   s(   |t j| j }| t j|||dS )Nr4  )r4   r   r   r/   r   rG   )r?   rE   rF   r-   Z
bytes_sizer   r   r   _new_shared_filename  s    z#_LegacyStorage._new_shared_filenameN)r   r   r   r   r   rP  rW  r   r   r   r   r)    s   

r)  )	metaclass)rG  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   rH  )rG  rI  r   r   r   #_get_dtype_from_pickle_storage_type  s    
rY  )r   )=r|   r4   r   r   r   Ztorch.typesr   typingr   r   r   Z_Dictr	   r   r
   r   r   rA  r,  r   r   r  r   numpyr  r  ModuleNotFoundErrorLockr   r   r   r   r   r   r   r   r   ZStorageBaser   r   r0   r   r   r   r  r  r  r	  r
  r  r  rf   r  r  __doc__r1   r7   r5   rT  r)  rY  r   r   r   r   <module>   sj    $
  &"?



     M

	