|
| 1 | +"""Unit tests for Phase 1 foundation types (RFC #1601). |
| 2 | +
|
| 3 | +Note: Uses importlib to load modules directly, bypassing the vllm_omni |
| 4 | +package __init__ which requires the vllm base package. |
| 5 | +""" |
| 6 | + |
| 7 | +import importlib.util |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | +import torch |
| 13 | + |
| 14 | +# ── Load modules without triggering vllm_omni.__init__ ───────────── |
| 15 | + |
| 16 | +_ENGINE_DIR = Path(__file__).resolve().parents[2] / "vllm_omni" / "engine" |
| 17 | + |
| 18 | + |
| 19 | +def _load_module(name: str, filepath: Path): |
| 20 | + spec = importlib.util.spec_from_file_location(name, filepath) |
| 21 | + mod = importlib.util.module_from_spec(spec) |
| 22 | + sys.modules[name] = mod |
| 23 | + spec.loader.exec_module(mod) |
| 24 | + return mod |
| 25 | + |
| 26 | + |
| 27 | +_om_mod = _load_module( |
| 28 | + "vllm_omni.engine.output_modality", |
| 29 | + _ENGINE_DIR / "output_modality.py", |
| 30 | +) |
| 31 | +_mm_mod = _load_module( |
| 32 | + "vllm_omni.engine.mm_outputs", |
| 33 | + _ENGINE_DIR / "mm_outputs.py", |
| 34 | +) |
| 35 | + |
| 36 | +OutputModality = _om_mod.OutputModality |
| 37 | +TensorAccumulationStrategy = _om_mod.TensorAccumulationStrategy |
| 38 | +get_accumulation_strategy = _om_mod.get_accumulation_strategy |
| 39 | +MultimodalPayload = _mm_mod.MultimodalPayload |
| 40 | +MultimodalCompletionOutput = _mm_mod.MultimodalCompletionOutput |
| 41 | + |
| 42 | + |
| 43 | +def test_output_modality_parsing_and_flags(): |
| 44 | + """Test OutputModality enum: from_string, aliases, compounds, properties, and accumulation strategy.""" |
| 45 | + # Defaults |
| 46 | + assert OutputModality.from_string(None) == OutputModality.TEXT |
| 47 | + assert OutputModality.from_string("") == OutputModality.TEXT |
| 48 | + |
| 49 | + # Direct names and case insensitivity |
| 50 | + assert OutputModality.from_string("image") == OutputModality.IMAGE |
| 51 | + assert OutputModality.from_string("Audio") == OutputModality.AUDIO |
| 52 | + |
| 53 | + # Aliases |
| 54 | + assert OutputModality.from_string("speech") == OutputModality.AUDIO |
| 55 | + assert OutputModality.from_string("latents") == OutputModality.LATENT |
| 56 | + assert OutputModality.from_string("pixel_values") == OutputModality.IMAGE |
| 57 | + |
| 58 | + # Compound |
| 59 | + compound = OutputModality.from_string("text+image") |
| 60 | + assert compound.has_text and compound.has_multimodal |
| 61 | + |
| 62 | + # Flag properties |
| 63 | + assert OutputModality.TEXT.has_text and not OutputModality.TEXT.has_multimodal |
| 64 | + assert OutputModality.IMAGE.has_multimodal and not OutputModality.IMAGE.has_text |
| 65 | + |
| 66 | + # Accumulation strategy |
| 67 | + assert get_accumulation_strategy(OutputModality.AUDIO) == TensorAccumulationStrategy.CONCAT_LAST |
| 68 | + assert get_accumulation_strategy(OutputModality.IMAGE) == TensorAccumulationStrategy.CONCAT_DIM0 |
| 69 | + |
| 70 | + # Unknown raises |
| 71 | + with pytest.raises(ValueError, match="Unknown modality"): |
| 72 | + OutputModality.from_string("video") |
| 73 | + |
| 74 | + |
| 75 | +def test_multimodal_payload_and_completion_output(): |
| 76 | + """Test MultimodalPayload and MultimodalCompletionOutput wrapper.""" |
| 77 | + # Payload from_dict separates tensors and metadata |
| 78 | + data = {"waveform": torch.ones(1, 16000), "sample_rate": 16000} |
| 79 | + p = MultimodalPayload.from_dict(data) |
| 80 | + assert p is not None |
| 81 | + assert "waveform" in p.tensors and torch.equal(p.primary_tensor, data["waveform"]) |
| 82 | + assert p.metadata["sample_rate"] == 16000 |
| 83 | + assert not p.is_empty and len(p) == 1 |
| 84 | + |
| 85 | + # None/empty returns None |
| 86 | + assert MultimodalPayload.from_dict(None) is None |
| 87 | + assert MultimodalPayload.from_dict({}) is None |
| 88 | + |
| 89 | + wrapper = MultimodalCompletionOutput( |
| 90 | + multimodal_output=p, |
| 91 | + index=0, |
| 92 | + text="hello", |
| 93 | + token_ids=[], |
| 94 | + cumulative_logprob=None, |
| 95 | + logprobs=None, |
| 96 | + ) |
| 97 | + assert wrapper.text == "hello" |
| 98 | + assert wrapper.multimodal_output is p |
| 99 | + |
| 100 | + |
| 101 | +def test_output_modality_printed_examples(capsys): |
| 102 | + """Printed examples for output modality types.""" |
| 103 | + print("\n=== OutputModality Parsing ===") |
| 104 | + for s in [None, "", "image", "Audio", "speech", "latents", "pixel_values", "text+image"]: |
| 105 | + print(f" from_string({s!r:20s}) -> {OutputModality.from_string(s)}") |
| 106 | + |
| 107 | + print("\n=== Flag Properties ===") |
| 108 | + for m in [ |
| 109 | + OutputModality.TEXT, |
| 110 | + OutputModality.IMAGE, |
| 111 | + OutputModality.AUDIO, |
| 112 | + OutputModality.TEXT | OutputModality.IMAGE, |
| 113 | + ]: |
| 114 | + print(f" {str(m):40s} has_text={m.has_text} has_multimodal={m.has_multimodal}") |
| 115 | + |
| 116 | + print("\n=== Accumulation Strategies ===") |
| 117 | + for m in [OutputModality.AUDIO, OutputModality.IMAGE, OutputModality.LATENT]: |
| 118 | + print(f" {str(m):30s} -> {get_accumulation_strategy(m)}") |
| 119 | + |
| 120 | + print("\n=== MultimodalPayload ===") |
| 121 | + data = {"waveform": torch.ones(1, 16000), "sample_rate": 16000} |
| 122 | + p = MultimodalPayload.from_dict(data) |
| 123 | + print(" from_dict({waveform: tensor, sample_rate: 16000})") |
| 124 | + print(f" tensors keys : {list(p.tensors.keys())}") |
| 125 | + print(f" primary_tensor: shape={p.primary_tensor.shape}, dtype={p.primary_tensor.dtype}") |
| 126 | + print(f" metadata : {p.metadata}") |
| 127 | + print(f" is_empty={p.is_empty}, len={len(p)}") |
| 128 | + print(f" from_dict(None) -> {MultimodalPayload.from_dict(None)}") |
| 129 | + print(f" from_dict({{}}) -> {MultimodalPayload.from_dict({})}") |
| 130 | + |
| 131 | + print("\n=== MultimodalCompletionOutput ===") |
| 132 | + wrapper = MultimodalCompletionOutput( |
| 133 | + multimodal_output=p, |
| 134 | + index=0, |
| 135 | + text="hello", |
| 136 | + token_ids=[], |
| 137 | + cumulative_logprob=None, |
| 138 | + logprobs=None, |
| 139 | + ) |
| 140 | + print(f" text : {wrapper.text}") |
| 141 | + print(f" index : {wrapper.index}") |
| 142 | + print(f" multimodal_output: {wrapper.multimodal_output}") |
| 143 | + print(f" repr : {wrapper!r}") |
| 144 | + |
| 145 | + print("\n=== Unknown Modality ===") |
| 146 | + try: |
| 147 | + OutputModality.from_string("video") |
| 148 | + except ValueError as e: |
| 149 | + print(f' from_string("video") raised ValueError: {e}') |
| 150 | + |
| 151 | + captured = capsys.readouterr() |
| 152 | + assert "OutputModality Parsing" in captured.out |
| 153 | + assert "MultimodalPayload" in captured.out |
0 commit comments