
    sg%                        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 Zd Zd Z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i ej&                  j)                         D  ci c]  } | e c} ej&                  j)                         D  ci c]  } | e c} Zd Zd Zd Zd Zedk(  r e        yyc c} w c c} w )    N)
namedtuple)COMPUTE_METADATA_SCOPE_NAME
TritonHookc                     g }| rl| D ]e  }|j                         }|D ]N  }|j                  d      d   j                         j                         }|||fv s:|j                  |dz           e g |S |d   gdz   }|S )N(r    (inc))lowersplitstripappend)metricsraw_metricsretmetric
raw_metricraw_metric_no_units         I/var/www/html/venv/lib/python3.12/site-packages/triton/profiler/viewer.pymatch_available_metricsr   
   s    
C 	F\\^F) 
%/%5%5c%:1%=%C%C%E%K%K%M"j*<==JJzH45		 J 1~)J    c                     t        j                  |       }|j                  d      }t        j                  j                  |      }||j                         |fS )N   )jsonloadpopht
GraphFramefrom_literalshow_metric_columns)filedatabasedevice_infogfs       r   get_raw_metricsr#      sF    yyH,,q/K		#	#H	-Br%%'44r   c           	      f   t        j                  d| j                  dg      }|D ]  }||   D ]  }||   |   d   }||   |   d   }||   |   d   }t        j                  D ]  }| d   |k(  }	| |	   }
d| |
j
                  vr"d	}|d
k(  r9|dk(  r	d|dz  z  }n_|dk(  r	d|dz  z  }nQ|dk(  rL|dz  |z  dz  dz  dz  |dz  z  }n4|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|    j                  d	      |z  z  cc<     
 |S )N        min_timeindexcolumnsarchnum_sms
clock_rateDeviceId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_flopsrH       s   \\#RXX
|LN" l'4 	lL{+L9&AD!+.|<YGG$[1,?MJ#// ln4 "35'?-*?*??	&(t|$*eai$8	%1eai$@	&-mj&@J&ORV&VZ^%^chklcl$m	 E)x'$*eai$8	)TX-=$-$;	$'@%NOO""3
?3}uUG_7U7\7\]^7_bk7kk3-l		ll8 r   c                    t        j                  d| j                  dg      }|D ]\  }||   D ]R  }| d   |k(  }| |   }||   |   d   }||   |   d   }d|z  |z  dz  d	z  }	|j                  |dfxx   |d
   |	z  z  cc<   T ^ |S )Nr%   r&   r'   r-   memory_clock_rate	bus_width   g     @@r1   bytes)r:   r;   r(   r>   )
r@   r!   min_time_bytesrB   rC   rE   rF   rJ   rK   peak_bandwidths
             r   get_min_time_bytesrP   A   s    \\#RXX
|LN" ['4 	[LZ.L0CsGM +K 8 FGZ [#K0>{KI]->>DqHNsJ/=3IN3ZZ/	[[ r   
FactorDictnamefactortimer   gMbP?gư>g&.>)time/sztime/msztime/usztime/nsr.   g    eAr5   )zflop/szgflop/sztflop/srM   )zbyte/szgbyte/sztbyte/sc                    g }g }t        t        j                  g|      d   }t        j                  dz   |j                  d      d   j                  d      d   z   }|D ]  }|dk(  rt	        | j
                  |      }	t        | j
                  |      }
| j
                  |   t        j                  |   t        j                  d   z  z  }|
d   j                  |	d   t              |z  | j
                  d	<   |j                  d	       |t        v rt        |   }|j                  }|j                  }t        |g|      d   }| j
                  |   | j
                  |   t        j                  |   z  z  ||   z  | j
                  | d
<   |j                  | d
       B|t        j                  v rt        j                  dz   |j                  d      d   z   }| j
                  |   t        j                  |   t        j                  |   z  z  | j
                  | d
<   |j                  | d
       |j                  |        |rt        ||      }||z   S )Nr   /r   r   )utilrU   r&   z
util (inc)r   )r   time_factor_dictrR   r
   rP   	dataframerH   rS   combinemaxr   derivable_metrics)r"   r   r   r!   derived_metricsoriginal_metricstime_metric_name	time_unitr   rN   rA   time_secderiveable_metricmetric_namemetric_factor_dictmatched_metric_namemetric_time_units                    r   derive_metricsri   [   sp   O.0@0E0E/FTUVW!&&,/?/E/Ec/J1/M/S/STW/XYZ/[[I ,V/kJN/kJN||$459I9P9PQZ9[9I9P9PQY9Z:[ \H)7
)C)K)KN[eLfhk)low)wBLL&""<0(( 1& 9+00K!2!9!9"9;-"UVW"X/1||<O/P02=M0NQaQhQhirQs0s0u/A&/I0JBLLF86*+ ""fXV#45'.../44s:V\\#=Nq=QQ.0ll;K.L ''	25E5L5LM]5^^/`BLLF86*+""fXV#45##F+/,2 23C[Q---r   c           	         t        |d      5 }t        |      \  }}}	t        |      dkD  sJ d       |j                          t	        || ||	      } |s|r#|rd| dn|}
dd|
ig}|j                  |d	      }ddt         dig}|j                  |d	      }|rd| d   d
| ig}|j                  |d	      }t        |j                  | d|d             d d d        y # 1 sw Y   y xY w)Nrr   z"No metrics found in the input filez^(?!z).**rR   T)squashz>= F)metric_columnexpand_namedepthrender_header)	openr#   lenupdate_inclusive_columnsri   filterr   printtree)r   filenameincludeexclude	thresholdrp   fr"   r   r!   name_filterquerys               r   parser   ~   s   	h	 b'6q'9$K;!#I%II#
##% Wk;Gg18D	-gK6;/0E5.BD!< =SABCYYuTY*71:YK'89:E5.BbggGUZ_g`a#b b bs   C	CC(c                 
   t        | d      5 }t        |      \  }}}t        d       |rE|D ]@  }|j                  d      d   j	                         j                         }t        d|        B 	 d d d        y # 1 sw Y   y xY w)Nrk   zAvailable metrics:r   r   z- )rr   r#   rv   r
   r   r	   )	file_namer|   _r   r   r   s         r   show_metricsr      s    	i	 +A.;"#) 1
%/%5%5c%:1%=%C%C%E%K%K%M"-./01 	  s   A#A99Bc                     t        j                  dt         j                        } | j                  dddd       | j                  dd	t        d d
       | j                  ddt        d d       | j                  ddt        d d       | j                  ddt
        d d       | j                  ddt        dd       | j                         \  }}t        |      dk(  sJ d       |d   }|j                  r|j                  j                  d      nd }|j                  }|j                  }|j                  }|j                  }|r|rt        d      |j                   rt#        |       y |rt%        ||||||       y y )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defaultr   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_argsrs   r   r
   ry   rz   r{   rp   r=   listr   r   )		argparserargstarget_argsr   r   ry   rz   r{   rp   s	            r   mainr      s   ''B 55I 	     
 N   N   |   /   "224D+{q ?"?? AI)-dll  %4GllGllGIJJE7BCCyyY	gy'7IuE 
r   __main__)r   collectionsr   r   pandasr:   hatchetr   triton.profiler.hookr   r   r   r#   rH   rP   rQ   rZ   flops_factor_dictbytes_factor_dictrS   keysr^   ri   r   r   r   __name__)keys   0r   <module>r      s#    "    H5B
 vx&89
ftPTae&fg w1QU(VW w1QU(VW #**//13  3 $**//13  3  .Fb*JFZ zF o33s   ;
C$
C