diff --git a/tensorrt_llm/serve/openai_server.py b/tensorrt_llm/serve/openai_server.py index fa47e70edbd7..890c76721254 100644 --- a/tensorrt_llm/serve/openai_server.py +++ b/tensorrt_llm/serve/openai_server.py @@ -118,7 +118,8 @@ ChatCompletionNamedToolChoiceParam, ChatCompletionRequest, ChatCompletionResponse, ChatCompletionResponseChoice, ChatCompletionToolsParam, ChatMessage, CompletionRequest, - CompletionResponse, CompletionResponseChoice, EmbeddingRequest, + CompletionResponse, CompletionResponseChoice, CompletionStreamResponse, + EmbeddingRequest, EmbeddingResponse, EmbeddingResponseData, EmbeddingUsageInfo, ErrorResponse, ImageEditRequest, ImageGenerationRequest, ImageGenerationResponse, ImageObject, MemoryUpdateRequest, ModelCard, ModelList, PromptTokensDetails, @@ -609,6 +610,114 @@ def resolve_spec_decode_num_spec_tokens(args: Any) -> Optional[int]: _InputProcessingResult = TypeVar("_InputProcessingResult") +def _is_stream_usage_chunk(payload: Any) -> bool: + """Whether a parsed SSE payload is a final per-prompt usage chunk. + + The completion post-processor appends one of these (empty choices, usage + set) when a prompt finishes. Chunks that carry choices, such as the + continuous-usage-stats chunks, are not usage chunks. + """ + return (isinstance(payload, dict) and payload.get("choices") == [] + and isinstance(payload.get("usage"), dict)) + + +async def merge_generators(generators: List[AsyncIterator[Any]]): + """Merge per-prompt completion streams, emitting one summed usage chunk. + + Each prompt's post-processor appends its own final usage chunk, so with + several prompts a client keeping the last chunk would see one prompt's + tokens instead of the request total. Hold those chunks back, sum them the + way the non-streaming path sums responses, and emit a single usage chunk + after the last choice chunk. + + A usage chunk whose fields are malformed is forwarded untouched rather + than absorbed, so one bad chunk cannot abort the merged stream. If any + prompt fails, its error chunk is forwarded and no aggregate is emitted: + a partial total would be mistaken for the request total, and it would + land after the failed prompt's [DONE] where clients no longer read. + """ + result_queue = asyncio.Queue() + finished = [False] * len(generators) + + async def producer(generator: AsyncIterator[Any], idx: int): + async for output in generator: + await result_queue.put(output) + finished[idx] = True + + tasks = [ + asyncio.create_task(producer(generator, idx)) + for idx, generator in enumerate(generators) + ] + + usage_total = None + usage_model = usage_id = usage_created = None + usage_details: Dict[str, int] = {} + + def _absorb_usage(payload: Dict[str, Any]) -> bool: + """Sum one final usage chunk into the request totals. + + Returns False when the chunk's usage fields are malformed, for + example a non-integer token count or a prompt_tokens_details that + is not an object. The caller then forwards the chunk untouched + instead of letting it abort the merged stream. + """ + nonlocal usage_total, usage_model, usage_id, usage_created + usage = payload["usage"] + for key in ("prompt_tokens", "completion_tokens", "total_tokens"): + value = usage.get(key) + if value is not None and not isinstance(value, int): + return False + details = usage.get("prompt_tokens_details") or {} + if not isinstance(details, dict): + return False + if usage_total is None: + usage_total = { + "prompt_tokens": 0, + "completion_tokens": 0, + "total_tokens": 0, + } + usage_model = payload.get("model") + usage_id = payload.get("id") + usage_created = payload.get("created") + for key in ("prompt_tokens", "completion_tokens", "total_tokens"): + usage_total[key] += usage.get(key) or 0 + for key, value in details.items(): + if isinstance(value, int): + usage_details[key] = usage_details.get(key, 0) + value + return True + + saw_error = False + while not all(finished) or not result_queue.empty(): + output = await result_queue.get() + if isinstance(output, str) and output.startswith("data: "): + try: + payload = json.loads(output[len("data: "):]) + except ValueError: + payload = None + if isinstance(payload, dict) and "error" in payload: + saw_error = True + elif payload is not None and _is_stream_usage_chunk( + payload) and _absorb_usage(payload): + continue + yield output + + await asyncio.gather(*tasks) + + if not saw_error and usage_total is not None and usage_model is not None: + usage = UsageInfo( + prompt_tokens=usage_total["prompt_tokens"], + completion_tokens=usage_total["completion_tokens"], + total_tokens=usage_total["total_tokens"], + prompt_tokens_details=PromptTokensDetails(**usage_details), + ) + chunk = CompletionStreamResponse(choices=[], + model=usage_model, + usage=usage, + id=usage_id, + created=usage_created) + yield f"data: {chunk.model_dump_json()}\n\n" + + class OpenAIServer(_VideoRoutesMixin): @staticmethod @@ -3311,25 +3420,6 @@ async def completion_generator(promise: RequestOutput, yield f"data: {error_data}\n\n" yield "data: [DONE]\n\n" - async def merge_generators(generators: List[AsyncIterator[Any]]): - result_queue = asyncio.Queue() - finished = [False] * len(generators) - - async def producer(generator: AsyncIterator[Any], idx: int): - async for output in generator: - await result_queue.put(output) - finished[idx] = True - - tasks = [ - asyncio.create_task(producer(generator, idx)) - for idx, generator in enumerate(generators) - ] - - while not all(finished) or not result_queue.empty(): - output = await result_queue.get() - yield output - await asyncio.gather(*tasks) - async def generator_wrapper(generator: AsyncIterator[Any]): first_response = await anext(generator) raw_request.state.server_first_token_time = self._adjusted_steady_clock.now( diff --git a/tests/unittest/llmapi/test_llm_serving.py b/tests/unittest/llmapi/test_llm_serving.py index ccbee41ac877..259dba52840e 100644 --- a/tests/unittest/llmapi/test_llm_serving.py +++ b/tests/unittest/llmapi/test_llm_serving.py @@ -3,15 +3,17 @@ import asyncio import json +from contextlib import contextmanager import pytest +from test_common.serve.app_factory import AggConfig, isolated_serve_env from tensorrt_llm._utils import AdjustedSteadyClock from tensorrt_llm.executor import GenerationResultBase from tensorrt_llm.llmapi.llm import BaseLLM from tensorrt_llm.sampling_params import SamplingParams from tensorrt_llm.serve.openai_protocol import CompletionRequest, StreamOptions -from tensorrt_llm.serve.openai_server import OpenAIServer +from tensorrt_llm.serve.openai_server import OpenAIServer, merge_generators from tensorrt_llm.serve.postprocess_handlers import ChatPostprocArgs, chat_stream_post_processor pytestmark = [pytest.mark.cpu_only, pytest.mark.threadleak(enabled=False)] @@ -177,6 +179,24 @@ async def is_disconnected(self): return True +@contextmanager +def _completion_server(tmp_path, generator): + # Build the server through the shared factory instead of + # object.__new__ (the ratchet in test_no_new_object_new_sites only + # lets the baseline sites shrink), then swap in the fake generator + # the test drives and pin the attributes the request path reads. + with isolated_serve_env(tmp_path) as env: + server = env.build_openai_server(AggConfig(model_name="test-model")) + server.generator = generator + server.model_config = _FakeModelConfig() + server.tokenizer = None + server.metrics_collector = None + server._collect_perf_metrics = False + server._input_proc_executor = None + server._adjusted_steady_clock = AdjustedSteadyClock() + yield server + + @pytest.mark.cpu_only @pytest.mark.parametrize( "checkpoint_io_policy", @@ -279,3 +299,223 @@ async def run_request(): assert choice_indexes == {0, 1} assert {args.stream_response_id for args in generator.postproc_args} == ids assert {args.stream_created for args in generator.postproc_args} == created + + +def test_openai_completion_multi_prompt_stream_sums_usage(tmp_path) -> None: + # A streaming /v1/completions request with several prompts and + # include_usage must end with a single usage chunk for the whole + # request, not one per prompt (issue #22740). The fake generator gives + # each prompt one prompt token and one generated token. + async def run_request(): + generator = _FakeCompletionGenerator() + with _completion_server(tmp_path, generator) as server: + request = CompletionRequest( + model="test-model", + prompt=["A", "B"], + stream=True, + stream_options=StreamOptions(include_usage=True), + ) + response = await server.openai_completion(request, _FakeRawRequest()) + chunks = [chunk async for chunk in response.body_iterator] + return _stream_payloads_from_chunks(chunks) + + payloads = asyncio.run(run_request()) + + # Both prompts still emit their choice chunk, indexes 0 and 1 exactly + # once each, before the aggregate. + choice_indexes = [ + payload["choices"][0]["index"] + for payload in payloads + if payload["choices"] + ] + assert sorted(choice_indexes) == [0, 1] + + usage_chunks = [payload for payload in payloads if payload["choices"] == []] + assert len(usage_chunks) == 1 + assert usage_chunks[0] is payloads[-1] + usage = usage_chunks[0]["usage"] + assert usage["prompt_tokens"] == 2 + assert usage["completion_tokens"] == 2 + assert usage["total_tokens"] == 4 + assert usage["prompt_tokens_details"]["cached_tokens"] == 0 + assert {payload["id"] for payload in payloads} == {usage_chunks[0]["id"]} + + +def test_openai_completion_multi_prompt_stream_continuous_usage( + tmp_path) -> None: + # With continuous_usage_stats, every choice chunk carries its own + # prompt's usage. Those chunks are not final usage chunks: the merger + # must pass them through untouched, and only the trailing usage-only + # chunk holds the request total. + async def run_request(): + generator = _FakeCompletionGenerator() + with _completion_server(tmp_path, generator) as server: + request = CompletionRequest( + model="test-model", + prompt=["A", "B"], + stream=True, + stream_options=StreamOptions( + include_usage=True, continuous_usage_stats=True), + ) + response = await server.openai_completion(request, _FakeRawRequest()) + chunks = [chunk async for chunk in response.body_iterator] + return _stream_payloads_from_chunks(chunks) + + payloads = asyncio.run(run_request()) + + choice_payloads = [payload for payload in payloads if payload["choices"]] + assert sorted( + payload["choices"][0]["index"] for payload in choice_payloads) == [0, 1] + for payload in choice_payloads: + # Each prompt generated one token from one prompt token, and its + # choice chunk keeps exactly that per-prompt usage. + assert payload["usage"]["prompt_tokens"] == 1 + assert payload["usage"]["completion_tokens"] == 1 + assert payload["usage"]["total_tokens"] == 2 + + usage_chunks = [payload for payload in payloads if payload["choices"] == []] + assert len(usage_chunks) == 1 + assert usage_chunks[0] is payloads[-1] + usage = usage_chunks[0]["usage"] + assert usage["prompt_tokens"] == 2 + assert usage["completion_tokens"] == 2 + assert usage["total_tokens"] == 4 + + +async def _fake_prompt_usage_stream(prompt_tokens: int, + completion_tokens: int, + cached_tokens: int = 0): + chunk = { + "id": "cmpl-1", + "object": "text_completion", + "created": 1, + "model": "test-model", + "choices": [{ + "index": 0, + "text": "x", + "finish_reason": "stop", + }], + "usage": None, + } + yield f"data: {json.dumps(chunk)}\n\n" + chunk["choices"] = [] + chunk["usage"] = { + "prompt_tokens": prompt_tokens, + "completion_tokens": completion_tokens, + "total_tokens": prompt_tokens + completion_tokens, + "prompt_tokens_details": { + "cached_tokens": cached_tokens + }, + } + yield f"data: {json.dumps(chunk)}\n\n" + + +def test_merge_generators_sums_per_prompt_usage_chunks() -> None: + async def collect(): + merged = merge_generators([ + _fake_prompt_usage_stream(3, 2, cached_tokens=1), + _fake_prompt_usage_stream(5, 4, cached_tokens=2), + ]) + return [chunk async for chunk in merged] + + payloads = _stream_payloads_from_chunks(asyncio.run(collect())) + + usage_chunks = [payload for payload in payloads if payload["choices"] == []] + assert len(usage_chunks) == 1 + assert usage_chunks[0] is payloads[-1] + assert usage_chunks[0]["usage"] == { + "prompt_tokens": 8, + "completion_tokens": 6, + "total_tokens": 14, + "prompt_tokens_details": { + "cached_tokens": 3 + }, + } + + +def test_merge_generators_forwards_malformed_usage_chunks() -> None: + # A usage chunk whose fields are malformed must be forwarded + # untouched: it must not abort the merged stream, and it must not + # change the summed total. One frame has prompt_tokens_details as a + # list instead of an object, the other a string token count. + def usage_frame(usage): + return "data: %s\n\n" % json.dumps({ + "id": "cmpl-1", + "object": "text_completion", + "created": 1, + "model": "test-model", + "choices": [], + "usage": usage, + }) + + bad_details = usage_frame({ + "prompt_tokens": 100, + "completion_tokens": 100, + "total_tokens": 200, + "prompt_tokens_details": [1], + }) + bad_count = usage_frame({ + "prompt_tokens": "3", + "completion_tokens": 2, + "total_tokens": 5, + "prompt_tokens_details": { + "cached_tokens": 0 + }, + }) + + async def malformed_stream(): + yield bad_details + yield bad_count + + async def collect(): + merged = merge_generators([ + malformed_stream(), + _fake_prompt_usage_stream(3, 2), + _fake_prompt_usage_stream(5, 4), + ]) + return [chunk async for chunk in merged] + + chunks = asyncio.run(collect()) + + assert bad_details in chunks + assert bad_count in chunks + payloads = _stream_payloads_from_chunks(chunks) + totals = sorted( + payload["usage"]["total_tokens"] + for payload in payloads + if payload["choices"] == [] and isinstance( + payload["usage"]["total_tokens"], int)) + # The two malformed frames keep their own totals, and the only + # aggregate is the sum of the two valid frames. + assert totals == [5, 14, 200] + + +def test_merge_generators_skips_aggregate_when_a_prompt_fails() -> None: + # One prompt completes with usage, the other fails: its generator + # yields an error chunk and [DONE], like completion_generator does. + # The error goes out unchanged and no aggregate follows it, since a + # summed total here would be partial and would land after [DONE]. + error_chunk = ('data: {"error": {"message": "boom", "type": ' + '"server_error", "code": null, "param": null}}\n\n') + + async def failing_stream(): + yield error_chunk + yield "data: [DONE]\n\n" + + async def collect(): + merged = merge_generators([ + _fake_prompt_usage_stream(3, 2), + failing_stream(), + ]) + return [chunk async for chunk in merged] + + chunks = asyncio.run(collect()) + + assert error_chunk in chunks + done_index = chunks.index("data: [DONE]\n\n") + assert _stream_payloads_from_chunks(chunks[done_index + 1:]) == [] + usage_payloads = [ + payload for payload in _stream_payloads_from_chunks(chunks) + if payload.get("choices") == [] and payload.get("usage") + ] + assert usage_payloads == []