[Bugfix][Multimodal] Normalize single-channel audio to 1D (#56691)
Signed-off-by: Ylean <61880605+Woolgathererer@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
This commit is contained in:
co-authored by
Codex
Cyrus Leung
parent
18f8f96025
commit
0872ddf4b9
@@ -216,17 +216,19 @@ class TestNormalizeAudio:
|
|||||||
assert result.ndim == 1
|
assert result.ndim == 1
|
||||||
torch.testing.assert_close(result, torch.tensor([0.0, 1.0]))
|
torch.testing.assert_close(result, torch.tensor([0.0, 1.0]))
|
||||||
|
|
||||||
def test_mono_passthrough_for_1d_numpy(self):
|
@pytest.mark.parametrize("shape", [(3,), (1, 3), (3, 1), (1, 1)])
|
||||||
"""1D numpy array should pass through unchanged with mono spec."""
|
def test_mono_passthrough_for_numpy(self, shape):
|
||||||
mono = np.array([1.0, 2.0, 3.0], dtype=np.float32)
|
"""Mono output is 1D, including audio with an explicit channel axis."""
|
||||||
result = normalize_audio(mono, MONO_AUDIO_SPEC)
|
mono = np.arange(max(shape), dtype=np.float32)
|
||||||
|
result = normalize_audio(mono.reshape(shape), MONO_AUDIO_SPEC)
|
||||||
assert result.ndim == 1
|
assert result.ndim == 1
|
||||||
np.testing.assert_array_equal(result, mono)
|
np.testing.assert_array_equal(result, mono)
|
||||||
|
|
||||||
def test_mono_passthrough_for_1d_torch(self):
|
@pytest.mark.parametrize("shape", [(3,), (1, 3), (3, 1), (1, 1)])
|
||||||
"""1D torch tensor should pass through unchanged with mono spec."""
|
def test_mono_passthrough_for_torch(self, shape):
|
||||||
mono = torch.tensor([1.0, 2.0, 3.0])
|
"""Mono output is 1D, including audio with an explicit channel axis."""
|
||||||
result = normalize_audio(mono, MONO_AUDIO_SPEC)
|
mono = torch.arange(max(shape), dtype=torch.float32)
|
||||||
|
result = normalize_audio(mono.reshape(shape), MONO_AUDIO_SPEC)
|
||||||
assert result.ndim == 1
|
assert result.ndim == 1
|
||||||
torch.testing.assert_close(result, mono)
|
torch.testing.assert_close(result, mono)
|
||||||
|
|
||||||
@@ -410,8 +412,9 @@ class TestMultiModalDataParserChannelNormalization:
|
|||||||
# When target_channels=None, stereo audio should be preserved
|
# When target_channels=None, stereo audio should be preserved
|
||||||
assert audio_item.ndim == 2, f"Expected 2D stereo audio, got {audio_item.ndim}D"
|
assert audio_item.ndim == 2, f"Expected 2D stereo audio, got {audio_item.ndim}D"
|
||||||
|
|
||||||
def test_parser_mono_passthrough_when_target_channels_1(self):
|
@pytest.mark.parametrize("shape", [(16000,), (1, 16000), (16000, 1)])
|
||||||
"""Parser should pass through mono audio unchanged when target_channels=1."""
|
def test_parser_mono_passthrough_when_target_channels_1(self, shape):
|
||||||
|
"""Mono layouts must preserve samples and report the waveform length."""
|
||||||
from vllm.multimodal.parse import MultiModalDataParser
|
from vllm.multimodal.parse import MultiModalDataParser
|
||||||
|
|
||||||
# Create parser with mono normalization enabled
|
# Create parser with mono normalization enabled
|
||||||
@@ -420,17 +423,20 @@ class TestMultiModalDataParserChannelNormalization:
|
|||||||
target_channels=1,
|
target_channels=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create mono audio (already 1D)
|
mono_audio = np.linspace(-1.0, 1.0, 16000, dtype=np.float32)
|
||||||
mono_audio = np.random.randn(16000).astype(np.float32)
|
|
||||||
|
|
||||||
# Parse audio data
|
# Parse audio data
|
||||||
result = parser._parse_audio_data((mono_audio, 16000))
|
result = parser.parse_mm_data({"audio": (mono_audio.reshape(shape), 16000)})[
|
||||||
|
"audio"
|
||||||
|
]
|
||||||
|
|
||||||
# Check that result is still mono (1D)
|
# Check that result is still mono (1D)
|
||||||
assert result is not None
|
assert result is not None
|
||||||
audio_item = result.get(0)
|
audio_item = result.get(0)
|
||||||
assert audio_item.ndim == 1
|
assert audio_item.ndim == 1
|
||||||
assert audio_item.shape == (16000,)
|
assert audio_item.shape == (16000,)
|
||||||
|
assert len(audio_item) == 16000
|
||||||
|
np.testing.assert_array_equal(audio_item, mono_audio)
|
||||||
|
|
||||||
def test_parser_with_target_channels_2(self):
|
def test_parser_with_target_channels_2(self):
|
||||||
"""Parser should reduce 6-channel to 2-channel when target_channels=2."""
|
"""Parser should reduce 6-channel to 2-channel when target_channels=2."""
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ def normalize_audio(
|
|||||||
|
|
||||||
# No reduction needed if already at target
|
# No reduction needed if already at target
|
||||||
if num_channels == spec.target_channels:
|
if num_channels == spec.target_channels:
|
||||||
return audio
|
return audio[0] if num_channels == 1 else audio
|
||||||
|
|
||||||
# Cannot expand channels
|
# Cannot expand channels
|
||||||
if num_channels < spec.target_channels:
|
if num_channels < spec.target_channels:
|
||||||
|
|||||||
Reference in New Issue
Block a user