Files
sglang/test/srt/test_srt_endpoint.py

80 lines
2.2 KiB
Python
Raw Normal View History

2024-08-01 21:20:17 -07:00
import json
import unittest
import requests
from sglang.srt.utils import kill_child_process
from sglang.test.test_utils import (
DEFAULT_MODEL_NAME_FOR_TEST,
2024-08-13 16:43:23 +08:00
DEFAULT_URL_FOR_UNIT_TEST,
popen_launch_server,
)
2024-08-01 21:20:17 -07:00
class TestSRTEndpoint(unittest.TestCase):
@classmethod
def setUpClass(cls):
2024-08-04 20:51:55 -07:00
cls.model = DEFAULT_MODEL_NAME_FOR_TEST
2024-08-13 16:43:23 +08:00
cls.base_url = DEFAULT_URL_FOR_UNIT_TEST
2024-08-04 16:02:05 -07:00
cls.process = popen_launch_server(cls.model, cls.base_url, timeout=300)
2024-08-01 21:20:17 -07:00
@classmethod
def tearDownClass(cls):
kill_child_process(cls.process.pid)
def run_decode(
self,
return_logprob=False,
top_logprobs_num=0,
return_text=False,
n=1,
stream=False,
2024-08-01 21:20:17 -07:00
):
response = requests.post(
self.base_url + "/generate",
json={
"text": "The capital of France is",
"sampling_params": {
"temperature": 0 if n == 1 else 0.5,
"max_new_tokens": 32,
"n": n,
},
"stream": stream,
2024-08-01 21:20:17 -07:00
"return_logprob": return_logprob,
"top_logprobs_num": top_logprobs_num,
"return_text_in_logprobs": return_text,
"logprob_start_len": 0,
},
)
if not stream:
response_json = response.json()
else:
response_json = []
for line in response.iter_lines():
if line.startswith(b"data: ") and line[6:] != b"[DONE]":
response_json.append(json.loads(line[6:]))
print(json.dumps(response_json))
2024-08-01 21:20:17 -07:00
print("=" * 100)
def test_simple_decode(self):
self.run_decode()
def test_parallel_sample(self):
self.run_decode(n=3)
def test_parallel_sample_stream(self):
self.run_decode(n=3, stream=True)
2024-08-01 21:20:17 -07:00
def test_logprob(self):
for top_logprobs_num in [0, 3]:
for return_text in [True, False]:
self.run_decode(
return_logprob=True,
top_logprobs_num=top_logprobs_num,
return_text=return_text,
)
if __name__ == "__main__":
2024-08-10 15:09:03 -07:00
unittest.main()