
    ^j8R                    F   d dl m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 d dlmZmZmZ ed   Zd dlZd dlmZ d dlmZ d d	lmZ d d
lm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( ddl)m*Z*m+Z+ erd dl,m-Z-m.Z. d dl/m0Z0 d dlm1Z1  ejd                  e3      Z4 G d de	      Z5 G d de5      Z6 G d de5      Z7d#dZ8	 d$	 	 	 	 	 	 	 d%dZ9ejt                  	 d$	 	 	 	 	 	 	 d&d       Z;dddd 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'd!Z<	 	 d(	 	 	 	 	 d)d"Z=y)*    )annotationsN)ABCabstractmethod)AbstractContextManagernullcontext)AnyLiteralTYPE_CHECKING)from_example_inputsfrom_tracing_context
from_graph)&BundledAOTAutogradSerializableCallable)dynamo_timed)normalize_path_separator)BoxedDeviceIndex)temporary_cache_dir)	BoxedBool	InputType)FakeTensorMode)ShapeEnv)._share_torchbind_and_process_group_on_deepcopy   )config)"_functionalize_inplace_collectives_unbox_process_group_torchbinds)CallableSequence)	CacheInfo)GraphModulec                  v    e Zd ZdZ	 	 	 	 d	dZed
d       Zedd	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Z	y)CompiledArtifacta  
    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                     || _         || _        y N_compiled_fn
_artifactsselfcompiled_fn	artifactss      m/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_inductor/standalone_compile.py__init__zCompiledArtifact.__init__>       
 (#    c                     y r#    r(   argss     r+   __call__zCompiledArtifact.__call__F   s    +.r.   binaryformatc                    y r#   r0   )r(   pathr6   s      r+   savezCompiledArtifact.saveI   s     r.   c                @   |dk(  rt         j                  | |      S |dk(  sJ t        | d      5 }ddlm} ddlm} |j                         } ||      }|j                         }|t        j                  k(  rZ|j                          |       k(  sJ |j                         }|j                         sJ t        j                  |      cd d d        S |t         j                  k(  r|j                          |       k(  sJ |j                         }	|j                         }
|j                         sJ t        j                   j#                  |
       t         j%                  t'               |	      cd d d        S t)        d	|j+                  d
      z         # 1 sw Y   y xY w)Nunpackedr8   r6   r4   rbr   BytesReaderr   	torch_keyzLInvalid header, expected CacheCompiledArtifact or AOTCompiledArtifact, got: utf-8)CacheCompiledArtifactloadopen&torch.utils._appending_byte_serializerr?   	codecacherA   read
read_bytesAOTCompiledArtifact
AOT_HEADERis_finisheddeserializeCACHE_HEADERread_strtorchcompilerload_cache_artifacts
_load_implr   RuntimeErrordecode)r8   r6   filer?   rA   result_bytesreaderheaderartifactkeyartifact_bytess              r+   rD   zCompiledArtifact.loadN   s|    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7FFNr)   Callable[..., Any]r*   ztuple[bytes, CacheInfo] | Noner2   r   returnr   r8   strr6   Literal['binary', 'unpacked']r`   Noner8   rb   r6   rc   r`   r!   )
__name__
__module____qualname____doc__r,   r   r3   r9   staticmethodrD   r0   r.   r+   r!   r!   ,   s    "$'$ 2$ . .DL$A	  >F!!;!	! !r.   r!   c                      e Zd ZdZ ed d      Z	 	 	 	 ddZddZddZdd	 	 	 	 	 ddZ	e
	 	 	 	 	 	 dd	       Ze
dd	 	 	 	 	 dd
       Ze
dd	 	 	 	 	 dd       Zy)rC   zN
    CompiledArtifact that depends on torch.compiler.save_cache_artifacts
    rB   c                     || _         || _        y r#   r$   r'   s      r+   r,   zCacheCompiledArtifact.__init__z   r-   r.   c                      | j                   | S r#   )r%   r1   s     r+   r3   zCacheCompiledArtifact.__call__   s     t  $''r.   c                j    | j                   y| j                   \  }}t        |j                        dk(  S )NFr   )r&   lenaot_autograd_artifacts)r(   _
cache_infos      r+   is_saveablez!CacheCompiledArtifact.is_saveable   s4    ??": :445::r.   r4   r5   c          
        t        d      5  | j                  t        d      | j                  \  }}t        |j                        dk(  rt        d|       t        |j                        dkD  rt        d|       |j                  d   }|dk(  rt        j                  j                  |      rJ ddl	m
} dd	lm}  |       }|j                  t        j                         |j                   |              |j!                  |       |j                  |       dd
lm}	  |	||j'                                np|dk(  sJ t        j                  j)                  |      r8t        j                  j                  |      sJ t+        j,                  |d       ddlm}
 t1        |      5  t2        j4                  j7                  |      }|J |j8                  D ]  }|
j;                  |      }t        j                  j)                  |      sJ t=        t        j>                  |            D ]p  }tA        t        j                  jC                  ||      d      5 }tE        jF                  |      }d d d        jI                         }tJ        jM                  d|       r  	 d d d        d d d        y # 1 sw Y   ExY w# 1 sw Y   xY w# 1 sw Y   y xY w)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. r4   BytesWriterr@   write_atomicr;   T)ignore_errors)FxGraphCacher=   zOutput code written to: %s)'r   r&   rT   ro   rp   AssertionErrorosr8   isdirrF   rv   rG   rA   write_bytesrC   rN   	write_strtorch._inductor.codecacherx   to_bytesexistsshutilrmtreerz   r   rP   rQ   rR   inductor_artifacts_get_tmp_dir_for_keysortedlistdirrE   joinpicklerD   write_to_diskloginfo)r(   r8   r6   r\   rr   r[   rv   rA   writerrx   rz   loaded_cache_infosubdirfgraphoutput_files                   r+   r9   zCacheCompiledArtifact.save   s    12 =	P&"\  *.&NJ:445:"\ "l	$  :4459$::DG  33A6C!77==...N0$""#8#E#EF""9;/  %"">2BT6??#45+++77>>$'77==...MM$d;3(. P(-(K(K&)% -888  1CC P!-!B!B3!G!ww~~f555$*2::f+=$> PD!%bggll64&@$!G 71(.A7*/*=*=*?KHH%A;O	PPP]=	P =	Pt7 7P P]=	P =	Ps=   F K,B0KJ622K%K6J?;KK	KKc                  	 | 5  t        j                  d      5  t        j                  j                   j                  d      5  ddlm} |j                  |ddg i d       }d d d        J |\  }}dd	lm}  |t        d      t        d      
      }t        j                  j                  t        t                           }t        j                  j                  |      5  |j!                  g |j"                  |      	d d d        d d d        d d d        t%        	fdd       S # 1 sw Y   xY w# 1 sw Y   4xY w# 1 sw Y   8xY w# 1 sw Y   <xY w)NT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cacher   )AOTAutogradCacheF)localremoter2   rr   
aot_configr   )_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 &     t        |             S r#   )list)r2   r)   s    r+   <lambda>z2CacheCompiledArtifact._load_impl.<locals>.<lambda>   s    ;tDz3J r.   )r   patchrP   
_functorch-torch._functorch._aot_autograd.autograd_cacher   _lookup
compile_fxr   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configrC   )
cache_dir_ctxr[   r   resultentryrq   r   	fx_configcontextr)   s
            @r+   rS   z CacheCompiledArtifact._load_impl   sF   
  	LLE 	 !!((..T.J  *11 !# 2  %%%JUA4($U++;A+>I
 mm22>HJ3WXG&&w/ #5522I; 	  	B %%JDQQ; 2 ; 	  	  	  	sS   E+ED,$BE'D8EE,D51E8E=EE		EEc                   t        |       } t        d      5  |dk(  rt        j                  j	                  |       rJ t        | d      5 }|j                         }ddd       ddlm} ddl	m
}  |      }|j                          |       k(  sJ |j                         }|j                         }|j                         sJ t        j                  j!                  |       |t#               fcddd       S |d	k(  sJ t        j                  j	                  |       sJ t        j                  j%                  | d
      }	t        j                  j	                  |	      sJ t'        t        j(                  |	            }
t+        |
      dk(  sJ |
d   }t-        |       }||fcddd       S # 1 sw Y   RxY w# 1 sw Y   yxY w)zU
        Do format specific prep and loads, return a context manager and key
        zCompiledArtifact.loadr4   r=   Nr   r>   r   r@   r;   aotautograd)r   r   r|   r8   r}   rE   rH   rF   r?   rG   rA   rI   rO   rL   rP   rQ   rR   r   r   r   r   ro   r   )r8   r6   rV   r*   r?   rA   rX   r[   r\   autograd_cache_dirfilesr   s               r+   _prepare_loadz#CacheCompiledArtifact._prepare_load   s    (-12 	*!77==...$% , $		I,N0$Y/((*ik999oo'!'!2!2!4))+++33NCKM)#	* 	*& +++ww}}T***%'WW\\$%F"ww}}%7888RZZ(:;<5zQ&Ah 3D 9M)7	* 	*, ,		* 	*s+   3F8
F+BF87B*F8+F5	0F88Gc                b    t         j                  | |      \  }}t         j                  ||      S )Nr<   )rC   r   rS   )r8   r6   r[   r   s       r+   rD   zCacheCompiledArtifact.load  s9     3@@f A 
] %//sCCr.   Nr]   r_   )r`   boolra   )r   zAbstractContextManager[Any]r[   rb   r`   r!   )r8   rb   r6   rc   r`   z'tuple[str, AbstractContextManager[Any]]re   )rf   rg   rh   ri   bytesrN   r,   r3   rs   r9   rj   rS   r   rD   r0   r.   r+   rC   rC   s   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.   rC   c                      e Zd ZdZ ed d      Z	 	 ddZe	 	 	 	 dd       ZddZ	dd	 	 	 	 	 ddZ
dd	Zedd
       Zedd	 	 	 	 	 dd       Zy)rJ   a9  
    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.
    rB   c                2    t        |      | _        d | _        y r#   )r   inner_fnr&   )r(   r)   s     r+   r,   zAOTCompiledArtifact.__init__1  s     ?{K 	r.   c                ,    t        | j                        S r#   )rJ   r)   )
bundled_fns    r+   from_bundled_callablez)AOTCompiledArtifact.from_bundled_callable:  s     #:#9#9::r.   c                      | j                   | S r#   )r   r1   s     r+   r3   zAOTCompiledArtifact.__call__@  s    t}}d##r.   r4   r5   c               0   |dk(  rt        d      | j                         }ddlm} ddlm}  |       }|j                  t        j                         |j                   |              |j                  |       ddl	m
}  |||j                                y )Nr;   8AOTCompiledArtifact does not support unpacked format yetr   ru   r   r@   rw   )rT   	serializerF   rv   rG   rA   r~   rJ   rK   r   rx   r   )r(   r8   r6   rW   rv   rA   r   rx   s           r+   r9   zAOTCompiledArtifact.saveC  s     ZJ  ~~'F(.99:9;'<(: 	T6??,-r.   c                @    t        j                  | j                        S r#   )r   serialize_compile_artifactsr   )r(   s    r+   r   zAOTCompiledArtifact.serializeY  s    5QQMM
 	
r.   c                z    t        j                  |       }t        |t               sJ t        j	                  |      S r#   )r   deserialize_compile_artifacts
isinstancerJ   r   )rW   deserializeds     r+   rM   zAOTCompiledArtifact.deserialize^  s>     3PP 	
 ,(NOOO"88FFr.   c                   |dk(  rt        d      t        | d      5 }ddlm} ddlm} |j                         } ||      }|j                         }|t        j                  k(  sJ |j                          |       k(  sJ |j                         }|j                         sJ t        j                  |      cd d d        S # 1 sw Y   y xY w)Nr;   r   r=   r   r>   r   r@   )rT   rE   rF   r?   rG   rA   rH   rI   rJ   rK   rL   rM   )	r8   r6   rV   r?   rA   rW   rX   rY   rZ   s	            r+   rD   zAOTCompiledArtifact.loadh  s     ZJ  $ 	=J,99;L .F&&(F0;;;;;$$&)+555((*H%%'''&228<	= 	= 	=s   BCC
N)r)   r^   )r   r   r`   rJ   r_   ra   )r`   r   )rW   r   r`   rJ   re   )rf   rg   rh   ri   r   rK   r,   rj   r   r3   r9   r   rM   rD   r0   r.   r+   rJ   rJ   &  s     ,g6J
'
 ;:;	; ;
$ EM..$A.	.,

 G G >F==;=	= =r.   rJ   c                    | dk(  S )Nr   r0   )dynamic_shapess    r+   _resolve_ignore_shape_envr     s     222r.   c                ~   |dk(  r/||j                   t        d      |S t        t                     S |t        d      |dk(  rBt        j
                  j                  j                         }|j                  J |j                  S |dk(  r!t        t        t        | j                  j                                    }|j                  dk(  sJ t        |j                         dk(  sJ t#        |j                   d	   t        j$                  j&                        r|j                   d	   gn|j                   d	   }|D ]\  }d
|j(                  v s|j(                  d
   }t#        |t        j*                  j,                  j.                        sP|j                  c S  t        t                     S t        d| d      )Nr   zgstandalone_compile requires `fake_mode` to have a ShapeEnv when `dynamic_shapes="from_example_inputs"`.r   zastandalone_compile only supports passing `fake_mode` when `dynamic_shapes="from_example_inputs"`.r   r   outputr   r   example_valuezJstandalone_compile got unsupported `dynamic_shapes` value: dynamic_shapes=.)r   
ValueErrorr   r   rP   r   r   get	fake_modenextiterreversedr   nodesopro   r2   r   fxNodemeta_subclassesfake_tensor
FakeTensor)gmr   r   r   	last_noder   nodemaybe_tensors           r+   _resolve_fake_moder     s   
 .. ""* C  
33		6
 	
 
1	1 --..224  ,,,   	<	'
 hrxx~~678	||x'''9>>"a''' )..+UXX]]; ^^A" 	
  	2D$))+#yy9lE,=,=,I,I,T,TU'111		2 
33XYgXhhij
 	
r.   c           	   #    K   ddl m} t        | ||      }t        j                  j                  |      }t        j                  j                  |      5  |j                         5  t        j                  dd      5  t        j                  j                  j                  |dd      5  d  d d d        d d d        d d d        d d d        y # 1 sw Y   "xY w# 1 sw Y   &xY w# 1 sw Y   *xY w# 1 sw Y   y xY ww)Nr   )CacheArtifactManagerztriton.autotune_at_compile_timeT)bundled_autograd_cache force_non_lazy_backward_lowering)torch.compiler._cacher   r   rP   r   r   r   with_fresh_cacher   r   r   )r   r   aotr   r   resolved_fake_modetracing_contexts          r+   _standalone_contextr     s      ;+B	Jmm223EFOo.--/ 	6= 	%%*-
 59		
	 	           sf   AC?C3%C'<-C)C	.C6C'>C3	C?CCC$ C''C0	,C33C<8C?F)r   donate_graph_moduler   c                  ddl m } t        |      }t        | |||      5  |s(t               5  t	        j
                  |       } ddd       t        |       } t        |       }  || |fd|i|}	t        |	      sJ |r+t        |	d      st        d      t        |	      cddd       S t        j                  j                         }
|
t        j!                  d       ddd       t#        	
      S # 1 sw Y   xY w# 1 sw Y   !xY w)z=
    Implementation of torch.inductor.standalone_compile
    r   r   N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   r   r   r   copydeepcopyr   r   callablehasattrrT   rJ   rP   rQ   save_cache_artifactsr   warningrC   )r   example_inputsr   optionsr   r   r   r   r   r)   r*   s              r+   standalone_compiler     s    '0@	Ri	@ ! #?A ']]2&' 03,R0 
1A
EL
 $$$;4"R  '{37! !8 NN779	KKY=!F !i887' '! !s)   C<C0AC<'5C<0C9	5C<<Dc                    ddl m} t        |      }t        | |||      5  |j	                  | ||      cd d d        S # 1 sw Y   y xY w)Nr   r   )r   ) r   r   r   autograd_cache_key)r   r   r   r   r   r   r   s          r+   r   r     sQ     0@	UNC	C 
,,- - 

 
 
s	   >A)r   DynamicShapesTyper#   )r   r   r   r   r   FakeTensorMode | Noner`   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   r`   r!   )FN)r   r   r   r   r   r   )>
__future__r   
contextlibr   loggingr|   r   r   abcr   r   r   r   typingr   r	   r
   r   torch.fxrP   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   torch.fx.graph_moduler   r   r   _functionalize_collectivesr   r   collections.abcr   r   r   r   r   	getLoggerrf   r   r!   rC   rJ   r   r   contextmanagerr   r   r   r0   r.   r+   <module>r     s   "    	   # : . . WX   R , @ < G 6 , : P  2/$ g!Ds DNpD, pDfV=* V=r3 (,7
7
%7
 %7
 	7
t 
 (,	% 
 %	 D  %'+3939'39 &	39
 39 
39 39 %39 39t '+
 &
 
	

 %
r.   