
    9i"                         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Jr  S SKrS SKr\ " S S5      5       rSrS	 rS
\4S jr\S:X  a  \" 5         gg)    N)ArgumentParser)	dataclass)Path)Listc                       \ rS rSr% SrSr\\S'   Sr\\S'   Sr	\\S'   Sr
\\S'   Sr\S-  \S	'   S
r\\S'   Sr\\S'   Sr\S-  \S'   Sr\S-  \S'   Srg)CompileArgs   z8
A class to contain arguments from command-line parser.
 pathkernel_name	signaturegridNtarget   	num_warps   
num_stagesout_nameout_path )__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   r   r   r   r   intr   r   r   r   __static_attributes__r       T/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/triton/tools/compile.pyr   r      sm     D#NKIsD#NFC$JIsJHcDj HdTk r    r   a  
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
c            	         [        [        S9n U R                  SSS9  U R                  SS[        SSS	S
9  U R                  SS[        S SS9  U R                  SS[        SSS9  U R                  SS[        SSS9  U R                  SS[        S SS9  U R                  SS[
        S SS9  U R                  SS[        SS	S 9  U R                  S!S"[        S#S	S 9  U R                  5       n[        S$0 [        U5      D6n[        U5        g )%N)descriptionr   zTPath to Python source containing desired kernel in its scope. File will be executed.)helpz--kernel-namez-nr
   zName of the kernel to compileT)typedefaultr$   requiredz--targetz-tzThe target to compile towards, in format of '<backend>:<arch>:<warp-size>'; e.g., 'cuda:80:32', 'hip:gfx942:64'. Default to None, which means using current machine's GPU target)r%   r&   r$   z--num-warpsz-wr   z$Number of warps to launch the kernelz--num-stagesz-nsr   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 kernelr   )
r   descadd_argumentr   r   r   
parse_argsr   varscompile_kernel)parsercli_argsargss      r!   mainr0   9   s4   -F
s  u
CJi!%  '
DsDo  p t#qGmn
CN  P
e#tJlm
dt.Y
t#<U`de
$S7R]ab  "H(h(D4r    r/   c                    U R                   (       a  U R                   OU R                  nU R                  (       a  U R                  O
[        U5      n[        U R                  5      n[
        R                  R                  S[        UR                  5      5        [        R                  R                  UR                  U5      n[        R                  R                  U5      nUR                  R                  U5        [!        XPR                  5      nU R"                  R%                  S5      n['        U5      S:X  d   e[)        [+        S U R,                  R%                  S5      5      5      nS[.        [           4S jn	SU R0                   SU R2                   3n
U	" X/-   5      nS	 n[5        U5       VVs0 s H)  u  pS
U;   d  M  U4U" UR%                  S
5      S   5      _M+     nnnUR7                  5        VVs0 s H  u  nnUc  M  UU_M     nnn[5        U5       VVs0 s H  u  pUR8                  U   U" U5      _M     nnnUR7                  5        VVs0 s H  u  nnUc  M  UU_M     nnnUR7                  5        H#  u  nnUS:X  d  M  UUUR8                  US      '   M%     [5        U5       VVs0 s H'  u  pUR8                  U   UR%                  S
5      S   _M)     nnnU H  nSUU'   M
     SR;                  UR=                  5        Vs/ s H  n[        U5      PM     sn5      nUR7                  5        VVs/ s H  u  nnU SU 3PM     nnnUSU R0                   3SU R2                   3/-  nUR=                  5        H  nUS;   a  M   SU 35       e   UR7                  5        VVs0 s H  u  nnUS:X  d  M  USS//_M     nnn[>        R@                  RC                  UUUUS9nU RD                  (       a@  [>        RF                  R@                  RH                  " U RD                  R%                  S
5      6 O1[>        RJ                  RL                  RN                  RQ                  5       n[>        R@                  RS                  U5      nU R0                  U R2                  S.nURU                  U5      n[>        RV                  " UUURX                  S9n[!        URZ                  SS5      S:  a  []        S5      eURZ                  R^                  S:  a  []        S5      e/ n/ n / n!/ n"[5        UR8                  5       H  u  nn#U#U;  aL  URa                  U#5        U Ra                  UU#   5        U!Ra                  U#5        U"Ra                  UU#   5        MX  URc                  U4S 5      S:X  d  Mq  URa                  U#5        U Ra                  S5        M     Sn$[5        UR=                  5       5       HN  u  nn%U$[        U5      -  n$URc                  U4S 5      S:X  a  U$S-  n$URc                  U4S 5      S:X  d  MI  U$S-  n$MP     SR;                  XU$/5      n&URd                  URf                     n'[        [h        Rj                  " U'5      5      S S! n([>        RJ                  RL                  RN                  Rl                  n)0 S"U&_S#U R                  _S$['        U'5      _S%S&R;                  [o        U(S S S 2   U(SS S 2   5       V*V+s/ s H  u  n*n+S'U* U+ 3PM     sn+n*5      _SS&R;                  [o        U!U"5       V,V%s/ s H  u  n,n%U)" U%5       S(U, 3PM     sn%n,5      _S)S&R;                  [o        UU 5       V,V%s/ s H  u  n,n%U)" U%5       S(U, 3PM     sn%n,5      _S*S&R;                  U! V-s/ s H  n-S+U- 3PM
     sn-S,/-   S-/-   5      _S.['        U!5      S -   _S/U_S0URZ                  Rp                  _S1U R0                  _S2SR;                  UU
/5      _S3US   _S4US   _S5US    _S6S_n./ n/URr                  n0[        [t        5      R                  S7-  U0-  n1U1Rw                  S85       H  n2U2Rx                  n3UR{                  S9U SU$ U3 35      n4U4R}                  S:5       n5U5R                  U2R                  5       R                  " S;0 U.D65        S S S 5        U/Ra                  U45        M     U&U/4$ 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%n,f s  sn-f ! , (       d  f       Nm= f)<Nr   ,r   c                 $    U R                  S5      $ )N )strip)ss    r!   <lambda> compile_kernel.<locals>.<lambda>_   s    1773<r    r   c                     [         R                  " 5       nUR                  SR                  U 5      R	                  5       5        UR                  5       S S $ )Nr4      )hashlibsha256updatejoinencode	hexdigest)r   ms     r!   hash_signature&compile_kernel.<locals>.hash_signaturea   s?    NN	)$++-.{{}Ra  r    warpsxstagesc                 z     [        U 5      nU$ ! [         a     Of = f [        U 5      nU$ ! [         a     g f = f)N)r   
ValueErrorfloat)r6   rets     r!   	constexpr!compile_kernel.<locals>.constexpri   sO    	a&CJ 			(CJ 		s    
- 
:::r   rJ   x=z
num_warps=znum_stages=)r      z#Only 1 and 16 are valid hints, got rO   ztt.divisibility)fn
constexprsr   attrs)r   r   )r   optionsglobal_scratch_sizezMAOT compiling kernels with global scratch requirements is not yet implementedzNAOT compiling kernels with profile scratch requirements is not yet implementedi32r
   cd_   r   triton_kernel_namebin_sizebin_dataz, 0xr4   full_signaturearg_pointers&z&global_scratchz&profile_scratchnum_argskernel_docstringsharedr   	algo_infogridXgridYgridZ_placeholderextraz	compile.*.wr   )Br   r   r   r   r   sysinsertr   parent	importlibutilspec_from_file_locationstemmodule_from_specloaderexec_modulegetattrr   splitlenlistmapr   r   r   r   	enumerateitems	arg_namesr>   valuestritoncompiler	ASTSourcer   backends	GPUTargetruntimedriveractiveget_current_targetmake_backendparse_optionscompile__dict__metadataRuntimeErrorprofile_scratch_sizeappendgetasm
binary_extbinasciihexlifymap_python_to_cpp_typeziprd   backend__file__globsuffixwith_suffixopenwrite	read_textformat)6r/   r   r   arg_pathspecmodkernelr   r   rB   meta_sigsig_hashrJ   ir6   hintskv	constantskeyvalue	const_sig
doc_stringhrR   srcr   r   kwargsrS   ccinfor~   	arg_typesarg_names_not_1arg_types_not_1arg_namer   ty	func_namer   hex_	ty_to_cpprM   ynameargparamsoutput_filesbackend_nametemplate_dirtemplate_pathextoutput_filefps6                                                         r!   r,   r,   P   s    $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i*45H <EY;O\;O41SVZ[S[.aUIaggcl1o..;OE\#kkm=mdaqTQTmE=?H?ST?Stq!!!$il2?SIT"+//"3E"3$!QqA"3IEkkm
UA:27If&&s1v./ $ CLIBVWBV$!!!!$aggcl1o5BVIW$	# )*:*:*<=*<Q#a&*<=>I)2):;):AQCq*):J;Z/0K?P2QRRJ\\^G|FB1#FF| 6;kkmOmdaqBw)Q#R())mEO
//
#
#v)y`e
#
fC ;; __%%//1B1B31GH"NN1188KKM oo**62G>>IF##F+G^^C8H8HIFv 5q9A=jkk++a/kllIIOO !1!1289$X&Yx01""8,""9X#67YYud#q(X&U# 3 F9++-.2#a&99aUD!Q&cMF99aUD!R'cMF / (f56I
**W''
(Cx$%a+D%%,,CCIyd.. 	CH 	DIIs4!9d14a4j7QR7Qtq!A3qc{7QRS	
 	TYY#o_nJopJohdB9R=/4& 9Jopq 	$))sS\^gOh$iOh84	"av%>Oh$ij 			"HQse9"HL]K^"^btau"uv 	C(1, 	J 	&//(( 	T^^ 	SXXy(34 	a 	a 	a  	!F$ L>>L>((72\AL%**;7""**Qxj&#+GHc"bHH],,.55??@ #K( 8 l""q ]=TE X >;
 P` Sp$i"H" #"sl   8c8 c8>
c>c>$"d
d
*d
9.ddd6d!
d!d'd- d32d9'0d>>
e	__main__)r   r;   importlib.utilrp   rm   argparser   dataclassesr   pathlibr   typingr   r   triton.backendsr   r(   r0   r,   r   r   r    r!   <module>r      sk       
 # !     ! ! !6.~# ~#B zF r    