
    wii                     B   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Zd dlmZ d dlmZ dZedk    r 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,Z0d-ej1         d.ej2         Z3 e0e/e3gz             Z4d/ Z5d0  e6e/          D             Z7d1 e78                                D             Z7d2  e6e/          D             Z9d3 e98                                D             Z9d4  e6e/          D             Z/d5:                    d6 e9;                                D                       Z<d7 e98                                D             Z=e=d8ej1         d9ej2         gz  Z=e7;                                D ]Z>e>d:v sJ d;e>             ej?        j@        jA        B                    e7          ZCeCD                                8                                D ]'\  ZEZFe9G                    e)jH        eE         eFi           (ej@        I                    e)e9e/eC<          ZJej1        ej2        d=ZK ejL        eJeK>          ZMg ZHg ZNg ZOg ZP e6e)jH                  D ]\  ZQZReRe9vraeHS                    eR           eNS                    e/eR                    eOS                    eR           ePS                    e/eR                    jeQeCjT        v r0eHS                    eR           eNS                    e/eR                     ee/;                                eC          ZUd?:                    ee4eUg          ZV e e jW        eMjX        d@                             dAdB         ZYi dCeVdDej        dE e,eY          dFdG:                    dH  eZeYdddA         eYdddA                   D                       d+dG:                    dI  eZeOeP          D                       dJdG:                    dK  eZeHeN          D                       dLdG:                    dM eOD                       dN e,eO          dOe=dPeMj[        j\        dQej1        dRd?:                    e<e3g          dSe*d          dTe*d         dUe*dA         dVdZ]dWD ]Z^ ee_          j        dXe^ z  Z`ea                    dYe4 d?eU dYe^           b                    dZ          5 Zcecd                      ee`          e                                jf        d[i e]           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    d/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/triton/tools/compile.py<lambda>r   G   s    1773<<     	signaturec                     t          j                    }|                    d                    |                                                      |                                d d         S )Nr      )hashlibsha256updatejoinencode	hexdigest)r   ms     r   hash_signaturer%   I   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.   Q   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                 T    i | ]%\  }}t           j        |         t          |          &S r7   )kernel	arg_namesr.   r2   s      r   r5   r5   `   s,    TTTtq!!!$illTTTr   c                     i | ]
\  }}|||S r)   r7   r8   s      r   r5   r5   a   s    EEE$!Qq}A}}}r   c                     i | ]J\  }}t           j        |         t          vt           j        |         |                    d           d         KS )r0   r   )r<   r=   	constantsr1   r2   s      r   r5   r5   b   sQ       AqAi// 	QWWS\\!_///r   xc                 ,    g | ]}t          |          S r7   )str)r3   r:   s     r   
<listcomp>rD   g   s    ===Q#a&&===r   c                 "    g | ]\  }}| d | S )=r7   r8   s      r   rD   rD   h   s&    ;;;AQ****;;;r   z
num_warps=znum_stages=)r      z#Only 1 and 16 are valid hints, got )fnr@   r   attrs)	num_warps
num_stages)options_cubin   kernel_nametriton_kernel_namebin_sizebin_dataz, c                 "    g | ]\  }}d | | S )0xr7   )r3   rA   ys      r   rD   rD      s&    RRRtq!{A{q{{RRRr   c                 <    g | ]\  }}t          |           d | S r   r   r3   nametys      r   rD   rD      s0    ppphdB9R== 9 94 9 9pppr   full_signaturec                 <    g | ]\  }}t          |           d | S rY   r   rZ   s      r   rD   rD      s0    $i$i$i84	"%>%>%>%>$i$i$ir   arg_pointersc                     g | ]}d | S )&r7   )r3   args     r   rD   rD      s    "H"H"H9s99"H"H"Hr   num_argskernel_docstringsharedrJ   	algo_infogridXgridYgridZ_placeholder)hczcompile..wr7   )gbinasciir   importlib.util	importlibsysargparser   pathlibr   typingr   tritontriton.backendstriton.compiler.code_generatorr   triton.backends.nvidia.driverr   desc__name__parseradd_argumentrC   r*   
parse_argsargsout_namerQ   out_pathr   arg_pathinsertparentutilspec_from_file_locationstemspecmodule_from_specmodloaderexec_modulegetattrr<   gridr1   lenlistmapr   r%   rJ   rK   meta_sigsig_hashr.   	enumeratehintsitemsr@   r!   values	const_sig
doc_stringrk   backendscompilerAttrsDescriptor
from_hintsrI   get_constantspr:   r    r=   	ASTSourcesrcoptscompileccinfo	arg_typesarg_names_not_1arg_types_not_1r4   arg_nameappend
equal_to_1suffix	func_namehexlifyasmhex_zipmetadatare   paramsext__file__template_pathwith_suffixopenfpwrite	read_textformatr7   r   r   <module>r      sY	         



 # # # # # #                  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TTyy?S?STTTIEE)//"3"3EEEI Ii((  I
 ==)*:*:*<*<===>>I;;):):;;;J0002Q2Q2QRRJ \\^^ G GG|||F1FF||||O$4??FFE##%%++-- 3 31&*1-q12222
/
#
#vi_d
#
e
eCtGGDV^C...FIIOO y!122 2 289$$X&&&Yx0111""8,,,""9X#67777%"""X&&&Yx0111 ]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pp##o_nJoJopppqq 	$))$i$issS\^gOhOh$i$i$ijj 			"H"H"H"H"HII 	CC(( 	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e `G Gs   	<\\	\	