
    sg.                         d dl Z d dlZd dlmZ d dlmZmZ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 y)    N)partial)AnyCallableDictListOptional)	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     ^/var/www/html/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
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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/   r0   r1   r2   r-   r3   r
   r   torchcudaget_device_capabilitymetadatar4   satisfies_precondition	_inductorconfigautoheuristic_log_pathget_default_log_pathlog_pathcollect_autoheuristic	save_data)
r   r.   r/   r0   r1   r2   r3   r4   r    feedback_vals
             r   r   zAutoHeuristic.__init__7   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   )r4   r:   r1   )r   s    r   r;   z$AutoHeuristic.satisfies_preconditionk   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.
        )r;   r.   r7   r<   r=   use_autoheuristicr2   r3   r1   apply_operationsr   r:   get_decisionr/   )r   
controllerdecisions      r   
get_choicezAutoHeuristic.get_choicep   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   )r;   r7   r<   r=   rF   r2   r3   r1   rG   r   r:   get_decisions_rankedappend)r   rL   rM   rI   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 _)r7   r8   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)r[   r   osmakedirsr2   )r   rZ   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)r:   to_dictr1   &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   rC   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_   ra   existsrl   r   r*   rP   r1   get_feature_names_csvr   r   get_feature_values_csvstropenwritejoin)r   r    rC   r@   lines
log_existsr:   existing_metadatafeature_headerheaderlinefeature_valuesfs                r   rB   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'   r   r   r   __annotations__r   r   r   r   r	   ru   r   r
   boolr   r;   rK   intrQ   r   rT   staticmethodr[   r?   rl   rB   r(   r   r   r,   r,   .   sS    VX-.. 8<JN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9h
 

F 4 BF+3DI+>	$v,	,9V 9 9 3  c )C )- -h -4 -r   r,   c                        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_nodesr1   r2   r3   r4   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   listkeysru   superr   r7   r<   r=   rA   r2   r;   register_global_feedback)r   r.   r/   r   r1   r2   r3   r4   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                    	 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        |||      } ||       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timingsr2   r   r/   r   Nc                      
|      }|| k7  ry  |||      }||k7  ry |j                         D ]%  \  }}	j                  |j                         |	       ' y r   )itemsrB   r   )r   r   r   r2   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   ru   r   r   floatr   r   r2   r   )
r   r   r/   r   r   
inputs_keyprecompile_keyfeedback_saverr   r   s
   `       @@r   r   z5AutoHeuristicSelectAlgorithm.register_global_feedback  s    	
 	
	@	@"	@ ,-.	@ 		@
 c	@ ,'	@ 	@& '{3
.tyy*gN !6
NS>*r   c                 Z    | j                         }| j                  j                  |d       S r   )rK   r   rS   r"   s     r   get_choice_callerz.AutoHeuristicSelectAlgorithm.get_choice_caller1  s'    "$$((66r   rL   rM   c                 p    | j                  ||      }|y |D cg c]  }| j                  |    c}S c c}w r   )rQ   r   )r   rL   rM   r/   r    s        r   get_top_k_choices_callerz5AutoHeuristicSelectAlgorithm.get_top_k_choices_caller5  s=     ((@?<CD&%%f-DDDs   3r   r   )r$   r%   r&   r'   r   r   r   r   r   r	   ru   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
 BFEE+3DI+>E	$|$	%Er   r   )!rg   r_   	functoolsr   typingr   r   r   r   r   r7   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     	  6 6 	 	 	 , ; 7( (9 h- h-VbE= bEr   