Repository navigation
[Bugfix] Improve GLM4 MoE Reasoning Parser's is_reasoning_end Condition #25355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
simon-mo
merged 4 commits into
vllm-project:main
from
frankwang28:fix-glm-is-reasoning-end
Sep 26, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c4e1f4
Update GLM4 MoE is_reasoning_end
frankwang28 5fcc0dc
Update vllm/reasoning/glm4_moe_reasoning_parser.py
frankwang28 e955664
Merge branch 'main' into fix-glm-is-reasoning-end
chaunceyjiang 26de016
Merge branch 'main' into fix-glm-is-reasoning-end
frankwang28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import pytest | ||
| from transformers import AutoTokenizer | ||
|
|
||
| from tests.reasoning.utils import run_reasoning_extraction | ||
| from vllm.reasoning import ReasoningParser, ReasoningParserManager | ||
|
|
||
| parser_name = "glm45" | ||
| start_token = "<think>" | ||
| end_token = "</think>" | ||
|
|
||
| REASONING_MODEL_NAME = "zai-org/GLM-4.5" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def glm45_tokenizer(): | ||
| return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME) | ||
|
|
||
|
|
||
| WITH_THINK = { | ||
| "output": "<think>This is a reasoning section</think>This is the rest", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": True, | ||
| } | ||
|
|
||
| WITH_THINK_STREAM = { | ||
| "output": "<think>This is a reasoning section</think>This is the rest", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": True, | ||
| } | ||
|
|
||
| WITHOUT_THINK = { | ||
| "output": "This is the rest", | ||
| "reasoning_content": None, | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": False, | ||
| } | ||
|
|
||
| WITHOUT_THINK_STREAM = { | ||
| "output": "This is the rest", | ||
| "reasoning_content": None, | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": False, | ||
| } | ||
|
|
||
| COMPLETE_REASONING = { | ||
| "output": "<think>This is a reasoning section</think>", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": None, | ||
| "is_reasoning_end": True, | ||
| } | ||
| MULTILINE_REASONING = { | ||
| "output": | ||
| "<think>This is a reasoning\nsection</think>This is the rest\nThat", | ||
| "reasoning_content": "This is a reasoning\nsection", | ||
| "content": "This is the rest\nThat", | ||
| "is_reasoning_end": True, | ||
| } | ||
| ONLY_OPEN_TAG = { | ||
| "output": "<think>This is a reasoning section", | ||
| "reasoning_content": None, | ||
| "content": "<think>This is a reasoning section", | ||
| "is_reasoning_end": False, | ||
| } | ||
|
|
||
| ONLY_OPEN_TAG_STREAM = { | ||
| "output": "<think>This is a reasoning section", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
|
|
||
| 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", | ||
| ), | ||
| ] | ||
|
|
||
| STILL_REASONING_PROMPT = """[gMASK]<sop><|system|> | ||
| You are a helpful assistant.<|user|> | ||
| What is the capital of France?<|assistant|> | ||
| <think>The user is asking for the capital of""" | ||
|
|
||
| DONE_REASONING_PROMPT = """[gMASK]<sop><|system|> | ||
| You are a helpful assistant.<|user|> | ||
| What is the capital of France?<|assistant|> | ||
| <think>The user is asking for the capital of France.</think> | ||
| The capital of France is Paris.""" | ||
|
|
||
| MULTI_TURN_STILL_REASONING_PROMPT = """[gMASK]<sop><|system|> | ||
| You are a helpful assistant.<|user|> | ||
| What is the capital of France?<|assistant|> | ||
| <think></think> | ||
| The capital of France is Paris.<|user|> | ||
| What about Chile?<|assistant|> | ||
| <think>The user is asking for the capital of""" | ||
|
|
||
| MULTI_TURN_DONE_REASONING_PROMPT = """[gMASK]<sop><|system|> | ||
| You are a helpful assistant.<|user|> | ||
| What is the capital of France?<|assistant|> | ||
| <think></think> | ||
| The capital of France is Paris.<|user|> | ||
| What about Chile?<|assistant|> | ||
| <think>The user is asking for the capital of Chile.</think> | ||
| The capital of Chile is Santiago.""" | ||
|
|
||
| REASONING_END_TEST_CASES = [ | ||
| pytest.param(STILL_REASONING_PROMPT, False, id="still_reasoning"), | ||
| pytest.param(DONE_REASONING_PROMPT, True, id="done_reasoning"), | ||
| pytest.param(MULTI_TURN_STILL_REASONING_PROMPT, | ||
| False, | ||
| id="multi_turn_still_reasoning"), | ||
| pytest.param(MULTI_TURN_DONE_REASONING_PROMPT, | ||
| True, | ||
| id="multi_turn_done_reasoning") | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("streaming, param_dict", TEST_CASES) | ||
| def test_reasoning( | ||
| streaming: bool, | ||
| param_dict: dict, | ||
| glm45_tokenizer, | ||
| ): | ||
| output = glm45_tokenizer.tokenize(param_dict["output"]) | ||
| output_tokens: list[str] = [ | ||
| glm45_tokenizer.convert_tokens_to_string([token]) for token in output | ||
| ] | ||
| parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser( | ||
| parser_name)(glm45_tokenizer) | ||
|
|
||
| reasoning, content = run_reasoning_extraction(parser, | ||
| output_tokens, | ||
| streaming=streaming) | ||
|
|
||
| assert reasoning == param_dict["reasoning_content"] | ||
| assert content == param_dict["content"] | ||
|
|
||
| output_ids = glm45_tokenizer.convert_tokens_to_ids(output) | ||
| is_reasoning_end = parser.is_reasoning_end(output_ids) | ||
| assert is_reasoning_end == param_dict["is_reasoning_end"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("prompt, is_reasoning_end", REASONING_END_TEST_CASES) | ||
| def test_is_reasoning_end_full_prompt(prompt: str, is_reasoning_end: bool, | ||
| glm45_tokenizer): | ||
| parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser( | ||
| parser_name)(glm45_tokenizer) | ||
| tokens = glm45_tokenizer.tokenize(prompt) | ||
| token_ids = glm45_tokenizer.convert_tokens_to_ids(tokens) | ||
| check_is_reasoning_end = parser.is_reasoning_end(token_ids) | ||
| assert check_is_reasoning_end == is_reasoning_end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.