o
    پiI                     @   s   d Z ddlZddlZddlZddlmZ ddlZddlZddlm	Z	 ej
G dd dZdefdd	Zed
krMe Zee ee Zee dS dS )z
Run one test prompt.

Usage:
python3 -m sglang.test.send_one
python3 -m sglang.test.send_one --profile --profile-steps 5
python3 -m sglang.test.send_one --profile --profile-by-stage
    N)Optional)run_profilec                   @   s"  e Zd ZU 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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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d< dZeed< dZe
ed< d	Zee ed< edejfddZe d ej!fd!d"Z"d	S )#	BenchArgs	localhosthosti0u  port   
batch_sizeFdifferent_promptsNseedg        temperaturei   max_new_tokensfrequency_penaltypresence_penaltyjsonreturn_logprobzSHuman: Give me a fully functional FastAPI server. Show the python code.

Assistant:promptimagemany_imagesstreamprofile   profile_stepsprofile_by_stageprofile_prefixparserc                 C   s>  | j dttjd | j dttjd | j dttjd | j ddtjd | j dttjd | j d	t	tj
d | j d
ttjd | j dt	tjd | j dt	tjd | j ddd | j ddd | j dttjd | j ddd | j ddd | j ddd | j ddd | j dttjd | j ddd | j dttjd d S )Nz--host)typedefaultz--portz--batch-sizez--different-prompts
store_true)actionr   z--seedz--temperaturez--max-new-tokensz--frequency-penaltyz--presence-penaltyz--json)r   z--return-logprobz--promptz--imagez--many-imagesz--streamz	--profilez--profile-stepsz--profile-by-stagez--profile-prefix)add_argumentstrr   r   intr   r	   r
   r   floatr   r   r   r   r   r   r   )r    r$   H/home/ubuntu/.local/lib/python3.10/site-packages/sglang/test/send_one.pyadd_cli_args-   sB   
zBenchArgs.add_cli_argsargsc                    s0   dd t | D }| di  fdd|D S )Nc                 S   s   g | ]}|j qS r$   )name.0attrr$   r$   r%   
<listcomp>S   s    z+BenchArgs.from_cli_args.<locals>.<listcomp>c                    s   i | ]}|t  |qS r$   )getattrr)   r'   r$   r%   
<dictcomp>T   s    z+BenchArgs.from_cli_args.<locals>.<dictcomp>r$   )dataclassesfields)clsr'   attrsr$   r.   r%   from_cli_argsQ   s   zBenchArgs.from_cli_args)#__name__
__module____qualname__r   r!   __annotations__r   r"   r	   r
   boolr   r   r   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   staticmethodargparseArgumentParserr&   classmethod	Namespacer4   r$   r$   r$   r%   r      s2   
 
#r   r'   c              
      s  d| j  d| j }| jrd| _d}n| jrd| _g d}nd }| j | jr+d d}nd }| jd	krH| js< g| j  n fd
dt| jD   || j	| j
| j| j| j|g dd| j| jd}| jr{td| j d t|| jddg| j| jd tj| d|| jd}| jrd}|jddD ]5}|d}|r|dr|dkr n"t|dd  d}|d |d  }	t|d }t|	dd d! qn| }| jd	kr|d }|jd"krt| d#S d$|d% v r|d% d$ dkr|d% d& |d% d$  }
nd'}
|d% d( }|d% d& | }|d% d& }| jst|d  t  g d)}|d*| |
d*|d+gg}tj||d,d-}t| |
|fS ).Nzhttp://:z@Human: Describe this image in a very short sentence.

Assistant:[https://raw.githubusercontent.com/sgl-project/sglang/main/examples/assets/example_image.pngzsHuman: I have one reference image and many images.Describe their relationship in a very short sentence.

Assistant:)r@   r@   r@   r@   zHuman: What is the capital of France and how is that city like. Give me 3 trivial information about that city. Write in a format of json.
Assistant:z$$ANY$$r   c                    s    g | ]}d |d  d  qS )z
Test case r   z: r$   )r*   ir   r$   r%   r,   ~   s     z#send_one_prompt.<locals>.<listcomp>)Questionz
Assistant:z<|separator|>z<|eos|>)sampling_seedr   r   r   r   json_schemastop)text
image_datasampling_paramsr   r   zRunning profiler with z	 steps...CPUGPU)url	num_steps
activitiesr   r   z	/generate)r   r   r   F)decode_unicodezutf-8zdata:zdata: [DONE]   
rG    T)endflush   )r   r   spec_verify_ct	meta_infocompletion_tokensg      ?e2e_latency)zLatency (s)Tokensz
Acc LengthzSpeed (token/s)z.3fz.2fpretty)headerstablefmt) r   r   r   r   r   r   r	   r
   ranger   r   r   r   r   r   r   r   printr   r   r   r   requestspost
iter_linesdecode
startswithloadsstriplenstatus_codetabulate)r'   base_urlrH   rE   	json_dataresponselast_lenchunkret	chunk_str
acc_lengthlatencyspeedtokensr\   rowsmsgr$   rB   r%   send_one_promptW   s   

		


rw   __main__)__doc__r;   r0   r   typingr   r`   ri   sglang.profilerr   	dataclassr   rw   r5   r<   r   r&   r4   
parse_argsr'   r$   r$   r$   r%   <module>   s"    	Ax
