o
    پi2J                     @   s   d 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dl
mZmZ e	 Zdd Zdd Zd	d
 Zdd Zdd Zdd Zd,ddZdd Zdd Zdd Zd,ddZdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Z dS )-z:This file contains the SGL programs used for unit testing.    N)is_hip)download_and_cache_file
read_jsonlc                  C   s   t jdd } | jdd}d|d   v s J d|d  | jdd	idd
iddigdd}dd |D }|g dksDJ d| d S )Nc                 S   sd   | d7 } | d7 } | d7 } | d7 } | d7 } | d7 } | d7 } | d| d	 7 } | d
t jdd	dd 7 } d S )Nz+The following are questions with answers.

z"Q: What is the capital of France?
z	A: Paris
z#Q: What is the capital of Germany?
z
A: Berlin
z!Q: What is the capital of Italy?
zA: Rome
zQ: 
zA:answerr   )stoptemperaturesglgensquestion r   M/home/ubuntu/.local/lib/python3.10/site-packages/sglang/test/test_programs.pyfew_shot_qa   s   z%test_few_shot_qa.<locals>.few_shot_qa)What is the capital of the United States?)r   
washingtonr   zanswer: r   zWhat is the capital of Japan?z*What is the capital of the United Kingdom?z"What is the capital city of China?皙?r   c                 S   s   g | ]
}|d     qS )r   )striplower).0xr   r   r   
<listcomp>(   s    z$test_few_shot_qa.<locals>.<listcomp>)tokyolondonbeijingz	answers: )r
   functionrunr   r   	run_batch)r   retretsanswersr   r   r   test_few_shot_qa   s   
&r$   c                  C   s@   t jdd } d}d}| j||ddd}t| dv sJ d S )	Nc                 S   s   | t d7 } | t |7 } | t t d7 } |   | |7 } W d    n1 s+w   Y  |   | t d7 } W d    d S 1 sGw   Y  d S )NYou are a helpful assistant.answer_1answer_2r
   systemuser	assistantr   )r   
question_1
question_2r   r   r   answer_mt_bench-   s   


"z&test_mt_bench.<locals>.answer_mt_benchCompose an engaging travel blog post about a recent trip to Hawaii, highlighting cultural experiences and must-see attractions.zGRewrite your previous response. Start every sentence with the letter A.gffffff?@   )r,   r-   r   max_new_tokens)      )r
   r   r   lenmessages)r.   r,   r-   r!   r   r   r   test_mt_bench,   s   
	r6   c                 C   s   t jdd }|jdd}| r|d dksJ | n|d dv s$J |jdd}| r9|d d	ks8J | n|d dv sAJ |jd
d}| rW|d dksUJ | d S |d dv s_J d S )Nc                 S   sD   | d7 } | d7 } | d7 } | d| d 7 } | dt dg d 7 } d S )	NzBDetermine whether the statement below is True, False, or Unknown.
z+Statement: The capital of France is Pairs.
zAnswer: True
zStatement: r   zAnswer:r   TrueFalseUnknown)r
   select)r   	statementr   r   r   true_or_falseB   s
   z"test_select.<locals>.true_or_falsez!The capital of Germany is Berlin.)r<   r   r8   r7   zThe capital of Canada is Tokyo.r9   z$Purple is a better color than green.r:   )r
   r   r   text)check_answerr=   r!   r   r   r   test_selectA   s(   
r@   c                  C   sV   t jdd } | jdd}t|d dksJ | t|d dks)J | d S )	Nc                 S   s0   | dt d d 7 } | dt d d 7 } d S )Nz The number of hours in a day is hoursr   z The number of days in a year is days)r
   gen_intr   r   r   r   
decode_intd   s   z#test_decode_int.<locals>.decode_intr   r   rA      rB   im  )r
   r   r   intr>   )rE   r!   r   r   r   test_decode_intc   s
   
 rH   c                  C   x   t jdd } | jdd}z	t|d }W n tjjy'   td|d   w t|d t	s1J t|d t
s:J d S )	Nc                 S   s   ddl m}m}m} | d7 } | d7 } | dJ | d7 } | dtj|d d	 7 } | d
tj|ddgd d	 7 } | dtj|ddgd d	 7 } | dtj|ddgd d 7 } | d7 } W d    d S 1 sdw   Y  d S )Nr   )REGEX_FLOAT	REGEX_INT	REGEX_STRHGenerate a JSON object to describe the basic city information of Paris.
zHere are the JSON object:
json_output{

  "name": regex,
  "population":  r   )rR   r   
  "area": z  "latitude": })sglang.lang.irrJ   rK   rL   	var_scoper
   r   )r   rJ   rK   rL   r   r   r   decode_jsono   s   
"z+test_decode_json_regex.<locals>.decode_jsong        r   rN   JSONDecodeErrorname
populationr
   r   r   jsonloadsdecoderr[   print
isinstancestrrG   rZ   r!   js_objr   r   r   test_decode_json_regexn   s   
rg   c                  C   rI   )	Nc                 S   s   | d7 } |  dG | d7 } | dt  d 7 } | dt  d 7 } | dtjtd d 7 } | d	t  d 7 } | d
tjtd d 7 } | d7 } W d    d S 1 sSw   Y  d S )NrM   rN   rO   rP   rS   rT   rV   dtypez  "country": z  "timezone": r   rW   )rY   r
   
gen_stringrC   r   rG   rd   rD   r   r   r   rZ      s   
"z%test_decode_json.<locals>.decode_jsonr0   )r1   rN   r[   r\   r]   r^   re   r   r   r   test_decode_json   s   
rk   Tc                 C   sJ   t jdd }|jddd}| r!d|  v s#J d|  d S d S )Nc                 S   sX   | d| d 7 } | dt jdddgd d 7 } | d| d  d	 t jd
dd d 7 } d S )N
Question: r   z(A good person to answer this question isexpert.r   .
zFor example,z would answer that r   r	   r   r   r   r   expert_answer   s&   z)test_expert_answer.<locals>.expert_answerzWhat is the capital of France?r   r   r   pariszAnswer: )r
   r   r   r>   r   )r?   rq   r!   r   r   r   test_expert_answer   s   
"rt   c                     sL   dd  t j fdd} d\}}| ||dd}t|d || ks$J d S )	Nc                 S   s
   t |  S )N)eval)
expressionr   r   r   	calculate   s   
z test_tool_use.<locals>.calculatec                    s   | d7 } | d7 } | dt | d t | d 7 } | dtjddd	 d
 7 } | d |  | d 7 } W d    d S 1 s>w   Y  d S )NzfPlease perform computations using a calculator. You can use calculate(expression) to get the results.
z0For example,
calculate(1+2)=3
calculate(3*4)=12
z!Question: What is the product of z and z?
z Answer: The answer is calculate(rv   )ro   z) = r   )rd   r
   r   rY   )r   lhsrhsrw   r   r   tool_use   s    "ztest_tool_use.<locals>.tool_use)i  i  r   )ry   rz   r   r   )r
   r   rG   )r|   ry   rz   r!   r   r{   r   test_tool_use   s   r}   c                  C   sD   t jdd } | jddd}|d  }d|v sd|v s J d S d S )	Nc                 S   s   | d7 } | d| d 7 } t ddD ]^}| d| dtjddgd	 d
 7 } | d| dtd| ddg 7 } | d|  dkr`| dtjdd	 d 7 } | d| dtjddgd	 d
 7 } q| dtjddd	 d 7 }  d S d S )Na  
Question: Which country does the founder of Microsoft live in?
Thought 1: I need to search for the founder of Microsoft.
Action 1: Search [Founder of Microsoft].
Observation 1: The founder of Microsoft is Bill Gates.
Thought 2: I need to search for the country where Bill Gates lives in.
Action 2: Search [Where does Bill Gates live].
Observation 2: Bill Gates lives in the United States.
Thought 3: The answer is the United States.
Action 3: Finish [United States].

rl   r      r3   zThought :rn   ro   rp   zAction : action_SearchFinishz []z].
zObservation r   )ranger
   r   r;   )r   r   ir   r   r   react   s   $&&ztest_react.<locals>.reactz/What country does the creator of Linux live in?r   rr   r   finlandstates)r
   r   r   r   )r   r!   r   r   r   r   
test_react   s   
r   c                     s@   dd t j fdd} | jddd}t|d tsJ d S )	Nr0   r3   c              	      s  | d7 } | d| d 7 } | dt   d 7 } tdd  D ]}| | dtjddd	gd
 d 7 } q|  }t D ]$}||  d|d  d|d  d7  < ||  tjddgd7  < q;|  | d7 } t D ]}| d|d  d|| d  d	 7 } ql| dtjddd 7 } d S )NzAct as a helpful assistant.
zUSER: Give some tips for rp   zASSISTANT: Okay. Here are z# concise tips, each under 8 words:
r~   rn      r   )
max_tokensr   zNow, I expand tip z  into a detailed paragraph:
Tip r   detailed_tip

ro   z/Here are these tips with detailed explanation:
zTip z
In summary,summaryi   r   )rd   r   r
   r   forkjoin)r   topicr   forks	fork_sizer   r   r   parallel_decoding   s0   &
 &z1test_parallel_decoding.<locals>.parallel_decodingzwriting a good blog postg333333?)r   r   r   )r
   r   r   rc   rd   )r   r!   r   r   r   test_parallel_decoding   s   r   c                    sJ   d t j fdd}|jdddddd	}|d
 }| r!d|v s#J d S d S )Nr0   c                    s~   | d7 } | d7 } | d7 } |||g |  t }| fdd7 }|jdd | d7 } | d	| d
 7 } | dtjdd 7 } d S )Nz6USER: I will ask a question based on some statements.
z)ASSISTANT: Sure. I will give the answer.
z(USER: Please memorize these statements.
c                    s   d|  d |   d S )Nz
Statement r   r   r   )r   contextsr   r   <lambda>%  s    zCtest_parallel_encoding.<locals>.parallel_encoding.<locals>.<lambda>concate_and_append)modez?Now, please answer the following question. Do not list options.z
Question: r   z
ASSISTANT:r   r   )r   r4   r   r
   r   )r   r   	context_0	context_1	context_2r   r   r   r   parallel_encoding  s   
z1test_parallel_encoding.<locals>.parallel_encodingzWho is the father of Julian?zEthan is the father of Liam.zNoah is the father of Julian.zOliver is the father of Carlos.r   )r   r   r   r   r   r   Noah)r
   r   r   )r?   r   r!   r   r   r   r   test_parallel_encoding  s   r   c                  C   sd   t jdd } | jdddd}d| d d	 v s.d
| d d	 v s0J | d d	  d S d S )Nc                 S   s0   | t t d| 7 } | t t d7 } d S )Nzexample_image.pngr   )r
   r*   imager+   r   r   r   r   r   image_qa:  s   ztest_image_qa.<locals>.image_qaz+Please describe this image in simple words.r   r0   )r   r   r1   taxicontentcar)r
   r   r   r5   )r   stater   r   r   test_image_qa9  s   
r   c                  C   s`   t jdd } | ddd}d}| D ]}||7 }q| ddd}d}|dD ]}||7 }q'd S )Nc                 S   s4   | t d7 } | t |7 } | t t d7 } d S )Nr%   r   r(   r   r   r   r   qaL  s   ztest_stream.<locals>.qar/   T)r   stream r   )r
   r   	text_iter)r   r!   outchunkr   r   r   test_streamK  s"   


r   c                     s:   d t j fdd} |  }|d }t |sJ d S )Nz@((25[0-5]|2[0-4]\d|[01]?\d\d?).){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)c                    s$   | d7 } | dt jdd d 7 } d S )Nz5Q: What is the IP address of the Google DNS servers?
A: r   r   )r   rR   r	   rD   rQ   r   r   	regex_genf  s   ztest_regex.<locals>.regex_genr   )r
   r   r   rematch)r   r   r   r   rQ   r   
test_regexc  s   r   c                  C   sf   t jdd } |  }zt|d |d< t|d |d< t|d |d< W d S  ty2   t|  w )Nc                 S   s   | d7 } | dt jdtdd d 7 } | d7 } | dt jdtd	 d 7 } | d
7 } | dt jdtd	 d 7 } | d7 } | dt jdtd	 d 7 } d S )Nz!Q: What is the full name of DNS?
zA: The full names is str_resr   )ri   r   z Q: Which year was DNS invented?
r   int_resrh   zQ: What is the value of pi?
	float_reszQ: Is the sky blue?
bool_res)r
   r   rd   rG   floatboolrD   r   r   r   	dtype_genu  s   z!test_dtype_gen.<locals>.dtype_genr   r   r   )r
   r   r   rG   r   r   
ValueErrorrb   )r   r   r   r   r   test_dtype_gent  s   

r   c                  C   sz   t jdddd } t jdd }t jjj}|  |    |j}|  |   |j}||k s;J | d| d S )Nr0   num_api_spec_tokensc                 S   ^   | d7 } | d7 } | d7 } | dt jddd d t jd	dd 7 } | d
t jddd d 7 } d S Nz3Construct a character within the following format:
?Name: Steve Jobs.
Birthday: February 24, 1955.
Job: Apple CEO.
z-
Please generate new Name, Birthday and Job.
Name:r\   r   ro   

Birthday:birthday
Job:jobr	   rD   r   r   r   gen_character_spec     z7test_completion_speculative.<locals>.gen_character_specc                 S   r   r   r	   rD   r   r   r   gen_character_no_spec  r   z:test_completion_speculative.<locals>.gen_character_no_specz vs )r
   r   global_configdefault_backendtoken_usageresetsyncprompt_tokens)r   r   r   usage_with_specusage_with_no_specr   r   r   test_completion_speculative  s   






r   c                  C   s"   t jdddd } |    d S )N   r   c                 S   s|   | t d7 } | t d7 } | t d7 } | t d7 } | t dt jddd d	 t jd
dd d t jddd 7 } d S )Nr%   z2Construct a character within the following format:r   z,Please generate new Name, Birthday and Job.
r   r\   r   ro   r   r   r   r   r(   rD   r   r   r   r     s&   
z<test_chat_completion_speculative.<locals>.gen_character_spec)r
   r   r   )r   r   r   r    test_chat_completion_speculative  s   

r   c                     s   dd fdd} d}t |}tt|}d}d}| || g }g }g }tt|d| D ]}	|||	d	 |||	 d
  |||	 d  q/dd t||D }
ddlj fdd}t	
 }|j|
dddd	d}g }t|D ]\}	}|||	 |d  qyt	
 | }tt|t|k}t	
 }|j|
ddddd}g }t|D ]\}	}|||	 |d  qt	
 | }tt|t|k}td|d| t|| dk sJ tst|| dk sJ 	 ||fS ||fS )z>Benchmark the accuracy of sgl.select on the HellaSwag dataset.c                 S   sD   | | d d | | d  d }|r || | d | | d  7 }|S )Nactivity_labelr   ctxrU   endingslabelr   )linesr   include_answerr!   r   r   r   get_one_example  s    z.test_hellaswag_select.<locals>.get_one_examplec                    s*   d}t |D ]}| | |dd 7 }q|S )Nr   Tr   )r   )r   kr!   r   )r   r   r   get_few_shot_examples  s   z4test_hellaswag_select.<locals>.get_few_shot_exampleszRhttps://raw.githubusercontent.com/rowanz/hellaswag/master/data/hellaswag_val.jsonl      NFr   r   c                 S   s   g | ]	\}}||d qS ))r   choicesr   )r   qcr   r   r   r     s    z)test_hellaswag_select.<locals>.<listcomp>r   c                    s"   |  | 7 } | j d|d7 } d S )Nr   )r   )r;   )r   r   r   )few_shot_examplesr
   r   r   few_shot_hellaswag  s   z1test_hellaswag_select.<locals>.few_shot_hellaswagr0   T)r   num_threadsprogress_bargenerator_styler   z	accuracy=z, accuracy_gen=r   r~   )r   listr   r   r4   appendzipsglangr   timeperf_counterr    	enumerateindexnpmeanarrayrb   abs_is_hip)r   urlfilenamer   num_questions	num_shots	questionsr   labelsr   	argumentsr   ticr"   predsr!   latencyaccuracy	preds_genlatency_genaccuracy_genr   )r   r   r
   r   test_hellaswag_select  sb   
	r  c                     sn   ddl ddlm}  jjj }d\ j fdd}fdd}| |}| }|||d	  dS )
av  
    Validate sgl.gen(min_tokens) functionality.

    The test asks a question where, without a min_tokens constraint, the generated answer is expected to be short.
    By enforcing the min_tokens parameter, we ensure the generated answer has at least the specified number of tokens.
    We verify that the number of tokens in the answer is >= the min_tokens threshold.
    r   N)get_tokenizer)r0      c                    s,   |  d7 } | jd d7 } d S )Nr   r   )
min_tokensr   )r*   r+   r   rD   
MAX_TOKENS
MIN_TOKENSr
   r   r   convo_12  s   
z(test_gen_min_new_tokens.<locals>.convo_1c                    s8   |  |}t| ksJ dt| d  d| d S )Nz
Generated z tokens, min required: z. Text: )encoder4   )	tokenizerr>   	token_ids)r  r   r   assert_min_tokens9  s   
z2test_gen_min_new_tokens.<locals>.assert_min_tokensr   )	r   &sglang.srt.utils.hf_transformers_utilsr  r   r   endpointget_model_namer   r   )r  
model_pathr  r  r  r   r   r  r   test_gen_min_new_tokens$  s   r  )T)!__doc__r_   r   r   numpyr   r   r
   sglang.srt.utilsr   sglang.utilsr   r   r   r$   r6   r@   rH   rg   rk   rt   r}   r   r   r   r   r   r   r   r   r   r  r  r   r   r   r   <module>   s8    "
$
& *Y