o
    0h$                     @   s   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 d dlm	Z	 d dl
Z
d dlmZ d dlZ
d dlm  mZ d dlmZ dd Zeddd	d
 Zddde
jdedefddZG dd dZG dd dZdS )    N)defaultdict)Optional)StorageWeakRefc                     s    fdd}|S )zkLazily wrap a function with torch.compile on the first call

    This avoids eagerly importing dynamo.
    c                    s   t   fdd}|S )Nc                     s6   t jfi  }t|t j< || i |S N)torchcompile	functoolswrapsglobals__name__)argskwargscompiled_fn)compile_kwargsfn N/var/www/vscode/kcb/lib/python3.10/site-packages/torch/utils/_content_store.pycompile_hook5   s   z7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook)r   r	   )r   r   r   )r   r   decorate_fn4   s   z!lazy_compile.<locals>.decorate_fnr   )r   r   r   r   r   lazy_compile.   s   	r   T)dynamicc                 C   sj   t jdd| j| jt jd }|d d  }t jdd| j| jt jd  }t||  | 	 dgS )Ni   l        )devicedtypei   r   )
r   randintshaper   int32abslongprimsxor_sumint)xabr   r   r   hash_storage_kernelD   s   r&   Fstable_hashstorager(   returnc             	      s  dd l }ddlm} | jj}|s||s3|  }tj|  	|
 }tjdd}|| | S |dkr<|jj}n+|dkrI|jj| jj }n|dkrS|j }n|dkr`|jj| jj }ntd	| | }	zO|d |jd|j| jd
|     d }
|
dkrt d|
fdd   |j! d} fddt"|D }t#j$dd|  g|R  % W |&|	 S |&|	 w )Nr   )is_compile_supportedF)usedforsecuritycpucudampsxpuzunhandled device type r   r      constant   c                    s   g | ]}t   qS r   )r&   item).0_r#   r   r   
<listcomp>   s    z hash_storage.<locals>.<listcomp>>i)'torch._dynamotorch._dynamo.utilsr+   r   typer-   ctypesc_bytenbytesfrom_addressdata_ptrhashlibsha1update	hexdigest_Cdefault_generatorr.   default_generatorsindexr/   _get_default_mps_generatorr0   AssertionError	get_statemanual_seedemptyuint8set_numelFpadviewr   rangestructpackhex	set_state)r)   r(   r   r+   device_typecpu_storagebufrE   	generatorstaterU   ITERcsr   r8   r   hash_storage_   s@   


rc   c                   @   sb   e Zd ZddededdfddZdejdefd	d
Zddej	fddZ
dedej	ddfddZdS )ContentStoreWriterFlocr(   r*   Nc                 C   s   || _ t | _|| _d S r   )re   setseen_storage_hashesr(   )selfre   r(   r   r   r   __init__   s   
zContentStoreWriter.__init__r)   c                 C   st   t || jd}|| jv r|S tj| jd}tj|dd tj||}tj|r,|S t	
|| | j| |S )Nr'   storagesTexist_ok)rc   r(   rg   ospathjoinre   makedirsexistsr   saveadd)rh   r)   h	subfoldertargetr   r   r   write_storage   s   
z ContentStoreWriter.write_storagetc                 C   sB   |d u rt | | jd}|j|| t|j| tj	
|fS )Nr'   )rc   untyped_storager(   r   storage_offsettupler   strider   _utilsget_tensor_metadata)rh   rx   rt   r   r   r   compute_tensor_metadata   s   
z*ContentStoreWriter.compute_tensor_metadatanamec           	      C   sj   |  }| |}tj|\}}| j||d}tj| jd|}tj|dd t	
|tj|| d S )N)rt   tensorsTrk   )ry   rw   rm   rn   splitr   ro   re   rp   r   rr   )	rh   r   rx   r)   rt   dfpayloadru   r   r   r   write_tensor   s   
zContentStoreWriter.write_tensor)Fr   )r   
__module____qualname__strboolri   r   UntypedStoragerw   Tensorr   r   r   r   r   r   rd      s
    rd   c                   @   sf   e Zd ZdddeddfddZddd	edejfd
dZdefddZdddedej	fddZ
dS )ContentStoreReaderT)cachere   r*   Nc                C   s"   || _ d | _|rtt| _d S d S r   )re   storage_cacher   dict)rh   re   r   r   r   r   ri      s   zContentStoreReader.__init__r   rt   c                C   s   |d ur	t |}| jd ur| j| |nd }|d ur)t j|j}|d ur)|S t jtj	
| jd|d|dj}|d us?J | jd urMt|| j| |< |S )Nrj   T)weights_onlymap_location)r   r   r   getr   _new_with_weak_ptrcdataloadrm   rn   ro   re   _untyped_storager   )rh   rt   r   wssr   r   r   read_storage   s*   


zContentStoreReader.read_storager   c                 C   s4   t j| jd|}t j|st|tj|ddS )Nr   T)r   )rm   rn   ro   re   rq   FileNotFoundErrorr   r   )rh   r   r   r   r   r   read_tensor_metadata   s   z'ContentStoreReader.read_tensor_metadatac                C   sX   |  |\}}}}}}| j||d}	tjg ||	jd}
|
|	||| tj|
| |
S )Nr   r1   )r   r   r   tensorr   rR   r}   set_tensor_metadata)rh   r   r   r   rt   rz   sizer|   metadatar)   rx   r   r   r   read_tensor   s   zContentStoreReader.read_tensor)r   r   r   r   ri   r   r   r   r   r   r   r   r   r   r   r      s
    r   )r?   r   rD   os.pathrm   rX   collectionsr   typingr   r   torch._prims_primsr    torch._utilstorch.nn.functionalnn
functionalrT    torch.multiprocessing.reductionsr   r   r&   r   r   r   rc   rd   r   r   r   r   r   <module>   s$   
/3