test_vertex_endpoint.py 1.82 KB
Newer Older
1
2
3
4
5
"""
python3 -m unittest test_vertex_endpoint.TestVertexEndpoint.test_vertex_generate
"""

import unittest
6
from http import HTTPStatus
7
8
9
10
11
12
13
14

import requests

from sglang.srt.utils import kill_process_tree
from sglang.test.test_utils import (
    DEFAULT_SMALL_MODEL_NAME_FOR_TEST,
    DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
    DEFAULT_URL_FOR_TEST,
15
    CustomTestCase,
16
17
18
19
    popen_launch_server,
)


20
class TestVertexEndpoint(CustomTestCase):
21
22
23
24
25
26
27
28
    @classmethod
    def setUpClass(cls):
        cls.model = DEFAULT_SMALL_MODEL_NAME_FOR_TEST
        cls.base_url = DEFAULT_URL_FOR_TEST
        cls.process = popen_launch_server(
            cls.model,
            cls.base_url,
            timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
29
            other_args=["--cuda-graph-max-bs", 2],
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        )

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

    def run_generate(self, parameters):
        data = {
            "instances": [
                {"text": "The capital of France is"},
                {"text": "The capital of China is"},
            ],
            "parameters": parameters,
        }
        response = requests.post(self.base_url + "/vertex_generate", json=data)
        response_json = response.json()
        assert len(response_json["predictions"]) == len(data["instances"])
        return response_json

    def test_vertex_generate(self):
        for parameters in [None, {"sampling_params": {"max_new_tokens": 4}}]:
            self.run_generate(parameters)

53
54
55
56
57
58
59
60
61
    def test_vertex_generate_fail(self):
        data = {
            "instances": [
                {"prompt": "The capital of France is"},
            ],
        }
        response = requests.post(self.base_url + "/vertex_generate", json=data)
        assert response.status_code == HTTPStatus.BAD_REQUEST

62
63
64

if __name__ == "__main__":
    unittest.main()