test_torch_compile.py 1.96 KB
Newer Older
1
import time
2
3
4
import unittest
from types import SimpleNamespace

5
6
import requests

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


17
class TestTorchCompile(unittest.TestCase):
18
19
    @classmethod
    def setUpClass(cls):
Ying Sheng's avatar
Ying Sheng committed
20
        cls.model = DEFAULT_MODEL_NAME_FOR_TEST
21
        cls.base_url = DEFAULT_URL_FOR_TEST
22
        cls.process = popen_launch_server(
23
24
25
            cls.model,
            cls.base_url,
            timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
26
            other_args=["--enable-torch-compile", "--cuda-graph-max-bs", "4"],
27
28
29
30
        )

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

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

        metrics = run_eval(args)
43
        self.assertGreaterEqual(metrics["score"], 0.65)
44

45
46
47
48
49
50
51
52
    def run_decode(self, max_new_tokens):
        response = requests.post(
            self.base_url + "/generate",
            json={
                "text": "The capital of France is",
                "sampling_params": {
                    "temperature": 0,
                    "max_new_tokens": max_new_tokens,
53
                    "ignore_eos": True,
54
55
56
57
58
59
                },
            },
        )
        return response.json()

    def test_throughput(self):
60
61
        # Warmup
        res = self.run_decode(16)
62
63
64
65
66

        max_tokens = 256
        tic = time.time()
        res = self.run_decode(max_tokens)
        tok = time.time()
67
        print(f"{res=}")
68
69
        throughput = max_tokens / (tok - tic)
        print(f"Throughput: {throughput} tokens/s")
70
        self.assertGreaterEqual(throughput, 152)
71

72
73

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