
    Vh	*                        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     N/home/dcms/DCMS/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2GU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9A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NN!+v7G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    '')OO-u5t8O)405TR 4	llG"68*,$ ,E<>I77<<9J9JKD77>>$/ $&JJt$4 E#%77<<h#?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7$K!"#"( %Z01MNRSSN %'GG$4$4X$>q$A	$(;(B(B(DD )//X0C0CD?E@ !1 % 1 1 % )M,Z !4\BBg4	 4	h t8i4	 4	" "*  ) XII&GRVI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+;+;<GGLL)2G2GH	77>>),RZZ	5J1Kq1P IIR! W##D4#@ TZZ\*'',,w$vh@G$ ) 3 3 JH!ww||GX5F5F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J  !)g"56 777>>)4"MM)4

7I67 7 JJw	2W+3Z )6e2	7 2	76, ,7 7U2	7 2	7sF   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     37Hht-./6 )NE(
 
 
  	 	   c 3 4   C	t)*H5J,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   