U
    ‹Ñh«G  ã                   @   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mZm	Z	 ddl
mZ dd„ Zd&d
d„Zd'dd„Zd(dd„ZG dd„ dƒZG dd„ dƒZdd„ Zd)dd„Zd*dd„Zdd„ Zed+d"d#„ƒZd,d$d%„ZdS )-é    N)Úcontextmanager)ÚAnyÚDictÚListé   )Úlanguagec                 C   sL   d  | ¡} dddd|  dg}t |¡}| tjj¡ d¡}dd„ |D ƒ}|S )	Nú,ú
nvidia-smiú-iÚ0z--query-gpu=z--format=csv,noheader,nounitsc                 S   s   g | ]}t |ƒ‘qS © )Úint©Ú.0Úxr   r   ú</tmp/user/2202/pip-unpacked-wheel-sn6cy74c/triton/testing.pyÚ
<listcomp>   s     znvsmi.<locals>.<listcomp>)ÚjoinÚ
subprocessÚcheck_outputÚdecodeÚsysÚstdoutÚencodingÚsplit)ÚattrsÚcmdÚoutÚretr   r   r   Únvsmi
   s    

r   é   Úmeanc              	   C   sÐ  ddl }|dkst‚|j ¡ |j ¡ kr0tdƒ‚| ƒ  |dk	r`|D ]}| ¡  | d¡ d|_qB|j 	¡ }|j 
|¡� | ƒ  W 5 Q R X |j ¡  |jjdd�}|jjdd�}| ¡  | ¡  | ¡  |j ¡  | |¡}	tdt||	 ƒƒ}
|j 	¡ }|j 
|¡�8 t|
ƒD ](}|dk	�r,|D ]}d|_�q| ƒ  �qW 5 Q R X |j ¡  g }d}t|ƒD ]X}|jjdd�}|jjdd�}| ¡  | ¡  | ¡  |j ¡  || |¡|
 g7 }�qZ| |¡}t||ƒ|ƒ ¡ S )	a+  
    Benchmark the runtime of the provided function.

    :param fn: Function to benchmark
    :type fn: Callable
    :param rep: Repetition time (in ms)
    :type rep: int
    :param grad_to_none: Reset the gradient of the provided tensor to None
    :type grad_to_none: torch.tensor, optional
    r   N©ÚminÚmaxr!   ZmedianzQCannot capture graph in default stream. Please use side stream in benchmark code.T©Zenable_timingr   é
   )ÚtorchÚAssertionErrorÚcudaZcurrent_streamZdefault_streamÚRuntimeErrorZdetach_Zrequires_grad_ÚgradZ	CUDAGraphÚgraphÚsynchronizeÚEventÚrecordZreplayÚelapsed_timer$   r   ÚrangeÚtensorÚgetattrÚitem)ÚfnÚrepÚgrad_to_noneÚreturn_moder'   r   ÚgÚstart_eventÚ	end_eventÚestimate_msÚn_repeatÚir   Z	n_retriesÚtimesr   r   r   Údo_bench_cudagraph   sT    










r@   é   éd   Tc                    sê  |dkst ‚ddl‰ | ƒ  ˆ j ¡  |r@ˆ jtdƒˆ jdd�}nˆ jtdƒˆ jdd�}ˆ jjdd	�}ˆ jjdd	�}	| ¡  t	d
ƒD ]}
| 
¡  | ƒ  q‚|	 ¡  ˆ j ¡  | |	¡d
 }tdt|| ƒƒ}tdt|| ƒƒ}‡ fdd„t	|ƒD ƒ}‡ fdd„t	|ƒD ƒ}	t	|ƒD ]}
| ƒ  �qt	|ƒD ]H}|dk	�rD|D ]}d|_�q6| 
¡  ||  ¡  | ƒ  |	|  ¡  �q$ˆ j ¡  ˆ jdd„ t||	ƒD ƒˆ jd�}|dk	�rØˆ  |ˆ j|ˆ jd�¡ ¡ }t|ƒdk�rÔ|d }|S tˆ |ƒ|ƒ ¡ S )a¯  
    Benchmark the runtime of the provided function. By default, return the median runtime of :code:`fn` along with
    the 20-th and 80-th performance percentile.

    :param fn: Function to benchmark
    :type fn: Callable
    :param warmup: Warmup time (in ms)
    :type warmup: int
    :param rep: Repetition time (in ms)
    :type rep: int
    :param grad_to_none: Reset the gradient of the provided tensor to None
    :type grad_to_none: torch.tensor, optional
    :param quantiles: Performance percentile to return in addition to the median.
    :type quantiles: list[float]
    :param fast_flush: Use faster kernel to flush L2 between measurements
    :type fast_flush: bool
    r"   r   Ng    €„ŽAr)   )ÚdtypeÚdeviceg    €„®ATr%   é   r   c                    s   g | ]}ˆ j jd d�‘qS ©Tr%   ©r)   r.   ©r   r>   ©r'   r   r   r   €   s     zdo_bench.<locals>.<listcomp>c                    s   g | ]}ˆ j jd d�‘qS rF   rG   rH   rI   r   r   r   �   s     c                 S   s   g | ]\}}|  |¡‘qS r   )r0   )r   ÚsÚer   r   r   r   •   s     )rC   )r(   r'   r)   r-   Úemptyr   Úint8r.   r/   r1   Zzero_r0   r$   r+   r2   ÚzipÚfloatZquantileÚtolistÚlenr3   r4   )r5   Zwarmupr6   r7   Z	quantilesZ
fast_flushr8   Úcacher:   r;   Ú_r<   Zn_warmupr=   r>   r   r?   r   r   rI   r   Údo_benchR   sL    





 
rT   Ú c                 C   sN  dd l }dd l}t| |jƒs&| | ¡} t||jƒs<| |¡}|d krHd}t|ƒrZ|| jƒn|}|d krjd}t|ƒr||| jƒn|}t| |jƒr°| j|jkr |  ¡ } |  	¡  
¡   ¡ } t||jƒrà|j|jkrÐ| ¡ }| 	¡  
¡   ¡ }| jdksö|jdk�r|jj| |||dd� d S |j| |||d��sJt|› d| › d	|› d
|› d|› d�
ƒ‚d S )Nr   g{®Gáz„?g        r   T)ÚatolÚrtolZ	equal_nan)rV   rW   ú z is not close to z (atol=z, rtol=ú))Znumpyr'   Ú
isinstanceZTensorr2   ÚcallablerC   Úbfloat16rO   ÚcpuÚdetachÚsizeZtestingZassert_allcloseZallcloser(   )r   ÚyrV   rW   Úerr_msgÚnpr'   r   r   r   Úassert_closež   s2    

rc   c                   @   sL   e Zd ZdZdee ee eee ee eeeef eeeedœdd„Z	dS )	Ú	Benchmarkzk
    This class is used by the :code:`perf_report` function to generate line plots with a concise API.
    rU   FN)Úx_namesÚx_valsÚline_argÚ	line_valsÚ
line_namesÚ	plot_nameÚargsÚxlabelÚylabelÚx_logÚy_logc                 C   sL   || _ || _|
| _|| _|| _|| _|| _|| _|| _|	| _	|| _
|| _dS )aÛ  
        Constructor.
        x_vals can be a list of scalars or a list of tuples/lists. If x_vals is a list
        of scalars and there are multiple x_names, all arguments will have the same value.
        If x_vals is a list of tuples/lists, each element should have the same length as
        x_names.

        :param x_names: Name of the arguments that should appear on the x axis of the plot.
        :type x_names: List[str]
        :param x_vals: List of values to use for the arguments in :code:`x_names`.
        :type x_vals: List[Any]
        :param line_arg: Argument name for which different values correspond to different lines in the plot.
        :type line_arg: str
        :param line_vals: List of values to use for the arguments in :code:`line_arg`.
        :type line_vals: List[Any]
        :param line_names: Label names for the different lines.
        :type line_names: List[str]
        :param plot_name: Name of the plot.
        :type plot_name: str
        :param args: Dictionary of keyword arguments to remain fixed throughout the benchmark.
        :type args: Dict[str, Any]
        :param xlabel: Label for the x axis of the plot.
        :type xlabel: str, optional
        :param ylabel: Label for the y axis of the plot.
        :type ylabel: str, optional
        :param x_log: Whether the x axis should be log scale.
        :type x_log: bool, optional
        :param y_log: Whether the y axis should be log scale.
        :type y_log: bool, optional
        N)re   rf   rn   rg   rh   ri   ro   Ústylesrl   rm   rj   rk   )Úselfre   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   Úcolorrp   r   r   r   Ú__init__È   s    .zBenchmark.__init__)rU   rU   FFNN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   Úboolrs   r   r   r   r   rd   Ã   s(         ò
ôrd   c                   @   s4   e Zd Zdd„ Zdeeeedœdd„Zdd	d
„ZdS )ÚMarkc                 C   s   || _ || _d S ©N)r5   Ú
benchmarks)rq   r5   r|   r   r   r   rs     s    zMark.__init__Fé   )ÚbenchÚ	save_pathÚ
show_plotsÚ
print_datac              
      sv  dd l }dd lm}	 dd l}
|j}dd„ |jD ƒ}dd„ |jD ƒ}t|jƒ}|
j|| | | d�}|jD �]‰ t	ˆ tt
fƒs�‡ fdd„|D ƒ‰ tˆ ƒt|ƒkr¸tdt|ƒ› dˆ › �ƒ‚tt|ˆ ƒƒ}g g g   }}}|jD ]t}| jf ||j|i|j|—Ž}z|\}}}W n& tk
�r0   |d d   }}}Y nX ||g7 }||g7 }||g7 }qÜtˆ ƒ| | | |jt|ƒ< qj|j�rÜ|	 ¡  |	 ¡ }|d }t|jƒD ]¼\}}||d	  ||d
   }}|j�rÔ|j| d nd }|j�rî|j| d nd }|j|| || |||d� | ¡  ¡ �sœ| ¡  ¡ �sœ| t¡}| t¡}|j|| ||d|d� �qœ|  ¡  | !|j"�pp|¡ | #|j$¡ | %|j&�r�dnd¡ | '|j(�r¦dnd¡ |�rº|	 )¡  |�rÜ|	 *|j+ ,||j› d�¡¡ |||j  }|�r"|j-d dk�r"|j. /¡ \}}|| ||  |d< |�rBt0|jd ƒ t0| 1¡ ƒ |�rr|j2|j+ ,||j› d�¡d|› d�dd� |S )Nr   c                 S   s   g | ]}|› d �‘qS )ú-minr   r   r   r   r   r     s     zMark._run.<locals>.<listcomp>c                 S   s   g | ]}|› d �‘qS )ú-maxr   r   r   r   r   r     s     )Úcolumnsc                    s   g | ]}ˆ ‘qS r   r   )r   rS   ©r   r   r   r     s     z	Expected z values, got r‚   rƒ   r   )Úlabelrr   Zlsg333333Ã?)Úalpharr   ÚlogZlinearz.pngé   ZDiffú:z.csvz%.ÚfF)Zfloat_formatÚindex)3ÚosZmatplotlib.pyplotZpyplotZpandasri   Úlistre   Z	DataFramerf   rZ   ÚtuplerQ   Ú
ValueErrorÚdictrN   rh   r5   rg   rk   Ú	TypeErrorÚlocrj   ÚfigureZsubplotÚ	enumeraterp   ZplotZisnullÚallZastyperO   Zfill_betweenZlegendZ
set_xlabelrl   Z
set_ylabelrm   Z
set_xscalern   Z
set_yscalero   ÚshowZsavefigÚpathr   Úshaper„   rP   ÚprintZ	to_stringZto_csv)rq   r~   r   r€   r�   Zdiff_colZsave_precisionZkwragsr�   ZpltÚpdZy_meanZy_minZy_maxre   ZdfZx_argsZrow_meanZrow_minZrow_maxr`   r   ZaxZfirst_xr>   ÚcolZstyZcol0Zcol1r   r…   r   Ú_run  sv    



 

"ÿz	Mark._runrU   c                 K   s¼   t | jtƒ}|r| jgn| j}g }|rRtj|dd� ttj |d¡dƒ}	|	 d¡ |D ]6}
| 	| j
|
|||f|Ž¡ |rV|	 d|
j› d�¡ qV|r¤|	 d¡ |	 ¡  |r¸|r´|d	 S |S d S )
NT)Úexist_okzresults.htmlÚwz<html><body>
z<image src="z.png"/>
z</body></html>
r   )rZ   r|   rd   r�   ÚmakedirsÚopenr˜   r   ÚwriteÚappendr�   rj   Úclose)rq   r€   r�   r   Z	return_dfÚkwargsZhas_single_benchr|   Z
result_dfsÚhtmlr~   r   r   r   ÚrunP  s&    

zMark.runN)Fr}   )FFrU   F)	rt   ru   rv   rs   rd   rx   ry   r�   r§   r   r   r   r   rz     s
     ÿErz   c                    s   ‡ fdd„}|S )zê
    Mark a function for benchmarking. The benchmark can then be executed by using the :code:`.run` method on the return value.

    :param benchmarks: Benchmarking configurations.
    :type benchmarks: List of :class:`Benchmark`
    c                    s
   t | ˆ ƒS r{   )rz   )r5   ©r|   r   r   Ú<lambda>o  ó    zperf_report.<locals>.<lambda>r   )r|   Úwrapperr   r¨   r   Úperf_reporth  s    r¬   c                 C   s^   ddl }ddlm} | s"|j ¡ } |jj | ¡d }|jj | ¡d }|| d d d	 }|S )
z return DRAM bandwidth in GB/s r   Nr   ©ÚdriverZmem_clock_rateZmem_bus_widthr‰   g    €„.Aé   )r'   Úruntimer®   r)   Úcurrent_deviceÚactiveÚutilsÚget_device_properties)rD   r'   r®   Zmem_clock_khzZ	bus_widthZbw_gbpsr   r   r   Úget_dram_gbpss  s    
rµ   c           	      C   sÎ   dd l }ddlm} |s"|j ¡ }|jj |¡d d }|j |¡}|d dk rd| |j	ks^t
‚d}nV| |j|jfkrzd}n@| |j	|j|jfkr”d}n&| |jtjtjtjfkr²d	}ntd
ƒ‚|| | d }|S )Nr   r   r­   Úmultiprocessor_counté   r¯   é   i   i   údtype not supportedç•Ö&è.>)r'   r°   r®   r)   r±   r²   r³   r´   Úget_device_capabilityÚfloat16r(   Úfloat32Zint32r\   Zint16rM   ÚtlZ
float8e4nvZfloat8e4b15Zfloat8e5r*   ©	rC   Z
clock_raterD   r'   r®   Znum_subcoresZ
capabilityZops_per_sub_coreÚtflopsr   r   r   Úget_max_tensorcore_tflops€  s$    
rÁ   c                     s   ‡ fdd„}|S )Nc                    s   t  ˆ ¡‡‡ fdd„ƒ}|S )Nc            
         sÚ   dd l }| t ¡ ¡ ¡ }ˆ  ¡ | ¡ k}|rÌ|dkrÌtj ˆjd ¡}tj	d ddœ}d|ksht
dƒ‚|d jjj}|› d	ˆj› d
|› d�}tjddd|gd|d�}	|	jdks¸t
dƒ‚dt|	jƒksÖt
‚n
ˆ| |Ž d S )Nr   zcuda-memcheckÚ__file__ÚPATHÚ1)rÃ   ZPYTORCH_NO_CUDA_MEMORY_CACHINGÚrequestz@memcheck'ed test must have a (possibly unused) `request` fixturez::ú[ú]Zpytestz-vsT)Úcapture_outputÚenvz7cuda-memcheck returned an error: bounds checking failedzERROR SUMMARY: 0 errors)ÚpsutilÚProcessr�   ÚgetppidÚnameÚitemsr˜   ÚrealpathÚ__globals__Úenvironr(   ÚnodeZcallspecÚidrt   r   r§   Ú
returncoderx   r   )
rk   r¥   rÊ   Z	ppid_nameZrun_cuda_memcheckr˜   rÉ   Ztest_idr   r   )Útarget_kwargsÚtest_fnr   r   r«   ¡  s    z1cuda_memcheck.<locals>.decorator.<locals>.wrapper)Ú	functoolsÚwraps)rÖ   r«   ©rÕ   )rÖ   r   Ú	decoratorŸ  s    z cuda_memcheck.<locals>.decoratorr   )rÕ   rÚ   r   rÙ   r   Úcuda_memcheck�  s    rÛ   éF  é¿  c                 c   s  zÎt  dddddg¡ t  dddd| › d	| › �g¡ t  dddd
|› d	|› �g¡ tdgƒd }tdgƒd }t||  ƒdk sŽtd| › d�ƒ‚t|| ƒdk s®td|› d�ƒ‚d|  }d| d }||fV  W 5 t  dddddg¡ t  ddddg¡ t  ddddg¡ X d S )Nr	   r
   r   z-pmz-rgcz-rmcrÄ   z--lock-gpu-clocks=r   z--lock-memory-clocks=zclocks.current.smr   zclocks.current.memoryr&   zGPU SMs must run at z MHzgÞ 3ßÁOÌ?i   gü©ñÒMbP?)r   r   r   Úabsr(   )Zref_sm_clockZref_mem_clockZcur_sm_clockZcur_mem_clockrÀ   Zgbpsr   r   r   Úset_gpu_clock¸  s0    üü  rß   c           	      C   sº   dd l }ddlm} |s"|j ¡ }|jj |¡d d }|j ¡ }|d dk rx| |j	kr^d}q¦| |j
krnd}q¦td	ƒ‚n.| |j	krˆd}n| |j
|jfkržd}ntd	ƒ‚|| | d
 }|S )Nr   r   r­   r¶   r·   r¯   é    é@   r¹   rº   )r'   r°   r®   r)   r±   r²   r³   r´   r»   r½   r¼   r*   r\   r¿   r   r   r   Úget_max_simd_tflopsÕ  s&    





râ   )r    Nr!   )rA   rB   NNTr!   )NNrU   )N)N)rÜ   rÝ   )N)r×   r�   r   r   Ú
contextlibr   Útypingr   r   r   rU   r   r¾   r   r@   rT   rc   rd   rz   r¬   rµ   rÁ   rÛ   rß   râ   r   r   r   r   Ú<module>   s&   	
?
L
%Bc

