
    Pi"                         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mZ d dlZd dlZe G d d                      ZdZd	 Zd
efdZedk    r e             dS dS )    N)ArgumentParser)	dataclass)Path)Listc                       e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
edz  ed<   d	Zeed
<   dZeed<   dZedz  ed<   dZedz  ed<   dS )CompileArgsz@
    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____doc__r
   str__annotations__r   r   r   r   r   intr   r   r   r        l/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/triton/tools/compile.pyr   r      s           D#NNNKIsD#NNN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                     t          t                    } |                     dd           |                     ddt          ddd	
           |                     ddt          d d           |                     ddt          dd           |                     ddt          dd           |                     ddt          d d           |                     ddt
          d d           |                     ddt          dd	            |                     d!d"t          d#d	            |                                 }t          d$i t          |          }t          |           d S )%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   mainr-   9   s   ---F
s  u u u
CJi!%  ' ' '
DsDo  p p p 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  ""H((h((D4r   r,   c                   ,-. | j         r| j         n| j        }| j        r| j        nt          |          }t          | j                  }t
          j                            dt          |j                             t          j
                            |j        |          }t          j
                            |          }|j                            |           t!          || j                  -| j                            d          }t'          |          dk    sJ t)          t+          d | j                            d                              }dt.          t                   fd}d| j         d| j         }	 |||	gz             }
d	 ,,fd
t5          |          D             }d |                                D             },-fdt5          |          D             }d |                                D             }|                                D ]!\  }}|dk    r||-j        |d                  <   "-fdt5          |          D             }|D ]}d||<   d                    d |                                D                       }d |                                D             }|d| j         d| j         gz  }|                                D ]}|dv sJ d|             d |                                D             }t>          j         !                    -|||          }| j"        r0t?          j#        j         j$        | j"                            d           n't>          j%        j&        j'        (                                }t>          j         )                    |          }| j        | j        d}|*                    |          }t?          j+        |||j,                  }t!          |j-        dd          dk    rt]          d          |j-        j/        dk    rt]          d          g }g }g }g }t5          -j                  D ]\  }}||vra|0                    |           |0                    ||                    |0                    |           |0                    ||                    j|1                    |fd           dk    r*|0                    |           |0                    d            d!}t5          |                                          D ]W\  }} |t          |          z  }|1                    |fd           dk    r|d"z  }|1                    |fd           d#k    r|d$z  }Xd%                    ||
|g          }!|j2        |j3                 }"t          ti          j5        |"                    d&d'         }#t>          j%        j&        j'        j6        .i d(|!d)| j        d*t'          |"          d+d,                    d- to          |#d d d&         |#dd d&                   D                       dd,                    .fd.to          ||          D                       d/d,                    .fd0to          ||          D                       d1d,                    d2 |D             d3gz   d4gz             d5t'          |          d&z   d6|d7|j-        j8        d8| j        d9d%                    ||	g          d:|d         d;|d         d<|d&         d=d!}$g }%|j9        }&t          tt                    j        d>z  |&z  }'|';                    d?          D ]}(|(j<        })|=                    d@|
 d%| |)           }*|*>                    dA          5 }+|+?                     |(@                                jA        dBi |$           d d d            n# 1 swxY w Y   |%0                    |*           |!|%fS )CNr   ,r   c                 ,    |                      d          S )N )strip)ss    r   <lambda>z compile_kernel.<locals>.<lambda>_   s    1773<< r   r   c                     t          j                    }|                    d                    |                                                      |                                d d         S )Nr1      )hashlibsha256updatejoinencode	hexdigest)r   ms     r   hash_signaturez&compile_kernel.<locals>.hash_signaturea   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)r   
ValueErrorfloat)r3   rets     r   	constexprz!compile_kernel.<locals>.constexpri   sq    	a&&CJ 	 	 	D		((CJ 	 	 	D	ts    
  5 
AAc                 j    i | ]/\  }}d |v 	|f |                     d           d                   0S ):r   )split).0ir3   rF   s      r   
<dictcomp>z"compile_kernel.<locals>.<dictcomp>v   sE    \\\41aSVZ[S[S[aUIIaggcll1o..S[S[S[r   c                     i | ]
\  }}|||S rB   r   rJ   kvs      r   rL   z"compile_kernel.<locals>.<dictcomp>w   s    ===daq}Q}}}r   c                 D    i | ]\  }}j         |          |          S r   )	arg_names)rJ   rK   r3   rF   kernels      r   rL   z"compile_kernel.<locals>.<dictcomp>x   s/    TTTtq!!!$iillTTTr   c                     i | ]
\  }}|||S rB   r   rN   s      r   rL   z"compile_kernel.<locals>.<dictcomp>y   s    EEE$!Qq}A}}}r   r   c                 d    i | ],\  }}j         |         |                    d           d         -S )rH   r   )rR   rI   )rJ   rK   r3   rS   s      r   rL   z"compile_kernel.<locals>.<dictcomp>}   s5    WWW$!Q!!$aggcll1oWWWr   rF   xc                 ,    g | ]}t          |          S r   )r   )rJ   rP   s     r   
<listcomp>z"compile_kernel.<locals>.<listcomp>   s    ===Q#a&&===r   c                 "    g | ]\  }}| d | S )=r   rN   s      r   rX   z"compile_kernel.<locals>.<listcomp>   s&    ;;;AQ****;;;r   z
num_warps=znum_stages=)r      z#Only 1 and 16 are valid hints, got c                 ,    i | ]\  }}|d k    |dd ggS )r[   ztt.divisibilityr   rN   s      r   rL   z"compile_kernel.<locals>.<dictcomp>   s,    OOOdaqBwwQ#R()wwwr   )fn
constexprsr   attrsrH   )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	   cr[   d_   r   triton_kernel_namebin_sizebin_dataz, c                 "    g | ]\  }}d | | S )0xr   )rJ   rV   ys      r   rX   z"compile_kernel.<locals>.<listcomp>   s&    RRRtq!{A{q{{RRRr   c                 6    g | ]\  }} |           d | S r1   r   rJ   namety	ty_to_cpps      r   rX   z"compile_kernel.<locals>.<listcomp>   s3    ppphdB99R== 9 94 9 9pppr   full_signaturec                 6    g | ]\  }} |           d | S ro   r   rp   s      r   rX   z"compile_kernel.<locals>.<listcomp>   s3    $i$i$i84		"%>%>%>%>$i$i$ir   arg_pointersc                     g | ]}d | S )&r   )rJ   args     r   rX   z"compile_kernel.<locals>.<listcomp>   s    "H"H"H9s99"H"H"Hr   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   rI   lenlistmapr   r   r   r   	enumerateitemsrR   r:   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zipr|   backend__file__globsuffixwith_suffixopenwrite	read_textformat)/r,   r   r   arg_pathspecmodr   r   r>   meta_sigsig_hashhints	constantskeyvalue	const_sig
doc_stringhr_   srcr   r   kwargsr`   ccinforR   	arg_typesarg_names_not_1arg_types_not_1rK   arg_namer   rr   	func_namer   hex_paramsoutput_filesbackend_nametemplate_dirtemplate_pathextoutput_filefprF   rS   rs   s/                                               @@@r   r)   r)   P   s    $Ct}}43CH $At}}4>>H DIHHOOAs8?++,,,>11(-JJD
.
)
)$
/
/CKC   S$*++F9??3Dt99>>>> S//1E1Ec1J1JKKLLI!$s) ! ! ! !
 @t~??do??H~i8*455H   ]\\\9Y;O;O\\\E==ekkmm===ETTTTTy?S?STTTIEE)//"3"3EEEIkkmm 8 8
UA::27If&s1v./WWWW)IBVBVWWWI % %$	#==)*:*:*<*<===>>I;;):):;;;J0002Q2Q2QRRJ\\^^ G GG|||F1FF||||OOekkmmOOOE
/
#
#v)y`e
#
f
fC ;NV_%/1B1B31G1GHH"N18KKMM o**622G>IIF##F++G^C8HIIIFv 5q99A==jkkk+a//klllIIOO !122 $ $89$$X&&&Yx0111""8,,,""9X#67777YYud##q((X&&&U### F9++--..  2#a&&99aUD!!Q&&cMF99aUD!!R''cMF(Hf566I
*W'
(Cx$$%%ad+D%,CIyd. 	CHH 	DIIRRs4!9d14a4j7Q7QRRRSS	
 	TYYpppp#o_nJoJopppqq 	$))$i$i$i$isS\^gOhOh$i$i$ijj 			"H"H"H"H"HL]K^"^btau"uvv 	C((1, 	J 	&/( 	T^ 	SXXy(344 	a 	a 	a  	!F$ L>L>>(72\AL%**;77 ) )"**+Gx+G+G&+G#+G+GHHc"" 	AbHH5],,..5????@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	AK((((l""s   $3_##_'	*_'	__main__)r   r7   importlib.utilr   r   argparser   dataclassesr   pathlibr   typingr   r   triton.backendsr   r%   r-   r)   r   r   r   r   <module>r      s         



 # # # # # # ! ! ! ! ! !                  ! ! ! ! ! ! ! !6  .~# ~# ~# ~# ~#B zDFFFFF r   