a
    d                     @   s  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 d dlZd dlZd dlmZ d dlZzd dlZdZW n ey   dZY n0 e	dd	d
ZG dd dZG dd dejjeZdd Zee_ee_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 Z$d.d d!Z%d"d# Z&G d$d% d%Z'ej(e'j_(ej(e'j_(G d&d' d'eZ)G d(d) d)e'e)d*Z*e+d+d,d-Z,dS )/    N   )_type_cuda)Storage)AnyTypeVarTypeUnioncast)	lru_cacheT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deeedddZedddZedddZdeee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dd)d*Zeddd+edd,d-Zeedd.d/Z eedd0d1Z!edd2d3Z"d4d5 Z#ed6d7d8Z$edd9d:Z%edd;d<Z&d=d> Z'd?d@ Z(dAdB Z)eddCdDZ*eeddEdFZ+dGdH Z,edIdJ Z-e.dKdL Z/eeddMdNZ0eeddOdPZ1dQdR Z2dSdT Z3dUdV Z4dWdX Z5dYdZ Z6d[d\ Z7 fd]d^Z8d_d` Z9dadb Z:dcdd Z;dedf Z<dgdh Z=didj Z>dkdl Z?dmdn Z@dodp ZAdqdr Zdsdt ZBdudv ZCdwdx ZDdydz Zd{d| ZEd}d~ ZFdd ZGdd ZHdd ZIeddddZJdd ZK  ZLS )_StorageBase_cdataF	is_sparseis_sparse_csrdevicec                 O   s   d S N selfargskwargsr   r   V/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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__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   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                 K   s   d S r   r   )r   r   r$   r   r   r   r   cuda"   r   z_StorageBase.cudac                 C   s   d S r   r   r   r   r   r   element_size#   r   z_StorageBase.element_sizec                 C   s   d S r   r   r   r   r   r   
get_device$   r   z_StorageBase.get_devicec                 C   s   d S r   r   r   r   r   r   data_ptr%   r   z_StorageBase.data_ptrc                 O   s   d S r   r   r   r   r   r   _share_filename_cpu_(   r   z!_StorageBase._share_filename_cpu_c                 O   s   d S r   r   r   r   r   r   _share_fd_cpu_)   r   z_StorageBase._share_fd_cpu_)clsr(   r   c                 C   s   d S r   r   r2   r(   r   r   r   _new_using_filename_cpu*   s    z$_StorageBase._new_using_filename_cpuc                 C   s   d S r   r   r3   r   r   r   _new_using_fd_cpu,   s    z_StorageBase._new_using_fd_cpuc                 O   s   d S r   r   r2   r   r   r   r   r   from_buffer.   s    z_StorageBase.from_buffer)r   r*   c                C   s   d S r   r   )r2   managerobjr(   r   r*   r   r   r   _new_shared_filename_cpu0   s    z%_StorageBase._new_shared_filename_cpuc                 O   s   d S r   r   r6   r   r   r   _release_ipc_counter_cuda2   s    z&_StorageBase._release_ipc_counter_cudac                 O   s   d S r   r   r6   r   r   r   _new_with_weak_ptr4   s    z_StorageBase._new_with_weak_ptrc                 C   s   d S r   r   r   r   r   r   _shared_decref6   r   z_StorageBase._shared_decrefc                 O   s   d S r   r   r   r   r   r   _write_file7   r   z_StorageBase._write_file)r(   c                 C   s   d S r   r   r   r(   r   r   r   resize_8   r   z_StorageBase.resize_c                 O   s   d S r   r   r   r   r   r   	_weak_ref9   r   z_StorageBase._weak_refc                 C   s   d S r   r   r   r   r   r   	is_pinned:   r   z_StorageBase.is_pinnedc                 O   s   d S r   r   r   r   r   r   _set_from_file;   r   z_StorageBase._set_from_filec                 O   s   d S r   r   r   r   r   r   
_set_cdata<   r   z_StorageBase._set_cdatac                 O   s   d S r   r   r   r   r   r   _share_cuda_=   r   z_StorageBase._share_cuda_c                 C   s   d S r   r   r   r   r   r   	is_shared>   r   z_StorageBase.is_sharedc                 O   s   d S r   r   r6   r   r   r   _new_shared_cuda?   s    z_StorageBase._new_shared_cudac                 O   s   d S r   r   r   r   r   r   _shared_increfA   r   z_StorageBase._shared_increfc                 O   s   d S r   r   r6   r   r   r   _free_weak_refB   s    z_StorageBase._free_weak_refc                 C   s   d S r   r   r   r   r   r   is_cudaD   s    z_StorageBase.is_cudac                 C   s   d S r   r   )r2   filenamesharedr'   r   r   r   	from_fileF   s    z_StorageBase.from_filec                 O   s   d S r   r   r6   r   r   r   _expiredH   s    z_StorageBase._expiredc                    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>R   r   z'_StorageBase.__str__.<locals>.<genexpr>
)torchtypenamer   lenr+   joinranger(   r   Zinfo_strZdata_strr   r   r   __str__K   s    $z_StorageBase.__str__c                 C   s   t | S r   rV   r   r   r   r   __repr__U   s    z_StorageBase.__repr__c                    s   t t fddt  S )Nc                    s    |  S r   r   rZ   r   r   r   <lambda>Y   r   z'_StorageBase.__iter__.<locals>.<lambda>)itermapra   r(   r   r   r   r   __iter__X   s    z_StorageBase.__iter__c                 C   s   |   S r   )cloner   r   r   r   __copy__[   s    z_StorageBase.__copy__c                 C   s6   | di }| j|v r || j S |  }||| j< |S )Nr]   )
setdefaultr   rj   )r   memoZnew_storager   r   r   __deepcopy__^   s    


z_StorageBase.__deepcopy__c                 C   s&   t  }tj| |dd t| ffS NF)Z_use_new_zipfile_serializationioBytesIOr]   save_load_from_bytesgetvaluer   br   r   r   
__reduce__f   s    z_StorageBase.__reduce__c                    s   t   |   S r   )super
__sizeof__r(   r   	__class__r   r   rz   k   s    z_StorageBase.__sizeof__c                 C   s   t | |  | jd| S )Returns a copy of this storager   )r+   r'   r   r&   r   r   r   r   rj   n   s    z_StorageBase.clonec                 C   s   t | S z6Returns a list containing the elements of this storage)listr   r   r   r   tolistr   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   r+   r]   UntypedStorager(   r&   r   r   r   r   r   v   s    z_StorageBase.cpuc                 C   s.   | j jdkr&tj|  dd| dS | S dS )r   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   )
isinstancer]   r*   	TypeErrorr+   tensoruint8r   set_r
   r   to_typed_storager/   rj   r   r*   storager   r   r   _to   s    *z_StorageBase._toc                 C   s   |  tjS z!Casts this storage to double type)r   r]   doubler   r   r   r   r      s    z_StorageBase.doublec                 C   s   |  tjS z Casts this storage to float type)r   r]   floatr   r   r   r   r      s    z_StorageBase.floatc                 C   s   |  tjS zCasts this storage to half type)r   r]   halfr   r   r   r   r      s    z_StorageBase.halfc                 C   s   |  tjS zCasts this storage to long type)r   r]   longr   r   r   r   r      s    z_StorageBase.longc                 C   s   |  tjS zCasts this storage to int type)r   r]   intr   r   r   r   r      s    z_StorageBase.intc                 C   s   |  tjS z Casts this storage to short type)r   r]   shortr   r   r   r   r      s    z_StorageBase.shortc                 C   s   |  tjS zCasts this storage to char type)r   r]   int8r   r   r   r   char   s    z_StorageBase.charc                 C   s   |  tjS zCasts this storage to byte type)r   r]   r   r   r   r   r   byte   s    z_StorageBase.bytec                 C   s   |  tjS zCasts this storage to bool type)r   r]   boolr   r   r   r   r      s    z_StorageBase.boolc                 C   s   |  tjS z#Casts this storage to bfloat16 type)r   r]   bfloat16r   r   r   r   r      s    z_StorageBase.bfloat16c                 C   s   |  tjS z)Casts this storage to complex double type)r   r]   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   r]   cfloatr   r   r   r   complex_float   s    z_StorageBase.complex_floatc                 C   sH   | j rtd|   dddl}|jj }t| |  |d| S )z@Copies the storage to pinned memory, if it's not already pinned.zcannot pin 'z' only CPU memory can be pinnedr   N)	allocator)	rJ   r   r+   Z
torch.cudar,   ZmemoryZ_host_allocatorr(   r&   )r   r]   r   r   r   r   
pin_memory   s
    z_StorageBase.pin_memoryc                 C   s4   ddl m} | jrn| dkr(|   n|   | S )  Moves the storage to shared memory.

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

        Returns: self
        r   get_sharing_strategyfile_system)torch.multiprocessingr   rJ   r0   r1   )r   r   r   r   r   share_memory_   s    	

z_StorageBase.share_memory_r   r~   c                C   sN   ddl m} t|}|jdkr,| ||dS | dkr@| |S | |S dS )>Creates a new storage in shared memory with the same data typer   r   r,   r~   r   N)r   r   r]   r   r+   r4   r5   )r2   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.untyped)N)NF)NF)M__name__
__module____qualname__r   __annotations__r   r   r   r]   r   r   r   r   r"   r   r&   r'   r(   rW   r+   r,   r-   r.   r/   r0   r1   classmethodr   r4   r5   r7   r:   r;   r<   r=   r>   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   propertyrJ   rM   rN   rc   rd   ri   rk   rn   rx   rz   rj   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(   e Zd Z fddZe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+   NotImplementedErrorry   r"   r   r{   r   r   r"      s    zUntypedStorage.__getitem__c                 C   s   | j jdkS Nr,   )r   r+   r   r   r   r   rJ      s    zUntypedStorage.is_cuda)r   r   r   r"   r   rJ   r   r   r   r{   r   r      s   r   c                 C   s   t t| S r   )r]   loadrq   rr   )rw   r   r   r   rt      s    rt   )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)r]   r   r   r   r   r   int16r   r   r   r   r   r   qint8qint32quint8quint4x2quint2x4r   r   r   r   _dtype_to_storage_type_map   s$    r   c                  C   s   dd t   D } | S )Nc                 S   s   i | ]\}}||qS r   r   )rY   key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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   )r]   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_removal7  s    r   c                 C   s   t | tsJ | ad S r   )r   r   r   )Zalways_warnr   r   r   &_set_always_warn_typed_storage_removal:  s    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_timeB  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_removalR  s    r   c                       s&  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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d d!d"Zd#d$ Zd%d& Zdeeeeef d'd(d)Zded*d+d,Zd-d. Zd/d0 Ze d*d1d2Z!d3d4 Z"d5d6 Z#d7d8 Z$d9d: Z%d;d< Z&d=d> Z' fd?d@Z(dAdB Z)dCdD Z*dEdF Z+dGdH Z,dIdJ Z-dKdL Z.ddMdNdOZ/edPdQ Z0edRdS Z1dTdU Z2dVdW Z3dXdY Z4dZd[ Z5d\d] Z6d^d_ Z7d`da Z8dbdc Z9ddde Z:e;dfdg Z<dhdi Z=e;djdk Z>e;dddldmdnZ?dodp Z@dqdr ZAdsdt ZBdudv ZCdwdx ZDdydz Z d{d| ZEd}d~ ZFdd ZGdd Zdd ZHdd ZIdd ZJe;dd ZKe;dd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSe;dd ZTdd ZUdd ZVe;ddMddZWdd ZXdd ZYdd ZZ  Z[S )TypedStorageFr*   c                 C   s   | j S r   )r*   r   r   r   r   _dtypeZ  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_storager*   r   	_internalc                   s~  |s
t   | tjjkrtd| tkr2t | S |  d}|d urPt|d |d urdt|d |d u rt|dkrt|d t|dkrt	|d st
|d tjjst|dt|d   t|| j| jd	krd
ndddS t|dkrt|d t
|tjs(t|dt|  | jd	kr8d
nd}|jj|krft|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: 
torch.cudar,   r   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   r]   r   _LegacyStorageRuntimeErrorr   ry   __new__r_   r   r   collectionsabcSequencer   r+   r   r   r   r   r*   )r2   r   r*   r   r   r   arg_error_msg
cls_devicer{   r   r   r   c  s    *zTypedStorage.__new__)r   r*   r   r   c                G   s  |s
t   d}|d urt|dkr.t|d |d u rBt|d t|tjsdt|dt|  |d urxt|d || _t|tjst|dt|  || _	n|d u rt
 n|| _t|d u rdn|}| jtjtjtjtjtjfv 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   r,   /Cannot create CUDA storage with quantized dtyper~   r   r   r   )r   r_   r   r   r]   r*   r   r+   r   r   get_default_dtyper   r   r   r   r   r   r   r   _element_sizer   r   r   r   )r   r   r*   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   r+   r   r   r   r   rJ     s    zTypedStorage.is_cudac                 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J t | tkr.t|| jddS t | |dS d S )NTr   r   )r+   r]   r   r   r*   )r   untyped_storager   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 u 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   r+   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   __setitem__!  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v 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   r+   r]   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   %  s(    


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v 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 )	NrR   r   z7slices are only supported in UntypedStorage.__getitem__r	  r
  Tr   r   )r   r   r+   r   r   r   r   r   r*   r]   r   r   r   r   r   r   r   r   r   r  r  r   r   item)r   r!   r   Zidx_wrappedr   r   r   r   r  A  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&   _  s
    
zTypedStorage.copy_c                 C   s   t   |  S r   )r   _nbytesr   r   r   r   r'   g  s    zTypedStorage.nbytesc                 C   s
   | j  S r   )r   r'   r   r   r   r   r  l  s    zTypedStorage._nbytesr)   c                 C   sV   t   |d u rD|  }|d ur.|jd |j S d| jt| jgS | j||S d S )N.)r   _get_legacy_storage_classr   r   r`   r+   r   )r   r*   r$   Zlegacy_classr   r   r   r+   o  s    zTypedStorage.typer   c                 K   sL   t   | jtjtjtjtjtjfv r,td| j	j
||fi |}| |S )Nr  )r   r*   r]   r   r   r   r   r   r   r   r,   r  )r   r   r$   r   Zcuda_storager   r   r   r,   |  s
    zTypedStorage.cudac                 C   s   t   |  S r   )r   r  r   r   r   r   r-     s    zTypedStorage.element_sizec                 C   s   t j| jS r   )r]   _utilsr  r*   r   r   r   r   r    s    zTypedStorage._element_sizec                 C   s   t   | j S r   )r   r   r.   r   r   r   r   r.     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 )NrO   z(dtype=z	, device=rP   rQ   rR   rS   rT   rU   c                 3   s   | ]}t  | V  qd S r   rV   rX   r   r   r   r[     r   z'TypedStorage.__str__.<locals>.<genexpr>r\   )
r   r]   r^   r*   r   r_   r+   r`   ra   r(   rb   r   r   r   rc     s    $zTypedStorage.__str__c                 C   s   t   t| S r   )r   rW   r   r   r   r   rd     s    zTypedStorage.__repr__c                    s$   t   tt fddt  S )Nc                    s    |  S r   r   re   r   r   r   rf     r   z'TypedStorage.__iter__.<locals>.<lambda>)r   rg   rh   ra   r(   r   r   r   r   ri     s    zTypedStorage.__iter__c                 C   s   t   | t| jS r   )r   r  copyr   r   r   r   r   rk     s    zTypedStorage.__copy__c                 C   s   t   | |S r   )r   	_deepcopyr   rm   r   r   r   rn     s    zTypedStorage.__deepcopy__c                 C   s   |  t| j|S r   )r  r  deepcopyr   r  r   r   r   r    s    zTypedStorage._deepcopyc                    s   t   t  |   S r   )r   ry   rz   r'   r   r{   r   r   rz     s    zTypedStorage.__sizeof__c                 C   s   t   | | j S )r}   )r   r  r   rj   r   r   r   r   rj     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.cpuc                 C   s   t   | | j S )zBCoppies the  storage to pinned memory, if it's not already pinned.)r   r  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   r    s    
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~   Tr   )r]   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   r  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   sL   zt  | j W S  tyF } z td| j d|W Y d }~n
d }~0 0 d S )Nzdtype z is not recognized)r   r*   KeyError)r   er   r   r   r    s    z!TypedStorage._pickle_storage_typec                 C   s&   t  }tj| |dd t| ffS ro   rp   rv   r   r   r   rx      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   r  
  s    zTypedStorage._data_ptrc                 C   s   t   | | d S r   )r   _resize_r?   r   r   r   r@     s    zTypedStorage.resize_c                 C   s   | j ||    d S r   )r   r@   r  r?   r   r   r   r    s    zTypedStorage._resize_c                 O   s   t j|i |S r   )r   rI   r6   r   r   r   rI     s    zTypedStorage._free_weak_refc                 O   s   | j j|i |S r   )r   rA   r   r   r   r   rA     s    zTypedStorage._weak_refc                 O   s   t   | j|i |S r   )r   _from_bufferr6   r   r   r   r7     s    zTypedStorage.from_bufferr   c                O   s   | t krd|d u rt n|}t|d u r,dn|}|jdkrLtd|j tjj|d|i|}nH|d usxt|dkrtd|d u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   r]   r  r   r+   r   r   r7   r_   r   )r2   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rZ| }|S r   )r   r]   r*   r   r+   r   r   r   r   r   r/   rj   r   r   r   r   r   <  s    $zTypedStorage._toc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   D  s    zTypedStorage.doublec                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   I  s    zTypedStorage.floatc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   N  s    zTypedStorage.halfc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   S  s    zTypedStorage.longc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   X  s    zTypedStorage.intc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   ]  s    zTypedStorage.shortc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   b  s    zTypedStorage.charc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   g  s    zTypedStorage.bytec                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   l  s    zTypedStorage.boolc                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   q  s    zTypedStorage.bfloat16c                 C   s   t   | tjS r   )r   r   r]   r   r   r   r   r   r   v  s    zTypedStorage.complex_doublec                 C   s   t   | tjS r   )r   r   r]   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   rM   r]   r  r  r*   )r2   rK   rL   r(   r  r   r   r   r   rM     s    
zTypedStorage.from_filec                 O   s   t j|i |S r   )r   rN   r6   r   r   r   rN     s    zTypedStorage._expiredc                 C   s   t   | j S r   )r   r   rB   r   r   r   r   rB     s    zTypedStorage.is_pinnedc                 O   s   | j j|i |S r   )r   r>   r   r   r   r   r>     s    zTypedStorage._write_filec                 O   s   | j j|i |S r   )r   rC   r   r   r   r   rC     s    zTypedStorage._set_from_filec                 O   s   | j j|i |S r   )r   rD   r   r   r   r   rD     s    zTypedStorage._set_cdatac                 O   s   | j j|i |S r   )r   rE   r   r   r   r   rE     s    zTypedStorage._share_cuda_c                 C   s   t   |  S r   )r   
_is_sharedr   r   r   r   rF     s    zTypedStorage.is_sharedc                 C   s
   | j  S r   )r   rF   r   r   r   r   r!    s    zTypedStorage._is_sharedc                 O   s   t jj|i |S r   )r]   r   rG   r6   r   r   r   rG     s    zTypedStorage._new_shared_cudac                 O   s*   | j j|i |\}}}||||   fS r   )r   r0   r  )r   r   r   Zmanager_handleZstorage_handler(   r   r   r   r0     s    z!TypedStorage._share_filename_cpu_c                 C   s   | j   | S r   )r   r=   r   r   r   r   r=     s    
zTypedStorage._shared_decrefc                O   s   t jj|i |S r   r]   r   r;   )r2   r   r   r   r   r   r   _release_ipc_counter  s    z!TypedStorage._release_ipc_counterc                 O   s   | j j|i |S r   )r   rH   r   r   r   r   rH     s    zTypedStorage._shared_increfc                 O   s&   | j j|i |\}}|||   fS r   )r   r1   r  )r   r   r   fdr(   r   r   r   r1     s    zTypedStorage._share_fd_cpu_c                 C   sh   | j t vrd S t | j  }| jjdvr,d S | jjdkr<tntj}zt||W S  tyb   Y d S 0 d S )N)r   r,   r   )r*   r   r   r+   r]   r,   getattrAttributeError)r   Zstorage_namemoduler   r   r   r    s    z&TypedStorage._get_legacy_storage_class)F)N)NF)NF)\r   r   r   r   r]   r*   r   r   r   r   r   r   rJ   r   r  r   r  r  r   r"   r  r   r   r&   r'   r  rW   r	   r+   r,   r-   r  r   r.   rc   rd   ri   rk   rn   r  rz   rj   r   r   r   r   r  r   r   r   r(   r   r  r  rx   r/   r  r@   r  r   rI   rA   r7   r  r   r   r   r   r   r   r   r   r   r   r   rM   rN   rB   r>   rC   rD   rE   rF   r!  rG   r0   r=   r#  rH   r1   r  r   r   r   r{   r   r   U  s   


HD








r   c                   @   s    e Zd ZU ejed< dd ZdS )_LegacyStorageMetar*   c                 C   s:   t |tkr6| jdkrdnd}||jj ko4| j|jkS dS )Nr   r,   r   F)r+   r   r   r   r*   )r2   instancer  r   r   r   __instancecheck__  s    z$_LegacyStorageMeta.__instancecheck__N)r   r   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 )r   c                 C   s    t j||    }| |dS )r   r  )r]   r   r   r  )r2   r(   r  r   r   r   r     s    z_LegacyStorage._new_sharedc                 O   s   t jj|i |S r   r"  r6   r   r   r   r#    s    z#_LegacyStorage._release_ipc_counterc                 C   s(   |t j| j }| t j|||dS )Nr  )r]   r  r  r*   r   r:   )r2   r8   r9   r(   Z
bytes_sizer   r   r   _new_shared_filename  s    z#_LegacyStorage._new_shared_filenameN)r   r   r   r   r   r#  r+  r   r   r   r   r     s   

r   )	metaclass)r  c              
   C   sH   zt  |  W S  tyB } ztd|  d|W Y d }~n
d }~0 0 d S )Nzpickle storage type "z" is not recognized)r   r  )r  r  r   r   r   #_get_dtype_from_pickle_storage_type  s    
r-  )r   )-rq   r]   r  r   r   Ztorch.typesr   typingr   r   r   r	   r
   r  r   	functoolsr   r   numpyr   r   ModuleNotFoundErrorr   r   Z_CZStorageBaser   rt   r+   r,   r   r   r   r   r   r   r   r   r   r   __doc__r(  r   rW   r-  r   r   r   r   <module>   sT   
 U



     

	