o
    i"                     @   s   d dl Z d dlZd dlZd dlZd dlmZ de jdeeef dedefddZ	de jdeeef de
fd	d
Zde jdeeef deeef defddZG dd dejZdededdfddZdS )    N)Anyargs
extra_info
field_namereturnc                 C   sB   ||v r|| S | }| dD ]}t||s dS t||}q|S )N. )splithasattrgetattr)r   r   r   vnested_field r   O/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/benchmarks/lib/utils.pyextract_field   s   
r   c                 C   s2   t | |ddkpdt| ddv pdt| ddv  S )z:
    Check if the benchmark is run with torch.compile
    compilation_config.mode0eageroutput_jsonr   result_filename)r   r   )r   r   r   r   r   use_compile   s
   r   metricsc                 C   s   g }t jdds|S | D ]Z\}}t|ts%td| dt|j dt	| t
| |dt
| |dt| |dd	d
| ji|||dd}|d d d d}|sdd|v rd|d |d d d d< || q|S )z
    Save the benchmark results in the format used by PyTorch OSS benchmark with
    on metric per record
    https://github.com/pytorch/pytorch/wiki/How-to-integrate-with-PyTorch-OSS-benchmark-database
     SAVE_TO_PYTORCH_BENCHMARK_FORMATFzbenchmark_values for metric 'z' must be a list, but got zvLLM benchmarkr   optimization_level)r   r   r   r   )namer   r   )r   benchmark_valuesr   )	benchmarkmodelmetricr   r   r   tensor_parallel_size)osenvirongetitems
isinstancelist	TypeErrortype__name__varsr   r   r   append)r   r   r   recordsr   r   recordtpr   r   r   #convert_to_pytorch_benchmark_format%   sD   
r.   c                       s4   e Zd ZdefddZdedef fddZ  ZS )
InfEncoderoc                    sX   t |tr fdd| D S t |tr fdd|D S t |tr*t|r*dS |S )Nc              
      s<   i | ]\}}t |tttttd fst|n| |qS N)r$   strintfloatboolr'   	clear_inf).0kr   selfr   r   
<dictcomp>`   s    

z(InfEncoder.clear_inf.<locals>.<dictcomp>c                    s   g | ]}  |qS r   )r6   )r7   r   r9   r   r   
<listcomp>g   s    z(InfEncoder.clear_inf.<locals>.<listcomp>inf)r$   dictr#   r%   r4   mathisinf)r:   r0   r   r9   r   r6   ^   s   


zInfEncoder.clear_infr   c                    s    t  j| |g|R i |S r1   )super
iterencoder6   )r:   r0   r   kwargs	__class__r   r   rB   l   s    zInfEncoder.iterencode)r(   
__module____qualname__r   r6   rB   __classcell__r   r   rD   r   r/   ]   s    r/   filenamer+   c                 C   sF   t | d}tj||tdd d W d    d S 1 sw   Y  d S )Nwc                 S   s   dt | j dS )N<z is not JSON serializable>)r'   r(   )r0   r   r   r   <lambda>v   s    zwrite_to_json.<locals>.<lambda>)clsdefault)openjsondumpr/   )rI   r+   fr   r   r   write_to_jsonp   s   "rS   )argparserP   r?   r    typingr   	Namespacer>   r2   r   r5   r   r%   r.   JSONEncoderr/   rS   r   r   r   r   <module>   s4   

 


8