U
    di%                     @   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re  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   :/tmp/pip-unpacked-wheel-8bvfr03u/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rqbd	}|d
kr|dkrd|d  }n>|dkrd|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90r   g    x:Ai     mBZHIPZgfx90ag  //bBZ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   gMbP?gư>g&.>)time/sztime/msztime/usztime/nsr&   g    eAr*   )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rt| 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rtjd |dd  }| j| tj| tj|   | j| d
< |
| d
 q@|
| q@|rt ||}|| 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	}|rd| 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st	d|d }|j
r|j
dnd }|j}|j}|j}|j}|r|rtd|jrt| 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	--metricszAt 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--depthd   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