test_generation_models.py 4.88 KB
Newer Older
1
2
3
4
5
6
7
8
"""
Usage:

To test a specific model:
1. Add it to ALL_OTHER_MODELS
2. Run `ONLY_RUN=Qwen/Qwen2-1.5B python3 -m unittest test_generation_models.TestGenerationModels.test_others`
"""

9
10
11
12
13
"""
Copyright 2023-2024 SGLang Team
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
14

15
    http://www.apache.org/licenses/LICENSE-2.0
16

17
18
19
20
21
22
23
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

24
import dataclasses
25
import multiprocessing as mp
26
import os
27
import unittest
28
from typing import List
29
30
31
32

import torch

from sglang.test.runners import DEFAULT_PROMPTS, HFRunner, SRTRunner
33
from sglang.test.test_utils import calculate_rouge_l, is_in_ci
34

35

36
37
38
39
40
41
42
@dataclasses.dataclass
class ModelCase:
    model_path: str
    tp_size: int = 1
    prefill_tolerance: float = 5e-2
    decode_tolerance: float = 5e-2
    rouge_l_tolerance: float = 1
43
44


45
46
47
48
49
# Popular models that run on CI
CI_MODELS = [
    ModelCase("meta-llama/Meta-Llama-3.1-8B-Instruct"),
    ModelCase("google/gemma-2-2b"),
]
50

51
52
53
54
# All other models
ALL_OTHER_MODELS = [
    ModelCase("Qwen/Qwen2-1.5B"),
]
55

56
TORCH_DTYPES = [torch.float16]
57
58


59
class TestGenerationModels(unittest.TestCase):
60
    def assert_close_logits_and_output_strs(
61
        self,
62
63
64
        prompts: List[str],
        model_case: ModelCase,
        torch_dtype: torch.dtype,
65
    ) -> None:
66
67
68
69
70
71
72
        model_path = model_case.model_path
        prefill_tolerance, decode_tolerance, rouge_l_tolerance = (
            model_case.prefill_tolerance,
            model_case.decode_tolerance,
            model_case.rouge_l_tolerance,
        )
        max_new_tokens = 32
73

74
        with HFRunner(
75
            model_path, torch_dtype=torch_dtype, is_generation=True
76
        ) as hf_runner:
77
            hf_outputs = hf_runner.forward(prompts, max_new_tokens=max_new_tokens)
78
79
80

        with SRTRunner(
            model_path,
81
            tp_size=model_case.tp_size,
82
            torch_dtype=torch_dtype,
83
            is_generation=True,
84
        ) as srt_runner:
85
            srt_outputs = srt_runner.forward(prompts, max_new_tokens=max_new_tokens)
86
87

        for i in range(len(prompts)):
88
            # Compare input logprobs
89
90
            hf_logprobs = torch.Tensor(hf_outputs.top_input_logprobs[i])
            srt_logprobs = torch.Tensor(srt_outputs.top_input_logprobs[i])
91
92
93
94
95
            input_len = hf_logprobs.shape[0]
            print(
                "prefill logprobs max_diff", torch.max(abs(hf_logprobs - srt_logprobs))
            )
            if input_len <= 100:
96
97
98
99
100
                assert torch.all(abs(hf_logprobs - srt_logprobs) < prefill_tolerance), (
                    f"prefill logprobs are not all close with model_path={model_path} prompts={prompts} "
                    f"prefill_tolerance={prefill_tolerance}."
                    f"{hf_logprobs=}, {srt_logprobs=}"
                )
101

102
            # Compare output logprobs
103
104
            hf_logprobs = torch.Tensor(hf_outputs.top_output_logprobs[i])
            srt_logprobs = torch.Tensor(srt_outputs.top_output_logprobs[i])
105

106
            print(
107
                "decode logprobs max_diff", torch.max(abs(hf_logprobs - srt_logprobs))
108
109
            )
            if input_len <= 100:
110
111
112
113
114
                assert torch.all(abs(hf_logprobs - srt_logprobs) < decode_tolerance), (
                    f"decode logprobs are not all close with model_path={model_path} prompts={prompts} "
                    f"decode_tolerance={decode_tolerance}."
                    f"{hf_logprobs=}, {srt_logprobs=}"
                )
115

116
117
118
        # Compare output strings
        print(f"{hf_outputs.output_strs=}")
        print(f"{srt_outputs.output_strs=}")
119
120
121
        rouge_l_scores = calculate_rouge_l(
            hf_outputs.output_strs, srt_outputs.output_strs
        )
122
        print(f"{rouge_l_scores=}")
123
        assert all(
124
125
126
127
128
            score >= rouge_l_tolerance for score in rouge_l_scores
        ), f"Not all ROUGE-L scores are greater than rouge_l_tolerance={rouge_l_tolerance}"

    def test_ci_models(self):
        for model_case in CI_MODELS:
129
            for torch_dtype in TORCH_DTYPES:
130
131
                self.assert_close_logits_and_output_strs(
                    DEFAULT_PROMPTS, model_case, torch_dtype
132
133
                )

134
    def test_others(self):
135
136
137
        if is_in_ci():
            return

138
139
140
141
142
143
144
145
146
        for model_case in ALL_OTHER_MODELS:
            if (
                "ONLY_RUN" in os.environ
                and os.environ["ONLY_RUN"] != model_case.model_path
            ):
                continue
            self.assert_close_logits_and_output_strs(
                DEFAULT_PROMPTS, model_case, torch.float16
            )
147

148

149
150
if __name__ == "__main__":
    mp.set_start_method("spawn")
Mingyi's avatar
Mingyi committed
151
    unittest.main()