§
    ÎÐj   ã                   ó¾   — d Z ddlZddlZddlZddlmZ ddlZej        d„ ¦   «         Z G d„ d¦  «        Z	ddl
mZmZ  e¦   «           ee	j        ¦  «        e	_        dS )aô  Launch configurations of WanGP Triton kernels, tuned once per kernel, tuning identity and GPU.

A call that cannot be tuned (too small to time, during CUDA graph recording or PyTorch compilation) uses the static configuration, the
first candidate, without caching it. The first call that can be tuned times every candidate on scratch work prepared by the caller (never
in place on live tensors) and keeps the winner in a versioned cache file shared by the sessions, so later sessions compile only it.é    N)ÚPathc                 ó@   — t           j                             | ¦  «        S )N)ÚtorchÚcudaÚget_device_name)Údevices    úCC:\Users\johnn\OneDrive\Documents\AI\shared\kernels\triton_tuner.pyÚ_device_namer
      s   € åŒ:×%Ò% fÑ-Ô-Ð-ó    c                   ó    — e Zd Zd„ Zd„ Zd„ ZdS )ÚLaunchTunerc                 óz   — t          j        ¦   «         dz  dz  |› d�z  | _        ||c| _        | _        d| _        dS )zqcache_name: file under ~/.triton/autotune; version: bumped when a kernel, its candidates or the benchmark change.z.tritonÚautotunez.jsonN)r   ÚhomeÚpathÚversionÚlabelÚ_tuned)ÚselfÚ
cache_namer   r   s       r	   Ú__init__zLaunchTuner.__init__   sB   € å”I‘K”K )Ñ+¨jÑ8¸jÐ;OÐ;OÐ;OÑOˆŒ	Ø#*¨EÐ ˆŒ�d”jØˆŒˆˆr   c                 ó  — | j         €€	 t          j        | j                             d¬¦  «        ¦  «        }|                     d¦  «        | j        k    r|d         ni | _         n$# t          t          t          f$ r
 i | _         Y nw xY w| j         S )Núutf-8©Úencodingr   Úconfigs)
r   ÚjsonÚloadsr   Ú	read_textÚgetr   ÚOSErrorÚ
ValueErrorÚKeyError)r   Úpayloads     r	   Ú_cachezLaunchTuner._cache   s’   € ØŒ;Ðð!Ýœ* T¤Y×%8Ò%8À'Ð%8Ñ%JÔ%JÑKÔK�Ø4;·K²KÀ	Ñ4JÔ4JÈdÌlÒ4ZÐ4Z˜g iÔ0Ð0Ð`b�”�øÝ�Z­Ð2ð !ð !ð !Ø �”��ð!øøøàŒ{Ðs   ‰AA$ Á$BÂBc           	      óÂ  — |› d|› dt          |¦  «        › �}|                      ¦   «                              |¦  «        }|�t          |¦  «        S |r<t          j                             ¦   «         st          j                             ¦   «         r|d         S t          j	        ¦   «         g }
}	 |¦   «         }|D ]å}t          d¦  «        D ]} ||¦  «         Œt          j                             d¬¦  «        t          j                             d¬¦  «        }}|                     ¦   «          t          d¦  «        D ]} ||¦  «         Œ|                     ¦   «          |                     ¦   «          |
                     |                     |¦  «        dz  |f¦  «         Œæt!          |
¦  «        \  }}t#          |¦  «        | j        |<   	 | j        j                             dd¬¦  «         | j                             t/          j        | j        | j        d	œd
¬¦  «        d¬¦  «         n9# t4          $ r,}t7          d| j        › d| j        › d|› �¦  «         Y d}~nd}~ww xY w|S )zùLaunch configuration of kernel `name` for `identity` (what changes its best configuration, such as a model width): the cached
        winner, else tuned now when `tunable`. prepare_scratch() returns launch(config) running the kernel on scratch work.ú|Nr   é   T)Úenable_timingé
   )ÚparentsÚexist_ok)r   r   é   )Úindentr   r   ú[z)] Could not save the Triton tuning cache z: )r
   r%   r    Útupler   r   Úis_current_stream_capturingÚcompilerÚis_compilingÚtimeÚrangeÚEventÚrecordÚsynchronizeÚappendÚelapsed_timeÚminÚlistr   r   ÚparentÚmkdirÚ
write_textr   Údumpsr   r!   Úprintr   )r   ÚnameÚidentityr   Ú
candidatesÚprepare_scratchÚtunableÚkeyÚconfigÚstartedÚtimingsÚlaunchÚ_ÚstartÚendÚbest_msÚbestÚerrors                     r	   rH   zLaunchTuner.config$   sq  € ð Ð9Ð9˜Ð9Ð9¥<°Ñ#7Ô#7Ð9Ð9ˆØ—’‘”×"Ò" 3Ñ'Ô'ˆØÐÝ˜‘=”=Ð Øð 	!�%œ*×@Ò@ÑBÔBð 	!ÅeÄn×FaÒFaÑFcÔFcð 	!Ø˜a”=Ð Ýœ9™;œ;¨�ˆØ �Ñ"Ô"ˆØ ð 		Cð 		CˆFÝ˜1‘X”Xð ð �Ø��v‘”��Ýœ×)Ò)¸Ð)Ñ=Ô=½u¼z×?OÒ?OÐ^bÐ?OÑ?cÔ?c�3ˆEØ�LŠL‰NŒNˆNÝ˜2‘Y”Yð ð �Ø��v‘”��Ø�JŠJ‰LŒLˆLØ�OŠOÑÔÐØ�NŠN˜E×.Ò.¨sÑ3Ô3°bÑ8¸&ÐAÑBÔBÐBÐBÝ˜G™œ‰ˆ�Ý ™:œ:ˆŒ�CÑð	aØŒIÔ×"Ò"¨4¸$Ð"Ñ?Ô?Ð?ØŒI× Ò ¥¤¸¼ÐQUÔQ\Ð,]Ð,]ÐfgÐ!hÑ!hÔ!hÐszÐ Ñ{Ô{Ð{Ð{øÝð 	að 	að 	aÝÐ_�d”jÐ_Ð_È4Ì9Ð_Ð_ÐX]Ð_Ð_Ñ`Ô`Ð`Ð`Ð`Ð`Ð`Ð`øøøøð	aøøøàˆs   ÇAH& È&
IÈ0"IÉIN)Ú__name__Ú
__module__Ú__qualname__r   r%   rH   © r   r	   r   r      sA   € € € € € ðð ð ðð ð ðð ð ð ð r   r   )Ú!install_triton_compilation_loggerÚlog_compilation_tuning)Ú__doc__Ú	functoolsr   r4   Úpathlibr   r   Úcacher
   r   Ú%shared.kernels.triton_compilation_logrV   rW   rH   rU   r   r	   ú<module>r]      sØ   ððGð Gð
 Ð Ð Ð Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð à €€€ð „ð.ð .ñ „ð.ð,ð ,ð ,ð ,ð ,ñ ,ô ,ð ,ð^ lÐ kÐ kÐ kÐ kÐ kÐ kÐ kØ !Ð !Ñ #Ô #Ð #Ø+Ð+¨KÔ,>Ñ?Ô?€Ô Ð Ð r   