o
    i5                     @   s\  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 zd dlZW n eyU   edZY nw ejdddee dee dedededefddZdeeef dededefddZdedB dee dededededefddZ d ed!ed"efd#d$Z!d%ededB fd&d'Z"d!ed(ed efd)d*Z#dedB dee d!ed"ed%ed+edefd,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d0ee%eef  defd1d2Z&eG d3d4 d4Z'd5e'fd6d7Z(d5e j)fd8d9Z*e+d:kr,e j,e'j-d;Z.e'/e. e*e.0  dS dS )<    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                c   s    | | }td td|  td|  |r#d V  td d S t|||d}|j|d |V  W d    n1 s>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server r   Q/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/benchmarks/sweep/serve.py
run_server   s   

r   run_databench_overrides
run_numberc                 C   s    || d< |  | |  | | S )Nr   )update)r   r   r   r   r   r   r   _update_run_data4   s   

r!   r   	bench_cmdoutput_pathc          
      C   s  g | |ddddt|jd|j}td td|  td|  td	|  td
|  | rbtd |d}t|}	t	|	|||W  d    S 1 s]w   Y  | d u ru|sot
d| td d S |jjddd | | |   |d}t|}	W d    n1 sw   Y  t	|	|||}	|d}tj|	|dd W d    n1 sw   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   r   r   r   run_benchmarkA   sn   

 	
r9   
output_dir
serve_comb
bench_combc                 C   sD   t t  }|r|d|jf |r|d|jf | td| S )NzSERVE-zBENCH--)listr+   extendr-   r   join)r:   r;   r<   partsr   r   r   _get_comb_base_path   s   
rB   	base_pathc                 C   s    |d u r| d S | d| d S )Nzsummary.jsonzrun=z.jsonr   )rC   r   r   r   r   _get_comb_run_path   s   rD   bench_combsc                 C   s0   |D ]}t || |}t|d d s dS qdS )Nr   TF)rB   rD   r.   )r;   rE   r:   r<   rC   r   r   r   _comb_needs_server   s   rG   num_runsc             
   C   s   t tttf   }t|D ]}t| ||||t|||d}	|	d ur&||	 q|r+d S t|d dd}
t	j
||
dd W d    |S 1 sHw   Y  |S )N)r   r   r   r#   r   rF   r'   r(   r)   )r>   dictr+   objectranger9   rD   appendr/   r0   r6   )r   r"   r;   r<   rC   rH   r   	comb_datar   r   r8   r   r   r   run_comb   s,   



rN   serve_paramsbench_paramslinksc                   s   t tttf   }|D ]Tt||rt| ||||
dnt 6}|D ]+ t fdd|	D }|s5q$t	| }t
|| |||d}|d urO|| q$W d    n1 sZw   Y  q|rdd S tj|}||d  |S )N)r   r   r   r   c                 3   s4    | ]\}}|v o| v o|  | kV  qd S Nr   ).0	serve_key	bench_keyr<   r;   r   r   	<genexpr>   s    
zrun_combs.<locals>.<genexpr>)r;   r<   rC   rH   r   zsummary.csv)r>   rI   r+   rJ   rG   r   
contextlibnullcontextallrB   rN   r?   pd	DataFramefrom_recordsto_csv)r   r"   r   r   rO   rP   r:   rH   r   rQ   r   all_datar   
should_runrC   rM   combined_dfr   rV   r   	run_combs   sR   

	

$rb   c                   @   s   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
B ed< ee
eef  d
B ed< e	ed< dZee ed< dZee ed< edejfddZedejdejfddZededee
eef  fddZd
S )SweepServeArgsr   r"   r   r   rO   rP   r:   rH   r   Nresume	link_varsr   serveparser_namez2Run vLLM server benchmark under multiple settings.parser_helpargsc           	      C   s   t |j}t |j}|jd u rg nt |j}|jr#t|j}nti g}|j	r3t|j	}nti g}| 
|j}|j}|dk rJtd| ||||j||t|j||j|j||jdS )Nr   z `num_runs` should be at least 1.)r   r"   r   r   rO   rP   r:   rH   r   rd   re   r   )shlexsplitr   r"   r   rO   r   	read_jsonr]   rP   parse_link_varsre   rH   r2   r   r   r:   r   rd   r   )	clsri   r   r"   r   rO   rP   re   rH   r   r   r   from_cli_args  s8   zSweepServeArgs.from_cli_argsparserreturnc                 C   s   |j dtddd |j dtddd |j dtd dd	 |j d
ddd |j dtddd	 |j dtd dd	 |j dtd dd	 |j ddtddd	 |j dtddd	 |j dddd |j dtd dd	 |j d td!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()`.)rr   defaultrt   z--show-stdout
store_truez^If set, logs the standard output of subcommands. Useful for debugging but can be quite spammy.)actionrt   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)rn   rp   r   r   r   add_cli_argsC  s   

	
zSweepServeArgs.add_cli_argssc                 C   sD   | sg S g }|  dD ]}| d\}}|| | f q|S )N,=)rk   rL   strip)r~   pairsitemabr   r   r   rm     s   zSweepServeArgs.parse_link_vars)__name__
__module____qualname__r>   r+   __annotations__boolr   r   r|   tuplerg   r   rh   classmethodargparse	Namespacero   ArgumentParserr}   staticmethodrm   r   r   r   r   rc   
  s*   
 &\$rc   ri   c                 C   s   | j p	t d}| j| }| j r| std| dzt| j| j	| j
| j| j| j|| j| j| j| jdW S  tyM } z	td| d|d }~ww )Nz%Y%m%d_%H%M%Sz+Cannot resume from non-existent directory ())r   r"   r   r   rO   rP   r:   rH   r   rQ   r   z/The script was terminated early. Use `--resume z2` to continue the script from its last checkpoint.)rd   r   nowstrftimer:   r.   r2   rb   r   r"   r   r   rO   rP   rH   r   re   r   BaseExceptionRuntimeError)ri   	timestampr:   excr   r   r   run_main  s4   

r   c                 C   s   t t|  d S rR   )r   rc   ro   )ri   r   r   r   main  s   r   __main__)description)1r   rX   r0   rj   dataclassesr   r   pathlibr   typingr   vllm.utils.import_utilsr   param_sweepr   r	   r   r
   utilsr   r   r[   ImportErrorcontextmanagerr>   r+   r   r|   r   rI   rJ   r!   r9   rB   rD   rG   rN   r   rb   rc   r   r   r   r   r   rh   rp   r}   
parse_argsr   r   r   r   <module>   s   


G


/	

= !

