
    wi5                        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	 n# e
$ r  e
d          w xY wd dlZd dlmZmZ d Zd Zd Zd	 Z ed
ddg          Z edddddd          Z edd ej                                        D                       Z eddddd          Zi d ej                                        D             ZddddddiZe                    d e                                D                        ej        D ]LZde Z de d dd!e d dd"e d diZ!e                    d# e!                                D                        Md$ Z"d% Z#d-d&Z$d.d(Z%d) Z&d* Z'd+ Z(e)d,k    r e(             dS dS )/    N)
namedtuple)NegationQueryzPFailed to import hatchet. `pip install llnl-hatchet` to get the correct version.)COMPUTE_METADATA_SCOPE_NAME
TritonHookc                 n   g }| r~| D ]z}|                                 }|D ]a}|                    d          d                                                                          }|||fv r|                    |dz               nb{n|d         dz   g}t	          |          dk    rt          d| d          |S )N(r    (inc)zMetric z< is not found. Use the --list flag to list available metrics)lowersplitstripappendlenRuntimeError)metricsraw_metricsretmetric
raw_metricraw_metric_no_units         f/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/triton/profiler/viewer.pymatch_available_metricsr      s    
C 	* 	 	F\\^^F)  
%/%5%5c%:%:1%=%C%C%E%E%K%K%M%M"j*<===JJzH4555E >		 1~()
3xx1}}iViiijjjJ    c                     t          j        |           }|                    d          }t          j                            |          }||                                |fS )N   )jsonloadpopht
GraphFramefrom_literalshow_metric_columns)filedatabasedevice_infogfs       r   get_raw_metricsr&      sL    yH,,q//K		#	#H	-	-Br%%''44r   c           	         t          j        d| j        dg          }|D ]2}||         D ]%}||         |         d         }||         |         d         }||         |         d         }t          j        D ]}| d         |k    }	| |	         }
d| |
j        vr#d	}|d
k    r<|dk    r	d|dz  z  }ni|dk    r	d|dz  z  }nZ|dk    r|dz  |z  dz  dz  dz  |dz  z  }n<|dk    r$|dk    r	d|dz  z  }n'|dk    s|dk    rd|dz  z  }nt          d|           |j        |	dfxx         |
d|                              d	          |z  z  cc<   ڐ'4|S )N        min_timeindexcolumnsarchnum_sms
clock_rate	device_idflopsr   CUDA80g  2C   89g bB90r   g    x:Ai     mBHIPgfx90ag  //bBgfx941gfx942g ?y"CzUnsupported device type: )	pd	DataFramer+   r   flops_widthr,   
ValueErrorlocfillna)dfr$   min_time_flopsdevice_typedevice_indexr-   r.   r/   widthidxdevice_frames	max_flopss               r   get_min_time_flopsrK   &   s   \#RX
|LLLN" l l'4 	l 	lL{+L9&AD!+.|<YGG$[1,?MJ#/ l lo5 "3"5??-*???	&((t||$*eai$8		%1eai$@		&-mj&@J&ORV&VZ^%^chklcl$m	 E))x''$*eai$8		))TX-=-=$-$;	$%N%N%NOOO"3
?333}_U__7U7\7\]^7_7_bk7kk3333-l		l6 r   c                 4   t          j        d| j        dg          }|D ]w}||         D ]l}| d         |k    }| |         }||         |         d         }||         |         d         }d|z  |z  dz  d	z  }	|j        |dfxx         |d
         |	z  z  cc<   mx|S )Nr(   r)   r*   r0   memory_clock_rate	bus_width   g     @@r4   bytes)r=   r>   r+   rA   )
rC   r$   min_time_bytesrE   rF   rH   rI   rM   rN   peak_bandwidths
             r   get_min_time_bytesrS   G   s    \#RX
|LLLN" [ ['4 	[ 	[L[/\1CsGM +K 8 FGZ [#K0>{KI]->>DqHNsJ///=3IN3ZZ////	[ r   
FactorDictnamefactortimer   gMbP?gư>g&.>)ztime/sztime/msztime/usztime/nsavg_timec                      i | ]\  }}d | |S )avg_ ).0keyvalues      r   
<dictcomp>r_   V   s$    .u.u.uzsE|c||U.u.u.ur   rP   g    eAr8   )zbyte/szgbyte/sztbyte/sc                     i | ]
}|t           S r[   )bytes_factor_dictr\   r]   s     r   r_   r_   Z   s+     3 3 3  3 3 3r   zflop/szgflop/sztflop/sc                 :    i | ]}|t          d t                    S )r1   )rT   default_flop_factor_dictrb   s     r   r_   r_   a   s5     1 1 1	 	*W6
7
7 1 1 1r   r1   flopz/sgfloptflopc                 D    i | ]}|t          t          t                    S r[   )rT   factor_namefactor_dictrb   s     r   r_   r_   f   s$    fffCc:k;#G#Gfffr   c                    g }| j         d                                         }fd}|D ]@}|dk    rt          | j         |          }t          | j         |          }	 || j                   }
|	d                             |d         t
                    |
z  | j         d<   t          j        | j         j        |df<   |	                    d           |t          v r~t          |         }|j        }|j        }t          |g          d         }| j         |          || j                   z  ||         z  | j         | d<   |	                    | d           -|t          j        v rst          j        dz   |                    d          d	         z   } || j                   t          j        |         z  | j         | d<   |	                    | d           |t           j        v rt           j        dz   |                    d          d	         z   } || j                   | j         d
         z  t           j        |         z  | j         | d<   t          j        | j         j        || df<   |	                    | d           [|                    d          }|d         }t#          |          d	k    r|d	         }|dk    rt%          d|           t          |g          d         }| j         |         }| j         |         j        d         }||z  dz  | j         | d<   |	                    | d           t          |g          d         }|	                    |           B|S )Nr0   c                    t          t          j        g          d         }t          j        dz   |                    d          d                             d          d         z   }| |         t          j        |         z  S )Nr   /r   r   ))r   time_factor_dictrU   r   rV   )rC   time_metric_name	time_unitr   s      r   get_time_secondsz(derive_metrics.<locals>.get_time_secondsm   su    24D4I3JKXXYZ[%*S03C3I3I#3N3Nq3Q3W3WX[3\3\]^3__	"#&6&=i&HHHr   utilr)   z
util (inc)r   r	   rm   r   count%zUnsupported unit g      Y@z/% (inc))	dataframeisnarS   rK   combinemaxnpnanrA   r   derivable_metricsrU   rV   r   ro   r   avg_time_factor_dictr   r@   iloc)r%   r   r   r$   derived_metricsinternal_frame_indicesrr   r   rQ   rD   time_secderivable_metricmetric_namemetric_factor_dictmatched_metric_namemetric_time_unitmetric_name_and_unitmetric_unitsingle_frametotals     `                 r   derive_metricsr   i   s   O\+6;;==I I I I I
  )< )<V/kJJN/kJJN''55H)7
)C)K)KN[eLfhk)l)low)wBL&EGVBL3\AB""<0000(((08*/K!1!8"9;-"U"UVW"X/1|<O/PTdTdegeqTrTr/s/A&/I0JBLF***+""f#4#4#45555'.../4s:V\\#=N=Nq=QQ/?/?/M/M/?/FGW/X0YBLF***+""f#4#4#45555+222383>cARARSTAUU/?/?/M/MPRP\]dPe/e/C/JK[/\0]BLF***+JL&BL35F5F5FFG""f#4#4#45555#)<<#4#4 .q1K'((1,,215#%%$%F%F%FGGG&={m[&Y&YZ[&\#!|,?@%89>qA:F:NRW9W5556&&+'?'?'?@@@@&={m[&Y&YZ[&\#&&':;;;;r   c                 $   |dk    r*| j         d                             d           | j         d<   n_|dk    r*| j         d                             d           | j         d<   n/|dk    r)| j         d                             d           | j         d<   | S )Nfile_function_linerU   c                 8    |                      d          d         S )Nrm   r   xs    r   <lambda>zformat_frames.<locals>.<lambda>       AGGCLLQSDT r   function_linec                 8    |                      d          d         S )N:r   r   r   s    r   r   zformat_frames.<locals>.<lambda>   r   r   file_functionc                 j    |                      d          d                              d          d         S )Nrm   r   @r   r   r   s    r   r   zformat_frames.<locals>.<lambda>   s+    AGGCLLQSDTDZDZ[^D_D_`aDb r   )rv   apply)r%   formats     r   format_framesr      s    %%%!|F399:T:TUUV	?	"	"!|F399:T:TUUV	?	"	"!|F399:b:bccVIr   c                 .   |rd| d}|                      |d          } |r,d| d}t          |          }|                      |d          } ddt           dig}|                      |d          } |r d	|d
| ig}|                      |d          } | S )Nz1
MATCH ("*")->(".", p)->("*")
WHERE p."name" =~ "z"
T)squashz*
MATCH (".", p)->("*")
WHERE p."name" =~ "rU   z^(?!z).**z>= )filterr   r   )r%   includeexclude	thresholdr   queryinclusion_querys          r   filter_framesr      s     +   YYuTY** +   o..YYuTY**=8===>?E	5	&	&B +v0Y0012YYuTY**Ir   d   c           	         t          |d          5 }t          |          \  }}	}
t          ||          }t          |	          dk    s
J d            |                                 t          || |	|
          } t          ||||| d                   }t          |                    | d|d                     t          ||            d d d            d S # 1 swxY w Y   d S )Nrr   z"No metrics found in the input fileTF)metric_columnexpand_namedepthrender_header)
openr&   r   r   update_inclusive_columnsr   r   printtreeemit_warnings)r   filenamer   r   r   r   r   fr%   r   r$   s              r   parser      s)   	h		 	#'6q'9'9$K2v&&;!###%I###
##%%% Wk;GG2wGAJGGbggGUZ_g``aaab'"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   B6CCCc                     d|v r=| j         d         j        }t          j        |          }|dk     rt	          d           d S d S d S )Nzbytes (inc)r   zZWarning: Negative byte values detected, this is usually the result of a datatype overflow
)rv   valuesrz   nanminr   )r%   r   byte_valuesmin_byte_values       r   r   r      s[    l=18;//Aoppppp	   r   c                 L   t          | d          5 }t          |          \  }}}t          d           |rV|D ]S}|                    d          d                                                                         }t          d|            Td d d            d S # 1 swxY w Y   d S )Nr   zAvailable metrics:r   r   z- )r   r&   r   r   r   r
   )	file_namer   _r   r   r   s         r   show_metricsr      s    	i		 1+A..;"### 	1) 1 1
%/%5%5c%:%:1%=%C%C%E%E%K%K%M%M"/-//00001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1s   A;BB Bc            	      ~   t          j        dt           j                  } |                     dddd           |                     dd	t          d d
           |                     ddt          d d           |                     ddt          d d           |                     ddt
          d d           |                     ddt          dd           |                     ddt          g ddd           |                                 \  }}t          |          dk    s
J d             |d!         }|j	        r|j	        
                    d"          nd }|j        }|j        }|j        }|j        }|j        }	|r|rt!          d#          |j        rt%          |           d S |rt'          |||||||	           d S 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
- avg_time/s, avg_time/ms, avg_time/us, avg_time/ns: time / count
- flop[<8/16/32/64>]/s, gflop[<8/16/32/64>]/s, tflop[<8/16/32/64>]/s: flops / time
- byte/s, gbyte/s, tbyte/s: bytes / time
- util: max(sum(flops<width>) / peak_flops<width>_time, sum(bytes) / peak_bandwidth_time)
- <metric>/%%: frame(metric) / sum(metric). Only availble for inclusive metrics (e.g. 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defaultr   z-iz	--includea  Find frames that match the given regular expression and return all nodes in the paths that pass through the matching frames.
For example, the following command will display all paths that contain frames that contains "test":
```
proton-viewer -i ".*test.*" path/to/file.json
```
z-ez	--excludezExclude frames that match the given regular expression and their children.
For example, the following command will exclude all paths that contain frames that contains "test":
```
proton-viewer -e ".*test.*" path/to/file.json
```
z-tz--thresholdzrExclude frames(kernels) whose metrics are below the given threshold. This filter only applies on the first metric.z-dz--depthr   z The depth of the tree to displayz-fz--format)fullr   r   r   r   a!  Formatting the frame name.
- full: include the path, file name, function name and line number.
- file_function_line: include the file name, function name and line number.
- function_line: include the function name and line number.
- file_function: include the file name and function name.
)r   choicesr   r   r   zMust specify a file to readr   ,z'Cannot specify both include and exclude)argparseArgumentParserRawTextHelpFormatteradd_argumentstrfloatintparse_known_argsr   r   r   r   r   r   r   r   r@   listr   r   )
	argparserargstarget_argsr   r   r   r   r   r   r   s
             r   mainr      sm   'B 5  I 	       
 
 
 	          |     /     js,l,l,l      "2244D+{q   "?   AI)-?dl  %%%4GlGlGIJE[F D7 DBCCCy NY	 Ngy'7IufMMMMMN Nr   __main__)NNNN)NNNr   N)*r   collectionsr   r   pandasr=   hatchetr   hatchet.queryr   ImportErrornumpyrz   triton.profiler.hookr   r   r   r&   rK   rS   rT   ro   rV   itemsr}   ra   keysr|   rd   updater?   rG   ri   rj   r   r   r   r   r   r   r   __name__r[   r   r   <module>r      s,    " " " " " "     j+++++++ j j j
+h
i
iij     H H H H H H H H  "5 5 5  B
 
 
 Zvx&899
:ftPTae&f&fgg !z*.u.uUeUlUrUrUtUt.u.u.uvv Jw1QU(V(VWW 3 3#*//113 3 3  &q*c:tL    1 1(--//1 1 12 2 2 # h hE!%//K#%###Q(9(9(9(93@Q@Q@Q@QSWXKffS^ScScSeSefffgggg3 3 3l     .
# 
# 
# 
#q q q1 1 1^N ^N ^NB zDFFFFF s   
 0