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

    This avoids eagerly importing dynamo.
    c                 F     t        j                          fd       }|S )Nc                      t        j                  fi } t        j                        |      t	               j
                  <    || i |S N)torchcompile	functoolswrapsglobals__name__)argskwargscompiled_fncompile_kwargsfns      J/home/dcms/DCMS/lib/python3.12/site-packages/torch/utils/_content_store.pycompile_hookz7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook5   sF    --=n=K%8Y__R%8%EGIbkk"///    )r   r   )r   r   r   s   ` r   decorate_fnz!lazy_compile.<locals>.decorate_fn4   s&    			0 
	0
 r    )r   r   s   ` r   lazy_compiler   .   s     r   T)dynamicc                    t        j                  dd| j                  | j                  t         j                        j                         }|dz  dz   j                         }t        j                  dd| j                  | j                  t         j                        j                         j                         }t        j                  || z  |z   j                         dg      S )Ni   l        )devicedtypei   r   )
r
   randintshaper   int32abslongprimsxor_sumint)xabs      r   hash_storage_kernelr+   D   s    
 	%		ce  y/Q	$$&Ahqwwqxxu{{S		  ==!a%!)*QC00r   Fstable_hashstorager-   returnc                   dd l }ddlm} | j                  j                  }|s ||      s| j                         }t        j                  |j                         z  j                  |j                               }t        j                  d      }|j                  |       |j                         S |dk(  r|j                  j                   }n|dk(  r.|j"                  j$                  | j                  j&                     }na|dk(  r|j(                  j+                         }nA|dk(  r.|j,                  j$                  | j                  j&                     }nt/        d	|       |j1                         }		 |j3                  d        |j4                  d|j6                  | j                  
      j9                  |       }
|
j;                          dz  }|dkD  rt=        j>                  |
d|fdd      }
|
jA                  |jB                        }
d}tE        |      D cg c]  }tG        |
      jI                          }}tK        jL                  dd|z  z   g| jO                         |jQ                  |	       S c c}w # |jQ                  |	       w xY w)Nr   )is_compile_supportedF)usedforsecuritycpucudampsxpuzunhandled device type r   r      constant   >i))torch._dynamotorch._dynamo.utilsr1   r   typer3   ctypesc_bytenbytesfrom_addressdata_ptrhashlibsha1update	hexdigest_Cdefault_generatorr4   default_generatorsindexr5   _get_default_mps_generatorr6   AssertionError	get_statemanual_seedemptyuint8set_numelFpadviewr"   ranger+   itemstructpackhex	set_state)r.   r-   r
   r1   device_typecpu_storagebufrF   	generatorstater(   rV   ITER_css                  r   hash_storagerf   _   s   8..%%K.{;kkm }}{1133AA  "
 ||E2C~~ eHH..			JJ11'..2F2FG			II88:			II001E1EF	5k]CDD!E#a EKKW^^DII'R wwyj1n7a!S:q1AFF5;; 5:4[A!!$))+AA{{3t+1b1557E" B 	E"s   1B&I9  I47+I9 4I9 9Jc                       e Zd ZddededdfdZdej                  defdZddej                  fd	Z
d
edej                  ddfdZy)ContentStoreWriterlocr-   r/   Nc                 >    || _         t               | _        || _        y r	   )ri   setseen_storage_hashesr-   )selfri   r-   s      r   __init__zContentStoreWriter.__init__   s    -0U &r   r.   c                    t        || j                        }|| j                  v r|S t        j                  j                  | j                  d      }t        j                  |d       t        j                  j                  ||      }t        j                  j                  |      r|S t        j                  ||       | j                  j                  |       |S )Nr,   storagesTexist_ok)rf   r-   rl   ospathjoinri   makedirsexistsr
   saveadd)rm   r.   h	subfoldertargets        r   write_storagez ContentStoreWriter.write_storage   s    d.>.>?(((H GGLL:6	
I-i+77>>&!H

7F#  $$Q'r   tc                    |%t        |j                         | j                        }|j                  ||j	                         t        |j                        |j                         t        j                  j                  |      fS )Nr,   )rf   untyped_storager-   r   storage_offsettupler!   strider
   _utilsget_tensor_metadata)rm   r~   rz   s      r   compute_tensor_metadataz*ContentStoreWriter.compute_tensor_metadata   sh    9Q..0d>N>NOAGG!''NHHJLL,,Q/
 	
r   namec                    |j                         }| j                  |      }t        j                  j	                  |      \  }}| j                  ||      }t        j                  j                  | j                  d|      }t        j                  |d       t        j                  |t        j                  j                  ||             y )N)rz   tensorsTrq   )r   r}   rs   rt   splitr   ru   ri   rv   r
   rx   )	rm   r   r~   r.   rz   dfpayloadr{   s	            r   write_tensorzContentStoreWriter.write_tensor   s    ##%w'ww}}T"1..qA.6GGLL9a8	
I-

7BGGLLA67r   )Fr	   )r   
__module____qualname__strboolrn   r
   UntypedStorager}   Tensorr   r   r   r   r   rh   rh      sc    'C 'd 't 'U%9%9 c 

 

8 8 8$ 8r   rh   c                       e Zd ZdddeddfdZdddedej                  fd	Zd
efdZddd
edej                  fdZ
y)ContentStoreReaderT)cacheri   r/   Nc                N    || _         d | _        |rt        t              | _        y y r	   )ri   storage_cacher   dict)rm   ri   r   s      r   rn   zContentStoreReader.__init__   s-      	 !,T!2D r   r   rz   c                   |t        j                  |      }| j                  | j                  |   j                  |      nd }|-t         j                  j                  |j                        }||S t        j                  t        j                  j                  | j                  d|      d|      j                  }|J | j                  t        |      | j                  |   |<   |S )Nrp   T)weights_onlymap_location)r
   r   r   getr   _new_with_weak_ptrcdataloadrs   rt   ru   ri   _untyped_storager   )rm   rz   r   wsss        r   read_storagezContentStoreReader.read_storage   s    \\&)F !!- v&**1- 	 >$$77AA}JJGGLL:q1
 
	 	

 }}),:1,=Dv&q)r   r   c                     t         j                  j                  | j                  d|      }t         j                  j	                  |      st        |      t        j                  |d      S )Nr   T)r   )rs   rt   ru   ri   rw   FileNotFoundErrorr
   r   )rm   r   r   s      r   read_tensor_metadataz'ContentStoreReader.read_tensor_metadata   sH    WW\\$((It4ww~~b!#B''zz"400r   c                   | j                  |      \  }}}}}}| j                  ||      }	t        j                  g ||	j                        }
|
j                  |	|||       t        j                  j                  |
|       |
S )Nr   r7   )r   r   r
   tensorr   rS   r   set_tensor_metadata)rm   r   r   r   rz   r   sizer   metadatar.   r~   s              r   read_tensorzContentStoreReader.read_tensor   sz    ;?;T;T<
8q.$ ##Af#5LL5@	wf5((H5r   )r   r   r   r   rn   r
   r   r   r   r   r   r   r   r   r   r      s^    *. 3C 34 3 .2 c e6J6J .1 1 04   r   r   )r@   r   rE   os.pathrs   rZ   collectionsr   typingr   r
   torch._prims_primsr%   torch._utilstorch.nn.functionalnn
functionalrU    torch.multiprocessing.reductionsr   r   r+   r   r   r   rf   rh   r   r   r   r   <module>r      s   >      #       ;, d1 14 HM ,#%.. ,# ,#QT ,#^08 08f. .r   