
    9j/                         d dl Z d dlZd dlmZ d dlmZ d dl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y)    N)Callable)partial)Any)	AHContext
AHMetadataAHOperationChoice
CHOICE_COLFeedbackFEEDBACK_COLget_metadata_str_from_log)LearnedHeuristicController)ChoiceCaller)	cache_dir)get_gpu_shared_memoryc                   <    e Zd ZdZdeegef   ddfdZdedefdZy)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                     || _         y Nr   )selfr   s     k/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_inductor/autoheuristic/autoheuristic.py__init__zLocalFeedback.__init__!   s
    &    choicec                 $    | j                  |      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      s9    'HfXx-?$@ 'T '(v (( (r   r   c                       e Zd ZdZy)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                   $   e Zd ZU dZeeef   ed<   	 	 ddeg ef   de	e   de
dz  deded	e	e   dz  d
eeegef   dz  ddfdZdefdZdefdZ	 ddede	e   dz  de	e   dz  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y)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                  | j                        | _        || _        | j                         syt        j                  j                   j"                  dk(  r| j%                         | _        n)t        j                  j                   j"                  | _        t        j                  j                   j)                  | j                        rB| j                  5| j                  D ]%  }| j                  |      }	| j+                  ||	       ' yyy)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.   r/   r*   r0   r   r   torchcudaget_device_capabilitymetadatar1   satisfies_precondition	_inductorconfigautoheuristic_log_pathget_default_log_pathlog_pathcollect_autoheuristic	save_data)
r   r+   r,   r-   r.   r/   r0   r1   r   feedback_vals
             r   r   zAutoHeuristic.__init__8   s   . ! 	"$."!#JJ,,.LLII	
 )**,??!!88IE 557DM!OO22IIDM??!!77		B}}("ll 9F#'==#8LNN6<89 ) Cr   c                 n    | j                   d u xs& | j                  | j                  | j                        S r   )r1   r7   r.   )r   s    r   r8   z$AutoHeuristic.satisfies_preconditionl   s4      D( 
D,=,=MM4<<-
 	
r   c                    | j                         s| j                         S t        j                  j                  j                  | j                        r| j                  %| j                  j                  | j                         t        | j                  | j                        }|j                         }|| j                  vr| j                         S ||S | j                         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.
        )r8   r+   r4   r9   r:   use_autoheuristicr/   r0   r.   apply_operationsr   r7   get_decisionr,   )r   
controllerdecisions      r   
get_choicezAutoHeuristic.get_choiceq   s     **,==?"??!!33DII>##/--d.B.BC3J "..0Ht||+}}&#}}r   top_kalways_includedc                    | j                         sy t        j                  j                  j	                  | j
                        r| j                  %| j                  j                  | j                         t        | j                  | j                        }|j                  |      }|y ||D ]  }||vs|j                  |        |S y r   )r8   r4   r9   r:   rC   r/   r0   r.   rD   r   r7   get_decisions_rankedappend)r   rI   rJ   rF   r,   r   s         r   get_top_k_choiceszAutoHeuristic.get_top_k_choices   s     **,??!!33DII>##/--d.B.BC3J !55e<G*- /FW,v./ Nr   r   c                 :    | j                   j                  |d       S r   )r*   getr   s     r   get_collected_feedbackz$AutoHeuristic.get_collected_feedback   s    &&**6488r   c                  b    t         j                  j                         j                  dd      } | S )N _)r4   r5   get_device_namereplace)device_names    r   get_device_identifierz#AutoHeuristic.get_device_identifier   s)     jj002::3Dr   c                     | j                         }t                d| d}t        j                  |d       || j                   dz  }|S )Nz/autoheuristic//T)exist_okz.txt)rX   r   osmakedirsr/   )r   rW   paths      r   r<   z"AutoHeuristic.get_default_log_path   sL    002+ok]!<
D4(499+T""r   c                     | j                   j                         }| j                  j                         \  }}||d<   ||d<   t	        j
                  |      S )Nnumerical_featurescategorical_features)r7   to_dictr.   &get_numerical_and_categorical_featuresjsondumps)r   metadata_dictnum_featurescat_featuress       r   serialize_metadataz AutoHeuristic.serialize_metadata   sV    --/ LL??A	
.:*+0<,-zz-((r   r@   c                    || j                   |<   | j                  }g }t        j                  j	                  |      }|r5| j                         }t        | j                        }||k7  rkt        d      |j                  | j                                | j                  j                         }|dz   t        z   dz   t        z   }	|j                  |	       d}
| j                  j                         }|
|dz   |z   dz   t        |      z   z  }
|j                  |
       t        |d      5 }|j!                  dj#                  |      dz          d d d        y # 1 sw Y   y xY w)Nz/Given metadata does not match existing metadata, a
)r*   r=   r\   r^   existsri   r   r'   rM   r.   get_feature_names_csvr
   r   get_feature_values_csvstropenwritejoin)r   r   r@   r=   lines
log_existsr7   existing_metadatafeature_headerheaderlinefeature_valuesfs                r   r?   zAutoHeuristic.save_data   s9   *6'==WW^^H-
..0H 9$-- H H,*E  LL0023!\\??AN#c)J6<|KFLL <<>$v-3c,6GGGT(C  	-AGGDIIe$t+,	- 	- 	-s   '$EENNr   )r!   r"   r#   r$   dictr	   r   __annotations__r   listr   r   rr   r   r   boolr   r8   rH   intrN   r   rQ   staticmethodrX   r<   ri   r?   r%   r   r   r)   r)   /   sR    VX-.. 59GK292v:&29 f29  $&	29
 29 29 k*T129 
I6<=D29 
29h
 

F 4 ?C+/9t+;	f	,9V 9 9 3  c )C )- -h -4 -r   r)   c                        e Zd ZdZ	 	 ddeg edz  f   dee   dee   dede	dee
   dz  d	eeegef   dz  d
df fdZdee   dee   d
dfdZd
edz  fdZ	 ddedee	   dz  d
ee   dz  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.   r/   r0   r1   r   c           	         || _         i | _        |D ]  }|| j                  |j                         <   ! t        | j                  j	                               }	dt
        ffd}
t        |   |
|	d||||       t        j                  j                  j                  | j                        r$| j                         r| j                  ||       yyy)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                  8            } | y| j                         S )Nunsure)autoheuristic_id)fallback_choicer+   s    r   fallback_strz;AutoHeuristicSelectAlgorithm.__init__.<locals>.fallback_str   s"    &jO&"3355r   N)r   choicestr2choicer   r   keysrr   superr   r4   r9   r:   r>   r/   r8   register_global_feedback)r   r+   r,   r   r.   r/   r0   r1   r   choices_strr   	__class__s    `         r   r   z%AutoHeuristicSelectAlgorithm.__init__   s      '9; 	FF?ED!!&"9"9";<	F4005578	6c 	6 		
 OO""88C++-))+w? . Dr   c                 p   	 ddl m}mm	 dt        dt        dt
        t        t        f   dt        dt        t           dt        t           d	t        g t
        t        t        f   f   d
t
        t        t        f   ddf	 fd} |      } 	 j                  ||      }t        |||      } ||       y)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timingsr/   r   r,   profiled_timeprecompile_timesr   Nc                      |      }|| k7  ry  |||      }	|	|k7  ry |j                         D ]%  \  }
}j                  |
j                         |       ' y r   )itemsr?   r   )r   r   r   r/   r   r,   r   r   current_inputs_keycurrent_precompile_keyr   timer   r   r   s               r   store_global_feedbackzTAutoHeuristicSelectAlgorithm.register_global_feedback.<locals>.store_global_feedback  sn     "3;!?!]2%:('&" &):: ' @v668$?@r   ) torch._inductor.select_algorithmr   r   r   rr   r   r   floatr   r   r   r/   r   )
r   r   r,   r   r   
inputs_keyprecompile_keyfeedback_saverr   r   s
   `       @@r   r   z5AutoHeuristicSelectAlgorithm.register_global_feedback  s    	
 	
	@	@"	@ ,-.	@ 		@
 c	@ ,'	@ $B\5-@(A$AB	@ #<#67	@ 	@* '{3
.tyy*gN !6
NS>*r   c                 Z    | j                         }| j                  j                  |d       S r   )rH   r   rP   r   s     r   get_choice_callerz.AutoHeuristicSelectAlgorithm.get_choice_caller4  s'    "$$((66r   rI   rJ   c                 p    | j                  ||      }|y |D cg c]  }| j                  |    c}S c c}w r   )rN   r   )r   rI   rJ   r,   r   s        r   get_top_k_choices_callerz5AutoHeuristicSelectAlgorithm.get_top_k_choices_caller8  s=     ((@?<CD&%%f-DDDs   3r~   r   )r!   r"   r#   r$   r   r   r   r   r   rr   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s    59GK+@2|d223+@ l#+@ #Y	+@
 +@ +@ k*T1+@ 
I6<=D+@ 
+@Z%+9%+/3L/A%+	%+N7<$#6 7
 ?CEE+/9t+;E	l	d	"Er   r   )rd   r\   collections.abcr   	functoolsr   typingr   r4   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      si     	 $   	 	 	 , ; 7( (9 h- h-VdE= dEr   