o
    h                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZmZmZmZ d dlmZ d dlZd dlmZmZ d dlmZmZmZ erSd dlmZ dd	gZzd dlZd
ZW n e yl   dZdZY nw e! Z"i Z#d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j,dd Z-ej,dd Z.ej,dd Z/dd Z0d d! Z1da2d"d# Z3d$d% Z4d8d'd(Z5d)d* Z6d9d-d.Z7G d/d dZ8ej9e8j:_9e&j;j9e8j;_9e&j<j9e8j<_9ej9e8j=_9G d0d1 d1e:Z>G d2d3 d3e8e>d4Z?d:d6d7Z@dS );    )annotationsN)AnycastOptionalTYPE_CHECKINGTypeVarUnion)Self)_to_type)_bool_intStorage)DeviceLikeTypeTypedStorageUntypedStorageTFzdict[int, threading.RLock]_share_memory_mapT!Union[_StorageBase, TypedStorage])boundc                      s8  e Zd ZU ded< dZded< dZded< ded< d	Zd
ed< d	Zded< dd ZdddZ	dd Z
dd ZddddZddd Zdd!d"Zdd#d$Z	ddd'd(Z	ddd)d*Zddd+d,Zdd-d.Zdd/d0Zdd1d2Zdd3d4Zd5d6 Zd7d8 Zedd;d<Zedd=d>Zedd?d@Zed	d	dAddBdCZeddDdEZeddFdGZddHdIZ dJdK Z!ddLdMZ"ddNdOZ#dPdQ Z$dRdS Z%dTdU Z&ddVdWZ'eddXdYZ(dZd[ Z)ed\d] Z*e+d^d_ Z,e+d`da Z-eddbdcZ.eddddeZ/dfdg Z0ddhdiZ1djdk Z2dldm Z3dndo Z4dpdq Z5drds Z6 fdtduZ7dvdw Z8dxdy Z9dzd{ Z:d|d} Z;d~d Z<dddddZ=dd Z>dd Z?dd Z@dd ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMddddZNddddZOdd ZPeddddZQdd ZRdd ZS  ZTS )_StorageBaser   _cdataFr   	is_sparseis_sparse_csrztorch.devicedeviceN_Optional[torch.device]_fake_devicez_Optional[int]_checkpoint_offsetc                 O  s   d S N selfargskwargsr   r   a/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/storage.py__init__5      z_StorageBase.__init__returnr   c                 C     t r   NotImplementedErrorr!   r   r   r$   __len__8   r&   z_StorageBase.__len__c                 C  r(   r   r)   r!   idxr   r   r$   __getitem__;   r&   z_StorageBase.__getitem__c                 O  r(   r   r)   r    r   r   r$   __setitem__>   r&   z_StorageBase.__setitem__sourcer   non_blocking_Optional[_bool]c                 C  r(   r   r)   r!   r1   r2   r   r   r$   copy_A   r&   z_StorageBase.copy_r   c                 C  r(   r   r)   r+   r   r   r$   newD   r&   z_StorageBase.newc                 C  r(   r   r)   r+   r   r   r$   nbytesG   r&   z_StorageBase.nbytesc                 C     |   S r   )r7   r+   r   r   r$   sizeJ      z_StorageBase.sizedtype_Optional[str]c                 C  s   t | ||S r   )r   )r!   r;   r2   r   r   r$   typeM   s   z_StorageBase.typec                 C  (   |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   r2   torchr   tor!   r   r2   device2r   r   r$   r?   R   s   z_StorageBase.cudac                 C  r>   )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.
        hpur@   rA   rD   r   r   r$   rF   c   s   z_StorageBase.hpuc                 C  r(   r   r)   r+   r   r   r$   element_sizer   r&   z_StorageBase.element_sizec                 C     | j jS r   )r   indexr+   r   r   r$   
get_deviceu   r:   z_StorageBase.get_devicec                 C  r(   r   r)   r+   r   r   r$   data_ptrx   r&   z_StorageBase.data_ptrc                 C  r(   r   r)   r+   r   r   r$   	resizable{   r&   z_StorageBase.resizablec                 O  r(   r   r)   r    r   r   r$   _share_filename_cpu_   r&   z!_StorageBase._share_filename_cpu_c                 O  r(   r   r)   r    r   r   r$   _share_fd_cpu_   r&   z_StorageBase._share_fd_cpu_r9   r	   c                 C  r(   r   r)   clsr9   r   r   r$   _new_using_filename_cpu      z$_StorageBase._new_using_filename_cpuc                 C  r(   r   r)   rO   r   r   r$   _new_using_fd_cpu   rR   z_StorageBase._new_using_fd_cpuc                 O  r(   r   r)   rP   r"   r#   r   r   r$   from_buffer   rR   z_StorageBase.from_buffer)r   r;   c                C  r(   r   r)   )rP   managerobjr9   r   r;   r   r   r$   _new_shared_filename_cpu   s   
z%_StorageBase._new_shared_filename_cpuc                 O  r(   r   r)   rT   r   r   r$   _release_ipc_counter_cuda   rR   z&_StorageBase._release_ipc_counter_cudac                 O  r(   r   r)   rT   r   r   r$   _new_with_weak_ptr   rR   z_StorageBase._new_with_weak_ptrc                 C  r(   r   r)   r+   r   r   r$   _shared_decref   r&   z_StorageBase._shared_decrefc                 O  r(   r   r)   r    r   r   r$   _write_file   r&   z_StorageBase._write_filec                 C  r(   r   r)   r!   r9   r   r   r$   resize_   r&   z_StorageBase.resize_c                 O  r(   r   r)   r    r   r   r$   	_weak_ref   r&   z_StorageBase._weak_refc                 O  r(   r   r)   r    r   r   r$   _set_from_file   r&   z_StorageBase._set_from_filec                 O  r(   r   r)   r    r   r   r$   
_set_cdata   r&   z_StorageBase._set_cdatac                 O  r(   r   r)   r    r   r   r$   _share_cuda_   r&   z_StorageBase._share_cuda_c                 C  r(   r   r)   r+   r   r   r$   	is_shared   r&   z_StorageBase.is_sharedc                 O  r(   r   r)   rT   r   r   r$   _new_shared_cuda   rR   z_StorageBase._new_shared_cudac                 O  r(   r   r)   r    r   r   r$   _shared_incref   r&   z_StorageBase._shared_increfc                 O  r(   r   r)   rT   r   r   r$   _free_weak_ref   rR   z_StorageBase._free_weak_refc                 C  r(   r   r)   r+   r   r   r$   is_cuda   rR   z_StorageBase.is_cudac                 C  r(   r   r)   r+   r   r   r$   is_hpu   rR   z_StorageBase.is_hpuc                 C  r(   r   r)   )rP   filenamesharedr7   r   r   r$   	from_file   rR   z_StorageBase.from_filec                 O  r(   r   r)   rT   r   r   r$   _expired   rR   z_StorageBase._expiredc                 O  r(   r   r)   r    r   r   r$   	_byteswap   r&   z_StorageBase._byteswapc                 O  r(   r   r)   r    r   r   r$   _get_filename   r&   z_StorageBase._get_filenamec                   sh   dt   d j dt  d} jjdkrd| S dd fd	d
t  D  }|d | S )N[z(device=
) of size ]meta...
 
 c                 3      | ]	}t  | V  qd S r   str.0ir+   r   r$   	<genexpr>       z(_StorageBase.__repr__.<locals>.<genexpr>
)rB   typenamer   lenr=   joinranger9   r!   info_strdata_strr   r+   r$   __repr__   s
   $$z_StorageBase.__repr__c                   s   t  fddt  D S )Nc                 3      | ]} | V  qd S r   r   ry   r+   r   r$   r|          z(_StorageBase.__iter__.<locals>.<genexpr>)iterr   r9   r+   r   r+   r$   __iter__   s   z_StorageBase.__iter__c                 C  r8   r   )cloner+   r   r   r$   __copy__   r:   z_StorageBase.__copy__c                 C  s6   | di }| j|v r|| j S |  }||| j< |S )NrB   )
setdefaultr   r   )r!   memonew_storager   r   r$   __deepcopy__   s   


z_StorageBase.__deepcopy__c                 C  &   t  }tj| |dd t| ffS NF)_use_new_zipfile_serializationioBytesIOrB   save_load_from_bytesgetvaluer!   br   r   r$   
__reduce__      z_StorageBase.__reduce__c                   s   t   |   S r   )super
__sizeof__r9   r+   	__class__r   r$   r         z_StorageBase.__sizeof__c                 C  s   t | |  | jd| S )Return a copy of this storage.r   )r=   r7   r   r5   r+   r   r   r$   r      s   z_StorageBase.clonec                 C  s   t | S z6Return a list containing the elements of this storage.)listr+   r   r   r$   tolist      z_StorageBase.tolistc                 C  s&   | j jdkrt|  | dS | S )AReturn a CPU copy of this storage if it's not already on the CPU.cpuFr   r=   rB   r   r9   r5   r+   r   r   r$   r     s   z_StorageBase.cpuc                 C  s*   | j jdkrtj|  dd| dS | S )zAReturn a MPS copy of this storage if it's not already on the MPS.mpsr   Fr   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r0| }|S Nz*Argument 'dtype' must be torch.dtype, not r;   r   )
isinstancerB   r;   	TypeErrorr=   tensoruint8r   set_r   r   rC   _typed_storagerK   r   r!   r;   storager   r   r$   r
     s   z_StorageBase._tor2   r   c                C  s"   t |tjst|}t| ||S r   )r   rB   r   r
   )r!   r   r2   r   r   r$   rC     s   
z_StorageBase.toc                 C     |  tjS z"Casts this storage to double type.)r
   rB   doubler+   r   r   r$   r   !     z_StorageBase.doublec                 C  r   z!Casts this storage to float type.)r
   rB   floatr+   r   r   r$   r   %  r   z_StorageBase.floatc                 C  r   z Casts this storage to half type.)r
   rB   halfr+   r   r   r$   r   )  r   z_StorageBase.halfc                 C  r   z Casts this storage to long type.)r
   rB   longr+   r   r   r$   r   -  r   z_StorageBase.longc                 C  r   zCasts this storage to int type.)r
   rB   intr+   r   r   r$   r   1  r   z_StorageBase.intc                 C  r   z!Casts this storage to short type.)r
   rB   shortr+   r   r   r$   r   5  r   z_StorageBase.shortc                 C  r   z Casts this storage to char type.)r
   rB   int8r+   r   r   r$   char9  r   z_StorageBase.charc                 C  r   z Casts this storage to byte type.)r
   rB   r   r+   r   r   r$   byte=  r   z_StorageBase.bytec                 C  r   z Casts this storage to bool type.)r
   rB   boolr+   r   r   r$   r   A  r   z_StorageBase.boolc                 C  r   z$Casts this storage to bfloat16 type.)r
   rB   bfloat16r+   r   r   r$   r   E  r   z_StorageBase.bfloat16c                 C  r   z*Casts this storage to complex double type.)r
   rB   cdoubler+   r   r   r$   complex_doubleI  r   z_StorageBase.complex_doublec                 C  r   z)Casts this storage to complex float type.)r
   rB   cfloatr+   r   r   r$   complex_floatM  r   z_StorageBase.complex_floatc                 C  r   z&Casts this storage to float8_e5m2 type)r
   rB   float8_e5m2r+   r   r   r$   r   Q  r   z_StorageBase.float8_e5m2c                 C  r   z(Casts this storage to float8_e4m3fn type)r
   rB   float8_e4m3fnr+   r   r   r$   r   U  r   z_StorageBase.float8_e4m3fnc                 C  r   z*Casts this storage to float8_e5m2fnuz type)r
   rB   float8_e5m2fnuzr+   r   r   r$   r   Y  r   z_StorageBase.float8_e5m2fnuzc                 C  r   z*Casts this storage to float8_e4m3fnuz type)r
   rB   float8_e4m3fnuzr+   r   r   r$   r   ]  r   z_StorageBase.float8_e4m3fnuzr?   Union[str, torch.device]c                 C  s&   t jg t j| jdtt| |S )a-  Determine whether the CPU storage is already pinned on device.

        Args:
            device (str or torch.device): The device to pin memory on (default: ``'cuda'``).
                This argument is discouraged and subject to deprecated.

        Returns:
            A boolean variable.
        r   )rB   r   r   r   r   r   r   	is_pinnedr!   r   r   r   r$   r   a  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 )a3  Copy 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'``).
                This argument is discouraged and subject to deprecated.

        Returns:
            A pinned CPU storage.
        r   zcannot pin 'z' only CPU memory can be pinnedr   )r   r=   r   rB   r   r   r   r   r   
pin_memoryuntyped_storage)r!   r   pinned_tensorr   r   r$   r   q  s   
z_StorageBase.pin_memoryc                 C  sJ   ddl m} | jjdtj fv r	 | S | dkr|   | S |   | S ).See :meth:`torch.UntypedStorage.share_memory_`r   get_sharing_strategyr?   file_system)	torch.multiprocessingr   r   r=   rB   _C_get_privateuse1_backend_namerM   rN   )r!   r   r   r   r$   share_memory_  s   
z_StorageBase.share_memory_r   r   c                C  sV   ddl m} t|}|jdtj dfv r| ||dS | dkr&| |S | |S )>Create a new storage in shared memory with the same data type.r   r   r?   rF   r   r   )	r   r   rB   r   r=   r   r   rQ   rS   )rP   r9   r   r   r   r   r$   _new_shared  s   



z_StorageBase._new_sharedc                 C  s   | S r   r   r+   r   r   r$   untyped  r&   z_StorageBase.untypedc                 C  s2   t j|}|jrtt|d d}| | dS )zSwap bytes in underlying data.      N)rB   _utils_element_size
is_complexmaxr   rm   )r!   r;   	elem_sizer   r   r$   byteswap  s   z_StorageBase.byteswapr'   r   r   )r1   r   r2   r3   r'   r   )r'   r   NF)r;   r<   r2   r   r'   r   )r'   r   )r9   r   r'   r	   r'   r	   )r9   r   r'   r<   )r   r   r2   r   r?   r   r   )U__name__
__module____qualname____annotations__r   r   r   r   r%   r,   r/   r0   r5   r6   r7   r9   r=   r?   rF   rG   rJ   rK   rL   rM   rN   classmethodrQ   rS   rU   rX   rY   rZ   r[   r\   r^   r_   r`   ra   rb   rc   rd   re   rf   propertyrg   rh   rk   rl   rm   rn   r   r   r   r   r   r   r   r   r   r   r
   rC   r   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                   sB  d }d }t " | j}|tv rt| }nt t|< t|   |}W d    n1 s+w   Y  |d urF| W d    n1 sAw   Y  z3 | g|R i |W |d ury| j|ks]J t  t|   t|= W d    S 1 ssw   Y  S S |d ur| j|ksJ t  t|   t|= W d    w 1 sw   Y  w r   )_share_memory_lockr   r   	threadingRLockacquirerelease)r!   r"   r#   to_free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                      sr   e Zd Z fddZedd Zedd Z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|i |S )Nrr   $Not available for 'meta' device type)r   r=   r*   r   r/   r    r   r   r$   r/     s   zUntypedStorage.__getitem__c                 C     | j jdkS Nr?   r   r=   r+   r   r   r$   rg     r   zUntypedStorage.is_cudac                 C  r  NrF   r  r+   r   r   r$   rh     r   zUntypedStorage.is_hpur'   r<   c                 C  r8   )zReturns the file name associated with this storage.

        The file name will be a string if the storage is on CPU and was created via
        :meth:`~torch.from_file()` with ``shared`` as ``True``. This attribute is ``None`` otherwise.
        )rn   r+   r   r   r$   ri     s   zUntypedStorage.filenamec                      t  j|i |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     s   !zUntypedStorage.share_memory_c                   r  r   )r   rN   r    r   r   r$   rN        zUntypedStorage._share_fd_cpu_c                   r  r   )r   rM   r    r   r   r$   rM     r  z#UntypedStorage._share_filename_cpu_r   )r   r   r   r/   r  rg   rh   ri   r  r   rN   rM   r  r   r   r   r$   r     s    

"c                 C  s   t jt| ddS )NF)weights_only)rB   loadr   r   )r   r   r   r$   r        r   c                   C  s<   t jt jt jt jt jt jt jt jt j	t j
t jt jt jt jhS r   )rB   r   r   r   r   float8_e8m0fnubits8bits16bits1x8bits2x4bits4x2	complex32uint16uint32uint64r   r   r   r$   _new_dtypes  s   r%  c                   C  s   i 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S )NDoubleStorageFloatStorageHalfStorageLongStorage
IntStorageShortStorageCharStorageByteStorageBoolStorageBFloat16StorageComplexDoubleStorageComplexFloatStorageQInt8StorageQInt32StorageQUInt8StorageQUInt4x2StorageQUInt2x4Storage)rB   r   r   r   r   r   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   )rz   r
  valr   r   r$   
<dictcomp>I  s    z._storage_type_to_dtype_map.<locals>.<dictcomp>)r=  items)	dtype_mapr   r   r$   _storage_type_to_dtype_mapG  s   rB  c              
   C  sx   |t jt jt jt jt jfv 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   )rB   r:  r;  r<  r9  r8  r   int32r   r   r   _untyped_storage)sequencer;   r   interpret_dtypes
tmp_tensorr   r   r$   _get_storage_from_sequenceM  s$   

rH  c                 C  s   t r
t| ttjfS t| tS r   )	HAS_NUMPYr   r   npinteger)xr   r   r$   _isintf  s   
rM  c                   C  s   t S r   )"_always_warn_typed_storage_removalr   r   r   r$   &_get_always_warn_typed_storage_removalp  r&   rO  c                 C  s   t | tsJ | ad S r   )r   r   rN  )always_warnr   r   r$   &_set_always_warn_typed_storage_removalt  s   rQ  r   c                 C  s>   dd }t  s
| rd}tj|t| d d dtjd< d S d S )Nc                   S  s   t tdsdS tj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   )
stacklevelTrR  )rO  warningswarnUserWarningrT  rU  )rW  rV  messager   r   r$   rT  z  s   rT  c                   C  s   dt jd< d S )NFrR  )rT  rU  r   r   r   r$   !_reset_warn_typed_storage_removal     r\  modulerx   c                 C  s,   |  ddd }|dtj dfv r|S dS )N.r   r?   rF   r   )rsplitrB   r   r   )r^  	last_partr   r   r$   _get_device_from_module  s   rc  c                      s  e Zd ZU dZded< dZded< ded< ed	d
 Zedd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dddZd d! Zdd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zddd0d1Zd2d3 Zd4d5 Z		ddd8d9Zddd:d;Zddd<d=Zdd>ddAdBZdCdD ZdEdF ZddHdIZdJdK Z dLdM Z!dNdO Z"dPdQ Z#dRdS Z$dTdU Z% fdVdWZ&dXdY Z'dZd[ Z(d\d] Z)ddd`daZ*dddbdcZ+ddde Z,dfdg Z-ddhdidjZ.edkdl Z/edmdn Z0dodp Z1dqdr Z2dsdt Z3dudv Z4dwdx Z5dydz Z6d{d| Z7d}d~ Z8dd Z9dd Z:e;dd Z<dd Z=e;dd Z>e;dddddZ?dd Z@dd ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPe;dd ZQe;dd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXe;dd ZYdd ZZddÄ Z[e;ddhddńZ\ddǄ Z]ddɄ Z^dd˄ Z_  Z`S )r   Fr   r   Nr   r   torch.dtyper;   c                 C  s   | j S r   )r;   r+   r   r   r$   _dtype  s   zTypedStorage._dtyper'   r<   c                 C  rH   )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.)rD  ri   r+   r   r   r$   ri     s   zTypedStorage.filenamec                 C  s    t   | td|  | | S )Nr   )rT  _setitemslice_size)r!   valuer   r   r$   fill_  s   zTypedStorage.fill_)wrap_storager;   r   	_internalc                  sb  |st   | tjjkrtd| tkrt | S |  d}|d ur(t|d |d ur2t|d |d u rqt|dkrBt|d t|dkrdt	|d sdt
|d tjjsdt|dt|d   t|| jt| jd	d
S t|dkr}t|d t
|tjst|dt|  t| j}|jj|krt|d| d|jj  t||| jd	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   rl  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 rk  r;   rl  )rT  rB   r   _LegacyStorageRuntimeErrorr   r   __new__r   rM  r   collectionsabcSequencer   r=   re  rc  r   r   r   r;   )rP   rk  r;   r   rl  r"   arg_error_msg
cls_devicer   r   r$   rt    s   	

zTypedStorage.__new__)r   r;   rk  rl  c                G  s  |st   d}|d urUt|dkrt|d |d u r!t|d t|tjs2t|dt|  |d ur<t|d || _t|tjsPt|dt|  || _	d S |d u r]t
 n|| _t|d u rhdn|}| jtjtjtjtjtjfv r|jd	krtd
t|dkrtj|d| _	d S t|dkrt|d rtjt|d |   |d| _	d S t|d tjjrt|d | j|| _	d S t|dt|d   t|d )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   ro  z#
Argument 'dtype' must be specifiedz+
Argument 'dtype' must be torch.dtype, not zG
Argument 'device' should not be specified when 'wrap_storage' is givenrp  r   r?   /Cannot create CUDA storage with quantized dtyper   r   rn  rm  )rT  r   rs  r   rB   r;   r   r=   r   rD  get_default_dtyper   r:  r;  r<  r9  r8  rM  r   r   ru  rv  rw  rH  )r!   r   r;   rk  rl  r"   rx  r   r   r$   r%     s   	


zTypedStorage.__init__c                 C     t   | jjjdkS r  rT  rD  r   r=   r+   r   r   r$   rg   g     zTypedStorage.is_cudac                 C  r|  r  r}  r+   r   r   r$   rh   l  r~  zTypedStorage.is_hpuc                 C  s   t   | jS )z2Return the internal :class:`torch.UntypedStorage`.)rT  rD  r+   r   r   r$   r   q  s   zTypedStorage.untypedr	   c                 C  sB   t |tjks	J t | tkrttt|| jddS t | |dS )NTrq  rk  )r=   rB   r   r   r   r	   r;   )r!   r   r   r   r$   _new_wrapped_storagev  s   z!TypedStorage._new_wrapped_storagec                 C     t   |  S r   rT  rh  r+   r   r   r$   r,        zTypedStorage.__len__c                 C  s   |d u r|r
|   S dS t|tkr tdt|  dt| |rG||   ks/||    k r;td| d|   |dkrA|S ||    S ||   ksT||    k r`td| d|   ||    S )Nr   can't index a  with zindex z" out of range for storage of size )rh  r=   r   r   
IndexErrorr9   )r!   r.   is_stopr   r   r$   _maybe_wrap_index  s&   zTypedStorage._maybe_wrap_indexc                 C  s   t   | ||S r   )rT  rf  )r!   r.   ri  r   r   r$   r0     s   zTypedStorage.__setitem__c              
   C  s   t |ttfstdt|  dt| t|r#tdt| | jtjtj	tj
tjtjfv rct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   Trq  )r   r   rg  rs  r=   rB   
is_storager;   r:  r;  r<  r9  r8  r   rC  r   r   rD  r   r   r   )r!   r.   ri  rF  	tmp_dtyperG  r   r   r$   rf    s@   


zTypedStorage._setitemc                 C     t   | |S r   )rT  _getitemr-   r   r   r$   r/        
zTypedStorage.__getitem__c              
   C  s  | j jjdkrtdt|trtdt|ts'tdt|  dt| | jt	j
t	jt	jt	jt	jfv rZt	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} |  t	jg | j| j jd
| }||  W  d    S 1 sw   Y  d S )Nrr   r  z7slices are only supported in UntypedStorage.__getitem__r  r  Trq  r   )unset_fake_temporarilyr   )rD  r   r=   r*   r   rg  rs  r   r;   rB   r:  r;  r<  r9  r8  r   rC  r   r   r  r  torch._subclasses.fake_tensorr  r   r   item)r!   r.   rF  idx_wrappedr  rG  r   r   r$   r    sL   



$zTypedStorage._getitemr1   r   r2   _Optional[bool]c                 C  s6   t   t|tr| j|j| | S | j|| | S r   )rT  r   r   rD  r5   r4   r   r   r$   r5     s   
zTypedStorage.copy_c                 C  r  r   )rT  _nbytesr+   r   r   r$   r7      r  zTypedStorage.nbytesc                 C  
   | j  S r   )rD  r7   r+   r   r   r$   r       
zTypedStorage._nbytesr   &Union[_StorageBase, TypedStorage, str]c                 C  sR   t   |d u r"|  }|d ur|jd |j S d| jt| jgS | j||S )Nr_  )rT  _get_legacy_storage_classr   r   r   r=   rD  )r!   r;   r2   legacy_classr   r   r$   r=     s   zTypedStorage.typec                 C  D   t   | jtjtjtjtjtjfv rtd| j	
||}| |S )Nrz  )rT  r;   rB   r:  r;  r<  r9  r8  rs  rD  r?   r  )r!   r   r2   cuda_storager   r   r$   r?        
zTypedStorage.cudac                 C  r  )Nz.Cannot create HPU storage with quantized dtype)rT  r;   rB   r:  r;  r<  r9  r8  rs  rD  rF   r  )r!   r   r2   hpu_storager   r   r$   rF   &  r  zTypedStorage.hpur   r   r   c                C  sj   t   t|tjst|}| jtjtjtjtjtj	fv r(t
d|j  d| jj||d}| |S )NzCannot create z storage with quantized dtyper@   )rT  r   rB   r   r;   r:  r;  r<  r9  r8  rs  r=   upperrD  rC   r  )r!   r   r2   
to_storager   r   r$   rC   3  s   

zTypedStorage.toc                 C  r  r   )rT  r   r+   r   r   r$   rG   D  r  zTypedStorage.element_sizec                 C  s   t j| jS r   )rB   r   r   r;   r+   r   r   r$   r   I  r]  zTypedStorage._element_sizer   c                 C     t   | j S r   )rT  rD  rJ   r+   r   r   r$   rJ   L  r  zTypedStorage.get_devicec              	     sv   t   dt  d j d j dt  d	} jjdkr#d| S dd	 fd
dt 	 D  }|d | S )Nro   z(dtype=z	, device=rp   rq   rr   rs   rt   ru   c                 3  rv   r   rw   ry   r+   r   r$   r|   Y  r}   z'TypedStorage.__str__.<locals>.<genexpr>r~   )
rT  rB   r   r;   r   r   r=   r   r   r9   r   r   r+   r$   __str__P  s   $zTypedStorage.__str__c                 C     t   t| S r   )rT  rx   r+   r   r   r$   r   \  r  zTypedStorage.__repr__c                   s$   t   t fddt  D S )Nc                 3  r   r   r   ry   r+   r   r$   r|   b  r   z(TypedStorage.__iter__.<locals>.<genexpr>)rT  r   r   r9   r+   r   r+   r$   r   `  s   zTypedStorage.__iter__c                 C  s   t   | t| jS r   )rT  r  copyrD  r+   r   r   r$   r   d     zTypedStorage.__copy__c                 C  r  r   )rT  	_deepcopyr!   r   r   r   r$   r   h  r  zTypedStorage.__deepcopy__c                 C  s   |  t| j|S r   )r  r  deepcopyrD  r  r   r   r$   r  m  r  zTypedStorage._deepcopyc                   s   t   t  |   S r   )rT  r   r   r7   r+   r   r   r$   r   p  r  zTypedStorage.__sizeof__c                 C     t   | | j S )r   )rT  r  rD  r   r+   r   r   r$   r   t     zTypedStorage.clonec                 C  r  r   )rT  r   r+   r   r   r$   r   y     zTypedStorage.tolistc                 C  r  )r   )rT  r  rD  r   r+   r   r   r$   r   ~  r  zTypedStorage.cpur?   r   c                 C  s   t   | j|S )a2  Determine whether the CPU TypedStorage is already pinned on device.

        Args:
            device (str or torch.device): The device to pin memory on (default: ``'cuda'``).
                This argument is discouraged and subject to deprecated.

        Returns:
            A boolean variable.
        )rT  rD  r   r   r   r   r$   r     s   
zTypedStorage.is_pinnedc                 C  s   t   | | jj|dS )a8  Copy 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'``).
                This argument is discouraged and subject to deprecated.

        Returns:
            A pinned CPU storage.
        r   )rT  r  rD  r   r   r   r   r$   r     s   
zTypedStorage.pin_memoryc                 C  r  )r   )rT  _share_memory_r+   r   r   r$   r     r  zTypedStorage.share_memory_c                 C     | j   | S r   )rD  r   r+   r   r   r$   r       
zTypedStorage._share_memory_r   c                C  s>   |du rd}t |}t jj||   |d}t|| jddS )r   Nr   r   Trq  )rB   r   r   r   r   r   r;   )r!   r9   r   r   r   r   r$   r     s   
zTypedStorage._new_sharedc                 C  rH   r   )rD  r   r+   r   r   r$   r     r   zTypedStorage._cdatac                 C  s   t   | jjS r   )rT  rD  r   r+   r   r   r$   r     r  zTypedStorage.devicec                 C  r  r   r  r+   r   r   r$   r9     r  zTypedStorage.sizec                 C  s   | j  |   S r   )rD  r7   r   r+   r   r   r$   rh    s   zTypedStorage._sizec                 C  r  r   )rT  _pickle_storage_typer+   r   r   r$   pickle_storage_type  r  z TypedStorage.pickle_storage_typec              
   C  s<   zt  | j W S  ty } z
td| j d|d }~ww )Nzdtype z is not recognized)r=  r;   KeyError)r!   er   r   r$   r    s   z!TypedStorage._pickle_storage_typec                 C  r   r   r   r   r   r   r$   r     r   zTypedStorage.__reduce__c                 C  r  r   )rT  	_data_ptrr+   r   r   r$   rK     r  zTypedStorage.data_ptrc                 C  r  r   )rD  rK   r+   r   r   r$   r    r  zTypedStorage._data_ptrc                 C  r  r   )rT  rD  rL   r+   r   r   r$   rL     r  zTypedStorage.resizablec                 C  s   t   | | d S r   )rT  _resize_r]   r   r   r$   r^     s   zTypedStorage.resize_c                 C  s   | j ||    d S r   )rD  r^   r   r]   r   r   r$   r    s   zTypedStorage._resize_c                 O     t j|i |S r   )r   rf   rT   r   r   r$   rf        zTypedStorage._free_weak_refc                 O     | j j|i |S r   )rD  r_   r    r   r   r$   r_     r   zTypedStorage._weak_refc                 O  s   t   | j|i |S r   )rT  _from_bufferrT   r   r   r$   rU     r  zTypedStorage.from_bufferr   c                O  s   | t kr2|d u rt n|}t|d u rdn|}|jdkr&td|j tjj|d|i|}n$|d us<t|dkr@td|d urHt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_bufferTrq  )
r   rB   r{  r   r=   rs  r   rU   r   re  )rP   r;   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r-| }|S r   )r   rB   r;   r   r=   r   r   r   rC   r   rK   r   r   r   r   r$   r
     s   zTypedStorage._toc                 C     t   | tjS r   )rT  r
   rB   r   r+   r   r   r$   r   %     zTypedStorage.doublec                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   *  r  zTypedStorage.floatc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   /  r  zTypedStorage.halfc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   4  r  zTypedStorage.longc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   9  r  zTypedStorage.intc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   >  r  zTypedStorage.shortc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   C  r  zTypedStorage.charc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   H  r  zTypedStorage.bytec                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   M  r  zTypedStorage.boolc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   R  r  zTypedStorage.bfloat16c                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   W  r  zTypedStorage.complex_doublec                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   \  r  zTypedStorage.complex_floatc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   a  r  zTypedStorage.float8_e5m2c                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   f  r  zTypedStorage.float8_e4m3fnc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   k  r  zTypedStorage.float8_e5m2fnuzc                 C  r  r   )rT  r
   rB   r   r+   r   r   r$   r   p  r  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 classesr  )	rT  r   rs  r   rk   rB   r   r   r;   )rP   ri   rj   r9   r   r   r   r   r$   rk   u  s   
zTypedStorage.from_filec                 O  r  r   )r   rl   rT   r   r   r$   rl     r  zTypedStorage._expiredc                 O  r  r   )rD  r\   r    r   r   r$   r\     r   zTypedStorage._write_filec                 O  r  r   )rD  r`   r    r   r   r$   r`     r   zTypedStorage._set_from_filec                 O  r  r   )rD  ra   r    r   r   r$   ra     r   zTypedStorage._set_cdatac                 O  r  r   )rD  rb   r    r   r   r$   rb     r   zTypedStorage._share_cuda_c                 C  r  r   )rT  
_is_sharedr+   r   r   r$   rc     r  zTypedStorage.is_sharedc                 C  r  r   )rD  rc   r+   r   r   r$   r    r  zTypedStorage._is_sharedc                 O     t jj|i |S r   )rB   r   rd   rT   r   r   r$   rd     r  zTypedStorage._new_shared_cudac                 O  s*   | j j|i |\}}}||||   fS r   )rD  rM   r   )r!   r"   r#   manager_handlestorage_handler9   r   r   r$   rM     s   z!TypedStorage._share_filename_cpu_c                 C  r  r   )rD  r[   r+   r   r   r$   r[     r  zTypedStorage._shared_decrefc                O  s   t jj|i |S r   rB   r   rY   )rP   r   r"   r#   r   r   r$   _release_ipc_counter  r  z!TypedStorage._release_ipc_counterc                 O  r  r   )rD  re   r    r   r   r$   re     r   zTypedStorage._shared_increfc                 O  s&   | j j|i |\}}|||   fS r   )rD  rN   r   )r!   r"   r#   fdr9   r   r   r$   rN     s   zTypedStorage._share_fd_cpu_c                 C  sz   | j t vrd S t | j  }| jjdddtj fvrd S | jjdkr%tntt| jj}zt||W S  ty<   Y d S w )Nr   r?   rF   )	r;   r=  r   r=   rB   r   r   getattrAttributeError)r!   storage_namer^  r   r   r$   r    s"   z&TypedStorage._get_legacy_storage_classr   r   )Fr   )r1   r   r2   r  r   )r;   r<   r2   r   r'   r  )r   r   r2   r   r'   r	   r   r   r   )ar   r   r   r   r   r   r  re  ri   rj  rt  r%   rg   rh   r   r  r,   r  r0   rf  r/   r  r5   r7   r  r=   r?   rF   rC   rG   r   rJ   r  r   r   r   r   r  r   r   r   r   r   r   r   r  r   r   r   r9   rh  r  r  r   rK   r  rL   r^   r  r   rf   r_   rU   r  r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   rl   r\   r`   ra   rb   rc   r  rd   rM   r[   r  re   rN   r  r  r   r   r   r$   r     s   
 
_[



#,







c                   @  s   e Zd ZU ded< dd ZdS )_LegacyStorageMetard  r;   c                 C  s2   t |tkrt| j}||jj ko| j|jkS dS r   )r=   r   rc  r   r   r;   )rP   instancery  r   r   r$   __instancecheck__  s   

z$_LegacyStorageMeta.__instancecheck__N)r   r   r   r   r  r   r   r   r$   r    s   
 r  c                   @  s0   e Zd Zedd Zedd Zedd ZdS )rr  c                 C  s    t j||    }| |dS )r   r  )rB   r   r   r   )rP   r9   r   r   r   r$   r     s   
z_LegacyStorage._new_sharedc                 O  r  r   r  rT   r   r   r$   r    r  z#_LegacyStorage._release_ipc_counterc                 C  s(   |t j| j }| t j|||dS )Nr  )rB   r   r   r;   r   rX   )rP   rV   rW   r9   
bytes_sizer   r   r$   _new_shared_filename  s   z#_LegacyStorage._new_shared_filenameN)r   r   r   r   r   r  r  r   r   r   r$   rr    s    

rr  )	metaclassr  c              
   C  s8   zt  |  W S  ty } z	td|  d|d }~ww )Nzpickle storage type "z" is not recognized)rB  r  )r  r  r   r   r$   #_get_dtype_from_pickle_storage_type  s   
r  )r   )r^  rx   )r  rx   )A
__future__r   ru  r  r  r   r  rX  typingr   r   r   	_Optionalr   r   r   typing_extensionsr	   rB   torch._utilsr
   r   torch.typesr   r   r   torch._prims_commonr   __all__numpyrJ  rI  ModuleNotFoundErrorLockr  r   r   r   r   r  r   StorageBaser   r   cacher%  r=  rB  rH  rM  rN  rO  rQ  rT  r\  rc  r   __doc__r=   r?   rF   rC   r  rr  r  r   r   r   r$   <module>   sx       $C




      
I
