| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | import torch |
| 6 | |
| 7 | from vllm.model_executor.models.gemma3n_audio_utils import ( |
| 8 | adjust_audio_features_to_expected_length, |
| 9 | ) |
| 10 | from vllm.multimodal import MULTIMODAL_REGISTRY |
| 11 | |
| 12 | from ....conftest import ImageTestAssets |
| 13 | from ...utils import build_model_context |
| 14 | |
| 15 | # Gemma3 (image) model |
| 16 | GEMMA3_MODEL_ID = "google/gemma-3-4b-it" |
| 17 | |
| 18 | # Gemma3n (multimodal with audio) model |
| 19 | GEMMA3N_MODEL_ID = "google/gemma-3n-E2B-it" |
| 20 | |
| 21 | # Expected audio tokens for Gemma3n (audio_soft_tokens_per_image) |
| 22 | GEMMA3N_EXPECTED_AUDIO_TOKENS = 188 |
| 23 | |
| 24 | |
| 25 | class TestGemma3nAudioTensorLogic: |
| 26 | """CPU-based tests for Gemma3n audio feature tensor manipulation. |
| 27 | |
| 28 | These tests validate the padding/truncation logic in |
| 29 | adjust_audio_features_to_expected_length() which fixes the |
| 30 | integer overflow in _process_audio_input when audio_seq_len > 188. |
| 31 | """ |
| 32 | |
| 33 | def test_padding_when_audio_short(self): |
| 34 | """Test that short audio is padded to expected length.""" |
| 35 | batch_size, seq_len, embed_dim = 1, 100, 256 |
| 36 | expected_tokens = GEMMA3N_EXPECTED_AUDIO_TOKENS |
| 37 | |
| 38 | audio_features = torch.randn(batch_size, seq_len, embed_dim) |
| 39 | padding_embs = torch.zeros(1, 1, embed_dim) |
| 40 | |
| 41 | result, tokens_truncated = adjust_audio_features_to_expected_length( |
| 42 | audio_features, expected_tokens, padding_embs |
| 43 | ) |
| 44 | |
| 45 | assert result.shape == (batch_size, expected_tokens, embed_dim) |
| 46 | assert tokens_truncated == 0 |
| 47 | # First 100 tokens should be original, rest should be padding (zeros) |
| 48 | assert torch.allclose(result[:, :seq_len, :], audio_features) |
| 49 | assert torch.allclose( |
| 50 | result[:, seq_len:, :], |
| 51 | torch.zeros(batch_size, expected_tokens - seq_len, embed_dim), |
| 52 | ) |
| 53 | |
| 54 | def test_truncation_when_audio_long(self): |
| 55 | """Test that long audio is truncated to expected length. |
| 56 | |
| 57 | This is the key test for the overflow fix. Previously, when |
| 58 | audio_seq_len > expected_tokens, the code would compute a negative |
| 59 | padding value causing: RuntimeError: numel: integer multiplication overflow |
| 60 | """ |
| 61 | batch_size, seq_len, embed_dim = 1, 192, 256 # 192 > 188 |
| 62 | expected_tokens = GEMMA3N_EXPECTED_AUDIO_TOKENS |
| 63 | |
| 64 | audio_features = torch.randn(batch_size, seq_len, embed_dim) |
| 65 | padding_embs = torch.zeros(1, 1, embed_dim) |
| 66 | |
| 67 | result, tokens_truncated = adjust_audio_features_to_expected_length( |
| 68 | audio_features, expected_tokens, padding_embs |
| 69 | ) |
| 70 | |
| 71 | assert result.shape == (batch_size, expected_tokens, embed_dim) |
| 72 | assert tokens_truncated == seq_len - expected_tokens # 192 - 188 = 4 |
| 73 | # Result should be first 188 tokens of original |
| 74 | assert torch.allclose(result, audio_features[:, :expected_tokens, :]) |
| 75 | |
| 76 | def test_no_change_when_exact_length(self): |
| 77 | """Test that exact-length audio passes through unchanged.""" |
| 78 | batch_size, embed_dim = 1, 256 |
| 79 | expected_tokens = GEMMA3N_EXPECTED_AUDIO_TOKENS |
| 80 | |
| 81 | audio_features = torch.randn(batch_size, expected_tokens, embed_dim) |
| 82 | padding_embs = torch.zeros(1, 1, embed_dim) |
| 83 | |
| 84 | result, tokens_truncated = adjust_audio_features_to_expected_length( |
| 85 | audio_features, expected_tokens, padding_embs |
| 86 | ) |
| 87 | |
| 88 | assert result.shape == audio_features.shape |
| 89 | assert tokens_truncated == 0 |
| 90 | assert torch.allclose(result, audio_features) |
| 91 | |
| 92 | def test_original_bug_would_fail(self): |
| 93 | """Verify the original buggy implementation would cause overflow. |
| 94 | |
| 95 | The original code always tried to pad, which fails when |
| 96 | audio_seq_len > expected_tokens because expand() gets negative size. |
| 97 | """ |
| 98 | batch_size, seq_len, embed_dim = 1, 192, 256 |
| 99 | expected_tokens = GEMMA3N_EXPECTED_AUDIO_TOKENS |
| 100 | |
| 101 | padding_embs = torch.zeros(1, 1, embed_dim) |
| 102 | |
| 103 | # Original buggy logic (always pads, never truncates) |
| 104 | extra_padding_tokens = expected_tokens - seq_len # = -4 (negative!) |
| 105 | |
| 106 | with pytest.raises(RuntimeError): |
| 107 | # This should fail with negative size error |
| 108 | padding_embs.expand(batch_size, extra_padding_tokens, embed_dim) |
| 109 | |
| 110 | @pytest.mark.parametrize( |
| 111 | "seq_len", |
| 112 | [50, 100, 150, 187, 188, 189, 192, 200, 300], |
| 113 | ) |
| 114 | def test_various_audio_lengths(self, seq_len: int): |
| 115 | """Test padding/truncation with various audio lengths.""" |
| 116 | batch_size, embed_dim = 1, 256 |
| 117 | expected_tokens = GEMMA3N_EXPECTED_AUDIO_TOKENS |
| 118 | |
| 119 | audio_features = torch.randn(batch_size, seq_len, embed_dim) |
| 120 | padding_embs = torch.zeros(1, 1, embed_dim) |
| 121 | |
| 122 | # Should not raise any errors |
| 123 | result, tokens_truncated = adjust_audio_features_to_expected_length( |
| 124 | audio_features, expected_tokens, padding_embs |
| 125 | ) |
| 126 | |
| 127 | # Output should always be expected_tokens length |
| 128 | assert result.shape == (batch_size, expected_tokens, embed_dim) |
| 129 | |
| 130 | # Verify truncation count is correct |
| 131 | if seq_len > expected_tokens: |
| 132 | assert tokens_truncated == seq_len - expected_tokens |
| 133 | else: |
| 134 | assert tokens_truncated == 0 |
| 135 | |
| 136 | def test_batch_processing(self): |
| 137 | """Test that batch processing works correctly.""" |
| 138 | batch_size, seq_len, embed_dim = 4, 192, 256 |
| 139 | expected_tokens = GEMMA3N_EXPECTED_AUDIO_TOKENS |
| 140 | |
| 141 | audio_features = torch.randn(batch_size, seq_len, embed_dim) |
| 142 | padding_embs = torch.zeros(1, 1, embed_dim) |
| 143 | |
| 144 | result, tokens_truncated = adjust_audio_features_to_expected_length( |
| 145 | audio_features, expected_tokens, padding_embs |
| 146 | ) |
| 147 | |
| 148 | assert result.shape == (batch_size, expected_tokens, embed_dim) |
| 149 | assert tokens_truncated == seq_len - expected_tokens |
| 150 | |
| 151 | |
| 152 | @pytest.mark.parametrize("model_id", [GEMMA3_MODEL_ID]) |
| 153 | @pytest.mark.parametrize("mm_processor_kwargs", [{}]) |
| 154 | def test_get_image_size_with_most_features( |
| 155 | image_assets: ImageTestAssets, |
| 156 | model_id: str, |
| 157 | mm_processor_kwargs: dict[str, object], |
| 158 | ): |
| 159 | ctx = build_model_context( |
| 160 | model_id, |
| 161 | mm_processor_kwargs={"do_pan_and_scan": True}, |
| 162 | limit_mm_per_prompt={"image": 1}, |
| 163 | ) |
| 164 | processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config) |
| 165 | |
| 166 | hf_processor = processor.info.get_hf_processor(**mm_processor_kwargs) |
| 167 | |
| 168 | max_image_size = processor.info.get_image_size_with_most_features() |
| 169 | max_tokens = processor.info.get_num_image_tokens( |
| 170 | image_width=max_image_size.width, |
| 171 | image_height=max_image_size.height, |
| 172 | processor=hf_processor, |
| 173 | mm_kwargs=mm_processor_kwargs, |
| 174 | ) |
| 175 | |
| 176 | prompt = "<start_of_image>" |
| 177 | image_seq_length = hf_processor.image_seq_length |
| 178 | |
| 179 | for asset in image_assets: |
| 180 | mm_data = {"image": [asset.pil_image]} |
| 181 | processed_inputs = processor( |
| 182 | prompt, |
| 183 | mm_items=processor.info.parse_mm_data(mm_data), |
| 184 | hf_processor_mm_kwargs=mm_processor_kwargs, |
| 185 | ) |
| 186 | mm_kwargs_data = processed_inputs["mm_kwargs"].get_data() |
| 187 | num_patches_tensor = mm_kwargs_data["num_patches"] |
| 188 | tokens = int(num_patches_tensor.item()) * image_seq_length |
| 189 | assert tokens <= max_tokens |