[BugFix] moss_tts_nano: eager-init lm + audio_tokenizer in __init__ so load_format: dummy works (#3230)

Signed-off-by: leohuang257 <masugchds@gmail.com>
This commit is contained in:
Guanzhe (Leo) Huang authored and GitHub committed 2026-06-10 20:50:53 +08:00
1 parent aa92a3a35d
commit c2b4564a67
1 file changed
+28 -40
@@ -13,8 +13,10 @@ Streaming is supported via the VoxCPM-style generator pattern:
- compute_logits() emits EOS only when the last chunk has been yielded, - compute_logits() emits EOS only when the last chunk has been yielded,
telling the AR scheduler to finish the request. telling the AR scheduler to finish the request.
Weight loading deliberately happens inside load_weights() -- NOT __init__ -- The AR LM and audio tokenizer are constructed eagerly in __init__ so that
so that vLLM initialises distributed state before any CUDA allocations occur. ``load_format: dummy`` works (DummyModelLoader skips ``load_weights``) and
to avoid OOM from vLLM's post-init KV-cache profiling. Mirrors qwen3_tts
(PR #3117).
""" """
from __future__ import annotations from __future__ import annotations
@@ -140,34 +142,11 @@ class MossTTSNanoForGeneration(nn.Module):
self.config = vllm_config.model_config.hf_config self.config = vllm_config.model_config.hf_config
self.model_path: str = vllm_config.model_config.model self.model_path: str = vllm_config.model_config.model
self._lm: nn.Module | None = None # Eager construction (not in load_weights) -- see module docstring.
self._audio_tokenizer: nn.Module | None = None
self._device: torch.device | None = None
self._lock = threading.Lock()
# Per-request streaming generators (VoxCPM pattern).
# Single-threaded AR worker: mutations happen only inside forward(),
# so we rely on the worker's serial execution instead of self._lock.
self._stream_gens: dict[str, Any] = {}
# Per-row EOS mask aligned with the most recent forward() batch.
# compute_logits() emits EOS for rows flagged True, keeps others alive.
self._ar_last_chunk_flags: list[bool] = []
# ------------------------------------------------------------------
# Weight loading
# ------------------------------------------------------------------
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
with self._lock:
if self._lm is not None:
return None
_patch_torchaudio_load() _patch_torchaudio_load()
try:
device = next(self.parameters()).device
except StopIteration:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self._device = device self._device: torch.device = device
if device.type == "cuda" and torch.cuda.is_bf16_supported(): if device.type == "cuda" and torch.cuda.is_bf16_supported():
tts_dtype = torch.bfloat16 tts_dtype = torch.bfloat16
@@ -207,7 +186,7 @@ class MossTTSNanoForGeneration(nn.Module):
if n_fixed > 0: if n_fixed > 0:
logger.info("MOSS-TTS-Nano: re-initialised %d rotary_emb.inv_freq buffers", n_fixed) logger.info("MOSS-TTS-Nano: re-initialised %d rotary_emb.inv_freq buffers", n_fixed)
self._lm = lm self._lm: nn.Module = lm
logger.info("MOSS-TTS-Nano LM loaded on %s", device) logger.info("MOSS-TTS-Nano LM loaded on %s", device)
codec_path: str = getattr( codec_path: str = getattr(
@@ -225,15 +204,31 @@ class MossTTSNanoForGeneration(nn.Module):
) )
audio_tokenizer.to(device=device) audio_tokenizer.to(device=device)
audio_tokenizer.eval() audio_tokenizer.eval()
self._audio_tokenizer = audio_tokenizer self._audio_tokenizer: nn.Module = audio_tokenizer
logger.info("MOSS-Audio-Tokenizer-Nano loaded on %s", device) logger.info("MOSS-Audio-Tokenizer-Nano loaded on %s", device)
# MOSS-TTS-Nano loads weights inline via from_pretrained() above; # Kept for API compat; vLLM worker init is single-threaded.
# vLLM's weight-loading protocol still requires us to exhaust the self._lock = threading.Lock()
# iterator so the underlying stream is closed cleanly.
# Per-request streaming generators (VoxCPM pattern).
# Single-threaded AR worker: mutations happen only inside forward(),
# so we rely on the worker's serial execution instead of self._lock.
self._stream_gens: dict[str, Any] = {}
# Per-row EOS mask aligned with the most recent forward() batch.
# compute_logits() emits EOS for rows flagged True, keeps others alive.
self._ar_last_chunk_flags: list[bool] = []
# ------------------------------------------------------------------
# Weight loading
# ------------------------------------------------------------------
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
# Weights are populated in __init__ via from_pretrained; drain the
# iterator and report all params as loaded. Not called under
# load_format=dummy (DummyModelLoader randomises params in place).
for _ in weights: for _ in weights:
pass pass
return None return {name for name, _ in self.named_parameters()}
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# Dummy run support # Dummy run support
@@ -400,13 +395,6 @@ class MossTTSNanoForGeneration(nn.Module):
runtime_additional_information: list[dict[str, Any]] | None = None, runtime_additional_information: list[dict[str, Any]] | None = None,
**kwargs: Any, **kwargs: Any,
) -> OmniOutput: ) -> OmniOutput:
if self._lm is None or self._audio_tokenizer is None:
# load_weights() loads the real LM from HuggingFace. When
# load_format=dummy the vllm DummyModelLoader skips calling
# model.load_weights(), so we do it lazily on first forward.
# Must happen before _make_dummy_hidden so self._device is set.
self.load_weights([])
sr = getattr(self.config, "audio_tokenizer_sample_rate", 48000) sr = getattr(self.config, "audio_tokenizer_sample_rate", 48000)
sr_tensor = torch.tensor(sr, dtype=torch.int32) sr_tensor = torch.tensor(sr, dtype=torch.int32)
empty = torch.zeros((0,), dtype=torch.float32) empty = torch.zeros((0,), dtype=torch.float32)