
    rh                         d dl Z d dlZd dlmZmZ d dlZd dlmc mZ d dl	m
Z
 d dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ  ej4                  e      Z G d	 d
e
j:                        Z G d de      Zy)    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                        e Zd ZdZdedee   dededede	f   dd	f fd
Z
defdZdee	   dej                  defdZdefdZdej$                  fdZdeee	f   fdZdefdZ xZS )SubgraphChoiceCallerz
    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         |   ||||       g | _        t        j                  5  | j
                  D ]  }t        t        |j                         d            dk(  sJ t        t        |j                         d            dk(  sJ |j                  j                          | j                  j                  t        |              	 d d d         || j                   | _        t        | j                         t!        | j
                        | _        y # 1 sw Y   MxY w)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          s/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_inductor/codegen/subgraph.pyr   zSubgraphChoiceCaller.__init__   s     	{FK@ [[ 	C'' C+CLLN$OPTUUUU+CNN,<DQRVWWWW&&(##**+<S+ABC	C  !4!45"477+-d.>.>?	C 	Cs   B"DD#c                 "    d| j                    dS )NzSubgraphCaller()r   r&   s    r)   __str__zSubgraphChoiceCaller.__str__8   s     1--    argsoutc                   dd l mc m} ddlm}  || j
                  | j                  t        j                  j                  t        j                  j                  t        j                  j                  t        j                  j                  t        j                  j                  t        j                  j                  d| j                   	      }| j                   D ]@  }||j"                  |j                  <   |j$                  j'                  |j                         B | j                   D cg c]>  }t)        t        j                  j*                  j,                  j/                  |            @ c}t1              dk(  rt3        | j                        D ]z  \  }}	t5        |t6        j8                        s!t5        |	t6        j8                        sJ |j:                  |	j:                  k(  sJ |j=                         |	j=                         k(  rzJ  t1              dk(  rt3        | j                        D ]z  \  }}	t5        |t6        j8                        s!t5        |	t6        j8                        sJ |j:                  |	j:                  k(  sJ |j=                         |	j=                         k(  rzJ  t        j>                  |      5  |jA                  ddd      5   |jB                  | j                    |jE                         }
|
jF                   g        d d d        d d d        t        jH                  rtK        fd      S tM        jN                  fd	      S c c}w # 1 sw Y   PxY w# 1 sw Y   TxY w)
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                       g        S N r0   bm_funcr%   s   r)   <lambda>z0SubgraphChoiceCaller.benchmark.<locals>.<lambda>z   s    G<Pj<P4<P4Q r/   c                       g        S r@   rA   rB   s   r)   rD   z0SubgraphChoiceCaller.benchmark.<locals>.<lambda>{   s    9M:9M9M1N r/   )(torch._inductor.config	_inductorconfigtorch._inductor.graphr3   r$   r   r   graph
_shape_envr6   r7   r8   r9   r:   r   r%   graph_inputsgraph_input_namesr#   intsizevarsr5   	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&   r1   r0   inductor_configr3   bm_graph_loweringsym_inpsym_vararexample_inpmodrC   r%   s     `        @@r)   	benchmarkzSubgraphChoiceCaller.benchmark;   s    	987)ww..gg((++WW%%#$77#A#A--++dii[)

  	EG;B**7<<8//66w||D	E  ??
   **44W=>


 z?a $'tT-@-@#A ?Kb%,,/%k5<<@@@88{'8'888899;+*<*<*>>>>? z?a $'tT-@-@#A ?Kb%,,/%k5<<@@@88{'8'888899;+*<*<*>>>>?   !23 	. &&""'+1 '  	.
 &!%%t':':;'99;((,*,t,-	.	. AA+,QRR(()NOOS
:	. 	.	. 	.s,   2AM0N$AM5&N5M>	:NN
c           
      v   dj                  | j                  j                  dd      d   g| j                  D cg c]  }t	        |j                                c}| j                  D cg c]  }t	        |j                                c}t	        | j                  j                              S c c}w c c}w )N-_   r   )	joinr   rsplitr   strr   r    r$   rJ   )r&   r'   s     r)   hash_keyzSubgraphChoiceCaller.hash_key}   s    xx		  a(+151A1AB##clln%B 483C3CDC#cnn&'D DGGMM"	
 	
 CDs    B1
* B6
c           	          t         j                  j                  t        j                  | j                  | j
                  | j                  | j                  | j                              S )N)r   r   r$   r   subgraph_name)	r   	TensorBoxcreateSubgraphBufferr   r   r$   r   r   r-   s    r)   output_nodez SubgraphChoiceCaller.output_node   sN    ||""{{ ,,77#22"ii
 	
r/   c                      d| j                   dS )zRInformation returned here is logged to the autotune log file when that is enabled.subgraph)backendkernel_namer,   r-   s    r)   	info_dictzSubgraphChoiceCaller.info_dict   s     "99
 	
r/   c                      d| j                    S )N	subgraph_r,   r-   s    r)   autoheuristic_idz%SubgraphChoiceCaller.autoheuristic_id   s    499+&&r/   )__name__
__module____qualname____doc__rl   listr   r   r   r   r   r.   rS   rT   floatre   rm   r   rp   rs   dictrx   r{   __classcell__)r(   s   @r)   r   r      s    
@@ &\@ 	@
 @  S)@ 
@2. .@PtCy @Pu|| @P @PD
# 
	
R\\ 	

4S> 
'# 'r/   r   c                   j    e Zd ZdZ ej
                         Zdedede	f   fdZ
dee   dede	d	efd
Zy)SubgraphTemplatea  
    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                 X    | dt        t        j                         | _        || _        y)z
        Initialize a subgraph template.

        Args:
            name: The name of this template
            graph: The FX graph
        rh   N)nextr   index_counterr   r   )r&   r   r   s      r)   r   zSubgraphTemplate.__init__   s+     fAd#3#A#ABCD	*r/   r   r   kwargsr   c                 J    t        | j                  ||d| j                        S )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)   generatezSubgraphTemplate.generate   s*    & $#,,
 	
r/   N)r|   r}   r~   r   	itertoolscountr   rl   r   r   r   r   r   r   r   r   rA   r/   r)   r   r      sf     $IOO%M++  S)+
&\
 
 	

 

r/   r   ) r   loggingtypingr   r   rS   rF   rG   rH   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   rA   r/   r)   <module>r      se         ' '  9  = : ) g!A'2?? A'H3
~ 3
r/   