
    3jH                       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rS SKJ	r	J
r
  S SKJrJr  S SK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+  \RX                  " \-5      r. " S S\	5      r/ " S S\/5      r0 " S S\/5      r1S S jr2S!S jr3\Rh                  S"S j5       r5SSS.             S#S jjr6 S$   S%S jjr7g)&    )annotationsN)ABCabstractmethod)AbstractContextManagernullcontext)AnyLiteral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/wildlama/miniconda3/lib/python3.13/site-packages/torch/_inductor/standalone_compile.py__init__CompiledArtifact.__init__6       
 (#    c                    g r    r#   argss     r&   __call__CompiledArtifact.__call__>   s    +.r*   binaryformatc                   g r   r,   )r#   pathr3   s      r&   saveCompiledArtifact.saveA   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r5   r3   r1   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)r5   r3   filer=   r?   result_bytesreaderheaderartifactkeyartifact_bytess              r&   rB   CompiledArtifact.loadF   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tuple[bytes, CacheInfo] | Noner.   r   returnr   r5   strr3   Literal['binary', 'unpacked']r`   Noner5   rb   r3   rc   r`   r   )__name__
__module____qualname____firstlineno____doc__r'   r   r/   r6   staticmethodrB   __static_attributes__r,   r*   r&   r   r   $   s    "$'$ 2$ . .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 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)rA   k   zF
CompiledArtifact that depends on torch.compiler.save_cache_artifacts
r@   c                    Xl         X l        g r   r   r"   s      r&   r'   CacheCompiledArtifact.__init__r   r)   r*   c                     U R                   " U6 $ r   )r    r-   s     r&   r/   CacheCompiledArtifact.__call__z   s      $''r*   c                j    U R                   c  gU R                   u  p[        UR                  5      S:H  $ )NFr   )r!   lenaot_autograd_artifacts)r#   _
cache_infos      r&   is_saveable!CacheCompiledArtifact.is_saveable}   s2    ??" :445::r*   r1   r2   c          
        [        S5         U R                  c  [        S5      eU R                  u  p4[        UR                  5      S:X  a  [        SU 35      e[        UR                  5      S:  a  [        SU 3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
  [1        U5         [2        R4                  R7                  U5      nUc   eUR8                   H  nU
R;                  U5      n[        R                  R)                  U5      (       d   e[=        [        R>                  " U5      5       Hq  n[A        [        R                  RC                  X5      S5       n[D        RF                  " U5      nS S S 5        WRI                  5       n[J        RM                  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   a  CompiledArtifact.save failed to save due to no aot_autograd artifacts. This likely means there was something that was not serializable in the graph passed to standalone_compile. This can generally be fixed by ensuring that your model only uses constructs that are serializable. r   zhCompiledArtifact.save failed to save because there was more than one artifact but we only expected one. r1   BytesWriterr>   write_atomicr9   T)ignore_errors)FxGraphCacher;   zOutput code written to: %s)'r   r!   rR   rt   ru   AssertionErrorosr5   isdirrD   r|   rE   r?   write_bytesrA   rL   	write_strtorch._inductor.codecacher~   to_bytesexistsshutilrmtreer   r   rN   rO   rP   inductor_artifacts_get_tmp_dir_for_keysortedlistdirrC   joinpicklerB   write_to_diskloginfo)r#   r5   r3   rZ   rw   rY   r|   r?   writerr~   r   loaded_cache_infosubdirfgraphoutput_files                   r&   r6   CacheCompiledArtifact.save   sn    12&"\  *.&N:445:"\ "l	$  :4459$::DG  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 /] 32t "H!G /.] 32s=   F0K2<B6K!2K	6K!?K2
KK!!
K/	+K22
L 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.   rw   
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   patchrN   
_functorch-torch._functorch._aot_autograd.autograd_cacher   _lookup
compile_fxr   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configrA   )
cache_dir_ctxrY   r   resultentryrv   r   	fx_configcontextr$   s
            @r&   rQ    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.loadr1   r;   Nr   r<   r   r>   r9   aotautograd)r   r   r   r5   r   rC   rF   rD   r=   rE   r?   rG   rM   rJ   rN   rO   rP   r   r   r   r   rt   r   )r5   r3   rT   r%   r=   r?   rV   rY   rZ   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:   )rA   r   rQ   )r5   r3   rY   r   s       r&   rB   CacheCompiledArtifact.load  s3     3@@ A 
 %//CCr*   r\   Nr]   r_   )r`   boolra   )r   zAbstractContextManager[Any]rY   rb   r`   r   )r5   rb   r3   rc   r`   z'tuple[str, AbstractContextManager[Any]]re   )rf   rg   rh   ri   rj   bytesrL   r'   r/   rx   r6   rk   rQ   r   rB   rl   r,   r*   r&   rA   rA   k   s     0':L$'$ 2$(; EM@P@P$A@P	@PD $R2$R9<$R	$R $RL >F"*"*;"*	0"* "*H >FDD;D	D Dr*   rA   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)rH   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.
r@   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   )rH   r$   )
bundled_fns    r&   from_bundled_callable)AOTCompiledArtifact.from_bundled_callable2  s     #:#9#9::r*   c                     U R                   " U6 $ r   )r   r-   s     r&   r/   AOTCompiledArtifact.__call__8  s    }}d##r*   r1   r2   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 )Nr9   8AOTCompiledArtifact does not support unpacked format yetr   r{   r   r>   r}   )rR   	serializerD   r|   rE   r?   r   rH   rI   r   r~   r   )r#   r5   r3   rU   r|   r?   r   r~   s           r&   r6   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.serializeQ  s    5QQMM
 	
r*   c                    [         R                  " U 5      n[        U[         5      (       d   e[        R	                  U5      $ r   )r   deserialize_compile_artifacts
isinstancerH   r   )rU   deserializeds     r&   rK   AOTCompiledArtifact.deserializeV  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)Nr9   r   r;   r   r<   r   r>   )rR   rC   rD   r=   rE   r?   rF   rG   rH   rI   rJ   rK   )	r5   r3   rT   r=   r?   rU   rV   rW   rX   s	            r&   rB   AOTCompiledArtifact.load`  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   r`   rH   r_   ra   )r`   r   )rU   r   r`   rH   re   )rf   rg   rh   ri   rj   r   rI   r'   rk   r   r/   r6   r   rK   rB   rl   r,   r*   r&   rH   rH     s     ,g6J
'
 ;:;	; ;
$ EM..$A.	.,

 G G >F==;=	= =r*   rH   c                    U S:H  $ )Nfrom_example_inputsr,   )dynamic_shapess    r&   _resolve_ignore_shape_envr   w  s     222r*   c                L   US:X  a  [        [        5       S9$ US:X  aC  [        R                  R                  R                  5       nUR                  c   eUR                  $ US:X  Ga/  [        [        [        U R                  R                  5      5      5      nUR                  S:X  d   e[        UR                  5      S:X  d   e[        UR                  S   [        R                   R"                  5      (       a  UR                  S   /OUR                  S   nU He  nSUR$                  ;   d  M  UR$                  S   n[        U[        R&                  R(                  R*                  5      (       d  MY  UR                  s  $    [        [        5       S9$ [-        S	U S
35      e)Nr   r   from_tracing_context
from_graphoutputr   r   example_valuezJstandalone_compile got unsupported `dynamic_shapes` value: dynamic_shapes=.)r   r   rN   r   r   get	fake_modenextiterreversedr   nodesoprt   r.   r   fxNodemeta_subclassesfake_tensor
FakeTensor
ValueError)gmr   r   	last_noder   nodemaybe_tensors          r&   _resolve_fake_moder   }  sf   ..
33	1	1 --..224  ,,,   	<	'
 hrxx~~678	||x'''9>>"a''' )..+UXX]];; ^^A" 	
 D$))+#yy9lE,=,=,I,I,T,TUU'111	  
33XYgXhhij
 	
r*   c           	   #  $  #    SSK Jn  [        X5      n[        R                  R                  U5      n[        R                  R                  U5         UR                  5          [        R                  " SS5         [        R                  R                  R                  SU5         S v   S S S 5        S S S 5        S S S 5        S S S 5        g ! , (       d  f       N'= f! , (       d  f       N0= f! , (       d  f       N9= f! , (       d  f       g = f7f)Nr   )CacheArtifactManagerztriton.autotune_at_compile_timeTbundled_autograd_cache)torch.compiler._cacher   r   rN   r   r   r   with_fresh_cacher   r   r   )r   r   aotr   r   tracing_contexts         r&   _standalone_contextr    s     :"26Imm229=Oo.--/6=%%&>D 	E 	> 	0 	/. 	ED 	>= 	0/ 	/.sf   ADC?#C.;+C&C	+C3C.;C?	D
CC
C+'C..
C<	8C??
D	DF)r   donate_graph_modulec                  SSK J n  [        U5      n[        XU5         U(       d  [        R                  " U 5      n U" X4SU0UD6n[        U5      (       d   eU(       a0  [        US5      (       d  [        S5      e[        U5      sSSS5        $ [        R                  R                  5       n	U	c  [        R                  S5        SSS5        [        WW	5      $ ! , (       d  f       N= f)z5
Implementation of torch.inductor.standalone_compile
r   r   ignore_shape_envr   z<Compiled function should have serialize method when aot=TrueNzstandalone_compile artifact generation failed, cannot save. Run with TORCH_LOGS=+torch._inductor.codecache to identify the problem)r   r   r  copydeepcopycallablehasattrrR   rH   rN   rO   save_cache_artifactsr   warningrA   )
r   example_inputsr   optionsr   r  r   r  r$   r%   s
             r&   standalone_compiler    s     '0@	R	5"r"B 
1A
EL
 $$$$;44"R  '{3 
6	5 NN779	KKY! 
6* !i88+ 
6	5s   A(C6C
C(c                    SSK Jn  [        U5      n[        XU5         UR	                  U UUS9sS S S 5        $ ! , (       d  f       g = f)Nr   r  )r  ) r   r   r  autograd_cache_key)r   r  r   r   r   r  s         r&   r  r    sD     0@	UC	8,,- - 
 
9	8	8s	   9
A)r   r   )r   r   r   r   r`   r   )r   r   r   r   r   r   )r   r   r  zSequence[InputType]r   r   r  r   r   r   r  r   r`   r   )F)r   r   r   r   )8
__future__r   
contextlibr  loggingr   r   r   abcr   r   r   r   typingr   r	   r
   torch.fxrN   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  r   collections.abcr   r   r   r   r   	getLoggerrf   r   r   rA   rH   r   r   contextmanagerr  r  r  r,   r*   r&   <module>r#     s(   "    	   # : . .  R , @ < G 6 , :  2/$ !Ds DNpD, pDfV=* V=r3'
T  (  %$9$9'$9 	$9
 $9 
$9 $9 $9V 	
 
 
	
r*   