[Mini-VLLM] Cut dead subsystems across serving, connectors, parsers
- Remove P/D transfer connectors: nixl, nixl_chat, mooncake x2, hf3fs, moriio, hisparse, example and hidden-states connectors - Reduce weight_transfer to the base API used by model loading - Remove kv_offload tiering, simple_kv_offload, ec cpu/example, snapshot command, engine snapshot server - Remove transformers model fallback, dsv4 tokenizers, dead platforms cpu/tpu/xpu, flashmla third_party, dsa, megamoe/flydsl, dead attn ops, dead layers (short_conv, fused_embed_norm, indexer_topk, relu2 gemma_ar, lightning_attn, minimax_rms_norm), gemm_rs_ar - Keep MoE LoRA op (imported eagerly by punica_gpu); drop fp8 variant - Rebuild parser hubs around Qwen3 adapters only (36 dead adapters) - Remove dead tests with the code they covered Co-authored-by: Hinata <hinata@idihore.id> Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
@@ -1,172 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Citation source resolution and the streamed citation wire shape."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
from vllm.parser.cohere_command import (
|
||||
_melody_citations_to_vllm,
|
||||
_melody_sources_to_vllm,
|
||||
)
|
||||
|
||||
from .utils import make_parser, stream_parser
|
||||
|
||||
|
||||
class TestMelodySourceResolution:
|
||||
"""Pin the parser-side source resolution (see
|
||||
``_melody_sources_to_vllm``). The parser receives melody's numeric
|
||||
``(tool_call_index, tool_result_indices)`` addressing and resolves
|
||||
it against a ``position_to_source`` map handed in by
|
||||
``CohereServingChatV2._apply_cohere_template_kwargs`` -- the same
|
||||
map that would otherwise live in the serving layer's resolver.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _fake_melody_source(bucket: int, indices: list[int]) -> Any:
|
||||
return SimpleNamespace(tool_call_index=bucket, tool_result_indices=indices)
|
||||
|
||||
def test_multi_index_source_fans_out(self):
|
||||
from vllm.entrypoints.cohere.cohere_chat_message import CitationSource
|
||||
|
||||
position_map: dict[tuple[int, int], CitationSource] = {
|
||||
(0, 0): CitationSource(type="document", id="d0", document={"id": "d0"}),
|
||||
(0, 1): CitationSource(type="document", id="d1", document={"id": "d1"}),
|
||||
}
|
||||
raw = [self._fake_melody_source(0, [0, 1])]
|
||||
out = _melody_sources_to_vllm(raw, position_map)
|
||||
assert [s.id for s in out] == ["d0", "d1"]
|
||||
# Verify type / payload were plumbed through, not just the id.
|
||||
assert out[0].type == "document"
|
||||
assert out[0].document == {"id": "d0"}
|
||||
|
||||
def test_unresolvable_position_skipped(self):
|
||||
from vllm.entrypoints.cohere.cohere_chat_message import CitationSource
|
||||
|
||||
position_map: dict[tuple[int, int], CitationSource] = {
|
||||
(0, 0): CitationSource(type="document", id="d0"),
|
||||
}
|
||||
raw = [self._fake_melody_source(9, [0])]
|
||||
assert _melody_sources_to_vllm(raw, position_map) == []
|
||||
|
||||
def test_missing_position_map_drops_all_sources(self):
|
||||
# A parser instance without a position map (parser wired
|
||||
# outside of ``CohereServingChatV2``) can't attribute anything,
|
||||
# so every source is dropped. Callers downstream will see the
|
||||
# citation with empty ``sources`` and drop it entirely.
|
||||
raw = [self._fake_melody_source(0, [0])]
|
||||
assert _melody_sources_to_vllm(raw, None) == []
|
||||
|
||||
def test_citations_pass_through_is_thinking_tag(self):
|
||||
from vllm.entrypoints.cohere.cohere_chat_message import CitationSource
|
||||
|
||||
position_map: dict[tuple[int, int], CitationSource] = {
|
||||
(0, 0): CitationSource(type="document", id="d0"),
|
||||
}
|
||||
raw = [
|
||||
SimpleNamespace(
|
||||
start_index=0,
|
||||
end_index=5,
|
||||
text="hello",
|
||||
is_thinking=True,
|
||||
sources=[self._fake_melody_source(0, [0])],
|
||||
)
|
||||
]
|
||||
out = _melody_citations_to_vllm(raw, position_map)
|
||||
assert out is not None
|
||||
assert out[0].type == "THINKING_CONTENT"
|
||||
assert out[0].sources[0].id == "d0"
|
||||
|
||||
|
||||
class TestParserStreamingEndToEnd:
|
||||
"""End-to-end streaming shape: raw model output text is tokenized
|
||||
with the mock byte-level tokenizer and fed one token at a time
|
||||
through :meth:`CohereCommand3ReasoningParser.extract_reasoning_streaming`.
|
||||
We collect the ordered sequence of ``DeltaMessage``s the parser
|
||||
hands back -- i.e. what the client would receive on the wire --
|
||||
and pin the exact shape.
|
||||
|
||||
Combines the three flavors that reach ``delta``: a thinking block
|
||||
(populates ``reasoning``), a text block (populates ``content``),
|
||||
and a resolved citation (populates ``citations``) referencing
|
||||
``(start, end)`` offsets into the already-streamed content.
|
||||
"""
|
||||
|
||||
_MODEL_OUTPUT = (
|
||||
"<|START_THINKING|>Let me check.<|END_THINKING|>"
|
||||
"<|START_RESPONSE|>The capital is <co>Paris</co: 0:[1]>.<|END_RESPONSE|>"
|
||||
)
|
||||
|
||||
def test_full_wire_stream_shape(self, tokenizer, request_obj):
|
||||
from vllm.entrypoints.cohere.cohere_chat_message import CitationSource
|
||||
from vllm.renderers.cohere import POSITION_TO_SOURCE_KEY
|
||||
|
||||
# Mimic what ``CohereServingChatV2._apply_cohere_template_kwargs``
|
||||
# installs on a request whose tool_call_index=0 tool result at
|
||||
# index 1 is the "France" document.
|
||||
position_to_source = {
|
||||
(0, 1): CitationSource(
|
||||
type="document",
|
||||
id="doc-paris",
|
||||
document={"id": "doc-paris", "title": "France"},
|
||||
),
|
||||
}
|
||||
|
||||
parser = make_parser(
|
||||
tokenizer,
|
||||
"cohere_command3",
|
||||
chat_template_kwargs={POSITION_TO_SOURCE_KEY: position_to_source},
|
||||
)
|
||||
deltas = stream_parser(parser, request_obj, tokenizer, self._MODEL_OUTPUT)
|
||||
|
||||
# -- 1. Reasoning and content stream separately and reach
|
||||
# the wire only as their real payload. Framing tokens
|
||||
# (``<|START_THINKING|>`` etc.) are consumed by the parser's
|
||||
# state machine and never produce a delta.
|
||||
reasoning_stream = [d.reasoning for d in deltas if d.reasoning is not None]
|
||||
content_stream = [d.content for d in deltas if d.content is not None]
|
||||
citation_deltas = [d for d in deltas if getattr(d, "citations", None)]
|
||||
|
||||
assert "".join(reasoning_stream) == "Let me check."
|
||||
assert "".join(content_stream) == "The capital is Paris."
|
||||
|
||||
# -- 2. Exactly one citation reaches the wire, resolved to the
|
||||
# source from ``position_to_source`` (not the raw
|
||||
# ``(bucket, idx)`` coordinates).
|
||||
assert len(citation_deltas) == 1
|
||||
(cite_delta,) = citation_deltas
|
||||
assert cite_delta.citations is not None
|
||||
assert len(cite_delta.citations) == 1
|
||||
cite = cite_delta.citations[0]
|
||||
assert cite.text == "Paris"
|
||||
# Char offsets are into the accumulated ``content``: "The
|
||||
# capital is " is 15 chars, "Paris" is 5.
|
||||
assert cite.start == 15
|
||||
assert cite.end == 20
|
||||
assert len(cite.sources) == 1
|
||||
assert cite.sources[0].id == "doc-paris"
|
||||
assert cite.sources[0].type == "document"
|
||||
|
||||
# -- 3. The citation's ``(start, end)`` offsets point into
|
||||
# bytes that were already emitted as content-deltas earlier
|
||||
# in the stream -- i.e. the citation is anchored to real
|
||||
# already-shipped content.
|
||||
cite_delta_idx = deltas.index(cite_delta)
|
||||
earlier_content = "".join(
|
||||
d.content for d in deltas[:cite_delta_idx] if d.content is not None
|
||||
)
|
||||
assert earlier_content[cite.start : cite.end] == "Paris"
|
||||
|
||||
# Everything after the citation delta is the trailing period.
|
||||
later_content = "".join(
|
||||
d.content for d in deltas[cite_delta_idx + 1 :] if d.content is not None
|
||||
)
|
||||
assert later_content == "."
|
||||
|
||||
# -- 4. Reasoning and content are never mixed on the same
|
||||
# delta: the parser flips modes on the ``<|END_THINKING|>``
|
||||
# boundary and never emits a delta with both fields set.
|
||||
for d in deltas:
|
||||
assert not (d.reasoning is not None and d.content is not None)
|
||||
@@ -1,476 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""``adjust_request``: folding tools and JSON schemas into Cohere structural tags."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections import UserDict
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
from xgrammar.testing import _is_grammar_accept_string
|
||||
|
||||
from vllm.entrypoints.generate.base.protocol import (
|
||||
JsonSchemaResponseFormat,
|
||||
ResponseFormat,
|
||||
StructuralTagResponseFormat,
|
||||
)
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
from vllm.exceptions import VLLMValidationError
|
||||
from vllm.parser.cohere_command import (
|
||||
CohereCommandParser,
|
||||
CohereNormalizedTool,
|
||||
_has_effective_tools,
|
||||
_response_format_type,
|
||||
_schema_dict_from_structured_outputs,
|
||||
collect_tool_schema,
|
||||
convert_schema_to_structural_tags,
|
||||
)
|
||||
from vllm.sampling_params import StructuredOutputsParams
|
||||
|
||||
from .utils import MockCohereTokenizer, make_parser
|
||||
|
||||
SCHEMA_A = {"type": "object", "properties": {"a": {"type": "string"}}}
|
||||
SCHEMA_B = {"type": "object", "properties": {"b": {"type": "number"}}}
|
||||
GET_WEATHER_TOOL = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
NESTED_DEFS_PARAMETERS = {
|
||||
"$defs": {
|
||||
"Location": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {"type": "string"},
|
||||
"country": {"type": "string"},
|
||||
},
|
||||
"required": ["city"],
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"properties": {"location": {"$ref": "#/$defs/Location"}},
|
||||
"required": ["location"],
|
||||
}
|
||||
NESTED_DEFS_TOOL = {
|
||||
"type": "function",
|
||||
"function": {"name": "get_weather", "parameters": NESTED_DEFS_PARAMETERS},
|
||||
}
|
||||
RECURSIVE_DEFS_PARAMETERS = {
|
||||
"$defs": {
|
||||
"Finding": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"note": {"type": "string"},
|
||||
"related": {"type": "array", "items": {"$ref": "#/$defs/Finding"}},
|
||||
},
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"properties": {"finding": {"$ref": "#/$defs/Finding"}},
|
||||
}
|
||||
DRAFT07_DEFINITIONS_PARAMETERS = {
|
||||
"definitions": {
|
||||
"Location": {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"properties": {"location": {"$ref": "#/definitions/Location"}},
|
||||
}
|
||||
VALID_STRUCTURAL_TAG = {
|
||||
"type": "structural_tag",
|
||||
"format": {
|
||||
"type": "triggered_tags",
|
||||
"tags": [
|
||||
{
|
||||
"begin": "<tool>",
|
||||
"content": {"type": "any_text"},
|
||||
"end": "</tool>",
|
||||
}
|
||||
],
|
||||
"triggers": ["<tool>"],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _model_config(arch: str) -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
architecture=arch,
|
||||
architectures=[arch],
|
||||
hf_text_config=SimpleNamespace(architectures=[arch]),
|
||||
)
|
||||
|
||||
|
||||
def _make_chat_request(**kwargs) -> ChatCompletionRequest:
|
||||
data = {"messages": [{"role": "user", "content": "hi"}], "model": "m"}
|
||||
data.update(kwargs)
|
||||
return ChatCompletionRequest.model_validate(data)
|
||||
|
||||
|
||||
def _first_json_schema(tag_json: str) -> dict | None:
|
||||
outer = json.loads(tag_json)
|
||||
for t in (outer.get("format") or {}).get("tags") or []:
|
||||
c = t.get("content") or {}
|
||||
if c.get("type") == "json_schema":
|
||||
js = c.get("json_schema")
|
||||
return js if isinstance(js, dict) else None
|
||||
return None
|
||||
|
||||
|
||||
def _content_types(tag_json: str) -> set[str]:
|
||||
outer = json.loads(tag_json)
|
||||
out: set[str] = set()
|
||||
for t in (outer.get("format") or {}).get("tags") or []:
|
||||
ty = (t.get("content") or {}).get("type")
|
||||
if isinstance(ty, str):
|
||||
out.add(ty)
|
||||
return out
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def parser(tokenizer: MockCohereTokenizer) -> CohereCommandParser:
|
||||
"""Parser configured with a supported Cohere architecture."""
|
||||
return make_parser(
|
||||
tokenizer, "cohere_command4", model_config=_model_config("Cohere2ForCausalLM")
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def parser_no_model_config(tokenizer: MockCohereTokenizer) -> CohereCommandParser:
|
||||
"""Parser with no ``model_config`` (cannot resolve architecture)."""
|
||||
return make_parser(tokenizer, "cohere_command4")
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def parser_unsupported_arch(tokenizer: MockCohereTokenizer) -> CohereCommandParser:
|
||||
"""Parser configured with an architecture that has no structural tag style."""
|
||||
return make_parser(
|
||||
tokenizer, "cohere_command4", model_config=_model_config("LlamaForCausalLM")
|
||||
)
|
||||
|
||||
|
||||
class TestAdjustRequestPassthrough:
|
||||
def test_structured_outputs_structural_tag_not_modified(self, parser) -> None:
|
||||
tag = json.dumps(VALID_STRUCTURAL_TAG)
|
||||
r = _make_chat_request(structured_outputs={"structural_tag": tag})
|
||||
o = parser.adjust_request(r)
|
||||
assert o.structured_outputs.structural_tag == tag
|
||||
|
||||
def test_response_format_structural_tag_short_circuit(self, parser) -> None:
|
||||
# ``ChatCompletionRequest`` validates ``response_format`` as a union;
|
||||
# bare ``{"type": "structural_tag"}`` is invalid (use pydantic model).
|
||||
rf = StructuralTagResponseFormat(
|
||||
type="structural_tag",
|
||||
format=VALID_STRUCTURAL_TAG["format"],
|
||||
)
|
||||
r = _make_chat_request(response_format=rf)
|
||||
o = parser.adjust_request(r)
|
||||
assert _response_format_type(o.response_format) == "structural_tag"
|
||||
assert o.structured_outputs is None
|
||||
|
||||
|
||||
class TestAdjustRequestNoOp:
|
||||
def test_no_schema_no_tools(self, parser) -> None:
|
||||
o = parser.adjust_request(_make_chat_request())
|
||||
assert o.structured_outputs is None
|
||||
assert o.response_format is None
|
||||
|
||||
def test_no_model_config(self, parser_no_model_config) -> None:
|
||||
inner = JsonSchemaResponseFormat(name="n", json_schema=SCHEMA_A)
|
||||
r = _make_chat_request(
|
||||
response_format=ResponseFormat(type="json_schema", json_schema=inner),
|
||||
)
|
||||
o = parser_no_model_config.adjust_request(r)
|
||||
assert o.response_format is not None
|
||||
assert o.structured_outputs is None
|
||||
|
||||
|
||||
class TestAdjustRequestUnsupportedArchitecture:
|
||||
def test_json_schema_raises(self, parser_unsupported_arch) -> None:
|
||||
inner = JsonSchemaResponseFormat(name="n", json_schema=SCHEMA_A)
|
||||
r = _make_chat_request(
|
||||
response_format=ResponseFormat(type="json_schema", json_schema=inner),
|
||||
)
|
||||
with pytest.raises(ValueError, match="does not support"):
|
||||
parser_unsupported_arch.adjust_request(r)
|
||||
|
||||
|
||||
class TestAdjustRequestFoldFromResponseFormat:
|
||||
@pytest.mark.parametrize(
|
||||
"response_format, expected_schema",
|
||||
[
|
||||
pytest.param(
|
||||
ResponseFormat(
|
||||
type="json_schema",
|
||||
json_schema=JsonSchemaResponseFormat(
|
||||
name="n", json_schema=SCHEMA_A
|
||||
),
|
||||
),
|
||||
SCHEMA_A,
|
||||
id="json_schema_pydantic",
|
||||
),
|
||||
pytest.param(
|
||||
{
|
||||
"type": "json_schema",
|
||||
"json_schema": {"name": "n", "schema": SCHEMA_A},
|
||||
},
|
||||
SCHEMA_A,
|
||||
id="json_schema_dict",
|
||||
),
|
||||
pytest.param(
|
||||
{"type": "json_object"},
|
||||
{"type": "object"},
|
||||
id="json_object",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_response_format_cleared(
|
||||
self, parser, response_format, expected_schema
|
||||
) -> None:
|
||||
r = _make_chat_request(response_format=response_format)
|
||||
o = parser.adjust_request(r)
|
||||
assert o.response_format is None
|
||||
assert (
|
||||
_first_json_schema(o.structured_outputs.structural_tag) == expected_schema
|
||||
)
|
||||
|
||||
|
||||
class TestHasEffectiveTools:
|
||||
@pytest.mark.parametrize(
|
||||
"tools, expected",
|
||||
[
|
||||
pytest.param(None, False, id="none"),
|
||||
pytest.param([], False, id="empty_list"),
|
||||
pytest.param(" ", False, id="blank_str"),
|
||||
pytest.param(
|
||||
[{"type": "function", "function": {"name": "f"}}],
|
||||
True,
|
||||
id="non_empty_list",
|
||||
),
|
||||
pytest.param('{"x": 1}', True, id="non_empty_str"),
|
||||
],
|
||||
)
|
||||
def test_has_effective_tools(self, tools, expected) -> None:
|
||||
assert _has_effective_tools(tools) is expected
|
||||
|
||||
def test_convert_schema_json_only_with_empty_tools_list(self) -> None:
|
||||
tag = convert_schema_to_structural_tags(
|
||||
schema=SCHEMA_B,
|
||||
tools=[],
|
||||
model_architecture="Cohere2ForCausalLM",
|
||||
)
|
||||
assert tag is not None
|
||||
assert _first_json_schema(tag) == SCHEMA_B
|
||||
|
||||
|
||||
class TestAdjustRequestFoldFromStructuredOutputs:
|
||||
@pytest.mark.parametrize(
|
||||
"structured_outputs, expected_schema",
|
||||
[
|
||||
pytest.param({"json": SCHEMA_B}, SCHEMA_B, id="json_dict"),
|
||||
pytest.param({"json": json.dumps(SCHEMA_B)}, SCHEMA_B, id="json_string"),
|
||||
pytest.param(
|
||||
{"json_object": True}, {"type": "object"}, id="json_object_flag"
|
||||
),
|
||||
pytest.param(
|
||||
StructuredOutputsParams(json=SCHEMA_B),
|
||||
SCHEMA_B,
|
||||
id="structured_outputs_dataclass",
|
||||
),
|
||||
pytest.param(
|
||||
{"json": {"name": "n", "schema": SCHEMA_A}},
|
||||
SCHEMA_A,
|
||||
id="openai_wrapper_dict_unwrapped",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_structured_outputs_folded(
|
||||
self, parser, structured_outputs, expected_schema
|
||||
) -> None:
|
||||
o = parser.adjust_request(
|
||||
_make_chat_request(structured_outputs=structured_outputs),
|
||||
)
|
||||
assert (
|
||||
_first_json_schema(o.structured_outputs.structural_tag) == expected_schema
|
||||
)
|
||||
|
||||
def test_responses_request_default_empty_tools(self, parser) -> None:
|
||||
"""``ResponsesRequest.tools`` defaults to ``[]``, not ``None``."""
|
||||
r = ResponsesRequest.model_validate(
|
||||
{
|
||||
"input": "hi",
|
||||
"model": "m",
|
||||
"structured_outputs": {"json": SCHEMA_B},
|
||||
}
|
||||
)
|
||||
assert r.tools == []
|
||||
o = parser.adjust_request(r)
|
||||
assert _first_json_schema(o.structured_outputs.structural_tag) == SCHEMA_B
|
||||
|
||||
def test_json_userdict_mapping_unwrapped(self) -> None:
|
||||
inner = {"type": "object", "properties": {"u": {"type": "number"}}}
|
||||
so = StructuredOutputsParams(json=UserDict(inner))
|
||||
assert _schema_dict_from_structured_outputs(so) == inner
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"json_value, match",
|
||||
[
|
||||
pytest.param("{not json}", "valid JSON", id="invalid_json_string"),
|
||||
pytest.param(
|
||||
json.dumps(["a", "b"]), "JSON object", id="non_object_json_string"
|
||||
),
|
||||
pytest.param(" ", "empty", id="empty_json_string"),
|
||||
],
|
||||
)
|
||||
def test_structured_outputs_json_string_raises(
|
||||
self, parser, json_value, match
|
||||
) -> None:
|
||||
with pytest.raises(ValueError, match=match):
|
||||
parser.adjust_request(
|
||||
_make_chat_request(structured_outputs={"json": json_value}),
|
||||
)
|
||||
|
||||
def test_deeply_nested_json_string_rejected(self, parser) -> None:
|
||||
"""json.loads raises RecursionError on a string nested this deeply."""
|
||||
schema = '{"type": "array", "items": ' * 20_000 + "{}" + "}" * 20_000
|
||||
with pytest.raises(VLLMValidationError, match="nested too deeply"):
|
||||
parser.adjust_request(
|
||||
_make_chat_request(structured_outputs={"json": schema}),
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"construct",
|
||||
[
|
||||
pytest.param(
|
||||
lambda: _make_chat_request(structured_outputs={"json": [1, 2, 3]}),
|
||||
id="chat_completion_request",
|
||||
),
|
||||
pytest.param(
|
||||
lambda: StructuredOutputsParams(json=[1, 2, 3]),
|
||||
id="structured_outputs_params",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_json_wrong_type_raises(self, construct) -> None:
|
||||
"""Non-str / non-dict ``json`` fails at Pydantic validation."""
|
||||
with pytest.raises(ValidationError):
|
||||
construct()
|
||||
|
||||
|
||||
class TestAdjustRequestPrecedence:
|
||||
def test_response_format_over_structured_outputs_json(self, parser) -> None:
|
||||
s_rf = {"type": "object", "properties": {"rf": {"type": "string"}}}
|
||||
s_so = {"type": "object", "properties": {"so": {"type": "number"}}}
|
||||
inner = JsonSchemaResponseFormat(name="n", json_schema=s_rf)
|
||||
r = _make_chat_request(
|
||||
response_format=ResponseFormat(type="json_schema", json_schema=inner),
|
||||
structured_outputs={"json": s_so},
|
||||
)
|
||||
o = parser.adjust_request(r)
|
||||
assert _first_json_schema(o.structured_outputs.structural_tag) == s_rf
|
||||
|
||||
|
||||
class TestAdjustRequestTextPlusStructuredOutputs:
|
||||
def test_text_response_format_preserved(self, parser) -> None:
|
||||
sch = {"type": "object", "properties": {"k": {"type": "string"}}}
|
||||
r = _make_chat_request(
|
||||
response_format=ResponseFormat(type="text"),
|
||||
structured_outputs={"json": sch},
|
||||
)
|
||||
o = parser.adjust_request(r)
|
||||
assert o.response_format is not None
|
||||
assert o.response_format.type == "text"
|
||||
assert _first_json_schema(o.structured_outputs.structural_tag) == sch
|
||||
|
||||
|
||||
class TestAdjustRequestTools:
|
||||
def test_tools_only_command_a_grammar(self, parser) -> None:
|
||||
o = parser.adjust_request(
|
||||
_make_chat_request(tools=[GET_WEATHER_TOOL], tool_choice="auto"),
|
||||
)
|
||||
assert "grammar" in _content_types(o.structured_outputs.structural_tag)
|
||||
|
||||
def test_tools_plus_json_schema_both_kinds(self, parser) -> None:
|
||||
inner = JsonSchemaResponseFormat(
|
||||
name="n",
|
||||
json_schema={"type": "object", "properties": {"r": {"type": "string"}}},
|
||||
)
|
||||
r = _make_chat_request(
|
||||
response_format=ResponseFormat(type="json_schema", json_schema=inner),
|
||||
tools=[GET_WEATHER_TOOL],
|
||||
tool_choice="auto",
|
||||
)
|
||||
o = parser.adjust_request(r)
|
||||
types = _content_types(o.structured_outputs.structural_tag)
|
||||
assert "grammar" in types
|
||||
assert "json_schema" in types
|
||||
|
||||
|
||||
class TestCollectToolSchemaDefs:
|
||||
def test_nested_defs_tool_adjust_request(self, parser) -> None:
|
||||
o = parser.adjust_request(
|
||||
_make_chat_request(tools=[NESTED_DEFS_TOOL], tool_choice="auto"),
|
||||
)
|
||||
assert "grammar" in _content_types(o.structured_outputs.structural_tag)
|
||||
|
||||
@staticmethod
|
||||
def _tool_calls_json(name: str, parameters: dict) -> str:
|
||||
return json.dumps(
|
||||
[{"tool_call_id": "0", "tool_name": name, "parameters": parameters}]
|
||||
)
|
||||
|
||||
def test_recursive_defs(self) -> None:
|
||||
grammar = collect_tool_schema(
|
||||
[CohereNormalizedTool(name="report", parameters=RECURSIVE_DEFS_PARAMETERS)]
|
||||
)
|
||||
nested = {"note": "a", "related": [{"note": "b", "related": []}]}
|
||||
assert _is_grammar_accept_string(
|
||||
grammar, self._tool_calls_json("report", {"finding": nested})
|
||||
)
|
||||
bad = {"note": 1, "related": []}
|
||||
assert not _is_grammar_accept_string(
|
||||
grammar, self._tool_calls_json("report", {"finding": bad})
|
||||
)
|
||||
|
||||
def test_draft07_definitions(self) -> None:
|
||||
grammar = collect_tool_schema(
|
||||
[
|
||||
CohereNormalizedTool(
|
||||
name="get_weather", parameters=DRAFT07_DEFINITIONS_PARAMETERS
|
||||
)
|
||||
]
|
||||
)
|
||||
assert _is_grammar_accept_string(
|
||||
grammar,
|
||||
self._tool_calls_json("get_weather", {"location": {"city": "Prague"}}),
|
||||
)
|
||||
assert not _is_grammar_accept_string(
|
||||
grammar,
|
||||
self._tool_calls_json("get_weather", {"location": {"city": 1}}),
|
||||
)
|
||||
|
||||
def test_deeply_nested_parameters_rejected(self) -> None:
|
||||
"""Tool schemas are compiled here, before request validation, and end up
|
||||
inside an EBNF grammar that the nesting check cannot see later."""
|
||||
parameters = json.loads('{"type": "array", "items": ' * 200 + "{}" + "}" * 200)
|
||||
with pytest.raises(VLLMValidationError, match="nested too deeply"):
|
||||
collect_tool_schema(
|
||||
[CohereNormalizedTool(name="deep", parameters=parameters)]
|
||||
)
|
||||
|
||||
def test_input_parameters_not_mutated(self) -> None:
|
||||
params = json.loads(json.dumps(NESTED_DEFS_PARAMETERS))
|
||||
collect_tool_schema(
|
||||
[CohereNormalizedTool(name="get_weather", parameters=params)]
|
||||
)
|
||||
assert params == NESTED_DEFS_PARAMETERS
|
||||
@@ -1,211 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Replay tests for DelegatingParser with engine adapters.
|
||||
|
||||
Exercises DelegatingParser in engine-adapter mode to verify that delegated
|
||||
routing produces correct output across chunk sizes.
|
||||
See test_replay.py for tests that target engine parsers directly.
|
||||
|
||||
Parser discovery is automatic: any engine parser in ``registered_adapters``
|
||||
that has both tool and reasoning adapters and a builder in
|
||||
``trace_builder._BUILDERS`` is picked up with zero manual wiring.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import NamedTuple
|
||||
|
||||
import pytest
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
from tests.parser.engine.replay_harness import (
|
||||
CHUNK_SIZES,
|
||||
DUMMY_TOOLS,
|
||||
MockTokenizer,
|
||||
_test_request,
|
||||
assert_no_terminal_leakage,
|
||||
assert_parse_output,
|
||||
collect_output,
|
||||
make_mock_tokenizer,
|
||||
parse_non_streaming,
|
||||
replay_streaming,
|
||||
)
|
||||
from tests.parser.engine.trace_builder import _BUILDERS, build_samples
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionToolsParam,
|
||||
)
|
||||
from vllm.parser.abstract_parser import DelegatingParser, Parser
|
||||
from vllm.parser.engine import registered_adapters as _adapters_mod
|
||||
from vllm.parser.engine.adapters import (
|
||||
ParserEngineReasoningAdapter,
|
||||
ParserEngineToolAdapter,
|
||||
)
|
||||
from vllm.parser.mistral import MistralParser
|
||||
|
||||
_TOOLS_VALIDATOR = TypeAdapter(list[ChatCompletionToolsParam])
|
||||
|
||||
# ── Pairing discovery ────────────────────────────────────────────────
|
||||
|
||||
|
||||
class _PairingInfo(NamedTuple):
|
||||
parser_cls: type[Parser]
|
||||
name: str
|
||||
samples: tuple
|
||||
|
||||
|
||||
def _discover_pairings() -> list[_PairingInfo]:
|
||||
"""Discover valid delegating pairings from registered engine adapters.
|
||||
|
||||
Groups tool and reasoning adapters by their engine class, then builds
|
||||
a DelegatingParser subclass for each engine that has both adapters
|
||||
and a test builder.
|
||||
"""
|
||||
bare_tok = MockTokenizer(vocab={}, tokens=[])
|
||||
engines: dict[type, dict[str, type]] = {}
|
||||
for obj in vars(_adapters_mod).values():
|
||||
if not isinstance(obj, type):
|
||||
continue
|
||||
if (
|
||||
issubclass(obj, ParserEngineToolAdapter)
|
||||
and obj is not ParserEngineToolAdapter
|
||||
):
|
||||
tool_adapter: type[ParserEngineToolAdapter] = obj
|
||||
engines.setdefault(tool_adapter._parser_engine_cls, {})["tool"] = obj
|
||||
elif (
|
||||
issubclass(obj, ParserEngineReasoningAdapter)
|
||||
and obj is not ParserEngineReasoningAdapter
|
||||
):
|
||||
reasoning_adapter: type[ParserEngineReasoningAdapter] = obj
|
||||
engines.setdefault(reasoning_adapter._parser_engine_cls, {})[
|
||||
"reasoning"
|
||||
] = obj
|
||||
|
||||
found: list[_PairingInfo] = []
|
||||
missing_builders: list[str] = []
|
||||
for engine_cls, adapters in engines.items():
|
||||
if "tool" not in adapters or "reasoning" not in adapters:
|
||||
continue
|
||||
if engine_cls is MistralParser:
|
||||
# Mistral uses brace-balanced JSON tool args with no TOOL_END
|
||||
# token, so it does not fit this TOOL_END-based replay harness.
|
||||
# It is covered by tests/parser/mistral/ instead.
|
||||
continue
|
||||
cfg = engine_cls(bare_tok, None).parser_engine_config
|
||||
if cfg.name not in _BUILDERS:
|
||||
missing_builders.append(f"{engine_cls.__name__} (config.name={cfg.name!r})")
|
||||
continue
|
||||
if cfg.name == "inkling":
|
||||
# Inkling uses typed structural blocks and opts out of token-id
|
||||
# terminal matching; combined-parser replay coverage lives in
|
||||
# test_inkling.py.
|
||||
continue
|
||||
if cfg.name == "granite":
|
||||
# Granite has a JSON-array tool body with no TOOL_END terminal;
|
||||
# its replay coverage lives in test_granite.py.
|
||||
continue
|
||||
|
||||
parser_cls = type(
|
||||
f"_Delegating{engine_cls.__name__}",
|
||||
(DelegatingParser,),
|
||||
{
|
||||
"reasoning_parser_cls": adapters["reasoning"],
|
||||
"tool_parser_cls": adapters["tool"],
|
||||
},
|
||||
)
|
||||
found.append(
|
||||
_PairingInfo(
|
||||
parser_cls=parser_cls,
|
||||
name=cfg.name,
|
||||
samples=build_samples(cfg.name),
|
||||
)
|
||||
)
|
||||
if missing_builders:
|
||||
raise RuntimeError(
|
||||
f"Engine adapters in registered_adapters have no test builder "
|
||||
f"in trace_builder._BUILDERS: {', '.join(missing_builders)}. "
|
||||
f"Add a builder to _BUILDERS for each new parser."
|
||||
)
|
||||
found.sort(key=lambda p: p.name)
|
||||
return found
|
||||
|
||||
|
||||
_PAIRINGS = _discover_pairings()
|
||||
|
||||
_ALL_SAMPLES = [(p.parser_cls, s) for p in _PAIRINGS for s in p.samples]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("chunk_size", CHUNK_SIZES, ids=lambda c: f"chunk={c}")
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample",
|
||||
_ALL_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else "",
|
||||
)
|
||||
def test_delegating_replay(parser_cls, sample, chunk_size):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
validated_tools = (
|
||||
_TOOLS_VALIDATOR.validate_python(sample.tools) if sample.tools else None
|
||||
)
|
||||
parser = parser_cls(
|
||||
tokenizer,
|
||||
validated_tools,
|
||||
chat_template_kwargs=sample.chat_template_kwargs,
|
||||
)
|
||||
|
||||
deltas = replay_streaming(
|
||||
parser,
|
||||
sample.tokens,
|
||||
chunk_size=chunk_size,
|
||||
finished_on_last=True,
|
||||
tools=sample.tools,
|
||||
prompt_token_ids=sample.prompt_token_ids,
|
||||
)
|
||||
output = collect_output(deltas)
|
||||
assert_parse_output(output, sample)
|
||||
|
||||
|
||||
_TOOL_CALL_SAMPLES = [
|
||||
(p.parser_cls, p.name, s)
|
||||
for p in _PAIRINGS
|
||||
for s in p.samples
|
||||
if s.expected_tool_calls
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,parser_name,sample",
|
||||
_TOOL_CALL_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else "",
|
||||
)
|
||||
def test_delegating_parse_tool_choice_none(parser_cls, parser_name, sample):
|
||||
"""Non-streaming parse() with tool_choice='none' via DelegatingParser
|
||||
must not leak special tokens into content."""
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
validated_tools = (
|
||||
_TOOLS_VALIDATOR.validate_python(sample.tools) if sample.tools else None
|
||||
)
|
||||
parser = parser_cls(
|
||||
tokenizer,
|
||||
validated_tools,
|
||||
chat_template_kwargs=sample.chat_template_kwargs,
|
||||
)
|
||||
|
||||
request = _test_request(tools=DUMMY_TOOLS)
|
||||
request.tool_choice = "none"
|
||||
|
||||
output = parse_non_streaming(parser, sample, request)
|
||||
|
||||
assert output.tool_calls == [], (
|
||||
f"Expected no tool calls but got {output.tool_calls}"
|
||||
)
|
||||
|
||||
cfg = parser._tool_parser._parser_engine.parser_engine_config
|
||||
terminals = sorted(
|
||||
v
|
||||
for v in cfg.terminal_literals | set(cfg.token_id_terminals.values())
|
||||
if len(v) > 1
|
||||
)
|
||||
assert_no_terminal_leakage(
|
||||
output,
|
||||
terminals,
|
||||
context=f"parser={parser_name}",
|
||||
)
|
||||
@@ -1,632 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Replay tests for engine parsers (holdback, skip-tool-parsing, adapters).
|
||||
|
||||
Replays dynamically built token sequences at different chunk sizes and
|
||||
holdback depths to verify chunk-size invariance and terminal-token hygiene.
|
||||
|
||||
Parser discovery is automatic: any ``ParserEngine`` subclass registered in
|
||||
``registered_adapters`` that also has a builder in ``trace_builder._BUILDERS``
|
||||
is picked up with zero manual wiring.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
from typing import NamedTuple
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.parser.engine.replay_harness import (
|
||||
DUMMY_TOOLS,
|
||||
MockTokenizer,
|
||||
Sample,
|
||||
_test_request,
|
||||
assert_no_terminal_leakage,
|
||||
assert_parse_output,
|
||||
collect_output,
|
||||
make_mock_tokenizer,
|
||||
parse_non_streaming,
|
||||
replay_streaming,
|
||||
replay_with_text_holdback,
|
||||
)
|
||||
from tests.parser.engine.trace_builder import _BUILDERS, build_samples
|
||||
from vllm.parser.engine import registered_adapters as _adapters_mod
|
||||
from vllm.parser.engine.parser_engine import ParserEngine
|
||||
from vllm.parser.engine.parser_engine_config import ParserState
|
||||
from vllm.parser.mistral import MistralParser
|
||||
|
||||
# ── Parser discovery ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
class _ParserInfo(NamedTuple):
|
||||
parser_cls: type[ParserEngine]
|
||||
name: str
|
||||
samples: tuple
|
||||
terminals: list[str]
|
||||
tool_end: str
|
||||
think_end: str
|
||||
tool_start: str
|
||||
|
||||
|
||||
def _discover_parsers() -> list[_ParserInfo]:
|
||||
"""Discover engine parsers from registered_adapters that have test builders.
|
||||
|
||||
Returns one ``_ParserInfo`` per parser, sorted by config name.
|
||||
Raises ``RuntimeError`` if any registered parser lacks a builder.
|
||||
"""
|
||||
bare_tok = MockTokenizer(vocab={}, tokens=[])
|
||||
found: list[_ParserInfo] = []
|
||||
missing_builders: list[str] = []
|
||||
for obj in vars(_adapters_mod).values():
|
||||
if not (
|
||||
isinstance(obj, type)
|
||||
and issubclass(obj, ParserEngine)
|
||||
and obj is not ParserEngine
|
||||
):
|
||||
continue
|
||||
if obj is MistralParser:
|
||||
# Mistral uses brace-balanced JSON tool args with no TOOL_END
|
||||
# token, so it does not fit this TOOL_END-based replay harness.
|
||||
# It is covered by tests/parser/mistral/ instead.
|
||||
continue
|
||||
cfg = obj(bare_tok, None).parser_engine_config
|
||||
if cfg.name not in _BUILDERS:
|
||||
missing_builders.append(f"{obj.__name__} (config.name={cfg.name!r})")
|
||||
continue
|
||||
if cfg.name in ("inkling", "plamo3", "minimax_m3"):
|
||||
# Inkling uses typed structural blocks. PLaMo and MiniMax M3
|
||||
# markers can span several token IDs. None fits this TOOL_END-token
|
||||
# harness; their direct and delegating coverage lives in dedicated
|
||||
# tests.
|
||||
continue
|
||||
if cfg.name == "granite":
|
||||
# Granite has a JSON-array tool body with no TOOL_END terminal, so
|
||||
# it does not fit this token-terminal harness; its replay coverage
|
||||
# lives in test_granite.py.
|
||||
continue
|
||||
tool_end = cfg.token_id_terminals.get("TOOL_END")
|
||||
if not tool_end:
|
||||
raise RuntimeError(
|
||||
f"{obj.__name__} config missing 'TOOL_END' in token_id_terminals"
|
||||
)
|
||||
all_vals = cfg.terminal_literals | set(cfg.token_id_terminals.values())
|
||||
found.append(
|
||||
_ParserInfo(
|
||||
parser_cls=obj,
|
||||
name=cfg.name,
|
||||
samples=build_samples(cfg.name),
|
||||
terminals=sorted(v for v in all_vals if len(v) > 1),
|
||||
tool_end=tool_end,
|
||||
think_end=cfg.terminal_literal("THINK_END") or "",
|
||||
tool_start=(
|
||||
cfg.terminal_literal("TOOL_SECTION_START")
|
||||
if (ParserState.CONTENT, "TOOL_SECTION_START") in cfg.transitions
|
||||
else cfg.terminal_literal("TOOL_START")
|
||||
)
|
||||
or "",
|
||||
)
|
||||
)
|
||||
if missing_builders:
|
||||
raise RuntimeError(
|
||||
f"Engine parsers in registered_adapters have no test builder "
|
||||
f"in trace_builder._BUILDERS: {', '.join(missing_builders)}. "
|
||||
f"Add a builder to _BUILDERS for each new parser."
|
||||
)
|
||||
found.sort(key=lambda p: p.name)
|
||||
return found
|
||||
|
||||
|
||||
_PARSERS = _discover_parsers()
|
||||
|
||||
|
||||
def _make_parser(parser_cls: type[ParserEngine], tokenizer, sample: Sample, **extra):
|
||||
kwargs = dict(extra)
|
||||
if sample.chat_template_kwargs:
|
||||
kwargs["chat_template_kwargs"] = sample.chat_template_kwargs
|
||||
return parser_cls(tokenizer, sample.tools, **kwargs)
|
||||
|
||||
|
||||
_ENGINE_PARSERS: dict[str, type[ParserEngine]] = {
|
||||
f"{p.name}_engine": p.parser_cls for p in _PARSERS
|
||||
}
|
||||
|
||||
# ── Parametrize sample lists ─────────────────────────────────────────
|
||||
|
||||
HOLDBACK_CONFIGS = [6, 12, 24]
|
||||
|
||||
_REPLAY_SAMPLES = [(p.parser_cls, s, p.terminals) for p in _PARSERS for s in p.samples]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("holdback", HOLDBACK_CONFIGS, ids=lambda h: f"holdback{h}")
|
||||
@pytest.mark.parametrize("chunk_size", [3, 5, 10], ids=lambda c: f"chunk{c}")
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample,terminals",
|
||||
_REPLAY_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else "",
|
||||
)
|
||||
class TestReplayWithHoldback:
|
||||
"""Replay all parsers with simulated detokenizer holdback."""
|
||||
|
||||
def test_replay(self, parser_cls, sample, terminals, chunk_size, holdback):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
parser = _make_parser(parser_cls, tokenizer, sample)
|
||||
deltas = replay_streaming(
|
||||
parser,
|
||||
sample.tokens,
|
||||
chunk_size=chunk_size,
|
||||
holdback_chars=holdback,
|
||||
prompt_token_ids=sample.prompt_token_ids,
|
||||
)
|
||||
output = collect_output(deltas)
|
||||
|
||||
assert_parse_output(output, sample)
|
||||
assert_no_terminal_leakage(
|
||||
output,
|
||||
terminals,
|
||||
context=f"chunk_size={chunk_size}, holdback={holdback}",
|
||||
)
|
||||
|
||||
|
||||
TEXT_HOLDBACK_DELAYS = [1, 2, 3]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("delay", TEXT_HOLDBACK_DELAYS, ids=lambda d: f"delay{d}")
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample,terminals",
|
||||
_REPLAY_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else "",
|
||||
)
|
||||
class TestTextHoldback:
|
||||
"""Replay with production-like text/token-ID misalignment.
|
||||
|
||||
In production the detokenizer sends token IDs immediately but holds
|
||||
back text by N tokens. This exercises the TokenIDScanner deferred
|
||||
terminal path that aligned-holdback tests do not cover.
|
||||
"""
|
||||
|
||||
def test_replay(self, parser_cls, sample, terminals, delay):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
parser = _make_parser(parser_cls, tokenizer, sample)
|
||||
deltas = replay_with_text_holdback(
|
||||
parser,
|
||||
sample.tokens,
|
||||
text_delay=delay,
|
||||
prompt_token_ids=sample.prompt_token_ids,
|
||||
)
|
||||
output = collect_output(deltas)
|
||||
|
||||
assert_parse_output(output, sample)
|
||||
assert_no_terminal_leakage(
|
||||
output,
|
||||
terminals,
|
||||
context=f"text_delay={delay}",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"chunk_size", [1, 2, 3, 5, 10, 19, 20, None], ids=lambda c: f"chunk{c}"
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample,terminals",
|
||||
_REPLAY_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else "",
|
||||
)
|
||||
class TestReplay:
|
||||
"""Replay all parsers at varied chunk sizes without holdback."""
|
||||
|
||||
def test_replay(self, parser_cls, sample, terminals, chunk_size):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
parser = _make_parser(parser_cls, tokenizer, sample)
|
||||
deltas = replay_streaming(
|
||||
parser,
|
||||
sample.tokens,
|
||||
chunk_size=chunk_size,
|
||||
prompt_token_ids=sample.prompt_token_ids,
|
||||
)
|
||||
output = collect_output(deltas)
|
||||
|
||||
assert_parse_output(output, sample)
|
||||
assert_no_terminal_leakage(output, terminals)
|
||||
|
||||
|
||||
_DEFERRAL_SAMPLES = [
|
||||
(p.parser_cls, s, p.tool_end)
|
||||
for p in _PARSERS
|
||||
for s in p.samples
|
||||
if s.expected_tool_calls
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample,tool_end_text",
|
||||
_DEFERRAL_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else getattr(v, "__name__", ""),
|
||||
)
|
||||
class TestDeferralFinish:
|
||||
"""Test that parse_delta(finished=True) resolves deferred scanner state.
|
||||
|
||||
Simulates a production failure where delta_text is missing the
|
||||
tool-call-end text but delta_token_ids has the token, causing the
|
||||
scanner to defer it. Without finish(), the deferred state is lost
|
||||
and tool call arguments are empty.
|
||||
"""
|
||||
|
||||
def test_misaligned_last_delta_with_finish(self, parser_cls, sample, tool_end_text):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
parser = _make_parser(parser_cls, tokenizer, sample)
|
||||
|
||||
request = _test_request()
|
||||
|
||||
all_ids = [tid for tid, _ in sample.tokens]
|
||||
all_texts = [text for _, text in sample.tokens]
|
||||
|
||||
tool_end_id = sample.vocab.get(tool_end_text)
|
||||
split_idx = None
|
||||
for i in range(len(all_ids) - 1, -1, -1):
|
||||
if all_ids[i] == tool_end_id:
|
||||
split_idx = i
|
||||
break
|
||||
|
||||
if split_idx is None:
|
||||
pytest.skip(f"no {tool_end_text} token found")
|
||||
|
||||
first_ids = all_ids[:split_idx]
|
||||
first_text = "".join(all_texts[:split_idx])
|
||||
|
||||
last_ids = all_ids[split_idx:]
|
||||
last_text_missing = "".join(all_texts[split_idx:]).replace(tool_end_text, "")
|
||||
|
||||
result1 = parser.parse_delta(
|
||||
first_text,
|
||||
first_ids,
|
||||
request,
|
||||
prompt_token_ids=[],
|
||||
finished=False,
|
||||
)
|
||||
result2 = parser.parse_delta(
|
||||
last_text_missing, last_ids, request, finished=True
|
||||
)
|
||||
|
||||
output = collect_output([result1, result2])
|
||||
|
||||
tool_calls_only = dataclasses.replace(
|
||||
sample, expected_reasoning=None, expected_content=None
|
||||
)
|
||||
assert_parse_output(output, tool_calls_only)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample",
|
||||
[(p.parser_cls, p.samples[0]) for p in _PARSERS],
|
||||
ids=[p.name for p in _PARSERS],
|
||||
)
|
||||
class TestParserEngineAdjustRequest:
|
||||
"""Verify ParserEngine and its adapters set skip_special_tokens=False."""
|
||||
|
||||
def test_adjust_request_disables_skip_special_tokens(self, parser_cls, sample):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
parser = parser_cls(tokenizer, sample.tools)
|
||||
request = _test_request()
|
||||
assert request.skip_special_tokens is True
|
||||
adjusted = parser.adjust_request(request)
|
||||
assert adjusted.skip_special_tokens is False
|
||||
|
||||
|
||||
_TOOL_CALL_SAMPLES = [
|
||||
(p.parser_cls, s, p.think_end, p.tool_start)
|
||||
for p in _PARSERS
|
||||
for s in p.samples
|
||||
if s.expected_tool_calls
|
||||
and s.expected_reasoning
|
||||
# Inkling's typed-block format (structural role/kind tokens, shared
|
||||
# block-end token) doesn't fit the generic reasoning/tool split
|
||||
# below; its filtering modes are covered in test_inkling.py instead.
|
||||
and p.name != "inkling"
|
||||
]
|
||||
|
||||
|
||||
def _tool_suppression_expectations(
|
||||
sample, think_end: str, tool_start: str, *, include_tool_block: bool
|
||||
) -> tuple[str, str]:
|
||||
reasoning, content = _raw_tool_suppression_expectations(
|
||||
sample, think_end, tool_start, include_tool_block=include_tool_block
|
||||
)
|
||||
if sample.content_lstrip:
|
||||
content = content.lstrip(sample.content_lstrip)
|
||||
return reasoning, content
|
||||
|
||||
|
||||
def _raw_tool_suppression_expectations(
|
||||
sample, think_end: str, tool_start: str, *, include_tool_block: bool
|
||||
) -> tuple[str, str]:
|
||||
"""Expected (reasoning, content) from the raw token text.
|
||||
|
||||
With ``include_tool_block=True`` (skip_tool_parsing / reasoning
|
||||
adapter first pass), tool terminal text is preserved as content so
|
||||
a second-pass parser can see it.
|
||||
|
||||
With ``include_tool_block=False`` (_suppress_tool_calls /
|
||||
tool_choice='none'), the state machine consumes tool blocks and
|
||||
only non-tool content survives.
|
||||
"""
|
||||
full_text = "".join(text for _, text in sample.tokens)
|
||||
reasoning = sample.expected_reasoning
|
||||
idx = full_text.find(reasoning)
|
||||
if idx < 0:
|
||||
return (full_text, "")
|
||||
after_reasoning = full_text[idx + len(reasoning) :]
|
||||
if think_end:
|
||||
pos = after_reasoning.find(think_end)
|
||||
if pos >= 0:
|
||||
if include_tool_block:
|
||||
return (reasoning, after_reasoning[pos + len(think_end) :])
|
||||
after_reasoning = after_reasoning[pos + len(think_end) :]
|
||||
if tool_start:
|
||||
pos = after_reasoning.find(tool_start)
|
||||
if pos >= 0:
|
||||
if include_tool_block:
|
||||
return (reasoning, after_reasoning[pos:])
|
||||
return (reasoning, after_reasoning[:pos])
|
||||
if include_tool_block:
|
||||
return (full_text, "")
|
||||
return (reasoning, after_reasoning)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("chunk_size", [1, 5, None], ids=lambda c: f"chunk{c}")
|
||||
@pytest.mark.parametrize(
|
||||
"mode",
|
||||
["skip_tool_parsing", "suppress_tool_calls"],
|
||||
ids=["skip_tool_parsing", "suppress_tool_calls"],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample,think_end,tool_start",
|
||||
_TOOL_CALL_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else getattr(v, "__name__", ""),
|
||||
)
|
||||
class TestToolCallFilteringReplay:
|
||||
"""Replay with tool calls not extracted, in both filtering modes.
|
||||
|
||||
``skip_tool_parsing`` (reasoning adapter first pass): tool terminal
|
||||
text is preserved as content for a second-pass tool parser.
|
||||
|
||||
``suppress_tool_calls`` (tool_choice='none'): tool call blocks are
|
||||
consumed by the state machine and do not leak into content.
|
||||
"""
|
||||
|
||||
def test_replay(self, parser_cls, sample, think_end, tool_start, mode, chunk_size):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
kwargs = {}
|
||||
if sample.chat_template_kwargs:
|
||||
kwargs["chat_template_kwargs"] = sample.chat_template_kwargs
|
||||
parser = parser_cls(tokenizer, **kwargs)
|
||||
|
||||
request = _test_request()
|
||||
request.tools = DUMMY_TOOLS
|
||||
if mode == "skip_tool_parsing":
|
||||
parser.skip_tool_parsing = True
|
||||
else:
|
||||
request.tool_choice = "none"
|
||||
|
||||
all_ids = [tid for tid, _ in sample.tokens]
|
||||
all_texts = [text for _, text in sample.tokens]
|
||||
if chunk_size is None:
|
||||
chunk_size = len(all_ids)
|
||||
|
||||
results = []
|
||||
chunks = list(range(0, len(all_ids), chunk_size))
|
||||
for i, start in enumerate(chunks):
|
||||
end = min(start + chunk_size, len(all_ids))
|
||||
is_last = i == len(chunks) - 1
|
||||
result = parser.parse_delta(
|
||||
"".join(all_texts[start:end]),
|
||||
all_ids[start:end],
|
||||
request,
|
||||
prompt_token_ids=(sample.prompt_token_ids or [])
|
||||
if start == 0
|
||||
else None,
|
||||
finished=is_last,
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
output = collect_output(results)
|
||||
|
||||
include_block = mode == "skip_tool_parsing"
|
||||
expected_reasoning, expected_content = _tool_suppression_expectations(
|
||||
sample, think_end, tool_start, include_tool_block=include_block
|
||||
)
|
||||
|
||||
assert output.reasoning == expected_reasoning, (
|
||||
f"Reasoning mismatch (mode={mode}):\n"
|
||||
f" expected: {expected_reasoning!r}\n"
|
||||
f" actual: {output.reasoning!r}"
|
||||
)
|
||||
assert output.tool_calls == [], (
|
||||
f"Expected no tool calls (mode={mode}) but got {output.tool_calls}"
|
||||
)
|
||||
assert output.content == expected_content, (
|
||||
f"Content mismatch (mode={mode}):\n"
|
||||
f" expected: {expected_content!r}\n"
|
||||
f" actual: {output.content!r}"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample,think_end,tool_start",
|
||||
_TOOL_CALL_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else getattr(v, "__name__", ""),
|
||||
)
|
||||
class TestToolCallFilteringNonStreaming:
|
||||
"""Non-streaming parse() with tool_choice='none' must suppress tool
|
||||
calls and not leak special tokens into content."""
|
||||
|
||||
def test_parse(self, parser_cls, sample, think_end, tool_start):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
kwargs = {}
|
||||
if sample.chat_template_kwargs:
|
||||
kwargs["chat_template_kwargs"] = sample.chat_template_kwargs
|
||||
parser = parser_cls(tokenizer, **kwargs)
|
||||
|
||||
request = _test_request()
|
||||
request.tools = DUMMY_TOOLS
|
||||
request.tool_choice = "none"
|
||||
|
||||
output = parse_non_streaming(parser, sample, request)
|
||||
|
||||
expected_reasoning, expected_content = _tool_suppression_expectations(
|
||||
sample, think_end, tool_start, include_tool_block=False
|
||||
)
|
||||
assert output.reasoning == expected_reasoning, (
|
||||
f"Reasoning mismatch:\n"
|
||||
f" expected: {expected_reasoning!r}\n"
|
||||
f" actual: {output.reasoning!r}"
|
||||
)
|
||||
assert output.tool_calls == [], (
|
||||
f"Expected no tool calls but got {output.tool_calls}"
|
||||
)
|
||||
assert output.content == expected_content, (
|
||||
f"Content mismatch:\n"
|
||||
f" expected: {expected_content!r}\n"
|
||||
f" actual: {output.content!r}"
|
||||
)
|
||||
|
||||
|
||||
_WS_TOOL_SAMPLES = [(t[0], t[1]) for t in _TOOL_CALL_SAMPLES if "whitespace" in t[1].id]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_cls,sample",
|
||||
_WS_TOOL_SAMPLES,
|
||||
ids=lambda v: v.id if hasattr(v, "id") else getattr(v, "__name__", ""),
|
||||
)
|
||||
class TestToolChoiceNoneStreamingParity:
|
||||
"""Streaming and non-streaming must return the same content
|
||||
when tool_choice='none' suppresses tool calls."""
|
||||
|
||||
def test_content_matches(self, parser_cls, sample):
|
||||
tokenizer = make_mock_tokenizer(sample)
|
||||
kwargs = {}
|
||||
if sample.chat_template_kwargs:
|
||||
kwargs["chat_template_kwargs"] = sample.chat_template_kwargs
|
||||
request = _test_request()
|
||||
request.tools = DUMMY_TOOLS
|
||||
request.tool_choice = "none"
|
||||
|
||||
ns_output = parse_non_streaming(
|
||||
parser_cls(tokenizer, **kwargs),
|
||||
sample,
|
||||
request,
|
||||
)
|
||||
|
||||
s_parser = parser_cls(tokenizer, **kwargs)
|
||||
results = []
|
||||
for i, (tid, text) in enumerate(sample.tokens):
|
||||
is_last = i == len(sample.tokens) - 1
|
||||
results.append(
|
||||
s_parser.parse_delta(
|
||||
text,
|
||||
[tid],
|
||||
request,
|
||||
prompt_token_ids=(sample.prompt_token_ids or [])
|
||||
if i == 0
|
||||
else None,
|
||||
finished=is_last,
|
||||
)
|
||||
)
|
||||
s_output = collect_output(results)
|
||||
|
||||
assert ns_output.content == s_output.content, (
|
||||
f"Streaming/non-streaming content mismatch:\n"
|
||||
f" streaming: {s_output.content!r}\n"
|
||||
f" non-streaming: {ns_output.content!r}"
|
||||
)
|
||||
|
||||
|
||||
_DROP_TOKENS = {"<bos>": 99990, "<eos>": 99991}
|
||||
|
||||
|
||||
def _inject_drop_tokens(sample):
|
||||
"""Insert <bos> at stream start and <eos> between the first two tokens."""
|
||||
new_vocab = {**sample.vocab, **_DROP_TOKENS}
|
||||
tokens = list(sample.tokens)
|
||||
tokens.insert(0, (99990, "<bos>"))
|
||||
if len(tokens) >= 3:
|
||||
tokens.insert(2, (99991, "<eos>"))
|
||||
else:
|
||||
tokens.append((99991, "<eos>"))
|
||||
return dataclasses.replace(sample, vocab=new_vocab, tokens=tokens)
|
||||
|
||||
|
||||
class TestDropTokenReplay:
|
||||
"""Verify unconfigured special tokens are silently dropped across
|
||||
all parsers and chunk sizes."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_info",
|
||||
_PARSERS,
|
||||
ids=[p.name for p in _PARSERS],
|
||||
)
|
||||
@pytest.mark.parametrize("chunk_size", [1, 3, None])
|
||||
def test_drop_tokens_removed_from_output(self, parser_info, chunk_size):
|
||||
for sample in parser_info.samples:
|
||||
injected = _inject_drop_tokens(sample)
|
||||
tokenizer = make_mock_tokenizer(injected)
|
||||
parser = _make_parser(parser_info.parser_cls, tokenizer, sample)
|
||||
|
||||
results = replay_streaming(
|
||||
parser,
|
||||
injected.tokens,
|
||||
chunk_size=chunk_size,
|
||||
tools=sample.tools,
|
||||
prompt_token_ids=sample.prompt_token_ids,
|
||||
)
|
||||
output = collect_output(results)
|
||||
|
||||
assert_no_terminal_leakage(
|
||||
output,
|
||||
list(_DROP_TOKENS.keys()),
|
||||
context=f"parser={parser_info.name}, chunk={chunk_size}",
|
||||
)
|
||||
assert_parse_output(output, sample)
|
||||
|
||||
|
||||
class TestDropTokenNonStreaming:
|
||||
"""Non-streaming parse() must also strip unconfigured special tokens."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_info",
|
||||
_PARSERS,
|
||||
ids=[p.name for p in _PARSERS],
|
||||
)
|
||||
def test_drop_tokens_removed_from_output(self, parser_info):
|
||||
for sample in parser_info.samples:
|
||||
injected = _inject_drop_tokens(sample)
|
||||
tokenizer = make_mock_tokenizer(injected)
|
||||
parser = _make_parser(parser_info.parser_cls, tokenizer, sample)
|
||||
|
||||
request = _test_request(tools=sample.tools)
|
||||
output = parse_non_streaming(parser, injected, request)
|
||||
|
||||
assert_no_terminal_leakage(
|
||||
output,
|
||||
list(_DROP_TOKENS.keys()),
|
||||
context=f"parser={parser_info.name}",
|
||||
)
|
||||
|
||||
|
||||
class TestAdapterReferences:
|
||||
"""Verify make_adapters sets reasoning/tool parser class refs on parser engine
|
||||
parser classes so the serving layer finds them and calls adjust_request."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parser_name",
|
||||
list(_ENGINE_PARSERS.keys()),
|
||||
)
|
||||
def test_adapter_cls_refs_set(self, parser_name):
|
||||
parser_cls = _ENGINE_PARSERS[parser_name]
|
||||
assert parser_cls.reasoning_parser_cls is not None, (
|
||||
f"{parser_name}: reasoning_parser_cls is None"
|
||||
)
|
||||
assert parser_cls.tool_parser_cls is not None, (
|
||||
f"{parser_name}: tool_parser_cls is None"
|
||||
)
|
||||
@@ -23,8 +23,6 @@ from tests.parser.engine.replay_harness import (
|
||||
)
|
||||
from vllm.parser.abstract_parser import DelegatingParser
|
||||
from vllm.parser.engine.registered_adapters import (
|
||||
Glm47MoeParserReasoningAdapter,
|
||||
Glm47MoeParserToolAdapter,
|
||||
Qwen3ParserReasoningAdapter,
|
||||
Qwen3ParserToolAdapter,
|
||||
)
|
||||
@@ -62,11 +60,6 @@ class ByteFallbackMockTokenizer(MockTokenizer):
|
||||
# ── Model-specific DelegatingParser subclasses ───────────────────────
|
||||
|
||||
|
||||
class _Glm47Delegating(DelegatingParser):
|
||||
reasoning_parser_cls = Glm47MoeParserReasoningAdapter
|
||||
tool_parser_cls = Glm47MoeParserToolAdapter
|
||||
|
||||
|
||||
class _Qwen3Delegating(DelegatingParser):
|
||||
reasoning_parser_cls = Qwen3ParserReasoningAdapter
|
||||
tool_parser_cls = Qwen3ParserToolAdapter
|
||||
@@ -92,20 +85,6 @@ EXPECTED_REASONING = "Let me think about Samsung."
|
||||
EXPECTED_CONTENT = "삼성전자의 주가를 분석합니다."
|
||||
|
||||
_MODEL_CONFIGS = [
|
||||
pytest.param(
|
||||
{
|
||||
"<think>": 50,
|
||||
"</think>": 51,
|
||||
"<tool_call>": 60,
|
||||
"</tool_call>": 61,
|
||||
"<arg_key>": 62,
|
||||
"</arg_key>": 63,
|
||||
"<arg_value>": 64,
|
||||
"</arg_value>": 65,
|
||||
},
|
||||
_Glm47Delegating,
|
||||
id="glm47",
|
||||
),
|
||||
pytest.param(
|
||||
{
|
||||
"<think>": 50,
|
||||
@@ -165,7 +144,7 @@ class TestUfffdReasoningTransition:
|
||||
vocab = dict(_MODEL_CONFIGS[0].values[0])
|
||||
|
||||
tokenizer = ByteFallbackMockTokenizer(vocab, tokens, ufffd_ids)
|
||||
parser = _Glm47Delegating(tokenizer)
|
||||
parser = _Qwen3Delegating(tokenizer)
|
||||
deltas = replay_streaming(
|
||||
parser,
|
||||
tokens,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user