
    sh	*                        S SK r S SKrS SKr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Jr  S SKJr  S SKJr  SSKJr  SS	KJrJr  \R,                  " \5      r\ R2                  " S
S9 " S S5      5       r\ R2                  " S
S9 " S S5      5       r\ R2                  " S
S9 " S S5      5       r\ R2                  " S
S9 " S S5      5       r " S S5      rg)    N)Path)Optional)countersdynamo_timedset_feature_use)justknobs_check)FileLock   )triton_cache_dir)_IS_WINDOWSGPU_KERNEL_BIN_EXTST)frozenc                   8    \ rS rSr% Sr\\S'   \\S'   \\S'   Srg)TritonBundleEntry   z
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	directory N)	__name__
__module____qualname____firstlineno____doc__str__annotations__int__static_attributes__r       r/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_inductor/triton_bundler.pyr   r      s     KNr   r   c                   N    \ rS rSr% Sr\\S'   \R                  " SS9r	\
\S'   Srg)	TritonKernelArtifact"   ze
Artifact for an individual kernel converted to bytes.
Bytes could be a cubin, json, ttir, or ttgir.
filenameF)reprpayloadr   N)r   r   r   r   r   r   r   dataclassesfieldr&   bytesr   r   r   r    r"   r"   "   s#    
 M &&E2GU2r   r"   c                   >    \ rS rSr% Sr\\S'   \\S'   \\	   \S'   Sr
g)TritonKernelArtifacts-   z2
Collection of artifacts for a particular kernel.
r   r   	artifactsr   N)r   r   r   r   r   r   r   r   listr"   r   r   r   r    r+   r+   -   s      K())r   r+   c                   *    \ rS rSr% Sr\\   \S'   Srg)TritonBundlerMetadata8   z#
Metadata used for instrumentation
cached_kernel_namesr   N)	r   r   r   r   r   r.   r   r   r   r   r   r    r0   r0   8   s     c"r   r0   c                       \ rS rSr% SrSr\\\      \	S'   Sr
\\	S'   \S\4S j5       r\SS	 j5       r\SS
 j5       r\S\S\SS4S j5       r\S\\\   \\   4   4S j5       r\S\\   S\\   4S j5       rSrg)TritonBundlerA   a  
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                      SSK Jn   U R                  (       a  gU R                  =nb  U$ U R	                  5       (       d  g[        S5      $ )Nr   )configFz9pytorch/remote_cache:bundle_triton_into_fx_graph_cache_v2)torch._inductorr:   force_disable_caches!bundle_triton_into_fx_graph_cache	is_fbcoder   )r:   bs     r    
is_enabledTritonBundler.is_enabledW   sI    *&&999AFH!!G
 	
r   c                     [         R                  5       (       d  g[        R                  S5        U R                  b   e/ U l        g)zh
Initializes the TritonBundler.
The current TritonBundler bundle is finalized by TritonBundler.collect.
Nz%TritonBundler.begin_compile is called)r4   r@   logdebugr6   clss    r    begin_compileTritonBundler.begin_compileh   s:     ''))		9:||###r   c                 <    [         R                  S5        SU l        g)z\
Finalizes the TritonBundler. If collect is not yet called, it
discards the current bundle.
z#TritonBundler.end_compile is calledN)rC   rD   r6   rE   s    r    end_compileTritonBundler.end_compilet   s     			78r   r   r   c           	      l    U R                   =nb%  UR                  [        X[        U5      5      5        gg)zo
Lazily observes that we have seen a Triton kernel compilation. Remembers
it for when collect is later called.
N)r6   appendr   r   )rF   r   r   entriess       r    putTritonBundler.put}   s4     ||#G0NN!+7G7OP 1r   c           
      N   [         R                  5       (       d   U R                  5         [        SS5        / S4$ [        SS5        [	        SSS9   U R
                  nUGbr  / n/ nU GHA  n/ n[        R                  R                  UR                  UR                  5      n[        R                  R                  U5      (       d  M`  [        R                  " U5       GH  n[        R                  R                  Xg5      n [        R                  R                  U5      (       d   e[        US5       n	U	R                  5       n
UR!                  S5      (       aw  [         R"                  U
;   a0  [$        R'                  S	[         R"                  U
5        [)        S
5      eU
R+                  [,        R/                  U5      [         R"                  5      n
UR1                  [3        Xz5      5        SSS5        [4        S   S==   S-  ss'   [        R                  R;                  U5      S   nU[<        R>                  " 5       ;   d  GMh  UR1                  [A        U5      RB                  5        GM     U(       d  GM  UR1                  [E        UR                  URF                  U5      5        GMD     U R                  5         U[I        U5      4sSSS5        $ / S4sSSS5        $ ! , (       d  f       N= f! [6         a    [$        R9                  SSS9   GNf = f! , (       d  f       g= f)z
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)%r4   r@   rJ   r   r   r6   ospathjoinr   r   existslistdirisfileopenreadendswithr7   rC   warningAssertionErrorreplacer   encoderM   r"   r   	ExceptionrD   splitextr   valuesr   stemr+   r   r0   )rF   rN   resultkernel_namesentryr-   r\   r$   filepathfiler&   	extensions               r    collectTritonBundler.collect   s    ''))OO-u5t8O)405TRllG"68*,$E<>I77<<9J9JKD77>>$// $&JJt$4#%77<<#?X#%77>>(#;#;;#;!%h!5*.))+#+#4#4W#=#='4'C'Cw'N(+,U,9,H,H,3)*
 /=,K/* )* /6oo(+

4(8-:V:V/&G !* 0 0$8$K!"# "6( %Z01MNRSSN %'GG$4$4X$>q$A	$(;(B(B(DD )//X0C0CD? %5@ !y1 % 1 1 % )M %Z !4\BBg SRh t8i SR "6!5*  ) XII&GRVIWXG SRs]   B0L2K13B8K 	+K19L.L8ALL 
K.*K11L	LL	L
L$bundlec           
         [         R                  5       (       d  g[        SSS9   / nU  GH  n[        UR                  5      n[
        R                  R                  X2R                  5      n[
        R                  R                  U5      (       a;  [        [
        R                  " U5      5      S:w  a  [        R                  SU5        M  [        U5      R                  SSS9  [!        ["        R$                  " 5       5      n[
        R                  R                  USU 35      n[
        R&                  " U5        UR(                   GH<  n[
        R                  R                  XgR*                  5      n[-        US	5       n	UR.                  n
UR*                  R1                  S
5      (       a3  U
R3                  [         R4                  [         R7                  U5      5      n
U	R9                  U
5        SSS5        [:        S   S==   S-  ss'   [
        R                  R=                  UR*                  5      S   nU[>        R@                  " 5       ;   d  GM  URC                  [        UR*                  5      RD                  5        GM?     [F        (       aj  [I        US-   5         [
        R                  R                  U5      (       a  [J        RL                  " U5        [
        R2                  " Xd5        SSS5        GM  [
        R2                  " Xd5        GM     [O        U5      sSSS5        $ ! , (       d  f       GNB= f! , (       d  f       GM$  = f! , (       d  f       g= f)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_emitTrS   r   z8Bailing out TritonBundler.read_and_emit, %s is non empty)parentsexist_okztmp.wbrW   rX   #triton_bundler_read_and_emit_kernelr
   z.lock)(r4   r@   r   r   r   r[   r\   r]   r   r^   lenr_   rC   rD   r   mkdirr   uuiduuid4makedirsr-   r$   ra   r&   rc   rf   r7   rg   writer   ri   r   rj   rM   rk   r   r	   shutilrmtreer0   )rt   rm   r-   basedirr   rnd_idtmp_dirartifactro   rp   r&   rq   s               r    read_and_emitTritonBundler.read_and_emit   sl     ''))-T
 ')L#	*9+;+;<GGLL2G2GH	77>>),,RZZ	5J1Kq1P IIR! W##D4#@ TZZ\*'',,w$vh@G$ ) 3 3H!ww||G5F5FGHh-"*"2"2#,,55g>>&-oo - < <cjj>S'G 

7+ . Z()NOSTTO " 0 01B1B CA FI $7$>$>$@@ %++D1B1B,C,H,HI !4  ;!)g"5677>>)44"MM)4

76 76 JJw2W $Z )6e
 
6 .- 76U
 
sF   EM A1L,	1AMAM!AL>20M,
L;6M>
M	M
Mr   )r8   N)r   r   r   r   r   r6   r   r.   r   r   r7   r)   staticmethodboolr@   classmethodrG   rJ   r   r   rO   tupler+   r0   rr   r   r   r   r   r    r4   r4   A   s     37Hht-./6 )NE(
 
 
  	 	   c 3 4   C	t)*H5J,KK	LC CJ D7*+D7	'	(D7 D7r   r4   )r'   loggingr[   r   r|   pathlibr   typingr   torch._dynamo.utilsr   r   r   torch._utils_internalr   torch.utils._filelockr	   runtime.runtime_utilsr   utilsr   r   	getLoggerr   rC   	dataclassr   r"   r+   r0   r4   r   r   r    <module>r      s      	     G G 1 * 3 3 ! d#
 
 $
 d#3 3 $3 d#* * $* d## # $#R7 R7r   