
    çgD                     N   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
 d dlZd dlmZ d dlmZ dZedk    rg ee	          Ze                    d
d           e                    ddeddd           e                    ddedd           e                    ddedd           e                    ddedd           e                    dd edd!           e                    d"d#ed$d%           e                    d&d'ed(d%           e                                Zej        rej        nej        Zej        rej        n
 ee          Z eej                  Zej                            d  eej                             ej                             ej!        e          Z"ej        #                    e"          Z$e"j%        &                    e$            e'e$ej                  Z(ej)        *                    d)          Z) e+e)          dk    sJ  e, e-d* ej.        *                    d)                              Z.d+e
e         fd,Z/d-ej0         d.ej1         Z2 e/e.e2gz             Z3d/ Z4d0  e5e.          D             Z6d1 e67                                D             Z6d2  e5e.          D             Z8d3 e87                                D             Z8d4  e5e.          D             Z.d59                    d6 e8:                                D                       Z;d7 e8<                                D             Z=e=d8ej0         d9ej1         gz  Z=e6:                                D ]Z>e>d:v sJ d;e>             d< e67                                D             Z?d= e67                                D             Z@ejA        B                    e?e@>          ZCe@D ]ZDe8E                    eDdi           ejA        F                    e(e8e.eC?          ZGej0        ej1        d@ZH ejI        eGeHA          ZJg ZKg ZLe.<                                D ]#ZDeDe@vreKe(jK        eD         gz  ZKeLe.eD         gz  ZL$ ee.:                                eC          ZMdB9                    ee3eMg          ZN e e jO        eJjP        dC                             dDdE         ZQi dFeNdGej        dH e+eQ          dIdJ9                    dK  eReQdddD         eQdddD                   D                       d+dJ9                    dL  eReKeL          D                       dMdJ9                    dN e.<                                D                       dOdJ9                    dP eKD                       dQ e+eK          dRe=dSeJjS        jT        dTej0        dUdB9                    e;e2g          dVe)d          dWe)d         dXe)dD         dYdZUdZD ]ZV eeW          j        d[eV z  ZXeY                    d\e3 dBeM d\eV           Z                    d]          5 Z[e[\                      eeX          ]                                j^        d^i eU           ddd           n# 1 swxY w Y   dS dS )_    N)ArgumentParser)Path)List)kernel_suffix	ty_to_cppa  
Triton ahead-of-time compiler:

This program compiles the kernel with name `kernel-name` in the file at the
provided `path` into self-contained C source-code that embeds the `cubin`
data along with utilities to load, unload and launch the kernel.

signature is provided as a list of (optionally divisibility-hinted) types
or constexpr values, e.g.

`compile.py --kernel-name kernel --signature "*fp32:16, i32:16, 1024, i32" --out-name kernel /path/to/kernel.py`

will compile triton.JITFunction of name `kernel` inside the file `/path/to/kernel.py`.
Said kernel will be specialized such that argument 0, 1 are assumed to be multiple of 16,
and argument 2 is assumed to be a compile-time constant of value 1024, i.e. it won't be part of the generated prototype.

The resulting entry point will have signature

CUresult kernel_{specialization_suffix}(CUstream stream, unsigned gX, unsigned gY, unsigned gZ, float* arg0, int32_t arg1, int32_t arg2)

Different such specialized entry points can be combined using the `linker.py` script.

NOTE: when resolving the scope of /path/to/kernel.py, the file will be executed from within its parent directory with the python interpreter
used to run this `compile.py` script
__main__)descriptionpathzTPath to Python source containing desired kernel in its scope. File will be executed.)helpz--kernel-namez-n zName of the kernel to compileT)typedefaultr   requiredz--num-warpsz-w   z$Number of warps to launch the kernel)r   r   r   z--num-stagesz-ns   z/Number of stages (meta-parameter of the kernel)z
--out-namez-onz Out name for the compiled kernelz
--out-pathz-ozOut filenamez--signaturez-szSignature of the kernel)r   r   r   z--gridz-gzLaunch grid of the kernel,c                 ,    |                      d          S )N )strip)ss    P/var/www/html/ai-engine/env/lib/python3.11/site-packages/triton/tools/compile.py<lambda>r   F   s    1773<<     	signaturec                     t          j                    }|                    d                    |                                                      |                                d d         S )Nr      )hashlibsha256updatejoinencode	hexdigest)r   ms     r   hash_signaturer%   H   sP    N	)$$++--...{{}}RaR  r   warpsxstagesc                     	 t          |           }|S # t          $ r Y nw xY w	 t          |           }|S # t          $ r Y nw xY wd S N)int
ValueErrorfloat)r   rets     r   	constexprr.   P   sq    	a&&CJ 	 	 	D		((CJ 	 	 	D	ts    
  5 
AAc                 n    i | ]2\  }}d |v 	|t          |                    d           d                   3S ):r   )r.   split.0ir   s      r   
<dictcomp>r5   ]   s:    XXXtq!sVWxxQ	!''#,,q/**xxxr   c                     i | ]
\  }}|||S r)    r3   kvs      r   r5   r5   ^   s    ===daq}Q}}}r   c                 4    i | ]\  }}|t          |          S r7   )r.   r2   s      r   r5   r5   _   s$    BBBTQIaLLBBBr   c                     i | ]
\  }}|||S r)   r7   r8   s      r   r5   r5   `   s    EEE$!Qq}A}}}r   c                 ^    i | ]*\  }}|t           v||                    d           d         +S )r0   r   )	constantsr1   r2   s      r   r5   r5   a   s7    [[[1QZHZHZAGGCLLOHZHZHZr   xc                 ,    g | ]}t          |          S r7   )str)r3   r:   s     r   
<listcomp>rB   b   s    ===Q#a&&===r   c                 R    g | ]$}t           j        |          d t          |          %S )=)kernel	arg_namesr>   r3   r4   s     r   rB   rB   c   s2    TTTaV%a(999Q<99TTTr   z
num_warps=znum_stages=)r      z#Only 1 and 16 are valid hints, got c                 $    g | ]\  }}|d k    |S )rH   r7   r3   r4   hs      r   rB   rB   i   s!    >>>TQa2ggqgggr   c                 $    g | ]\  }}|d k    |S )r   r7   rJ   s      r   rB   rB   j   s!    8881a!r   )divisible_by_16
equal_to_1)fnr>   r   attrs)	num_warps
num_stages)options_cubin   kernel_nametriton_kernel_namebin_sizebin_dataz, c                 "    g | ]\  }}d | | S )0xr7   )r3   r?   ys      r   rB   rB      s&    RRRtq!{A{q{{RRRr   c                 <    g | ]\  }}t          |           d | S r   r   )r3   nametys      r   rB   rB      s0    dddhdB9R== 9 94 9 9dddr   full_signaturec                 l    g | ]1}t          t          |                    d t          j        |          2S r`   )r   r   rE   rF   rG   s     r   rB   rB      s<    $r$r$r\]	)A,(?(?%W%W&BRSTBU%W%W$r$r$rr   arg_pointersc                     g | ]}d | S )&r7   )r3   args     r   rB   rB      s    "B"B"B9s99"B"B"Br   num_argskernel_docstringsharedrQ   	algo_infogridXgridYgridZ_placeholder)rK   czcompile..wr7   )_binasciir   importlib.util	importlibsysargparser   pathlibr   typingr   tritontriton.compiler.code_generatorr   triton.backends.nvidia.driverr   desc__name__parseradd_argumentrA   r*   
parse_argsargsout_namerX   out_pathr   arg_pathinsertparentutilspec_from_file_locationstemspecmodule_from_specmodloaderexec_modulegetattrrE   gridr1   lenlistmapr   r%   rQ   rR   meta_sigsig_hashr.   	enumeratehintsitemsr>   r!   values	const_sigkeys
doc_stringrK   rM   rN   compilerAttrsDescriptorrP   r4   r    	ASTSourcesrcoptscompileccinforF   	arg_typessuffix	func_namehexlifyasmhex_zipmetadatark   paramsext__file__template_pathwith_suffixopenfpwrite	read_textformatr7   r   r   <module>r      s         



 # # # # # #              8 8 8 8 8 8 3 3 3 3 3 34 z ^---F
s  u u u
CJi!%  ' ' '
t#qGmnnn
CN  P P P
e#tJlmmm
dt.YYY
t#<U`deee
$S7R]abbbD $Ct}}43CH $At}}44>>H tDIHHOOAss8?++,,,>11(-JJD
.
)
)$
/
/CKC   WS$*++F9??3D3t99>>>> SS//1E1Ec1J1JKKLLI!$s) ! ! ! !
 @t~??do??H~i8*455H   YXyy7K7KXXXE==ekkmm===EBBYYy-A-ABBBIEE)//"3"3EEEI[[		)0D0D[[[I==)*:*:*<*<===>>ITT9>>CSCSTTTJ0002Q2Q2QRRJ \\^^ G GG|||F1FF||||>>U[[]]>>>O88888JO++OXb+ccE ! !!Q    
/
#
#vi_d
#
e
eCtGGDV^C...FII^^ ( (J&*1-..I)A,'I ]9++--u55F(Hf566I3x
7 34455ad;Dyd. 	CCII 	DIIRRss4!9d14a4j7Q7QRRRSS	
 	TYYdd##iYbJcJcdddee 	$))$r$rajaoaoaqaq$r$r$rss 			"B"B	"B"B"BCC 	CC	NN 	J 	&/( 	T^ 	SXXy(344 	a 	a 	a  	!F$  G GX-0@30@0@@!!"?h"?"?"?"?#"?"?@@EEcJJ 	GbHH;TT-((2244;EEfEEFFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	GS NG Gs   <ZZ	Z	