o
    پis,                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ dZ	dZ
dZdZejejedZi Zdd	 Zedfd
dZdd Zdd Z	d ddZdd Zdd Z	d!ddZ	d"ddZ	d"ddZdS )#    N)get_tokenizerzTHUDM/LongBench-v2train0   i  z.longbench_cachec                 C   s&   |  dd}|  dd}| d| S )zRFormat a LongBench V2 example into a single text string (context + question only).context question )get)exampler   r    r   M/home/ubuntu/.local/lib/python3.10/site-packages/sglang/test/kl_test_utils.pyformat_longbench_v2_example   s   r   c              
   C   s  |du rt }|  d| d| }|tv r$tdtt|  d t| S tjtdd | dddd}tj	td	| d| d| d
}tj
|r}td|  t|d}t|}W d   n1 shw   Y  |t|< tdt| d |S zddlm} W n ty }	 ztd|	d}	~	ww t| }
td| d |ttdd}g }t|D ](\}}t||kr nt|}|
|}t|tdd }||d|  qt|d}t|| W d   n1 sw   Y  tdt| d|  |t|< |S )z;Get input_ids from LongBench V2 dataset with local caching.N_zUsing in-memory cached data (z	 prompts)T)exist_ok/\
input_ids_z.jsonzLoading from local cache: rzLoaded z prompts from cacher   )load_datasetz;Please install the 'datasets' package: pip install datasetszDownloading z) samples from LongBench V2 (streaming)...)split	streaming      ?g      ?wzSaved z prompts to cache: )DEFAULT_NUM_SAMPLES_cached_input_idsprintlenosmakedirs	CACHE_DIRreplacepathjoinexistsopenjsonloaddatasetsr   ImportErrorr   LONGBENCH_V2_DATASETLONGBENCH_V2_SPLIT	enumerater   encodeintrandomuniformappenddump)tokenizer_pathmax_prompt_tokensnum_samples	cache_key	safe_name
cache_filef	input_idsr   exc	tokenizerdatasetir
   texttokenstruncate_lenr   r   r   get_input_ids   sd   
rA   c              	   C   s   g }t | |D ]#\}}t|}t|}|| }t|d | }	|t|	 qtd|  t|t| }
td|
  td||   |
|| d k sfJ d|
 d|| d  d| d| d	S )
zECompare the KL divergence between input and output log probabilities.   zkl_divs=zavg_kl_div=zACC_THRESHOLDS=kl_divz > threshold=z for r   N)	zipnparrayexpr0   meanr   sumr   )input_logprobsoutput_logprobsACC_THRESHOLDS
model_name	test_namekl_divsinput_logproboutput_logproblogr	kl_approx
avg_kl_divr   r   r   compare_kl_divergenceb   s$   

rU   c                 C   s   t | d  d S )Nz/flush_cache)requestspost)base_urlr   r   r   _flush_cachey   s   rY   Fc                 C   sB   |d|ddd}|r| dd|d tj| d |d}| S )	z)Send generate request and return results.rB   T)temperaturemax_new_tokens
ignore_eos)r9   sampling_paramsF)return_logprobreturn_text_in_logprobslogprob_start_lenz	/generate)r%   )updaterV   rW   r%   )rX   r9   r\   r_   ra   	json_dataresponser   r   r   	_generate}   s   re   c                 C   s~   t |  t| |dddd}t|t|ksJ g }t|D ]\}}|d d }dd |D t||  d }|| q|S )	z;Run prefill to get input logprobs matching output logprobs.r   T)r\   r_   ra   	meta_infoinput_token_logprobsc                 S      g | ]}|d  qS r   r   .0xr   r   r   
<listcomp>       z'_get_input_logprobs.<locals>.<listcomp>N)rY   re   r   r+   r0   )rX   new_input_idsrK   resultsrJ   r=   resultlogprobr   r   r   _get_input_logprobs   s    rs   c                 C   s   dd | d d D S )z&Extract output logprobs from a result.c                 S   rh   ri   r   rj   r   r   r   rm      rn   z,_extract_output_logprobs.<locals>.<listcomp>rf   output_token_logprobsr   )rq   r   r   r   _extract_output_logprobs   s   ru   >  c                 C   s   t }|d ur||kr|}t||d}|d ur|d | }tdt| d td t|  t| ||dd}t|t|ks@J g }g }	t|D ]\}
}|||
 |d   |	t| qHtd t	| ||	}t
||	||t jj d S )	Nr2   r4   z.Running test_input_output_logprobs_match with  promptsz=Flush Cache and Running generation to get output logprobs ...Tr_   
output_idsz)Running prefill to get input logprobs ...)r   rA   r   r   rY   re   r+   r0   ru   rs   rU   inspectcurrentframef_codeco_name)rX   rL   rM   max_samplesr\   r4   r9   rp   ro   rK   r=   rq   rJ   r   r   r   'test_input_output_logprobs_match_helper   s2   
r       c                 C   s|  t | d  }|d rtd d S t}|d ur||kr|}t||d}|d ur/|d | }tdt| d td t|  t| |dd	 td
 t| ||dd}t|t|ks^J g }	g }
t	|D ]'\}}|d d dkr{td| d qf|	
|| |d   |

t| qft|	dt| ksJ dt|	 dt| td t| |	|
}t||
||t jj d S )N/get_server_infodisable_radix_cache&Radix cache is disabled, skipping testrw   z@Running test_input_output_logprobs_match_prefill_cache_hit with rx   z.Flush Cache and Prefill to cache the input ...r   )r\   -Running generation to get output logprobs ...Try   rf   cached_tokenszPrefill cache miss for prompt 
, skippingrz   r   zToo few prefill cache hits: r   5Flush Cache and run prefill to get input logprobs ...)rV   r	   r%   r   r   rA   r   rY   re   r+   r0   ru   rs   rU   r{   r|   r}   r~   )rX   rL   rM   r   r\   server_infor4   r9   rp   ro   rK   r=   rq   rJ   r   r   r   9test_input_output_logprobs_match_prefill_cache_hit_helper   sR   


r   c                    s  t | d  }|d rtd d S t}|d ur||kr|}t||d|d ur/d | tdt d td t|  t| |dd	}t|tksSJ t	|d
}|
d  fddt|D }	td t| |	|dd	}t|t|	ksJ g }
g }t|D ]-\}}|d d t| d krtd| d q|
|	| |d   |t| qt|
dt|	 ksJ dt|
 dt|	 td t| |
|}t||||t jj d S )Nr   r   r   rw   z?Running test_input_output_logprobs_match_decode_cache_hit with rx   z@Flush Cache and First turn: Prefill + Decode to cache decode ...Try   )tokenizer_name,c                    s$   g | ]\}}| |d     qS )rz   r   )rk   r=   rq   comma_token_idfirst_turn_input_idsr   r   rm   $  s    zLtest_input_output_logprobs_match_decode_cache_hit_helper.<locals>.<listcomp>r   rf   r   rB   zDecode cache miss for prompt r   rz   r   zToo few decode cache hits: r   r   )rV   r	   r%   r   r   rA   r   rY   re   r   r,   r+   r0   ru   rs   rU   r{   r|   r}   r~   )rX   rL   rM   r   r\   r   r4   rp   r;   second_turn_input_idsro   rK   r=   rq   rJ   r   r   r   8test_input_output_logprobs_match_decode_cache_hit_helper  sj   




r   )FrZ   )Nrv   )Nr   )r{   r%   r   r.   numpyrE   rV   &sglang.srt.utils.hf_transformers_utilsr   r)   r*   r   DEFAULT_PROMPT_TOKENSr!   r"   dirname__file__r   r   r   rA   rU   rY   re   rs   ru   r   r   r   r   r   r   r   <module>   s8    
D

#
6