chore: import upstream snapshot with attribution
pre-commit / pre-run-check (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 12:55:37 +08:00
commit 7ce4c8e27e
5900 changed files with 1668062 additions and 0 deletions
@@ -0,0 +1,398 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import multiprocessing
import socket
import threading
import time
from unittest.mock import patch
import pytest
import zmq
from vllm.utils.network_utils import make_zmq_socket, split_zmq_path
from vllm.v1.utils import (
APIServerProcessManager,
get_engine_client_zmq_addr,
wait_for_completion_or_failure,
)
# Global variables to control worker behavior
WORKER_RUNTIME_SECONDS = 0.5
# Mock implementation of run_api_server_worker
def mock_run_api_server_worker(listen_address, sock, args, client_config=None):
"""Mock run_api_server_worker that runs for a specific time."""
print(f"Mock worker started with client_config: {client_config}")
time.sleep(WORKER_RUNTIME_SECONDS)
print("Mock worker completed successfully")
# Module-level stub for the gather_actual_addresses test. Must be
# importable by `multiprocessing.spawn` (no closures, no nesting).
def defer_addresses_stub_worker(listen_address, sock, args, client_config):
"""Bind ROUTER/PULL with a kernel-assigned port, report the actual
endpoints back via the pipe, then exit."""
ctx = zmq.Context()
try:
in_sock = make_zmq_socket(
ctx, client_config["input_address"], zmq.ROUTER, bind=True
)
out_sock = make_zmq_socket(
ctx, client_config["output_address"], zmq.PULL, bind=True
)
try:
pipe = client_config["actual_address_pipe"]
try:
pipe.send(
{
"input_address": in_sock.getsockopt(zmq.LAST_ENDPOINT).decode(),
"output_address": out_sock.getsockopt(
zmq.LAST_ENDPOINT
).decode(),
}
)
finally:
pipe.close()
finally:
in_sock.close(linger=0)
out_sock.close(linger=0)
finally:
ctx.term()
@pytest.fixture
def api_server_args():
"""Fixture to provide arguments for APIServerProcessManager."""
sock = socket.socket()
return {
"target_server_fn": mock_run_api_server_worker,
"listen_address": "localhost:8000",
"sock": sock,
"args": "test_args", # Simple string to avoid pickling issues
"num_servers": 3,
"input_addresses": [
"tcp://127.0.0.1:5001",
"tcp://127.0.0.1:5002",
"tcp://127.0.0.1:5003",
],
"output_addresses": [
"tcp://127.0.0.1:6001",
"tcp://127.0.0.1:6002",
"tcp://127.0.0.1:6003",
],
"stats_update_address": "tcp://127.0.0.1:7000",
}
@pytest.mark.parametrize("with_stats_update", [True, False])
def test_api_server_process_manager_init(api_server_args, with_stats_update):
"""Test initializing the APIServerProcessManager."""
# Set the worker runtime to ensure tests complete in reasonable time
global WORKER_RUNTIME_SECONDS
WORKER_RUNTIME_SECONDS = 0.5
# Copy the args to avoid mutating them
args = api_server_args.copy()
if not with_stats_update:
args.pop("stats_update_address")
manager = APIServerProcessManager(**args)
try:
# Verify the manager was initialized correctly
assert len(manager.processes) == 3
# Verify all processes are running
for proc in manager.processes:
assert proc.is_alive()
print("Waiting for processes to run...")
time.sleep(WORKER_RUNTIME_SECONDS / 2)
# They should still be alive at this point
for proc in manager.processes:
assert proc.is_alive()
finally:
# Always clean up the processes
print("Cleaning up processes...")
manager.shutdown()
# Give processes time to terminate
time.sleep(0.2)
# Verify all processes were terminated
for proc in manager.processes:
assert not proc.is_alive()
@patch("vllm.v1.utils.run_api_server_worker_proc", mock_run_api_server_worker)
def test_wait_for_completion_or_failure(api_server_args):
"""Test that wait_for_completion_or_failure works with failures."""
global WORKER_RUNTIME_SECONDS
WORKER_RUNTIME_SECONDS = 1.0
# Create the manager
manager = APIServerProcessManager(**api_server_args)
try:
assert len(manager.processes) == 3
# Create a result capture for the thread
result: dict[str, Exception | None] = {"exception": None}
def run_with_exception_capture():
try:
wait_for_completion_or_failure(api_server_manager=manager)
except Exception as e:
result["exception"] = e
finally:
manager.shutdown()
# Start a thread to run wait_for_completion_or_failure
wait_thread = threading.Thread(target=run_with_exception_capture, daemon=True)
wait_thread.start()
# Let all processes run for a short time
time.sleep(0.2)
# All processes should still be running
assert all(proc.is_alive() for proc in manager.processes)
# Now simulate a process failure
print("Simulating process failure...")
manager.processes[0].terminate()
# Wait for the wait_for_completion_or_failure
# to detect and handle the failure
# This should trigger it to terminate all other processes
wait_thread.join(timeout=1.0)
# The wait thread should have exited
assert not wait_thread.is_alive()
# Verify that an exception was raised with appropriate error message
assert result["exception"] is not None
assert "died with exit code" in str(result["exception"])
# All processes should now be terminated
for i, proc in enumerate(manager.processes):
assert not proc.is_alive(), f"Process {i} should not be alive"
finally:
manager.shutdown()
time.sleep(0.2)
@pytest.mark.timeout(30)
def test_normal_completion(api_server_args):
"""Test that wait_for_completion_or_failure works in normal completion."""
global WORKER_RUNTIME_SECONDS
WORKER_RUNTIME_SECONDS = 0.1
# Create the manager
manager = APIServerProcessManager(**api_server_args)
try:
# Give processes time to terminate
# wait for processes to complete
remaining_processes = manager.processes.copy()
while remaining_processes:
for proc in remaining_processes:
if not proc.is_alive():
remaining_processes.remove(proc)
time.sleep(0.1)
# Verify all processes have terminated
for i, proc in enumerate(manager.processes):
assert not proc.is_alive(), f"Process {i} still alive after terminate()"
# Now call wait_for_completion_or_failure
# since all processes have already
# terminated, it should return immediately
# with no error
try:
wait_for_completion_or_failure(api_server_manager=manager)
finally:
manager.shutdown()
finally:
# Clean up just in case
manager.shutdown()
time.sleep(0.2)
@pytest.mark.timeout(30)
def test_external_process_monitoring(api_server_args):
"""Test that wait_for_completion_or_failure handles additional processes."""
global WORKER_RUNTIME_SECONDS
WORKER_RUNTIME_SECONDS = 100
# Create and start the external process
# (simulates local_engine_manager or coordinator)
spawn_context = multiprocessing.get_context("spawn")
external_proc = spawn_context.Process(
target=mock_run_api_server_worker, name="MockExternalProcess"
)
external_proc.start()
# Create the class to simulate a coordinator
class MockCoordinator:
def __init__(self, proc):
self.proc = proc
def shutdown(self):
if self.proc.is_alive():
self.proc.terminate()
self.proc.join(timeout=0.5)
# Create a mock coordinator with the external process
mock_coordinator = MockCoordinator(external_proc)
# Create the API server manager
manager = APIServerProcessManager(**api_server_args)
try:
# Verify manager initialization
assert len(manager.processes) == 3
# Create a result capture for the thread
result: dict[str, Exception | None] = {"exception": None}
def run_with_exception_capture():
try:
wait_for_completion_or_failure(
api_server_manager=manager, coordinator=mock_coordinator
)
except Exception as e:
result["exception"] = e
finally:
manager.shutdown()
mock_coordinator.shutdown()
# Start a thread to run wait_for_completion_or_failure
wait_thread = threading.Thread(target=run_with_exception_capture, daemon=True)
wait_thread.start()
# Terminate the external process to trigger a failure
time.sleep(0.2)
external_proc.terminate()
# Wait for the thread to detect the failure
wait_thread.join(timeout=1.0)
# The wait thread should have completed
assert not wait_thread.is_alive(), (
"wait_for_completion_or_failure thread still running"
)
# Verify that an exception was raised with appropriate error message
assert result["exception"] is not None, "No exception was raised"
error_message = str(result["exception"])
assert "died with exit code" in error_message, (
f"Unexpected error message: {error_message}"
)
assert "MockExternalProcess" in error_message, (
f"Error doesn't mention external process: {error_message}"
)
# Verify that all API server processes were terminated as a result
for i, proc in enumerate(manager.processes):
assert not proc.is_alive(), f"API server process {i} was not terminated"
finally:
# Clean up
manager.shutdown()
mock_coordinator.shutdown()
time.sleep(0.2)
@pytest.mark.timeout(60)
def test_gather_actual_addresses_end_to_end():
"""Each child binds ROUTER/PULL with a kernel-picked port and reports
the bound endpoints back via its per-child pipe; the manager surfaces
them via :py:meth:`gather_actual_addresses`."""
host = "127.0.0.1"
num_servers = 4
placeholder_inputs = [
get_engine_client_zmq_addr(local_only=False, host=host)
for _ in range(num_servers)
]
placeholder_outputs = [
get_engine_client_zmq_addr(local_only=False, host=host)
for _ in range(num_servers)
]
for addr in placeholder_inputs + placeholder_outputs:
assert addr == f"tcp://{host}:0", addr
sock = socket.socket()
manager = APIServerProcessManager(
listen_address=f"tcp://{host}:0",
sock=sock,
args="test_args",
num_servers=num_servers,
input_addresses=placeholder_inputs,
output_addresses=placeholder_outputs,
target_server_fn=defer_addresses_stub_worker,
)
try:
assert len(manager.processes) == num_servers
actual_inputs, actual_outputs = manager.gather_actual_addresses(timeout=15.0)
finally:
manager.shutdown()
time.sleep(0.2)
sock.close()
assert len(actual_inputs) == num_servers
assert len(actual_outputs) == num_servers
for addr in actual_inputs + actual_outputs:
scheme, parsed_host, port = split_zmq_path(addr)
assert scheme == "tcp", addr
assert parsed_host == host, addr
assert port and int(port) > 0, addr
all_addrs = actual_inputs + actual_outputs
assert len(set(all_addrs)) == len(all_addrs), all_addrs
@pytest.mark.timeout(30)
def test_gather_actual_addresses_child_crash_before_report():
"""A child that exits before sending its endpoints must surface a
clear ``RuntimeError`` rather than hang or return ``None`` slots."""
host = "127.0.0.1"
num_servers = 2
placeholder_inputs = [
get_engine_client_zmq_addr(local_only=False, host=host)
for _ in range(num_servers)
]
placeholder_outputs = [
get_engine_client_zmq_addr(local_only=False, host=host)
for _ in range(num_servers)
]
sock = socket.socket()
manager = APIServerProcessManager(
listen_address=f"tcp://{host}:0",
sock=sock,
args="test_args",
num_servers=num_servers,
input_addresses=placeholder_inputs,
output_addresses=placeholder_outputs,
# mock_run_api_server_worker exits without touching
# ``actual_address_pipe`` — simulates a child that dies before
# reporting its bound addresses.
target_server_fn=mock_run_api_server_worker,
)
try:
# Sentinel-first vs pipe-EOF-first both produce "reporting".
with pytest.raises(RuntimeError, match="reporting"):
manager.gather_actual_addresses(timeout=10.0)
finally:
manager.shutdown()
time.sleep(0.2)
sock.close()
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,919 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from unittest.mock import patch
import pytest
from openai_harmony import Author, Message, Role, TextContent
from vllm.entrypoints.openai.responses.context import (
HarmonyContext,
SimpleContext,
TurnMetrics,
)
from vllm.outputs import CompletionOutput, RequestOutput
from vllm.parser.harmony import ChunkResult, HarmonyParser, Segment
def create_mock_request_output(
prompt_token_ids=None,
output_token_ids=None,
num_cached_tokens=0,
finished=True,
):
"""Helper function to create a mock RequestOutput object for testing."""
outputs = []
token_ids = output_token_ids if output_token_ids is not None else []
outputs = [
CompletionOutput(
index=0,
text="Test output",
token_ids=token_ids,
cumulative_logprob=0.0,
logprobs=None,
finish_reason=None,
stop_reason=None,
)
]
return RequestOutput(
request_id="test-id",
prompt="Test prompt",
prompt_token_ids=prompt_token_ids,
prompt_logprobs=None,
outputs=outputs,
finished=finished,
num_cached_tokens=num_cached_tokens,
)
async def generate_mock_outputs(
num_turns, prompt_token_counts, output_token_counts, cached_token_counts=None
):
"""Generate a sequence of mock RequestOutput objects to simulate multiple
turns."""
if cached_token_counts is None:
cached_token_counts = [0] * num_turns
for i in range(num_turns):
# Create mock prompt token IDs and output token IDs
prompt_token_ids = list(range(1, prompt_token_counts[i] + 1))
output_token_ids = list(range(1, output_token_counts[i] + 1))
# Create and yield the RequestOutput
yield create_mock_request_output(
prompt_token_ids=prompt_token_ids,
output_token_ids=output_token_ids,
num_cached_tokens=cached_token_counts[i],
)
class FakeHarmonyParser(HarmonyParser):
def __init__(self):
# Skip HarmonyParser initialization and script outputs directly.
self.reasoning_parser = None
self.tool_parser = None
self._chunk_results: list[ChunkResult] = []
self._flush_results: list[list[Segment]] = []
self.processed_chunks: list[list[int]] = []
def enqueue_chunk_result(
self,
segments: list[Segment] | None = None,
reasoning_token_count: int = 0,
) -> None:
self._chunk_results.append(
ChunkResult(
segments=[] if segments is None else segments,
reasoning_token_count=reasoning_token_count,
)
)
def enqueue_flush_result(self, segment: list[Segment]) -> None:
self._flush_results.append(segment)
def process_chunk(self, token_ids) -> ChunkResult:
self.processed_chunks.append(list(token_ids))
if self._chunk_results:
return self._chunk_results.pop(0)
return ChunkResult(segments=[], reasoning_token_count=0)
def flush(self) -> list[Segment]:
if self._flush_results:
return self._flush_results.pop(0)
return []
def make_harmony_context(
messages=None, available_tools=None, function_tool_names=None
) -> tuple[HarmonyContext, FakeHarmonyParser]:
fake_parser = FakeHarmonyParser()
context = HarmonyContext(
messages=[] if messages is None else messages,
available_tools=[] if available_tools is None else available_tools,
function_tool_names=function_tool_names,
response_parser=fake_parser,
)
return context, fake_parser
def test_single_turn_token_counting():
"""Test token counting behavior for a single turn."""
# Create a context
context, _ = make_harmony_context()
# Create a mock RequestOutput with specific token counts
mock_output = create_mock_request_output(
prompt_token_ids=[1, 2, 3, 4, 5], # 5 prompt tokens
output_token_ids=[6, 7, 8], # 3 output tokens
num_cached_tokens=2, # 2 cached tokens
)
# Append the output to the context
context.append_output(mock_output)
# Verify the token counts
assert context.num_prompt_tokens == 5
assert context.num_output_tokens == 3
assert context.num_cached_tokens == 2
assert context.num_tool_output_tokens == 0 # No tool tokens in first turn
# Verify internal state tracking
assert not context.is_first_turn
assert len(context.all_turn_metrics) == 1
previous_turn = context.all_turn_metrics[0]
assert previous_turn.input_tokens == 5
assert previous_turn.output_tokens == 3
assert previous_turn.cached_input_tokens == 2
assert previous_turn.tool_output_tokens == 0
@pytest.mark.asyncio
async def test_multi_turn_token_counting():
"""Test token counting behavior across multiple turns with tool output."""
# Create a context
context, _ = make_harmony_context(available_tools=["browser"])
# Simulate a conversation with 3 turns
# Turn 1: prefill 5, decode 3, tool 7
# Turn 2: prefill 15, cached 5, decode 4, tool 1
# Turn 3: prefill 20, cached 15, decode 5
prompt_token_counts = [5, 15, 20]
output_token_counts = [3, 4, 5]
cached_token_counts = [0, 5, 15]
mock_generator = generate_mock_outputs(
3, prompt_token_counts, output_token_counts, cached_token_counts
)
# First turn - initial prompt and response
mock_output1 = await anext(mock_generator)
context.append_output(mock_output1)
# At this point, we should have 5 prompt tokens and 3 output tokens
assert context.num_prompt_tokens == 5
assert context.num_output_tokens == 3
assert context.num_tool_output_tokens == 0
# Second turn - after tool output
mock_output2 = await anext(mock_generator)
context.append_output(mock_output2)
# Current prompt tokens (15) - last_turn_input_tokens (5) -
# last_turn_output_tokens (3) = 7
expected_tool_output = 7
assert context.num_prompt_tokens == 5 + 15
assert context.num_output_tokens == 3 + 4
assert context.num_tool_output_tokens == expected_tool_output
assert context.num_cached_tokens == 5
# Third turn - final response
mock_output3 = await anext(mock_generator)
context.append_output(mock_output3)
# Additional tool output tokens from third turn:
# Current prompt (20) - last_turn_input_tokens (15) -
# last_turn_output_tokens (4) = 1
expected_tool_output = 7 + 1
assert context.num_prompt_tokens == 5 + 15 + 20
assert context.num_output_tokens == 3 + 4 + 5
assert context.num_tool_output_tokens == expected_tool_output
assert context.num_cached_tokens == 5 + 15
# Validate all turn metrics
assert len(context.all_turn_metrics) == 3
for i, turn in enumerate(context.all_turn_metrics):
assert turn.input_tokens == prompt_token_counts[i]
assert turn.output_tokens == output_token_counts[i]
assert turn.cached_input_tokens == cached_token_counts[i]
assert context.all_turn_metrics[1].tool_output_tokens == 7
assert context.all_turn_metrics[2].tool_output_tokens == 1
def test_empty_output_tokens():
"""Test behavior when RequestOutput has empty output tokens."""
context, _ = make_harmony_context()
# Create a RequestOutput with empty output tokens
mock_output = create_mock_request_output(
prompt_token_ids=[1, 2, 3], # 3 prompt tokens
output_token_ids=[], # Empty output tokens list
num_cached_tokens=1,
)
context.append_output(mock_output)
# Should handle empty outputs gracefully
assert context.num_prompt_tokens == 3
assert context.num_output_tokens == 0 # No output tokens
assert context.num_cached_tokens == 1
assert context.num_tool_output_tokens == 0
def test_missing_prompt_token_ids():
"""Test behavior when RequestOutput has None prompt_token_ids."""
context, _ = make_harmony_context()
mock_output = create_mock_request_output(
prompt_token_ids=None, # No prompt token IDs
output_token_ids=[1, 2], # 2 output tokens
num_cached_tokens=0,
)
# Logger.error will be called, but we don't need to check for warnings
# here Just ensure it doesn't raise an exception
context.append_output(mock_output)
# Should handle missing prompt tokens gracefully
assert context.num_prompt_tokens == 0
assert context.num_output_tokens == 2
assert context.num_cached_tokens == 0
assert context.num_tool_output_tokens == 0
def test_reasoning_tokens_counting():
"""Test that reasoning tokens are counted correctly."""
context, parser = make_harmony_context()
parser.enqueue_chunk_result(reasoning_token_count=4)
mock_output = create_mock_request_output(
prompt_token_ids=[1, 2, 3],
output_token_ids=[4, 5, 6, 7], # 4 tokens, all in reasoning
num_cached_tokens=0,
)
context.append_output(mock_output)
# All output tokens should be counted as reasoning
assert context.num_reasoning_tokens == 4
assert context.num_output_tokens == 4
def test_preamble_tokens_not_counted_as_reasoning():
"""Preambles (commentary with no recipient) are visible user text,
not hidden reasoning. They must NOT inflate num_reasoning_tokens."""
context, parser = make_harmony_context()
parser.enqueue_chunk_result(reasoning_token_count=0)
mock_output = create_mock_request_output(
prompt_token_ids=[1, 2, 3],
output_token_ids=[4, 5, 6],
num_cached_tokens=0,
)
context.append_output(mock_output)
assert context.num_reasoning_tokens == 0
assert context.num_output_tokens == 3
def test_commentary_with_recipient_counted_as_reasoning():
"""Commentary directed at a tool (recipient != None) is hidden from
the user, so it should still count as reasoning tokens."""
context, parser = make_harmony_context()
parser.enqueue_chunk_result(reasoning_token_count=3)
mock_output = create_mock_request_output(
prompt_token_ids=[1, 2, 3],
output_token_ids=[4, 5, 6],
num_cached_tokens=0,
)
context.append_output(mock_output)
assert context.num_reasoning_tokens == 3
assert context.num_output_tokens == 3
def test_zero_tokens_edge_case():
"""Test behavior with all zero token counts."""
context, _ = make_harmony_context()
# Create a request with empty lists (not None) for both prompt and
# output tokens
mock_output = create_mock_request_output(
prompt_token_ids=[], # Empty prompt tokens
output_token_ids=[], # Empty output tokens
num_cached_tokens=0,
)
context.append_output(mock_output)
# All counts should be zero
assert context.num_prompt_tokens == 0
assert context.num_output_tokens == 0
assert context.num_cached_tokens == 0
assert context.num_tool_output_tokens == 0
assert context.num_reasoning_tokens == 0
@pytest.mark.asyncio
async def test_single_turn_no_tool_output():
"""Test that first turn never generates tool output tokens."""
context, _ = make_harmony_context(available_tools=["browser"])
# Even with large prompt in first turn, no tool tokens should be counted
mock_output = create_mock_request_output(
prompt_token_ids=list(range(100)), # 100 tokens
output_token_ids=[1, 2, 3],
num_cached_tokens=0,
)
context.append_output(mock_output)
# First turn should never have tool output tokens
assert context.num_tool_output_tokens == 0
assert context.is_first_turn is False # Should be updated after first turn
@pytest.mark.asyncio
async def test_negative_tool_tokens_edge_case():
"""Test edge case where calculation could result in negative tool
tokens. We should log an error and clamp the value to 0."""
# Use patch to check if logger.error was called
with patch("vllm.entrypoints.openai.responses.context.logger.error") as mock_log:
context, _ = make_harmony_context(available_tools=["browser"])
# First turn
mock_output1 = create_mock_request_output(
prompt_token_ids=list(range(10)), # 10 tokens
output_token_ids=[1, 2, 3, 4, 5], # 5 tokens
)
context.append_output(mock_output1)
# Second turn with fewer new tokens than previous output
# This could happen in edge cases with aggressive caching
mock_output2 = create_mock_request_output(
prompt_token_ids=list(range(12)), # 12 tokens (only 2 new)
output_token_ids=[6, 7], # 2 tokens
)
context.append_output(mock_output2)
# Calculated negative tool tokens (12 - 10 - 5 = -3) should be clamped
# to 0 and an error should be logged
assert context.num_tool_output_tokens == 0
assert context.num_prompt_tokens == 10 + 12
assert context.num_output_tokens == 5 + 2
# Verify the error was logged properly
mock_log.assert_called_once()
# Extract the actual log message and arguments from the call
args, _ = mock_log.call_args
log_message = args[0]
# Check for key parts of the message
assert "Negative tool output tokens calculated" in log_message
assert "-3" in str(args) # Check that -3 is in the arguments
@pytest.mark.asyncio
async def test_streaming_multi_turn_token_counting():
"""Test token counting for streaming multi-turn conversations.
This test focuses on how HarmonyContext counts tokens in a
multi-turn conversation with streaming (token-by-token) outputs and
message boundaries.
"""
# Create a streaming context
context, parser = make_harmony_context(available_tools=["browser"])
num_prompt_tokens = [3, 8, 13]
num_output_tokens = [3, 3, 2]
num_cached_tokens = [0, 3, 8]
# Simulate three turns of conversation:
# Turn 1: stream tokens one by one, then finish the message
# Turn 2: new prompt, stream more tokens with a reasoning segment
# Turn 3: new prompt with tool output and cached tokens
# First turn: 3 tokens streamed one by one
# First token of first turn
context.append_output(
create_mock_request_output(
prompt_token_ids=[1, 2, 3], # 3 prompt tokens
output_token_ids=[101], # Single token
num_cached_tokens=num_cached_tokens[0],
finished=False, # Not end of message yet
)
)
# Second token of first turn
context.append_output(
create_mock_request_output(
output_token_ids=[102],
finished=False,
)
)
# Last token of first turn (finished=True signals end of message)
context.append_output(
create_mock_request_output(
output_token_ids=[103],
finished=True, # End of message
)
)
# Check token counts after first turn
assert context.num_prompt_tokens == 3 # Initial prompt tokens
assert context.num_output_tokens == 3 # Three output tokens
assert context.num_cached_tokens == 0
assert context.num_tool_output_tokens == 0 # No tool output in first turn
assert context.first_tok_of_message is True # Ready for next message
# First token of second turn
parser.enqueue_chunk_result(reasoning_token_count=1)
context.append_output(
create_mock_request_output(
prompt_token_ids=[
1,
2,
3,
101,
102,
103,
4,
5,
], # 8 tokens (includes previous)
output_token_ids=[201],
num_cached_tokens=num_cached_tokens[1], # Some tokens cached
finished=False,
)
)
# More tokens in reasoning channel
parser.enqueue_chunk_result(reasoning_token_count=1)
context.append_output(
create_mock_request_output(
output_token_ids=[202],
finished=False,
)
)
parser.enqueue_chunk_result(reasoning_token_count=1)
context.append_output(
create_mock_request_output(
output_token_ids=[203],
finished=True, # End of reasoning message
)
)
# Check counts after second turn (reasoning message)
assert context.num_prompt_tokens == 3 + 8 # Initial + second prompt
assert context.num_output_tokens == 3 + 3 # First turn + second turn
assert context.num_reasoning_tokens == 3 # All tokens in analysis channel
assert context.num_cached_tokens == 3 # Cached tokens from second turn
# Formula: this turn prompt tokens - last turn prompt - last turn output
expected_tool_tokens = 8 - 3 - 3 # = 2
assert context.num_tool_output_tokens == expected_tool_tokens
# Third turn (with more cached tokens)
context.append_output(
create_mock_request_output(
prompt_token_ids=[
1,
2,
3,
101,
102,
103,
4,
5,
201,
202,
203,
6,
7,
], # 13 tokens
output_token_ids=[301],
num_cached_tokens=num_cached_tokens[2], # More cached tokens
finished=False,
)
)
context.append_output(
create_mock_request_output(
output_token_ids=[302],
finished=True,
)
)
# Final token counts check
assert context.num_prompt_tokens == sum(num_prompt_tokens) # All prompts
assert context.num_output_tokens == sum(num_output_tokens) # All outputs
assert context.num_reasoning_tokens == 3 # Unchanged from second turn
assert context.num_cached_tokens == sum(
num_cached_tokens
) # Accumulated cached tokens
# Additional tool tokens from third turn
# Formula: this turn prompt - last turn prompt - last turn output
additional_tool_tokens = 13 - 8 - 3 # = 2
assert (
context.num_tool_output_tokens == expected_tool_tokens + additional_tool_tokens
)
# Validate all turn metrics
assert len(context.all_turn_metrics) == 3
for i, turn in enumerate(context.all_turn_metrics):
assert turn.input_tokens == num_prompt_tokens[i]
assert turn.output_tokens == num_output_tokens[i]
assert turn.cached_input_tokens == num_cached_tokens[i]
assert context.all_turn_metrics[1].tool_output_tokens == 2
assert context.all_turn_metrics[2].tool_output_tokens == 2
@pytest.mark.asyncio
async def test_streaming_message_synchronization():
"""Completed messages from append-local and flush segments sync into context."""
# Create a streaming context with some initial messages
initial_messages = [
Message(
author=Author(role=Role.USER, name="user"),
content=[TextContent(text="Hello")],
recipient=Role.ASSISTANT,
)
]
context, parser = make_harmony_context(messages=initial_messages)
# Verify initial state
assert len(context._messages) == 1
assert context.num_init_messages == 1
response_text = "First response"
message = Message(
author=Author(role=Role.ASSISTANT, name="assistant"),
content=[TextContent(text=response_text)],
recipient=Role.USER,
)
parser.enqueue_chunk_result(
segments=[
Segment(
channel="commentary",
recipient=None,
delta="",
completed_message=message,
)
]
)
# This should sync the completed message from the latest append
context.append_output(
create_mock_request_output(
prompt_token_ids=[1, 2, 3], output_token_ids=[101], finished=False
)
)
# Verify that messages were synchronized
assert len(context._messages) == 2
# Verify the new messages were added correctly
assert context._messages[1].content[0].text == response_text
messages_minus_init = len(context._messages) - context.num_init_messages
assert messages_minus_init == 1
response_text = "Second response"
message = Message(
author=Author(role=Role.ASSISTANT, name="assistant"),
content=[TextContent(text=response_text)],
recipient=Role.USER,
)
flush_segments = [
Segment(
channel="final",
recipient=None,
delta=response_text,
completed_message=None,
),
Segment(
channel="final",
recipient=None,
delta="",
completed_message=message,
),
]
parser.enqueue_flush_result(flush_segments)
# Create another output to trigger synchronization via flush()
context.append_output(
create_mock_request_output(
prompt_token_ids=[1, 2, 3], output_token_ids=[102], finished=True
)
)
# Verify the flushed response was added, num_init_messages is still 1
assert len(context._messages) == 3
assert context.num_init_messages == 1
assert context._messages[2].content[0].text == response_text
assert context.last_append_flush_status is True
assert len(context.last_append_segments) == 2
assert context.last_append_segments[-2].delta == response_text
assert context.last_append_segments[-1].completed_message is message
def test_turn_metrics_copy_and_reset():
"""Test TurnMetrics copy and reset methods work correctly."""
# Create a TurnMetrics with specific values
original_metrics = TurnMetrics(
input_tokens=10,
output_tokens=20,
cached_input_tokens=5,
tool_output_tokens=3,
)
# Test copy functionality
copied_metrics = original_metrics.copy()
# Verify copy has same values
assert copied_metrics.input_tokens == 10
assert copied_metrics.output_tokens == 20
assert copied_metrics.cached_input_tokens == 5
assert copied_metrics.tool_output_tokens == 3
# Verify they are separate objects
assert copied_metrics is not original_metrics
# Modify copy to ensure independence
copied_metrics.input_tokens = 999
assert original_metrics.input_tokens == 10 # Original unchanged
assert copied_metrics.input_tokens == 999
# Test reset functionality
original_metrics.reset()
# Verify all fields are reset to zero
assert original_metrics.input_tokens == 0
assert original_metrics.output_tokens == 0
assert original_metrics.cached_input_tokens == 0
assert original_metrics.tool_output_tokens == 0
# Verify copied metrics are unaffected by reset
assert copied_metrics.input_tokens == 999
assert copied_metrics.output_tokens == 20
assert copied_metrics.cached_input_tokens == 5
assert copied_metrics.tool_output_tokens == 3
# ==================== SimpleContext Tests ====================
def create_simple_context_output(
text="",
token_ids=None,
prompt="Test prompt",
prompt_token_ids=None,
num_cached_tokens=0,
logprobs=None,
finished=True,
):
"""Helper to create a RequestOutput with customizable text for
SimpleContext tests."""
if token_ids is None:
token_ids = []
return RequestOutput(
request_id="test-id",
prompt=prompt,
prompt_token_ids=prompt_token_ids,
prompt_logprobs=None,
outputs=[
CompletionOutput(
index=0,
text=text,
token_ids=token_ids,
cumulative_logprob=0.0,
logprobs=logprobs,
finish_reason=None,
stop_reason=None,
)
],
finished=finished,
num_cached_tokens=num_cached_tokens,
)
def test_simple_context_output_messages_empty():
"""output_messages should be empty before any output is appended."""
context = SimpleContext()
assert context.output_messages == []
def test_simple_context_output_messages_single_call():
"""Non-streaming: single append_output produces a single output message."""
context = SimpleContext()
output = create_simple_context_output(
text="Hello world",
token_ids=[10, 20, 30],
prompt_token_ids=[1, 2, 3],
)
context.append_output(output)
messages = context.output_messages
assert len(messages) == 1
assert messages[0].message == "Hello world"
assert messages[0].tokens == [10, 20, 30]
assert messages[0].type == "raw_message_tokens"
def test_simple_context_output_messages_streaming_consolidation():
"""Streaming: multiple append_output calls consolidate into one message."""
context = SimpleContext()
# Simulate 3 streaming deltas
context.append_output(
create_simple_context_output(
text="Hello",
token_ids=[10],
prompt_token_ids=[1, 2, 3],
)
)
context.append_output(
create_simple_context_output(
text=" world",
token_ids=[20],
prompt_token_ids=[1, 2, 3],
)
)
context.append_output(
create_simple_context_output(
text="!",
token_ids=[30],
prompt_token_ids=[1, 2, 3],
)
)
messages = context.output_messages
assert len(messages) == 1
assert messages[0].message == "Hello world!"
assert messages[0].tokens == [10, 20, 30]
def test_simple_context_output_messages_many_deltas():
"""Streaming with many small deltas still produces a single message."""
context = SimpleContext()
words = ["The", " quick", " brown", " fox", " jumps"]
for i, word in enumerate(words):
context.append_output(
create_simple_context_output(
text=word,
token_ids=[100 + i],
prompt_token_ids=[1, 2],
)
)
messages = context.output_messages
assert len(messages) == 1
assert messages[0].message == "The quick brown fox jumps"
assert messages[0].tokens == [100, 101, 102, 103, 104]
def test_simple_context_input_messages():
"""input_messages is populated on the first append_output call."""
context = SimpleContext()
assert context.input_messages == []
context.append_output(
create_simple_context_output(
text="Hi",
token_ids=[10],
prompt="My prompt text",
prompt_token_ids=[1, 2, 3],
)
)
assert len(context.input_messages) == 1
assert context.input_messages[0].message == "My prompt text"
assert context.input_messages[0].tokens == [1, 2, 3]
# Second call should not add another input message
context.append_output(
create_simple_context_output(
text=" there",
token_ids=[20],
prompt="My prompt text",
prompt_token_ids=[1, 2, 3],
)
)
assert len(context.input_messages) == 1
def test_simple_context_token_counting():
"""Token counting accumulates across streaming deltas."""
context = SimpleContext()
context.append_output(
create_simple_context_output(
text="a",
token_ids=[10, 11],
prompt_token_ids=[1, 2, 3, 4, 5],
num_cached_tokens=2,
)
)
context.append_output(
create_simple_context_output(
text="b",
token_ids=[12],
prompt_token_ids=[1, 2, 3, 4, 5],
num_cached_tokens=2,
)
)
assert context.num_prompt_tokens == 5
assert context.num_output_tokens == 3 # 2 + 1
assert context.num_cached_tokens == 2
def test_simple_context_final_output():
"""final_output reconstructs accumulated text and token_ids."""
context = SimpleContext()
context.append_output(
create_simple_context_output(
text="foo",
token_ids=[1, 2],
prompt_token_ids=[10],
)
)
context.append_output(
create_simple_context_output(
text="bar",
token_ids=[3],
prompt_token_ids=[10],
)
)
final = context.final_output
assert final is not None
assert final.outputs[0].text == "foobar"
assert final.outputs[0].token_ids == (1, 2, 3)
def test_simple_context_output_messages_empty_text_with_tokens():
"""output_messages should be returned when tokens exist even if text is
empty (e.g. special tokens)."""
context = SimpleContext()
context.append_output(
create_simple_context_output(
text="",
token_ids=[99],
prompt_token_ids=[1],
)
)
messages = context.output_messages
assert len(messages) == 1
assert messages[0].message == ""
assert messages[0].tokens == [99]
def test_simple_context_output_messages_no_mutation():
"""Each call to output_messages returns a fresh list; callers can't
corrupt internal state."""
context = SimpleContext()
context.append_output(
create_simple_context_output(
text="hello",
token_ids=[1],
prompt_token_ids=[10],
)
)
msgs1 = context.output_messages
msgs2 = context.output_messages
assert msgs1 is not msgs2
assert msgs1[0].message == msgs2[0].message
# Appending more output updates the property
context.append_output(
create_simple_context_output(
text=" world",
token_ids=[2],
prompt_token_ids=[10],
)
)
msgs3 = context.output_messages
assert len(msgs3) == 1
assert msgs3[0].message == "hello world"
assert msgs3[0].tokens == [1, 2]
@@ -0,0 +1,143 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
grpc = pytest.importorskip("grpc")
health_pb2 = pytest.importorskip("grpc_health.v1.health_pb2")
VllmHealthServicer = pytest.importorskip(
"smg_grpc_servicer.vllm.health_servicer"
).VllmHealthServicer
SERVING = health_pb2.HealthCheckResponse.SERVING
NOT_SERVING = health_pb2.HealthCheckResponse.NOT_SERVING
SERVICE_UNKNOWN = health_pb2.HealthCheckResponse.SERVICE_UNKNOWN
@pytest.fixture
def async_llm():
mock = MagicMock()
mock.check_health = AsyncMock()
return mock
@pytest.fixture
def context():
return MagicMock(spec=grpc.aio.ServicerContext)
@pytest.fixture
def servicer(async_llm):
return VllmHealthServicer(async_llm)
@pytest.fixture
def request_msg():
msg = MagicMock()
msg.service = ""
return msg
# -- Check() tests --
@pytest.mark.asyncio
async def test_check_serving_overall(servicer, request_msg, context, async_llm):
request_msg.service = ""
response = await servicer.Check(request_msg, context)
assert response.status == SERVING
async_llm.check_health.assert_awaited_once()
@pytest.mark.asyncio
async def test_check_serving_vllm_service(servicer, request_msg, context, async_llm):
request_msg.service = "vllm.grpc.engine.VllmEngine"
response = await servicer.Check(request_msg, context)
assert response.status == SERVING
async_llm.check_health.assert_awaited_once()
@pytest.mark.asyncio
async def test_check_not_serving_engine_errored(
servicer, request_msg, context, async_llm
):
async_llm.check_health = AsyncMock(side_effect=Exception("engine dead"))
request_msg.service = ""
response = await servicer.Check(request_msg, context)
assert response.status == NOT_SERVING
@pytest.mark.asyncio
async def test_check_not_serving_shutting_down(
servicer, request_msg, context, async_llm
):
servicer.set_not_serving()
request_msg.service = ""
response = await servicer.Check(request_msg, context)
assert response.status == NOT_SERVING
async_llm.check_health.assert_not_awaited()
@pytest.mark.asyncio
async def test_check_unknown_service_status(servicer, request_msg, context):
request_msg.service = "nonexistent.Service"
response = await servicer.Check(request_msg, context)
assert response.status == SERVICE_UNKNOWN
@pytest.mark.asyncio
async def test_check_unknown_service_grpc_code(servicer, request_msg, context):
request_msg.service = "fake.Svc"
await servicer.Check(request_msg, context)
context.set_code.assert_called_once_with(grpc.StatusCode.NOT_FOUND)
context.set_details.assert_called_once()
details_arg = context.set_details.call_args[0][0]
assert "fake.Svc" in details_arg
@pytest.mark.asyncio
@patch("smg_grpc_servicer.vllm.health_servicer.logger")
async def test_check_logs_exception_on_error(
mock_logger, servicer, request_msg, context, async_llm
):
async_llm.check_health = AsyncMock(side_effect=Exception("engine exploded"))
request_msg.service = ""
await servicer.Check(request_msg, context)
mock_logger.exception.assert_called_once()
log_args = mock_logger.exception.call_args
assert "service" in str(log_args).lower()
# -- Watch() tests --
@pytest.mark.asyncio
async def test_watch_yields_serving(servicer, request_msg, context, async_llm):
request_msg.service = ""
watch_iter = servicer.Watch(request_msg, context)
first = await anext(watch_iter.__aiter__())
assert first.status == SERVING
@pytest.mark.asyncio
async def test_watch_yields_not_serving(servicer, request_msg, context, async_llm):
async_llm.check_health = AsyncMock(side_effect=Exception("engine down"))
request_msg.service = ""
watch_iter = servicer.Watch(request_msg, context)
first = await anext(watch_iter.__aiter__())
assert first.status == NOT_SERVING
@pytest.mark.asyncio
async def test_watch_unknown_service(servicer, request_msg, context):
request_msg.service = "fake.Service"
results = []
async for response in servicer.Watch(request_msg, context):
results.append(response)
assert len(results) == 1
assert results[0].status == SERVICE_UNKNOWN
# Watch returns SERVICE_UNKNOWN in the response body (not as a gRPC error
# code) so the stream terminates normally -- unlike Check, which sets
# NOT_FOUND on the gRPC context for unknown services.
context.set_code.assert_not_called()
@@ -0,0 +1,111 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Unit tests for the `vllm launch` CLI subcommand."""
import argparse
from unittest.mock import patch
import pytest
from vllm.entrypoints.cli.launch import (
LaunchSubcommand,
RenderSubcommand,
cmd_init,
)
from vllm.utils.argparse_utils import FlexibleArgumentParser
@pytest.fixture
def launch_parser():
parser = FlexibleArgumentParser(description="test")
subparsers = parser.add_subparsers(required=False, dest="subparser")
LaunchSubcommand().subparser_init(subparsers)
return parser
def test_subcommand_name():
assert LaunchSubcommand().name == "launch"
def test_cmd_init_returns_subcommand():
result = cmd_init()
assert len(result) == 1
assert isinstance(result[0], LaunchSubcommand)
# -- Parsing: `vllm launch render` --
def test_parse_launch_render(launch_parser):
args = launch_parser.parse_args(["launch", "render", "--model", "test-model"])
assert args.launch_component == "render"
def test_parse_launch_requires_component(launch_parser):
with pytest.raises(SystemExit):
launch_parser.parse_args(["launch", "--model", "test-model"])
def test_parse_launch_invalid_component(launch_parser):
with pytest.raises(SystemExit):
launch_parser.parse_args(["launch", "unknown", "--model", "test-model"])
# -- Dispatch --
def test_cmd_launch_render_calls_run():
args = argparse.Namespace(model_tag=None, model="test-model")
with patch("vllm.entrypoints.cli.launch.uvloop.run") as mock_uvloop_run:
RenderSubcommand.cmd(args)
mock_uvloop_run.assert_called_once()
def test_cmd_launch_model_tag_overrides():
args = argparse.Namespace(
model_tag="tag-model",
model="original-model",
launch_command=lambda a: None,
)
LaunchSubcommand.cmd(args)
assert args.model == "tag-model"
def test_cmd_launch_model_tag_none():
args = argparse.Namespace(
model_tag=None,
model="original-model",
launch_command=lambda a: None,
)
LaunchSubcommand.cmd(args)
assert args.model == "original-model"
def test_cmd_dispatches():
called = {}
def fake_dispatch(args):
called["args"] = args
args = argparse.Namespace(launch_command=fake_dispatch)
LaunchSubcommand.cmd(args)
assert "args" in called
# -- Module registration --
def test_subparser_init_returns_parser():
parser = FlexibleArgumentParser(description="test")
subparsers = parser.add_subparsers(required=False, dest="subparser")
result = LaunchSubcommand().subparser_init(subparsers)
assert isinstance(result, FlexibleArgumentParser)
def test_launch_registered_in_main():
"""Verify that launch module is importable as a CLI module."""
import vllm.entrypoints.cli.launch as launch_module
assert hasattr(launch_module, "cmd_init")
subcmds = launch_module.cmd_init()
assert any(s.name == "launch" for s in subcmds)