o
    :/i&                     @   sX  d Z ddlmZ ddlmZ ddlmZ zddlmZ	 ddl
mZ W n ey9   edZedZ	edZY nw zddlmZ W n eyT   ed	Zed
ZY nw 			ddeeeef  dedee dB dee dB dee dB ddfddZdeeeef  dee dee deeeef  fddZdeeeef  deddfddZdS )z%Generate plots for benchmark results.    )Path)Any)PlaceholderModuleNplotlyexpressio
matplotlibpyplotresultsoutput_pathcolorsitl_thresholdslabelsreturnc           	   
   C   s  |du rg d}|du rddg}|du r;d|d d dd	|d d dd
|d d dd	d|d d dd	g}ddit t||}dg| }t| ||}|sYtd dS tj|dddd|d|ig dd}|jdd |jdd |jdddd t	
|t| td|  dS )a  
    Generate an HTML timeline plot from benchmark results.

    Args:
        results: List of per-request result dictionaries containing:
            - start_time: Request start time (seconds)
            - ttft: Time to first token (seconds)
            - itl: List of inter-token latencies (seconds)
            - latency: Total request latency (seconds)
            - prompt_len: Number of prompt tokens
            - output_tokens: Number of output tokens
        output_path: Path where the HTML file will be saved
        colors: List of colors for ITL categories (default: green, orange, red, black)
        itl_thresholds: ITL thresholds in seconds (default: [1.0, 4.0, 6.0])
        labels: Labels for ITL categories (default based on thresholds)
    N)z#109618z#FF7F0Ez#D62728g?g?zITL < r   i  z.0fmsu   ms ≤ ITL <    u   ITL ≥ TTFTz#636EFAzNo timeline data to plotstartend
request_idtype)prompt_tokensoutput_tokensreq_start_timereq_finish_timesegment_startsegment_endduration)x_startx_endycolorcolor_discrete_mapcategory_orders
hover_dataa  <b>%{y}</b><br>Type: %{fullData.name}<br>Start: %{customdata[4]}<br>End: %{customdata[5]}<br>Duration: %{customdata[6]}<br>Prompt Tokens: %{customdata[0]}<br>Output Tokens: %{customdata[1]}<br>Request Start Time: %{customdata[2]}<br>Request End Time: %{customdata[3]}<br><extra></extra>)hovertemplatereversed)	autorangeTime
Request IDT)xaxis_titleyaxis_title
showlegendzTimeline plot saved to: )dictzipconstruct_timeline_dataprintpxtimelineupdate_tracesupdate_yaxesupdate_layoutpio
write_htmlstr)	r
   r   r   r   r   labels_colorslabels_ordertimeline_datafig r=   a/lsinfo/ai/hellotax_ai/llm_service/venv_vllm/lib/python3.10/site-packages/vllm/benchmarks/plot.pygenerate_timeline_plot   sH   "
r?   requests_datac                    s  dt dtfdd}dt dtf fdd}d}| D ]}|d	}|dur.|du s,||k r.|}q|du r5g S g }t| D ]\}	}|d	}|d
}
|dg }|d}|dd}|dd}|du sl|
du sl|du rmq;|| }||}||
 }||}|d|	 ||d||||||| |||
ddd |}|}|D ].}|| }||}|d|	 ||||||||||| |||ddd |}|}qq;|S )a  
    Construct timeline data from request results.

    Args:
        requests_data: List of per-request result dictionaries
        itl_thresholds: ITL thresholds in seconds
        labels: Labels for ITL categories

    Returns:
        List of timeline segments for plotting
    sec_timer   c                 S   sN   t | d }|dk sJ dt | d d }| d }|dd|dd|dS )z'Convert seconds to HH:MM:SS.mmm format.i  d   z&time seems to last more than 100 hours<   02d:z06.3f)int)rA   hmsr=   r=   r>   tostr   s
   z&construct_timeline_data.<locals>.tostritlc                    s0   |  d k r
d S |  d k rd S d S )z#Categorize ITL based on thresholds.r   r      r=   )rK   r   r   r=   r>   itl_type   s
   z)construct_timeline_data.<locals>.itl_typeN
start_timettftlatency
prompt_lenr   r   zReq r   z.3frI   )r   r   r   r   r   r   r   r   r   r   r   )floatr8   get	enumerateappend)r@   r   r   rJ   rN   t0requestrO   r;   irP   rK   rQ   rR   r   start_time_strttft_endttft_end_str	prev_timeprev_time_str	itl_valueitl_enditl_end_strr=   rM   r>   r/   t   sz   








r/   c                 C   s  g }g }g }| D ]}| dd}| dd}|| || |||  q|s0td dS tjdddd\}\\}	}
\}}|	j|d	d
ddd |	d |	d |	d |	j	ddd |
j|d	dddd |
d |
d |
d |
j	ddd |j|d	dddd |d |d |d |j	ddd t
tt|}|j||dd
dd |j|||dddd |d |d |d |  |j	dddd  t  tjt|d!d"d# t| td$|  dS )%aC  
    Generate a matplotlib figure with dataset statistics.

    Creates a figure with 4 subplots:
    - Top-left: Prompt tokens distribution (histogram)
    - Top-right: Output tokens distribution (histogram)
    - Bottom-left: Prompt+output tokens distribution (histogram)
    - Bottom-right: Stacked bar chart (request_id vs tokens)

    Args:
        results: List of per-request result dictionaries containing:
            - prompt_len: Number of prompt tokens
            - output_tokens: Number of output tokens
        output_path: Path where the figure will be saved
    rR   r   r   z-No data available for dataset statistics plotNrL   )   
   )figsize   	steelblueblackgffffff?)binsr!   	edgecoloralphazPrompt Tokens	FrequencyzPrompt Tokens DistributionTg333333?)rj   coralzOutput TokenszOutput Tokens DistributionmediumseagreenzTotal Tokens (Prompt + Output)zTotal Tokens Distribution)labelr!   rj   )bottomrn   r!   rj   r)   TokenszTokens per Request (Stacked)r    )rj   axis   tight)dpibbox_inchesz"Dataset statistics plot saved to: )rT   rV   r0   pltsubplotshist
set_xlabel
set_ylabel	set_titlegridlistrangelenbarlegendtight_layoutsavefigr8   close)r
   r   r   r   total_tokensrX   rR   
output_lenr<   ax1ax2ax3ax4request_idsr=   r=   r>   generate_dataset_stats_plot   sf   

 














r   )NNN)__doc__pathlibr   typingr   vllm.utils.import_utilsr   plotly.expressr   r1   	plotly.ior   r6   ImportError_plotlyplaceholder_attrmatplotlib.pyplotr	   rv   _matplotlibr}   r-   r8   rS   r?   r/   r   r=   r=   r=   r>   <module>   sd   




[
p