
    9i              	       $   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKrSSKJrJr  SSKJrJrJrJr  SSKJr  SSKrSSKrSSKJr  SSKJrJ r   SSK!J"r"  SS	K#J$r$  S
SK%J&r&  S
SK'J(r(J)r)  \	RT                  " \+5      r,\RZ                  " SS9 " S S5      5       r.\RZ                   " S S5      5       r/\" S\05      r1\" S\05      r2\RZ                  " SS9 " S S5      5       r3\RZ                   " S S5      5       r4\RZ                   " S S\45      5       r5S\5S\Rl                  4S jr7S\Rl                  S\S\4S  jr8S\Rl                  S\9\0\04   4S! jr:\RZ                   " S" S#5      5       r;\$Rx                   " S$ S%\\;   5      5       r=S&\0S\04S' jr>S(\R~                  S)\@S*\@S\A\0   4S+ jrBS(\R~                  S)\@S*\@S\04S, jrCS\Rl                  S\R                  S   4S- jrE " S. S/5      rF\$Rx                   " S0 S1\\   5      5       rG\H\1\\   4   rI " S2 S3\R                  5      rK " S4 S5\K5      rL " S6 S7\K5      rM " S8 S9\M5      rN\N" \
R                  R                  \"" 5       S:5      5      rQg);a  
This module provides the infrastructure for creating and managing compile package
for torch.compile. We mainly have two abstractions here:
  - CompilePackage: Overarching data structure for store and lookup a list of compiled codes.
  - CodeCacheEntry: Data structure for a single code being compiled by torch.compile.
The caching behavior is always under user control explicitly so that a stronger guarantee can
be provided about cache hit for a specific compiled model. Users can load the compile package
from a different process or host.
    N)	GeneratorIterator)AnyCallableNewTypeOptional)NeverPackageError)PrecompileCacheArtifactPrecompileContext)	cache_dir)CacheArtifactFactory   )get_code_keys)dynamo_timedincrement_frameT)frozenc                      \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S	4   \S
'   \\	S	4   \S'   \\	S	4   \S'   \	\S'   \	\S'   \\S'   \\	S	4   \S'   \\	S	4   \S'   Sr
\\   \S'   Sr\\	   \S'   Sr\\   \S'   Sr\\	   \S'   \\R"                  S\R&                  SS 4S j5       5       r\\R"                  SS S\R&                  4S j5       5       rSrg)SerializedCode,   co_argcountco_posonlyargcountco_kwonlyargcount
co_nlocalsco_stacksizeco_flagsco_code.	co_constsco_namesco_varnamesco_filenameco_nameco_firstlinenoco_cellvarsco_freevarsNco_linetableco_qualnameco_exceptiontable	co_lnotabcodereturnc                    ^  [        5        Vs0 s H  o"[        X5      _M     nn[        U 4S jUS    5       5      US'   T " S0 UD6$ s  snf )Nc              3      >#    U  H7  n[        U[        R                  5      (       a  TR                  U5      OUv   M9     g 7fN)
isinstancetypesCodeTypefrom_code_object.0cclss     U/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/package.py	<genexpr>2SerializedCode.from_code_object.<locals>.<genexpr>F   s8      $
( (2!U^^'D'DC  #!K(s   ?Ar    )r   getattrtuple)r7   r+   keykwargss   `   r8   r3   SerializedCode.from_code_objectB   sY     6C_E_cwt))_E# $
K($
 
{ }V} Fs   Aserialized_codec                    ^  [        5        Vs0 s H  o"[        X5      _M     nn[        U 4S jUS    5       5      US'   [        R                  " UR                  5       6 $ s  snf )Nc              3   t   >#    U  H-  n[        U[        5      (       a  TR                  U5      OUv   M/     g 7fr/   )r0   r   to_code_objectr4   s     r8   r9   0SerializedCode.to_code_object.<locals>.<genexpr>P   s4      $
( &0>%B%BCq!I(s   58r   )r   r<   r=   r1   r2   values)r7   rA   r>   r?   s   `   r8   rD   SerializedCode.to_code_objectL   sh     ANPw44P# $
K($
 
{ ~~]]_
 	
 Qs   A!r;   )__name__
__module____qualname____firstlineno__int__annotations__bytesr=   r   strr'   r   r(   r)   r*   classmethod	functoolscacher1   r2   r3   rD   __static_attributes__r;       r8   r   r   ,   s   OMNS#XCHosCx LsCx sCx $(L(5/(!%K#%)-x-#Ix}#__ENN 7G    __
-= 
%.. 
  
rT   r   c                   .    \ rS rSr% Sr\\S'   \\S'   Srg)_GuardedCodeCacheEntryY   z
Contains the serializable information associated with a single compilation in dynamo.
To restore an execution of compiled code, we will need to serialize the following data:
  - Dynamo bytecode for mapping Python inputs/outputs.
  - Dynamo guards.
guards_statedynamo_coder;   N)	rH   rI   rJ   rK   __doc__rN   rM   r   rS   r;   rT   r8   rV   rV   Y   s     rT   rV   
_BackendId_FunctionIdc                   >    \ rS rSr% \\S'   \\S'   \\S'   \\S'   Srg)InlinedSourcej   modulefirstlineno
lastlinenochecksumr;   N)rH   rI   rJ   rK   rO   rM   rL   rS   r;   rT   r8   r^   r^   j   s    KOMrT   r^   c                   :    \ rS rSr% Sr\\   \S'   \\   \S'   Sr	g)DynamoCaptureOutputr   z<
Core information generated from Dynamo for fullgraph=True.
guarded_codesbackend_idsr;   N)
rH   rI   rJ   rK   rZ   listrV   rM   r[   rS   r;   rT   r8   re   re   r   s      .//j!!rT   re   c                       \ rS rSr% Sr\\S'   \\S'   \\	   \S'   \
\\4   \S'   \\   \S'   \\S'   S	r\\S
'   S	r\\S'   Srg)_DynamoCodeCacheEntry|   a  
Contains the serializable information associated with a single code object
in dynamo. To restore an execution of compiled code, we will need the following
ingredients:
  1. The "original" code object, which serves as the entry point for eager
     execution, i.e. the code only executed when there's no cache entry hit.
  2. The python module name this code object belongs to, for identifying the
     enclosing global scope to inject compiled and resume functions.
  3. A list of function names that pointing to this code object. There could be
     multiple function objects pointing to the same code such as recursive functions.
  4. A list of guarded code that eval frame dispatches to.
  5. A list of imported module objects unioned from all compiled branches.
  6. A list of "backends" (compiled fx graph) unioned from all compield branches.
  7. A string path used to access the original code object users defined.
     A code object can be accessed by "{python_module}.{function_name}.{code_source}" .
  8. A boolean flag indicating whether the function is installed to global scope.
  9. A boolean flag indicating whether the function has a compile id.
  10. Whether or not this code entry was bypassed
python_codepython_modulefunction_namesimport_sourcescode_sourceinstall_to_globalFhas_compile_idbypassedr;   N)rH   rI   rJ   rK   rZ   r   rM   rO   ri   r\   dictr   boolrs   rt   rS   r;   rT   r8   rk   rk   |   sR    (  %%cN"# ND HdrT   rk   entryr,   c                    [        U R                  5      S:X  d   e[        R                  U R                     nU R                  S   R                  S5      nU H  n[        X5      nM     U R                  (       a  U R                  R                  S5      nU H  nUR                  S5      (       an  UR                  S5      n[        U[        5      (       a  US:  d   e[        XS U S 5      nUc  [        SU  35      eU[        R                  " X4S-   S 5         nM  [        X5      nM     O[        SU  35      e[        U[        R                   5      (       d   eU$ )Nr   r   .][z"Cannot find source for code entry )lenro   sysmodulesrn   splitr<   rq   endswithrfindr0   rL   r   astliteral_evalr1   r2   )rw   fnpartspartindex_beginattrs         r8   _lookup_coder      sA   u##$)))kk%--.B  #))#.ER !!'',D}}S!!"jjo!+s33q8HHHr#5t<<&)KE7'STT#**4a"+EFGR&  ?wGHHb%..))))IrT   r+   scopec                 $    [        SU  SU 35      e)Nz*Cannot resolve a fully qualified name for z. Lookup scope: r
   )r+   r   s     r8   _raise_resolution_errorr      s     

4TF:J5'R rT   c                   ^ ^^^ [         R                  " T 5      nUc  [        ST  35      eUm[        R                  S:  aj  T R
                  R                  S5      nU HI  n[        TU5      (       d  [        T T5        [        TU5      m[         R                  " T5      (       d  MI    O   [        5       mS[        S[        [           4UU UU4S jjmT" T5      nUc  [        T T5        TR                  UR!                  S5      4$ )a/  
Given a code object, return a fully qualified name which will be used as
a serialized handle to access the code object from the new process.
This is normally a straightforward process, but there are some corner cases:
1. When a function is defined with decorator, then this function will be captured
   inside a closure with the wrapper object.
2. When a function is defined as a nested function, then the code object will be
   stored on the co_consts field of the parent code object by Python compiler.
This function handles all of the corner cases above.
Cannot find module for code       ry   objr,   c                   > U T
;   a  g T
R                  U 5        [        R                  " U 5      (       a9  U T	L a  g[        U R                  5       H  u  pT" U5      =nc  M  SU SU 3s  $    [        R
                  " U 5      (       a  T" U R                  5      =nb  U mSU 3$ U R                  bz  [        U R                  5       Ha  u  p UR                  n[        R
                  " U5      (       d  [        R                  " U5      (       d  MJ  T" U5      =nc  MW  U mSU SU 3s  $    [        R                  S:  Ga/  [        R                  " U 5      (       ah  U R                  R                  5        HJ  n[        R
                  " U5      (       d  [        R                  " U5      (       d  M;  T" U5      =nc  MH  Us  $    [        R                  " U 5      (       a  U R                  R!                  5        Hs  u  pv[#        X5      n[        R
                  " U5      (       d  [        R                  " U5      (       d  MH  T" U5      =nc  MU  UR$                  U:w  a  ['        T	T5        Us  $    g ! [         a     GM  f = f)N z.co_consts[rz   z	.__code__z.__closure__[z].cell_contentsr   )addinspectiscode	enumerater   
isfunction__code____closure__cell_contents
ValueErrorr~   version_infoismodule__dict__rF   isclassitemsr<   rH   r   )r   iconstrescellr   valuename_find_code_sourcer+   seentoplevels           r8   r   +_get_code_source.<locals>._find_code_source   s)    $;>>#d{%cmm4,U33C@(1SE22 5 c""(66C"3%((*(9GA!(,(:(:  **=99">>-88 0??L#&!.qcFF  : g%$$ \\002E#..u559O9O 077D"
	 3 s###&<<#5#5#7KD#C.E#..u559O9O 077D >>T13D(C"
 $8 9 & ! !s   I11
J ?J )r   	getmoduler   r~   r   r(   r   hasattrr   r<   r   setr   r   rO   rJ   strip)r+   r`   r   r   rq   r   r   r   s   `    @@@r8   _get_code_sourcer      s     t$F~9$@AAH
7"  &&s+D8T**'h7x.H!!(++  5D4s 4x} 4 4l $H-Kh/  +"3"3C"888rT   c                       \ rS rSr% \\   \S'   \\   \S'   \	R                  " 5       r
\\S'   \R                  r\\S'   \S\\   4S j5       rSrg	)
_DynamoCacheEntryi  codesinlined_sourcespython_versiontorch_versionr,   c                 p    U R                    VVs1 s H  oR                    H  o"iM     M     snn$ s  snnf r/   )r   rh   )selfr+   
backend_ids      r8   rh   _DynamoCacheEntry.backend_ids  s+    '+zzUztDTDTj
DT
zUUUs   2r;   N)rH   rI   rJ   rK   ri   rk   rM   r   r^   platformr   rO   torch__version__r   propertyr[   rh   rS   r;   rT   r8   r   r     s\    %&&''"113NC3**M3*VS_ V VrT   r   c                   :    \ rS rSr\S\4S j5       rS\4S jrSr	g)_DynamoCacheArtifacti  r,   c                      g)Nprecompile_dynamor;   r;   rT   r8   type_DynamoCacheArtifact.type   s    "rT   c                 B    [         R                  " U R                  5      $ r/   pickleloadscontentr   s    r8   after_deserialization*_DynamoCacheArtifact.after_deserialization$      ||DLL))rT   r;   N)
rH   rI   rJ   rK   staticmethodrO   r   r   r   rS   r;   rT   r8   r   r     s(    ## # #*'8 *rT   r   sourcec                     [         R                  " 5       nUR                  U R                  5       5        UR	                  5       $ r/   )hashlibsha256updateencode	hexdigest)r   sha256_hashs     r8   _hash_sourcer   (  s1    .."Kv}}'  ""rT   mra   rb   c                 F    [         R                  " U 5      S   US-
  US-
   $ )Nr   r   )r   getsourcelinesr   ra   rb   s      r8   _get_sourcelinesr   .  s)     !!!$Q'a*q.IIrT   c           	      J    [        SR                  [        XU5      5      5      $ )Nr   )r   joinr   r   s      r8   _hash_sourcelinesr   4  s     0 LMNNrT   c                    ^ ^^^^ SSK JmJm  SSKJmJm  [        R                  S[        S    4UU UUU4S jj5       nU" 5       $ )Nr   )get_compile_idlog_dynamo_start)compile_contextCompileContextr,   c               3   ~  >#    [        5         T" 0 S9n T" T" U 5      5         [        SSS[        [        R                  R
                  R                  5      TR                  TR                  TR                  S.S9   T" T5        S v   S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f7f)N)frame_statez_compile.compile_innerentire_frame_compile!dynamo_cumulative_compile_time_us)	frame_keyr#   r"   r$   )
phase_namedynamo_compile_column_usmetadata)
r   r   rO   r   _dynamoutils
curr_framer#   r"   r$   )
compile_idr   r+   r   r   r   s    r8   _ctx$_compile_frame_context.<locals>._ctxC  s     #3
N:67(1)L "%U]]%8%8%C%C!D#||#'#3#3&*&9&9	 T" 87  87s5   B=AB,=B
B,	B=
B)	%B,,
B:6B=)	torch._dynamo.convert_framer   r   torch._guardsr   r   
contextlibcontextmanagerr   )r+   r   r   r   r   r   s   ` @@@@r8   _compile_frame_contextr   8  s?     M= (4.   * 6MrT   c                      \ rS rSrSr  S0S\\S\4      S\\   S\	SS4S	 jjr
S\	4S
 jr  S0S\S\\   S\	SS4S jjr   S1S\R                  S\S\\   S\\   S\	SS4S jjr\S\\\4   4S j5       r\R.                  S\4S j5       rS\R                  SS4S jr\R6                  S\R                  S\S   4S j5       rS\S\R                  SS4S jrS\ \R                     SS4S jr!S2S jr"S\R                  S\S\\   SS4S jr#S\S \SS4S! jr$S3S"\S#\\   SS4S$ jjr%S2S% jr&S&\RN                  S'\S(\SS4S) jr(S2S* jr)S+\\\4   SS4S, jr*S\4S- jr+\,S\S\4   S\4S. j5       r-S/r.g)4CompilePackagei\  a|  
CompilePackage is considered a low level component and should not be directly exposed to
end users. It has the following interface:

1. `CompilePackage.__init__()` which optionally takes previously serialized dynamo states.
    a. when `dynamo` argument is None, it will construct a brand new CompilePackage object.
    b. when `dynamo` argument is not None, it will load a pre-compiled dynamo state.
2. `package.save()` which dumps the dynamo and backend states to a DynamoCacheEntry object.
3. `package.install(backends) which will handle all the side-effectful global scope
    updates with compiled functions and resume functions.
Nr   .dynamoignore_inlined_sourcesr,   c                     S U l         0 U l        S U l        0 U l        0 U l        [        5       U l        [        5       U l        SU l        Ub3  U R                  XU5        U R                  5         U R                  5         g g NF)_innermost_fn_codes_current_entry_installed_globals_cached_backendsr   _inlined_sources_resume_codes_initialized
initialize	uninstallvalidate)r   r   r   r   s       r8   __init__CompilePackage.__init__i  su     "CE?CEG 8:47E25%!>OOB(>?NNMMO rT   c                     U R                   $ r/   )r  r   s    r8   is_initializedCompilePackage.is_initialized  s       rT   c           
      >   SSK Jn  U R                  (       a   e[        5       U l        U" U5      U l        U R
                  c   eUGb  [        U[        5      (       d   eUR                  [        R                  " 5       :w  a  [        SUR                   35      eUR                  [        R                  :w  a  [        SUR                   35      eU(       d  UR                   H  n[        R                   " UR"                  5      n[%        XeR&                  UR(                  5      nXuR*                  :w  d  MT  [        SUR"                   SUR&                   SUR(                   S35      e   UR                  U l        UR,                  tpU R
                  R.                  U0U l        U	 H.  nXPR0                  [2        R5                  UR6                  5      '   M0     O:U R9                  U R
                  R.                  U R
                  R:                  5        S	U l        g )
Nr   innermost_fnz=Compile package was created with a different Python version: z>Compile package was created with a different PyTorch version: z!Source code changes detected for z (line z - line )T)
eval_framer  r  r   r  r   r0   r   r   r   RuntimeErrorr   r   r   r   	importlibimport_moduler`   r   ra   rb   rc   r   r   r  r   rD   rm   _add_functionrI   )
r   r   r   r   r  r+   r   rc   mainr   s
             r8   r  CompilePackage.initialize  s    	-$$$$ #)"-!!---f&78888$$(?(?(AA"STZTiTiSjk  ##u'8'88"TU[UiUiTjk  *"22D!//<A04D4DdooVH==0*?}GTXTdTdSeemnrn}n}m~~  A 	 3 )/(>(>%!<<LD--66=DKOSN99$:J:JKL  ""++T-?-?-J-J !rT   rm   rn   function_namerq   rr   c                 N   XR                   ;  a2  [        [        R                  U5      U/ / 0 / UUS9nX`R                   U'   OEU R                   U   nUR                  U:X  d   eUR
                  U:X  d   eUR                  U:X  d   eUb  UR                  R                  U5        g g )N)rm   rn   ro   rg   rp   rh   rq   rr   )	r  rk   r   r3   rn   rr   rq   ro   append)r   rm   rn   r  rq   rr   r+   s          r8   r  CompilePackage._add_function  s     kk)(*;;KH+! !'"3	D (,KK$;;{+D%%666))->>>>##{222$&&}5 %rT   c                     U R                   $ r/   )r  r   s    r8   cached_backendsCompilePackage.cached_backends  s    $$$rT   c                 ^    U R                   c   e[        R                  U R                   5      $ r/   )r   r   source_id_from_fnr   s    r8   	source_idCompilePackage.source_id  s+    !!---//0B0BCCrT   r+   c                     [        U5      u  p#[        R                  " U5      nUc  [        SU 35      eU R	                  UUR
                  [        U5      US9  g )Nr   )r  rq   )r   r   r   r   r  rH   r\   )r   r+   r  rq   r`   s        r8   _add_user_function!CompilePackage._add_user_function  s_    %5d%;"""4(>!=dVDEEOO%m4#	 	 	
rT   )NNNc              #   X  #    U R                   b   eXR                  ;  a  U R                  U5        U R                  U   nX l          S v   UR                  (       a  U R                  U	 SUl        S U l         g ! UR                  (       a  U R                  U	 SUl        S U l         f = f7fNT)r  r  r'  rt   rs   )r   r+   rw   s      r8   code_contextCompilePackage.code_context  s     ""*** {{"##D)D!#	' KK%#'E "&D	 KK%#'E "&Ds   AB*A9 -B*9.B''B*rX   rY   c                     U R                   c   eU R                   R                  (       a  g [        U[        R	                  U5      S9nU R                   R
                  R                  U5        g )N)rX   rY   )r  rt   rV   r   r3   rg   r  )r   rX   rY   guarded_code_entrys       r8   add_guarded_codeCompilePackage.add_guarded_code  sa    
 ""...''3%&77D
 	))001CDrT   sourcesc                    U R                   c   eU R                   R                  (       a  g U H  nX R                  ;   a  M  [        R                  " U5      nUc  M/  [        R
                  " U5      u  pEU[        U5      -   nSR                  U5      nUSR                  [        X5U5      5      :X  d   eU R                  R                  [        UR                  UU[        U5      S95        M     g )Nr   )r`   ra   rb   rc   )r  rt   r  r   r   r   r}   r   r   r  r   r^   rH   r   )r   r1  r+   r`   sourcelinesra   rb   r   s           r8   add_inlined_source!CompilePackage.add_inlined_source  s    ""...''D)))&&t,F~'.'='=d'C$K$s;'77JWW[)FRWW%5f:%VWWWW!!%%!?? +))&1	 rT   c                 D    U R                   c   eSU R                   l        g r*  )r  rt   r   s    r8   bypass_current_entry#CompilePackage.bypass_current_entry  s"    ""...'+$rT   c                     U R                  UUU(       a  [        U5      OS SS9  U R                  R                  U5        g )NT)r  rr   )r  r\   r  r   )r   rm   rn   r  s       r8   add_resume_function"CompilePackage.add_resume_function  sB     	8E+m44"	 	 	
 	{+rT   aliasmodule_namec                 R    U R                   c   eX R                   R                  U'   g r/   )r  rp   )r   r<  r=  s      r8   add_import_source CompilePackage.add_import_source+  s(    ""...4?**51rT   r   backendc                     U R                   c   eUR                  S5      (       d   e[        U5      nU R                   R                  R	                  U5        Ub  X R
                  U'   g g )N__compiled_fn_)r  
startswithr[   rh   r  r  )r   r   rA  s      r8   add_backend_idCompilePackage.add_backend_id/  si    ""...$$%56666
+
''..z:07!!*- rT   c                     U R                   b   eU R                  c   eU R                  (       d   e[        [	        U R
                  5      5      U R                  R                  L d   eg r/   )r  r   r  nextiterr  r   r   s    r8   r
  CompilePackage.validate7  s]    ""***!!---    D%&$*<*<*E*EEEErT   r`   r   r   c                 v    X1R                   U'   U R                  R                  U/ 5      R                  U5        g r/   )r   r  
setdefaultr  )r   r`   r   r   s       r8   _install_globalCompilePackage._install_global=  s/     %**626==dCrT   c                    SSK Jn  U R                  c   eU R                  R	                  5        H)  u  p#U H  nUR
                  R                  U5        M      M+     0 U l        U" U R                  R                  5        g )Nr   )_reset_precompile_entries)torch._C._dynamo.eval_framerP  r   r  r   r   popr   )r   rP  r`   namesr   s        r8   r	  CompilePackage.uninstallA  sl    I!!---!44::<MF##D)  = #%!$"4"4"="=>rT   backendsc                    SSK Jn  SSKJn  U R	                  5         U R
                  R                  5        GH  u  pEUR                  (       a  [        U5      O[        R                  " 5       nU   [        R                  UR                     nUR                  R                  5        H+  u  pU R                  Xx[         R"                  " U	5      5        M-     Un
UR$                  (       aF  UR&                   H6  n[(        R*                  " XGR,                  U5      nU R                  X{U5        M8     UR.                  (       a  [1        U5      n
UR2                   Hl  nX;  a  [5        SU S35      e[7        SSS	9   X   R9                  5       nU R                  UU[:        R<                  R?                  U5      5        S
S
S
5        Mn     [A        URB                  5      S:X  a)  [:        R<                  RD                  RG                  U
5        URB                   GH*  n[H        RJ                  " URL                  5      n[        R                  UR                     R,                  nUR                  RN                  =n(       a  U" U5      nUU;   a  UU   UL d   eOUUU'   [Q        U[:        R<                  RR                  RT                  5      (       d   e[:        R<                  RR                  RW                  U
UR                  URX                  US9nU" U
URZ                  [\        R_                  UR`                  5      5        GM-     S
S
S
5        GM     g
! , (       d  f       GM  = f! , (       d  f       GM?  = f)a  
Sync the package states to the compiled function. This includes the following actions:
  1. Clean up the previously installed states.
  2. Install the compiled functions to global scopes.
  3. Install the precompiled cache entries to ExtraStates on the code object.
r   )_load_precompile_entryr   )get_builtins_dictBackend # is not found in the given backendsr   backend_compile)r   N)shape_code_partsruntime_global_scope)1rQ  rW  output_graphrX  r	  r  r   rs   r   r   nullcontextr~   r   rn   rp   rM  r  r  rr   ro   r1   FunctionTyper   rq   r   rh   r  r   r   r   r   disabler}   rg   r  	skip_coder   r   rX   %name_of_builtins_dict_key_in_fglobalsr0   guardsGuardsStateCheckFunctionManagerr\  guard_managerr   rD   rY   )r   rU  rW  rX  r+   rw   contextr`   r<  r=  target_coder  r   r   rA  guarded_coderX   r]  builtin_dict_namebuiltins_dictcheck_fn_managers                        r8   installCompilePackage.installM  s    	G3;;,,.KD '' 't,++- 
 U%8%89*/*>*>*D*D*F&E((y'>'>{'K +G #**).)=)="//oo}U,,VBG *> $$".u"5K"'"3"3J!1*&zl2UV  &/<M #+"6"L"L"N,,"&!MM11':	  #4 u**+q0 MM,,66{C$)$7$7L#)<<0I0I#JL+.;;u7J7J+K+T+T(
 (44ZZ[) [ )::N(O,0DD 45F G= X X GT01BC%lEMM4H4H4T4TUUUU',}}';';'P'P#$11)5)F)F-A	 (Q ($ +#(66&55l6N6NO/ %8E  /2 ' s'   0D	M&9AM	<F	M&
M#	M&&
M6	c                     U R                  5         [        [        U R                  R	                  5       5      U R
                  S9$ )N)r   r   )r
  r   ri   r  rF   r  r   s    r8   cache_entryCompilePackage.cache_entry  s5     t{{))+,d>S>S
 	
rT   c                 2   SSK Jn  U" U 5      n[        R                  " 5       nUR	                  UR
                  R                  5       5        UR	                  [        UR                  R                  5      R                  5       5        UR                  5       $ )Nr   r  )r  r  r   r   r   rJ   r   rO   r   r$   r   )r   r  innermost_fn_r   s       r8   r#   CompilePackage.source_id_from_fn  so    ,$R(nn&=55<<>?3}55DDELLNO$$&&rT   )r  r  r  r  r  r   r  r  r   )NNFr,   Nr/   )/rH   rI   rJ   rK   rZ   r   r   r   r   rv   r  r  r  r1   r2   rO   r\   r  r   ru   r[   r   rQ   cached_propertyr$  r'  r   r   r   r+  rN   r/  ri   r4  r7  r:  r?  rE  r
  
ModuleTyperM  r	  rn  rq  r   r#  rS   r;   rT   r8   r   r   \  s   
 /3',	Xc3h'( *+ !%	
 
,! ! /3',	)!)! *+)! !%	)!
 
)!^ 04%)"'6^^6 6  ,	6
 c]6  6 
6: %j#o!6 % % D3 D D

u~~ 

$ 

 ' 'I>N4O ' '(EE ^^E 
	E$u~~*> 4 .,,^^, ,  }	,
 
,@s @ @ @8 8x} 8PT 8FDe&6&6 Dc D# DRV D
?OZ_ 5 O$ Ob
. 
 'hsCx0 'S ' 'rT   r   c                   :    \ rS rSr\S\4S j5       rS\4S jrSr	g)EagerCacheArtifacti  r,   c                      g)Nprecompile_eagerr;   r;   rT   r8   r   EagerCacheArtifact.type  s    !rT   c                 B    [         R                  " U R                  5      $ r/   r   r   s    r8   r   (EagerCacheArtifact.after_deserialization  r   rT   r;   N)
rH   rI   rJ   rK   r   rO   r   r   r   rS   r;   rT   r8   rz  rz    s'    "# " "*s *rT   rz  c            
       V   \ rS rSrSrS\SS4S jrS\S\SS4S	 jr	\
R                  SS
 j5       r\
R                  S\S\S\SS4S j5       rS\S\SS4S jrS\S\SS4S jr\
R                  S\S\\\4   4S j5       rS\S\\\\\4   4   4S jrS\S\S\\\\\4   4   4S jrSrg)DynamoStorei  z
A DynamoStore tracks active CompilePackages, and provides methods to store and retrieve them.

This is an abstract base class for different storage implementations.
packager,   Nc                     UR                  5       n[        R                  " U5      n[        R                  " [
        R                  5       UR                  US9  g)zM
Records a package to PrecompileContext, so that it can be serialized later.
r>   r   N)rq  r   dumpsr   record_artifactr   r   r$  )r   r  rq  pickled_results       r8   record_packageDynamoStore.record_package  sD     ))+k2)) %%'W->->	
rT   r   rA  c                 ~    [         R                  " U5      n[        R                  " [        R                  5       XS9  g)zD
Records eager fx graphs to PrecompileContext for testing purposes.
r  N)r   r  r   r  rz  r   )r   r   rA  r  s       r8   record_eager_backend DynamoStore.record_eager_backend  s.      g.))##%:	
rT   c                     g r/   r;   r   s    r8   clearDynamoStore.clear  s     rT   r   rU  pathc                     g)z
Abstract method to write dynamo cache entry and backends to storage.

Args:
    dynamo: The dynamo cache entry to write
    backends: Dictionary of backend content to write
    path: Path or key to identify where to write the data
Nr;   r   r   rU  r  s       r8   writeDynamoStore.write  s     	rT   rq  r>   c                     0 nUR                    HF  n[        R                  " U5      nUc  [        SU S35      e[	        U[
        5      (       d   eXSU'   MH     U R                  XU5        g)I
Saves a package to a given path. Grabs backends from PrecompileContext.
NrY  rZ  )rh   r   serialize_artifact_by_keyr  r0   r   r  )r   rq  r>   backend_contentr   serialized_backends         r8   save_cache_entryDynamoStore.save_cache_entry  sy     &(%11J!2!L!LZ!X!)"zl*MN  02IJJJJ*<J' 2 	

;5rT   c                 h    U R                  U5        UR                  5       nU R                  X25        g)r  N)r  rq  r  )r   r  r>   rq  s       r8   save_packageDynamoStore.save_package  s.     	G$))+k/rT   c                     g)z
Abstract method to read dynamo cache entry and backends from storage.

Args:
    path: Path or key to identify where to read the data from

Returns:
    A tuple containing (dynamo_cache_entry, backend_content)
Nr;   r   r  s     r8   readDynamoStore.read  s     	rT   c                     U R                  U5      u  p#UR                  5        HA  u  pE[        R                  " UR	                  5       UR
                  UR                  S9  XSU'   MC     X#4$ )Nr  )r  r   r   r  r   r>   r   )r   r>   rq  r  r   rA  s         r8   load_cache_entryDynamoStore.load_cache_entry  sb     (,yy~$#2#8#8#:J--GKK +2J'	 $; ++rT   r   c                 D    U R                  U5      u  p4[        X5      nXT4$ )W
Loads a package from a given path and returns it plus a list of deserialized backends
)r  r   )r   r   r>   rq  r  r  s         r8   load_packageDynamoStore.load_package  s*     (,'<'<S'A$ 1''rT   r;   rv  )rH   rI   rJ   rK   rZ   r   r  r[   r   r  abcabstractmethodr  r   	_BackendsrO   r  r  r  r=   r  ru   r  r  rS   r;   rT   r8   r  r    sI   
n 
 

z 
C 
D 
 	   !  	
 
  6,= 6C 6D 6 0N 0 0 0 	
 
'8)'C!D 
 

,
,	 $z3"77	8
,(((	~tJO44	5(rT   r  c                   b    \ rS rSrSrSS jrSS jrS\S\S	\	SS4S
 jr
S	\	S\\\4   4S jrSrg)InMemoryDynamoStorei'  zP
A DynamoStore implementation that keeps state about CompilePackages in memory.
r,   Nc                     0 U l         g r/   packagesr   s    r8   r  InMemoryDynamoStore.__init__,  s	    HJrT   c                 8    U R                   R                  5         g r/   )r  r  r   s    r8   r  InMemoryDynamoStore.clear/  s    rT   r   rU  r  c                 $    X4U R                   U'   g)zQ
Store the dynamo cache entry and backends in memory instead of writing to disk.
Nr  r  s       r8   r  InMemoryDynamoStore.write2  s      &0drT   c                 Z    XR                   ;  a  [        SU 35      eU R                   U   $ )z3
Read dynamo cache entry and backends from memory.
zNo package found with key )r  r  r  s     r8   r  InMemoryDynamoStore.read=  s0     }}$!;D6BCC}}T""rT   r  rv  )rH   rI   rJ   rK   rZ   r  r  r   r  rO   r  r=   r  rS   r;   rT   r8   r  r  '  s[    K	1!	1 	1 		1
 
	1# #'8)'C!D #rT   r  c                   j    \ rS rSrSrSS\4S jjrSS jrS\S	\	S
\SS4S jr
S
\S\\\	4   4S jrSrg)DiskDynamoStoreiG  zN
A DynamoStore implementation that keeps state about CompilePackages on disk.
path_prefixc                     Xl         g)z
Initialize a DiskDynamoStore with a path prefix.

Args:
    path_prefix: Prefix directory for where to put CompilePackages on disk
Nr  )r   r  s     r8   r  DiskDynamoStore.__init__L  s
     'rT   r,   Nc                 f    U R                   (       a   [        R                  " U R                   SS9  gg)z&
Clear all CompilePackages from disk.
T)ignore_errorsN)r  shutilrmtreer   s    r8   r  DiskDynamoStore.clearU  s&     MM$**$? rT   r   rU  r  c                 Z   U R                   (       a*  [        R                  R                  U R                   U5      OUn [        R                  " USS9  [        [        R                  R                  US5      S5       n[        R                  " X5        SSS5        [        [        R                  R                  US5      S5       n[        R                  " X%5        SSS5        g! , (       d  f       NX= f! , (       d  f       g= f! [         a  n[        SU SU 35      UeSnAff = f)	z0
Write dynamo cache entry and backends to disk.
T)exist_okr   wbNrU  zFailed to save package to : )
r  osr  r   makedirsopenr   dump	Exceptionr  )r   r   rU  r  dynamo_pathbackend_pathes          r8   r  DiskDynamoStore.write\  s     8<7G7Grww||D,,d3T	PKKt,bggll42D9[F0 :bggll44d;|H3 <; :9;; 	P!;D6A3GHaO	PsM   ?D	 >C'2D	 C8D	 '
C51D	 8
DD	 D	 	
D*D%%D*c                 6   U R                   (       a*  [        R                  R                  U R                   U5      OUn [	        [        R                  R                  US5      S5       n[
        R                  " U5      nSSS5        [	        [        R                  R                  US5      S5       n[
        R                  " U5      nSSS5        WW4$ ! , (       d  f       N[= f! , (       d  f       N#= f! [         a  n[        SU SU 35      UeSnAff = f)z1
Read dynamo cache entry and backends from disk.
r   rbNrU  z!Failed to load package from path r  )	r  r  r  r   r  r   loadr  r  )r   r  r  rq  r  r  r  s          r8   r  DiskDynamoStore.reado  s     8<7G7Grww||D,,d3T	Wbggll42D9[$kk+6 :bggll44d;|"(++l"; <//	 :9;;  	W!B4&1#NOUVV	WsG   *C7 )C 2C7 2C&	C7 
C#C7 &
C40C7 7
DDDr  )r   rv  )rH   rI   rJ   rK   rZ   rO   r  r  r   r  r  r=   r  rS   r;   rT   r8   r  r  G  sj    'C '@P!P P 	P
 
P&W W'8)'C!D WrT   r  c            	          ^  \ rS rSrSrS\SS4U 4S jjrS\S\4   S\	\
\\\\4   4      4U 4S	 jjrS\S\4   S\	\   4S
 jrSrU =r$ )DiskDynamoCachei~  zg
Special DiskDynamoStore which adds some helper functions for automatically
tracking paths of packages
r  r,   Nc                 |   > UR                   n[        R                  SUR                   5        [        TU ]  X5        g)r  zSaving CompilePackage for %sN)r$  loggerinfosuperr  )r   r  r>   	__class__s      r8   saveDiskDynamoCache.save  s2     2G4E4EFW*rT   r   .c                   > [         R                  U5      n[        R                  SU5        [        R
                  R                  U R                  U5      n[        R
                  R                  U5      (       a   [        TU ])  U5      nU$ [        R                  SU5        g! [         a*  n[        R                  SU[        U5      5         SnAgSnAff = f)r  zLoading CompilePackage for %sz'Failed to load package from path %s: %sNzNo package found for %s)r   r#  r  r  r  r  r   r  existsr  r  r  warningrO   )r   r   r>   r  resultr  r  s         r8   r  DiskDynamoCache.load  s     ..r23S9ww||D,,c277>>$1#6 	-s3	  H$PSTUPVWs   <B$ $
C. CCc                 p    U R                  U5      nUc  gUu  p4[        X5      nUR                  U5        U$ )z3
Load directly into a package and install backends
N)r  r   rn  )r   r   resultsrw   rU  r  s         r8   load_and_install_package(DiskDynamoCache.load_and_install_package  s<     ))B-? 'U$R/GOOH%NrT   r;   )rH   rI   rJ   rK   rZ   r   r  r   r   r   r=   r   ru   r[   r  r  rS   __classcell__)r  s   @r8   r  r  ~  sz    
+N +t +38$	%)4
C+@@A	B&38$	.	! rT   r  r   )RrZ   r  r   r   dataclassesrQ   r   r  r   loggingr  r   r   r  r~   r1   collections.abcr   r   typingr   r   r   r   typing_extensionsr	   r   torch._inductor.packagetorch._dynamo.excr    torch._dynamo.precompile_contextr   r   'torch._inductor.runtime.cache_dir_utilsr   torch.compiler._cacher   bytecode_transformationr   r   r   r   	getLoggerrH   r  	dataclassr   rV   rO   r[   r\   r^   re   rk   r2   r   r   r=   r   r   registerr   r   rx  rL   ri   r   r   AbstractContextManagerr   r   rz  ru   r  ABCr  r  r  r  r  r   DynamoCacher;   rT   r8   <module>r     s    
        	    
  / 3 3 #   * W = 6 2 0 
		8	$ d#)
 )
 $)
X 	  	  	  \3'
mS) d#  $ " " " /  >- %.. 0%..   U95>> U9eCHo U9p V V V *23DE * *# # #JJ&)J7:J	#YJO)) O O OQT O!
..!&&t,!HQ' Q'h
 *05 * * 4S99:	g(#'' g(T#+ #@4Wk 4Wn.o .b bggll9;ABrT   