test_engine_token_ids.py 1.41 KB
Newer Older
1
2
3
4
5
import unittest

from transformers import AutoTokenizer

import sglang as sgl
6
from sglang.test.test_utils import DEFAULT_SMALL_MODEL_NAME_FOR_TEST
7
8
9
10
11


class TestEngineTokenIds(unittest.TestCase):
    def test_token_ids_in_generate(self):
        llm = sgl.Engine(
12
            model_path=DEFAULT_SMALL_MODEL_NAME_FOR_TEST, return_token_ids=True
13
        )
14
        tokenizer = AutoTokenizer.from_pretrained(DEFAULT_SMALL_MODEL_NAME_FOR_TEST)
15
16
17
18
19
20
21

        prompts = [
            "Hello, my name is",
            "The president of the United States is",
            "The capital of France is",
            "The future of AI is",
        ]
22
        sampling_params = {"temperature": 0, "top_p": 0.95}
23
24
25
        outputs = llm.generate(prompts, sampling_params)

        for prompt, output in zip(prompts, outputs):
26
27
28
29
30
31
32
33
34
35
36
            # SGLang's input_ids has a start token, so we remove it for comparison.
            deocode_input = tokenizer.decode(output["input_ids"][1:])
            assert (
                deocode_input in prompt
            ), f"Decode input: {deocode_input} mismatch for: {prompt}"

            # SGLang's output_ids does not have a start token.
            deocode_output = tokenizer.decode(output["output_ids"])
            assert (
                deocode_output in output["text"]
            ), f"Decode output: {deocode_output} mismatch for: {output['text']}"
37
38
39
40
41
42

        llm.shutdown()


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