
    -`i5                     R   d dl Z d dlZ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mZ dd	lmZ dd
lmZ 	 d dlZn# e$ r  ed          ZY nw xY wej        dddee         dee         dedededefd            Zdeeef         dededefdZdedz  dee         dededededefdZ dededefd Z!d!ededz  fd"Z"ded#edefd$Z#dedz  dee         deded!ed%edefd&Z$dddee         dee         dee         ded'ed(eded%eded)ee%eef                  defd*Z&e G d+ d,                      Z'd-e'fd.Z(d-e j)        fd/Z*e+d0k    rJ e j,        e'j-        1          Z.e'/                    e.            e*e.0                                           dS dS )2    N)	dataclass)datetime)Path)ClassVar)PlaceholderModule   )ParameterSweepParameterSweepItem)ServerProcess)sanitize_filenamepandas,  )server_ready_timeout	serve_cmdafter_bench_cmdshow_stdoutserve_overridesdry_runr   c             #   n  K   |                     |           }t          d           t          d|            t          d|            |rd V  t          d           d S t          |||          5 }|                    |           |V  d d d            n# 1 swxY w Y   t          d           d S )Nz[BEGIN SERVER]zServer overrides: zServer command: z[END SERVER])r   )timeout)apply_to_cmdprintr   wait_until_ready)r   r   r   r   r   r   
server_cmdservers           o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/benchmarks/sweep/serve.py
run_serverr      s      !--i88J	
	
0
0
0111	
)Z
)
)*** 


n	z?	L	L	L PV(<===               
.s   4BB"Brun_databench_overrides
run_numberc                 d    || d<   |                      |           |                      |           | S )Nr    )update)r   r   r   r    s       r   _update_run_datar#   4   s7     (H\OOO$$$OOO$$$O    r   	bench_cmdoutput_pathc                <   g |                     |          ddddt          |j                  d|j        }t	          d           t	          d|            t	          d|            t	          d	|            t	          d
|            |                                rbt	          d           |                    d          5 }t          j        |          }	t          |	|||          cd d d            S # 1 swxY w Y   | %|st          d|           t	          d           d S |j                            dd           |                     |           |                                  |                    d          5 }t          j        |          }	d d d            n# 1 swxY w Y   t          |	|||          }	|                    d          5 }t          j        |	|d           d d d            n# 1 swxY w Y   t	          d           |	S )Nz--percentile-metricszttft,tpot,itl,e2elz--save-resultz--result-dirz--result-filenamez[BEGIN BENCHMARK]zBenchmark overrides: zRun Number: zBenchmark command: zOutput file: z!Found existing results. Skipping.rbzCannot find results at z[END BENCHMARK]T)parentsexist_okw   indent)r   strparentnamer   existsopenjsonloadr#   
ValueErrormkdirrun_subcommandafter_benchdump)
r   r%   r   r   r    r&   r   benchmark_cmdfr   s
             r   run_benchmarkr=   A   s6   			%	%i	0	0		 		 			
 		 	K	 		 		M 

	
3/
3
3444	
%
%
%&&&	
/
/
/000	
'+
'
'(((  
1222d## 	qy||H#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ~ 	FD{DDEEE   tTD999
-(((
			$		  19Q<<                               	 H 
		#		 )!	(Aa(((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) 

Os6   &C??DDF,,F03F0HH	H
output_dir
serve_comb
bench_combc                     t          t                               }|r|                    d|j        f           |r|                    d|j        f           | t	          d                    |                    z  S )NzSERVE-zBENCH--)listr/   extendr1   r   join)r>   r?   r@   partss       r   _get_comb_base_pathrG      sq    
 IKKE 2h
0111 2h
0111)#((5//::::r$   	base_pathc                 "    || dz  S | d| dz  S )Nzsummary.jsonzrun=z.json )rH   r    s     r   _get_comb_run_pathrK      s*    >))/j/////r$   bench_combsc                 ~    |D ]9}t          || |          }t          |d                                           s dS :dS )Nr    TF)rG   rK   r2   )r?   rL   r>   r@   rH   s        r   _comb_needs_serverrO      sX    
 "  
'
J
KK	!)===DDFF 	44	 5r$   num_runsc                   t          t          t          t          f                              }t	          |          D ]=}t          | ||||t          ||          |          }	|	|                    |	           >|rd S t          |d                               d          5 }
t          j
        ||
d           d d d            n# 1 swxY w Y   |S )N)r   r   r    r&   r   rN   r+   r,   r-   )rC   dictr/   objectranger=   rK   appendr3   r4   r:   )r   r%   r?   r@   rH   rP   r   	comb_datar    r   r<   s              r   run_combrW      s!    T#v+&'))IHoo ' '
 &&!*9jAA
 
 
 X&&& t	I$	7	7	7	<	<S	A	A *Q	)Qq))))* * * * * * * * * * * * * * * s   !CC	C	serve_paramsbench_paramslinksc                H   t          t          t          t          f                              }|D ]t	          ||          rt          | ||||
          nt          j                    5 }|D ]_t          fd|	D                       }|s!t          |          }t          |||||          }||                    |           `	 d d d            n# 1 swxY w Y   |rd S t          j                            |          }|                    |dz             |S )N)r   r   r   r   c              3   R   K   | ]!\  }}|v o|v o|         |         k    V  "d S NrJ   ).0	serve_key	bench_keyr@   r?   s      r   	<genexpr>zrun_combs.<locals>.<genexpr>   sh       ! ! -	9 + G!Z/G"9-I1FF! ! ! ! ! !r$   )r?   r@   rH   rP   r   zsummary.csv)rC   rR   r/   rS   rO   r   
contextlibnullcontextallrG   rW   rD   pd	DataFramefrom_recordsto_csv)r   r%   r   r   rX   rY   r>   rP   r   rZ   r   all_datar   
should_runrH   rV   combined_dfr@   r?   s                    @@r   	run_combsrl      s    Df%&((H" #/ #/
 "*lJGG	*J' *%9    '))"	/ * / /
  ! ! ! ! ! 16	! ! !  
 " /
J
SS	$))'%#  	 (OOI...-/"	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/ "	/H  t,++H55KzM1222s   +A#CC	"C	c                      e Zd ZU 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	ed<   eed	<   ed
z  ed<   ee
eef                  d
z  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        dej        fd            Zededee
eef                  fd            Zd
S )SweepServeArgsr   r%   r   r   rX   rY   r>   rP   r   Nresume	link_varsr   serveparser_namez2Run vLLM server benchmark under multiple settings.parser_helpargsc                 h   t          j        |j                  }t          j        |j                  }|j        g nt          j        |j                  }|j        rt          j        |j                  }nt          j        i g          }|j	        rt          j        |j	                  }nt          j        i g          }| 
                    |j                  }|j        }|dk     rt          d           | ||||j        ||t          |j                  ||j        |j        ||j                  S )Nr   z `num_runs` should be at least 1.)r   r%   r   r   rX   rY   r>   rP   r   ro   rp   r   )shlexsplitr   r%   r   rX   r	   	read_jsonrg   rY   parse_link_varsrp   rP   r6   r   r   r>   r   ro   r   )	clsrt   r   r%   r   rX   rY   rp   rP   s	            r   from_cli_argszSweepServeArgs.from_cli_args  s4   K//	K//	&.BBEK@T4U4U 	  	=)3D4EFFLL *6t<<L 	=)3D4EFFLL *6t<<L''77	=a<<?@@@s+(%%DO,,L;!%!:
 
 
 	
r$   parserreturnc                    |                     dt          dd           |                     dt          dd           |                     dt          d d	           |                     d
dd           |                     dt          dd	           |                     dt          d d	           |                     dt          d d	           |                     ddt          dd	           |                     dt          dd	           |                     ddd           |                     dt          d d	           |                     d t          d!d"	           |S )#Nz--serve-cmdTz4The command used to run the server: `vllm serve ...`)typerequiredhelpz--bench-cmdz=The command used to run the benchmark: `vllm bench serve ...`z--after-bench-cmdzlAfter a benchmark run is complete, invoke this command instead of the default `ServerWrapper.clear_cache()`.)r   defaultr   z--show-stdout
store_truez^If set, logs the standard output of subcommands. Useful for debugging but can be quite spammy.)actionr   z--server-ready-timeoutr   z:Timeout in seconds to wait for the server to become ready.z--serve-paramsa  Path to JSON file containing parameter combinations for the `vllm serve` command. Can be either a list of dicts or a dict where keys are benchmark names. If both `serve_params` and `bench_params` are given, this script will iterate over their Cartesian product.z--bench-paramsa  Path to JSON file containing parameter combinations for the `vllm bench serve` command. Can be either a list of dicts or a dict where keys are benchmark names. If both `serve_params` and `bench_params` are given, this script will iterate over their Cartesian product.z-oz--output-dirresultsz+The directory to which results are written.z
--num-runs   z)Number of runs per parameter combination.z	--dry-runzFIf set, prints the commands to run, then exits without executing them.z--resumezSet this to the name of a directory under `output_dir` (which is a timestamp) to resume a previous execution of this script, i.e., only run parameter combinations for which there are still no output files.z--link-vars zComma-separated list of linked variables between serve and bench, e.g. max_num_seqs=max_concurrency,max_model_len=random_input_len)add_argumentr/   int)rz   r|   s     r   add_cli_argszSweepServeArgs.add_cli_argsC  s   G	 	 	
 	
 	
 	P	 	 	
 	
 	
 	9	 	 	
 	
 	
 	< 	 	
 	
 	
 	$M	 	 	
 	
 	
 	E	 	 		
 		
 		
 	E	 	 		
 		
 		
 	> 	 	
 	
 	
 	<	 	 	
 	
 	
 	1 	 	
 	
 	
 	P	 	 	
 	
 	
 	S 	 	
 	
 	
 r$   sc                     | sg S g }|                      d          D ]U}|                     d          \  }}|                    |                                |                                f           V|S )N,=)rw   rU   strip)r   pairsitemabs        r   ry   zSweepServeArgs.parse_link_vars  so     	IGGCLL 	1 	1D::c??DAqLL!''))QWWYY/0000r$   )__name__
__module____qualname__rC   r/   __annotations__boolr	   r   r   tuplerr   r   rs   classmethodargparse	Namespacer{   ArgumentParserr   staticmethodry   rJ   r$   r   rn   rn   
  s        CyCy#Y        MMMMMM$JE#s(O$t++++!(K#(((!UK#UUU$
!3 $
 $
 $
 [$
L Z("9 Zh>U Z Z Z [Zx 3 4c3h#8    \  r$   rn   rt   c                    | j         p%t          j                                        d          }| j        |z  }| j         r'|                                st          d| d          	 t          | j        | j	        | j
        | j        | j        | j        || j        | j        | j        | j                  S # t$          $ r}t'          d| d          |d }~ww xY w)Nz%Y%m%d_%H%M%Sz+Cannot resume from non-existent directory ())r   r%   r   r   rX   rY   r>   rP   r   rZ   r   z/The script was terminated early. Use `--resume z2` to continue the script from its last checkpoint.)ro   r   nowstrftimer>   r2   r6   rl   r   r%   r   r   rX   rY   rP   r   rp   r   BaseExceptionRuntimeError)rt   	timestampr>   excs       r   run_mainr     s   Gx|~~66GGI9,J{ V:,,.. VTzTTTUUUnn 0(**!]L.!%!:
 
 
 	
    @i @ @ @
 
 	s   'AB3 3
C=CCc                 T    t          t                              |                      d S r]   )r   rn   r{   )rt   s    r   mainr     s$    ^))$//00000r$   __main__)description)1r   rb   r4   rv   dataclassesr   r   pathlibr   typingr   vllm.utils.import_utilsr   param_sweepr	   r
   r   r   utilsr   r   re   ImportErrorcontextmanagerrC   r/   r   r   r   rR   rS   r#   r=   rG   rK   rO   rW   r   rl   rn   r   r   r   r   r   rs   r|   r   
parse_argsrJ   r$   r   <module>r      s          ! ! ! ! ! !                   5 5 5 5 5 5 ; ; ; ; ; ; ; ; ! ! ! ! ! ! $ $ $ $ $ $% % % %		8	$	$BBB%  !$  Cy#Y 	
 (     6
3;
'
 (
 	
 
 
 
DD DCyD (	D
 (D D D D D D DN;;"; #; ; ; ;0$ 0C$J 0 0 0 0
"

 
 
 
 
 D  Cy  #	 
 #             ^ !$: : :Cy:Cy: #Y:
 : !: !: : : : c3h : : : : :z ] ] ] ] ] ] ] ]@>    81x! 1 1 1 1 z$X$1KLLLF'''D					 s   A	 	AA