
    ȅiA                       S SK J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  S SKJrJrJrJr  S SKrS SKJr  S SKJr  S SKJr  S S	KJ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&  \(       a  S SK'J(r(J)r)  S SK*J+r+  S SKJ,r,  \RZ                  " \.5      r/ " S S\5      r0 " S S\05      r1 " S S\05      r2SS.           SS jjr3g)    )annotationsN)ABCabstractmethod)AbstractContextManagernullcontext)AnyLiteralOptionalTYPE_CHECKING)&BundledAOTAutogradSerializableCallable)dynamo_timed)normalize_path_separator)BoxedDeviceIndex)temporary_cache_dir)	BoxedBool	InputType)FakeTensorMode)ShapeEnv   )config)CallableSequence)	CacheInfo)GraphModulec                      \ rS rSrSr    SS jr\SS j5       r\SS.     SS jj5       r\	SS.     SS jj5       r
S	rg
)CompiledArtifact#   ax  
CompiledArtifact class represents the inductor cache artifacts that
can be invoked in order to avoid repeated compilation.

CompiledArtifact can be obtained by calling standalone_compile(gm, example_inputs)
to create a fresh CompiledArtifact from a GraphModule and example inputs.

Later this CompiledArtifact can be saved to disk, either as a binary or unpacked
into the provided folder via the CompiledArtifact.save function.

CompiledArtifact.load provides a way to create a CompiledArtifact from the
binary or unpacked data.

Finally, the CompiledArtifact can be invoked via the __call__ method
to execute the cached artifact.
c                    Xl         X l        g N_compiled_fn
_artifactsselfcompiled_fn	artifactss      \/home/james-whalen/.local/lib/python3.13/site-packages/torch/_inductor/standalone_compile.py__init__CompiledArtifact.__init__5       
 (#    c                    g r    r$   argss     r'   __call__CompiledArtifact.__call__=   s    +.r+   binaryformatc                   g r   r-   )r$   pathr4   s      r'   saveCompiledArtifact.save@   s     r+   c                b   US:X  a  [         R                  XS9$ US:X  d   e[        U S5       nSSKJn  SSKJn  UR                  5       nU" U5      nUR                  5       nU[        R                  :X  a`  UR                  5       U" 5       :X  d   eUR                  5       nUR                  5       (       d   e[        R                  U5      sS S S 5        $ U[         R                  :X  a  UR                  5       U" 5       :X  d   eUR                  5       n	UR                  5       n
UR                  5       (       d   e[        R                   R#                  U
5        [         R%                  ['        5       U	5      sS S S 5        $ [)        S	UR+                  S
5      -   5      e! , (       d  f       g = f)Nunpackedr6   r4   r2   rbr   BytesReaderr   	torch_keyzLInvalid header, expected CacheCompiledArtifact or AOTCompiledArtifact, got: utf-8)CacheCompiledArtifactloadopen&torch.utils._appending_byte_serializerr>   	codecacher@   read
read_bytesAOTCompiledArtifact
AOT_HEADERis_finisheddeserializeCACHE_HEADERread_strtorchcompilerload_cache_artifacts
_load_implr   RuntimeErrordecode)r6   r4   filer>   r@   result_bytesreaderheaderartifactkeyartifact_bytess              r'   rC   CompiledArtifact.loadE   su    Z(--4-GG!!!$J,99;L .F&&(F,777((*ik999!,,.))++++*66x@  0===((*ik999oo'!'!2!2!4))++++33NC,77sK) , #bmmG,- - s   BF B"F F  
F.r"   r!   Nr%   Callable[..., Any]r&   z!Optional[tuple[bytes, CacheInfo]]r/   r   returnr   r6   strr4   Literal['binary', 'unpacked']ra   Noner6   rc   r4   rd   ra   r   )__name__
__module____qualname____firstlineno____doc__r(   r   r0   r7   staticmethodrC   __static_attributes__r-   r+   r'   r   r   #   s    "$'$ 5$ . .DL$A	  >F!!;!	! !r+   r   c                      \ rS rSrSr\" S S5      r    SS jrSS jrSS.     SS jjr	\
      SS	 j5       r\
SS.     SS
 jj5       r\
SS.     SS jj5       rSrg)rB   j   zF
CompiledArtifact that depends on torch.compiler.save_cache_artifacts
rA   c                    Xl         X l        g r   r    r#   s      r'   r(   CacheCompiledArtifact.__init__q   r*   r+   c                     U R                   " U6 $ r   )r!   r.   s     r'   r0   CacheCompiledArtifact.__call__y   s      $''r+   r2   r3   c          
        [        S5         U R                  c  [        S5      eU R                  u  p4[        UR                  5      S:X  d   U5       eUR                  S   nUS:X  a  [
        R                  R                  U5      (       a   eSSKJ	n  SSK
Jn  U" 5       nUR                  [        R                  5        UR                  U" 5       5        UR                  U5        UR                  U5        SSKJn	  U	" XR%                  5       5        GOUS	:X  d   e[
        R                  R'                  U5      (       a;  [
        R                  R                  U5      (       d   e[(        R*                  " US
S9  SSK
Jn
  [/        U5         [0        R2                  R5                  U5      nUc   eUR6                   H  nU
R9                  U5      n[
        R                  R'                  U5      (       d   e[;        [
        R<                  " U5      5       Hq  n[?        [
        R                  RA                  X5      S5       n[B        RD                  " U5      nS S S 5        WRG                  5       n[H        RK                  SU5        Ms     M     S S S 5        S S S 5        g ! , (       d  f       NM= f! , (       d  f       N(= f! , (       d  f       g = f)NzCompiledArtifact.savezFCompiledArtifact.save failed to save since there's no artifact to saver   r   r2   BytesWriterr?   write_atomicr:   T)ignore_errors)FxGraphCacher<   zOutput code written to: %s)&r   r"   rS   lenaot_autograd_artifactsosr6   isdirrE   rv   rF   r@   write_bytesrB   rM   	write_strtorch._inductor.codecacherx   to_bytesexistsshutilrmtreerz   r   rO   rP   rQ   inductor_artifacts_get_tmp_dir_for_keysortedlistdirrD   joinpicklerC   write_to_diskloginfo)r$   r6   r4   r[   
cache_inforZ   rv   r@   writerrx   rz   loaded_cache_infosubdirfgraphoutput_files                   r'   r7   CacheCompiledArtifact.save|   s&    12&"\  *.&Nz889Q>J
J>33A6C!77==....N0$""#8#E#EF""9;/  %"">2BT??#45+++77>>$''77==....MM$d;3(.(-(K(K&)% -888  1CC!-!B!B3!G!ww~~f5555$*2::f+=$>D!%bggll6&@$!G1(.A "H*/*=*=*?KHH%A;O	 %?  D /E 32\ "H!G /.E 32s=   FKB6J3J"6J3K"
J0,J33
K	=K
Kc                  ^	 U    [         R                  " SS9   [        R                  R                   R                  SS9   SSKJn  UR                  USS/ 0 S S9nS S S 5        Wc   eUu  pESS	KJn  U" [        S5      [        S5      S
9n[        R                  R                  [        [        5       S95      n[        R                  R                  U5         UR!                  / UR"                  U5      m	S S S 5        S S S 5        S S S 5        [%        U	4S jS 5      $ ! , (       d  f       N= f! , (       d  f       N?= f! , (       d  f       NH= f! , (       d  f       NQ= f)NT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cacher   )AOTAutogradCacheF)localremoter/   r   
aot_configr   )_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 &   > T" [        U 5      5      $ r   )list)r/   r%   s    r'   <lambda>2CacheCompiledArtifact._load_impl.<locals>.<lambda>   s    ;tDz3Jr+   )r   patchrO   
_functorch-torch._functorch._aot_autograd.autograd_cacher   _lookup
compile_fxr   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configrB   )
cache_dir_ctxrZ   r   resultentry_r   	fx_configcontextr%   s
            @r'   rR    CacheCompiledArtifact._load_impl   s)   
 LLE!!((..T.J *11 !# 2  K %%%JU4($U++;A+>I
 mm22>HJ3WXG&&w/#5522I 07 F @ %%JDQQ; KJ2 0/7 FE MsS   E(EDA=ED07E?E
D-)E0
D>:E
E	E
E c                   [        U 5      n [        S5         US:X  a  [        R                  R	                  U 5      (       a   e[        U S5       nUR                  5       nSSS5        SSKJn  SSK	J
n  U" W5      nUR                  5       U" 5       :X  d   eUR                  5       nUR                  5       nUR                  5       (       d   e[        R                  R!                  U5        U[#        5       4sSSS5        $ US	:X  d   e[        R                  R	                  U 5      (       d   e[        R                  R%                  U S
5      n	[        R                  R	                  U	5      (       d   e['        [        R(                  " U	5      5      n
[+        U
5      S:X  d   eU
S   n[-        U 5      nX{4sSSS5        $ ! , (       d  f       GNi= f! , (       d  f       g= f)zE
Do format specific prep and loads, return a context manager and key
zCompiledArtifact.loadr2   r<   Nr   r=   r   r?   r:   aotautograd)r   r   r}   r6   r~   rD   rG   rE   r>   rF   r@   rH   rN   rK   rO   rP   rQ   r   r   r   r   r{   r   )r6   r4   rU   r&   r>   r@   rW   rZ   r[   autograd_cache_dirfilesr   s               r'   _prepare_load#CacheCompiledArtifact._prepare_load   s    (-12!77==....$% $		I &N0$Y/((*ik999oo'!'!2!2!4))++++33NCKM)# 32& +++ww}}T****%'WW\\$%F"ww}}%78888RZZ(:;<5zQ&Ah 3D 9)7 32 &%	 32s+   9GG!BGB6G
G	G
G#c                V    [         R                  XS9u  p#[         R                  X25      $ )Nr;   )rB   r   rR   )r6   r4   rZ   r   s       r'   rC   CacheCompiledArtifact.load   s3     3@@ A 
 %//CCr+   r]   Nr^   r`   rb   )r   zAbstractContextManager[Any]rZ   rc   ra   r   )r6   rc   r4   rd   ra   z'tuple[str, AbstractContextManager[Any]]rf   )rg   rh   ri   rj   rk   bytesrM   r(   r0   r7   rl   rR   r   rC   rm   r-   r+   r'   rB   rB   j   s     0':L$'$ 5$( EM4P4P$A4P	4Pl $R2$R9<$R	$R $RL >F"*"*;"*	0"* "*H >FDD;D	D Dr+   rB   c                      \ rS rSrSr\" S S5      r  SS jr\    SS j5       r	SS jr
SS.     SS	 jjrSS
 jr\SS j5       r\SS.     SS jj5       rSrg)rI   i  a%  
Similar to CompiledArtifact, but the object is a single, bundled precompiled function.
This object is always a serializable callable function.

This object is essentially a wrapper for BundledAOTAutogradSerializableCallable, which
is used by torch._dynamo.aot_compile for AOT Precompilation.
rA   c                2    [        U5      U l        S U l        g r   )r   inner_fnr"   )r$   r%   s     r'   r(   AOTCompiledArtifact.__init__  s     ?{K 	r+   c                ,    [        U R                  5      $ r   )rI   r%   )
bundled_fns    r'   from_bundled_callable)AOTCompiledArtifact.from_bundled_callable  s     #:#9#9::r+   c                     U R                   " U6 $ r   )r   r.   s     r'   r0   AOTCompiledArtifact.__call__"  s    }}d##r+   r2   r3   c               0   US:X  a  [        S5      eU R                  5       nSSKJn  SSKJn  U" 5       nUR                  [        R                  5        UR                  U" 5       5        UR                  U5        SSK	J
n  U" XR                  5       5        g )Nr:   8AOTCompiledArtifact does not support unpacked format yetr   ru   r   r?   rw   )rS   	serializerE   rv   rF   r@   r   rI   rJ   r   rx   r   )r$   r6   r4   rV   rv   r@   r   rx   s           r'   r7   AOTCompiledArtifact.save%  s~     ZJ  ~~'F(.99:9;'<(: 	T??,-r+   c                B    [         R                  " U R                  5      $ r   )r   serialize_compile_artifactsr   )r$   s    r'   r   AOTCompiledArtifact.serialize;  s    5QQMM
 	
r+   c                    [         R                  " U 5      n[        U[         5      (       d   e[        R	                  U5      $ r   )r   deserialize_compile_artifacts
isinstancerI   r   )rV   deserializeds     r'   rL   AOTCompiledArtifact.deserialize@  sA     3PP 	
 ,(NOOOO"88FFr+   c                   US:X  a  [        S5      e[        U S5       nSSKJn  SSKJn  UR                  5       nU" U5      nUR                  5       nU[        R                  :X  d   eUR                  5       U" 5       :X  d   eUR                  5       nUR                  5       (       d   e[        R                  U5      sS S S 5        $ ! , (       d  f       g = f)Nr:   r   r<   r   r=   r   r?   )rS   rD   rE   r>   rF   r@   rG   rH   rI   rJ   rK   rL   )	r6   r4   rU   r>   r@   rV   rW   rX   rY   s	            r'   rC   AOTCompiledArtifact.loadJ  s     ZJ  $J,99;L .F&&(F0;;;;;$$&)+555((*H%%''''&228< s   B!C		
C)r"   r   N)r%   r_   )r   r   ra   rI   r`   rb   )ra   r   )rV   r   ra   rI   rf   )rg   rh   ri   rj   rk   r   rJ   r(   rl   r   r0   r7   r   rL   rC   rm   r-   r+   r'   rI   rI     s     ,g6J
'
 ;:;	; ;
$ EM..$A.	.,

 G G >F==;=	= =r+   rI   F)aotc          	     T  ^ SSK Jn  SSKJn  SnUS:X  a  [        [	        5       S9mSnGOAUS	:X  aD  [
        R                  R                  R                  5       nUR                  c   eUR                  mOUS:X  a  [        [	        5       S9m[        [        [        U R                  R                  5      5      5      n	U	R                  S:X  d   e[!        U	R"                  5      S:X  d   eSU4S jjn
[%        U	R"                  S   [
        R&                  R(                  5      (       a  U
" U	R"                  S   5        O.U	R"                  S    H  nU
" U5        M     O[+        SU S35      e[
        R                  R                  T5      n[
        R                  R-                  U5         UR/                  5          [0        R2                  " SS5         [
        R4                  R0                  R3                  SU5         [6        R8                  " U 5      n U" X4SU0UD6n[;        U5      (       d   eU(       aK  [=        US5      (       d  [?        S5      e[A        U5      sS
S
S
5        sS
S
S
5        sS
S
S
5        sS
S
S
5        $ [
        RB                  RE                  5       nUc  [F        RI                  S5        S
S
S
5        S
S
S
5        S
S
S
5        S
S
S
5        [K        WW5      $ ! , (       d  f       N2= f! , (       d  f       N;= f! , (       d  f       ND= f! , (       d  f       NM= f)z5
Implementation of torch.inductor.standalone_compile
r   )CacheArtifactManagerr   )r   Ffrom_example_inputsr   Tfrom_tracing_contextN
from_graphoutputc                   > SU R                   ;   aP  U R                   S   n[        U[        R                  R                  R
                  5      (       a  UR                  mg g g )Nexample_value)metar   rO   _subclassesfake_tensor
FakeTensor	fake_mode)nodemaybe_tensorr   s     r'   handle_node'standalone_compile.<locals>.handle_node  sR    $))+#yy9lE,=,=,I,I,T,TUU , 6 6I V ,r+   zJstandalone_compile got unsupported `dynamic_shapes` value: dynamic_shapes=.ztriton.autotune_at_compile_timebundled_autograd_cacheignore_shape_envr   z<Compiled function should have serialize method when aot=Truezstandalone_compile artifact generation failed, cannot save. Run with TORCH_LOGS=+torch._inductor.codecache to identify the problem)r   ztorch.fx.Nodera   re   )&torch.compiler._cacher   r   r   r   rO   r   r   getr   nextiterreversedr   nodesopr{   r/   r   fxNode
ValueErrorr   with_fresh_cacher   r   r   copydeepcopycallablehasattrrS   rI   rP   save_cache_artifactsr   warningrB   )gmexample_inputsdynamic_shapesoptionsr   r   r   r   r   	last_noder   r   r%   r&   r   s                 @r'   standalone_compiler  a  s    ;&.."XZ8	  	1	1 --..224  ,,,%%		<	'"XZ8	
 hrxx~~678	||x'''9>>"a'''	7 innQ'77	q)*!q)D! * XYgXhhij
 	
 mm**95Gg&--/6=%%&>D ]]2 
1A
EL
 $$$$;44"R  '{3 	ED 	>= 	0/ 	'&" NN779	KKY! 	E 	> 	0 	'0 !i88+ 	ED 	>= 	0/ 	'&sm   *L;L+K7>A!K&		K7(	L1	L6K&	:K7L
L&
K40K77
LL
L	L
L')r   r   r   zSequence[InputType]r  r   r  r   r   boolra   r   )4
__future__r   r   loggingr}   r   r   abcr   r   
contextlibr   r   typingr   r	   r
   r   torch.fxrO   torch._dynamo.aot_compile_typesr   torch._dynamo.utilsr   torch._inductor.cpp_builderr   torch._inductor.cudagraph_utilsr   'torch._inductor.runtime.cache_dir_utilsr   torch._inductor.utilsr   r   torch._subclassesr   %torch.fx.experimental.symbolic_shapesr    r   collections.abcr   r   r   r   r   	getLoggerrg   r   r   rB   rI   r  r-   r+   r'   <module>r     s    "   	   # : 8 8  R , @ < G 6 , :  2/$ !Ds DN[D, [D|V=* V=~ W9W9'W9 	W9
 W9 
W9 W9r+   