o
    پi                     @   s  d Z 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	m
Z
 ddlZeddZ				d5d	e
e d
ede	e de
e dedede
e defddZedkreddZejdeddd ejdeddd ejdeddd ejdejedd d! ejd"ed#d$ ejd%ejed&d'd! ejd(ejed&d)d! ejd*ejedd+d! ejd,ejedd-d! ejd.ejedd/d! e Zg Zejred0 ejred1 ejred2 ejred3 eejejeej ej!ej"ej#d4 dS dS )6z8
Run live profiling.

Usage:
python3 -m sglang.profiler
    N)ArgumentParser)Path)ListOptionalSGLANG_TORCH_PROFILER_DIRz/tmpFurl	num_steps
activities
output_dirprofile_by_stagemerge_profilesprofile_prefixreturnc                 C   s  |d u rt }ttjtj|tt  }|jddd t	d|  t	d| d|d t|d }|
 sgt| d }|  | }	t|d	}
|
t|	 W d    n1 sbw   Y  t|t|||||d
}tj| d |d}|  t|}|S )NT)exist_okparentszDump profiling traces to zWaiting for z9 steps and the trace to be flushed.... (profile_by_stage=)zserver_args.jsonz/get_server_infow)r
   r   r	   r   r   r   z/start_profile)r   json)PROFILER_DIRr   ospathabspathnormpathstrtimemkdirprintexistsrequestsgetraise_for_statusr   openwritedumpspost)r   r   r	   r
   r   r   r   	file_pathresponseserver_args_datafile	json_data
trace_link r+   C/home/ubuntu/.local/lib/python3.10/site-packages/sglang/profiler.pyrun_profile   s6   	$	r-   __main__z(Benchmark the online serving throughput.)descriptionz--urlzhttp://localhost:30000z7Server or API base url if not using http host and port.)typedefaulthelpz--output-dirz)Profile directory to dump profile traces.z--num-steps   z'The number of forward steps to profile.z--profile-by-stagez0Whether to profile prefill and decode separately)actionr0   r1   r2   z--profile-prefixz!The prefix of this profiler file.)r0   r2   z--cpuTzWhether to profile CPU activityz--gpuzWhether to profile GPU activityz--memz@Whether to profile memory usage (https://pytorch.org/memory_viz)z--rpdzJWhether to use ROCM rpd profiler (https://github.com/ROCm/rocmProfileData)z--merge-profileszAWhether to merge profiles from all ranks into a single trace fileCPUGPUMEMRPD)r   r   r	   r
   r   r   r   )NFFN)$__doc__argparser   r   r   r   pathlibr   typingr   r   r   getenvr   r   intboolr-   __name__parseradd_argumentBooleanOptionalAction
parse_argsargsr	   cpuappendgpumemrpdr   r   r
   r   r   r   r+   r+   r+   r,   <module>   s    
/





