[Feature] Support output modalities control per request (#298)

Signed-off-by: Gaohan123 <hgaoaf@connect.ust.hk>
This commit is contained in:
Gao Han authored and GitHub committed 2025-12-17 17:19:56 +08:00
1 parent 2b6d8f17ba
commit 8722c6374c
30 files changed
+349 -61

No files matched your search

+5
View File
@@ -79,6 +79,7 @@ class OmniRunner:
images: PromptImageInput = None,
videos: PromptVideoInput = None,
mm_processor_kwargs: dict[str, Any] | None = None,
modalities: list[str] | None = None,
) -> list[dict[str, Any]]:
"""
Construct Omni input format from prompts and multimodal data.
@@ -175,6 +176,8 @@ class OmniRunner:
input_dict: dict[str, Any] = {"prompt": full_prompt}
if multi_modal_data:
input_dict["multi_modal_data"] = multi_modal_data
if modalities:
input_dict["modalities"] = modalities
if mm_processor_kwargs:
input_dict["mm_processor_kwargs"] = mm_processor_kwargs
@@ -213,6 +216,7 @@ class OmniRunner:
images: PromptImageInput = None,
videos: PromptVideoInput = None,
mm_processor_kwargs: dict[str, Any] | None = None,
modalities: list[str] | None = None,
) -> list[Any]:
"""
Convenience method to generate with multimodal inputs.
@@ -236,6 +240,7 @@ class OmniRunner:
images=images,
videos=videos,
mm_processor_kwargs=mm_processor_kwargs,
modalities=modalities,
)
return self.generate(omni_inputs, sampling_params_list)
@@ -85,3 +85,46 @@ def test_mixed_modalities_to_audio(omni_runner: type[OmniRunner], test_config: t
audio_tensor = audio_output.request_output[0].multimodal_output["audio"]
assert audio_tensor is not None
assert audio_tensor.numel() > 0
@pytest.mark.core_model
@pytest.mark.parametrize("test_config", test_params)
@create_new_process_for_each_test()
def test_mixed_modalities_to_text_only(omni_runner: type[OmniRunner], test_config: tuple[str, str]) -> None:
"""Test processing audio, image, and video together, generating audio output."""
model, stage_config_path = test_config
with omni_runner(model, seed=42, stage_configs_path=stage_config_path) as runner:
# Prepare multimodal inputs
question = "What is recited in the audio? What is in this image? Describe the video briefly."
audio = AudioAsset("mary_had_lamb").audio_and_sample_rate
audio = (audio[0][: 16000 * 5], audio[1]) # Trim to first 5 seconds
image = convert_image_mode(ImageAsset("cherry_blossom").pil_image.resize((128, 128)), "RGB")
video = VideoAsset(name="baby_reading", num_frames=4).np_ndarrays
modalities = ["text"]
outputs = runner.generate_multimodal(
prompts=question,
audios=audio,
images=image,
videos=video,
modalities=modalities,
)
# Verify we got outputs from multiple stages
assert len(outputs) > 0
for stage_output in outputs:
assert stage_output.final_output_type != "audio"
# Find and verify text output (thinker stage)
text_output = None
for stage_output in outputs:
if stage_output.final_output_type == "text":
text_output = stage_output
break
assert text_output is not None
assert len(text_output.request_output) > 0
text_content = text_output.request_output[0].outputs[0].text
assert text_content is not None
assert len(text_content.strip()) > 0
+31 -6
View File
@@ -1,3 +1,4 @@
import uuid
import warnings
from queue import Empty, Queue
from typing import Any
@@ -639,17 +640,25 @@ def test_generate_pipeline_and_final_outputs(monkeypatch, fake_stage_config):
monkeypatch.setattr(omni_llm_module, "load_stage_configs_from_model", _fake_loader)
monkeypatch.setattr(omni_llm_module, "OmniStage", lambda cfg: _FakeStage(cfg))
# Mock uuid.uuid4() to return a predictable value for request ID generation
test_uuid = uuid.UUID("00000000-0000-0000-0000-000000000000")
monkeypatch.setattr(uuid, "uuid4", lambda: test_uuid)
monkeypatch.setattr(omni_llm_module, "uuid", uuid)
from vllm_omni.entrypoints.omni_llm import OmniLLM
llm = OmniLLM(model="any", init_timeout=1)
# Generate the expected request ID format: "0_<uuid>"
expected_request_id = f"0_{test_uuid}"
# Simulate worker behavior: manually put results into output queues
# Note: We put results before calling generate, which simulates worker processes
# that have already completed. The polling loop will collect them in stage order.
# Stage 0 output (will be collected first)
llm.stage_list[0]._out_q.put_nowait(
{
"request_id": 0,
"request_id": expected_request_id,
"engine_outputs": [{"stage": 0, "text": "s0"}],
"metrics": {"num_tokens_out": 1, "stage_gen_time_ms": 10.0},
}
@@ -661,7 +670,7 @@ def test_generate_pipeline_and_final_outputs(monkeypatch, fake_stage_config):
# then stage 1 result will be collected.
llm.stage_list[1]._out_q.put_nowait(
{
"request_id": 0,
"request_id": expected_request_id,
"engine_outputs": [{"stage": 1, "text": "s1"}],
"metrics": {"num_tokens_out": 1, "stage_gen_time_ms": 10.0},
}
@@ -734,21 +743,29 @@ def test_generate_no_final_output_returns_empty(monkeypatch, fake_stage_config):
monkeypatch.setattr(omni_llm_module, "load_stage_configs_from_model", _fake_loader)
monkeypatch.setattr(omni_llm_module, "OmniStage", lambda cfg: _FakeStage(cfg))
# Mock uuid.uuid4() to return a predictable value for request ID generation
test_uuid = uuid.UUID("00000000-0000-0000-0000-000000000000")
monkeypatch.setattr(uuid, "uuid4", lambda: test_uuid)
monkeypatch.setattr(omni_llm_module, "uuid", uuid)
from vllm_omni.entrypoints.omni_llm import OmniLLM
llm = OmniLLM(model="any", init_timeout=1)
# Generate the expected request ID format: "0_<uuid>"
expected_request_id = f"0_{test_uuid}"
# Simulate worker behavior: put results into output queues
llm.stage_list[0]._out_q.put_nowait(
{
"request_id": 0,
"request_id": expected_request_id,
"engine_outputs": [{"stage": 0}],
"metrics": {"num_tokens_out": 1, "stage_gen_time_ms": 10.0},
}
)
llm.stage_list[1]._out_q.put_nowait(
{
"request_id": 0,
"request_id": expected_request_id,
"engine_outputs": [{"stage": 1}],
"metrics": {"num_tokens_out": 1, "stage_gen_time_ms": 10.0},
}
@@ -923,14 +940,22 @@ def test_generate_handles_error_messages(monkeypatch, fake_stage_config):
monkeypatch.setattr(omni_llm_module, "load_stage_configs_from_model", _fake_loader)
monkeypatch.setattr(omni_llm_module, "OmniStage", lambda cfg: _FakeStage(cfg))
# Mock uuid.uuid4() to return a predictable value for request ID generation
test_uuid = uuid.UUID("00000000-0000-0000-0000-000000000000")
monkeypatch.setattr(uuid, "uuid4", lambda: test_uuid)
monkeypatch.setattr(omni_llm_module, "uuid", uuid)
from vllm_omni.entrypoints.omni_llm import OmniLLM
llm = OmniLLM(model="any", init_timeout=1)
# Generate the expected request ID format: "0_<uuid>"
expected_request_id = f"0_{test_uuid}"
# Put error message in output queue
llm.stage_list[0]._out_q.put_nowait(
{
"request_id": 0,
"request_id": expected_request_id,
"error": "test error",
}
)
@@ -938,7 +963,7 @@ def test_generate_handles_error_messages(monkeypatch, fake_stage_config):
# (error handling continues the loop, so we need a valid result to finish)
llm.stage_list[0]._out_q.put_nowait(
{
"request_id": 0,
"request_id": expected_request_id,
"engine_outputs": [{"stage": 0, "text": "result"}],
"metrics": {"num_tokens_out": 1, "stage_gen_time_ms": 10.0},
}