
    8h                         S SK r S SKrS SKJrJr  S SKrS SKJs  Jr  S SK	J
r
  S SKJr  S SKJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  \R4                  " \5      r " S	 S
\
R:                  5      r " S S\5      rg)    N)AnyCallable)ir)KernelTemplate)Bufferget_free_symbolsget_symbolic_inputsgm_original_output_stridesir_node_to_tensorLayout)benchmarker)do_bench_using_profiling)Vc                      ^  \ rS rSrSrS\S\\   S\S\S\	S\
4   S	S
4U 4S jjrS	\4S jrS\\
   S\R                  S	\4S jrS	\4S jrS	\R&                  4S jrS	\\\
4   4S jrS	\4S jrSrU =r$ )SubgraphChoiceCaller   z
Represents a Subgraph Autotuning choice, and the subgraph can be any arbitrary
GraphModule. Compiles the Subgraph down to a module for benchmarking.
nameinput_nodeslayoutdescriptionmake_fx_graph.returnNc           	      L  > [         TU ]  XX45        / U l        [        R                     U R
                   H  n[        [        UR                  5       SS95      S:X  d   e[        [        UR                  5       SS95      S:X  d   eUR                  R                  5         U R                  R                  [        U5      5        M     S S S 5        U" U R                  6 U l        [        U R                  5        [!        U R
                  5      U l        g ! , (       d  f       NR= f)NT)unbacked_onlyr   )super__init__example_inputsr   	fake_moder   lenr   get_size
get_stridedatafreeze_layoutappendr   gmr
   r	   
sym_inputs)selfr   r   r   r   r   inp	__class__s          Z/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_inductor/codegen/subgraph.pyr   SubgraphChoiceCaller.__init__   s     	F@ [[''+CLLN$OPTUUUU+CNN,<DQRVWWWW&&(##**+<S+AB (   !4!45"477+-d.>.>? [s   B D
D#c                 "    SU R                    S3$ )NzSubgraphCaller()r   r'   s    r*   __str__SubgraphChoiceCaller.__str__8   s     1--    argsoutc                x  ^^^ SS K Js  Jn  SSKJn  U" U R
                  U R                  [        R                  R                  [        R                  R                  [        R                  R                  [        R                  R                  [        R                  R                  [        R                  R                  SU R                   3S9	nU R                    H@  nXeR"                  UR                  '   UR$                  R'                  UR                  5        MB     U R                    Vs/ s H?  n[)        [        R                  R*                  R,                  R/                  U5      5      PMA     snm[1        T5      S:X  a  [3        TU R                  5       H  u  p[5        U[6        R8                  5      (       d  M&  [5        U	[6        R8                  5      (       d   eUR:                  U	R:                  :X  d   eUR=                  5       U	R=                  5       :X  a  M   e   [1        T5      S:X  a  [3        TU R                  5       H  u  p[5        U[6        R8                  5      (       d  M&  [5        U	[6        R8                  5      (       d   eUR:                  U	R:                  :X  d   eUR=                  5       U	R=                  5       :X  a  M   e   [        R>                  " U5         URA                  SSSS9   URB                  " U R                  6   URE                  5       n
U
RF                  mT" / TQTQ5        S S S 5        S S S 5        [        RH                  (       a  [K        UUU4S j5      $ [L        RN                  " UUU4S	 j5      $ s  snf ! , (       d  f       N]= f! , (       d  f       Nf= f)
Nr   )GraphLowering
benchmark_)	r%   r   	shape_envcpp_wrapperaot_modeextern_node_serializeris_inferenceis_backwardr   FATEN)max_autotunemax_autotune_gemmmax_autotune_gemm_backendsc                     > T" / TQT Q5      $ N r3   bm_funcr&   s   r*   <lambda>0SubgraphChoiceCaller.benchmark.<locals>.<lambda>z   s    G<Pj<P4<P4Qr2   c                     > T" / TQT Q5      $ rC   rD   rE   s   r*   rG   rH   {   s    9M:9M9M1Nr2   )(torch._inductor.config	_inductorconfigtorch._inductor.graphr6   r%   r   r   graph
_shape_envr9   r:   r;   r<   r=   r   r&   graph_inputsgraph_input_namesr$   intsizevarsr8   	size_hintr   zip
isinstancetorchTensorshapestrideset_graph_handlerpatchruncompile_to_modulecall/profile_bandwidth_with_do_bench_using_profilingr   r   benchmark_gpu)r'   r4   r3   inductor_configr6   bm_graph_loweringsym_inpsym_vararexample_inpmodrF   r&   s     `        @@r*   	benchmarkSubgraphChoiceCaller.benchmark;   s    	987)ww..gg((++WW%%#$77#A#A--++dii[)

 G;B**7<<8//66w||D '  ??
*   **44W=>*


 z?a $'tT-@-@#Ab%,,//%k5<<@@@@88{'8'888899;+*<*<*>>>> $B z?a $'tT-@-@#Ab%,,//%k5<<@@@@88{'8'888899;+*<*<*>>>> $B   !23 &&""'+1 ' 
 "%%t':':;'99;((,*,t,- 4 AA+,QRR(()NOOS
:  43s,   0AN/N+ANN+
N(	$N++
N9c           
         SR                  U R                  R                  SS5      S   /U R                   Vs/ s H  n[	        UR                  5       5      PM     snQU R                   Vs/ s H  n[	        UR                  5       5      PM     snQ[	        U R                  R                  5      P5      $ s  snf s  snf )N-_   r   )	joinr   rsplitr   strr    r!   r%   rN   )r'   r(   s     r*   hash_keySubgraphChoiceCaller.hash_key}   s    xx		  a(+151A1AB1A##clln%1AB 483C3CD3CC#cnn&'3CD DGGMM"	
 	
 CDs   #B7
-#B<
c           
          [         R                  R                  [         R                  " U R                  U R
                  U R                  U R                  U R                  S95      $ )N)r   r   r%   r   subgraph_name)	r   	TensorBoxcreateSubgraphBufferr   r   r%   r   r   r/   s    r*   output_node SubgraphChoiceCaller.output_node   sN    ||""{{ ,,77#22"ii
 	
r2   c                      SU R                   S.$ )zRInformation returned here is logged to the autotune log file when that is enabled.subgraph)backendkernel_namer.   r/   s    r*   	info_dictSubgraphChoiceCaller.info_dict   s     "99
 	
r2   c                      SU R                    3$ )N	subgraph_r.   r/   s    r*   autoheuristic_id%SubgraphChoiceCaller.autoheuristic_id   s    499+&&r2   )r   r%   r&   )__name__
__module____qualname____firstlineno____doc__rq   listr   r   r   r   r   r0   rW   rX   floatri   rr   r   rv   ry   dictr   r   __static_attributes____classcell__)r)   s   @r*   r   r      s    
@@ &\@ 	@
 @  S)@ 
@2. .@PtCy @Pu|| @P @PD
# 
	
R\\ 	

4S> 
'# ' 'r2   r   c                   v    \ rS rSrSr\R                  " 5       rS\S\	S\
4   4S jrS\\   S\S	\
S
\4S jrSrg)SubgraphTemplate   z
A template for subgraph evaluation to be used in autotuning.

This class allows creating customized subgraphs that can be appended
as choices during the autotuning process, enabling the selection of
optimal implementations for complex operations.
r   r   .c                 V    U S[        [        R                  5       3U l        X l        g)zd
Initialize a subgraph template.

Args:
    name: The name of this template
    graph: The FX graph
rm   N)nextr   index_counterr   r   )r'   r   r   s      r*   r   SubgraphTemplate.__init__   s)     fAd#3#A#ABCD	*r2   r   r   kwargsr   c                 D    [        U R                  UUSU R                  S9$ )a  
Generate a SubgraphChoiceCaller instance for autotuning.

Args:
    input_nodes: List of input nodes to the subgraph
    layout: Memory layout information for the output
    example_inputs: Example tensor inputs used to trace and benchmark the subgraph
    **kwargs: Additional keyword arguments

Returns:
    SubgraphChoiceCaller: A callable object that can be used for autotuning
 )r   r   r   r   r   )r   r   r   )r'   r   r   r   s       r*   generateSubgraphTemplate.generate   s*    & $#,,
 	
r2   )r   r   N)r   r   r   r   r   	itertoolscountr   rq   r   r   r   r   r   r   r   r   r   rD   r2   r*   r   r      sd     OO%M++  S)+
&\
 
 	

 

r2   r   ) r   loggingtypingr   r   rW   rJ   rK   rL   torch._inductorr   torch._inductor.codegen.commonr   torch._inductor.irr   r   r	   r
   r   r   $torch._inductor.runtime.benchmarkingr   torch._inductor.utilsr   torch._inductor.virtualizedr   	getLoggerr   logChoiceCallerr   r   rD   r2   r*   <module>r      sc         ' '  9  = : ) !A'2?? A'H3
~ 3
r2   