
    ȅi!                         S SK r S SKrS SKrS SKJr  \R
                  " \5      rS/rS r	S r
S rS\R                  R                  4S jr " S	 S
5      r " S S5      rS rS r\" SS9S 5       rg)    N)compatibilityregional_inductorc                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                     > T" U 0 UD6$ N )argskwargsfns     [/home/james-whalen/.local/lib/python3.13/site-packages/torch/fx/passes/regional_inductor.pyinner_dummy_wrapper.<locals>.inner   s    4"6""    )	functoolswraps)r   r   s   ` r   _dummy_wrapperr      s%    __R# # Lr   c                     SSK Jn  SSKJn  U" XSS9nUR	                  5       nU" UR
                  U Vs/ s H  owR                  PM     snUSS9nU$ s  snf )Nr   )CapabilityBasedPartitioner)fuse_by_partitionsT)allows_single_node_partition)prefixalways_return_tuple)!torch.fx.passes.infra.partitionerr   !torch.fx.passes.utils.fuser_utilsr   propose_partitionsgraph_modulenodes)	gmsupported_opsr   r   r   partitionercandidate_partitions	partitionpartitioned_gms	            r   _partition_by_supported_nodesr$      se    LD,
K '99;'  *>?*>Y*>? 	N  	@s   A
c           	         SSK Jn  U R                  R                   GH  nUR                  S:X  d  M  UR
                  R                  U5      (       d  M8  / nUR                   HP  n[        US5      (       a0  SUR                  ;   a   UR                  UR                  S   5        MD  [        SU 35      e   [        XR
                  5      n0 nUR                  R                   H  n[        US5      (       d  M  UR                  R                  SS 5      (       d  M9  UR                  S   n	[        U	[        5      (       d  M_  SU	;   d  Mg  U	S   n
[        U
[        5      (       d  M  S	U
;   d  M  U
S	   n  O   [         R#                  S
UR
                  U5        SS KJs  Jn  U H"  n[        X5      (       a  M  [+        SU S35      e   UR-                  U5         [.        R&                  R1                  XdSSS9nS S S 5        [        WU5      (       d   e[3        U5      nU R                  R5                  U5         U R                  R7                  XR8                  UR:                  S9nUR                  Ul	        UR=                  U5        U R                  R?                  U5        U R@                  UR
                  	 S S S 5        GM     U RC                  5         U $ ! , (       d  f       N= f! , (       d  f       GM  = f)Nr   )AOTCompiledArtifactcall_modulemetavalz7Partition is bad because non fake tensor value is seen customcompile_with_inductorinductor_configsz0Compiling submodule %s with inductor options: %szInvalid inductor config key 'z_' in regional_inductor annotation. Available config keys can be found in torch._inductor.configfrom_tracing_contextT)dynamic_shapesaot)r	   r
   )""torch._inductor.standalone_compiler&   graphr   optarget
startswithall_input_nodeshasattrr(   appendRuntimeErrorgetattrget
isinstancedictloggerinfotorch._inductor.config	_inductorconfig
ValueErrorpatchtorchstandalone_compiler   inserting_aftercall_functionr	   r
   replace_all_uses_with
erase_node_modules	recompile)r   r   r&   nodefake_inputsinp_nodesubmodinductor_optionssub_noder*   compile_valueinductor_configkeycompiled_fncompiled_submodnew_nodes                   r   _compile_submodrX   -   s   F77m#(>(>v(F(FK 008V,,(--1G&&x}}U';<&QRZQ[\ 	 1 R-F
  ""LL..8V,,1B1B8T1R1R%]]84F!&$//4Kv4U(./F(G&}d;; 2m C/<=O/P,! / KKB  =< (44$7u =W X  ( !&&'78#oo@@8NTX A  9 k+>????,[9O))$/8811#))DKK 2  !%		**84##D)KK, 0/m ~ LLNI# 98 0/s    K<BK/
K,	/
K?	rL   c                     U R                   S;  =(       aH    [        U S5      =(       a5    U R                  R                  SS 5      =(       a    SU R                  S   ;   $ )N)placeholderoutputr(   r*   r+   )r2   r6   r(   r:   )rL   s    r   _needs_inductor_compiler\   s   sR    00 	;D&!	;IIMM(D)	; $tyy'::	r   c                   >    \ rS rSrSr\S 5       r\S 5       rS rSr	g)_RegionScooper|   zC
Scoops out the inductor marked regions. It does NOT compile them.
c                     SSK Jn  SnU R                  R                   H  n[	        U5      (       d  M  Sn  O   U(       d  [
        R                  S5        U $  " S SU5      nU" 5       n[        XS5      $ )	Nr   )OperatorSupportFTzNo inductor marked nodes foundc                   N    \ rS rSrS\R
                  R                  S\4S jrSr	g)9_RegionScooper.scoop_regions.<locals>.InductorMarkedNodes   rL   returnc                     [        U5      $ r   )r\   )self
submodulesrL   s      r   is_node_supportedK_RegionScooper.scoop_regions.<locals>.InductorMarkedNodes.is_node_supported   s    .t44r   r   N)
__name__
__module____qualname____firstlineno__rD   fxNodeboolri   __static_attributes__r   r   r   InductorMarkedNodesrc      s    5%((-- 5D 5r   rs   __marked_inductor_submod) torch.fx.passes.operator_supportra   r1   r   r\   r=   r>   r$   )r   ra   found_marked_noderL   rs   marked_nodess         r   scoop_regions_RegionScooper.scoop_regions   sq    D!HHNND&t,,$(! #
 !KK89I	5/ 	5 +,,8
 	
r   c                 :   U R                   R                  SS9 Hj  n[        U5      (       a  M  [        XR                  5      n[        U[        R                  R                  5      (       d  MU  [        R                  U5        Ml     [        R                  U 5      $ )Nget_attrr2   )r1   
find_nodesr\   r9   r3   r;   rD   ro   GraphModuler^   recursively_scoop_regionsrx   )r   rL   rO   s      r   r   (_RegionScooper.recursively_scoop_regions   st    HH'':'6D&t,, R-F&%(("6"67788@ 7 ++B//r   c                     [         R                  R                  R                  SS9   [        R                  U5      sS S S 5        $ ! , (       d  f       g = fNFenable)rD   ro   	tracebackpreserve_node_metar^   r   rg   r   s     r   __call___RegionScooper.__call__   s8    XX22%2@!;;B? A@@   A
Ar   N)
rk   rl   rm   rn   __doc__staticmethodrx   r   r   rr   r   r   r   r^   r^   |   s5     
 
, 0 0@r   r^   c                   >    \ rS rSrSr\S 5       r\S 5       rS rSr	g)_RegionCompiler   z#
Compiles the scooped out regions.
c                     SSK Jn  [        U S5      n U R                  R	                  U" 5       5        U R                  5         U $ )Nr   )_BoxedCodeGenrt   )torch.fx.graphr   rX   r1   set_codegenrK   )r   r   s     r   compile_region_RegionCompiler.compile_region   s4    0R!;<
]_-
	r   c                 4   SnU R                   R                  SS9 Hg  n[        XR                  5      n[	        U[
        R                  R                  5      (       d  MC  UR                  R                  S5      (       d  Me  SnMi     U R                   R                  SS9 HX  n[        XR                  5      n[	        U[
        R                  R                  5      (       d  MC  [        R                  U5        MZ     U(       a  [        R                  U 5      $ U $ )NFr'   r|   rt   Tr{   )r1   r}   r9   r3   r;   rD   ro   r~   r4   r   recursively_compile_regionsr   )r   found_regionrL   rO   s       r   r   +_RegionCompiler.recursively_compile_regions   s     HH''='9DR-F&%(("6"677;;))*DEE#'L	 : HH'':'6DR-F&%(("6"677;;FC 7
 "11"55	r   c                     [         R                  R                  R                  SS9   [        R                  U5      sS S S 5        $ ! , (       d  f       g = fr   )rD   ro   r   r   r   r   r   s     r   r   _RegionCompiler.__call__   s8    XX22%2@">>rB A@@r   r   N)
rk   rl   rm   rn   r   r   r   r   r   rr   r   r   r   r   r      s5        &Cr   r   c                     [         R                  R                  R                  SS9   [	        5       " U 5      sS S S 5        $ ! , (       d  f       g = fr   )rD   ro   r   r   r^   r   s    r   _create_inductor_marked_regionsr      s4    				.	.e	.	<# 
=	<	<   A
Ac                     [         R                  R                  R                  SS9   [	        5       " U 5      sS S S 5        $ ! , (       d  f       g = fr   )rD   ro   r   r   r   r   s    r    _compile_inductor_marked_regionsr      s4    				.	.e	.	< $ 
=	<	<r   F)is_backward_compatiblec                 $   [         R                  R                  R                  SS9   [	        U 5      n [        U 5      n [         R                  R                  R                  (       a  SSK	J
n  U" U 5      n U sSSS5        $ ! , (       d  f       g= f)a9  
Scoops out inductor marked regions and compiles them with inductor.

Inductor options should be provided via the annotation API:
with fx_traceback.annotate({
    "compile_with_inductor": {
        "inductor_configs": {
            "max_autotune": True,
            "triton.cudagraphs": False
        }
    }
}):
Fr   r   )RegionalOutputCodeN)rD   ro   r   r   r   r   
_functorchrA   force_autograd_cachetorch._inductor.output_coder   )r   example_argsr   s      r   r   r      sd    $ 
			.	.e	.	<,R0-b1""77F#B'B 
=	<	<s   AB
B)r   loggingrD   torch.fx._compatibilityr   	getLoggerrk   r=   __all__r   r$   rX   ro   rp   r\   r^   r   r   r   r   r   r   r   <module>r      s       1 
		8	$
&CL%((-- ,@ ,@^$C $CN$
%
 e, -r   