U
    ‹Ñh£%  ã                   @   sü   d dl Z d dlmZ d dlZd dlZd dlZd dlm	Z	m
Z
 dd„ Zdd„ Zdd	„ Zd
d„ ZedddgƒZeddddddœƒZedddddœƒZedddddœƒZdd„ ej ¡ D ƒdd„ ej ¡ D ƒ–Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zed&krøeƒ  dS )'é    N)Ú
namedtuple)ÚCOMPUTE_METADATA_SCOPE_NAMEÚ
TritonHookc                 C   sl   g }| rZ| D ]J}|  ¡ }|D ]8}| d¡d  ¡   ¡ }|||fkr| |d ¡  qqqn|d gd }|S )Nú(r   ú (inc))ÚlowerÚsplitÚstripÚappend)ÚmetricsÚraw_metricsÚretÚmetricÚ
raw_metricÚraw_metric_no_unit© r   úD/tmp/user/2202/pip-unpacked-wheel-sn6cy74c/triton/profiler/viewer.pyÚmatch_available_metrics
   s    
r   c                 C   s.   t  | ¡}| d¡}tj |¡}|| ¡ |fS )Né   )ÚjsonÚloadÚpopÚhtZ
GraphFrameZfrom_literalZshow_metric_columns)ÚfileZdatabaseÚdevice_infoÚgfr   r   r   Úget_raw_metrics   s    

r   c              	   C   sv  t jd| jdgd�}|D �]V}|| D �]F}|| | d }|| | d }|| | d }tjD �]}| d |k}	| |	 }
d|› �|
jkrŽqbd	}|d
krð|dkr°d|d  }n>|dkrÆd|d  }n(|dkrî|d | d d d |d  }nR|dk�r4|dk�rd|d  }n |dk�s&|dk�rBd|d  }ntd|› �ƒ‚|j|	df  |
d|› �  d	¡| 7  < qbq&q|S )Nç        Úmin_time©ÚindexÚcolumnsÚarchÚnum_smsÚ
clock_rateÚDeviceIdÚflopsr   ZCUDAÚ80g  ¸’2¼Cé   Z89g €µbÆòBZ90ér   g    xÇ:Aié  ç   ¢”mBZHIPZgfx90ag  //bÅõBZgfx941Zgfx942g ?³y”"CzUnsupported device type: )	ÚpdÚ	DataFramer    r   Zflops_widthr!   Ú
ValueErrorÚlocZfillna)Údfr   Úmin_time_flopsÚdevice_typeÚdevice_indexr"   r#   r$   ÚwidthÚidxÚdevice_framesZ	max_flopsr   r   r   Úget_min_time_flops    s6    
"

0r6   c           
      C   s–   t jd| jdgd�}|D ]x}|| D ]j}| d |k}| | }|| | d }|| | d }d| | d d	 }	|j|df  |d
 |	 7  < q$q|S )Nr   r   r   r%   Úmemory_clock_rateÚ	bus_widthé   g     @�@r(   Úbytes)r+   r,   r    r.   )
r/   r   Úmin_time_bytesr1   r2   r4   r5   r7   r8   Zpeak_bandwidthr   r   r   Úget_min_time_bytesA   s    "r<   Ú
FactorDictÚnameÚfactorÚtimer   gü©ñÒMbP?g�íµ ÷Æ°>g•Ö&è.>)útime/sztime/msztime/usztime/nsr&   g    eÍÍAr*   )zflop/szgflop/sztflop/sr:   )zbyte/szgbyte/sztbyte/sc                 C   s   i | ]
}|t “qS r   )Úflops_factor_dict©Ú.0Úkeyr   r   r   Ú
<dictcomp>T   s   ÿ rF   c                 C   s   i | ]
}|t “qS r   )Úbytes_factor_dictrC   r   r   r   rF   V   s   ÿ c                 C   s¤  g }g }t tjg|ƒd }tjd | d¡d  d¡d  }|D �]H}|dkr®t| j|ƒ}	t| j|ƒ}
| j| tj| tjd   }|
d  |	d t	¡| | jd	< | 
d	¡ q@|tk�r t| }|j}|j}t |g|ƒd }| j| | j| tj|   ||  | j|› d
�< | 
|› d
�¡ q@|tjk�r€tjd | d¡d  }| j| tj| tj|   | j|› d
�< | 
|› d
�¡ q@| 
|¡ q@|�rœt ||ƒ}|| S )Nr   ú/r   r   ú)ÚutilrA   r   z
util (inc)r   )r   Útime_factor_dictr>   r   r<   Z	dataframer6   r?   ÚcombineÚmaxr
   Úderivable_metrics)r   r   r   r   Zderived_metricsZoriginal_metricsZtime_metric_nameZ	time_unitr   r;   r0   Ztime_secZderiveable_metricZmetric_nameZmetric_factor_dictZmatched_metric_nameZmetric_time_unitr   r   r   Úderive_metrics[   sD    "
ÿ
ÿþÿ
rO   c              	   C   sæ   t |dƒ�Ò}t|ƒ\}}}	t|ƒdks.tdƒ‚| ¡  t|| ||	ƒ} |sL|rz|r\d|› d�n|}
dd|
ig}|j|dd	�}ddt› d�ig}|j|dd	�}|rÂd| d d
|› �ig}|j|dd	�}t|j	| d|dd�ƒ W 5 Q R X d S )NÚrr   z"No metrics found in the input filez^(?!z).*Ú*r>   T)Zsquashz>= F)Zmetric_columnZexpand_nameÚdepthZrender_header)
Úopenr   ÚlenÚAssertionErrorZupdate_inclusive_columnsrO   Úfilterr   ÚprintÚtree)r   ÚfilenameÚincludeÚexcludeÚ	thresholdrR   Úfr   r   r   Zname_filterÚqueryr   r   r   Úparse~   s    r_   c              	   C   sl   t | dƒ�X}t|ƒ\}}}tdƒ |rT|D ](}| d¡d  ¡  ¡ }td|› �ƒ q*W 5 Q R £ d S Q R X d S )NrP   zAvailable metrics:r   r   z- )rS   r   rW   r   r	   r   )Ú	file_namer]   Ú_r   r   r   r   r   r   Úshow_metrics“   s    rb   c            	      C   s  t jdt jd�} | jddddd� | jdd	td d
d� | jddtd dd� | jddtd dd� | jddtd dd� | jddtddd� |  ¡ \}}t|ƒdks¦t	dƒ‚|d }|j
rÀ|j
 d¡nd }|j}|j}|j}|j}|rì|rìtdƒ‚|jrüt|ƒ n|�rt||||||ƒ d S )Nz,Performance data viewer for proton profiles.)ÚdescriptionÚformatter_classz-lz--listÚ
store_truea`  List available metrics. Metric names are case insensitive and ignore units.
Derived metrics can be created when source metrics are available.
- time/s, time/ms, time/us, time/ns: time
- flop/s, gflop/s, tflop/s: flops / time
- byte/s, gbyte/s, tbyte/s: bytes / time
- util: max(sum(flops<width>) / peak_flops<width>_time, bytes / peak_bandwidth_time))
)ÚactionÚhelpz-mz	--metricszÐAt maximum two metrics can be specified, separated by comma.
There are two modes:
1) Choose the output metric to display. It's case insensitive and ignore units.
2) Derive a new metric from existing metrics.
)ÚtypeÚdefaultrg   z-iz	--includez?Include frames(kernels) that match the given regular expressionz-ez	--excludez?Exclude frames(kernels) that match the given regular expressionz-tz--thresholdzrExclude frames(kernels) whose metrics are below the given threshold. This filter only applies on the first metric.z-dz--depthéd   z The depth of the tree to displayr   zMust specify a file to readr   ú,z'Cannot specify both include and exclude)ÚargparseÚArgumentParserÚRawTextHelpFormatterÚadd_argumentÚstrÚfloatÚintÚparse_known_argsrT   rU   r   r   rZ   r[   r\   rR   r-   Úlistrb   r_   )	Z	argparserÚargsZtarget_argsr`   r   rZ   r[   r\   rR   r   r   r   Úmainž   sv    þüûûûú	û
rv   Ú__main__)rl   Úcollectionsr   r   Zpandasr+   Zhatchetr   Ztriton.profiler.hookr   r   r   r   r6   r<   r=   rK   rB   rG   r?   ÚkeysrN   rO   r_   rb   rv   Ú__name__r   r   r   r   Ú<module>   s4   !ÿÿý#M