test_srt_endpoint.py 3.77 KB
Newer Older
1
2
3
4
"""
python3 -m unittest test_srt_endpoint.TestSRTEndpoint.test_simple_decode
"""

5
6
7
8
9
10
import json
import unittest

import requests

from sglang.srt.utils import kill_child_process
11
12
from sglang.test.test_utils import (
    DEFAULT_MODEL_NAME_FOR_TEST,
13
14
    DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
    DEFAULT_URL_FOR_TEST,
15
16
    popen_launch_server,
)
17
18
19
20
21


class TestSRTEndpoint(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
Ying Sheng's avatar
Ying Sheng committed
22
        cls.model = DEFAULT_MODEL_NAME_FOR_TEST
23
24
25
26
        cls.base_url = DEFAULT_URL_FOR_TEST
        cls.process = popen_launch_server(
            cls.model, cls.base_url, timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH
        )
27
28
29

    @classmethod
    def tearDownClass(cls):
Lianmin Zheng's avatar
Lianmin Zheng committed
30
        kill_child_process(cls.process.pid, include_self=True)
31
32

    def run_decode(
33
34
35
36
37
38
        self,
        return_logprob=False,
        top_logprobs_num=0,
        return_text=False,
        n=1,
        stream=False,
39
40
41
42
43
44
45
    ):
        response = requests.post(
            self.base_url + "/generate",
            json={
                "text": "The capital of France is",
                "sampling_params": {
                    "temperature": 0 if n == 1 else 0.5,
46
                    "max_new_tokens": 16,
47
48
                    "n": n,
                },
49
                "stream": stream,
50
51
52
53
54
55
                "return_logprob": return_logprob,
                "top_logprobs_num": top_logprobs_num,
                "return_text_in_logprobs": return_text,
                "logprob_start_len": 0,
            },
        )
56
57
58
59
60
61
62
        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:]))
63
64

        print(json.dumps(response_json, indent=2))
65
66
67
68
69
70
71
72
        print("=" * 100)

    def test_simple_decode(self):
        self.run_decode()

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

73
74
75
    def test_parallel_sample_stream(self):
        self.run_decode(n=3, stream=True)

76
    def test_logprob(self):
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        self.run_decode(
            return_logprob=True,
            top_logprobs_num=5,
            return_text=True,
        )

    def test_logprob_start_len(self):
        logprob_start_len = 4
        new_tokens = 4
        prompts = [
            "I have a very good idea on",
            "Today is a sunndy day and",
        ]

        response = requests.post(
            self.base_url + "/generate",
            json={
                "text": prompts,
                "sampling_params": {
                    "temperature": 0,
                    "max_new_tokens": new_tokens,
                },
                "return_logprob": True,
                "top_logprobs_num": 5,
                "return_text_in_logprobs": True,
                "logprob_start_len": logprob_start_len,
            },
        )
        response_json = response.json()
        print(json.dumps(response_json, indent=2))

        for i, res in enumerate(response_json):
            assert res["meta_info"]["prompt_tokens"] == logprob_start_len + 1 + len(
                res["meta_info"]["input_token_logprobs"]
            )
            assert prompts[i].endswith(
                "".join([x[-1] for x in res["meta_info"]["input_token_logprobs"]])
            )

            assert res["meta_info"]["completion_tokens"] == new_tokens
            assert len(res["meta_info"]["output_token_logprobs"]) == new_tokens
            res["text"] == "".join(
                [x[-1] for x in res["meta_info"]["output_token_logprobs"]]
            )
121

122
123
124
125
    def test_get_memory_pool_size(self):
        response = requests.post(self.base_url + "/get_memory_pool_size")
        assert isinstance(response.json(), int)

126
127

if __name__ == "__main__":
Lianmin Zheng's avatar
Lianmin Zheng committed
128
    unittest.main()