[Bugfix][Reasoning] Nemotron V3: surface reasoning as content when thinking is unterminated (#39091)
Signed-off-by: Andrii Skliar <askliar@nvidia.com> Co-authored-by: Andrii Skliar <askliar@nvidia.com>
This commit is contained in:
co-authored by
Andrii Skliar
parent
77f42d9725
commit
4882fd7632
@@ -8,7 +8,9 @@ import regex as re
|
||||
|
||||
from tests.reasoning.utils import run_reasoning_extraction
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
||||
from vllm.parser.abstract_parser import DelegatingParser
|
||||
from vllm.reasoning import ReasoningParser, ReasoningParserManager
|
||||
from vllm.reasoning.nemotron_v3_reasoning_parser import NemotronV3ReasoningParser
|
||||
|
||||
parser_name = "nemotron_v3"
|
||||
|
||||
@@ -106,7 +108,7 @@ def test_nemotron_v3_reasoning(
|
||||
assert content == param_dict["content"]
|
||||
|
||||
|
||||
def test_nemotron_v3_without_thinking_returns_content(
|
||||
def test_nemotron_v3_without_thinking_moves_into_content(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
|
||||
@@ -124,11 +126,13 @@ def test_nemotron_v3_without_thinking_returns_content(
|
||||
streaming=False,
|
||||
)
|
||||
|
||||
# No real content followed the reasoning, so the trace is moved into
|
||||
# content (reasoning left empty) — matching main's behavior.
|
||||
assert reasoning is None
|
||||
assert content == "This is plain content"
|
||||
|
||||
|
||||
def test_nemotron_v3_force_nonempty_content_returns_content(
|
||||
def test_nemotron_v3_force_nonempty_content_moves_into_content(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
|
||||
@@ -150,6 +154,30 @@ def test_nemotron_v3_force_nonempty_content_returns_content(
|
||||
assert content == "This is plain content"
|
||||
|
||||
|
||||
def test_nemotron_v3_force_nonempty_keeps_real_content(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
# When real content follows the closing tag nothing is promoted: the
|
||||
# content after </think> is returned as-is and reasoning stays separate.
|
||||
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
|
||||
parser = parser_cls(tokenizer)
|
||||
request = ChatCompletionRequest(
|
||||
model="test-model",
|
||||
messages=[],
|
||||
chat_template_kwargs={"force_nonempty_content": True},
|
||||
)
|
||||
|
||||
reasoning, content = run_reasoning_extraction(
|
||||
parser,
|
||||
["<think>reasoning here</think>real answer"],
|
||||
request=request,
|
||||
streaming=False,
|
||||
)
|
||||
|
||||
assert reasoning == "reasoning here"
|
||||
assert content == "real answer"
|
||||
|
||||
|
||||
def test_nemotron_v3_with_thinking_keeps_truncated_reasoning(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
@@ -170,3 +198,91 @@ def test_nemotron_v3_with_thinking_keeps_truncated_reasoning(
|
||||
|
||||
assert reasoning == "This is truncated reasoning"
|
||||
assert content is None
|
||||
|
||||
|
||||
_SPECIAL_TOKEN_IDS = {"<think>": 1, "</think>": 2}
|
||||
|
||||
|
||||
def _token_id(token: str) -> int:
|
||||
# Only the think markers need stable ids; everything else is non-special.
|
||||
return _SPECIAL_TOKEN_IDS.get(token, 0)
|
||||
|
||||
|
||||
def _make_reasoning_parser(tokenizer):
|
||||
class _NemotronParser(DelegatingParser):
|
||||
reasoning_parser_cls = NemotronV3ReasoningParser
|
||||
tool_parser_cls = None
|
||||
|
||||
return _NemotronParser(tokenizer)
|
||||
|
||||
|
||||
def _run_parse_delta(parser, tokenizer, text, request):
|
||||
tokens = tokenizer.tokenize(text)
|
||||
reasoning_parts: list[str] = []
|
||||
content_parts: list[str] = []
|
||||
for i, token in enumerate(tokens):
|
||||
delta = parser.parse_delta(
|
||||
delta_text=token,
|
||||
delta_token_ids=[_token_id(token)],
|
||||
request=request,
|
||||
prompt_token_ids=[] if i == 0 else None,
|
||||
finished=(i == len(tokens) - 1),
|
||||
)
|
||||
if delta is None:
|
||||
continue
|
||||
if delta.reasoning:
|
||||
reasoning_parts.append(delta.reasoning)
|
||||
if delta.content:
|
||||
content_parts.append(delta.content)
|
||||
return "".join(reasoning_parts), "".join(content_parts)
|
||||
|
||||
|
||||
def test_nemotron_v3_streaming_promotes_reasoning_to_content(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
# Model never closes <think>: reasoning streams normally AND is duplicated
|
||||
# into content on the terminal delta.
|
||||
request = ChatCompletionRequest(
|
||||
model="test-model",
|
||||
messages=[],
|
||||
chat_template_kwargs={"force_nonempty_content": True},
|
||||
)
|
||||
parser = _make_reasoning_parser(tokenizer)
|
||||
|
||||
reasoning, content = _run_parse_delta(parser, tokenizer, "<think>4", request)
|
||||
|
||||
assert reasoning == "4"
|
||||
assert content == "4"
|
||||
|
||||
|
||||
def test_nemotron_v3_streaming_no_promotion_with_real_content(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
request = ChatCompletionRequest(
|
||||
model="test-model",
|
||||
messages=[],
|
||||
chat_template_kwargs={"force_nonempty_content": True},
|
||||
)
|
||||
parser = _make_reasoning_parser(tokenizer)
|
||||
|
||||
reasoning, content = _run_parse_delta(
|
||||
parser, tokenizer, "<think>reason</think>real answer", request
|
||||
)
|
||||
|
||||
# Real content followed </think>, so nothing is duplicated.
|
||||
assert reasoning == "reason"
|
||||
assert content == "real answer"
|
||||
|
||||
|
||||
def test_nemotron_v3_streaming_no_promotion_without_opt_in(
|
||||
tokenizer: FakeNemotronTokenizer,
|
||||
):
|
||||
# Without enable_thinking=False / force_nonempty_content the fallback must
|
||||
# stay disabled: the response stays reasoning-only, content empty.
|
||||
request = ChatCompletionRequest(model="test-model", messages=[])
|
||||
parser = _make_reasoning_parser(tokenizer)
|
||||
|
||||
reasoning, content = _run_parse_delta(parser, tokenizer, "<think>4", request)
|
||||
|
||||
assert reasoning == "4"
|
||||
assert content == ""
|
||||
|
||||
Reference in New Issue
Block a user