[Perf] Stacked audio ops: batched matmul and embedding gather in MOSS-TTS talker (#4230)

Signed-off-by: leo.yang <leo.yang.engineer@gmail.com>
This commit is contained in:
yangyonggit authored and GitHub committed 2026-06-19 00:38:32 +00:00
1 parent 37a442447e
commit 63ba11b8b1
3 files changed
+534 -166

No files matched your search

+171 -156
View File
@@ -1,77 +1,97 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""E2E offline inference tests for MOSS-TTS two-stage pipeline.
"""E2E offline inference tests for MOSS-VoiceGenerator (MossTTSDelayModel, 1.7B).
Tests the MossTTSDelayModel path using MOSS-VoiceGenerator (1.7B) — the
smallest MossTTSDelayModel variant — so the test runs on a single A10G or L4
without requiring an 80 GB GPU.
Uses the standard omni_runner + pytestmark pattern (one module-scoped engine
per file, skill invariant I4). The realtime variant lives in the sibling file
test_moss_tts_realtime.py.
A second test class covers MOSS-TTS-Realtime (1.7B, MossTTSRealtime) to
exercise the local-transformer generation path.
MOSS-VoiceGenerator synthesizes speech from a text *instruction* that describes
the desired voice (e.g. "a warm female voice with an American accent"). It does
NOT accept a reference audio clip. The request format requires running
AutoProcessor.from_pretrained once per call to encode the (text, instruction)
pair into the (prompt_token_ids, codes.ref) grid that the MossTTSDelayModel
talker expects — same as examples/offline_inference/text_to_speech/moss_tts/
end2end.py:_build_unified_codes.
Both classes use a module-scoped engine so the model is loaded once per test
module execution. Do NOT add a second engine fixture in this file — that
triggers mid-module teardown races (see skill invariant I4).
No determinism test: VoiceGenerator produces variable-length output even with
a fixed seed; waveform length reproducibility is not guaranteed.
"""
from __future__ import annotations
import gc
import os
import urllib.request
import pytest
import torch
from vllm import SamplingParams
from tests.helpers.mark import hardware_test
from vllm_omni import Omni
from tests.helpers.runtime import OmniRunner
from tests.helpers.stage_config import get_deploy_config_path, modify_stage_config
# ---------------------------------------------------------------------------
# Shared constants
# Constants
# ---------------------------------------------------------------------------
# Selected by the nightly "TTS · Function Test" (-m "full_model and L4 and tts").
# Without the tts + run-level marks these tests are collected but never run.
# TODO: Fix this test
# pytestmark = [pytest.mark.full_model, pytest.mark.tts]
MODEL = "OpenMOSS-Team/MOSS-VoiceGenerator"
SAMPLE_RATE = 24_000
SAMPLE_RATE = 24_000 # All MOSS-TTS full variants output 24 kHz
REF_AUDIO_URL = "https://raw.githubusercontent.com/OpenMOSS/MOSS-TTS/main/assets/audio/reference_zh_1.wav"
_DEFAULT_SAMPLING = SamplingParams(
# Stage 0 = AR talker; max_tokens=512 keeps tests fast (~20 s of audio).
# Stage 1 = codec decoder; greedy, large context to hold all codec tokens.
_STAGE0_PARAMS = SamplingParams(
temperature=1.7,
top_p=0.8,
top_k=25,
max_tokens=512, # short for tests (~20 s at 24 kHz, ~12.5 tokens/s)
max_tokens=512,
seed=42,
detokenize=False,
)
_STAGE1_PARAMS = SamplingParams(
temperature=0.0,
top_p=1.0,
top_k=-1,
max_tokens=65536,
seed=42,
detokenize=False,
)
_SAMPLING = [_STAGE0_PARAMS, _STAGE1_PARAMS]
# ---------------------------------------------------------------------------
# Session fixtures
# Deploy config
# ---------------------------------------------------------------------------
@pytest.fixture(scope="session")
def ref_audio_path(tmp_path_factory) -> str:
"""Download the upstream reference clip once per session.
def _get_test_config() -> str:
"""Derive a CI-friendly config from moss_voice_generator.yaml.
Set ``MOSS_TTS_SKIP_ON_NET_FAIL=1`` to skip in air-gapped environments.
Reduces Stage 0 gpu_memory_utilization from 0.60 → 0.45 to leave headroom
for Stage 1 (0.12) on a shared L4/A10G. max_num_seqs=1 keeps per-test
peak memory predictable.
"""
cache_dir = tmp_path_factory.mktemp("moss_tts_ref")
target = cache_dir / "zh_1.wav"
try:
with urllib.request.urlopen(REF_AUDIO_URL, timeout=30) as resp:
target.write_bytes(resp.read())
except Exception as exc:
msg = f"Cannot fetch reference clip {REF_AUDIO_URL}: {exc}"
if os.environ.get("MOSS_TTS_SKIP_ON_NET_FAIL"):
pytest.skip(msg)
pytest.fail(msg)
if not target.exists() or target.stat().st_size == 0:
pytest.fail(f"Reference clip empty after download: {target}")
return str(target)
return modify_stage_config(
get_deploy_config_path("moss_voice_generator.yaml"),
updates={
"stages": {
0: {
"max_num_seqs": 1,
"gpu_memory_utilization": 0.45,
},
},
},
)
# ---------------------------------------------------------------------------
# pytestmark — one engine for the whole module
# ---------------------------------------------------------------------------
pytestmark = [
pytest.mark.full_model,
pytest.mark.tts,
pytest.mark.parametrize("omni_runner", [(MODEL, _get_test_config())], indirect=True),
]
# ---------------------------------------------------------------------------
@@ -79,147 +99,142 @@ def ref_audio_path(tmp_path_factory) -> str:
# ---------------------------------------------------------------------------
def _build_request(text: str, ref_audio_path: str, seed: int = 42) -> dict:
def _build_request(text: str, instruction: str) -> dict:
"""Build a VoiceGenerator request using AutoProcessor.
Calls proc.build_user_message + proc() to produce the unified-codes grid
(L, 1+n_vq) and splits it into prompt_token_ids + codes.ref, exactly as
end2end.py:_build_unified_codes does for the voice_generator variant.
The processor (including its CPU-resident audio_tokenizer) is freed before
returning so it does not compete with the running engine.
Set MOSS_TTS_SKIP_ON_NET_FAIL=1 to skip in air-gapped environments where
the HF snapshot is not cached.
"""
from transformers import AutoProcessor
try:
proc = AutoProcessor.from_pretrained(MODEL, trust_remote_code=True)
except Exception as exc:
msg = f"Cannot load AutoProcessor for {MODEL}: {exc}"
if os.environ.get("MOSS_TTS_SKIP_ON_NET_FAIL"):
pytest.skip(msg)
pytest.fail(msg)
user_msg = proc.build_user_message(text=text, instruction=instruction)
batch = proc(conversations=[[user_msg]], mode="generation")
unified = batch["input_ids"][0] # (L, 1+n_vq)
text_ids = unified[:, 0].tolist()
audio_codes = unified[:, 1:].contiguous().to(torch.int64) # (L, n_vq)
del proc
gc.collect()
return {
"prompt": "<|im_start|>assistant\n",
"additional_information": {
"task_type": ["voice_clone"],
"text": [text],
"mode": ["voice_clone"],
"prompt_audio_path": [ref_audio_path],
"seed": [seed],
},
"prompt_token_ids": text_ids,
"additional_information": {"codes": {"ref": audio_codes}},
}
def _collect_audio(omni: Omni, request: dict) -> tuple[torch.Tensor, int]:
"""Run one request; return (waveform_cpu, sample_rate)."""
for stage_outputs in omni.generate(request, _DEFAULT_SAMPLING):
for req_output in stage_outputs.request_output:
mm = req_output.outputs[0].multimodal_output
assert mm is not None, "Expected non-None multimodal_output"
# Never use ``a or b`` on tensor values — a multi-element tensor
# raises on truthiness. Check ``is None`` explicitly and handle the
# pre-consolidation list form.
audio = mm.get("audio")
if audio is None:
audio = mm.get("model_outputs")
sr = mm.get("sr")
assert audio is not None, "Expected 'audio' or 'model_outputs' in multimodal_output"
if isinstance(audio, list):
audio = torch.cat(
[t.reshape(-1) for t in audio if isinstance(t, torch.Tensor) and t.numel() > 0],
dim=0,
)
assert isinstance(audio, torch.Tensor), f"Expected Tensor, got {type(audio)}"
return audio.reshape(-1).cpu(), int(sr.item()) if sr is not None else SAMPLE_RATE
raise AssertionError("No stage outputs received")
def _collect_audio(omni_runner: OmniRunner, request: dict) -> tuple[torch.Tensor, int]:
"""Run one request and return (waveform_cpu, sample_rate).
Iterates all OmniRequestOutputs from generate() via the top-level
multimodal_output property (which aggregates from completion outputs).
AR-stage outputs have empty multimodal_output and are silently skipped.
With async_chunk=True and FINAL_ONLY, the codec stage consolidates its
chunks into a single tensor before yielding — but we handle list fallback
for robustness.
"""
chunks: list[torch.Tensor] = []
sr_final = SAMPLE_RATE
for out in omni_runner.omni.generate(request, _SAMPLING):
mm = out.multimodal_output
if not mm:
continue
audio = mm.get("audio")
if audio is None:
audio = mm.get("model_outputs")
if audio is None:
continue
sr = mm.get("sr")
if sr is not None:
sr_final = int(sr.item() if hasattr(sr, "item") else sr)
if isinstance(audio, list):
audio = torch.cat(
[t.reshape(-1) for t in audio if isinstance(t, torch.Tensor) and t.numel() > 0],
dim=0,
)
if isinstance(audio, torch.Tensor) and audio.numel() > 0:
chunks.append(audio.reshape(-1).cpu())
if not chunks:
raise AssertionError("No audio output received from generate()")
return torch.cat(chunks, dim=0), sr_final
# ---------------------------------------------------------------------------
# MossTTSDelayModel — MOSS-VoiceGenerator 1.7B
# (tests the delay-pattern generation path on a small GPU)
# Tests
# ---------------------------------------------------------------------------
_DELAY_MODEL = "OpenMOSS-Team/MOSS-VoiceGenerator"
@pytest.fixture(scope="module")
def delay_engine():
return Omni(model=_DELAY_MODEL, stage_init_timeout=300)
@hardware_test(res={"cuda": "L4"})
def test_moss_tts_delay_english(delay_engine, ref_audio_path):
"""MossTTSDelayModel: English voice_clone produces non-empty 24 kHz audio."""
req = _build_request("Hello, this is a MOSS-TTS voice cloning test.", ref_audio_path)
audio, sr = _collect_audio(delay_engine, req)
@pytest.mark.advanced_model
@hardware_test(res={"cuda": "L4"}, num_cards=1)
def test_moss_tts_english(omni_runner: OmniRunner) -> None:
"""VoiceGenerator: English instruction produces non-empty 24 kHz audio."""
req = _build_request(
"Hello, this is a MOSS voice design test.",
"a warm female voice with an American accent",
)
audio, sr = _collect_audio(omni_runner, req)
assert sr == SAMPLE_RATE, f"Expected {SAMPLE_RATE} Hz, got {sr}"
assert audio.numel() > 0, "Audio tensor is empty"
assert not torch.all(audio == 0), "Audio is silence"
@hardware_test(res={"cuda": "L4"})
def test_moss_tts_delay_chinese(delay_engine, ref_audio_path):
"""MossTTSDelayModel: Chinese input produces non-empty audio."""
req = _build_request("你好,这是语音合成测试。", ref_audio_path)
audio, sr = _collect_audio(delay_engine, req)
@pytest.mark.advanced_model
@hardware_test(res={"cuda": "L4"}, num_cards=1)
def test_moss_tts_chinese(omni_runner: OmniRunner) -> None:
"""VoiceGenerator: Chinese input produces non-empty audio."""
req = _build_request(
"你好,这是语音合成测试。",
"一个清晰温暖的女声",
)
audio, sr = _collect_audio(omni_runner, req)
assert sr == SAMPLE_RATE
assert audio.numel() > 0
assert not torch.all(audio == 0)
@hardware_test(res={"cuda": "L4"})
def test_moss_tts_delay_deterministic(delay_engine, ref_audio_path):
"""MossTTSDelayModel: same seed yields identical waveforms."""
req = _build_request("Reproducibility check.", ref_audio_path, seed=99)
audio1, _ = _collect_audio(delay_engine, req)
audio2, _ = _collect_audio(delay_engine, req)
assert audio1.shape == audio2.shape, "Shapes differ across identical seeds"
assert torch.allclose(audio1, audio2, atol=1e-4), "Waveforms differ across identical seeds"
@hardware_test(res={"cuda": "L4"})
def test_moss_tts_delay_batch(delay_engine, ref_audio_path):
"""MossTTSDelayModel: batch of two requests each returns non-empty audio."""
@pytest.mark.advanced_model
@hardware_test(res={"cuda": "L4"}, num_cards=1)
def test_moss_tts_batch(omni_runner: OmniRunner) -> None:
"""VoiceGenerator: batch of two requests each returns non-empty audio."""
requests = [
_build_request("First sentence.", ref_audio_path),
_build_request("Second sentence.", ref_audio_path),
_build_request("First sentence.", "a warm female voice"),
_build_request("Second sentence.", "a young male voice"),
]
results: list[torch.Tensor] = []
for stage_outputs in delay_engine.generate(requests, [_DEFAULT_SAMPLING] * 2):
for req_output in stage_outputs.request_output:
mm = req_output.outputs[0].multimodal_output
assert mm is not None
audio = mm.get("audio")
if audio is None:
audio = mm.get("model_outputs")
assert audio is not None
if isinstance(audio, list):
audio = torch.cat(
[t.reshape(-1) for t in audio if isinstance(t, torch.Tensor) and t.numel() > 0],
dim=0,
)
for out in omni_runner.omni.generate(requests, _SAMPLING):
mm = out.multimodal_output
if not mm:
continue
audio = mm.get("audio")
if audio is None:
audio = mm.get("model_outputs")
if audio is None:
continue
if isinstance(audio, list):
audio = torch.cat(
[t.reshape(-1) for t in audio if isinstance(t, torch.Tensor) and t.numel() > 0],
dim=0,
)
if isinstance(audio, torch.Tensor) and audio.numel() > 0:
results.append(audio.reshape(-1).cpu())
assert len(results) == 2, f"Expected 2 outputs, got {len(results)}"
# With async_chunk streaming + FINAL_ONLY, each request produces one
# consolidated output. The >= 2 guard handles backward-compatibility if
# consolidation behavior changes.
assert len(results) >= 2, f"Expected at least 2 audio outputs, got {len(results)}"
for i, audio in enumerate(results):
assert audio.numel() > 0, f"Audio[{i}] is empty"
# ---------------------------------------------------------------------------
# MossTTSRealtime — MOSS-TTS-Realtime 1.7B
# (tests the local-transformer generation path)
# ---------------------------------------------------------------------------
_REALTIME_MODEL = "OpenMOSS-Team/MOSS-TTS-Realtime"
@pytest.fixture(scope="module")
def realtime_engine():
return Omni(model=_REALTIME_MODEL, stage_init_timeout=300)
@hardware_test(res={"cuda": "L4"})
def test_moss_tts_realtime_english(realtime_engine, ref_audio_path):
"""MossTTSRealtime: English voice_clone produces non-empty 24 kHz audio."""
req = _build_request("This is a real-time TTS streaming test.", ref_audio_path)
audio, sr = _collect_audio(realtime_engine, req)
assert sr == SAMPLE_RATE, f"Expected {SAMPLE_RATE} Hz, got {sr}"
assert audio.numel() > 0, "Audio tensor is empty"
assert not torch.all(audio == 0), "Audio is silence"
@hardware_test(res={"cuda": "L4"})
def test_moss_tts_realtime_deterministic(realtime_engine, ref_audio_path):
"""MossTTSRealtime: same seed yields identical waveforms."""
req = _build_request("Determinism check for realtime model.", ref_audio_path, seed=7)
audio1, _ = _collect_audio(realtime_engine, req)
audio2, _ = _collect_audio(realtime_engine, req)
assert audio1.shape == audio2.shape
assert torch.allclose(audio1, audio2, atol=1e-4)
assert audio.numel() > 0, f"Audio chunk[{i}] is empty"