test_qwen3_reasoning_parser.py 3.47 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

import pytest
from transformers import AutoTokenizer

from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager

parser_name = "qwen3"
start_token = "<think>"
end_token = "</think>"

REASONING_MODEL_NAME = "Qwen/Qwen3-0.6B"


@pytest.fixture(scope="module")
def qwen3_tokenizer():
    return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)


# 带 <think></think>,非stream
WITH_THINK = {
    "output": "<think>This is a reasoning section</think>This is the rest",
25
    "reasoning": "This is a reasoning section",
26
27
28
29
30
    "content": "This is the rest",
}
# 带 <think></think>,stream
WITH_THINK_STREAM = {
    "output": "<think>This is a reasoning section</think>This is the rest",
31
    "reasoning": "This is a reasoning section",
32
33
34
35
36
    "content": "This is the rest",
}
# 不带 <think></think>,非stream
WITHOUT_THINK = {
    "output": "This is the rest",
37
    "reasoning": None,
38
39
40
41
42
    "content": "This is the rest",
}
# 不带 <think></think>,stream
WITHOUT_THINK_STREAM = {
    "output": "This is the rest",
43
    "reasoning": None,
44
45
46
47
48
    "content": "This is the rest",
}

COMPLETE_REASONING = {
    "output": "<think>This is a reasoning section</think>",
49
    "reasoning": "This is a reasoning section",
50
51
52
    "content": None,
}
MULTILINE_REASONING = {
53
    "output": "<think>This is a reasoning\nsection</think>This is the rest\nThat",
54
    "reasoning": "This is a reasoning\nsection",
55
56
57
58
    "content": "This is the rest\nThat",
}
ONLY_OPEN_TAG = {
    "output": "<think>This is a reasoning section",
59
    "reasoning": None,
60
61
62
63
64
    "content": "<think>This is a reasoning section",
}

ONLY_OPEN_TAG_STREAM = {
    "output": "<think>This is a reasoning section",
65
    "reasoning": "This is a reasoning section",
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    "content": None,
}

TEST_CASES = [
    pytest.param(
        False,
        WITH_THINK,
        id="with_think",
    ),
    pytest.param(
        True,
        WITH_THINK_STREAM,
        id="with_think_stream",
    ),
    pytest.param(
        False,
        WITHOUT_THINK,
        id="without_think",
    ),
    pytest.param(
        True,
        WITHOUT_THINK_STREAM,
        id="without_think_stream",
    ),
    pytest.param(
        False,
        COMPLETE_REASONING,
        id="complete_reasoning",
    ),
    pytest.param(
        True,
        COMPLETE_REASONING,
        id="complete_reasoning_stream",
    ),
    pytest.param(
        False,
        MULTILINE_REASONING,
        id="multiline_reasoning",
    ),
    pytest.param(
        True,
        MULTILINE_REASONING,
        id="multiline_reasoning_stream",
    ),
    pytest.param(
        False,
        ONLY_OPEN_TAG,
        id="only_open_tag",
    ),
    pytest.param(
        True,
        ONLY_OPEN_TAG_STREAM,
        id="only_open_tag_stream",
    ),
]


@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
    streaming: bool,
    param_dict: dict,
    qwen3_tokenizer,
):
    output = qwen3_tokenizer.tokenize(param_dict["output"])
    output_tokens: list[str] = [
        qwen3_tokenizer.convert_tokens_to_string([token]) for token in output
    ]
133
134
135
    parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
        qwen3_tokenizer
    )
136

137
138
139
    reasoning, content = run_reasoning_extraction(
        parser, output_tokens, streaming=streaming
    )
140

141
    assert reasoning == param_dict["reasoning"]
142
    assert content == param_dict["content"]