[Bugfix] Preserve prompt token usage details (#5181)
Signed-off-by: Ian Eaves <ian.k.eaves@gmail.com>
This commit is contained in:
1 parent
37c11e6e4f
commit
5730af0a95
9 files changed
+418
-257
No files matched your search
@@ -16,19 +16,20 @@ Key invariants tested:
|
||||
- voice/speaker parameter compatibility in chat completions
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionResponseStreamChoice,
|
||||
)
|
||||
from vllm.entrypoints.openai.engine.protocol import DeltaMessage
|
||||
from vllm.entrypoints.openai.models.serving import OpenAIServingModels
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
||||
from vllm.outputs import CompletionOutput, RequestOutput
|
||||
|
||||
from tests.helpers.serving_chat import (
|
||||
build_serving_chat,
|
||||
collect_stream,
|
||||
make_request,
|
||||
make_text_omni_output,
|
||||
parse_sse_chunks,
|
||||
)
|
||||
from vllm_omni.entrypoints.openai.serving_chat import OmniOpenAIServingChat
|
||||
from vllm_omni.outputs import OmniRequestOutput
|
||||
|
||||
@@ -40,43 +41,6 @@ pytestmark = [pytest.mark.core_model, pytest.mark.cpu]
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _make_text_omni_output(
|
||||
request_id: str = "test-req",
|
||||
text: str = "hello",
|
||||
token_ids: list[int] | None = None,
|
||||
finish_reason: str | None = None,
|
||||
index: int = 0,
|
||||
num_prompt_tokens: int = 3,
|
||||
) -> OmniRequestOutput:
|
||||
"""Build an OmniRequestOutput wrapping a text RequestOutput."""
|
||||
if token_ids is None:
|
||||
token_ids = [10, 11, 12]
|
||||
res = RequestOutput(
|
||||
request_id=request_id,
|
||||
prompt="test",
|
||||
prompt_token_ids=list(range(num_prompt_tokens)),
|
||||
prompt_logprobs=None,
|
||||
outputs=[
|
||||
CompletionOutput(
|
||||
index=index,
|
||||
text=text,
|
||||
token_ids=token_ids,
|
||||
cumulative_logprob=0.0,
|
||||
logprobs=None,
|
||||
finish_reason=finish_reason,
|
||||
stop_reason=None,
|
||||
)
|
||||
],
|
||||
finished=finish_reason is not None,
|
||||
)
|
||||
return OmniRequestOutput.from_stage_output(
|
||||
res,
|
||||
request_id=request_id,
|
||||
final_output_type="text",
|
||||
finished=finish_reason is not None,
|
||||
)
|
||||
|
||||
|
||||
def _make_audio_omni_output(
|
||||
request_id: str = "test-req",
|
||||
index: int = 0,
|
||||
@@ -118,82 +82,6 @@ def _make_audio_omni_output(
|
||||
)
|
||||
|
||||
|
||||
def _mock_audio_choices(index: int = 0, role: str = "assistant"):
|
||||
return [
|
||||
ChatCompletionResponseStreamChoice(
|
||||
index=index,
|
||||
delta=DeltaMessage(role=role, content="dGVzdA=="),
|
||||
logprobs=None,
|
||||
finish_reason="stop",
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def _build_serving_chat():
|
||||
"""Create a minimal OmniOpenAIServingChat for testing."""
|
||||
mock_engine = MagicMock()
|
||||
mock_engine.errored = False
|
||||
|
||||
models = OpenAIServingModels(
|
||||
engine_client=mock_engine,
|
||||
base_model_paths=[],
|
||||
)
|
||||
mock_render = MagicMock()
|
||||
|
||||
instance = OmniOpenAIServingChat(
|
||||
engine_client=mock_engine,
|
||||
models=models,
|
||||
response_role="assistant",
|
||||
online_renderer=mock_render,
|
||||
request_logger=None,
|
||||
chat_template=None,
|
||||
chat_template_content_format="auto",
|
||||
)
|
||||
instance._create_audio_choice = MagicMock(
|
||||
side_effect=lambda omni_res, role, request, stream=False: _mock_audio_choices(
|
||||
index=omni_res.outputs[0].index,
|
||||
role=role,
|
||||
)
|
||||
)
|
||||
return instance
|
||||
|
||||
|
||||
def _make_request(modalities: list[str], n: int = 1) -> ChatCompletionRequest:
|
||||
req = ChatCompletionRequest(
|
||||
model="test-model",
|
||||
messages=[{"role": "user", "content": "hello"}],
|
||||
n=n,
|
||||
stream=True,
|
||||
)
|
||||
req.modalities = modalities # type: ignore[attr-defined]
|
||||
return req
|
||||
|
||||
|
||||
def _parse_sse_chunks(lines: list[str]) -> list[dict]:
|
||||
"""Parse SSE lines into JSON dicts."""
|
||||
prefix = "data: "
|
||||
chunks = []
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if not line.startswith(prefix):
|
||||
continue
|
||||
payload = line[len(prefix) :].strip()
|
||||
if payload == "[DONE]":
|
||||
continue
|
||||
try:
|
||||
chunks.append(json.loads(payload))
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return chunks
|
||||
|
||||
|
||||
async def _collect_stream(gen):
|
||||
result = []
|
||||
async for item in gen:
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tests: finish_reason correctness
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -202,14 +90,14 @@ async def _collect_stream(gen):
|
||||
@pytest.mark.asyncio
|
||||
async def test_single_modality_text_only_one_stop():
|
||||
"""Text-only streaming: exactly one chunk has finish_reason='stop'."""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text"])
|
||||
|
||||
async def result_generator():
|
||||
yield _make_text_omni_output(text="he", token_ids=[10, 11], finish_reason=None)
|
||||
yield _make_text_omni_output(text="llo", token_ids=[12], finish_reason="stop")
|
||||
yield make_text_omni_output(text="he", token_ids=[10, 11], finish_reason=None)
|
||||
yield make_text_omni_output(text="llo", token_ids=[12], finish_reason="stop")
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -221,7 +109,7 @@ async def test_single_modality_text_only_one_stop():
|
||||
)
|
||||
)
|
||||
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
finish_reasons = [c["choices"][0]["finish_reason"] for c in chunks if c.get("choices")]
|
||||
|
||||
assert finish_reasons[-1] == "stop"
|
||||
@@ -233,15 +121,15 @@ async def test_single_modality_text_only_one_stop():
|
||||
@pytest.mark.asyncio
|
||||
async def test_multi_modal_text_audio_only_last_stop():
|
||||
"""text+audio: text finish sends finish_reason=null, audio sends stop."""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text", "audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text", "audio"])
|
||||
|
||||
async def result_generator():
|
||||
yield _make_text_omni_output(text="he", token_ids=[10, 11], finish_reason=None)
|
||||
yield _make_text_omni_output(text="llo", token_ids=[12], finish_reason="stop")
|
||||
yield make_text_omni_output(text="he", token_ids=[10, 11], finish_reason=None)
|
||||
yield make_text_omni_output(text="llo", token_ids=[12], finish_reason="stop")
|
||||
yield _make_audio_omni_output()
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -253,7 +141,7 @@ async def test_multi_modal_text_audio_only_last_stop():
|
||||
)
|
||||
)
|
||||
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
finish_reasons = [ch["finish_reason"] for c in chunks for ch in c.get("choices", [])]
|
||||
|
||||
assert finish_reasons.count("stop") == 1
|
||||
@@ -269,18 +157,18 @@ async def test_multi_modal_text_audio_only_last_stop():
|
||||
@pytest.mark.asyncio
|
||||
async def test_multi_modal_n2_independent_per_choice():
|
||||
"""n=2 with text+audio: each choice gets exactly one stop, at the end."""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text", "audio"], n=2)
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text", "audio"], n=2)
|
||||
|
||||
async def result_generator():
|
||||
yield _make_text_omni_output(text="A", token_ids=[10], finish_reason=None, index=0)
|
||||
yield _make_text_omni_output(text="B", token_ids=[20], finish_reason=None, index=1)
|
||||
yield _make_text_omni_output(text="", token_ids=[11], finish_reason="stop", index=0)
|
||||
yield _make_text_omni_output(text="", token_ids=[21], finish_reason="stop", index=1)
|
||||
yield make_text_omni_output(text="A", token_ids=[10], finish_reason=None, index=0)
|
||||
yield make_text_omni_output(text="B", token_ids=[20], finish_reason=None, index=1)
|
||||
yield make_text_omni_output(text="", token_ids=[11], finish_reason="stop", index=0)
|
||||
yield make_text_omni_output(text="", token_ids=[21], finish_reason="stop", index=1)
|
||||
yield _make_audio_omni_output(index=0)
|
||||
yield _make_audio_omni_output(index=1)
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -292,7 +180,7 @@ async def test_multi_modal_n2_independent_per_choice():
|
||||
)
|
||||
)
|
||||
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
per_choice: dict[int, list] = {}
|
||||
for c in chunks:
|
||||
for ch in c.get("choices", []):
|
||||
@@ -306,13 +194,13 @@ async def test_multi_modal_n2_independent_per_choice():
|
||||
@pytest.mark.asyncio
|
||||
async def test_single_modality_audio_only_one_stop():
|
||||
"""Audio-only streaming: the audio chunk carries finish_reason='stop'."""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["audio"])
|
||||
|
||||
async def result_generator():
|
||||
yield _make_audio_omni_output()
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -324,7 +212,7 @@ async def test_single_modality_audio_only_one_stop():
|
||||
)
|
||||
)
|
||||
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
finish_reasons = [ch["finish_reason"] for c in chunks for ch in c.get("choices", [])]
|
||||
|
||||
assert finish_reasons.count("stop") == 1
|
||||
@@ -349,8 +237,8 @@ async def test_streaming_audio_metrics_resolve_replica_id(
|
||||
import vllm_omni.entrypoints.openai.serving_chat as serving_chat_mod
|
||||
from vllm_omni.entrypoints.client_request_state import ClientRequestState
|
||||
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["audio"])
|
||||
req_state = ClientRequestState(
|
||||
request_id="internal-req",
|
||||
external_request_id="test-req",
|
||||
@@ -377,7 +265,7 @@ async def test_streaming_audio_metrics_resolve_replica_id(
|
||||
async def result_generator():
|
||||
yield _make_audio_omni_output(stage_id=2, replica_id=output_replica_id, audio_samples=2400)
|
||||
|
||||
await _collect_stream(
|
||||
await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -408,15 +296,15 @@ async def test_streaming_audio_metrics_resolve_replica_id(
|
||||
async def test_declared_modality_not_produced_emits_fallback_stop():
|
||||
"""If request.modalities declares ["text","audio"] but engine only produces
|
||||
text, a fallback stop chunk is emitted at stream end."""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text", "audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text", "audio"])
|
||||
|
||||
async def result_generator():
|
||||
# Engine only produces text, no audio output at all
|
||||
yield _make_text_omni_output(text="hi", token_ids=[10], finish_reason=None)
|
||||
yield _make_text_omni_output(text="!", token_ids=[11], finish_reason="stop")
|
||||
yield make_text_omni_output(text="hi", token_ids=[10], finish_reason=None)
|
||||
yield make_text_omni_output(text="!", token_ids=[11], finish_reason="stop")
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -428,7 +316,7 @@ async def test_declared_modality_not_produced_emits_fallback_stop():
|
||||
)
|
||||
)
|
||||
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
finish_reasons = [ch["finish_reason"] for c in chunks for ch in c.get("choices", [])]
|
||||
|
||||
# Text finish is suppressed (audio not seen yet), but fallback stop
|
||||
@@ -441,15 +329,15 @@ async def test_declared_modality_not_produced_emits_fallback_stop():
|
||||
async def test_declared_modality_not_produced_text_finish_suppressed():
|
||||
"""When text finishes but audio (declared in modalities) never appears,
|
||||
the text finish chunk has finish_reason=null (suppressed)."""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text", "audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text", "audio"])
|
||||
|
||||
async def result_generator():
|
||||
yield _make_text_omni_output(text="hi", token_ids=[10], finish_reason=None)
|
||||
yield _make_text_omni_output(text="!", token_ids=[11], finish_reason="stop")
|
||||
yield make_text_omni_output(text="hi", token_ids=[10], finish_reason=None)
|
||||
yield make_text_omni_output(text="!", token_ids=[11], finish_reason="stop")
|
||||
# No audio output — stream ends
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -461,7 +349,7 @@ async def test_declared_modality_not_produced_text_finish_suppressed():
|
||||
)
|
||||
)
|
||||
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
|
||||
# Find the text finish chunk (content "!")
|
||||
for c in chunks:
|
||||
@@ -485,8 +373,8 @@ async def test_audio_chunk_without_waveform_keeps_stream_alive():
|
||||
yields (field, value) tuples, so the loop raised
|
||||
``AttributeError: 'tuple' object has no attribute 'finish_reason'``.
|
||||
"""
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text", "audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text", "audio"])
|
||||
|
||||
empty_audio = _make_audio_omni_output()
|
||||
empty_audio.outputs[0].multimodal_output = {"audio": []}
|
||||
@@ -497,11 +385,11 @@ async def test_audio_chunk_without_waveform_keeps_stream_alive():
|
||||
serving_chat._create_audio_choice = create_audio_choice
|
||||
|
||||
async def result_generator():
|
||||
yield _make_text_omni_output(text="hi", token_ids=[10], finish_reason=None)
|
||||
yield _make_text_omni_output(text="!", token_ids=[11], finish_reason="stop")
|
||||
yield make_text_omni_output(text="hi", token_ids=[10], finish_reason=None)
|
||||
yield make_text_omni_output(text="!", token_ids=[11], finish_reason="stop")
|
||||
yield empty_audio
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -514,7 +402,7 @@ async def test_audio_chunk_without_waveform_keeps_stream_alive():
|
||||
)
|
||||
|
||||
assert not any("Error in chat completion stream generator" in line for line in raw_lines)
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
finish_reasons = [ch["finish_reason"] for c in chunks for ch in c.get("choices", [])]
|
||||
assert finish_reasons.count("stop") == 1, f"Expected 1 stop, got {finish_reasons}"
|
||||
assert finish_reasons[-1] == "stop"
|
||||
@@ -525,18 +413,18 @@ async def test_audio_choice_error_response_is_not_iterated_as_choices():
|
||||
"""Any ErrorResponse from the audio path is skipped, not iterated."""
|
||||
from vllm.entrypoints.openai.engine.protocol import ErrorResponse
|
||||
|
||||
serving_chat = _build_serving_chat()
|
||||
request = _make_request(modalities=["text", "audio"])
|
||||
serving_chat = build_serving_chat()
|
||||
request = make_request(modalities=["text", "audio"])
|
||||
|
||||
serving_chat._create_audio_choice = MagicMock(
|
||||
side_effect=lambda omni_res, role, request, stream=False: serving_chat._create_error_response("boom")
|
||||
)
|
||||
|
||||
async def result_generator():
|
||||
yield _make_text_omni_output(text="hi", token_ids=[10], finish_reason="stop")
|
||||
yield make_text_omni_output(text="hi", token_ids=[10], finish_reason="stop")
|
||||
yield _make_audio_omni_output()
|
||||
|
||||
raw_lines = await _collect_stream(
|
||||
raw_lines = await collect_stream(
|
||||
serving_chat.chat_completion_stream_generator(
|
||||
request=request,
|
||||
result_generator=result_generator(),
|
||||
@@ -550,7 +438,7 @@ async def test_audio_choice_error_response_is_not_iterated_as_choices():
|
||||
|
||||
assert isinstance(serving_chat._create_error_response("boom"), ErrorResponse)
|
||||
assert not any("AttributeError" in line for line in raw_lines)
|
||||
chunks = _parse_sse_chunks(raw_lines)
|
||||
chunks = parse_sse_chunks(raw_lines)
|
||||
finish_reasons = [ch["finish_reason"] for c in chunks for ch in c.get("choices", [])]
|
||||
assert finish_reasons.count("stop") == 1, f"Expected 1 stop, got {finish_reasons}"
|
||||
|
||||
|
||||
Reference in new issue
Block a user