
    7hP%                    L   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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Jr  S S	KJr  S S
KJr  SSKJ r   \(       a  S SK!J"r"  S SK#J$r$  S SKJ%r%  \RL                  " \'5      r( " S S5      r)          SS jr*g)    )annotationsN)AbstractContextManagernullcontext)AnyCallableLiteralOptionalTYPE_CHECKING)dynamo_timed)BoxedDeviceIndex)temporary_cache_dir)	BoxedBool	InputType)FakeTensorMode)ShapeEnv   )config)Sequence)	CacheInfo)GraphModulec                      \ rS rSr% SrS\S'   S\S'       SS jrSS jrS	S
.     SS jjr\	S	S
.     SS jj5       r
Srg)CompiledArtifact    a  
CompiledArtifact class represents the precompiled inductor artifact 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 precompiled artifact.
Callable[..., Any]_compiled_fn!Optional[tuple[bytes, CacheInfo]]
_artifactsc                    Xl         X l        g N)r   r   )selfcompiled_fn	artifactss      \/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_inductor/standalone_compile.py__init__CompiledArtifact.__init__5   s    
 (#    c                     U R                   " U6 $ r   )r   )r    argss     r#   __call__CompiledArtifact.__call__=   s      $''r&   binary)formatc          
        [        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                  U" 5       5        UR                  U5        UR                  U5        [        US5       n	U	R                  UR!                  5       5        S S S 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         [,        R.                  R1                  U5      nUc   eUR2                   H  nU
R5                  U5      n[
        R                  R#                  U5      (       d   e[7        [
        R8                  " U5      5       Hq  n[        [
        R                  R;                  X5      S5       n[<        R>                  " U5      nS S S 5        WRA                  5       n[B        RE                  SU5        Ms     M     S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N^= f! , (       d  f       N9= f! , (       d  f       g = f)NzCompiledArtifact.savezFCompiledArtifact.save failed to save since there's no artifact to saver   r   r+   )BytesWriter	torch_keywbunpackedT)ignore_errors)FxGraphCacherbzOutput code written to: %s)#r   r   RuntimeErrorlenaot_autograd_artifactsospathisdir&torch.utils._appending_byte_serializerr.   	codecacher0   write_bytes	write_stropenwriteto_bytesexistsshutilrmtreer4   r   torchcompilerload_cache_artifactsinductor_artifacts_get_tmp_dir_for_keysortedlistdirjoinpickleloadwrite_to_diskloginfo)r    r:   r,   artifact_bytes
cache_infokeyr.   r0   writerfiler4   loaded_cache_infosubdirfgraphoutput_files                   r#   saveCompiledArtifact.save@   s.    12&"\  *.&Nz889Q>J
J>33A6C!77==....N0$""9;/  %"">2$%JJv01 &% +++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2* &%, "H!G /.? 32sV   CK% JBKB6J==J,6J=
K
J)	%K,
J:6J==
K	K
Kc                X  ^ [        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        [!        5       n	OU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	U	   [,        R.                  " S
S9   [        R0                  R,                  R/                  S
S9   SSKJn  UR7                  US
S/ 0 S S9nS S S 5        Wc   eSSKJn  U" [=        S5      [?        S5      S9n[        R@                  RC                  [E        [G        5       S95      n[        R@                  RI                  U5         URK                  / URL                  U5      mS S S 5        S S S 5        S S S 5        [O        U4S jS 5      sS S S 5        $ ! , (       d  f       GNu= f! , (       d  f       N= f! , (       d  f       NZ= f! , (       d  f       Nc= f! , (       d  f       Nl= f! , (       d  f       g = f)NzCompiledArtifact.loadr+   r5   r   )BytesReaderr   r/   r2   aotautogradT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cache)AOTAutogradCacheF)localremoter(   rT   
aot_config)_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 &   > T" [        U 5      5      $ r   )list)r(   r!   s    r#   <lambda>'CompiledArtifact.load.<locals>.<lambda>   s    +d4j2Ir&   )(r   r9   r:   r;   r@   readr<   r`   r=   r0   
read_bytesread_stris_finishedrF   rG   rH   r   rM   rn   rL   r7   r   r   patch
_functorch-torch._functorch._aot_autograd.autograd_cacherd   _lookup
compile_fxrh   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configr   )r:   r,   rW   r"   r`   r0   readerrU   rS   cache_dir_ctxautograd_cache_dirfilesrd   entryrh   	fx_configcontextr!   s                    @r#   rO   CompiledArtifact.loads   s    12!77==....$% $		I &N0$Y/((*ik999oo'!'!2!2!4))++++33NC>Im+++ww}}T****%'WW\\$%F"ww}}%78888RZZ(:;<5zQ&Ah 3D 9 DI%%,,222N -44"$#%#' 5 E O (((8,(//?/B	
  --66"XZ8 ]]**73"'"9"9E66	#K 49 J B $$I4P 32 &%: ON4 439 JI = 32s   9LKEL%L
:(K9"K	=A9K96K(	K9L
$L
K	L
K%!K9(
K62K99
LL


L	L
L))r   r   N)r!   r   r"   r   )r(   r   returnr   )r:   strr,   Literal['binary', 'unpacked']r   None)r:   r   r,   r   r   r   )__name__
__module____qualname____firstlineno____doc____annotations__r$   r)   r]   staticmethodrO   __static_attributes__ r&   r#   r   r       s    " %$11$'$ 5$( EM1P1P$A1P	1Pf >FBQBQ;BQ	BQ BQr&   r   c                  SSK Jn  SSKJn  SnUS:X  a  [        [	        5       S9nSnGO8US	:X  a5  [
        R                  R                  R                  5       nUR                  nOUS
:X  a  [        [	        5       S9n[        [        [        U R                  R                  5      5      5      n	U	R                  S:X  d   e[!        U	R"                  5      S:X  d   eU	R"                  S    He  n
SU
R$                  ;   d  M  U
R$                  S   n['        U[
        R(                  R*                  R,                  5      (       d  MY  UR                  nMg     O[/        SU S35      e[
        R                  R                  U5      n[
        R                  R1                  U5         UR3                  5          [4        R6                  " SS5         [8        R:                  " U 5      n U" X4SU0UD6n[=        U5      (       d   e[
        R>                  RA                  5       nUc  [B        RE                  S5        S S S 5        S S S 5        S S S 5        [G        WW5      $ ! , (       d  f       N*= f! , (       d  f       N3= f! , (       d  f       N<= f)Nr   )CacheArtifactManagerr   )ry   Ffrom_example_inputsrk   Tfrom_tracing_context
from_graphoutputexample_valuezJstandalone_compile got unsupported `dynamic_shapes` value: dynamic_shapes=.ztriton.autotune_at_compile_timeignore_shape_envzstandalone_compile artifact generation failed, cannot save. Run with TORCH_LOGS=+torch._inductor.codecache to identify the problem)$torch.compiler._cacher   ry   r   r   rF   rz   r{   get	fake_modenextiterreversedr[   nodesopr7   r(   meta
isinstance_subclassesfake_tensor
FakeTensor
ValueErrorr|   with_fresh_cacher   ru   copydeepcopycallablerG   save_cache_artifactsrQ   warningr   )gmexample_inputsdynamic_shapesoptionsr   ry   r   r   r   	last_nodenodemaybe_tensorr!   r"   s                 r#   standalone_compiler      s    ;&.."XZ8	  	1	1 --..224%%		<	'"XZ8	
 hrxx~~678	||x'''9>>"a'''NN1%D$))+#yy9lE,=,=,I,I,T,TUU , 6 6I	 & XYgXhhij
 	
 mm**95Gg&--/6= ]]2 
1A
EL
 $$$$NN779	KKY 	> 	0 	'$ K33! 	>= 	0/ 	'&s=    I91I(	A*I3I(;I9
I%!I((
I6	2I99
J)
r   r   r   zSequence[InputType]r   r   r   r   r   r   )+
__future__r   r   loggingr9   rN   rD   
contextlibr   r   typingr   r   r   r	   r
   torch.fxrF   torch._dynamo.utilsr   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   	getLoggerr   rQ   r   r   r   r&   r#   <module>r      s    "   	   : B B  , < G 6 , :  (/$ !VQ VQr=4=4'=4 	=4
 =4 =4r&   