test_torch_compile.py 1.99 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
    CustomTestCase,
14
15
    popen_launch_server,
)
16
17


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

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

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

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

46
47
48
49
50
51
52
53
    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,
54
                    "ignore_eos": True,
55
56
57
58
59
60
                },
            },
        )
        return response.json()

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

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

73
74

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