
    Ǆgu#                        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	m
Z
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 dlmZ d Z ed      d	        Zd
ddej6                  dedefdZ G d d      Z G d d      Z y)    N)defaultdict)DictOptionalSet)default_generator)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      b/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/utils/_content_store.pycompile_hookz7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook6   sF    --=n=K%8Y__R%8%EGIbkk"///    )r   r   )r   r   r   s   ` r   decorate_fnz!lazy_compile.<locals>.decorate_fn5   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.   E   s    
 	%		ce  y/Q	$$&Ahqwwqxxu{{S		  ==!a%!)*QC00r   Fstable_hashstorager0   returnc                \   dd l }ddlm} | j                  j                  }|s ||      s| j                         }t        j                  |j                         z  j                  |j                               }t        j                         }|j                  |       |j                         S |dk(  rt        }nE|dk(  r2dd l}|j"                  j$                  | j                  j&                     }nt)        d|       |j+                         }		 |j-                  d        |j.                  d|j0                  | j                        j3                  |       }
|
j5                          dz  }|dkD  rt7        j8                  |
d|fdd      }
|
j;                  |j<                        }
d	}t?        |      D cg c]  }tA        |
      jC                          }}tE        jF                  d
d|z  z   g| jI                         |jK                  |	       S c c}w # |jK                  |	       w xY w)Nr   )is_compile_supportedcpucudazunhandled device type r!   r       constant   >i)&torch._dynamotorch._dynamo.utilsr4   r    typer5   ctypesc_bytenbytesfrom_addressdata_ptrhashlibsha1update	hexdigestr   
torch.cudar6   default_generatorsindexAssertionError	get_statemanual_seedemptyuint8set_numelFpadviewr%   ranger.   itemstructpackhex	set_state)r1   r0   r   r4   device_typecpu_storagebufrF   	generatorstater+   rT   ITER_css                  r   hash_storagerd   `   s   8..%%K.{;kkm }}{1133AA  "
 ||~C~~ e%			JJ11'..2F2FG	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   B&H 6 H+H H H+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locr0   r2   Nc                 >    || _         t               | _        || _        y r   )rg   setseen_storage_hashesr0   )selfrg   r0   s      r   __init__zContentStoreWriter.__init__   s    -0U &r   r1   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)rd   r0   rj   ospathjoinrg   makedirsexistsr   saveadd)rk   r1   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/   )rd   untyped_storager0   r!   storage_offsettupler$   strider   _utilsget_tensor_metadata)rk   r|   rx   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)rx   tensorsTro   )r~   r{   rq   rr   splitr   rs   rg   rt   r   rv   )	rk   r   r|   r1   rx   dfpayloadry   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boolrl   r   UntypedStorager{   Tensorr   r   r   r   r   rf   rf      sc    'C 'd 't 'U%9%9 c 

 

8 8 8$ 8r   rf   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)cacherg   r2   Nc                N    || _         d | _        |rt        t              | _        y y r   )rg   storage_cacher   dict)rk   rg   r   s      r   rl   zContentStoreReader.__init__   s-      	 !,T!2D r   r    rx   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 )Nrn   T)weights_onlymap_location)r   r    r   getr   _new_with_weak_ptrcdataloadrq   rr   rs   rg   _untyped_storager   )rk   rx   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   )rq   rr   rs   rg   ru   FileNotFoundErrorr   r   )rk   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    rQ   r   set_tensor_metadata)rk   r   r    r!   rx   r   sizer   metadatar1   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   rl   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.pathrq   rX   collectionsr   typingr   r   r   r   torch._prims_primsr(   torch._utilstorch.nn.functionalnn
functionalrS   torch._Cr    torch.multiprocessing.reductionsr   r   r.   r   r   r   rd   rf   r   r   r   r   <module>r      s   >      # & &      & ;, d1 14 HM *#%.. *# *#QT *#Z08 08f. .r   