[Model] Add MiniCPM-o 4.5 turn-mode MRv2 and batched audio inference (#8222)

Signed-off-by: Sy03 <1370724210@qq.com>
This commit is contained in:
Sy03 authored and GitHub committed 2026-09-29 11:41:39 -07:00
1 parent 43ef1e61fb
commit 5af3a16526
28 files changed
+1577 -51

No files matched your search

@@ -143,6 +143,53 @@ class ResBlock(torch.nn.Module):
return folded
def _istft_envelope(window: torch.Tensor, n_fft: int, hop_length: int, n_frames: int) -> torch.Tensor:
"""Trimmed overlap-add window envelope that ``torch.istft`` divides by.
Built with the same ops as ``at::native::istft`` (squared window folded by
``unfold_backward``, centre padding trimmed). ``torch.istft`` also checks
here that no sample has a vanishing envelope, which reads the result back
to the host; the envelope only depends on the window and the frame count,
so the check runs once per shape instead of once per call.
"""
expected_len = n_fft + hop_length * (n_frames - 1)
squared = window.pow(2).expand(1, n_frames, n_fft)
envelope = torch.ops.aten.unfold_backward(squared, [1, expected_len], 1, n_fft, hop_length)
envelope = envelope[:, n_fft // 2 : expected_len - n_fft // 2]
if bool(envelope.abs().min().lt(1e-11)):
raise RuntimeError("window overlap add min: True")
return envelope
def _istft_without_host_sync(
spec: torch.Tensor,
n_fft: int,
hop_length: int,
window: torch.Tensor,
envelopes: dict[tuple, torch.Tensor],
) -> torch.Tensor:
"""``torch.istft(spec, n_fft, hop_length, n_fft, window=window)`` without a host sync.
Mirrors ``at::native::istft`` for this call (``center=True``, one-sided,
unnormalized, ``win_length == n_fft``, no ``length``): the same inverse
real FFT, window product, ``unfold_backward`` overlap-add, trim and
division, so the waveform is bitwise identical. The envelope, and with it
the only device-to-host read, comes from ``envelopes`` after the first
call for a frame count.
"""
batch, _, n_frames = spec.shape
key = (n_frames, n_fft, hop_length, window.device, window.dtype, window.data_ptr())
envelope = envelopes.get(key)
if envelope is None:
envelope = _istft_envelope(window, n_fft, hop_length, n_frames)
envelopes[key] = envelope
frames = torch.fft.irfft(spec.transpose(1, 2), n=n_fft, dim=-1)
frames = frames * window.view(1, 1, n_fft)
expected_len = n_fft + hop_length * (n_frames - 1)
signal = torch.ops.aten.unfold_backward(frames, [batch, expected_len], 1, n_fft, hop_length)
return signal[:, n_fft // 2 : expected_len - n_fft // 2] / envelope
def _carry_phase_at_boundary(
cum_total: torch.Tensor,
phase_acc: torch.Tensor | None,
@@ -620,6 +667,10 @@ class HiFTGenerator(nn.Module):
)
self.f0_predictor = f0_predictor
def enable_cached_istft(self) -> None:
"""Opt into CUDA ISTFT with a cached overlap envelope."""
self._use_cached_istft = True
def remove_weight_norm(self) -> int:
"""Fold the generator's frozen weight norms into plain weights.
@@ -675,12 +726,28 @@ class HiFTGenerator(nn.Module):
magnitude = torch.clip(magnitude, max=1e2)
real = magnitude * torch.cos(phase)
imag = magnitude * torch.sin(phase)
return torch.istft(
torch.complex(real, imag),
spec = torch.complex(real, imag)
window = self._get_stft_window(magnitude)
if (
not getattr(self, "_use_cached_istft", False)
or spec.dim() != 3
or window.dim() != 1
or window.shape[0] != self.istft_params["n_fft"]
):
return torch.istft(
spec,
self.istft_params["n_fft"],
self.istft_params["hop_len"],
self.istft_params["n_fft"],
window=window,
)
envelopes = self.__dict__.setdefault("_istft_envelopes", {})
return _istft_without_host_sync(
spec,
self.istft_params["n_fft"],
self.istft_params["hop_len"],
self.istft_params["n_fft"],
window=self._get_stft_window(magnitude),
window,
envelopes,
)
def _istft_on_cpu(self, magnitude, phase):
@@ -268,6 +268,9 @@ class BatchedToken2Wav(nn.Module):
self._trt_stepper = trt_stepper
self.flow = token2wav.flow
self.hift = token2wav.hift
enable_cached_istft = getattr(self.hift, "enable_cached_istft", None)
if callable(enable_cached_istft):
enable_cached_istft()
encoder = getattr(self.flow, "encoder", None)
if encoder is not None:
_undecorate_dynamo(encoder, "forward_chunk")
@@ -22,6 +22,7 @@ from vllm.config import VllmConfig
from vllm.logger import init_logger
from vllm_omni.model_executor.models.output_templates import OmniOutput
from vllm_omni.utils.device_copy import to_device_nonblocking
from .batched_token2wav import (
BatchedToken2Wav,
@@ -146,10 +147,12 @@ def _normalize_reference(
def _codec_tensor(value: Any, fallback: torch.Tensor) -> torch.Tensor:
# Codec ids arrive from the connector as host data. A pageable host copy
# would block the host once per request per step; stage them pinned.
if isinstance(value, torch.Tensor):
return value.reshape(-1).to(device=fallback.device, dtype=torch.long)
return to_device_nonblocking(value.reshape(-1).to(dtype=torch.long), fallback.device)
if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
return torch.as_tensor(value, device=fallback.device, dtype=torch.long).reshape(-1)
return to_device_nonblocking(torch.as_tensor(value, dtype=torch.long).reshape(-1), fallback.device)
return fallback.reshape(-1).to(dtype=torch.long)
@@ -730,6 +733,33 @@ class MiniCPMO45Code2Wav(nn.Module):
inputs_embeds: torch.Tensor | None = None,
runtime_additional_information: list[dict[str, Any]] | None = None,
**kwargs: Any,
) -> OmniOutput:
# This stage owns the vocoder process. Restore its previous matmul
# policy after eager execution/capture; cuDNN's policy is independent.
previous_tf32 = torch.backends.cuda.matmul.allow_tf32
try:
if self._extra_config().get("token2wav_allow_tf32", False):
torch.backends.cuda.matmul.allow_tf32 = True
return self._forward_impl(
input_ids,
positions,
intermediate_tensors,
inputs_embeds,
runtime_additional_information,
**kwargs,
)
finally:
torch.backends.cuda.matmul.allow_tf32 = previous_tf32
@torch.inference_mode()
def _forward_impl(
self,
input_ids: torch.Tensor | None = None,
positions: torch.Tensor | None = None,
intermediate_tensors: Any = None,
inputs_embeds: torch.Tensor | None = None,
runtime_additional_information: list[dict[str, Any]] | None = None,
**kwargs: Any,
) -> OmniOutput:
del positions, intermediate_tensors, inputs_embeds
ids = input_ids if isinstance(input_ids, torch.Tensor) else torch.empty(0, dtype=torch.long)
@@ -94,6 +94,19 @@ class MiniCPMO45OmniForConditionalGeneration(nn.Module, SupportsMultiModal, Supp
patch_minicpmo_remote_config(config)
self.model_stage = vllm_config.model_config.model_stage
self._use_v2_model_runner = bool(getattr(vllm_config.model_config, "use_v2_model_runner", False))
if (
self.model_stage == "llm"
and self._use_v2_model_runner
and getattr(vllm_config.model_config, "session_mode", "turn") != "turn"
):
raise NotImplementedError("MiniCPM-o duplex Thinker requires model_runner: v1")
if (
self.model_stage == "llm"
and self._use_v2_model_runner
and getattr(vllm_config.model_config, "async_chunk", False)
):
raise ValueError("MiniCPM-o MRv2 Thinker requires async_chunk: false for its full llm2tts payload")
# The Thinker's row ledger needs real token identities even when
# embeddings are supplied, including during CUDA graph capture/replay.
self.requires_raw_input_tokens = self.model_stage == "llm"
@@ -155,6 +168,22 @@ class MiniCPMO45OmniForConditionalGeneration(nn.Module, SupportsMultiModal, Supp
if hasattr(self.talker, "init_multi_modal"):
self.talker.init_multi_modal(config)
self.model = self.talker
# The runner looks this hook up on the wrapper. Without it every
# decode row runs scalar preprocess, which reads its codec id with
# a blocking ``.item()``.
batch_decode = getattr(self.talker, "preprocess_decode_batch", None)
if callable(batch_decode):
self.preprocess_decode_batch = batch_decode
# Model Runner V2 hooks: device-side codec output, EOS control and
# codec penalty (see MiniCPMO45OmniTTSForConditionalGeneration).
for hook_name in ("preprocess_decode_batch_mrv2", "make_omni_output_mrv2", "mrv2_custom_sampler"):
hook = getattr(self.talker, hook_name, None)
if callable(hook):
setattr(self, hook_name, hook)
self.mrv2_decode_preprocess_is_identity = bool(
getattr(self.talker, "mrv2_decode_preprocess_is_identity", False)
)
self.logits_vocab_size = int(self.talker.logits_vocab_size)
else:
raise ValueError(f"Invalid model stage: {self.model_stage}. Must be one of: 'llm', 'tts'")
@@ -173,7 +202,13 @@ class MiniCPMO45OmniForConditionalGeneration(nn.Module, SupportsMultiModal, Supp
# preprocess for duplex audio, while the Talker converts the
# tts_token_ids/tts_hidden_states handoff into its conditioning
# embeddings and initializes request-local codec generation state.
self.has_preprocess = self.model_stage in {"llm", "tts"}
# Turn-mode Thinker inputs use the native multimodal encoder/cache on
# MRv2. Marking it as a custom-preprocess model disables that encoder
# path in the runner. Duplex still uses the V1 preprocess hook.
self.has_preprocess = self.model_stage == "tts" or not self._use_v2_model_runner
# Neither AR stage has a postprocess, so step outputs can use the
# runner's async snapshot instead of a blocking per-step D2H.
self.use_async_omni_output = self.model_stage in {"llm", "tts"}
if self.model_stage == "llm" and getattr(vllm_config.model_config, "session_mode", "turn") == "duplex":
# Build the Stage-0 duplex runtime (remote-code processor and
@@ -617,6 +652,12 @@ class MiniCPMO45OmniForConditionalGeneration(nn.Module, SupportsMultiModal, Supp
if text_hidden_states.ndim == 3 and text_hidden_states.shape[0] == 1:
text_hidden_states = text_hidden_states.squeeze(0)
if getattr(self, "_use_v2_model_runner", False):
# FULL graphs return tensors. The row ledger is reconstructed
# from this step's live input batch after replay, never from
# Python values or token buffers captured during warmup.
return text_hidden_states
# Return hidden states with latent in multimodal_outputs for stage_input_processors
multimodal_outputs = {"latent": text_hidden_states}
# Keep per-forward row identities alongside the latent payload.
@@ -691,6 +732,27 @@ class MiniCPMO45OmniForConditionalGeneration(nn.Module, SupportsMultiModal, Supp
raise ValueError(f"Unsupported model stage: {self.model_stage}")
def make_omni_output_mrv2(self, model_outputs, *, input_batch, req_states, model_intermediate_buffer):
"""Attach the Thinker row identities used by the llm2tts bridge."""
if self.model_stage != "llm":
return self.talker.make_omni_output_mrv2(
model_outputs,
input_batch=input_batch,
req_states=req_states,
model_intermediate_buffer=model_intermediate_buffer,
)
if any(isinstance(info, dict) and info.get("duplex") for info in model_intermediate_buffer):
raise NotImplementedError("MiniCPM-o duplex Thinker requires model_runner: v1")
num_tokens = model_outputs.shape[0]
return OmniOutput(
text_hidden_states=model_outputs,
multimodal_outputs={
"latent": model_outputs,
"latent_input_ids": input_batch.input_ids[:num_tokens].reshape(-1, 1),
"latent_positions": input_batch.positions[:num_tokens].reshape(-1, 1),
},
)
def make_omni_output(self, model_outputs, **kwargs):
if self.model_stage != "tts":
return model_outputs
@@ -14,8 +14,10 @@ Pipeline:
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import replace
from types import SimpleNamespace
from typing import Any
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -34,6 +36,8 @@ from vllm_omni.model_executor.models.minicpmo_4_5 import (
)
from vllm_omni.model_executor.models.output_templates import OmniOutput
from vllm_omni.platforms import current_omni_platform
from vllm_omni.utils.device_copy import index_to_device, to_device_nonblocking
from vllm_omni.worker_v2.omni_sampler import OmniSampler
logger = init_logger(__name__)
@@ -105,6 +109,38 @@ def _restore_weight_norm_weight(weight_g: torch.Tensor, weight_v: torch.Tensor)
return torch._weight_norm(weight_v, weight_g, dim=0)
class _HostCodecIds:
"""A batch of device codec ids, copied to pinned host memory without a host sync.
The copy is queued before the Talker forward, so ``values()`` waits only
for the previous step's sampler (the same point vLLM's penalty path already
waits for), not for this step's forward.
"""
__slots__ = ("_event", "_host", "_values")
def __init__(self, ids: torch.Tensor) -> None:
self._event: torch.cuda.Event | None = None
self._host: torch.Tensor | None = None
self._values: list[int] | None = None
ids = ids.detach().reshape(-1)
if ids.device.type == "cuda":
self._host = torch.empty(ids.shape, dtype=ids.dtype, device="cpu", pin_memory=True)
self._host.copy_(ids, non_blocking=True)
self._event = torch.cuda.Event()
self._event.record()
else:
self._values = ids.tolist()
def values(self) -> list[int]:
if self._values is None:
assert self._event is not None and self._host is not None
self._event.synchronize()
self._values = self._host.tolist()
self._event = self._host = None
return self._values
def _apply_batched_repetition_penalty(
logits: torch.Tensor,
histories: Sequence[torch.Tensor],
@@ -149,7 +185,8 @@ def _apply_batched_repetition_penalty(
# The vocabulary fixes the output size. CUDA bincount still reads the
# maximum id back to the host even when minlength is provided.
encoded = encoded_rows[0] if len(encoded_rows) == 1 else torch.cat(encoded_rows)
encoded = encoded.to(device=logits.device)
# Pinned, non-blocking: a pageable H2D would wait for queued GPU work.
encoded = to_device_nonblocking(encoded, logits.device)
frequencies = torch.zeros((end - start) * vocab_size, dtype=torch.long, device=logits.device)
frequencies.scatter_add_(0, encoded, torch.ones_like(encoded))
frequencies = frequencies.reshape(end - start, vocab_size)
@@ -159,6 +196,150 @@ def _apply_batched_repetition_penalty(
return penalized
def _apply_codec_window_penalty_gpu(
logits: torch.Tensor,
slots: torch.Tensor,
all_token_ids: torch.Tensor,
total_len: torch.Tensor,
prompt_len: torch.Tensor,
penalty: torch.Tensor,
*,
window_size: int,
) -> None:
"""In place: ``_apply_batched_repetition_penalty`` on the runner's device token history.
Model Runner V2 keeps every request's sampled ids (``all_token_ids``) and
lengths on the device, so the 16-frame window is gathered there instead of
being rebuilt on the host each step. Row ``r`` is scored over its last
``window_size`` output ids, ``[max(total_len - window, prompt_len),
total_len)``: the codes sampled so far, exactly the history the V1 path
builds in ``make_omni_output``. Same arithmetic as the V1 helper, so the
penalized logits are identical.
"""
num_rows, vocab_size = logits.shape
if num_rows == 0:
return
slots = slots.long()
end = total_len.index_select(0, slots).long()
start = torch.maximum(end - window_size, prompt_len.index_select(0, slots).long())
offsets = torch.arange(window_size, device=logits.device, dtype=torch.long)
positions = end.unsqueeze(1) - window_size + offsets.unsqueeze(0)
valid = positions >= start.unsqueeze(1)
rows = all_token_ids.index_select(0, slots)
history = rows.gather(1, positions.clamp_min(0)).long()
# Invalid positions count into a spare column that is dropped below.
history = torch.where(valid & (history >= 0) & (history < vocab_size), history, vocab_size)
frequencies = torch.zeros((num_rows, vocab_size + 1), dtype=torch.long, device=logits.device)
frequencies.scatter_add_(1, history, torch.ones_like(history))
alpha = torch.pow(
penalty.index_select(0, slots).to(dtype=logits.dtype).unsqueeze(1),
frequencies[:, :vocab_size].to(dtype=logits.dtype),
)
logits.copy_(torch.where(logits < 0, logits * alpha, logits / alpha))
class _CodecWindowPenaltiesState:
"""MRv2 sampler penalties for the MiniCPM-o Talker.
Replaces the sampler's presence-based repetition penalty (whole prompt and
output) with MiniCPMTTS's frequency penalty over the last 16 codes, scored
on the device. Frequency and presence penalties, if a request sets them,
still go through the upstream state, which keeps the output bin counts.
"""
def __init__(self, base: Any, *, window_size: int) -> None:
from vllm.v1.worker.gpu.buffer_utils import UvaBackedTensor
self.base = base
self.req_states = base.req_states
self.window_size = int(window_size)
max_num_reqs = int(self.req_states.max_num_reqs)
self.repetition_penalty = UvaBackedTensor(max_num_reqs, dtype=torch.float32)
self.repetition_penalty.np.fill(1.0)
self.repetition_penalty.copy_to_uva()
self.use_window = np.zeros(max_num_reqs, dtype=bool)
self.use_penalty = np.zeros(max_num_reqs, dtype=bool)
@property
def output_bin_counts(self) -> torch.Tensor:
return self.base.output_bin_counts
def add_request(self, req_idx: int, sampling_params: Any) -> None:
repetition = float(getattr(sampling_params, "repetition_penalty", 1.0))
self.repetition_penalty.np[req_idx] = repetition
self.use_window[req_idx] = repetition != 1.0
self.base.add_request(
req_idx,
SimpleNamespace(
repetition_penalty=1.0,
frequency_penalty=float(getattr(sampling_params, "frequency_penalty", 0.0)),
presence_penalty=float(getattr(sampling_params, "presence_penalty", 0.0)),
),
)
self.use_penalty[req_idx] = bool(self.use_window[req_idx] or self.base.use_penalty[req_idx])
def apply_staged_writes(self) -> None:
self.repetition_penalty.copy_to_uva()
self.base.apply_staged_writes()
def apply_penalties(
self,
logits: torch.Tensor,
expanded_idx_mapping: torch.Tensor,
idx_mapping_np: np.ndarray,
input_ids: torch.Tensor,
expanded_local_pos: torch.Tensor,
) -> None:
if np.any(self.use_window[idx_mapping_np]):
_apply_codec_window_penalty_gpu(
logits,
expanded_idx_mapping,
self.req_states.all_token_ids.gpu,
self.req_states.total_len.gpu,
self.req_states.prompt_len.gpu,
self.repetition_penalty.gpu,
window_size=self.window_size,
)
self.base.apply_penalties(logits, expanded_idx_mapping, idx_mapping_np, input_ids, expanded_local_pos)
def _install_mrv2_talker_sampler(sampler: Any, talker: "MiniCPMO45OmniTTSForConditionalGeneration") -> Any:
"""Wrap the runner's MRv2 sampler with codec penalties and EOS control.
The upstream sampler already applies ``min_tokens`` (codec EOS is a stage
stop token), temperature, top-k/top-p and seeded sampling on the device.
The Talker adds its 16-frame codec penalty and overrides rows the model
terminates (``_mrv2_forced_eos``) with codec EOS after sampling, exactly
where V1's ``_force_eos_on_sampled_ids`` does.
"""
sampler.penalties_state = _CodecWindowPenaltiesState(sampler.penalties_state, window_size=_CODEC_PENALTY_WINDOW)
talker._mrv2_empty_speech = torch.zeros(
int(sampler.req_states.max_num_reqs), dtype=torch.bool, device=sampler.req_states.device
)
logger.info(
"MiniCPM-o Talker: MRv2 sampler with device-side %d-frame codec penalty and EOS control",
_CODEC_PENALTY_WINDOW,
)
return MiniCPMO45TalkerSampler(sampler, talker)
class MiniCPMO45TalkerSampler(OmniSampler):
omni_static_staged_writes = True
def __init__(self, base_sampler, talker):
super().__init__(base_sampler)
self.talker = talker
def __call__(self, logits: torch.Tensor, input_batch: Any) -> Any:
forced = self.talker.take_mrv2_forced_eos(input_batch, self.req_states, logits.shape[0])
output = self.base_sampler(logits, input_batch)
if forced is not None:
sampled = output.sampled_token_ids
sampled.masked_fill_(forced.view(-1, *([1] * (sampled.ndim - 1))), int(self.talker._codec_eos_id))
return output
class _MiniCPMTTSProjector(nn.Module):
"""Checkpoint-compatible hidden-state projector used by MiniCPMTTS."""
@@ -188,6 +369,9 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
self._mask_eos_rows: list[bool] | None = None
self._pending_force_eos_rows: list[bool] | None = None
self._penalty_histories: list[torch.Tensor] | None = None
# Host copies of this step's decode input ids, keyed by request id:
# make_omni_output reads them for EOS detection and penalty history.
self._decode_codec_ids: dict[str, tuple[_HostCodecIds, int]] = {}
self._request_audio_states: dict[str, dict[str, Any]] = {}
# Mirrors upstream TTSStreamingGenerator._chunk_info: one committed
# condition plus, during a rollover, one immutable recompute recipe.
@@ -212,9 +396,17 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
self.has_preprocess = True
self.has_postprocess = False
# Same-step codes travel through make_omni_output from the previous
# sampled id (decode preprocess embeds that id via emb_code).
self.gpu_resident_buffer_keys: set[tuple[str, str]] = {("codes", "audio")}
# sampled id (decode preprocess embeds that id via emb_code). They are
# CPU transport deltas, so no buffer key is GPU-resident.
self._init_native_talker(prefix)
# Model Runner V2 keeps the sampled id, codec history and EOS state on
# the GPU (see make_omni_output_mrv2 and mrv2_custom_sampler).
self._use_v2_model_runner = bool(getattr(vllm_config.model_config, "use_v2_model_runner", False))
# Per request slot: the Thinker handed over an empty condition.
self._mrv2_empty_speech: torch.Tensor | None = None
# Rows the sampler must force to codec EOS, computed with this step's output.
self._mrv2_forced_eos: torch.Tensor | None = None
self._mrv2_decode_rows_logged = False
def _init_native_talker(self, prefix: str) -> None:
if self._tts_config is None:
@@ -255,11 +447,7 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
def _boundary_embeddings(self) -> torch.Tensor:
"""Embed the ``<text_eos><audio_bos>`` tail every condition ends with."""
ids = torch.tensor(
[self._text_eos_id, self._tts_bos_id],
device=self.emb_text.weight.device,
dtype=torch.long,
)
ids = index_to_device([self._text_eos_id, self._tts_bos_id], self.emb_text.weight.device)
return self.emb_text(ids)
def _build_condition_embeddings(
@@ -278,8 +466,9 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
return self._boundary_embeddings()
device = self.emb_text.weight.device
dtype = self.emb_text.weight.dtype
token_ids = tts_token_ids.to(device=device, dtype=torch.long).reshape(-1)
hidden = tts_hidden_states.to(device=device, dtype=dtype)
# Pinned, non-blocking H2D: a pageable copy would stall every row in the step.
token_ids = to_device_nonblocking(tts_token_ids, device).to(dtype=torch.long).reshape(-1)
hidden = to_device_nonblocking(tts_hidden_states, device).to(dtype=dtype)
if hidden.shape[0] != token_ids.shape[0] and token_ids.shape[0] != 1:
raise ValueError(
"MiniCPM-o Talker condition length mismatch: "
@@ -289,7 +478,7 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
hidden_embeds = self.projector_semantic(hidden)
if self._normalize:
hidden_embeds = F.normalize(hidden_embeds, p=2, dim=-1)
audio_bos = self.emb_text(torch.tensor([self._tts_bos_id], device=device, dtype=torch.long))
audio_bos = self.emb_text(index_to_device([self._tts_bos_id], device))
condition = text_embeds + hidden_embeds
if native_duplex:
# Match MiniCPMTTS.generate_chunk's streaming condition.
@@ -565,6 +754,8 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
# Decode: vLLM's previous sampled codec id is this step's input.
# Embed it with the codec table (not the 32k Llama embed_tokens) and
# hand the same id to make_omni_output so Code2Wav sees it this step.
# The runner sends one-token decode rows through preprocess_decode_batch,
# which defers this ``.item()`` EOS check to make_omni_output.
code = input_ids.to(device=self.emb_code[0].weight.device, dtype=torch.long).reshape(-1)[-1:]
embeds = self.emb_code[0](code)
code_id = int(code.item())
@@ -580,6 +771,185 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
delta = torch.tensor([[code_id]], dtype=torch.long, device="cpu")
return input_ids, embeds, {"codes": {"audio": delta}}
def _decode_codec_id_map(self) -> dict[str, tuple[_HostCodecIds, int]]:
pending = getattr(self, "_decode_codec_ids", None)
if pending is None:
pending = {}
self._decode_codec_ids = pending
return pending
def preprocess_decode_batch(
self,
*,
input_ids: torch.Tensor,
req_infos: list[dict[str, Any]],
) -> tuple[torch.Tensor, torch.Tensor, list[dict[str, Any]]]:
"""Embed every one-token decode row at once, without reading ids on the host.
Same result per row as scalar ``preprocess``, except that its per-row
``.item()`` moves to ``make_omni_output``: one non-blocking copy of all
rows' ids is queued here, before the forward, and read there to build the
CPU delta and apply the codec-EOS check. Nothing reads the request's
``finished`` state between the two points.
"""
num_rows = len(req_infos)
if input_ids.numel() != num_rows:
raise ValueError(f"MiniCPM-o Talker batched decode needs one id per row: {input_ids.numel()} != {num_rows}")
weight = self.emb_code[0].weight
codes = input_ids.reshape(-1).to(device=weight.device, dtype=torch.long)
embeds = self.emb_code[0](codes)
# The runner and connector consume CPU codec ids.
empty_codes = torch.empty(0, dtype=torch.long, device="cpu")
request_states = self._request_audio_states
updates: list[dict[str, Any]] = []
live_rows: list[tuple[int, str]] = []
zero_rows: list[int] = []
for row, info in enumerate(req_infos):
request_id = str(info.get("request_id", "0"))
state = info.get("audio_state")
if info.get("_omni_is_prefill", False) or not isinstance(state, dict):
# A row without Talker state builds its condition like a prefill.
_, row_embeds, update = self.preprocess(input_ids=input_ids[row : row + 1], input_embeds=None, **info)
embeds[row : row + 1] = row_embeds
updates.append(update)
continue
stored = request_states.get(request_id)
if isinstance(stored, dict):
state = stored
if state.get("finished"):
# Leftover decode of a finished request: shape-correct zeros.
zero_rows.append(row)
updates.append({"codes": {"audio": empty_codes}})
continue
live_rows.append((row, request_id))
# The id is not on the host yet: make_omni_output builds this row's
# delta from the host copy. The empty entry keeps no stale delta.
updates.append({"codes": {"audio": empty_codes}})
if zero_rows:
embeds.index_fill_(0, index_to_device(zero_rows, embeds.device), 0.0)
if live_rows:
host_ids = _HostCodecIds(codes)
pending = self._decode_codec_id_map()
for row, request_id in live_rows:
pending[request_id] = (host_ids, row)
return input_ids, embeds, updates
# ------------------------------------------------------------------
# Model Runner V2
# ------------------------------------------------------------------
#
# Under MRv2 a decode row's input id is the previous step's sample, which
# the runner gathers on the device (``last_sampled_tokens``) and embeds
# into its static input buffer with ``embed_input_ids`` (``emb_code``).
# That is the whole decode preprocess, so decode rows skip the per-row
# hook. Everything V1 derives on the host from that id each step -- the
# codec delta, codec-EOS detection, the step count behind the length cap
# and the 16-frame penalty history -- is derived on the device from the
# runner's request state instead, and nothing waits for a D2H copy.
#: Decode rows need no per-row preprocess beyond the runner's embedding.
mrv2_decode_preprocess_is_identity = True
@property
def logits_vocab_size(self) -> int:
return int(self._num_audio_tokens)
def preprocess_decode_batch_mrv2(
self,
*,
input_ids: torch.Tensor,
input_embeds: torch.Tensor,
req_infos: list[dict[str, Any]],
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, list[dict[str, Any]]]:
"""Decode rows are ``emb_code`` of their input id, already in ``input_embeds``."""
num_rows = len(req_infos)
empty = input_embeds.new_empty((num_rows, 0))
return input_ids, input_embeds, empty, empty, [{} for _ in range(num_rows)]
def mrv2_custom_sampler(self, sampler: Any) -> tuple[Any, None]:
return _install_mrv2_talker_sampler(sampler, self), None
def make_omni_output_mrv2(
self,
model_outputs: torch.Tensor | OmniOutput,
*,
input_batch: Any,
req_states: Any,
model_intermediate_buffer: list[dict[str, Any]],
) -> OmniOutput:
"""Device-side ``make_omni_output`` for Model Runner V2.
Emits every token row's input id as ``codes.audio`` plus
``meta.codec_frame_valid``: true only for a decode row whose input is a
codec id of a request that has not ended. That id is the code V1 hands
to Code2Wav on this step; the stage payload builder drops the other
rows on the host, after the asynchronous output copy. Rows the model
terminates this step (empty condition, codec EOS already sampled, or
the offline length cap) are recorded for the sampler.
"""
if isinstance(model_outputs, OmniOutput):
return model_outputs
hidden = model_outputs
empty_speech = self._mrv2_empty_speech
if empty_speech is None:
raise RuntimeError("MiniCPM-o Talker MRv2 output requires its MRv2 sampler (mrv2_custom_sampler)")
num_reqs = int(input_batch.num_reqs)
device = hidden.device
if input_batch.has_prefill:
# Prefill rows only: record whether the Thinker handed over an
# empty condition (preprocess marks such a request finished).
slots: list[int] = []
flags: list[bool] = []
for row in np.flatnonzero(input_batch.is_prefilling_np[:num_reqs]).tolist():
info = model_intermediate_buffer[row]
if info.get("native_duplex") is True:
raise NotImplementedError("MiniCPM-o native duplex Talker requires model_runner: v1")
state = info.get("audio_state")
flags.append(bool(state.get("finished")) if isinstance(state, dict) else False)
slots.append(int(input_batch.idx_mapping_np[row]))
if slots:
empty_speech.index_copy_(
0,
index_to_device(slots, device),
index_to_device(flags, device, dtype=torch.bool),
)
num_tokens = int(hidden.shape[0])
token_ids = input_batch.input_ids[:num_tokens]
last_rows = input_batch.logits_indices[:num_reqs].long()
slot_ids = input_batch.idx_mapping[:num_reqs].long()
prompt_len = req_states.prompt_len.gpu.index_select(0, slot_ids).long()
# Codes emitted before this step's input (V1's ``state["step"]``).
step = input_batch.seq_lens[:num_reqs].long() - prompt_len
last_ids = token_ids.index_select(0, last_rows)
empty = empty_speech.index_select(0, slot_ids)
decode = step > 0
eos_input = decode & (last_ids == int(self._codec_eos_id))
valid = decode & ~eos_input & ~empty
# MiniCPMTTS.generate's max_new_token, clamped to the Talker context
# (see ``preprocess``); the sample after the last allowed code is EOS.
remaining = int(self._tts_config.max_position_embeddings) - prompt_len
limit = torch.clamp(remaining, min=1, max=_OFFLINE_CODEC_MAX_NEW_TOKENS) - 1
self._mrv2_forced_eos = empty | eos_input | (step >= limit)
frame_valid = torch.zeros(num_tokens, dtype=torch.bool, device=device)
frame_valid.index_copy_(0, last_rows, valid)
if not self._mrv2_decode_rows_logged and not input_batch.has_prefill:
self._mrv2_decode_rows_logged = True
logger.info("MiniCPM-o Talker: MRv2 device-side codec output active (no host read of sampled ids)")
return OmniOutput(
text_hidden_states=hidden,
multimodal_outputs={
"codes": {"audio": token_ids.to(dtype=torch.long).reshape(num_tokens, 1)},
"meta": {"codec_frame_valid": frame_valid},
},
)
def take_mrv2_forced_eos(self, input_batch: Any, req_states: Any, num_rows: int) -> torch.Tensor | None:
"""This step's forced-EOS rows, once; ``None`` outside a model step (warmup)."""
forced, self._mrv2_forced_eos = self._mrv2_forced_eos, None
if forced is None or int(forced.shape[0]) != int(num_rows):
return None
return forced
def make_omni_output(
self,
model_outputs: torch.Tensor | OmniOutput,
@@ -606,6 +976,8 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
mask_eos_rows = [False] * len(infos)
empty_history = torch.empty(0, dtype=torch.long, device="cpu")
penalty_histories = [empty_history for _ in infos]
pending_codec_ids = self._decode_codec_id_map()
codec_eos_id = int(self._codec_eos_id)
for index, info in enumerate(infos):
info_dict = info if isinstance(info, dict) else {}
native_duplex = info_dict.get("native_duplex") is True
@@ -657,9 +1029,20 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
if not isinstance(state, dict):
state = dict(info.get("audio_state", {}) or {})
self._request_audio_states[request_id] = state
empty_speech = bool(state.get("finished"))
codes = info.get("codes", {})
audio = codes.get("audio") if isinstance(codes, Mapping) else None
pending = pending_codec_ids.pop(request_id, None)
if pending is not None:
# A preprocess_decode_batch row: build what scalar preprocess
# would have stored, from this step's host copy of the ids.
host_ids, row = pending
code_id = host_ids.values()[row]
if code_id == codec_eos_id:
state["finished"] = True
audio = None
else:
audio = torch.tensor([[code_id]], dtype=torch.long, device="cpu")
empty_speech = bool(state.get("finished"))
if isinstance(audio, torch.Tensor) and audio.numel() > 0:
audio = audio.to(device="cpu", dtype=torch.long)
codec_deltas[index] = audio.reshape(-1, 1)
@@ -725,9 +1108,11 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
def _flush_deferred_cleanup(self) -> None:
request_audio_states = getattr(self, "_request_audio_states", {})
request_condition_states = getattr(self, "_request_condition_states", {})
decode_codec_ids = getattr(self, "_decode_codec_ids", {})
for request_id in self._deferred_cleanup_ids:
request_audio_states.pop(request_id, None)
request_condition_states.pop(request_id, None)
decode_codec_ids.pop(request_id, None)
self._deferred_cleanup_ids.clear()
def _dummy_hidden_states(
@@ -789,18 +1174,21 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
# we just forced (it is in the stage's ``stop_token_ids``), leaving an
# all -inf row and a request that never releases.
self._pending_force_eos_rows = force_eos if need_force else None
# Row masks go up through pinned memory and are applied with where /
# masked_fill: a pageable H2D or boolean-mask indexing blocks the host.
if need_force or need_mask:
logits = logits.clone()
eos_id = int(self._codec_eos_id)
if need_force:
assert force_eos is not None
forced = torch.tensor(force_eos, dtype=torch.bool, device=logits.device)
logits[forced] = float("-inf")
logits[forced, eos_id] = 0.0
forced = index_to_device(force_eos, logits.device, dtype=torch.bool)
eos_only = torch.full_like(logits[:1], float("-inf"))
eos_only[:, eos_id] = 0.0
logits = torch.where(forced.unsqueeze(1), eos_only, logits)
if need_mask:
assert mask_eos is not None
masked = torch.tensor(mask_eos, dtype=torch.bool, device=logits.device)
logits[masked, eos_id] = float("-inf")
masked = index_to_device(mask_eos, logits.device, dtype=torch.bool)
logits[:, eos_id].masked_fill_(masked, float("-inf"))
return logits
def sample(self, logits, sampling_metadata):
@@ -867,8 +1255,8 @@ class MiniCPMO45OmniTTSForConditionalGeneration(nn.Module, SupportsPP):
sampled = getattr(output, "sampled_token_ids", None)
if not isinstance(sampled, torch.Tensor) or sampled.shape[0] != len(force_eos):
return output
rows = torch.tensor(force_eos, dtype=torch.bool, device=sampled.device)
sampled[rows] = int(self._codec_eos_id)
rows = index_to_device(force_eos, sampled.device, dtype=torch.bool)
sampled.masked_fill_(rows.view(-1, *([1] * (sampled.ndim - 1))), int(self._codec_eos_id))
return output
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]):
@@ -47,6 +47,7 @@ MINICPMO_4_5_PIPELINE = PipelineConfig(
owns_tokenizer=True,
requires_multimodal_data=True,
engine_output_type="latent",
supports_native_mrv2_data_plane=True,
sampling_constraints={
"detokenize": True,
# The llm2tts bridge discards this boundary and every row
@@ -65,6 +66,9 @@ MINICPMO_4_5_PIPELINE = PipelineConfig(
custom_process_input_func=f"{_PROC}.llm2tts",
custom_process_next_stage_input_func=f"{_PROC}.tts2code2wav_full_payload",
async_chunk_process_next_stage_input_func=f"{_PROC}.tts2code2wav_async_chunk",
# Takes effect only when the deploy selects model_runner v2 for
# this stage (turn sessions only; duplex stays on V1).
supports_native_mrv2_data_plane=True,
sampling_constraints={
"detokenize": False,
# MiniCPM-o 4.5 codec EOS is tts_config.num_audio_tokens - 1.
@@ -81,6 +85,7 @@ MINICPMO_4_5_PIPELINE = PipelineConfig(
final_output_type="audio",
engine_output_type="audio",
model_arch="MiniCPMO45Code2Wav",
supports_native_mrv2_data_plane=True,
sync_process_input_func=f"{_PROC}.tts2code2wav_token_only",
sampling_constraints={"detokenize": True},
requires_full_payload_input=True,