Ë
    g^(hž.  ã                   óè   — 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y)é    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                   ó<   — 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     úi/var/www/skyplay_api_hub/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
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-   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‰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Ø�}‰}Ð(Ø"Ÿ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   )r2   r8   r/   )r   s    r   r9   z$AutoHeuristic.satisfies_preconditionk   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,   r5   r:   r;   Úuse_autoheuristicr0   r1   r/   Úapply_operationsr   r8   Úget_decisionr-   )r   Ú
controllerÚdecisions      r   Ú
get_choicezAutoHeuristic.get_choicep   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   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¸   € ð ×*Ñ*Ô,ØÜ�?‰?×!Ñ!×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                  ób   — t         j                  j                  «       j                  dd«      } | S )Nú Ú_)r5   r6   Úget_device_nameÚreplace)Údevice_names    r   Úget_device_identifierz#AutoHeuristic.get_device_identifier¢   s)   € ô —j‘j×0Ñ0Ó2×:Ñ:¸3ÀÓDˆØÐr   c                 ó˜   — | j                  «       }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«   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_   Ú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¼   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   r   rs   r	   r   Úboolr   r9   rI   ÚintrO   r   rR   ÚstaticmethodrY   r=   rj   r@   r&   r   r   r*   r*   .   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   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_nodesr/   r0   r1   r2   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‚   Ú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Ø?EˆD×!Ñ! &×"9Ñ"9Ó";Ò<ð	Fä˜4×0Ñ0×5Ñ5Ó7Ó8ˆð	6œcõ 	6ô 	‰ÑØØØØØØØô	
ô �O‰O×"Ñ"×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Útimingsr0   rˆ   r-   r   Nc                 ó²   •—  ‰
|«      }|| k7  ry  ‰|||«      }||k7  ry |j                  «       D ]%  \  }}	‰j                  |j                  «       |	«       Œ' y 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  sn   ø€ ñ "3°;Ó!?ÐØ! ]Ò2ØÙ%:ØÐ(¨'ó&Ð"ð &Ð):Ò:ØØ '§¡£ò @‘�˜Ø—‘˜v×6Ñ6Ó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ˆ
Ù.¨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_caller1  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_caller5  s=   € ð ×(Ñ(¨°Ó@ˆØˆ?ØØ<CÖD°&�×%Ñ% fÓ-ÒDÐDùÒDs   š3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ñ+@à˜2˜x¨Ñ5Ð5Ñ6ð+@ð �lÑ#ð+@ð ˜#‘Yð	+@ð
 ð+@ð ð+@ð " $ {Ñ"3Ñ4ð+@ð ˜x¨°YÐ(?ÀÐ(EÑFÑGð+@ð 
õ+@ðZ#+Ø ™9ð#+Ø/3°LÑ/Að#+à	ó#+ðJ7 8¨LÑ#9ó 7ð
 BFñEØðEØ+3°D¸±IÑ+>ð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´      si   ðÛ Û 	Ý ß *Ñ *ã ÷	÷ 	ó 	õõ ,Ý ;Ý 7÷(ñ (ô˜9ô ÷h-ñ h-ôVbE =õ bEr   