
    `i.                        d dl Z d dlZd dlmZ d dlmZmZmZ d dlZd dl	m
Z
mZ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 d dlmZ  G d	 d
          Z G d de          Z G d d          Z G d de          ZdS )    N)partial)AnyCallableOptional)	AHContext
AHMetadataAHOperationChoice
CHOICE_COLFeedbackFEEDBACK_COLget_metadata_str_from_log)LearnedHeuristicController)ChoiceCaller)	cache_dir)get_gpu_shared_memoryc                   D    e Zd ZdZdeegef         ddfdZdedefdZdS )LocalFeedbacka7  
    To be able to collect data for a choice, a function providing feedback given a choice has to be provided.
    LocalFeedback can be used when AutoHeuristic should immediately run the function to collect feedback for each choice
    (see pad_mm.py, where the autotuning happens locally, for an example).
    feedback_fnreturnNc                     || _         d S Nr   )selfr   s     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/_inductor/autoheuristic/autoheuristic.py__init__zLocalFeedback.__init__    s    &    choicec                 ,    |                      |          S r   r   r   r   s     r   __call__zLocalFeedback.__call__#   s    '''r   )	__name__
__module____qualname____doc__r   r
   r   r   r!    r   r   r   r      sn         'HfXx-?$@ 'T ' ' ' '(v (( ( ( ( ( ( (r   r   c                       e Zd ZdZdS )InconsistentMetadataz
    Exception that is thrown when AutoHeuristic tries to log data to a file where the metadata stored in the file does
    not match the metadata it would store if the file didn't exist.
    N)r"   r#   r$   r%   r&   r   r   r(   r(   '   s           r   r(   c                   t   e Zd ZU dZeeef         ed<   	 	 ddeg ef         de	e         de
e         deded	e
e	e                  d
e
eeegef                  ddfdZdefdZdefdZ	 ddede
e	e                  de
e	e                  fdZdedefdZedefd            ZdefdZdefdZdededdfdZdS )AutoHeuristica  
    AutoHeuristic is a framework that allows one to collect data, learn a heuristic (i.e. a regression tree) and
    generate the heuristic to code. This class allows one to collect data. The collected data can then be used to train
    a heuristic (see torchgen/autoheuristic/).
    collected_feedbackNfallbackchoicesfeedbackcontextnameaugment_contextpreconditionr   c                    || _         || _        || _        || _        || _        i | _        || _        t          t                      t          j
                                        | j        | j                  | _        || _        |                                 sdS t          j        j        j        dk    r|                                 | _        nt          j        j        j        | _        t          j        j                            | j                  r<| j        7| j        D ]1}|                     |          }	|                     ||	           .dS dS dS )a  
        Initializes an instance of the AutoHeuristic class.

        Args:
            fallback: A callable that returns a Choice when the heuristic is unsure which choice to make, or
            AutoHeuristic is in data collection mode.
            choices: A list of possible choices the heuristic can make.
            feedback: An instance of LocalFeedback that provides feedback for a given choice.
            context: Context to store with each choice and feedback.
            name: A string that identifies the heuristic.
            augment_context: An optional list of AHOperation instances that augment the context.
            precondition: A callable that returns a boolean indicating whether AutoHeuristic should run.
        NDEFAULT)r,   r-   r.   r/   r0   r+   r1   r   r   torchcudaget_device_capabilitymetadatar2   satisfies_precondition	_inductorconfigautoheuristic_log_pathget_default_log_pathlog_pathcollect_autoheuristic	save_data)
r   r,   r-   r.   r/   r0   r1   r2   r   feedback_vals
             r   r   zAutoHeuristic.__init__7   s>   . ! 	"$."!##J,,..LI	
 
 )**,, 	F?!8IEE 5577DMM!O2IDM?!77	BB 	9}("l 9 9F#'==#8#8LNN6<8888		9 	9((9 9r   c                 T    | j         d u p|                      | j        | j                  S r   )r2   r8   r/   )r   s    r   r9   z$AutoHeuristic.satisfies_preconditionk   s2     D( 
D,=,=M4<-
 -
 	
r   c                    |                                  s|                                 S t          j        j                            | j                  ru| j        | j        	                    | j                   t          | j        | j                  }|                                }|| j        vr|                                 S ||S |                                 S )a  
        Returns the chosen option based on the value of autoheuristic_use.
        If self.name is one of the comma separated strings in autoheuristic_use,
        it queries a learned heuristic to make a decision. Otherwise, it returns the fallback option.
        )r9   r,   r5   r:   r;   use_autoheuristicr0   r1   r/   apply_operationsr   r8   get_decisionr-   )r   
controllerdecisions      r   
get_choicezAutoHeuristic.get_choicep   s     **,, 	#==??"?!33DI>> 	 #/--d.BCCC3 J "..00Ht|++}}&#}}r   top_kalways_includedc                 z   |                                  sd S t          j        j                            | j                  r{| j        | j                            | j                   t          | j
        | j                  }|                    |          }|d S ||D ]}||vr|                    |           |S d S r   )r9   r5   r:   r;   rD   r0   r1   r/   rE   r   r8   get_decisions_rankedappend)r   rJ   rK   rG   r-   r   s         r   get_top_k_choiceszAutoHeuristic.get_top_k_choices   s     **,, 	4?!33DI>> 	#/--d.BCCC3 J !55e<<Gt*- / /FW,,v...Ntr   r   c                 8    | j                             |d           S r   )r+   getr    s     r   get_collected_feedbackz$AutoHeuristic.get_collected_feedback   s    &**64888r   c                  j    t           j                                                            dd          } | S )N _)r5   r6   get_device_namereplace)device_names    r   get_device_identifierz#AutoHeuristic.get_device_identifier   s-     j0022::3DDr   c                     |                                  }t                       d| d}t          j        |d           || j         dz  }|S )Nz/autoheuristic//T)exist_okz.txt)rY   r   osmakedirsr0   )r   rX   paths      r   r=   z"AutoHeuristic.get_default_log_path   s\    0022++<<k<<<
D4((((49""""r   c                     | j                                         }| j                                        \  }}||d<   ||d<   t	          j        |          S )Nnumerical_featurescategorical_features)r8   to_dictr/   &get_numerical_and_categorical_featuresjsondumps)r   metadata_dictnum_featurescat_featuress       r   serialize_metadataz AutoHeuristic.serialize_metadata   sY    --// L??AA	
.:*+0<,-z-(((r   rA   c                    || j         |<   | j        }g }t          j                            |          }|r>|                                 }t          | j                  }||k    rt          d          nm|                    |                                            | j	        
                                }|dz   t          z   dz   t          z   }	|                    |	           d}
| j	                                        }|
|dz   |z   dz   t          |          z   z  }
|                    |
           t          |d          5 }|                    d                    |          dz              d d d            d S # 1 swxY w Y   d S )Nz/Given metadata does not match existing metadata, a
)r+   r>   r]   r_   existsrj   r   r(   rN   r/   get_feature_names_csvr   r   get_feature_values_csvstropenwritejoin)r   r   rA   r>   lines
log_existsr8   existing_metadatafeature_headerheaderlinefeature_valuesfs                r   r@   zAutoHeuristic.save_data   s   *6'=W^^H--
 	!..00H 9$- H H H,,*E   -
 LL0022333!\??AAN#c)J6<|KFLL   <<>>$v-3c,6G6GGGT(C   	-AGGDIIe$$t+,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   >,E77E;>E;NNr   )r"   r#   r$   r%   dictr
   r   __annotations__r   listr   r   r   rs   r	   r   boolr   r9   rI   intrO   r   rR   staticmethodrY   r=   rj   r@   r&   r   r   r*   r*   .   s           VX-.... 8<JN29 292v:&29 f29 =)	29
 29 29 "${"3429 xY(?(EFG29 
29 29 29 29h
 
 
 
 

F    4 BF +3DI+>	$v,	   ,9V 9 9 9 9 9 3    \c    )C ) ) ) )- -h -4 - - - - - -r   r*   c                   B    e Zd ZdZ	 	 ddeg ee         f         dee         dee         de	de
deee                  d	eeee	gef                  d
df fdZdee         dee         d
dfdZd
ee         fdZ	 ddedeee
                  d
eee                  fdZ xZS )AutoHeuristicSelectAlgorithmz
    AutoHeuristicSelectAlgorithm is a subclass of AutoHeuristic that allows one to collect data and learn a heuristic
    when one wants to use AutoHeuristic for kernel choice selection.
    Nr,   r-   input_nodesr/   r0   r1   r2   r   c           	         || _         i | _        |D ]}|| j        |                                <   t          | j                                                  }	dt
          ffd}
t                                          |
|	d||||           t          j	        j
                            | j                  r,|                                 r|                     ||           dS dS dS )a<  
        The arguments choices, input_nodes and name have to match the ones used in the call to
        autotune_select_algorithm(), e.g. if the following call is made
        autotune_select_algorithm(name, choices, input_nodes, layout), the same name, choices and input_nodes
        have to be used here.
        r   c                  H                 } | dS |                                  S )Nunsure)autoheuristic_id)fallback_choicer,   s    r   fallback_strz;AutoHeuristicSelectAlgorithm.__init__.<locals>.fallback_str   s+    &hjjO&x"33555r   N)r   choicestr2choicer   r   keysrs   superr   r5   r:   r;   r?   r0   r9   register_global_feedback)r   r,   r-   r   r/   r0   r1   r2   r   choices_strr   	__class__s    `         r   r   z%AutoHeuristicSelectAlgorithm.__init__   s"     '9; 	F 	FF?ED!&"9"9";";<<40557788	6c 	6 	6 	6 	6 	6 	6 		
 	
 	
 O"88CC	@++--	@ ))+w?????		@ 	@ 	@ 	@r   c                 J   	 ddl m}mm	 dt          dt          dt
          t          t          f         dt          dt          t                   dt          t                   d	d
f	 fd} |          } 	 j
        ||          }t          |||          } ||           d
S )zk
        Registers a callback in select_algorithm, which is called with the timing of each choice.
        r   )add_feedback_savercreate_inputs_keycreate_precompile_keyah_inputs_keyah_precompile_keytimingsr0   r   r-   r   Nc                      
|          }|| k    rd S  |||          }||k    rd S |                                 D ]-\  }}	                    |                                |	           .d S r   )itemsr@   r   )r   r   r   r0   r   r-   current_inputs_keycurrent_precompile_keyr   timer   r   r   s             r   store_global_feedbackzTAutoHeuristicSelectAlgorithm.register_global_feedback.<locals>.store_global_feedback  s     "3!2;!?!?!]22%:%:('& &" &)::: ' @ @v6688$????@ @r   ) torch._inductor.select_algorithmr   r   r   rs   r   r   floatr   r   r0   r   )
r   r   r-   r   r   
inputs_keyprecompile_keyfeedback_saverr   r   s
   `       @@r   r   z5AutoHeuristicSelectAlgorithm.register_global_feedback  s   	
 	
 	
 	
 	
 	
 	
 	
 	
 	
	@	@"	@ ,-.	@ 		@
 c	@ ,'	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@& '&{33
..ty*gNN !6
NSS>*****r   c                 `    |                                  }| j                            |d           S r   )rI   r   rQ   r    s     r   get_choice_callerz.AutoHeuristicSelectAlgorithm.get_choice_caller1  s*    ""$((666r   rJ   rK   c                 T                           ||          }|d S  fd|D             S )Nc                 *    g | ]}j         |         S r&   )r   ).0r   r   s     r   
<listcomp>zIAutoHeuristicSelectAlgorithm.get_top_k_choices_caller.<locals>.<listcomp>;  s!    DDD&%f-DDDr   )rO   )r   rJ   rK   r-   s   `   r   get_top_k_choices_callerz5AutoHeuristicSelectAlgorithm.get_top_k_choices_caller5  s>     ((@@?4DDDDGDDDDr   r   r   )r"   r#   r$   r%   r   r   r   r   r   r   rs   r	   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s         8<JN+@ +@2x556+@ l#+@ #Y	+@
 +@ +@ "${"34+@ xY(?(EFG+@ 
+@ +@ +@ +@ +@ +@Z#+9#+/3L/A#+	#+ #+ #+ #+J78L#9 7 7 7 7
 BFE EE+3DI+>E	$|$	%E E E E E E E Er   r   )re   r]   	functoolsr   typingr   r   r   r5   1torch._inductor.autoheuristic.autoheuristic_utilsr   r   r	   r
   r   r   r   r   :torch._inductor.autoheuristic.learned_heuristic_controllerr   torch._inductor.irr   %torch._inductor.runtime.runtime_utilsr   torch._inductor.utilsr   r   	Exceptionr(   r*   r   r&   r   r   <module>r      s    				       * * * * * * * * * * 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	      , + + + + + ; ; ; ; ; ; 7 7 7 7 7 7( ( ( ( ( ( ( (    9   h- h- h- h- h- h- h- h-VbE bE bE bE bE= bE bE bE bE bEr   