o
    i                     @   s  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reed	Zejd
dd ejddedddd ejddeddd ejddeddd ejddeddd ejdded dd! e ZejrejnejZejrejn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)e*d"d# ej+,d$Z+d%e
e fd&d'Z-e-e+Z.d(d) Z/d*d+ e0e+D Z1d,d+ e12 D Z1d-d+ e0e+D Z3d.d+ e32 D Z3d/d+ e0e+D Z+e14 D ]Z5e5d0v sJ d1e5 qd2d3 e12 D Z6d4d3 e12 D Z7ej8j9e6e7d5Z:ej;e(e+e3e:gej<d6Z=d7d3 e+> D Z?ee+4 e:Z@d8Aee.e@gZBd8Aeje@gZCee De=jEd9 d:d; ZFeBeCeGeFd<Ad=d3 eHeFddd: eFddd: D d<Ad>d3 eHe?e+4 D d<Ad?d3 e?D eGe?de=jIej<dd@ZJdAD ]@ZKeeLjdBeK  ZMeNdCe. d8e@ dCeK OdDZPePQeeMR jSdEi eJ W d   n	1 sw   Y  qdS dS )F    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 "*f32: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
--out-namez-onz Out name for the compiled kernelz
--out-pathz-ozOut filenamez--signaturez-szSignature of the kernel)r   r   r   c                 C   s
   |  dS )N )strip)s r   c/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/triton/tools/compile.py<lambda>?   s   
 r   ,	signaturec                 C   s,   t  }|d|   | d d S )Nr      )hashlibsha256updatejoinencode	hexdigest)r   mr   r   r   hash_signatureA   s   r!   c                 C   sF   zt | }|W S  ty   Y nw zt| }|W S  ty"   Y d S w N)int
ValueErrorfloat)r   retr   r   r   	constexprH   s   r'   c                 C   s,   i | ]\}}d |v r|t |d d qS ):r   )r'   split.0ir   r   r   r   
<dictcomp>U   s   , r-   c                 C      i | ]\}}|d ur||qS r"   r   r+   kvr   r   r   r-   V       c                 C   s   i | ]	\}}|t |qS r   )r'   r*   r   r   r   r-   W   s    c                 C   r.   r"   r   r/   r   r   r   r-   X   r2   c                 C   s(   i | ]\}}|t vr||d d qS )r(   r   )
constexprsr)   r*   r   r   r   r-   Y   s   ( )r      z#Only 1 and 16 are valid hints, got c                 C      g | ]
\}}|d kr|qS )r4   r   r+   r,   hr   r   r   
<listcomp>^       r8   c                 C   r5   )r   r   r6   r   r   r   r8   _   r9   )divisible_by_16
equal_to_1)r   	constantsZconfigs	num_warpsc                 C   s   g | ]}t j| qS r   )kernel	arg_names)r+   r,   r   r   r   r8   b       _Zcubin   z, c                 C   s   g | ]\}}d | | qS )0xr   )r+   xyr   r   r   r8   m   r2   c                 C   s"   g | ]\}}t | d | qS )r   r   )r+   nametyr   r   r   r8   n   s   " c                 C   s   g | ]}d | qS )&r   )r+   argr   r   r   r8   o   r@   )kernel_nametriton_kernel_nameZbin_sizeZbin_datar   Zarg_pointersnum_argsZkernel_docstringsharedr=   Z_placeholder)r7   czcompile..wr   )Tbinasciir   importlib.util	importlibsysargparser   pathlibr   typingr   ZtritonZtriton.compiler.code_generatorr   Ztriton.compiler.make_launcherr   Zdesc__name__parseradd_argumentstrr#   
parse_argsargsZout_namerK   Zout_pathr
   Zarg_pathinsertparentutilspec_from_file_locationstemspecmodule_from_specmodloaderexec_modulegetattrr>   listmapr   r)   r!   Zsig_hashr'   	enumeratehintsitemsr3   valuesr7   r:   r;   compilerZinstance_descriptorconfigcompiler=   Zccinfokeysr?   suffixr   	func_namerL   hexlifyasmZhex_lenziprN   paramsext__file__Ztemplate_pathwith_suffixopenfpwrite	read_textformatr   r   r   r   <module>   s    


,$ O