o
    wZh$                     @   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kwargsZcompiled_fn)compile_kwargsfn I/var/www/auris/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)Z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primsZ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      Zconstant   c                    s   g | ]}t   qS r   )r#   item).0_r    r   r   
<listcomp>   s    z hash_storage.<locals>.<listcomp>>i)'Ztorch._dynamoZtorch._dynamo.utilsr(   r   typer*   ctypesZc_bytenbytesZfrom_addressZdata_ptrhashlibsha1update	hexdigestZ_CZdefault_generatorr+   Zdefault_generatorsindexr,   Z_get_default_mps_generatorr-   AssertionErrorZ	get_stateZmanual_seedemptyZuint8set_ZnumelFpadviewr   rangestructpackhexZ	set_state)r&   r%   r   r(   Zdevice_typeZcpu_storagebufr<   	generatorstaterD   ZITERcsr   r4   r   hash_storage_   s@   


rN   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   )rP   setseen_storage_hashesr%   )selfrP   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)rN   r%   rR   ospathjoinrP   makedirsexistsr   saveadd)rS   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$   )rN   untyped_storager%   r   storage_offsettupler   strider   _utilsZget_tensor_metadata)rS   rc   r_   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)r_   tensorsTrV   )rd   rb   rX   rY   splitri   rZ   rP   r[   r   r]   )	rS   rj   rc   r&   r_   dfpayloadr`   r   r   r   write_tensor   s   
zContentStoreWriter.write_tensor)Fr   )r   
__module____qualname__strboolrT   r   UntypedStoragerb   Tensorri   rp   r   r   r   r   rO      s
    rO   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)cacherP   r'   Nc                C   s"   || _ d | _|rtt| _d S d S r   )rP   storage_cacher   dict)rS   rP   rx   r   r   r   rT      s   zContentStoreReader.__init__r   r_   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 )NrU   T)weights_onlyZmap_location)r   r   ry   getru   Z_new_with_weak_ptrcdataloadrX   rY   rZ   rP   Z_untyped_storager   )rS   r_   r   wssr   r   r   read_storage   s*   


zContentStoreReader.read_storagerj   c                 C   s4   t j| jd|}t j|st|tj|ddS )Nrk   T)r|   )rX   rY   rZ   rP   r\   FileNotFoundErrorr   r   )rS   rj   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{   r.   )r   r   r   Ztensorr   rB   rh   Zset_tensor_metadata)rS   rj   r   r   r_   re   sizerg   metadatar&   rc   r   r   r   read_tensor   s   zContentStoreReader.read_tensor)r   rq   rr   rs   rT   r   ru   r   r   rv   r   r   r   r   r   rw      s
    rw   )r9   r   r;   Zos.pathrX   rG   collectionsr   typingr   r   Ztorch._primsZ_primsr   Ztorch._utilsZtorch.nn.functionalnnZ
functionalrC   Z torch.multiprocessing.reductionsr   r   r#   ru   rt   rs   rN   rO   rw   r   r   r   r   <module>   s$   
/3