
    -`iL              #          U d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ ddlmZ 	 d dlmZ d dlZd dlZ n?# e!$ r7  ed          "                    d          Z ed          Z ed          ZY nw xY we G d de                      Z#e G d de#                      Z$e G d de#                      Z%e G d de#                      Z&e G d de#                      Z'e G d de#                      Z(e G d de#                      Z)e$e%e'e)e&e(d Z*e+e,e-e#         f         e.d!<    G d" d#e/e#                   Z0e G d$ d%                      Z1d&e1iZ2e+e,e-e1         f         e.d'<    G d( d)e/e1                   Z3d*ed+e/e+e,e4f                  fd,Z5d-e/e+e,e4f                  d+e/e+e,e4f                  fd.Z6d/e+e,e4f         d0e,fd1Z7d/e+e,e4f         d2e/e,         fd3Z8d4ed5e9e9e,e,f         d6f         d7e,fd8Z: G d9 d:          Z;d4ed;e9e9e9e,e,f         d6f         e/e+e,e4f                  f         d<e/e,         d=e/e,         d>e/e,         d?e,d@e,dAe0dBe3dCe,dz  dDe,dz  dEe<d7e,dFe<dGe=dHe>f dIZ?dJdKdLdMdNdOed4edPe/e,         d<e/e,         d=e/e,         d>e/e,         d?e,d@e,dAe0dBe3dCe,dz  dDe,dz  dEe<d7e,dFe<dGe=dHe>f"dQZ@e G dR dS                      ZAdTeAfdUZBdTe jC        fdVZDeEdWk    rJ e jF        eAjG        X          ZHeAI                    eH            eDeHJ                                           dS dS )Y    N)ABCabstractmethod)ProcessPoolExecutor)	dataclass)partial)Path)TracebackType)ClassVar)Selfoverride)full_groupby)PlaceholderModule   )sanitize_filename
matplotlibpyplotpandasseabornc                   X    e Zd ZU eed<   eed<   edefd            Zed
d            Zd	S )PlotFilterBasevartargetsc                 >   t           D ]o}||v ri|                    |          \  }}t          |         ||                    |                              d                              d                    c S pt	          d| dt          t                                )N'"z"Invalid operator for plot filter ''. Valid operators are: )PLOT_FILTERSsplitremoveprefixstrip
ValueErrorsortedclsr   op_keykeyvalues        n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/benchmarks/sweep/plot.py	parse_strzPlotFilterBase.parse_str#   s    " 	 	F{{WWV__
U#F+&&v..44S99??DD      ?Q ? ?(.|(<(<? ?      dfpd.DataFramereturnc                     t           )z#Applies this filter to a DataFrame.)NotImplementedErrorselfr,   s     r)   applyzPlotFilterBase.apply2   s
     "!r+   Nr,   r-   r.   r-   )	__name__
__module____qualname__str__annotations__classmethodr*   r   r3    r+   r)   r   r      sm         	HHHKKK#    [ " " " ^" " "r+   r   c                   &    e Zd Zedd            ZdS )PlotEqualTor,   r-   r.   c                     	 t          | j                  }n# t          $ r
 | j        }Y nw xY w||| j                 |k             S Nfloatr   r"   r   r2   r,   r   s      r)   r3   zPlotEqualTo.apply:   V    	!4;''FF 	! 	! 	![FFF	! "TX,&())    ++Nr4   r5   r6   r7   r   r3   r;   r+   r)   r=   r=   8   2        * * * X* * *r+   r=   c                   &    e Zd Zedd            ZdS )PlotNotEqualTor,   r-   r.   c                     	 t          | j                  }n# t          $ r
 | j        }Y nw xY w||| j                 |k             S r?   r@   rB   s      r)   r3   zPlotNotEqualTo.applyF   rC   rD   Nr4   rE   r;   r+   r)   rH   rH   D   rF   r+   rH   c                   &    e Zd Zedd            ZdS )PlotLessThanr,   r-   r.   c                 T    ||| j                  t          | j                  k              S r?   r   rA   r   r1   s     r)   r3   zPlotLessThan.applyR   "    "TX,t{!3!3344r+   Nr4   rE   r;   r+   r)   rK   rK   P   2        5 5 5 X5 5 5r+   rK   c                   &    e Zd Zedd            ZdS )PlotLessThanOrEqualTor,   r-   r.   c                 T    ||| j                  t          | j                  k             S r?   rM   r1   s     r)   r3   zPlotLessThanOrEqualTo.applyY   "    "TX,%"4"4455r+   Nr4   rE   r;   r+   r)   rQ   rQ   W   2        6 6 6 X6 6 6r+   rQ   c                   &    e Zd Zedd            ZdS )PlotGreaterThanr,   r-   r.   c                 T    ||| j                  t          | j                  k             S r?   rM   r1   s     r)   r3   zPlotGreaterThan.apply`   rN   r+   Nr4   rE   r;   r+   r)   rV   rV   ^   rO   r+   rV   c                   &    e Zd Zedd            ZdS )PlotGreaterThanOrEqualTor,   r-   r.   c                 T    ||| j                  t          | j                  k             S r?   rM   r1   s     r)   r3   zPlotGreaterThanOrEqualTo.applyg   rS   r+   Nr4   rE   r;   r+   r)   rY   rY   e   rT   r+   rY   )z==z!=z<=z>=<>r   c                   2    e Zd Zedefd            ZddZdS )	PlotFiltersr   c                 j    |s
 |             S  | d |                     d          D                       S )Nc              3   J   K   | ]}t                               |          V  d S r?   )r   r*   .0es     r)   	<genexpr>z(PlotFilters.parse_str.<locals>.<genexpr>}   s0      EE1>++A..EEEEEEr+   ,r   r%   r   s     r)   r*   zPlotFilters.parse_strx   s>     	355LsEEEEEEEEr+   r,   r-   r.   c                 :    | D ]}|                     |          }|S r?   r3   r2   r,   items      r)   r3   zPlotFilters.apply   '     	  	 DBBB	r+   Nr4   r5   r6   r7   r:   r8   r*   r3   r;   r+   r)   r^   r^   w   sR        F# F F F [F     r+   r^   c                   H    e Zd ZU eed<   eed<   edefd            Zd
dZd	S )
PlotBinnerr   bin_sizer   c           	         t           D ]V}||v rP|                    |          \  }}t          |         |t          |                    |                              c S Wt	          d| dt          t                                )Nz"Invalid operator for plot binner 'r   )PLOT_BINNERSr   rA   r    r"   r#   r$   s        r)   r*   zPlotBinner.parse_str   s    " 	 	F{{WWV__
U#F+Cu7I7I&7Q7Q1R1RSSSSS  ?Q ? ?(.|(<(<? ?  r+   r,   r-   r.   c                 x    |                                 }|| j                 | j        z  | j        z  || j        <   |S )z#Applies this binner to a DataFrame.)copyr   rp   r1   s     r)   r3   zPlotBinner.apply   s3    WWYY$(|t}4t}D48	r+   Nr4   )	r5   r6   r7   r8   r9   rA   r:   r*   r3   r;   r+   r)   ro   ro      sa         	HHHOOO	# 	 	 	 [	     r+   ro   %rr   c                   2    e Zd Zedefd            ZddZdS )	PlotBinnersr   c                 j    |s
 |             S  | d |                     d          D                       S )Nc              3   J   K   | ]}t                               |          V  d S r?   )ro   r*   ra   s     r)   rd   z(PlotBinners.parse_str.<locals>.<genexpr>   s0      AAq:''**AAAAAAr+   re   rf   rg   s     r)   r*   zPlotBinners.parse_str   s>     	355LsAAAGGCLLAAAAAAr+   r,   r-   r.   c                 :    | D ]}|                     |          }|S r?   ri   rj   s      r)   r3   zPlotBinners.apply   rl   r+   Nr4   rm   r;   r+   r)   rw   rw      sR        B# B B B [B     r+   rw   pathr.   c                     |                      d          5 }t          j        |          cd d d            S # 1 swxY w Y   d S )Nrb)openjsonload)r{   fs     r)   _json_load_bytesr      s~    	4 Ay||                 s   7;;datac                     g }| D ]j}i }|                                 D ]<\  }}t          |t                    r|dv rt          |          ||<   1|||<   7|||<   =|                    |           k|S )z
    Convert string values "inf", "-inf", and "nan" to their float equivalents.

    This handles the case where JSON serialization represents inf/nan as strings.
    )infz-infnan)items
isinstancer8   rA   append)r   converted_datarecordconverted_recordr'   r(   s         r)   _convert_inf_nan_stringsr      s     N 
0 
0 ,,.. 	. 	.JC%%% .222,1%LL$S)),1$S))(- %%.////r+   run_data
metric_keyc                 d    	 | |         S # t           $ r}t          d|d|           |d }~ww xY w)NzCannot find metric z in run_data=)KeyErrorr"   )r   r   excs      r)   _get_metricr      sX    W
## W W WLzLLLLMMSVVWs   
 
/*/
group_keysc                 :     t           fd|D                       S )Nc              3   V   K   | ]#}|t          t          |                    fV  $d S r?   )r8   r   )rb   kr   s     r)   rd   z_get_group.<locals>.<genexpr>   s:      HH!SXq11223HHHHHHr+   )tuple)r   r   s   ` r)   
_get_groupr      s&    HHHHZHHHHHHr+   fig_dirgroup.fig_namec                     t          t                               }|                    |           |r|                    d |D                        | t	          d                    |          dz             z  S )Nc              3   *   K   | ]\  }}| d | V  dS )=Nr;   )rb   r   vs      r)   rd   z _get_fig_path.<locals>.<genexpr>   s0      22DAqZZAZZ222222r+   -z.png)listr8   r   extendr   join)r   r   r   partss       r)   _get_fig_pathr      sq    IKKE 
LL  322E222222&sxx'?@@@@r+   c                   T    e Zd ZeZdefdZdee         dz  dedz  dedz  ddfdZ	dS )DummyExecutorr.   c                     | S r?   r;   )r2   s    r)   	__enter__zDummyExecutor.__enter__   s    r+   exc_typeN	exc_valueexc_tracebackc                     d S r?   r;   )r2   r   r   r   s       r)   __exit__zDummyExecutor.__exit__   s	     tr+   )
r5   r6   r7   mapr   r   typeBaseExceptionr	   r   r;   r+   r)   r   r      s        
C4    }%, !4' %t+	
 
     r+   r   fig_group_datarow_bycol_bycurve_byvar_xvar_y	filter_bybin_byscale_xscale_ydry_run
error_bars
fig_heightfig_dpic          
      6	   |\  }}t          |fd          }t          |          }t          fd|D                       }t          | ||          }t	          d           t	          dt          |                      t	          d| d| d           t	          d	|            |rt	          d
           d S t          |          }t          j        	                    |          |j
        vr,t          d|dj
                                                   |j
        vr,t          d|dj
                                                   D ]7}|j
        vr,t          d|dj
                                                   8D ]7}|j
        vr,t          d|dj
                                                   8|D ]7}|j
        vr,t          d|dj
                                                   8|                              |                              |r                    |          r<t          j        fdD             d                              dj        d          ndd<   r<t          j        fdD             d                              dj        d          ndd<   t%          j        dd|          }rr|                    d           nEr|                    d           n-r|                    d           n|                    d           |	r|                    |	           |
r|                    |
            t          |          d!k    rQg |d d d R ^}}}}|                    t$          j        |||||d"|rd#nd $           |                    |%           n|r<t          j        fd&|D             d                              dj        d          ndd'<   |                    t$          j        ||d'd"|rd#nd (           |                                 |                    ||)           t5          j        |j                   t	          d
           d S )*Nc                 $    t          |           S r?   r   )rk   r   s    r)   <lambda>z_plot_fig.<locals>.<lambda>
      D&11 r+   r'   c              3   `   K   | ](\  }}t          t          |fd                     V  )dS )c                 $    t          |           S r?   r   )rk   r   s    r)   r   z%_plot_fig.<locals>.<genexpr>.<lambda>  s    JtV4L4L r+   r   N)lenr   )rb   _row_datar   s      r)   rd   z_plot_fig.<locals>.<genexpr>  sZ        Ax 	L'L'L'L'LMMMNN     r+   z[BEGIN FIGURE]zGroup: zGrid: z rows x z colszOutput file: z[END FIGURE]zCannot find var_x=z2 in parameter sweep results. Available variables: zCannot find var_y=zCannot find row_by=zCannot find col_by=zCannot find curve_by=)byc                 \    g | ](}|d z   |                              t                    z   )S r   astyper8   rb   r   r,   s     r)   
<listcomp>z_plot_fig.<locals>.<listcomp>G  2    999QQWr!u||C(((999r+   r   )axis
z(All)	row_groupc                 \    g | ](}|d z   |                              t                    z   )S r   r   r   s     r)   r   z_plot_fig.<locals>.<listcomp>P  r   r+   	col_group)rowcolheightz{row_name}
{col_name}z
{row_name}z
{col_name} )xscale)yscale   Tsd)xyhuestylesizemarkerserrorbar)titlec                 \    g | ](}|d z   |                              t                    z   )S r   r   r   s     r)   r   z_plot_fig.<locals>.<listcomp>y  s2    ???S2a5<<,,,???r+   curve_group)r   r   r   r   r   )dpi)r   r   maxr   printdictr   pd	DataFramefrom_recordscolumnsr"   tolistr3   sort_valuesconcataggr   sns	FacetGrid
set_titlessetmap_dataframelineplot
add_legendsavefigpltclosefigure)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	fig_groupfig_data
row_groupsnum_rowsnum_colsfig_pathr   gr   r   r   r   r,   s     ``                        @r)   	_plot_figr	     s   & )Ix1111  J :H    %    H
 Wi::H	
	
%DOO
%
%&&&	
48
4
4X
4
4
4555	
$(
$
$%%% n (11H		"	"8	,	,BBJ:5 : :$&J$5$5$7$7: :
 
 	
 BJ:5 : :$&J$5$5$7$7: :
 
 	
   BJ>a > >(*
(9(9(;(;> >   
   BJ>a > >(*
(9(9(;(;> >   
   BJ> > >(*
(9(9(;(;> >    
		B	b		B  )^^x^(( 			9999&999	
 	
 	
 #dia#
 
 
  {O 			9999&999	
 	
 	
 #dia#
 
 
  {O 	bk{:NNNA & 	-....	 	\""""	 	\""""	R 	W 	W
8}}<<4<<t<<UD1	L'1TTT 	 		
 		
 		
 	
3 	BI????h???   c$)!c$$$ 	= 	
L'1TTT 	 	
 	
 	
 	
IIhGI$$$Iah	.r+   FIGURET皙@,  )r   r   r   r   
output_dirfig_byc                   d |                      d          D             }|st          d|            |                    dd           t          |fd          }t	          |          dk    rt                      nt                      5 }t          |                    t          t          ||||||||	|
||||||	          |                     d d d            d S # 1 swxY w Y   d S )
Nc                 6    g | ]}t          |          D ]}|S r;   )r   )rb   r{   r   s      r)   r   zplot.<locals>.<listcomp>  sG       (..   	   r+   z**/summary.jsonz/Did not find any parameter sweep results under T)parentsexist_okc                 $    t          |           S r?   r   )rk   r  s    r)   r   zplot.<locals>.<lambda>  r   r+   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rglobr"   mkdirr   r   r   r   allr   r   r	  )r  r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   all_data
fig_groupsexecutors     `                 r)   plotr    s}   ( $$%677  H  YW:WWXXXMM$M...1111  J
  
OOq006I6K6K 
xLL!!%'!###%))#!  $ ' 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   AC""C&)C&c                      e Zd ZU eed<   eed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   eed<   eed<   eed	<   eed
<   edz  ed<   edz  ed<   e	ed<   dZ
eed<   dZe	ed<   dZeed<   dZeed<   dZee         ed<   dZee         ed<   edej        fd            Zedej        dej        fd            ZdS ) SweepPlotArgsr  r   r  r   r   r   r   r   r   r   Nr   r   r   r
  r   Tr   r  r   r  r   r  parser_namez5Plot performance curves from parameter sweep results.parser_helpargsc                    t          |j                  }|                                st          d|           |j        sg n|j                            d          }|j        sg n|j                            d          }|j        sg n|j                            d          }|j        sg n|j                            d          } | di d|d||j	        z  d|d|d|d|d	|j
        d
|j        dt                              |j                  dt                              |j                  d|j        d|j        d|j        d|j        d|j         d|j        d|j        S )Nz!No parameter sweep results under re   r  r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   )r   
OUTPUT_DIRexistsr"   r   r   r   r   r  r   r   r   r^   r*   r   rw   r   r   r   r   r   no_error_barsr   r   )r%   r  r  r   r   r   r  s          r)   from_cli_argszSweepPlotArgs.from_cli_args  s   $/**
  "" 	OMMMNNN!]H220C0CC0H0H;BDK,=,=c,B,B;BDK,=,=c,B,B;BDK,=,=c,B,Bs 
 
 
!z
--
 6
 6	

 6
 X
 **
 **
 "++DN;;;
 ((555
 LL
 LL
 LL
 ]]
  ---
  !
" LL#
 	
r+   parserr.   c                    |                     dt          dd           |                     dt          dd           |                     dt          dd	           |                     d
t          dd           |                     dt          dd           |                     dt          d d           |                     dt          dd           |                     dt          dd           |                     dt          dd           |                     dt          dd           |                     dt          d d           |                     dt          d d           |                     dt          dd            |                     d!d"d#$           |                     d%t          d&d'           |                     d(t          d)d*           |                     d+d"d,$           |S )-Nr!  resultsznThe directory containing the results to plot, i.e., the `--output-dir` argument to the parameter sweep script.)r   defaulthelpz	--fig-dirr   zdThe directory to save the figures, relative to `OUTPUT_DIR`. By default, the same directory is used.z--fig-byztA comma-separated list of variables, such that a separate figure is created for each combination of these variables.z--row-byzqA comma-separated list of variables, such that a separate row is created for each combination of these variables.z--col-byztA comma-separated list of variables, such that a separate column is created for each combination of these variables.z
--curve-byzsA comma-separated list of variables, such that a separate curve is created for each combination of these variables.z--var-xrequest_throughputzThe variable for the x-axis.z--var-yp99_e2el_mszThe variable for the y-axisz--filter-bya  A comma-separated list of statements indicating values to filter by. This is useful to remove outliers. Example: `max_concurrency<1000,max_num_batched_tokens<=4096` means plot only the points where `max_concurrency` is less than 1000 and `max_num_batched_tokens` is no greater than 4096.z--bin-byzA comma-separated list of statements indicating values to bin by. This is useful to avoid plotting points that are too close together. Example: `request_throughput%%1` means use a bin size of 1 for the `request_throughput` variable.z	--scale-xzThe scale to use for the x-axis. Currently only accepts string values such as 'log' and 'sqrt'. See also: https://seaborn.pydata.org/generated/seaborn.objects.Plot.scale.htmlz	--scale-yzThe scale to use for the y-axis. Currently only accepts string values such as 'log' and 'sqrt'. See also: https://seaborn.pydata.org/generated/seaborn.objects.Plot.scale.htmlz
--fig-namer
  zName prefix for the output figure file. Group data is always appended when present. Default: 'FIGURE'. Example: --fig-name my_performance_plotz--no-error-bars
store_truezJIf set, disables error bars on the plot. By default, error bars are shown.)actionr)  z--fig-heightr  z.Height of each subplot in inches. Default: 6.4z	--fig-dpir  z>Resolution of the output figure in dots per inch. Default: 300z	--dry-runzZIf set, prints the information about each figure to plot, then exits without drawing them.)add_argumentr8   rA   int)r%   r%  s     r)   add_cli_argszSweepPlotArgs.add_cli_args  s    O	 	 	
 	
 	
 	6	 	 	
 	
 	
 	B	 	 	
 	
 	
 	B	 	 	
 	
 	
 	B	 	 	
 	
 	
 	B	 	 	
 	
 	
 	(/	 	 	
 	
 	
 	!.	 	 	
 	
 	
 	@	 	 		
 		
 		
 	I	 	 	
 	
 	
 	]	 	 	
 	
 	
 	]	 	 	
 	
 	
 	I	 	 	
 	
 	
 	0 	 	
 	
 	
 	A	 	 	
 	
 	
 	Q	 	 	
 	
 	
 	/ 	 	
 	
 	
 r+   )r5   r6   r7   r   r9   r   r8   r^   rw   boolr   r   r   rA   r   r/  r  r
   r  r:   argparse	Namespacer$  ArgumentParserr0  r;   r+   r)   r  r    s        MMMIII3iJJJJJJ4Z4ZMMMHcJJGS!'K#'''!XK#XXX
!3 
 
 
 [
< {("9 {h>U { { { [{ { {r+   r  r  c                 &   t          di d| j        d| j        d| j        d| j        d| j        d| j        d| j        d| j        d	| j	        d
| j
        d| j        d| j        d| j        d| j        d| j        d| j        d| j        S )Nr  r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   )r  r  r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  s    r)   run_mainr7    s      ?? {{ {{	
 {{  jj jj .. {{     ??  ??!" # r+   c                 T    t          t                              |                      d S r?   )r7  r  r$  r6  s    r)   mainr9    s$    ]((../////r+   __main__)description)Kr2  r   abcr   r   concurrent.futuresr   dataclassesr   	functoolsr   pathlibr   typesr	   typingr
   typing_extensionsr   r   vllm.utils.collection_utilsr   vllm.utils.import_utilsr   utilsr   matplotlib.pyplotr   r   r   r   r   r   ImportErrorplaceholder_attrr   r=   rH   rK   rQ   rV   rY   r   r   r8   r   r9   r   r^   ro   rr   rw   objectr   r   r   r   r   r   r   r1  rA   r/  r	  r  r  r7  r3  r9  r5   r4  r  r%  r0  
parse_argsr;   r+   r)   <module>rL     s     # # # # # # # # 2 2 2 2 2 2 ! ! ! ! ! !                         , , , , , , , , 4 4 4 4 4 4 5 5 5 5 5 5 $ $ $ $ $ $+###### + + +

L
)
)
:
:8
D
DC		8	$	$B	**GGG+ " " " " "S " " "2 * * * * *. * * * * * * * *^ * * * 5 5 5 5 5> 5 5 5 6 6 6 6 6N 6 6 6 5 5 5 5 5n 5 5 5 6 6 6 6 6~ 6 6 6 


"		1 1d3^,,-       $~&           0 -d3Z(()   
    $z"   4 Dc6k):$;    
4S&[(9#: tDfDU?V    *W$sF{+ W W W W WIc6k* IS	 I I I I
A4 
AeCHos.B(C 
As 
A 
A 
A 
A       [[%c3h 45tDf<M7NNO[ I[ I	[
 3i[ [ [ [ [ 4Z[ 4Z[ [ [ [  ![" #[ [ [ [Z %<
 <
 <
<
<
 I<
 I	<

 I<
 3i<
 <
 <
 <
 <
 4Z<
 4Z<
 <
 <
  !<
" #<
$ %<
 <
 <
 <
~ q q q q q q q qh=    ,0x! 0 0 0 0 z$X$1JKKKFv&&&D					 s   A   9BB