
    9jm)                     (   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ  ej                  e	      Z
dgZd Ze j                  ded   fd       Zd Zd	ej                   j"                  fd
Z G d d      Z G d d      Zd Zd Z ed      d        Zy)    N)Iterator)compatibilityregional_inductorc                 B     t        j                          fd       }|S )Nc                       | i |S N )argskwargsfns     a/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/fx/passes/regional_inductor.pyinnerz_dummy_wrapper.<locals>.inner   s    4"6""    )	functoolswraps)r   r   s   ` r   _dummy_wrapperr      s%    __R# # Lr   returnc               #      K   t         j                  j                  j                  d      5  d  d d d        y # 1 sw Y   y xY ww)NF)'remat_using_tags_for_fwd_loss_bwd_graph)torch
_functorchconfigpatchr	   r   r   &_disable_remat_for_regional_subcompiler      s<      
			 	 	&	&u	&	U   s   +A;	AA Ac           	         ddl m} | j                  j                  D ]  }|j                  dk(  s|j
                  j                  |      s0g }|j                  D ]H  }t        |d      r-d|j                  v r|j                  |j                  d          <t        d|        t        | |j
                        }i }|j                  j                  D ]r  }t        |d      s|j                  j                  dd       s-|j                  d   }	t        |	t              sMd|	v sR|	d   }
t        |
t              shd	|
v sm|
d	   } n t         j#                  d
|j
                  |       dd lmc m} |D ]  }t        ||      rt+        d| d       |j-                  |      5  t/               5  t0        j&                  j3                  ||dd      }d d d        d d d        t        |      st5        dt7        |             t9        |      }| j                  j;                  |      5  | j                  j=                  ||j>                  |j@                        }|j                  |_	        |jC                  |       | j                  jE                  |       | jF                  |j
                  = d d d         | jI                          | S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)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aotz"Expected AOTCompiledArtifact, got )r
   r   )%"torch._inductor.standalone_compiler   graphnodesoptarget
startswithall_input_nodeshasattrr   appendRuntimeErrorgetattrget
isinstancedictloggerinfotorch._inductor.config	_inductorr   
ValueErrorr   r   r   standalone_compileAssertionErrortyper   inserting_aftercall_functionr
   r   replace_all_uses_with
erase_node_modules	recompile)gmprefixr   nodefake_inputsinp_nodesubmodinductor_optionssub_noder    compile_valueinductor_configkeycompiled_fncompiled_submodnew_nodes                   r   _compile_submodrP   '   s   F F-77m#(>(>v(FK 00 8V,(--1G&&x}}U';<&QRZQ[\ 	 R-F
  ""LL.. 
"8V,1B1B8T1R%]]84F!&$/4Kv4U(./F(G&}d; 2m C/<=O/P,!
" KKB  =< ( 4$7u =W X   %%&67	68	 $oo@@#9	 A 		 	 k+>?$8k9J8KL  -[9O))$/ -8811#$))DKK 2  !%		**84##D)KK,- -F-P LLNI5	 	 	 	 - -s1   &K1$KK.BK+KKK(	+K5	rD   c                     | j                   dvxr= t        | d      xr/ | j                  j                  dd       xr d| j                  d   v S )N)placeholderoutputr   r    r!   )r)   r-   r   r1   )rD   s    r   _needs_inductor_compilerT   v   sR    00 	;D&!	;IIMM(D)	; $tyy'::	r   c                   8    e Zd ZdZed        Zedd       Zd Zy)_RegionScooperzK
    Scoops out the inductor marked regions. It does NOT compile them.
    c                 J   ddl m} ddlm} ddlm} t               }i }| j                  j                  D ]e  }t        |      s|j                  d   d   }t        |t              r
d|v r|d   }n|}|j                  |t                     j                  |       g |st         j#                  d       | S d	 }	g }
|j%                         D ]K  } | |	|            } || |d
      }|j'                         D ]  }|
j)                  |j                          M  || |
dd
      S )Nr   )CapabilityBasedPartitioner)create_op_support)fuse_by_partitionsr    r!   inductor_regionzNo inductor marked nodes foundc                       fd}|S )Nc                     |v S r   r	   )_submodulesrD   region_nodess     r   is_node_supportedzN_RegionScooper.scoop_regions.<locals>._is_in_region.<locals>.is_node_supported   s    |++r   r	   )r_   r`   s   ` r   _is_in_regionz3_RegionScooper.scoop_regions.<locals>._is_in_region   s    , %$r   T)allows_single_node_partition__marked_inductor_submod)rC   always_return_tuple)!torch.fx.passes.infra.partitionerrX    torch.fx.passes.operator_supportrY   !torch.fx.passes.utils.fuser_utilsrZ   objectr'   r(   rT   r   r2   r3   
setdefaultsetaddr4   r5   valuespropose_partitionsr.   )rB   rX   rY   rZ   _DEFAULT_REGIONregionsrD   rJ   ridra   all_partitionsr_   supportpartitioner	partitions                  r   scoop_regionsz_RegionScooper.scoop_regions   s-   PFH
 !(46HHNN 
	9D&t, $		( 34K L}d3)]:'(9:C)C""3.2248
	9 KK89I	% AC#NN, 	7L'l(CDG4G$K );;= 7	%%ioo67	7 "- $	
 	
r   Nc                    |
t               }| j                  j                  d      D ]  }t        |      rt	        | |j
                        }t        |t        j                  j                        sJt        |      |vsX|j                  t        |             t        j                  ||        t        j                  |       S )Nget_attrr)   )rj   r'   
find_nodesrT   r0   r*   r2   r   fxGraphModuleidrk   rV   recursively_scoop_regionsru   )rB   
_processedrD   rG   s       r   r}   z(_RegionScooper.recursively_scoop_regions   s    JHH'':'6 	MD&t, R-F 6588#7#78vJj0r&z*88L	M ++B//r   c                     t         j                  j                  j                  d      5  t        j                  |      cd d d        S # 1 sw Y   y xY wNFenable)r   rz   	tracebackpreserve_node_metarV   r}   selfrB   s     r   __call__z_RegionScooper.__call__   sD    XX22%2@ 	@!;;B?	@ 	@ 	@   A

Ar   )__name__
__module____qualname____doc__staticmethodru   r}   r   r	   r   r   rV   rV      s6     0
 0
d 0 0(@r   rV   c                   6    e Zd ZdZed        Zed        Zd Zy)_RegionCompilerz+
    Compiles the scooped out regions.
    c                     ddl m} t        | d      } | j                  j	                   |              | j                          | S )Nr   )_BoxedCodeGenrc   )torch.fx.graphr   rP   r'   set_codegenrA   )rB   r   s     r   compile_regionz_RegionCompiler.compile_region   s4    0R!;<
]_-
	r   c                    d}| j                   j                  d      D ][  }t        | |j                        }t	        |t
        j                  j                        s>|j                  j                  d      sZd}] | j                   j                  d      D ]R  }t        | |j                        }t	        |t
        j                  j                        s>t        j                  |       T |rt        j                  |       S | S )NFr   rx   rc   Trw   )r'   ry   r0   r*   r2   r   rz   r{   r+   r   recursively_compile_regionsr   )rB   found_regionrD   rG   s       r   r   z+_RegionCompiler.recursively_compile_regions   s     HH''='9 	(DR-F&%(("6"67;;))*DE#'L		( HH'':'6 	DDR-F&%(("6"67;;FC	D
 "11"55	r   c                     t         j                  j                  j                  d      5  t        j                  |      cd d d        S # 1 sw Y   y xY wr   )r   rz   r   r   r   r   r   s     r   r   z_RegionCompiler.__call__   sD    XX22%2@ 	C">>rB	C 	C 	Cr   N)r   r   r   r   r   r   r   r   r	   r   r   r   r      s5        &Cr   r   c                     t         j                  j                  j                  d      5   t	               |       cd d d        S # 1 sw Y   y xY wr   )r   rz   r   r   rV   rB   s    r   _create_inductor_marked_regionsr      s?    				.	.e	.	< $~#$ $ $   AAc                     t         j                  j                  j                  d      5   t	               |       cd d d        S # 1 sw Y   y xY wr   )r   rz   r   r   r   r   s    r    _compile_inductor_marked_regionsr      s?    				.	.e	.	< %  $% % %r   F)is_backward_compatiblec                    t         j                  j                  j                  d      5  t	        |       } t        |       } t         j                  j                  j                  rddl	m
}  ||       } | cddd       S # 1 sw Y   yxY w)a  
    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)r   rz   r   r   r   r   r   r   force_autograd_cachetorch._inductor.output_coder   )rB   example_argsr   s      r   r   r     sm    . 
			.	.e	.	< ,R0-b1""77F#B'B  s   A
A??B)
contextlibr   loggingcollections.abcr   r   torch.fx._compatibilityr   	getLoggerr   r4   __all__r   contextmanagerr   rP   rz   NoderT   rV   r   r   r   r   r	   r   r   <module>r      s       $  1 
		8	$
   L^%((-- O@ O@d$C $CN$
%
 e, -r   