test_pytorch_sampling_backend.py 2.46 KB
Newer Older
1
2
3
import unittest
from types import SimpleNamespace

4
5
import requests

6
from sglang.srt.utils import kill_process_tree
7
8
9
10
11
from sglang.test.run_eval import run_eval
from sglang.test.test_utils import (
    DEFAULT_MODEL_NAME_FOR_TEST,
    DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
    DEFAULT_URL_FOR_TEST,
12
    CustomTestCase,
13
14
15
16
    popen_launch_server,
)


17
class TestPyTorchSamplingBackend(CustomTestCase):
18
19
20
21
22
23
24
25
    @classmethod
    def setUpClass(cls):
        cls.model = DEFAULT_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,
26
            other_args=["--sampling-backend", "pytorch", "--disable-radix-cache"],
27
28
29
30
        )

    @classmethod
    def tearDownClass(cls):
31
        kill_process_tree(cls.process.pid)
32
33
34
35
36
37
38
39

    def test_mmlu(self):
        args = SimpleNamespace(
            base_url=self.base_url,
            model=self.model,
            eval_name="mmlu",
            num_examples=64,
            num_threads=32,
40
            temperature=0.1,
41
42
43
        )

        metrics = run_eval(args)
Lianmin Zheng's avatar
Lianmin Zheng committed
44
        self.assertGreaterEqual(metrics["score"], 0.65)
45

46
    def test_greedy(self):
47
48
49
50
51
52
53
54

        first_text = None

        # ensure the answer is identical across single response
        for _ in range(5):
            response_single = requests.post(
                self.base_url + "/generate",
                json={
Lianmin Zheng's avatar
Lianmin Zheng committed
55
                    "text": "The capital of Germany is",
56
57
58
59
                    "sampling_params": {
                        "temperature": 0,
                        "max_new_tokens": 32,
                    },
60
                },
61
62
63
64
65
            ).json()
            text = response_single["text"]
            if first_text is None:
                first_text = text

Lianmin Zheng's avatar
Lianmin Zheng committed
66
            self.assertEqual(text, first_text)
67
68
69

        first_text = None

70
71
72
        response_batch = requests.post(
            self.base_url + "/generate",
            json={
Lianmin Zheng's avatar
Lianmin Zheng committed
73
                "text": ["The capital of Germany is"] * 10,
74
75
76
77
78
79
                "sampling_params": {
                    "temperature": 0,
                    "max_new_tokens": 32,
                },
            },
        ).json()
80
81

        # ensure the answer is identical among the batch
82
        for i in range(10):
83
84
85
            text = response_batch[i]["text"]
            if first_text is None:
                first_text = text
Lianmin Zheng's avatar
Lianmin Zheng committed
86
            self.assertEqual(text, first_text)
87

88
89
90

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