Ë
    g^(h	*  ã                   óÄ  — d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	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mZ  ej,                  e«      Z e j2                  d
¬«       G d„ d«      «       Z e j2                  d
¬«       G d„ d«      «       Z e j2                  d
¬«       G d„ d«      «       Z e j2                  d
¬«       G d„ d«      «       Z G d„ d«      Zy)é    N)ÚPath)ÚOptional)ÚcountersÚdynamo_timedÚset_feature_use)Újustknobs_check)ÚFileLocké   )Útriton_cache_dir)Ú_IS_WINDOWSÚGPU_KERNEL_BIN_EXTST)Úfrozenc                   ó0   — e Zd ZU dZeed<   eed<   eed<   y)ÚTritonBundleEntryzÿ
    When we have compiled a triton kernel, we take note of that kernel by
    its triton generated hash, its device, and where this kernel is located.
    This is the minimum information we can use to later retrieve this kernel
    from file system.
    Úkernel_hashÚdeviceÚ	directoryN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__Úint© ó    ú\/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/_inductor/triton_bundler.pyr   r      s   … ñð ÓØƒKØ„Nr   r   c                   óL   — e Zd ZU dZeed<    ej                  d¬«      Ze	ed<   y)ÚTritonKernelArtifactzq
    Artifact for an individual kernel converted to bytes.
    Bytes could be a cubin, json, ttir, or ttgir.
    ÚfilenameF)ÚreprÚpayloadN)
r   r   r   r   r   r   ÚdataclassesÚfieldr"   Úbytesr   r   r   r   r   "   s%   … ñð
 ƒMØ&�[×&Ñ&¨EÔ2€GˆUÔ2r   r   c                   ó6   — e Zd ZU dZeed<   eed<   ee   ed<   y)ÚTritonKernelArtifactsz:
    Collection of artifacts for a particular kernel.
    r   r   Ú	artifactsN)	r   r   r   r   r   r   r   Úlistr   r   r   r   r'   r'   -   s    … ñð ÓØƒKØÐ(Ñ)Ô)r   r'   c                   ó"   — e Zd ZU dZee   ed<   y)ÚTritonBundlerMetadataz+
    Metadata used for instrumentation
    Úcached_kernel_namesN)r   r   r   r   r)   r   r   r   r   r   r+   r+   8   s   … ñð ˜c™Ô"r   r+   c                   óä   — e Zd ZU dZdZeee      ed<   dZ	e
ed<   edefd„«       Zedd„«       Zedd	„«       Zed
ededdfd„«       Zedeee   ee   f   fd„«       Zedee   dee   fd„«       Zy)ÚTritonBundleraó  
    Lightweight Triton Kernel bundler that notes each time we compile a triton
    kernel. When collect is called, converts all the previously noted kernels and
    their artifacts into a structured bytes blob, and later when write is called
    it writes this structured blob back to file system.

    Intended Life cycle:
    - TritonBundler.begin_compile is called when we start compiling in Inductor
    - TritonBundler.put is called each time a Triton Kernel is compiled
    - TritonBundler.collect is called when a cache entry is being generated
    - TritonBundler.end_compile is called to indicate bundling is completed,
      collect will execute this function as well.
    - TritonBundler.read_and_emit is called when a cache entry is read
    NÚ_entriess	   [REPLACE]Ú_REPLACE_BYTESÚreturnc                  ó€   — ddl m}  | j                  ry| j                  x}�|S | j	                  «       syt        d«      S )Nr   )ÚconfigFz9pytorch/remote_cache:bundle_triton_into_fx_graph_cache_v2)Útorch._inductorr3   Úforce_disable_cachesÚ!bundle_triton_into_fx_graph_cacheÚ	is_fbcoder   )r3   Úbs     r   Ú
is_enabledzTritonBundler.is_enabledW   sF   € å*à×&Ò&Øà×9Ñ9Ð9ˆAÐFØˆHà×ÑÔ!ØäØGó
ð 	
r   c                 ó‚   — t         j                  «       syt        j                  d«       | j                  �J ‚g | _        y)z€
        Initializes the TritonBundler.
        The current TritonBundler bundle is finalized by TritonBundler.collect.
        Nz%TritonBundler.begin_compile is called)r.   r9   ÚlogÚdebugr/   ©Úclss    r   Úbegin_compilezTritonBundler.begin_compileh   s7   € ô ×'Ñ'Ô)ØÜ�	‰	Ð9Ô:Ø�|‰|Ð#Ð#Ð#Øˆ�r   c                 ó<   — t         j                  d«       d| _        y)zt
        Finalizes the TritonBundler. If collect is not yet called, it
        discards the current bundle.
        z#TritonBundler.end_compile is calledN)r;   r<   r/   r=   s    r   Úend_compilezTritonBundler.end_compilet   s   € ô 	�	‰	Ð7Ô8Øˆ�r   r   r   c           	      ól   — | j                   x}�&|j                  t        ||t        |«      «      «       yy)z‡
        Lazily observes that we have seen a Triton kernel compilation. Remembers
        it for when collect is later called.
        N)r/   Úappendr   r   )r>   r   r   Úentriess       r   ÚputzTritonBundler.put}   s6   € ð —|‘|Ð#ˆGÐ0Ø�N‰NÜ! +¨vÔ7GÈÓ7OÓPõð 1r   c           
      óø  — t         j                  «       s | j                  «        t        dd«       g dfS t        dd«       t	        dd¬«      5  | j
                  }|��Sg }g }|D �]#  }g }t        j                  j                  |j                  |j                  «      }t        j                  j                  |«      sŒZt        j                  |«      D �]}  }t        j                  j                  ||«      }	 t        j                  j                  |«      sJ ‚t        |d«      5 }	|	j                  «       }
|j!                  d«      rut         j"                  |
v r0t$        j'                  d	t         j"                  |
«       t)        d
«      ‚|
j+                  t,        j/                  |«      t         j"                  «      }
|j1                  t3        ||
«      «       ddd«       t4        d   dxx   dz  cc<   t        j                  j;                  |«      d   }|t=        j>                  «       v s�ŒZ|j1                  tA        |«      jB                  «       �Œ€ |s�Œô|j1                  tE        |j                  |jF                  |«      «       �Œ& | j                  «        |tI        |«      fcddd«       S g dfcddd«       S # 1 sw Y   ŒèxY w# t6        $ r t$        j9                  dd¬«       Y Œùw xY w# 1 sw Y   yxY w)a  
        This is the main function called when a cache write happens. This function
        converts all the previously remembered kernels into bundled format so that
        it can be written into a cache entry.
        This function also finalizes the current bundle.
        Útriton_bundlingFNTzTritonBundler.collect©ÚkeyÚlog_pt2_compile_eventÚrbú.jsonz'Bundle contains illegal %s, payload: %szBundle contains illegal bytesÚinductorÚtriton_bundler_save_kernelr
   zfailed to collect triton kernel)Úexc_info)%r.   r9   rA   r   r   r/   ÚosÚpathÚjoinr   r   ÚexistsÚlistdirÚisfileÚopenÚreadÚendswithr0   r;   ÚwarningÚAssertionErrorÚreplacer   ÚencoderC   r   r   Ú	Exceptionr<   Úsplitextr   Úvaluesr   Ústemr'   r   r+   )r>   rD   ÚresultÚkernel_namesÚentryr(   rQ   r    ÚfilepathÚfiler"   Ú	extensions               r   ÚcollectzTritonBundler.collectˆ   s�  € ô ×'Ñ'Ô)Ø�O‰OÔÜÐ-¨uÔ5Ø�t�8ˆOÜÐ)¨4Ô0äÐ5ÈTÔRñ 4	Ø—l‘lˆGØÑ"Ø68�Ø*,�Ø$ó ,�EØ<>�IÜŸ7™7Ÿ<™<¨¯©¸×9JÑ9JÓK�DÜŸ7™7Ÿ>™>¨$Ô/Ø Ü$&§J¡J¨tÓ$4ó E˜Ü#%§7¡7§<¡<°°hÓ#?˜ðXÜ#%§7¡7§>¡>°(Ô#;Ð;Ð#;Ü!% h°Ó!5ð "¸Ø*.¯)©)«+ Ø#+×#4Ñ#4°WÔ#=ä'4×'CÑ'CÀwÑ'NÜ(+¯©Ø,UÜ,9×,HÑ,HØ,3ô)*ô
 /=Ø,Kó/*ð )*ð /6¯o©oÜ(+¯
©
°4Ó(8¼-×:VÑ:Vó/& Gð !*× 0Ñ 0Ü$8¸À7Ó$Kô!"÷#"ô( % ZÑ0Ð1MÓNÐRSÑSÓNô %'§G¡G×$4Ñ$4°XÓ$>¸qÑ$A˜	Ø$Ô(;×(BÑ(BÓ(DÓDð )×/Ñ/´°X³×0CÑ0CÖDð?Eó@ !ØŸ™Ü1Ø %× 1Ñ 1Ø %§¡Ø )óöðM,ðZ —‘Ô!ØÔ4°\ÓBÐB÷g4	ñ 4	ðh �t�8÷i4	ñ 4	÷"ð "ûô*  )ò XÜŸI™IÐ&GÐRV˜IÖWðXú÷G4	ð 4	ús]   ÁB)K0Ã8-K
Ä%B2J>	ÇK
Ç38K0È-)K0ÉAK0Ê1K0Ê>KËK
Ë
 K-	Ë*K0Ë,K-	Ë-K0Ë0K9Úbundlec           
      ód  — t         j                  «       syt        dd¬«      5  g }| D �]Ì  }t        |j                  «      }t
        j                  j                  ||j                  «      }t
        j                  j                  |«      r8t        t        j                  |«      «      dk7  rt        j                  d|«       Œšt        |«      j                  dd¬«       t!        t#        j$                  «       «      }t
        j                  j                  |d|› �«      }t        j&                  |«       |j(                  D �]3  }t
        j                  j                  ||j*                  «      }t-        |d	«      5 }	|j.                  }
|j*                  j1                  d
«      r3|
j3                  t         j4                  t         j7                  |«      «      }
|	j9                  |
«       ddd«       t:        d   dxx   dz  cc<   t
        j                  j=                  |j*                  «      d   }|t?        j@                  «       v s�Œ|jC                  t        |j*                  «      jD                  «       �Œ6 tF        rctI        |dz   «      5  t
        j                  j                  |«      rtK        jL                  |«       t        j2                  ||«       ddd«       �Œ·t        j2                  ||«       �ŒÏ tO        |«      cddd«       S # 1 sw Y   �Œ)xY w# 1 sw Y   �ŒûxY w# 1 sw Y   yxY w)a  
        This is the main function called when a cache read happens. This function
        converts the bundled format back into individual files and writes them
        to the filesystem.

        NOTE: When we are writing to the filesystem, we assume exclusive access
        to the target directory.
        This means that if the target folder already exists and is non-empty,
        we bail out.
        Exclusive access means that no other process should be writing to
        or reading from the target directory.
        NzTritonBundler.read_and_emitTrH   r   z8Bailing out TritonBundler.read_and_emit, %s is non empty)ÚparentsÚexist_okztmp.ÚwbrL   rM   Ú#triton_bundler_read_and_emit_kernelr
   z.lock)(r.   r9   r   r   r   rP   rQ   rR   r   rS   ÚlenrT   r;   r<   r   Úmkdirr   ÚuuidÚuuid4Úmakedirsr(   r    rV   r"   rX   r[   r0   r\   Úwriter   r^   r   r_   rC   r`   r   r	   ÚshutilÚrmtreer+   )rh   rb   r(   Úbasedirr   Úrnd_idÚtmp_dirÚartifactrd   re   r"   rf   s               r   Úread_and_emitzTritonBundler.read_and_emitÎ   s‡  € ô  ×'Ñ'Ô)ØäØ-ÀTô
ñ 2	7ð ')ˆLà#ó +3�	Ü*¨9×+;Ñ+;Ó<�ÜŸG™GŸL™L¨°)×2GÑ2GÓH�	ä—7‘7—>‘> )Ô,´´R·Z±ZÀ	Ó5JÓ1KÈqÒ1Pô —I‘IØRØ!ôð ä�W“×#Ñ#¨D¸4Ð#Ô@ô œTŸZ™Z›\Ó*�ÜŸ'™'Ÿ,™, w°$°v°h°Ó@�Ü—‘˜GÔ$à )× 3Ñ 3ó J�HÜ!Ÿw™wŸ|™|¨G°X×5FÑ5FÓG�HÜ˜h¨Ó-ð ,°Ø"*×"2Ñ"2˜Ø#×,Ñ,×5Ñ5°gÔ>Ø&-§o¡oÜ -× <Ñ <¼c¿j¹jÈÓ>Só'˜Gð Ÿ
™
 7Ô+÷,ô ˜ZÑ(Ð)NÓOÐSTÑTÓOÜ "§¡× 0Ñ 0°×1BÑ1BÓ CÀAÑ F�IØ Ô$7×$>Ñ$>Ó$@Ó@ð %×+Ñ+¬D°×1BÑ1BÓ,C×,HÑ,HÖIðJõ  Ü! )¨gÑ"5Ó6ñ 7ÜŸ7™7Ÿ>™>¨)Ô4Ü"ŸM™M¨)Ô4ÜŸ
™
 7¨IÔ6÷7ñ 7ô —J‘J˜w¨	Ö2ðW+3ôZ )¨Ó6÷e2	7ñ 2	7÷6,ñ ,ú÷7ñ 7ú÷U2	7ð 2	7úsF   £EL&Å9A,L	Ç%AL&ÉAL&Ê
ALË-L&ÌLÌL&ÌL#ÌL&Ì&L/)r1   N)r   r   r   r   r/   r   r)   r   r   r0   r%   ÚstaticmethodÚboolr9   Úclassmethodr?   rA   r   r   rE   Útupler'   r+   rg   rz   r   r   r   r.   r.   A   sþ   … ñð 37€Hˆh�tÐ-Ñ.Ñ/Ó6ð )€N�EÓ(àð
˜ò 
ó ð
ð  ò	ó ð	ð òó ðð ð˜cð ¨3ð °4ò ó ðð ðCà	ˆtÐ)Ñ*¨HÐ5JÑ,KÐKÑ	LòCó ðCðJ ðD7ØÐ*Ñ+ðD7à	Ð'Ñ	(òD7ó ñD7r   r.   )r#   ÚloggingrP   rt   rp   Úpathlibr   Útypingr   Útorch._dynamo.utilsr   r   r   Útorch._utils_internalr   Útorch.utils._filelockr	   Úruntime.runtime_utilsr   Úutilsr   r   Ú	getLoggerr   r;   Ú	dataclassr   r   r'   r+   r.   r   r   r   ú<module>r‰      sä   ðÛ Û Û 	Û Û Ý Ý ç GÑ GÝ 1Ý *å 3ß 3ð €g×Ñ˜Ó!€ð €×Ñ˜dÔ#÷
ð 
ó $ð
ð €×Ñ˜dÔ#÷3ð 3ó $ð3ð €×Ñ˜dÔ#÷*ð *ó $ð*ð €×Ñ˜dÔ#÷#ð #ó $ð#÷R7ò R7r   