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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   H/var/www/html/venv/lib/python3.8/site-packages/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   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r7   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/   )
r0   r   min_time_bytesr2   r3   r5   r6   r8   r9   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    rG   c                 C   s   i | ]
}|t qS r   )bytes_factor_dictrD   r   r   r   rG   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   )utilrB   r   z
util (inc)r   )r   time_factor_dictr?   r   r=   Z	dataframer7   r@   combinemaxr
   derivable_metrics)r   r   r   r   Zderived_metricsZoriginal_metricsZtime_metric_nameZ	time_unitr   r<   r1   Ztime_secZderiveable_metricZmetric_nameZmetric_factor_dictZmatched_metric_nameZmetric_time_unitr   r   r   derive_metrics[   sD    "


rP   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_columnsrP   filterr   printtree)r   filenameincludeexclude	thresholdrS   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 )NrQ   zAvailable metrics:r   r   z- )rT   r   rX   r   r	   r   )	file_namer^   _r   r   r   r   r   r   show_metrics   s    rc   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defaultrh   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_argsrU   rV   r   r   r[   r\   r]   rS   r.   listrc   r`   )	Z	argparserargsZtarget_argsra   r   r[   r\   r]   rS   r   r   r   main   sv    	
rw   __main__)rm   collectionsr   r   Zpandasr,   Zhatchetr   Ztriton.profiler.hookr   r   r   r   r7   r=   r>   rL   rC   rH   r@   keysrO   rP   r`   rc   rw   __name__r   r   r   r   <module>   s4   !#M