
    -`i*                     `   d dl Z d dl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 d	d
lmZ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deee f         de!e         fdZ"dddeee f         de!e         de#de$dz  fdZ%deee f         dedz  de$dz  fdZ&deee f         dedz  de$fdZ'deee f         dede$fdZ(de!eee f                  dedz  dedz  de)e!eee f                  e*f         fdZ+d d!d"d#d$ed%ed&e$dd#f
d'Z,d(e	d)e)e)eef         d*f         de	fd+Z-d(e	d,e)e)e)eef         d*f         e!eee f                  f         d-e!e         d.e#fd/Z.d0e	dedz  dedz  d-e!e         d.e#f
d1Z/e G d2 d3                      Z0d4e0fd5Z1d4e j2        fd6Z3e4d7k    rJ e j5        e0j6        8          Z7e08                    e7            e3e79                                           dS dS )9    N)ProcessPoolExecutor)	dataclass)partial)Path)ClassVar)full_groupby)PlaceholderModule   )DummyExecutor_json_load_bytes)sanitize_filename
matplotlibpyplotpandasseabornrun_datakeysc                     |D ]B}||                     dd          |                     dd          hD ]}|| v r| |         c c S Cd S )N_-)replace)r   r   key	candidates       u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/benchmarks/sweep/plot_pareto.py_first_presentr      sv     + +s{{344ckk#s6K6KL 	+ 	+IH$$	****** %	+ 4    T
allow_zeror   returnc          	          t          | |          }|d S 	 t          |          }n2# t          t          f$ r}t          d| d|d|           |d }~ww xY w|s|dk    rd S |S )Nz"Expected numeric value for one of z, but found z in run_data=r   )r   float	TypeError
ValueError)r   r   r   valuenumericexcs         r   _get_numericr'   #   s     8T**E}t,,z"   2 2 22 2&.2 2
 
 	  'Q,,tNs   & AAAuser_count_varc                     |r|gng }|                     dg           t          | |d          }||S t          | dgd          S )Nrequest_rateFr   max_concurrent_requests)extendr'   )r   r(   
candidates
user_counts       r   _infer_user_countr/   ;   sh     &4;.!!J~&'''h
uEEEJ #<"=%PPPPr   gpu_count_varc                     |r|gng }t          | |d          }|r|S t          | ddg          }t          | ddg          }t          | ddg          }d	}|r||z  }|r||z  }|r||z  }|S )
NFr   tensor_parallel_sizetppipeline_parallel_sizeppdata_parallel_sizedpg      ?)r'   )r   r0   direct_candidatesdirect_gpu_counttp_sizepp_sizedp_size
world_sizes           r   _infer_gpu_countr>   I   s     ,9@b#H.?ERRR  8&<d%CDDG8&>%EFFG8&:D%ABBGJ g
 g
 g
r   throughput_varc                 p    t          | |g          }|"t          d|dt          |                      |S )NzCannot find throughput metric z in run data. Available keys: )r'   r#   sorted)r   r?   
throughputs      r   _get_throughputrC   `   s\     h(899J2^ 2 2%h//2 2
 
 	

 r   all_datac          	          g }d}| D ]b}t          |d          }t          ||          }||dz  }*t          ||          }||z  }	||z  }
|                    i ||	|
||d           c||fS )Nr   output_throughputr
   )tokens_per_usertokens_per_gpuuser_count_estimate	gpu_count)rC   r/   r>   append)rD   r(   r0   preparedskipped_missing_usersrecordrB   r.   rJ   rG   rH   s              r   _prepare_recordsrO   n   s     H 
 
$V-@AA
&v~>>
!Q&!$V];;	$z1#i/#2"0'1&  	
 	
 	
 	
 ***r   g&.>)epsilondfzpd.DataFramex_coly_colrP   c                   |                      ||gddg          }g }t          j         }|                                D ];\  }}||         }	|	||z
  k    r%|                    |           t          ||	          }<| j        |         S )NF)	ascending)sort_valuesmathinfiterrowsrK   maxloc)
rQ   rR   rS   rP   	sorted_dffrontier_indicesbest_yidxrowy_vals
             r   _pareto_frontierrb      s     u~%HHIhYF&&(( ( (SE
FW$$$##C(((''F6"##r   fig_dir	fig_group.c                     dg}|r|                     d |D                        t          d                    |          dz             }| |z  S )NPARETOc              3   *   K   | ]\  }}| d | V  dS )=N ).0kvs      r   	<genexpr>z _get_fig_path.<locals>.<genexpr>   s0      66DAqZZAZZ666666r   r   z.png)r,   r   join)rc   rd   partsfilenames       r   _get_fig_pathrq      s[     JE 766I666666 %6!9::HXr   fig_group_datalabel_bydry_runc          	         |\  }}t          | |          }t          d           t          dt          |                      t          d|            |rt          d           d S t          j                            |          }|                    ddg          }|j        r t          d           t          d           d S t          |dd          }|	                    d          }t          j                    \  }	}
t          j        |ddd	d
|
d           t          j        |ddd|
d           |r|                                D ]j\  }}g }|D ]&}||v r |                    | d||                     '|r8|
                    |d         |d         d                    |          d           k|
                    d           |
                    d           |
                    dddd
           |	                                 |	                    |           t          j        |	           t          dt3          |           dt3          |           d           t          d           d S )Nz[BEGIN FIGURE]zGroup: zOutput file: z[END FIGURE]rG   rH   )subsetz3No data points available after filtering; skipping.z0.5g333333?zAll runs)dataxycoloralphaaxlabelozPareto frontier)rw   rx   ry   markerr|   r}   rh   
   )fontsizezTokens/s/userzTokens/s/GPUTz--g      ?)	linestyle	linewidthr{   zPlotted z points; Pareto frontier size: .)rq   printdictpd	DataFramefrom_recordsdropnaemptyrb   rV   pltsubplotssnsscatterplotlineplotrY   rK   textrn   
set_xlabel
set_ylabelgridtight_layoutsavefigcloselen)rc   rr   rs   rt   rd   fig_datafig_pathrQ   frontierfigr|   r   r`   label_partsr   s                  r   	_plot_figr      s    )IxWi00H	
	
%DOO
%
%&&&	
$(
$
$%%% n		"	"8	,	,B	,.>?	@	@B	x CDDDn$57GHHH##$566HlnnGCO

    L

     '')) 	 	FAsK < <#::&&#':':C':':;;; )*()IIk**	     MM/"""MM.!!!GGDDCsG;;;KKIcNNN	K3r77KK3x==KKK   
.r   
output_dirc                @   | dz  }d |                      d          D             }|st          d|            |                    dd           t          |||          \  }}|rt	          d| d	           |st          d
          t          |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 )Nparetoc                 6    g | ]}t          |          D ]}|S ri   )r   )rj   pathr   s      r   
<listcomp>zplot_pareto.<locals>.<listcomp>  sG       (..   	   r   z**/summary.jsonz/Did not find any parameter sweep results under T)parentsexist_ok)r(   r0   zSkipped zL runs without a user count (`max_concurrency` or `max_concurrent_requests`).zUNo data points with both throughput and user count available to plot Pareto frontier.c                     t                      S N)tuple)items    r   <lambda>zplot_pareto.<locals>.<lambda>!  s
     r   )r   r
   )rs   rt   )rglobr#   mkdirrO   r   r   r   r   r   allmapr   r   )r   r(   r0   rs   rt   rc   raw_dataprepared_datarM   
fig_groupsexecutors              r   plot_paretor      s    8#G $$%677  H  YW:WWXXXMM$M...+;%#, , ,(M(  
@, @ @ @	
 	
 	

  
'
 
 	

     J
  
OOq006I6K6K 
xLL%#	    
	
 
	
 
	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   :DDDc                       e Zd ZU eed<   edz  ed<   edz  ed<   ee         ed<   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        fd            ZdS )SweepPlotParetoArgsr   Nr(   r0   rs   rt   r   parser_namezYPlot Pareto frontier between tokens/s/user and tokens/s/GPU from parameter sweep results.parser_helpargsc                     t          |j                  }|                                st          d|           |j        sg n|j                            d          } | ||j        |j        ||j                  S )Nz!No parameter sweep results under ,r   r(   r0   rs   rt   )	r   
OUTPUT_DIRexistsr#   rs   splitr(   r0   rt   )clsr   r   rs   s       r   from_cli_argsz!SweepPlotParetoArgs.from_cli_args@  s    $/**
  "" 	OMMMNNN!]H220C0CC0H0Hs!.,L
 
 
 	
r   parserc                 &   |                     dt          dd           |                     dt          dd           |                     dt          d d	           |                     d
t          dd           |                     ddd           |S )Nr   resultsz3The directory containing the sweep results to plot.)typedefaulthelpz--user-count-varmax_concurrencyz_Result key that stores concurrent user count. Falls back to max_concurrent_requests if missing.z--gpu-count-varzResult key that stores GPU count. If not provided, falls back to num_gpus/gpu_count or tensor_parallel_size * pipeline_parallel_size.z
--label-byzmax_concurrency,gpu_countzEComma-separated list of fields to annotate on Pareto frontier points.z	--dry-run
store_truez8If set, prints the figures to plot without drawing them.)actionr   )add_argumentstr)r   r   s     r   add_cli_argsz SweepPlotParetoArgs.add_cli_argsP  s    F	 	 	
 	
 	
 	%@	 	 	
 	
 	
 	@	 	 	
 	
 	
 	/	 	 	
 	
 	
 	K 	 	
 	
 	
 r   )__name__
__module____qualname__r   __annotations__r   listboolr   r   r   classmethodargparse	Namespacer   ArgumentParserr   ri   r   r   r   r   2  s         $J:3iMMM!.K#...	( #   
 
!3 
 
 
 [
 #("9 # # # [# # #r   r   r   c                 \    t          | j        | j        | j        | j        | j                  S )Nr   )r   r   r(   r0   rs   rt   r   s    r   run_mainr   w  s4    ?*(   r   c                 T    t          t                              |                      d S r   )r   r   r   r   s    r   mainr     s%     ..t4455555r   __main__)description):r   rW   concurrent.futuresr   dataclassesr   	functoolsr   pathlibr   typingr   vllm.utils.collection_utilsr   vllm.utils.import_utilsr	   plotr   r   utilsr   matplotlib.pyplotr   r   r   r   r   r   ImportErrorplaceholder_attrr   r   objectr   r   r   r!   r'   r/   r>   rC   r   intrO   rb   rq   r   r   r   r   r   r   r   r   r   r   r   
parse_argsri   r   r   <module>r      s"     2 2 2 2 2 2 ! ! ! ! ! !                   4 4 4 4 4 4 5 5 5 5 5 5 1 1 1 1 1 1 1 1 $ $ $ $ $ $'###### ' ' '

L
)
)
:
:8
D
DC		8	$	$B

I
&
&CCC'T#v+. d3i     	  3;
s) 	
 T\   0Q3;Q$JQ T\Q Q Q Q3;:    .3;    +4V$%+ $J+ :	+
 4S&[!"C'(+ + + +L $ $ $$$ $
 $ $ $ $ $(U38_c)* 
   HH%c3h 45tDf<M7NNOH 3iH
 H H H HV6
6
$J6
 :6
 3i	6
 6
 6
 6
 6
r A A A A A A A AH&    6x! 6 6 6 6 z$X$1D1PQQQF$$V,,,D					 s   A 9BB