test_eagle_infer.py 6.88 KB
Newer Older
1
import random
2
import threading
3
import time
4
import unittest
5
from types import SimpleNamespace
6

7
import requests
8

9
import sglang as sgl
10
from sglang.srt.hf_transformers_utils import get_tokenizer
11
from sglang.srt.utils import kill_process_tree
12
from sglang.test.few_shot_gsm8k import run_eval
13
from sglang.test.test_utils import (
14
15
    DEFAULT_EAGLE_DRAFT_MODEL_FOR_TEST,
    DEFAULT_EAGLE_TARGET_MODEL_FOR_TEST,
16
17
18
19
    DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
    DEFAULT_URL_FOR_TEST,
    popen_launch_server,
)
20
21
22
23
24


class TestEAGLEEngine(unittest.TestCase):

    def test_eagle_accuracy(self):
25
26
        prompt1 = "Today is a sunny day and I like"
        sampling_params1 = {"temperature": 0, "max_new_tokens": 8}
27

28
29
        # Get the reference output
        ref_engine = sgl.Engine(model_path=DEFAULT_EAGLE_TARGET_MODEL_FOR_TEST)
30
        ref_output = ref_engine.generate(prompt1, sampling_params1)["text"]
31
32
        ref_engine.shutdown()

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        # Test cases with different configurations
        configs = [
            # Original config
            {
                "model_path": DEFAULT_EAGLE_TARGET_MODEL_FOR_TEST,
                "speculative_draft_model_path": DEFAULT_EAGLE_DRAFT_MODEL_FOR_TEST,
                "speculative_algorithm": "EAGLE",
                "speculative_num_steps": 5,
                "speculative_eagle_topk": 8,
                "speculative_num_draft_tokens": 64,
                "mem_fraction_static": 0.7,
            },
            # Config with CUDA graph disabled
            {
                "model_path": DEFAULT_EAGLE_TARGET_MODEL_FOR_TEST,
                "speculative_draft_model_path": DEFAULT_EAGLE_DRAFT_MODEL_FOR_TEST,
                "speculative_algorithm": "EAGLE",
                "speculative_num_steps": 5,
                "speculative_eagle_topk": 8,
                "speculative_num_draft_tokens": 64,
                "mem_fraction_static": 0.7,
                "disable_cuda_graph": True,
            },
        ]
57

58
59
60
        for config in configs:
            # Launch EAGLE engine
            engine = sgl.Engine(**config)
61

62
            # Case 1: Test the output of EAGLE engine is the same as normal engine
63
            out1 = engine.generate(prompt1, sampling_params1)["text"]
64
65
            print(f"{out1=}, {ref_output=}")
            self.assertEqual(out1, ref_output)
66

67
            # Case 2: Test the output of EAGLE engine does not contain unexpected EOS
68
69
            prompt2 = "[INST] <<SYS>>\\nYou are a helpful assistant.\\n<</SYS>>\\nToday is a sunny day and I like [/INST]"
            sampling_params2 = {
70
71
72
73
                "temperature": 0,
                "max_new_tokens": 1024,
                "skip_special_tokens": False,
            }
74

75
            tokenizer = get_tokenizer(DEFAULT_EAGLE_TARGET_MODEL_FOR_TEST)
76
            out2 = engine.generate(prompt2, sampling_params2)["text"]
77
78
79
80
81
82
83
84
85
86
87
            print(f"{out2=}")
            tokens = tokenizer.encode(out2, truncation=False)
            assert tokenizer.eos_token_id not in tokens

            # Case 3: Batched prompts
            prompts = [
                "Hello, my name is",
                "The president of the United States is",
                "The capital of France is",
                "The future of AI is",
            ]
88
89
            sampling_params3 = {"temperature": 0, "max_new_tokens": 30}
            outputs = engine.generate(prompts, sampling_params3)
90
91
92
            for prompt, output in zip(prompts, outputs):
                print("===============================")
                print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
93

94
95
            # Shutdown the engine
            engine.shutdown()
96

97

98
99
100
101
102
103
104
105
106
prompts = [
    "[INST] <<SYS>>\\nYou are a helpful assistant.\\n<</SYS>>\\nToday is a sunny day and I like[/INST]"
    '[INST] <<SYS>>\\nYou are a helpful assistant.\\n<</SYS>>\\nWhat are the mental triggers in Jeff Walker\'s Product Launch Formula and "Launch" book?[/INST]',
    "[INST] <<SYS>>\\nYou are a helpful assistant.\\n<</SYS>>\\nSummarize Russell Brunson's Perfect Webinar Script...[/INST]",
    "[INST] <<SYS>>\\nYou are a helpful assistant.\\n<</SYS>>\\nwho are you?[/INST]",
    "[INST] <<SYS>>\\nYou are a helpful assistant.\\n<</SYS>>\\nwhere are you from?[/INST]",
]


107
class TestEAGLEServer(unittest.TestCase):
108
109
110
111
    @classmethod
    def setUpClass(cls):
        cls.base_url = DEFAULT_URL_FOR_TEST
        cls.process = popen_launch_server(
112
            DEFAULT_EAGLE_TARGET_MODEL_FOR_TEST,
113
114
115
116
117
118
            cls.base_url,
            timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
            other_args=[
                "--speculative-algorithm",
                "EAGLE",
                "--speculative-draft-model-path",
119
                DEFAULT_EAGLE_DRAFT_MODEL_FOR_TEST,
120
                "--speculative-num-steps",
121
                "5",
122
                "--speculative-eagle-topk",
123
                "8",
124
                "--speculative-num-draft-tokens",
125
126
127
                "64",
                "--mem-fraction-static",
                "0.7",
128
129
130
131
132
133
134
            ],
        )

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

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    def send_request(self):
        time.sleep(random.uniform(0, 2))
        for prompt in prompts:
            url = self.base_url + "/generate"
            data = {
                "text": prompt,
                "sampling_params": {
                    "temperature": 0,
                    "max_new_tokens": 1024,
                },
            }
            response = requests.post(url, json=data)
            assert response.status_code == 200

    def send_requests_abort(self):
        for prompt in prompts:
            try:
                time.sleep(random.uniform(0, 2))
                url = self.base_url + "/generate"
                data = {
                    "model": "base",
                    "text": prompt,
                    "sampling_params": {
                        "temperature": 0,
                        "max_new_tokens": 1024,
                    },
                }
                # set timeout = 1s,mock disconnected
                requests.post(url, json=data, timeout=1)
            except Exception as e:
                print(e)
                pass

    def test_request_abort(self):
169
        concurrency = 4
170
171
        threads = [
            threading.Thread(target=self.send_request) for _ in range(concurrency)
172
        ] + [
173
            threading.Thread(target=self.send_requests_abort)
174
175
            for _ in range(concurrency)
        ]
176
        for worker in threads:
177
            worker.start()
178
        for p in threads:
179
180
            p.join()

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    def test_gsm8k(self):
        args = SimpleNamespace(
            num_shots=5,
            data_path=None,
            num_questions=200,
            max_new_tokens=512,
            parallel=128,
            host="http://127.0.0.1",
            port=int(self.base_url.split(":")[-1]),
        )
        metrics = run_eval(args)
        print(f"{metrics=}")

        self.assertGreater(metrics["accuracy"], 0.20)

196

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