
    Ϧii                        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
  S SKrS SKrS SKJr  S SKJr  Sr\S:X  Ga|  \" \S	9r\R)                  S
SS9  \R)                  SS\SSSS9  \R)                  SS\SSS9  \R)                  SS\SSS9  \R)                  SS\SSS9  \R)                  SS \SS!S9  \R)                  S"S#\S$SS%9  \R)                  S&S'\S(SS%9  \R/                  5       r\R2                  (       a  \R2                  O\R4                  r\R6                  (       a  \R6                  O\" \5      r\" \R8                  5      r\R8                  R=                  S \" \R>                  5      5        \R@                  RC                  \RD                  \5      r#\R@                  RI                  \#5      r%\#RL                  RO                  \%5        \(" \%\R4                  5      r)\RT                  RW                  S)5      r*\," \*5      S:X  d   e\-" \." S* \R^                  RW                  S)5      5      5      r/S+\
\   4S, jr0S-\Rb                   S.\Rd                   3r3\0" \/\3/-   5      r4S/ r5\6" \/5       V Vs0 s H(  u  pS0U;   d  M  U \5" URW                  S05      S   5      _M*     snn r7\7Rq                  5        VVs0 s H  u  p#Uc  M
  X#_M     snnr7\6" \/5       V Vs0 s H  u  p\)Rr                  U    \5" U5      _M     snn r:\:Rq                  5        VVs0 s H  u  p#Uc  M
  X#_M     snnr:\6" \/5       V Vs0 s H<  u  p\)Rr                  U    \:;  d  M  \)Rr                  U    URW                  S05      S    _M>     snn r/S1Rw                  \:Ry                  5        Vs/ s H  n\" U5      PM     sn5      r=\:Rq                  5        VVs/ s H  u  p#U S2U 3PM     snnr>\>S3\Rb                   3S4\Rd                   3/-  r>\7Ry                  5        H  r?\?S5;   a  M   S6\? 35       e   \R                  R                  R                  R                  \75      rD\DR                  5       Rq                  5        H&  u  rFrG\:R                  \)Rr                  \F   \G05        M(     \R                  R                  \)\:\/\DS79rJ\Rb                  \Rd                  S8.rK\R                  " \J\KS99rM/ r9/ rN/ rO/ rP\6" \)Rr                  5       H  u  rQrR\R\:;  aL  \9R                  \R5        \NR                  \/\R   5        \OR                  \R5        \PR                  \/\R   5        MX  \Q\DR                  ;   d  Mj  \9R                  \R5        \NR                  \/\R   5        M     \" \/Ry                  5       \D5      rUS:Rw                  \\4\U/5      rV\" \ R                  " \MR                  S;   5      5      S<S= rY0 S>\V_S?\R4                  _S@\," \Y5      _SASBRw                  \Z" \YSSS<2   \YSSS<2   5       VVs/ s H  u  pESCU U 3PM     snn5      _S+SBRw                  \Z" \O\P5       VVs/ s H  u  pg\" U5       SDU 3PM     snn5      _SESBRw                  \Z" \9\N5       VVs/ s H  u  pg\" U5       SDU 3PM     snn5      _SFSBRw                  \O Vs/ s H  nSGU 3PM
     sn5      _SH\," \O5      _SI\>_SJ\MR                  R                  _SK\Rb                  _SLS:Rw                  \=\3/5      _SM\*S    _SN\*S   _SO\*S<   _SPS_r]SQ H  r^\" \_5      R>                  SR\^ 3-  r`\R                  SS\4 S:\U SS\^ 35      R                  ST5       rc\cR                  \" \`5      R                  5       R                  " SU0 \]D65        SSS5        M     ggs  snn f s  snnf s  snn f s  snnf s  snn f s  snf s  snnf s  snnf s  snnf s  snnf s  snf ! , (       d  f       M  = f)V    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                 $    U R                  S5      $ )N )strip)ss    S/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/triton/tools/compile.py<lambda>r   G   s    1773<    	signaturec                     [         R                  " 5       nUR                  SR                  U 5      R	                  5       5        UR                  5       S S $ )Nr      )hashlibsha256updatejoinencode	hexdigest)r   ms     r   hash_signaturer$   I   s?    NN	)$++-.{{}Ra  r   warpsxstagesc                 z     [        U 5      nU$ ! [         a     Of = f [        U 5      nU$ ! [         a     g f = f)N)int
ValueErrorfloat)r   rets     r   	constexprr,   Q   sO    	a&CJ 			(CJ 		s    
- 
:::x=z
num_warps=znum_stages=)r      z#Only 1 and 16 are valid hints, got )fn	constantsr   attrs)	num_warps
num_stages)options_cubin   kernel_nametriton_kernel_namebin_sizebin_dataz, 0xr   full_signaturearg_pointers&num_argskernel_docstringsharedr4   	algo_infogridXgridYgridZ_placeholder)hczcompile..w )gbinasciir   importlib.util	importlibsysargparser   pathlibr   typingr   tritontriton.backendstriton.compiler.code_generatorr   triton.backends.nvidia.driverr   desc__name__parseradd_argumentstrr(   
parse_argsargsout_namer;   out_pathr
   arg_pathinsertparentutilspec_from_file_locationstemspecmodule_from_specmodloaderexec_modulegetattrkernelgridsplitlenlistmapr   r$   r4   r5   meta_sigsig_hashr,   	enumeratehintsitems	arg_namesr2   r    values	const_sig
doc_stringrK   backendscompilerAttrsDescriptor
from_hintsr3   get_constantspvr   	ASTSourcesrcoptscompileccinfo	arg_typesarg_names_not_1arg_types_not_1i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format)	r   r   kr   r.   ynametyargs	   000000000r   <module>r      s,      
 #     8 34 z -F
s  u
CJi!%  '
t#qGmn
CN  P
e#tJlm
dt.Y
t#<U`de
$S7R]abD $t}}43C3CH $t}}4>H DIIHHHOOAs8??+,>>11(--JD
..
)
)$
/CKKC S$**+F99??3Dt9>> S/1E1Ec1JKLI!$s) !
 t~~&gdoo->?Hi8*45H 8A7KX7KtqsVWx*Q	!''#,q/**7KXE#kkm=mdaqTQTm=E?H?ST?Stq!!!$il2?STI"+//"3E"3$!q"3EI i((DAAi/ 	-QWWS\!_,(I
 )*:*:*<=*<Q#a&*<=>I)2):;):QCq*):;JZ/0K?P2QRRJ \\^G|FB1#FF| OO$$44??FE##%++-1&**1-q12 .
//
#
#vi_d
#
eCtGD^^C.FIIOO !1!1289$X&Yx01""8,""9X#67%"""X&Yx01 3 9++-u5F(Hf56Ix

7 345a;Dyd.. 	CI 	DIIs4!9d14a4j7QR7QtqA3qc{7QRS	
 	TYY#o_nJopJohd9R=/4& 9Jopq 	$))sS\^gOh$iOh84	"av%>Oh$ij 			"HQse9"HI 	C( 	J 	&//(( 	T^^ 	SXXy(34 	a 	a 	a  	!F$ X--(3%0@@!!AhZq#"?@EEcJbHHT-(224;;EfEF KJ a l Y=TE
 >;D Sp$i"H KJsf   9^=	^=?	__!"_		_'_<_&_ __ &_&_,_2_896_==
`	