
    i              	       "   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
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mZ ddlmZmZmZmZ ddlmZ ddlZddlZddlmZ ddlmZm Z  ddl!m"Z" dd	l#m$Z$ d
dl%m&Z& d
dl'm(Z(m)Z)  e	jT                  e+      Z, ejZ                  d       G d d             Z.ejZ                   G d d             Z/ ede0      Z1 ede0      Z2 ejZ                  d       G d d             Z3ejZ                   G d d             Z4ejZ                   G d de4             Z5de5dejl                  fdZ7dejl                  dedefd Z8dejl                  de9e0e0f   fd!Z:ejZ                   G d" d#             Z;e$jx                   G d$ d%ee;                Z=d&e0de0fd'Z>d(ej~                  d)e@d*e@deAe0   fd+ZBd(ej~                  d)e@d*e@de0fd,ZCdejl                  dej                  d   fd-ZE G d. d/      ZFe$jx                   G d0 d1ee                ZGeHe1ee   f   ZI G d2 d3ej                        ZK G d4 d5eK      ZL G d6 d7eK      ZM G d8 d9eM      ZN eNe
j                  j                   e"       d:            ZQy);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                      e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   eed<   eedf   ed	<   eedf   ed
<   eedf   ed<   eed<   eed<   eed<   eedf   ed<   eedf   ed<   dZ	e
e   ed<   dZe
e   ed<   dZe
e   ed<   dZe
e   ed<   eej                   dej$                  dd fd              Zeej                   dd dej$                  fd              Zy)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                      t               D ci c]  }|t        ||       }}t         fd|d   D              |d<     di |S c c}w )Nc              3   x   K   | ]1  }t        |t        j                        rj                  |      n| 3 y wN)
isinstancetypesCodeTypefrom_code_object.0cclss     O/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/package.py	<genexpr>z2SerializedCode.from_code_object.<locals>.<genexpr>F   s6      $
 (2!U^^'DC  #!K$
s   7:r    )r   getattrtuple)r6   r*   keykwargss   `   r7   r2   zSerializedCode.from_code_objectB   s\     6C_Ec#wtS))EE# $
K($
 
{ }V} Fs   Aserialized_codec                      t               D ci c]  }|t        ||       }}t         fd|d   D              |d<   t        j                  |j                          S c c}w )Nc              3   d   K   | ]'  }t        |t              rj                  |      n| ) y wr.   )r/   r   to_code_objectr3   s     r7   r8   z0SerializedCode.to_code_object.<locals>.<genexpr>P   s2      $
 &0>%BCq!I$
s   -0r   )r   r:   r;   r0   r1   values)r6   r>   r<   r=   s   `   r7   rA   zSerializedCode.to_code_objectL   sk     ANP#w44PP# $
K($
 
{ ~~]]_
 	
 Qs   A)__name__
__module____qualname__int__annotations__bytesr;   r   strr&   r   r'   r(   r)   classmethod	functoolscacher0   r1   r2   rA   r9       r7   r   r   ,   s   OMNS#XCHosCx LsCx sCx $(L(5/(!%K#%)-x-#Ix}#__ENN 7G    __
-= 
%.. 
  
rM   r   c                   &    e Zd ZU dZeed<   eed<   y)_GuardedCodeCacheEntrya  
    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_codeN)rC   rD   rE   __doc__rH   rG   r   r9   rM   r7   rO   rO   Y   s     rM   rO   
_BackendId_FunctionIdc                   6    e Zd ZU eed<   eed<   eed<   eed<   y)InlinedSourcemodulefirstlineno
lastlinenochecksumN)rC   rD   rE   rI   rG   rF   r9   rM   r7   rV   rV   j   s    KOMrM   rV   c                   2    e Zd ZU dZee   ed<   ee   ed<   y)DynamoCaptureOutputzD
    Core information generated from Dynamo for fullgraph=True.
    guarded_codesbackend_idsN)rC   rD   rE   rR   listrO   rG   rS   r9   rM   r7   r\   r\   r   s      .//j!!rM   r\   c                       e Zd ZU dZeed<   eed<   ee   ed<   e	eef   ed<   e
e   ed<   eed<   dZeed	<   dZeed
<   y)_DynamoCodeCacheEntrya  
    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bypassedN)rC   rD   rE   rR   r   rG   rI   r_   rT   dictr   boolrh   ri   r9   rM   r7   ra   ra   |   sR    (  %%cN"# ND HdrM   ra   entryr+   c                    t        | j                        dk(  sJ t        j                  | j                     }| j                  d   j                  d      }|D ]  }t        ||      } | j                  r| j                  j                  d      }|D ]  }|j                  d      rg|j                  d      }t        |t              r|dk\  sJ t        ||d | d       }|t        d|        |t        j                  ||dz   d          }{t        ||      } nt        d|        t        |t        j                         sJ |S )Nr   r   .][z"Cannot find source for code entry )lenrd   sysmodulesrc   splitr:   rf   endswithrfindr/   rF   r   astliteral_evalr0   r1   )rl   fnpartspartindex_beginattrs         r7   _lookup_coder      sJ   u##$)))kk%--.B  #))#.E R!!'', 		'D}}S!"jjo!+s3q8HHHr4#5t<<&)KE7'STT#**4a"+EFGR&		' ?wGHHb%..)))IrM   r*   scopec                 $    t        d|  d|       )Nz*Cannot resolve a fully qualified name for z. Lookup scope: r
   )r*   r   s     r7   _raise_resolution_errorr      s     

4TF:J5'R rM   c                     t        j                         }|t        d        |t        j                  dk\  r] j
                  j                  d      }|D ]=  }t        |      st                t        |      t        j                        s= n t               dt        dt        t           f fd       }|t                j                  |j!                  d      fS )aS  
    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       rn   objr+   c                 f   | 
v ry 
j                  |        t        j                  |       r6| 	u ryt        | j                        D ]  \  }} |      x}d| d| c S  t        j
                  |       r | j                        x}| d| S | j                  kt        | j                        D ]S  \  }}	 |j                  }t        j
                  |      st        j                  |      s> |      x}I| d| d| c S  t        j                  dk  rt        j                  |       rX| j                  j                         D ];  }t        j
                  |      st        j                  |      s. |      x}9|c S  t        j                  |       r| j                  j!                         D ]e  \  }}t#        | |      }t        j
                  |      st        j                  |      s= |      x}H|j$                  |k7  rt'        	       |c S  y # t        $ r Y {w xY w)N z.co_consts[ro   z	.__code__z.__closure__[z].cell_contentsr   )addinspectiscode	enumerater   
isfunction__code____closure__cell_contents
ValueErrorrs   version_infoismodule__dict__rB   isclassitemsr:   rC   r   )r   iconstrescellr   valuename_find_code_sourcer*   seentoplevels           r7   r   z+_get_code_source.<locals>._find_code_source   s)    $;>>#d{%cmm4 35,U33C@(1SE223 c"(66C"3%((*(9 GGAt!(,(:(:  **=9">>-8 0??L#&!.qcFFG g%$ \\002 #E#..u59O 077D"
	# s##&<<#5#5#7 #KD%#C.E#..u59O 077D >>T13D(C"
# 9 & ! !s   ;H##	H0/H0)r   	getmoduler   rs   r   r'   ru   hasattrr   r:   r   setr   r   rI   rE   strip)r*   rW   r{   r|   rf   r   r   r   s   `    @@@r7   _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8rM   c                       e Zd ZU ee   ed<   ee   ed<    ej                         Z	e
ed<   ej                  Ze
ed<   edee   fd       Zy)_DynamoCacheEntrycodesinlined_sourcespython_versiontorch_versionr+   c                 h    | j                   D ch c]  }|j                  D ]  }|  c}}S c c}}w r.   )r   r^   )selfr*   
backend_ids      r7   r^   z_DynamoCacheEntry.backend_ids  s,    '+zzUtDDTDTUj
U
UUUs   .N)rC   rD   rE   r_   ra   rG   r   rV   platformr   rI   torch__version__r   propertyrS   r^   r9   rM   r7   r   r     s^    %&&''1(113NC3**M3*VS_ V VrM   r   c                   .    e Zd Zedefd       ZdefdZy)_DynamoCacheArtifactr+   c                       y)Nprecompile_dynamor9   r9   rM   r7   typez_DynamoCacheArtifact.type   s    "rM   c                 @    t        j                  | j                        S r.   pickleloadscontentr   s    r7   after_deserializationz*_DynamoCacheArtifact.after_deserialization$      ||DLL))rM   N)rC   rD   rE   staticmethodrI   r   r   r   r9   rM   r7   r   r     s(    ## # #*'8 *rM   r   sourcec                     t        j                         }|j                  | j                                |j	                         S r.   )hashlibsha256updateencode	hexdigest)r   sha256_hashs     r7   _hash_sourcer   (  s1    .."Kv}}'  ""rM   mrX   rY   c                 D    t        j                  |       d   |dz
  |dz
   S )Nr   r   )r   getsourcelinesr   rX   rY   s      r7   _get_sourcelinesr   .  s)     !!!$Q'a*q.IIrM   c           	      L    t        dj                  t        | ||                  S )Nr   )r   joinr   r   s      r7   _hash_sourcelinesr   4  s      0K LMNNrM   c                      ddl mm ddlmm t        j                  dt        d    f fd       } |       S )Nr   )get_compile_idlog_dynamo_start)compile_contextCompileContextr+   c               3   v  K   t                 i       }   |             5  t        dddt        t        j                  j
                  j                        j                  j                  j                  d      5          d  d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY ww)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   rI   r   _dynamoutils
curr_framer"   r!   r#   )
compile_idr   r*   r   r   r   s    r7   _ctxz$_compile_frame_context.<locals>._ctxC  s     #3
N:67	(1)L "%U]]%8%8%C%C!D#||#'#3#3&*&9&9		 T"!	 	 	 	 	 	s5   "B9AB-B!B-	B9!B*	&B--B62B9)	torch._dynamo.convert_framer   r   torch._guardsr   r   
contextlibcontextmanagerr   )r*   r   r   r   r   r   s   ` @@@@r7   _compile_frame_contextr   8  s?     M= (4.   * 6MrM   c                      e Zd ZdZ	 	 d.deedef      dee   deddfdZ	defd	Z
	 	 d.dedee   deddfd
Z	 	 	 d/dej                  dedee   dee   deddfdZedeeef   fd       Zej,                  defd       Zdej                  ddfdZej4                  dej                  ded   fd       Zdedej                  ddfdZdeej                     ddfdZ d0dZ!dej                  dedee   ddfdZ"dededdfd Z#d1d!ed"ee   ddfd#Z$d0d$Z%d%ejL                  d&ed'eddfd(Z'd0d)Z(d*eeef   ddfd+Z)defd,Z*e+dedef   defd-       Z,y)2CompilePackagea  
    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.
    Nrz   .dynamoignore_inlined_sourcesr+   c                     d | _         i | _        d | _        i | _        i | _        t               | _        t               | _        d| _        |4| j                  |||       | j                          | j                          y y NF)_innermost_fn_codes_current_entry_installed_globals_cached_backendsr   _inlined_sources_resume_codes_initialized
initialize	uninstallvalidate)r   rz   r   r   s       r7   __init__zCompilePackage.__init__i  sw     "CE?CEG 8:47E25%!>OOB(>?NNMMO rM   c                     | j                   S r.   )r   r   s    r7   is_initializedzCompilePackage.is_initialized  s       rM   c           
          ddl m} | j                  rJ t               | _         ||      | _        | j
                  J |t        |t              sJ |j                  t        j                         k7  rt        d|j                         |j                  t        j                  k7  rt        d|j                         |s|j                  D ]  }t        j                   |j"                        }t%        ||j&                  |j(                        }||j*                  k7  sSt        d|j"                   d|j&                   d|j(                   d       |j                  | _        |j,                  ^}}	| j
                  j.                  |i| _        |	D ].  }|| j0                  t2        j5                  |j6                        <   0 d	| _        y | j9                  | j
                  j.                  | j
                  j:                         d	| _        y )
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   r/   r   r   r   RuntimeErrorr   r   r   r   	importlibimport_modulerW   r   rX   rY   rZ   r   r   r   r   rA   rb   _add_functionrD   )
r   rz   r   r   r   r*   r   rZ   mainr   s
             r7   r   zCompilePackage.initialize  s    	-$$$$ #)"-!!---f&7888$$(?(?(AA"STZTiTiSjk  ##u'8'88"TU[UiUiTjk  *"22 D!//<A0D4D4DdooVH4==0*?}GTXTdTdSeemnrn}n}m~~  A 	 )/(>(>%!<<LD5--66=DK TOSN99$:J:JKLT ! ""++T-?-?-J-J !rM   rb   rc   function_namerf   rg   c           
      L   || j                   vr6t        t        j                  |      |g g i g ||      }|| j                   |<   nB| j                   |   }|j                  |k(  sJ |j
                  |k(  sJ |j                  |k(  sJ ||j                  j                  |       y y )N)rb   rc   rd   r]   re   r^   rf   rg   )	r   ra   r   r2   rc   rg   rf   rd   append)r   rb   rc   r  rf   rg   r*   s          r7   r  zCompilePackage._add_function  s     dkk)(*;;KH+! !'"3	D (,DKK$;;{+D%%666))->>>>##{222$&&}5 %rM   c                     | j                   S r.   )r   r   s    r7   cached_backendszCompilePackage.cached_backends  s    $$$rM   c                 \    | j                   J t        j                  | j                         S r.   )r   r   source_id_from_fnr   s    r7   	source_idzCompilePackage.source_id  s+    !!---//0B0BCCrM   r*   c                     t        |      \  }}t        j                  |      }|t        d|       | j	                  ||j
                  t        |      |       y )Nr   )r  rf   )r   r   r   r   r  rC   rT   )r   r*   r  rf   rW   s        r7   _add_user_functionz!CompilePackage._add_user_function  sa    %5d%;"{""4(>!=dVDEEOO%m4#	 	 	
rM   )NNNc              #   B  K   | j                   J || j                  vr| j                  |       | j                  |   }|| _         	 d  |j                  r| j                  |= d|_        d | _         y # |j                  r| j                  |= d|_        d | _         w xY wwNT)r   r   r  ri   rh   )r   r*   rl   s      r7   code_contextzCompilePackage.code_context  s     ""*** t{{"##D)D!#	' KK%#'E "&D	 KK%#'E "&Ds   ABA3 (B3)BBrP   rQ   c                     | j                   J | j                   j                  ry t        |t        j	                  |            }| j                   j
                  j                  |       y )N)rP   rQ   )r   ri   rO   r   r2   r]   r
  )r   rP   rQ   guarded_code_entrys       r7   add_guarded_codezCompilePackage.add_guarded_code  sa    
 ""...''3%&77D
 	))001CDrM   sourcesc                    | j                   J | j                   j                  ry |D ]  }|| j                  v rt        j                  |      }|*t        j
                  |      \  }}|t        |      z   }dj                  |      }|dj                  t        |||            k(  sJ | j                  j                  t        |j                  ||t        |                    y )Nr   )rW   rX   rY   rZ   )r   ri   r   r   r   r   rr   r   r   r   r   rV   rC   r   )r   r  r*   rW   sourcelinesrX   rY   r   s           r7   add_inlined_sourcez!CompilePackage.add_inlined_source  s    ""...'' 	Dt)))&&t,F~'.'='=d'C$K$s;'77JWW[)FRWW%5fk:%VWWWW!!%%!?? +))&1		rM   c                 B    | j                   J d| j                   _        y r  )r   ri   r   s    r7   bypass_current_entryz#CompilePackage.bypass_current_entry  s"    ""...'+$rM   c                 ~    | j                  |||rt        |      nd d       | j                  j                  |       y )NT)r  rg   )r  rT   r   r   )r   rb   rc   r  s       r7   add_resume_functionz"CompilePackage.add_resume_function  sB     	8E+m44"	 	 	
 	{+rM   aliasmodule_namec                 R    | j                   J || j                   j                  |<   y r.   )r   re   )r   r   r!  s      r7   add_import_sourcez CompilePackage.add_import_source+  s*    ""...4?**51rM   r   backendc                     | j                   J |j                  d      sJ t        |      }| j                   j                  j	                  |       ||| j
                  |<   y y )N__compiled_fn_)r   
startswithrS   r^   r
  r   )r   r   r$  s      r7   add_backend_idzCompilePackage.add_backend_id/  sh    ""...$$%5666
+
''..z:07D!!*- rM   c                     | j                   J | j                  J | j                  sJ t        t	        | j
                              | j                  j                  u sJ y r.   )r   r   r   nextiterr   r   r   s    r7   r   zCompilePackage.validate7  s]    ""***!!---    D%&$*<*<*E*EEEErM   rW   r   r   c                 x    ||j                   |<   | j                  j                  |g       j                  |       y r.   )r   r   
setdefaultr
  )r   rW   r   r   s       r7   _install_globalzCompilePackage._install_global=  s1     %**626==dCrM   c                     ddl m} | j                  J | j                  j	                         D ]'  \  }}|D ]  }|j
                  j                  |        ) i | _         || j                  j                         y )Nr   )_reset_precompile_entries)torch._C._dynamo.eval_framer0  r   r   r   r   popr   )r   r0  rW   namesr   s        r7   r   zCompilePackage.uninstallA  sx    I!!---!44::< 	*MFE *##D)*	* #%!$"4"4"="=>rM   backendsc                    ddl m} ddlm} | j	                          | j
                  j                         D ]  \  }}|j                  rt        |      nt        j                         }|5  t        j                  |j                     }|j                  j                         D ]+  \  }}	| j                  ||t!        j"                  |	             - |}
|j$                  rE|j&                  D ]6  }t)        j*                  ||j,                  |      }| j                  |||       8 |j.                  rt1        |      }
|j2                  D ]n  }||vrt5        d| d      t7        dd	      5  ||   j9                         }| j                  ||t:        j<                  j?                  |             d
d
d
       p tA        |jB                        dk(  r)t:        j<                  jD                  jG                  |
       |jB                  D ]  }tI        jJ                  |jL                        }t        j                  |j                     j,                  }|j                  jN                  x}r ||      }||v r	||   |u sJ |||<   tQ        |t:        j<                  jR                  jT                        sJ t:        j<                  jR                  jW                  |
|j                  |jX                  |      } ||
|jZ                  t\        j_                  |j`                                 	 d
d
d
        y
# 1 sw Y   xY w# 1 sw Y   xY w)a3  
        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)1r1  r6  output_graphr7  r   r   r   rh   r   r   nullcontextrs   rt   rc   re   r.  r  r  rg   rd   r0   FunctionTyper   rf   r   r^   r  r   r   r   r   disablerr   r]   r  	skip_coder   r   rP   %name_of_builtins_dict_key_in_fglobalsr/   guardsGuardsStateCheckFunctionManagerr;  guard_managerr   rA   rQ   )r   r4  r6  r7  r*   rl   contextrW   r   r!  target_coder  rz   r   r$  guarded_coderP   r<  builtin_dict_namebuiltins_dictcheck_fn_managers                        r7   installzCompilePackage.installM  s(    	G3;;,,. C	KD% '' 't,++- 
  =U%8%89*/*>*>*D*D*F &E;((y'>'>{'K #**).)=)= H"//foo}U,,V]BGH $$".u"5K"'"3"3 J!1*&zl2UV  &/<M  #+:"6"L"L"N,,"&!MM11':	  u**+q0 MM,,66{C$)$7$7 L#)<<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',}}';';'P'P#$11)5)F)F-A	 (Q ($ +#(66&55l6N6NO/E= =C	2 '= =s'   +C?M *AL3	.E8M 3L=8M  M
	c                     | j                          t        t        | j                  j	                               | j
                        S )N)r   r   )r   r   r_   r   rB   r   r   s    r7   cache_entryzCompilePackage.cache_entry  s5     t{{))+,d>S>S
 	
rM   c                 0   ddl m}  ||       }t        j                         }|j	                  |j
                  j                                |j	                  t        |j                  j                        j                                |j                         S )Nr   r   )r  r   r   r   r   rE   r   rI   r   r#   r   )rz   r   innermost_fn_r   s       r7   r  z CompilePackage.source_id_from_fn  so    ,$R(nn&=55<<>?3}55DDELLNO$$&&rM   r   )NNFr+   Nr.   )-rC   rD   rE   rR   r   r   r   r   rk   r   r   r   r0   r1   rI   rT   r  r   rj   rS   r  rK   cached_propertyr  r  r   r   r   r  rH   r  r_   r  r  r  r#  r(  r   
ModuleTyper.  r   rM  rO  r   r  r9   rM   r7   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 ' 'rM   r   c                   .    e Zd Zedefd       ZdefdZy)EagerCacheArtifactr+   c                       y)Nprecompile_eagerr9   r9   rM   r7   r   zEagerCacheArtifact.type  s    !rM   c                 @    t        j                  | j                        S r.   r   r   s    r7   r   z(EagerCacheArtifact.after_deserialization  r   rM   N)rC   rD   rE   r   rI   r   r   r   r9   rM   r7   rV  rV    s'    "# " "*s *rM   rV  c            
       <   e Zd ZdZdeddfdZdededdfdZe	j                  dd	       Ze	j                  d
edededdfd       ZdededdfdZdededdfdZe	j                  dedeeef   fd       Zdedeeeeef   f   fdZdededeeeeef   f   fdZy)DynamoStorez
    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                     |j                         }t        j                  |      }t        j                  t
        j                         |j                  |       y)z]
        Records a package to PrecompileContext, so that it can be serialized later.
        r<   r   N)rO  r   dumpsr   record_artifactr   r   r  )r   r\  rO  pickled_results       r7   record_packagezDynamoStore.record_package  sD     ))+k2)) %%'W->->	
rM   r   r$  c                     t        j                  |      }t        j                  t        j                         ||       y)zT
        Records eager fx graphs to PrecompileContext for testing purposes.
        r^  N)r   r_  r   r`  rV  r   )r   r   r$  ra  s       r7   record_eager_backendz DynamoStore.record_eager_backend  s0      g.))##%:~	
rM   c                      y r.   r9   r   s    r7   clearzDynamoStore.clear  s     rM   r   r4  pathc                      y)a  
        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
        Nr9   r   r   r4  rg  s       r7   writezDynamoStore.write  s     	rM   rO  r<   c                     i }|j                   D ]?  }t        j                  |      }|t        d| d      t	        |t
              sJ |||<   A | j                  |||       y)Y
        Saves a package to a given path. Grabs backends from PrecompileContext.
        Nr8  r9  )r^   r   serialize_artifact_by_keyr  r/   r   rj  )r   rO  r<   backend_contentr   serialized_backends         r7   save_cache_entryzDynamoStore.save_cache_entry  s     &(%11 	=J!2!L!LZ!X!)"zl*MN  02IJJJ*<OJ'	= 	

;5rM   c                 j    | j                  |       |j                         }| j                  ||       y)rl  N)rb  rO  rp  )r   r\  r<   rO  s       r7   save_packagezDynamoStore.save_package  s0     	G$))+k3/rM   c                      y)a  
        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)
        Nr9   r   rg  s     r7   readzDynamoStore.read  s     	rM   c                     | j                  |      \  }}|j                         D ]D  \  }}t        j                  |j	                         |j
                  |j                         |||<   F ||fS )Nr^  )ru  r   r   r`  r   r<   r   )r   r<   rO  rn  r   r$  s         r7   load_cache_entryzDynamoStore.load_cache_entry  so     (,yy~$_#2#8#8#: 	2J--GKK +2OJ'		2 O++rM   rz   c                 J    | j                  |      \  }}t        ||      }||fS )g
        Loads a package from a given path and returns it plus a list of deserialized backends
        )rw  r   )r   rz   r<   rO  rn  r\  s         r7   load_packagezDynamoStore.load_package  s0     (,'<'<S'A$_ [1''rM   rR  )rC   rD   rE   rR   r   rb  rS   r   rd  abcabstractmethodrf  r   	_BackendsrI   rj  rp  rr  r;   ru  rj   rw  rz  r9   rM   r7   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(rM   r[  c                   R    e Zd ZdZddZddZdedededdfd	Z	dede
eef   fd
Zy)InMemoryDynamoStorezX
    A DynamoStore implementation that keeps state about CompilePackages in memory.
    r+   Nc                     i | _         y r.   packagesr   s    r7   r   zInMemoryDynamoStore.__init__,  s	    HJrM   c                 8    | j                   j                          y r.   )r  rf  r   s    r7   rf  zInMemoryDynamoStore.clear/  s    rM   r   r4  rg  c                 &    ||f| j                   |<   y)za
        Store the dynamo cache entry and backends in memory instead of writing to disk.
        Nr  ri  s       r7   rj  zInMemoryDynamoStore.write2  s      &x0drM   c                 X    || j                   vrt        d|       | j                   |   S )zC
        Read dynamo cache entry and backends from memory.
        zNo package found with key )r  r  rt  s     r7   ru  zInMemoryDynamoStore.read=  s2     t}}$!;D6BCC}}T""rM   rR  )rC   rD   rE   rR   r   rf  r   r}  rI   rj  r;   ru  r9   rM   r7   r  r  '  s[    K	1!	1 	1 		1
 
	1# #'8)'C!D #rM   r  c                   X    e Zd ZdZddefdZddZdeded	eddfd
Z	d	ede
eef   fdZy)DiskDynamoStorezV
    A DynamoStore implementation that keeps state about CompilePackages on disk.
    path_prefixc                     || _         y)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     r7   r   zDiskDynamoStore.__init__L  s     'rM   r+   Nc                 `    | j                   r"t        j                  | j                   d       yy)z6
        Clear all CompilePackages from disk.
        T)ignore_errorsN)r  shutilrmtreer   s    r7   rf  zDiskDynamoStore.clearU  s&     MM$**$? rM   r   r4  rg  c                 >   | j                   r*t        j                  j                  | j                   |      n|}	 t        j                  |d       t        t        j                  j                  |d      d      5 }t        j                  ||       ddd       t        t        j                  j                  |d      d      5 }t        j                  ||       ddd       y# 1 sw Y   SxY w# 1 sw Y   yxY w# t        $ r}t        d| d|       |d}~ww xY w)	z@
        Write dynamo cache entry and backends to disk.
        T)exist_okr   wbNr4  zFailed to save package to : )
r  osrg  r   makedirsopenr   dump	Exceptionr  )r   r   r4  rg  dynamo_pathbackend_pathes          r7   rj  zDiskDynamoStore.write\  s     8<7G7Grww||D,,d3T	PKKt,bggll42D9 1[FK01bggll44d; 4|Hl34 41 14 4 	P!;D6A3GHaO	PsN   AC< ;C$2C< C0C< $C-)C< 0C95C< 9C< <	DDDc                    | j                   r*t        j                  j                  | j                   |      n|}	 t	        t        j                  j                  |d      d      5 }t        j                  |      }ddd       t	        t        j                  j                  |d      d      5 }t        j                  |      }ddd       fS # 1 sw Y   UxY w# 1 sw Y   xY w# t        $ r}t        d| d|       |d}~ww xY w)zA
        Read dynamo cache entry and backends from disk.
        r   rbNr4  z!Failed to load package from path r  )	r  r  rg  r   r  r   loadr  r  )r   rg  r  rO  r  rn  r  s          r7   ru  zDiskDynamoStore.reado  s     8<7G7Grww||D,,d3T	Wbggll42D9 7[$kk+67bggll44d; <|"(++l";<//	7 7< <  	W!B4&1#NOUVV	WsG   *C& $C:2C& ,CC& CC& C#C& &	D/DD)r   rR  )rC   rD   rE   rR   rI   r   rf  r   r}  rj  r;   ru  r9   rM   r7   r  r  G  sj    'C '@P!P P 	P
 
P&W W'8)'C!D WrM   r  c            	            e Zd ZdZdeddf fdZdedef   dee	e
eeef   f      f fdZdedef   dee   fd	Z xZS )
DiskDynamoCachezs
    Special DiskDynamoStore which adds some helper functions for automatically
    tracking paths of packages
    r\  r+   Nc                 ~    |j                   }t        j                  d|j                          t        |   ||       y)rl  zSaving CompilePackage for %sN)r  loggerinfosuperrr  )r   r\  r<   	__class__s      r7   savezDiskDynamoCache.save  s4     2G4E4EFWc*rM   rz   .c                    t         j                  |      }t        j                  d|       t        j
                  j                  | j                  |      }t        j
                  j                  |      r	 t        | )  |      }|S t        j                  d|       y# t        $ r*}t        j                  d|t        |             Y d}~yd}~ww xY w)ry  zLoading CompilePackage for %sz'Failed to load package from path %s: %sNzNo package found for %s)r   r  r  r  r  rg  r   r  existsr  rw  r  warningrI   )r   rz   r<   rg  resultr  r  s         r7   r  zDiskDynamoCache.load  s     ..r23S9ww||D,,c277>>$1#6 	-s3	  H$PSTUPVWs   7B 	C( CCc                 r    | j                  |      }|y|\  }}t        ||      }|j                  |       |S )zC
        Load directly into a package and install backends
        N)r  r   rM  )r   rz   resultsrl   r4  r\  s         r7   load_and_install_packagez(DiskDynamoCache.load_and_install_package  s@     ))B-? 'UH$R/GOOH%NrM   )rC   rD   rE   rR   r   r  r   r   r   r;   r   rj   rS   r  r  __classcell__)r  s   @r7   r  r  ~  su    
+N +t +38$	%)4
C+@@A	B&38$	.	!rM   r  r   )RrR   r{  rx   r   dataclassesrK   r   r  r   loggingr  r   r   r  rs   r0   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   	getLoggerrC   r  	dataclassr   rO   rI   rS   rT   rV   r\   ra   r1   r   r   r;   r   r   registerr   r   rT  rF   r_   r   r   AbstractContextManagerr   r   rV  rj   r}  ABCr[  r  r  r  rg  r   DynamoCacher9   rM   r7   <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rM   