Ë
    ÄÓ^jƒ/  ã                   óì   — 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     úv/var/www/ramen.bs-engineer-server.com/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   „ ñð' H¨f¨X°xÐ-?Ñ$@ð 'À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                  «       rt        j                  j                  «       nd| 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.
        )r   r   NÚDEFAULT)r+   r,   r-   r.   r/   r*   r0   r   r   ÚtorchÚcudaÚis_availableÚ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   s1  € ð. !ˆŒØˆŒØ ˆŒØˆŒØˆŒ	Ø"$ˆÔØ.ˆÔÜ"Ü!Ó#Ü27·*±*×2IÑ2IÔ2KŒE�J‰J×,Ñ,Ô.ÐQWØ�L‰LØ�I‰Ió	
ˆŒð )ˆÔà×*Ñ*Ô,Øä�?‰?×!Ñ!×8Ñ8¸IÒEØ ×5Ñ5Ó7ˆD�Mä!ŸO™O×2Ñ2×IÑIˆDŒMä�?‰?×!Ñ!×7Ñ7¸¿	¹	ÔBØ�}‰}Ð(Ø"Ÿl™lò 9�FØ#'§=¡=°Ó#8�LØ—N‘N 6¨<Õ8ñ9ð )ð Cr   c                 ón   — | j                   d u xs& | j                  | j                  | j                  «      S r   )r1   r8   r.   )r   s    r   r9   z$AutoHeuristic.satisfies_preconditionl   s4   € Ø× Ñ  DÐ(ò 
¨D×,=Ñ,=Ø�M‰M˜4Ÿ<™<ó-
ð 	
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.
        )r9   r+   r4   r:   r;   Úuse_autoheuristicr/   r0   r.   Úapply_operationsr   r8   Úget_decisionr,   )r   Ú
controllerÚdecisions      r   Ú
get_choicezAutoHeuristic.get_choiceq   s³   € ð ×*Ñ*Ô,Ø—=‘=“?Ð"ä�?‰?×!Ñ!×3Ñ3°D·I±IÔ>Ø×#Ñ#Ð/Ø—‘×-Ñ-¨d×.BÑ.BÔCÜ3Ø—‘Ø—‘óˆJð "×.Ñ.Ó0ˆHØ˜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   )r9   r4   r:   r;   rD   r/   r0   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¸   € ð ×*Ñ*Ô,ØÜ�?‰?×!Ñ!×3Ñ3°D·I±IÔ>Ø×#Ñ#Ð/Ø—‘×-Ñ-¨d×.BÑ.BÔCÜ3Ø—‘Ø—‘óˆJð !×5Ñ5°eÓ<ˆ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   € Ø×&Ñ&×*Ñ*¨6°4Ó8Ð8r   c                  ó    — t         j                  j                  «       r0t         j                  j                  «       j	                  dd«      } | S y)Nú Ú_Únon_cuda_device)r4   r5   r6   Úget_device_nameÚreplace)Údevice_names    r   Úget_device_identifierz#AutoHeuristic.get_device_identifier£   s<   € ô �:‰:×"Ñ"Ô$ÜŸ*™*×4Ñ4Ó6×>Ñ>¸sÀCÓHˆKØÐØ r   c                 ó˜   — | j                  «       }t        «       › d|› d�}t        j                  |d¬«       || j                  › d�z  }|S )Nz/autoheuristic/ú/T)Úexist_okz.txt)rZ   r   ÚosÚmakedirsr/   )r   rY   Úpaths      r   r=   z"AutoHeuristic.get_default_log_path®   sL   € Ø×0Ñ0Ó2ˆÜ“+�˜o¨k¨]¸!Ð<ˆÜ
�‰�D 4Õ(Ø�4—9‘9�+˜TÐ"Ñ"ˆØˆr   c                 ó®   — | j                   j                  «       }| 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µ   sV   € ØŸ™×-Ñ-Ó/ˆð �L‰L×?Ñ?ÓAñ	
ØØà.:ˆÐ*Ñ+Ø0<ˆÐ,Ñ-Ü�z‰z˜-Ó(Ð(r   rA   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`   Úexistsrk   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¿   s9  € Ø*6ˆ×Ñ Ñ'Ø—=‘=ˆàˆÜ—W‘W—^‘^ HÓ-ˆ
Ùà×.Ñ.Ó0ˆHÜ 9¸$¿-¹-Ó HÐØ  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©NNr   )r!   r"   r#   r$   Údictr	   r   Ú__annotations__r   Úlistr   r   rt   r   r   Úboolr   r9   rI   ÚintrO   r   rR   ÚstaticmethodrZ   r=   rk   r@   r%   r   r   r)   r)   /   sR  … ñð ˜V XÐ-Ñ.Ó.ð 59ØGKñ29à˜2˜v˜:Ñ&ð29ð �f‘ð29ð   $Ñ&ð	29ð
 ð29ð ð29ð ˜kÑ*¨TÑ1ð29ð  
¨IÐ6¸Ð<Ñ=ÀÑDð29ð 
ó29ðh
¨ó 
ð
˜Fó ð4 ?CñØðØ+/°©9°tÑ+;ðà	ˆf‰˜Ñ	óð,9¨Vð 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"   ø€ Ù&›jˆOØÐ&àØ"×3Ñ3Ó5Ð5r   N)r‰   Úchoicestr2choicer�   rƒ   Úkeysrt   Úsuperr   r4   r:   r;   r?   r/   r9   Ú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Ø?EˆD×!Ñ! &×"9Ñ"9Ó";Ò<ð	Fä˜4×0Ñ0×5Ñ5Ó7Ó8ˆð	6œcõ 	6ô 	‰ÑØØØØØØØô	
ô �O‰O×"Ñ"×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™   rt   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ˆ
Ù.¨t¯y©y¸*ÀgÓNˆÜ Ð!6¸
ÀNÓSˆÙ˜>Õ*r   c                 óZ   — | j                  «       }| j                  j                  |d «      S r   )rI   r�   rQ   r   s     r   Úget_choice_callerz.AutoHeuristicSelectAlgorithm.get_choice_caller6  s'   € Ø—‘Ó"ˆØ×$Ñ$×(Ñ(¨°Ó6Ð6r   rJ   rK   c                 óp   — | j                  ||«      }|€y |D �cg c]  }| j                  |   ‘Œ c}S c c}w r   )rO   r�   )r   rJ   rK   r,   r   s        r   Úget_top_k_choices_callerz5AutoHeuristicSelectAlgorithm.get_top_k_choices_caller:  s=   € ð ×(Ñ(¨°Ó@ˆØˆ?ØØ<CÖD°&�×%Ñ% fÓ-ÒDÐDùÒDs   š3r€   r   )r!   r"   r#   r$   r   r   rƒ   r   r   rt   r   r   r„   r   r“   r«   r…   r­   Ú__classcell__)r•   s   @r   rˆ   rˆ   Ü   s  ø„ ñð 59ØGKñ+@à˜2˜|¨dÑ2Ð2Ñ3ð+@ð �lÑ#ð+@ð ˜#‘Yð	+@ð
 ð+@ð ð+@ð ˜kÑ*¨TÑ1ð+@ð  
¨IÐ6¸Ð<Ñ=ÀÑDð+@ð 
õ+@ðZ%+Ø ™9ð%+Ø/3°LÑ/Að%+à	ó%+ðN7 <°$Ñ#6ó 7ð
 ?CñEØðEØ+/°©9°tÑ+;ðEà	ˆlÑ	˜dÑ	"÷Er   rˆ   )rf   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ô ÷j-ñ j-ôZdE =õ dEr   