test_mistral_reasoning_parser.py 8.8 KB
Newer Older
Julien Denize's avatar
Julien Denize committed
1
2
3
4
5
6
7
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import pytest

from tests.reasoning.utils import run_reasoning_extraction_mistral
from vllm.reasoning import ReasoningParser, ReasoningParserManager
8
from vllm.tokenizers import MistralTokenizer
Julien Denize's avatar
Julien Denize committed
9
10
11
12
13
14
15

parser_name = "mistral"


@pytest.fixture(scope="module")
def mistral_tokenizer():
    mistral_tokenizer = MistralTokenizer.from_pretrained(
16
        "mistralai/Magistral-Small-2509"
17
    )
Julien Denize's avatar
Julien Denize committed
18
19
20
21
22
    return mistral_tokenizer


SIMPLE_REASONING = {
    "output": "This is a reasoning section[/THINK]This is the rest",
23
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
24
25
26
27
28
    "content": "This is the rest",
    "is_reasoning_end": True,
}
COMPLETE_REASONING = {
    "output": "This is a reasoning section[/THINK]",
29
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
30
31
32
33
34
    "content": None,
    "is_reasoning_end": True,
}
NO_CONTENT = {
    "output": "This is content",
35
    "reasoning": "This is content",
Julien Denize's avatar
Julien Denize committed
36
37
38
39
40
    "content": None,
    "is_reasoning_end": False,
}
NO_REASONING_STREAMING = {
    "output": "This is a reasoning section",
41
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
42
43
44
45
46
    "content": None,
    "is_reasoning_end": False,
}
MULTIPLE_LINES = {
    "output": "This\nThat[/THINK]This is the rest\nThat",
47
    "reasoning": "This\nThat",
Julien Denize's avatar
Julien Denize committed
48
49
50
51
52
    "content": "This is the rest\nThat",
    "is_reasoning_end": True,
}
SHORTEST_REASONING_NO_STREAMING = {
    "output": "[/THINK]This is the rest",
53
    "reasoning": "",
Julien Denize's avatar
Julien Denize committed
54
55
56
57
58
    "content": "This is the rest",
    "is_reasoning_end": True,
}
SHORTEST_REASONING = {
    "output": "[/THINK]This is the rest",
59
    "reasoning": None,
Julien Denize's avatar
Julien Denize committed
60
61
62
63
64
    "content": "This is the rest",
    "is_reasoning_end": True,
}
REASONING_WITH_THINK = {
    "output": "[THINK]This is a reasoning section[/THINK]This is the rest",
65
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
66
67
68
69
70
    "content": "This is the rest",
    "is_reasoning_end": True,
}
COMPLETE_REASONING_WITH_THINK = {
    "output": "[THINK]This is a reasoning section[/THINK]",
71
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
72
73
74
75
76
    "content": None,
    "is_reasoning_end": True,
}
MULTIPLE_LINES_WITH_THINK = {
    "output": "[THINK]This\nThat[/THINK]This is the rest\nThat",
77
    "reasoning": "This\nThat",
Julien Denize's avatar
Julien Denize committed
78
79
80
81
82
    "content": "This is the rest\nThat",
    "is_reasoning_end": True,
}
SHORTEST_REASONING_NO_STREAMING_WITH_THINK = {
    "output": "[/THINK]This is the rest",
83
    "reasoning": "",
Julien Denize's avatar
Julien Denize committed
84
85
86
87
88
    "content": "This is the rest",
    "is_reasoning_end": True,
}
SHORTEST_REASONING_WITH_THINK = {
    "output": "[/THINK]This is the rest",
89
    "reasoning": None,
Julien Denize's avatar
Julien Denize committed
90
91
92
93
94
    "content": "This is the rest",
    "is_reasoning_end": True,
}
THINK_NO_END = {
    "output": "[THINK]This is a reasoning section",
95
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
96
97
98
99
100
    "content": None,
    "is_reasoning_end": False,
}
EMPTY = {
    "output": "",
101
    "reasoning": "",
Julien Denize's avatar
Julien Denize committed
102
103
104
105
106
    "content": None,
    "is_reasoning_end": False,
}
EMPTY_STREAMING = {
    "output": "",
107
    "reasoning": None,
Julien Denize's avatar
Julien Denize committed
108
109
110
111
112
    "content": None,
    "is_reasoning_end": False,
}
NEW_LINE = {
    "output": "\n[THINK]This is a reasoning section[/THINK]\nThis is the rest",
113
    "reasoning": "This is a reasoning section",
Julien Denize's avatar
Julien Denize committed
114
115
116
117
118
119
120
121
122
    "content": "\nThis is the rest",
    "is_reasoning_end": True,
}
# Streaming cannot handle new lines at the beginning of the output
# because we need to support [THINK]...[/THINK] and [/THINK]...
# We cannot know if the text before [THINK] is reasoning content
# or not.
NEW_LINE_STREAMING = {
    "output": "\n[THINK]This is a reasoning section[/THINK]\nThis is the rest",
123
    "reasoning": "\nThis is a reasoning section",
Julien Denize's avatar
Julien Denize committed
124
125
126
127
128
129
130
131
132
133
134
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    "content": "\nThis is the rest",
    "is_reasoning_end": True,
}

TEST_CASES = [
    pytest.param(
        False,
        SIMPLE_REASONING,
        id="simple_reasoning",
    ),
    pytest.param(
        True,
        SIMPLE_REASONING,
        id="simple_reasoning_streaming",
    ),
    pytest.param(
        False,
        COMPLETE_REASONING,
        id="complete_reasoning",
    ),
    pytest.param(
        True,
        COMPLETE_REASONING,
        id="complete_reasoning_streaming",
    ),
    pytest.param(
        False,
        NO_CONTENT,
        id="no_content_token",
    ),
    pytest.param(
        True,
        NO_REASONING_STREAMING,
        id="no_reasoning_token_streaming",
    ),
    pytest.param(
        False,
        MULTIPLE_LINES,
        id="multiple_lines",
    ),
    pytest.param(
        True,
        MULTIPLE_LINES,
        id="multiple_lines_streaming",
    ),
    pytest.param(
        True,
        SHORTEST_REASONING,
        id="shortest",
    ),
    pytest.param(
        False,
        SHORTEST_REASONING_NO_STREAMING,
        id="shortest_streaming",
    ),
    pytest.param(
        False,
        REASONING_WITH_THINK,
        id="reasoning_with_think",
    ),
    pytest.param(
        True,
        REASONING_WITH_THINK,
        id="reasoning_with_think_streaming",
    ),
    pytest.param(
        False,
        COMPLETE_REASONING_WITH_THINK,
        id="complete_reasoning_with_think",
    ),
    pytest.param(
        True,
        COMPLETE_REASONING_WITH_THINK,
        id="complete_reasoning_with_think_streaming",
    ),
    pytest.param(
        False,
        MULTIPLE_LINES_WITH_THINK,
        id="multiple_lines_with_think",
    ),
    pytest.param(
        True,
        MULTIPLE_LINES_WITH_THINK,
        id="multiple_lines_with_think_streaming",
    ),
    pytest.param(
        False,
        SHORTEST_REASONING_NO_STREAMING_WITH_THINK,
        id="shortest_with_think",
    ),
    pytest.param(
        True,
        SHORTEST_REASONING_WITH_THINK,
        id="shortest_with_think_streaming",
    ),
    pytest.param(
        False,
        THINK_NO_END,
        id="think_no_end",
    ),
    pytest.param(
        True,
        THINK_NO_END,
        id="think_no_end_streaming",
    ),
    pytest.param(
        False,
        EMPTY,
        id="empty",
    ),
    pytest.param(
        True,
        EMPTY_STREAMING,
        id="empty_streaming",
    ),
    pytest.param(
        False,
        NEW_LINE,
        id="new_line",
    ),
    pytest.param(
        True,
        NEW_LINE_STREAMING,
        id="new_line_streaming",
    ),
]


@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_mistral_reasoning(
    streaming: bool,
    param_dict: dict,
    mistral_tokenizer: MistralTokenizer,
):
    output = param_dict["output"]

    index_think = output.find("[THINK]")
    len_think = len("[THINK]")
    index_end_think = output.find("[/THINK]")
    len_end_think = len("[/THINK]")

    # encode everything to tokens ids
    output_tokens = []
    if index_think != -1:
        output_before_think = output[:index_think]
        output_tokens += mistral_tokenizer.tokenizer.encode(
270
271
            output_before_think, False, False
        )
Julien Denize's avatar
Julien Denize committed
272
273
274
        output_tokens += [mistral_tokenizer.instruct.BEGIN_THINK]

        if index_end_think != -1:
275
276
            output_middle = output[index_think + len_think : index_end_think]
            output_after_think = output[index_end_think + len_end_think :]
Julien Denize's avatar
Julien Denize committed
277
            output_tokens += mistral_tokenizer.tokenizer.encode(
278
279
                output_middle, False, False
            )
Julien Denize's avatar
Julien Denize committed
280
281
            output_tokens += [mistral_tokenizer.instruct.END_THINK]
            output_tokens += mistral_tokenizer.tokenizer.encode(
282
283
                output_after_think, False, False
            )
Julien Denize's avatar
Julien Denize committed
284
        else:
285
            output_middle = output[index_think + len_think :]
Julien Denize's avatar
Julien Denize committed
286
            output_tokens += mistral_tokenizer.tokenizer.encode(
287
288
                output_middle, False, False
            )
Julien Denize's avatar
Julien Denize committed
289
290
    elif index_end_think != -1:
        output_before_think = output[:index_end_think]
291
        output_after_think = output[index_end_think + len_end_think :]
Julien Denize's avatar
Julien Denize committed
292
        output_tokens += mistral_tokenizer.tokenizer.encode(
293
294
            output_before_think, False, False
        )
Julien Denize's avatar
Julien Denize committed
295
296
        output_tokens += [mistral_tokenizer.instruct.END_THINK]
        output_tokens += mistral_tokenizer.tokenizer.encode(
297
298
            output_after_think, False, False
        )
Julien Denize's avatar
Julien Denize committed
299
    else:
300
        output_tokens += mistral_tokenizer.tokenizer.encode(output, False, False)
Julien Denize's avatar
Julien Denize committed
301

302
303
304
    parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
        mistral_tokenizer
    )
Julien Denize's avatar
Julien Denize committed
305

306
307
308
    reasoning, content = run_reasoning_extraction_mistral(
        parser, output_tokens, streaming=streaming
    )
Julien Denize's avatar
Julien Denize committed
309

310
    assert reasoning == param_dict["reasoning"]
Julien Denize's avatar
Julien Denize committed
311
312
313
314
315
316
317
318
319
320
    assert content == param_dict["content"]

    # Test is_reasoning_end
    is_reasoning_end = parser.is_reasoning_end(output_tokens)
    assert is_reasoning_end == param_dict["is_reasoning_end"]

    # Test extract_content
    if param_dict["content"] is not None:
        content = parser.extract_content_ids(output_tokens)
        assert content == mistral_tokenizer.tokenizer.encode(
321
322
            param_dict["content"], bos=False, eos=False
        )
Julien Denize's avatar
Julien Denize committed
323
324
325
    else:
        content = parser.extract_content_ids(output_tokens)
        assert content == []