
    tio$                         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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)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      `/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/utils/_content_store.pycompile_hookz7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook4   sF    --=n=K%8Y__R%8%EGIbkk"///    )r   r   )r   r   r   s   ` r   decorate_fnz!lazy_compile.<locals>.decorate_fn3   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*   C   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.utilsr0   r   typer2   ctypesc_bytenbytesfrom_addressdata_ptrhashlibsha1update	hexdigest_Cdefault_generatorr3   default_generatorsindexr4   _get_default_mps_generatorr5   AssertionError	get_statemanual_seedemptyuint8set_numelFpadviewr!   ranger*   itemstructpackhex	set_state)r-   r,   r	   r0   device_typecpu_storagebufrE   	generatorstater'   rU   ITER_css                  r   hash_storagere   ^   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   )rh   setseen_storage_hashesr,   )selfrh   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)re   r,   rk   ospathjoinrh   makedirsexistsr	   saveadd)rl   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+   )re   untyped_storager,   r   storage_offsettupler    strider	   _utilsget_tensor_metadata)rl   r}   ry   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)ry   tensorsTrp   )r   r|   rr   rs   splitr   rt   rh   ru   r	   rw   )	rl   r   r}   r-   ry   dfpayloadrz   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boolrm   r	   UntypedStorager|   Tensorr   r   r   r   r   rg   rg      sc    'C 'd 't 'U%9%9 c 

 

8 8 8$ 8r   rg   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)cacherh   r.   Nc                N    || _         d | _        |rt        t              | _        y y r   )rh   storage_cacher   dict)rl   rh   r   s      r   rm   zContentStoreReader.__init__   s-      	 !,T!2D r   r   ry   c          	      F   |t        j                  |      }| j                  | j                  |   j                  |      nd }|-t         j                  j                  |j                        }||S t        j                  t        j                  j                  | j                  d|      d|      j                  }|:t        d| dt        j                  j                  | j                  d       d      | j                  t        |      | j                  |   |<   |S )Nro   T)weights_onlymap_locationzexpected storage for hash z in z
, got None)r	   r   r   getr   _new_with_weak_ptrcdataloadrr   rs   rt   rh   _untyped_storagerM   r   )rl   ry   r   wsss        r   read_storagezContentStoreReader.read_storage   s   \\&)F !!- v&**1- 	 >$$77AA}JJGGLL:q1
 
	 	

 9 ,QCtBGGLL:4V3WWab  ),:1,=Dv&q)r   r   c                     t         j                  j                  | j                  d|      }t         j                  j	                  |      st        |      t        j                  |d      S )Nr   T)r   )rr   rs   rt   rh   rv   FileNotFoundErrorr	   r   )rl   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   r6   )r   r   r	   tensorr   rR   r   set_tensor_metadata)rl   r   r   r   ry   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   rm   r	   r   r   r   r   r   r   r   r   r   r      s^    *. 3C 34 3 .2 c e6J6J 41 1 04   r   r   )r?   r   rD   os.pathrr   rY   collectionsr   r	   torch._prims_primsr$   torch._utilstorch.nn.functionalnn
functionalrT    torch.multiprocessing.reductionsr   r   r*   r   r   r   re   rg   r   r   r   r   <module>r      s   >      #      ;, d1 14 HM ,#%.. ,# ,#QT ,#^08 08f1 1r   