ó
    Eñi6/  ã                   óð   • S SK r S SKrS SKJr  S SKJr  S SKJrJr  S SK	r	S SK
JrJr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  S S	KJr   " S
 S5      r " S S\5      r " S S5      r " S S\5      rg)é    N)ÚCallable)Úpartial)ÚAnyÚOptional)Ú	AHContextÚ
AHMetadataÚAHOperationÚChoiceÚ
CHOICE_COLÚFeedbackÚFEEDBACK_COLÚget_metadata_str_from_log)ÚLearnedHeuristicController)ÚChoiceCaller)Ú	cache_dir)Úget_gpu_shared_memoryc                   óH   • \ rS rSrSrS\\/\4   SS4S jrS\S\4S jr	S	r
g)
ÚLocalFeedbacké   a'  
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                 ó   • Xl         g ©N©r   )Úselfr   s     Úh/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/_inductor/autoheuristic/autoheuristic.pyÚ__init__ÚLocalFeedback.__init__!   s   € Ø&Õó    Úchoicec                 ó$   • U R                  U5      $ r   r   ©r   r    s     r   Ú__call__ÚLocalFeedback.__call__$   s   € Ø×Ñ Ó'Ð'r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r
   r   r   r#   Ú__static_attributes__© r   r   r   r      s9   † ñð' H¨f¨X°xÐ-?Ñ$@ð 'ÀTô 'ð(˜vð (¨(÷ (r   r   c                   ó   • \ rS rSrSrSrg)ÚInconsistentMetadataé(   z´
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.
r+   N)r%   r&   r'   r(   r)   r*   r+   r   r   r-   r-   (   s   † ôr   r-   c                   óB  • \ rS rSr% Sr\\\4   \S'     SS\	/ \4   S\
\   S\\   S\S	\S
\\
\      S\\	\\/\4      SS4S jjrS\4S jrS\4S jr SS\S\\
\      S\\
\      4S jjrS\S\4S jr\S\4S j5       rS\4S jrS\4S jrS\S\SS4S jrSrg)ÚAutoHeuristicé/   a  
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                 óî  • Xl         X l        X0l        X@l        XPl        0 U l        X`l        [        [        5       [        R                  R                  5       U R                  U R                  5      U l        Xpl        U R                  5       (       d  g[        R                  R                   R"                  S:X  a  U R%                  5       U l        O)[        R                  R                   R"                  U l        [        R                  R                   R)                  U R                  5      (       aD  U R                  b6  U R                   H%  nU R                  U5      n	U R+                  X‰5        M'     ggg)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)r3   r4   r5   r6   r7   r2   r8   r   r   ÚtorchÚcudaÚget_device_capabilityÚmetadatar9   Úsatisfies_preconditionÚ	_inductorÚconfigÚautoheuristic_log_pathÚget_default_log_pathÚlog_pathÚcollect_autoheuristicÚ	save_data)
r   r3   r4   r5   r6   r7   r8   r9   r    Úfeedback_vals
             r   r   ÚAutoHeuristic.__init__8   s  € ð. !ŒØŒØ ŒØŒØŒ	Ø"$ˆÔØ.ÔÜ"Ü!Ó#Ü�J‰J×,Ñ,Ó.Ø�L‰LØ�I‰Ió	
ˆŒð )Ôà×*Ñ*×,Ñ,Øä�?‰?×!Ñ!×8Ñ8¸IÓEØ ×5Ñ5Ó7ˆD�Mä!ŸO™O×2Ñ2×IÑIˆDŒMä�?‰?×!Ñ!×7Ñ7¸¿	¹	×BÑBØ�}‰}Ñ(Ø"Ÿlœl�FØ#'§=¡=°Ó#8�LØ—N‘N 6Ö8ò +ð )ð Cr   c                 óx   • U R                   S L =(       d&    U R                  U R                  U R                  5      $ r   )r9   r?   r6   )r   s    r   r@   Ú$AutoHeuristic.satisfies_preconditionl   s4   € Ø× Ñ  DÐ(÷ 
¨D×,=Ñ,=Ø�M‰M˜4Ÿ<™<ó-
ð 	
r   c                 óè  • U R                  5       (       d  U R                  5       $ [        R                  R                  R                  U R                  5      (       a†  U R                  b%  U R                  R                  U R                  5        [        U R                  U R                  5      nUR                  5       nX R                  ;  a  U R                  5       $ Ub  U$ U R                  5       $ )zë
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@   r3   r<   rA   rB   Úuse_autoheuristicr7   r8   r6   Úapply_operationsr   r?   Úget_decisionr4   )r   Ú
controllerÚdecisions      r   Ú
get_choiceÚAutoHeuristic.get_choiceq   s·   € ð ×*Ñ*×,Ñ,Ø—=‘=“?Ð"ä�?‰?×!Ñ!×3Ñ3°D·I±I×>Ñ>Ø×#Ñ#Ñ/Ø—‘×-Ñ-¨d×.BÑ.BÔCÜ3Ø—‘Ø—‘óˆJð "×.Ñ.Ó0ˆHØŸ|™|Ó+à—}‘}“Ð&ØÑ#Ø�Ø�}‰}‹Ðr   Útop_kÚalways_includedc                 óº  • U R                  5       (       d  g [        R                  R                  R	                  U R
                  5      (       a�  U R                  b%  U R                  R                  U R                  5        [        U R                  U R                  5      nUR                  U5      nUc  g Ub!  U H  nXT;  d  M
  UR                  U5        M     U$ g r   )r@   r<   rA   rB   rM   r7   r8   r6   rN   r   r?   Úget_decisions_rankedÚappend)r   rT   rU   rP   r4   r    s         r   Úget_top_k_choicesÚAutoHeuristic.get_top_k_choicesŠ   s·   € ð ×*Ñ*×,Ñ,ØÜ�?‰?×!Ñ!×3Ñ3°D·I±I×>Ñ>Ø×#Ñ#Ñ/Ø—‘×-Ñ-¨d×.BÑ.BÔCÜ3Ø—‘Ø—‘óˆJð !×5Ñ5°eÓ<ˆGØ‰ØØÑ*Û-�FØÕ,ØŸ™ vÖ.ñ .ð ˆNØr   r    c                 ó:   • U R                   R                  US 5      $ r   )r2   Úgetr"   s     r   Úget_collected_feedbackÚ$AutoHeuristic.get_collected_feedback    s   € Ø×&Ñ&×*Ñ*¨6°4Ó8Ð8r   c                  ób   • [         R                  R                  5       R                  SS5      n U $ )NÚ Ú_)r<   r=   Úget_device_nameÚreplace)Údevice_names    r   Úget_device_identifierÚ#AutoHeuristic.get_device_identifier£   s)   € ô —j‘j×0Ñ0Ó2×:Ñ:¸3ÀÓDˆØÐr   c                 ó’   • U R                  5       n[        5        SU S3n[        R                  " USS9  X R                   S3-  nU$ )Nz/autoheuristic/Ú/T)Úexist_okz.txt)re   r   ÚosÚmakedirsr7   )r   rd   Úpaths      r   rD   Ú"AutoHeuristic.get_default_log_path¬   sJ   € Ø×0Ñ0Ó2ˆÜ“+�˜o¨k¨]¸!Ð<ˆÜ
�Š�D 4Ò(Ø—9‘9�+˜TÐ"Ñ"ˆØˆr   c                 ó¬   • U R                   R                  5       nU R                  R                  5       u  nnX!S'   X1S'   [        R
                  " U5      $ )NÚnumerical_featuresÚcategorical_features)r?   Úto_dictr6   Ú&get_numerical_and_categorical_featuresÚjsonÚdumps)r   Úmetadata_dictÚnum_featuresÚcat_featuress       r   Úserialize_metadataÚ AutoHeuristic.serialize_metadata³   sR   € ØŸ™×-Ñ-Ó/ˆð �L‰L×?Ñ?ÓAñ	
ØØà.:Ð*Ñ+Ø0<Ð,Ñ-Ü�zŠz˜-Ó(Ð(r   rH   c                 óÒ  • X R                   U'   U R                  n/ n[        R                  R	                  U5      nU(       a6  U R                  5       n[        U R                  5      nXv:w  a  [        S5      eO`UR                  U R                  5       5        U R                  R                  5       nUS-   [        -   S-   [        -   n	UR                  U	5        Sn
U R                  R                  5       nX«S-   U-   S-   [        U5      -   -  n
UR                  U
5        [        US5       nUR!                  SR#                  U5      S-   5        S S S 5        g ! , (       d  f       g = f)Nz/Given metadata does not match existing metadataÚ,Ú ÚaÚ
)r2   rE   rj   rl   Úexistsrx   r   r-   rX   r6   Úget_feature_names_csvr   r   Úget_feature_values_csvÚstrÚopenÚwriteÚjoin)r   r    rH   rE   ÚlinesÚ
log_existsr?   Úexisting_metadataÚfeature_headerÚheaderÚlineÚfeature_valuesÚfs                r   rG   ÚAutoHeuristic.save_data½   s/  € Ø*6×Ñ Ñ'Ø—=‘=ˆàˆÜ—W‘W—^‘^ HÓ-ˆ
Þà×.Ñ.Ó0ˆHÜ 9¸$¿-¹-Ó HÐØ Ó,Ü*ØEóð ð -ð
 �L‰L˜×0Ñ0Ó2Ô3Ø!Ÿ\™\×?Ñ?ÓAˆNØ# cÑ)¬JÑ6¸Ñ<¼|ÑKˆFØ�L‰L˜Ô àˆØŸ™×<Ñ<Ó>ˆØ Ñ$ vÑ-°Ñ3´c¸,Ó6GÑGÑGˆØ�‰�TÔä�(˜CÔ  AØ�G‰G�D—I‘I˜eÓ$ tÑ+Ô,÷ !× Ö ús   Ä+$EÅ
E&)
r8   r4   r2   r6   r3   r5   rE   r?   r7   r9   ©NNr   )r%   r&   r'   r(   r)   Údictr
   r   Ú__annotations__r   Úlistr   r   r   r‚   r	   r   Úboolr   r@   rR   ÚintrY   r   r]   Ústaticmethodre   rD   rx   rG   r*   r+   r   r   r0   r0   /   sS  ‡ ñð ˜V XÐ-Ñ.Ó.ð 8<ØJNñ29à˜2˜v˜:Ñ&ð29ð �f‘ð29ð ˜=Ñ)ð	29ð
 ð29ð ð29ð " $ {Ñ"3Ñ4ð29ð ˜x¨°YÐ(?ÀÐ(EÑFÑGð29ð 
õ29ðh
¨ô 
ð
˜Fô ð4 BFñØðØ+3°D¸±IÑ+>ðà	�$�v‘,Ñ	õð,9¨Vð 9¸ô 9ð ð 3ó ó ðð cô ð) Cô )ð- ð -°hð -À4÷ -r   r0   c                   óþ   ^ • \ rS rSrSr  SS\/ \\   4   S\\   S\\	   S\
S\S	\\\      S
\\\\
/\4      SS4U 4S jjjrS\\	   S\\   SS4S jrS\\   4S jr SS\S\\\      S\\\      4S jjrSrU =r$ )ÚAutoHeuristicSelectAlgorithméÚ   z´
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.
Nr3   r4   Úinput_nodesr6   r7   r8   r9   r   c           	      ó¸  >^• X0l         0 U l        U H  nX€R                  UR                  5       '   M!     [        U R                  R	                  5       5      n	S[
        4U4S jjn
[        TU ]  U
U	SUUUU5        [        R                  R                  R                  U R                  5      (       a(  U R                  5       (       a  U R                  X25        ggg)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                  ó:   >• T" 5       n U c  gU R                  5       $ )NÚunsure)Úautoheuristic_id)Úfallback_choicer3   s    €r   Úfallback_strÚ;AutoHeuristicSelectAlgorithm.__init__.<locals>.fallback_strö   s"   ø€ Ù&›jˆOØÑ&àØ"×3Ñ3Ó5Ð5r   N)r™   Úchoicestr2choicer�   r’   Úkeysr‚   Úsuperr   r<   rA   rB   rF   r7   r@   Úregister_global_feedback)r   r3   r4   r™   r6   r7   r8   r9   r    Úchoices_strrŸ   Ú	__class__s    `         €r   r   Ú%AutoHeuristicSelectAlgorithm.__init__à   sÆ   ù€ ð  'ÔØ9;ˆÔÛˆFØ?E×!Ñ! &×"9Ñ"9Ó";Ó<ñ ä˜4×0Ñ0×5Ñ5Ó7Ó8ˆð	6œc÷ 	6ô 	‰ÑØØØØØØØô	
ô �O‰O×"Ñ"×8Ñ8¸¿¹×CÑCØ×+Ñ+×-Ñ-à×)Ñ)¨+Õ?ð .ð Dr   c                 óp  ^ ^^	• SSK JnJmJm	  S[        S[        S[
        [        [        4   S[        S[        [           S[        [           S	[        / [
        [        [        4   4   S
[
        [        [        4   SS4UU	U 4S jjnT" U5      nT	" T R                  XR5      n[        XEU5      nU" U5        g)z[
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Útimingsr7   r™   r4   Úprofiled_timeÚprecompile_timesr   Nc                 ó²   >• T" U5      nX€:w  a  g T" X8U5      n	X‘:w  a  g UR                  5        H%  u  p«TR                  U
R                  5       U5        M'     g r   )ÚitemsrG   r�   )r¬   r­   r®   r7   r™   r4   r¯   r°   Úcurrent_inputs_keyÚcurrent_precompile_keyr    Útimerª   r«   r   s               €€€r   Ústore_global_feedbackÚTAutoHeuristicSelectAlgorithm.register_global_feedback.<locals>.store_global_feedback  s]   ø€ ñ "3°;Ó!?ÐØ!Ó2ØÙ%:Ø¨'ó&Ð"ð &Ó:ØØ '§¡¦‘�Ø—‘˜v×6Ñ6Ó8¸$Ö?ò !0r   )Ú torch._inductor.select_algorithmr©   rª   r«   r‚   r�   r   Úfloatr’   r   r   r7   r   )
r   r™   r4   r©   r¶   Ú
inputs_keyÚprecompile_keyÚfeedback_saverrª   r«   s
   `       @@r   r¤   Ú5AutoHeuristicSelectAlgorithm.register_global_feedback  sÚ   ú€ ÷	
ñ 	
ð	@Üð	@ä"ð	@ô œ,¬Ð-Ñ.ð	@ô ð		@ô
 œc™ð	@ô œ,Ñ'ð	@ô $ B¬¬\¼5Ð-@Ñ(AÐ$AÑBð	@ô #¤<´Ð#6Ñ7ð	@ð ÷	@ñ 	@ñ* ' {Ó3ˆ
Ù.¨t¯y©y¸*ÓNˆÜ Ð!6ÀNÓSˆÙ˜>Õ*r   c                 óZ   • U R                  5       nU R                  R                  US 5      $ r   )rR   r¡   r\   r"   s     r   Úget_choice_callerÚ.AutoHeuristicSelectAlgorithm.get_choice_caller4  s'   € Ø—‘Ó"ˆØ×$Ñ$×(Ñ(¨°Ó6Ð6r   rT   rU   c                 ót   • U R                  X5      nUc  g U Vs/ s H  o@R                  U   PM     sn$ s  snf r   )rY   r¡   )r   rT   rU   r4   r    s        r   Úget_top_k_choices_callerÚ5AutoHeuristicSelectAlgorithm.get_top_k_choices_caller8  s=   € ð ×(Ñ(¨Ó@ˆØ‰?ØÙ<CÓDºG°&×%Ñ% fÔ-¹GÑDÐDùÒDs   š5)r¡   r™   r�   r   )r%   r&   r'   r(   r)   r   r   r   r’   r   r   r‚   r	   r   r“   r   r¤   r¿   r”   rÂ   r*   Ú__classcell__)r¦   s   @r   r—   r—   Ú   s"  ø† ñð 8<ØJNñ+@à˜2˜x¨Ñ5Ð5Ñ6ð+@ð �lÑ#ð+@ð ˜#‘Yð	+@ð
 ð+@ð ð+@ð " $ {Ñ"3Ñ4ð+@ð ˜x¨°YÐ(?ÀÐ(EÑFÑGð+@ð 
÷+@ð +@ðZ%+Ø ™9ð%+Ø/3°LÑ/Að%+à	ô%+ðN7 8¨LÑ#9ô 7ð
 BFñEØðEØ+3°D¸±IÑ+>ðEà	�$�|Ñ$Ñ	%÷Eó Er   r—   ) rs   rj   Úcollections.abcr   Ú	functoolsr   Útypingr   r   r<   Ú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-   r0   r—   r+   r   r   Ú<module>rÎ      si   ðÛ Û 	Ý $Ý ß  ã ÷	÷ 	ó 	õõ ,Ý ;Ý 7÷(ñ (ô˜9ô ÷h-ñ h-ôVdE =õ dEr   