test_srt_endpoint.py 2.2 KB
Newer Older
1
2
3
4
5
6
import json
import unittest

import requests

from sglang.srt.utils import kill_child_process
7
8
from sglang.test.test_utils import (
    DEFAULT_MODEL_NAME_FOR_TEST,
Yineng Zhang's avatar
Yineng Zhang committed
9
    DEFAULT_URL_FOR_UNIT_TEST,
10
11
    popen_launch_server,
)
12
13
14
15
16


class TestSRTEndpoint(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
Ying Sheng's avatar
Ying Sheng committed
17
        cls.model = DEFAULT_MODEL_NAME_FOR_TEST
Yineng Zhang's avatar
Yineng Zhang committed
18
        cls.base_url = DEFAULT_URL_FOR_UNIT_TEST
19
        cls.process = popen_launch_server(cls.model, cls.base_url, timeout=300)
20
21
22
23
24
25

    @classmethod
    def tearDownClass(cls):
        kill_child_process(cls.process.pid)

    def run_decode(
26
27
28
29
30
31
        self,
        return_logprob=False,
        top_logprobs_num=0,
        return_text=False,
        n=1,
        stream=False,
32
33
34
35
36
37
38
39
40
41
    ):
        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,
                },
42
                "stream": stream,
43
44
45
46
47
48
                "return_logprob": return_logprob,
                "top_logprobs_num": top_logprobs_num,
                "return_text_in_logprobs": return_text,
                "logprob_start_len": 0,
            },
        )
49
50
51
52
53
54
55
56
        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))
57
58
59
60
61
62
63
64
        print("=" * 100)

    def test_simple_decode(self):
        self.run_decode()

    def test_parallel_sample(self):
        self.run_decode(n=3)

65
66
67
    def test_parallel_sample_stream(self):
        self.run_decode(n=3, stream=True)

68
69
70
71
72
73
74
75
76
77
78
    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__":
Lianmin Zheng's avatar
Lianmin Zheng committed
79
    unittest.main()