Merge main into dev/vllm-align for vLLM 0.31 release

Signed-off-by: tzhouam <tzhouam@connect.ust.hk>
This commit is contained in:
tzhouam committed 2026-10-03 21:05:17 +08:00
commit 008fa25263
252 files changed
+27589 -1237

No files matched your search

@@ -148,11 +148,13 @@ _MODEL_PROMOTE = (
"COSMOS3_SOUND_TOKENIZER_CONFIG_PATH",
"COSMOS3_SOUND_TOKENIZER_PATH",
"COSYVOICE3_BATCH_FLOW",
"COSYVOICE3_CACHED_ISTFT",
"COSYVOICE3_ESTIMATOR_ONNX",
"COSYVOICE3_FLOW_CUDAGRAPH",
"COSYVOICE3_FLOW_GRAPH",
"COSYVOICE3_FLOW_GRAPH_MAX_TOTAL",
"COSYVOICE3_FULL_RESPONSE_OPTIMIZATIONS",
"COSYVOICE3_HIFT_GRAPH",
"COSYVOICE3_PACKED_STREAMING",
"COSYVOICE3_REFERENCE_PREFETCH",
"COSYVOICE3_REFERENCE_WORKERS",
@@ -180,6 +182,24 @@ _MODEL_PROMOTE = (
"VLLM_GEPARD_FIRST_CHUNK_FRAMES",
"VLLM_GEPARD_LOOKBACK_FRAMES",
"VLLM_OMNI_FISH_KVCACHE_ATTN",
"VLLM_OMNI_MOSS_LOCAL_COMPILE_AUDIO_SAMPLER",
"VLLM_OMNI_MOSS_LOCAL_SHORT_ATTN",
"VLLM_OMNI_MOSS_REF_ATTN",
"VLLM_OMNI_MOSS_REF_BATCHED_TRANSFER",
"VLLM_OMNI_MOSS_REF_BATCH_WINDOW_MS",
"VLLM_OMNI_MOSS_REF_CACHE_ROPE",
"VLLM_OMNI_MOSS_REF_CODES_SHARED_DIR",
"VLLM_OMNI_MOSS_REF_COMPILE",
"VLLM_OMNI_MOSS_REF_ENCODER_WORKERS",
"VLLM_OMNI_MOSS_REF_FA_VERSION",
"VLLM_OMNI_MOSS_REF_GPU_PREP",
"VLLM_OMNI_MOSS_REF_GRAPHS",
"VLLM_OMNI_MOSS_REF_HOST_WINDOW_MS",
"VLLM_OMNI_MOSS_REF_INFLIGHT",
"VLLM_OMNI_MOSS_REF_SHARED_ENCODER",
"VLLM_OMNI_MOSS_REF_SINGLETON_BUCKET_MS",
"VLLM_OMNI_MOSS_REF_SKIP_PADDED_QUERY_MASK",
"VLLM_OMNI_MOSS_REF_STREAM_PRIORITY",
"VLLM_OMNI_SANA_WM_STAGE1_TEXT_ENCODER",
"VLLM_OMNI_SEEDVR2_LONG_JOB_TTL_SECONDS",
"VLLM_OMNI_SEEDVR2_LONG_MAX_UPLOAD_BYTES",
@@ -189,6 +209,7 @@ _MODEL_PROMOTE = (
"VLLM_OMNI_SEEDVR2_SHARDED_CLIP_PIXELS",
"VLLM_OMNI_SEEDVR2_SHARDED_FRAME_PIXELS",
"VLLM_OMNI_SENSENOVA_PAGED_DECODE",
"YUE2_VAE", # VAE checkpoint selection belongs in typed model configuration.
)
_MODEL_REQUEST_SCOPE = (
@@ -259,6 +280,7 @@ _BENCHMARK_TRANSITIONAL = (
)
_INTERNAL = (
"VLLM_OMNI_TALKER_FIRST_AUDIO",
"VLLM_OMNI_CODEC_FIRST_CHUNK_EXPRESS",
"VLLM_OMNI_CODEC_FIRST_CHUNK_EXPRESS_SLACK_S",
"VLLM_OMNI_CONNECTOR_RECV_POLL_MS",
+5 -1
View File
@@ -5,6 +5,7 @@ from dataclasses import MISSING, field
from typing import Any
from pydantic import ConfigDict, TypeAdapter
from transformers import PretrainedConfig
from vllm.config import ModelConfig
from vllm.config.utils import config
from vllm.logger import init_logger
@@ -124,9 +125,12 @@ class OmniModelConfig(ModelConfig):
"""
stage_id: int = 0
# Match the upstream derived field while its value is replaced per stage.
hf_text_config: PretrainedConfig = field(init=False)
async_chunk: bool = False
session_mode: str = "turn"
retains_state_across_chunks: bool = False
supports_running_prefix_cache_reset: bool = True
use_v2_model_runner: bool = False
supports_native_mrv2_data_plane: bool = False
# Stage-1 active stream slots; 0 keeps legacy chunk-level round-robin.
@@ -218,7 +222,7 @@ class OmniModelConfig(ModelConfig):
return convertor.convert()
return super().get_model_arch_config()
def draw_hf_text_config(self):
def draw_hf_text_config(self) -> PretrainedConfig:
# transformers' get_text_config method is used to get the text config from thinker_config.
# to handle the case that each model stage has their own text config,
# we need to draw the text config from the corresponding model stage.
+5
View File
@@ -482,6 +482,7 @@ class OmniStageModelConfig(_TrackExplicitConfigFields):
interleave_mm_strings: bool | None = None
media_io_kwargs: dict[str, Any] | None = None
final_output: bool = False
supports_running_prefix_cache_reset: bool = True
active_stream_window: int = Field(default=0, ge=0)
session_mode: str = "turn"
duplex_max_sessions: int = Field(default=1, ge=1)
@@ -817,6 +818,7 @@ class _DiffusionConfigProjection:
prompt_embed_cache_size: int = Field(default=32, ge=1)
enable_session_state_manager: bool = False
diffusion_load_format: str = "default"
hsdp_weight_load_strategy: str = "full"
diffusers_load_kwargs: dict[str, Any] = field(default_factory=dict)
diffusers_call_kwargs: dict[str, Any] = field(default_factory=dict)
diffusers_pipeline_cls: Any = None
@@ -1324,6 +1326,7 @@ _DIFFUSION_STAGE_METADATA_FIELDS = frozenset(
"model_arch",
"model_stage",
"retains_state_across_chunks",
"supports_running_prefix_cache_reset",
"scheduler_cls",
"stage_connector_spec",
"worker_type",
@@ -1971,6 +1974,8 @@ def _build_model_config(
if "active_stream_window" not in kwargs:
kwargs["active_stream_window"] = _copy_value(deploy.active_stream_window)
kwargs["final_output"] = topology.final_output
if not topology.supports_running_prefix_cache_reset:
kwargs["supports_running_prefix_cache_reset"] = False
if "custom_voice_dir" not in kwargs and deploy.custom_voice_dir is not None:
kwargs["custom_voice_dir"] = _copy_value(deploy.custom_voice_dir)
stage_runner = resolve_stage_model_runner(deploy, stage_deploy)
+4 -1
View File
@@ -117,7 +117,7 @@ from vllm_omni.model_executor.models.qwen3_omni.pipeline import (
QWEN3_OMNI_THINKER_ONLY_PIPELINE,
resolve_qwen3_omni_pipeline,
)
from vllm_omni.model_executor.models.qwen3_tts.pipeline import QWEN3_TTS_PIPELINE
from vllm_omni.model_executor.models.qwen3_tts.pipeline import QWEN3_TTS_FUSED_PIPELINE, QWEN3_TTS_PIPELINE
from vllm_omni.model_executor.models.step_audio2.pipeline import (
STEP_AUDIO2_ASR_PIPELINE,
STEP_AUDIO2_PIPELINE,
@@ -125,6 +125,7 @@ from vllm_omni.model_executor.models.step_audio2.pipeline import (
from vllm_omni.model_executor.models.voxcpm2.pipeline import VOXCPM2_PIPELINE
from vllm_omni.model_executor.models.voxtral_tts.pipeline import VOXTRAL_TTS_PIPELINE
from vllm_omni.model_executor.models.wan2_2.pipeline import WAN2_2_TI2V_PIPELINE
from vllm_omni.model_executor.models.yue2.pipeline import YUE2_PIPELINE
logger = init_logger(__name__)
@@ -203,11 +204,13 @@ OMNI_PIPELINES: dict[str, PipelineConfig | PipelineResolverFunc] = {
"qwen3_omni_moe": resolve_qwen3_omni_pipeline,
"qwen3_omni_moe_thinker_only": QWEN3_OMNI_THINKER_ONLY_PIPELINE,
"qwen3_tts": QWEN3_TTS_PIPELINE,
"qwen3_tts_fused": QWEN3_TTS_FUSED_PIPELINE,
"step_audio_2": STEP_AUDIO2_PIPELINE,
"step_audio_2_asr": STEP_AUDIO2_ASR_PIPELINE,
"voxcpm2": VOXCPM2_PIPELINE,
"voxtral_tts": VOXTRAL_TTS_PIPELINE,
"wan2_2_ti2v": WAN2_2_TI2V_PIPELINE,
"yue2": YUE2_PIPELINE,
}
+14 -1
View File
@@ -245,6 +245,8 @@ class StagePipelineConfig:
# The model keeps per-request execution state while awaiting the next
# async chunk, so the parked request continues to consume model capacity.
retains_state_across_chunks: bool = False
# Some stateful audio stages cannot roll back already consumed frames.
supports_running_prefix_cache_reset: bool = True
sampling_constraints: dict[str, Any] = field(default_factory=dict)
custom_process_input_func: str | None = None
custom_process_next_stage_input_func: str | None = None
@@ -295,6 +297,9 @@ class PipelineConfig:
model_type: str
model_arch: str = ""
stages: tuple[StagePipelineConfig, ...] = ()
# A single stage that streams its own final output in async-chunk mode
# (e.g. a Talker decoding audio in-stage) keeps deploy.async_chunk.
single_stage_async_chunk: bool = False
# HF architecture aliases: used by StageConfigFactory when the model's
# HF config reports a generic model_type that collides with a different
# model (e.g. MiMo Audio reports model_type="qwen2"). The factory
@@ -492,6 +497,7 @@ class StageDeployConfig:
auxiliary_text_encoder: str | None = None
# Runtime optimizations used by diffusion loading/execution.
hsdp_weight_load_strategy: str | None = None
enable_multithread_weight_load: bool | None = None
enable_broadcast_weight_load: bool | None = None
num_weight_load_threads: int | None = None
@@ -1106,6 +1112,8 @@ def _build_engine_args(
if ps.omni_kv_config:
engine_args["omni_kv_config"] = dict(ps.omni_kv_config)
engine_args["requires_full_payload_input"] = ps.requires_full_payload_input
if not ps.supports_running_prefix_cache_reset:
engine_args["supports_running_prefix_cache_reset"] = False
return engine_args
@@ -1171,8 +1179,11 @@ def _resolve_pipeline_async_chunk_enabled(
and False otherwise. If the user tried to enable async chunk through the deploy
config, but it's inapplicable or unsupported, it will be disabled with a warning.
"""
# Single stage should never use async chunk
if len(pipeline.stages) <= 1:
if pipeline.single_stage_async_chunk:
if deploy.async_chunk is False:
raise ValueError(f"Pipeline {pipeline.model_type!r} requires async_chunk=True")
return True
if deploy.async_chunk:
logger.warning(
"Deploy config set async_chunk=True, but async chunk is inapplicable "
@@ -1349,6 +1360,8 @@ class StageConfig:
# Terminal-stage ownership comes from topology, not engine overrides.
engine_args["final_output"] = self.final_output
if self.yaml_engine_args.get("supports_running_prefix_cache_reset") is False:
engine_args["supports_running_prefix_cache_reset"] = False
# Build runtime config from YAML defaults + CLI overrides
runtime: dict[str, Any] = dict(self.yaml_runtime)
+83 -29
View File
@@ -100,6 +100,23 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
max_num_active_reqs: int
def reset_prefix_cache(self, reset_running_requests: bool = False, reset_connector: bool = False) -> bool:
model_config = self.vllm_config.model_config
if (
reset_running_requests
and self.running
and not getattr(model_config, "supports_running_prefix_cache_reset", True)
):
# Reset preempts and resumes in the same step while discarding
# in-flight tokens. The stateful codec and queued PCM have already
# consumed those frames and cannot roll back to that boundary.
logger.warning(
"This stage cannot reset running requests; wait for "
"completion or abort them before resetting the prefix cache."
)
return False
return super().reset_prefix_cache(reset_running_requests, reset_connector)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Track requests that need KV cache transfer when finished
@@ -126,8 +143,11 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
# Track requests that have already triggered prefill transfer to avoid duplicates
self.transfer_triggered_requests: set[str] = set()
# Cache per-request flag to avoid repeated deserialization of additional_information
self._omits_kv_transfer_cache: dict[str, bool] = {}
# Cache per-request flags to avoid repeated deserialization of
# additional_information. Value is (payload_id, is_stage_zero_final,
# force_kv_transfer); payload_id invalidates the entry when the
# object is replaced.
self._omits_kv_transfer_cache: dict[str, tuple[int, bool, bool]] = {}
# KV-wait start ts for the vllm_omni:kv_wait_s metric; see
# _emit_kv_wait_output for the engine-core → orchestrator carry.
@@ -246,26 +266,40 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
return config.get(key, default)
return getattr(config, key, default) if config is not None else default
def _request_omits_kv_transfer_to_next_stage(self, request: Request) -> bool:
"""True when this stage-zero-final request does not need downstream KV.
The result is cached per request to avoid repeated deserialization of
additional_information on every scheduler tick.
"""
rid = request.request_id
cached = self._omits_kv_transfer_cache.get(rid)
if cached is not None:
return cached
def _omni_final_stage_flags(self, request: Request) -> tuple[bool, bool]:
"""Return ``(is_stage_zero_final, force_kv_transfer)`` from request metadata."""
payload = getattr(request, "additional_information", None)
if payload is None:
result = False
else:
info = deserialize_additional_information(payload)
result = info.get("omni_final_stage_id") == 0 and not bool(info.get("omni_force_kv_transfer", False))
return False, False
self._omits_kv_transfer_cache[rid] = result
return result
cache = getattr(self, "_omits_kv_transfer_cache", None)
rid = request.request_id
payload_id = id(payload)
if cache is not None:
cached = cache.get(rid)
if cached is not None and cached[0] == payload_id:
return cached[1], cached[2]
info = deserialize_additional_information(payload)
is_final = info.get("omni_final_stage_id") == 0
force_kv = bool(info.get("omni_force_kv_transfer", False))
if cache is not None:
cache[rid] = (payload_id, is_final, force_kv)
return is_final, force_kv
def _request_omits_kv_transfer_to_next_stage(self, request: Request) -> bool:
"""True when this stage-zero-final request does not need downstream KV."""
is_final, force_kv = self._omni_final_stage_flags(request)
return is_final and not force_kv
def _request_omits_chunk_transfer_to_next_stage(self, request: Request) -> bool:
"""True when this request has no downstream chunk consumer.
CFG companions still force KV transfer but are stage-0-final for
ordinary inter-stage chunks, so they omit ``save_async`` here.
"""
is_final, _ = self._omni_final_stage_flags(request)
return is_final
def _should_defer_waiting_admission(self) -> bool:
return False
@@ -517,7 +551,7 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
# domains must consume the old frame before new output passes.
request.async_tokens_to_discard = max(0, stale_async_tokens - len(generated_token_ids))
if output_is_stale or async_output_is_stale:
if async_output_is_stale or (output_is_stale and request.drop_stale_output):
# Output of a step scheduled before the request's in-flight
# tokens were discarded (segment stop / session replacement).
# num_computed_tokens was rolled back at the discard site, so
@@ -556,12 +590,13 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
# tokens and rejections. If some tokens are rejected,
# num_computed_tokens is decreased by the number of rejected
# tokens.
if request.num_computed_tokens > 0:
request.num_computed_tokens -= num_rejected
# If async scheduling, num_output_placeholders also includes
# the scheduled spec tokens count and so is similarly adjusted.
if request.num_output_placeholders > 0:
request.num_output_placeholders -= num_rejected
if not output_is_stale:
if request.num_computed_tokens > 0:
request.num_computed_tokens -= num_rejected
# If async scheduling, num_output_placeholders also includes
# the scheduled spec tokens count and so is similarly adjusted.
if request.num_output_placeholders > 0:
request.num_output_placeholders -= num_rejected
spec_decoding_stats = self.make_spec_decoding_stats(
spec_decoding_stats,
num_draft_tokens=num_draft_tokens,
@@ -600,7 +635,10 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
# Check for stop and update request status.
if new_token_ids:
num_sampled_tokens = len(new_token_ids)
new_token_ids, stopped = self._update_request_with_output(request, new_token_ids)
if output_is_stale:
new_token_ids, stopped = self._update_request_with_output(request, new_token_ids, is_stale=True)
else:
new_token_ids, stopped = self._update_request_with_output(request, new_token_ids)
if new_logprobs is not None and len(new_token_ids) < num_sampled_tokens:
# A mid-step stop (e.g. spec-decode tokens sampled past
# EOS) trims new_token_ids after the validation slice
@@ -648,6 +686,11 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
confirmed_num_computed_tokens = None
boundary_generation = None
# Only when this step might save. Read additional_information
# before _free_request rewrites it.
omits_chunk_transfer = False
if self.chunk_transfer_adapter is not None and (inter_stage_output is not None or stopped):
omits_chunk_transfer = self._request_omits_chunk_transfer_to_next_stage(request)
# Capture before resumable stop handling can clear token history.
output_token_ids: Any = getattr(request, "output_token_ids", None)
if output_token_ids is None:
@@ -707,6 +750,7 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
# a queued streaming update, and adding twice swallows the
# next duplex unit's listen/speak under async scheduling.
if request.num_in_flight_tokens > 0:
request.drop_stale_output = True
request.num_stale_output_tokens = request.num_in_flight_tokens
if outstanding_async_tokens > 0:
# Discard only outputs that are already in flight and
@@ -771,8 +815,16 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
assert not prompt_logprobs_tensors
assert prompt_token_id_logprobs is None
if self.chunk_transfer_adapter is not None and (
inter_stage_output is not None or is_segment_finished or finished
if omits_chunk_transfer and inter_stage_output is not None:
logger.warning(
"Skipping inter-stage chunk for request %s: "
"omni_final_stage_id=0 but inter_stage_output is present",
req_id,
)
if (
self.chunk_transfer_adapter is not None
and not omits_chunk_transfer
and (inter_stage_output is not None or is_segment_finished or finished)
):
save_kwargs = {
"new_token_ids": new_token_ids,
@@ -890,6 +942,7 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
# a resumable stop applies a queued update through this helper.
in_flight_tokens = int(getattr(session, "num_in_flight_tokens", 0) or 0)
if in_flight_tokens > 0:
session.drop_stale_output = True
session.num_stale_output_tokens = in_flight_tokens
if outstanding_async_tokens > 0:
# Async scheduling may already have sampled the previous
@@ -1413,6 +1466,7 @@ class OmniARScheduler(OmniSchedulerMixin, VLLMScheduler):
if status is not None:
request.status = status
if native_transfer and connector_delay_free_blocks:
assert transfer_params is not None
kv_xfer_params = {
**(kv_xfer_params or {}),
"transfer_id": transfer_params["transfer_id"],
@@ -3,7 +3,9 @@
from __future__ import annotations
import math
import os
import queue
import time
from collections import defaultdict
from typing import TYPE_CHECKING, Any
@@ -57,6 +59,48 @@ class OmniGenerationScheduler(OmniSchedulerMixin, VLLMScheduler):
self._init_omni_io_scheduling_state()
self._retains_state_across_chunks = bool(getattr(model_config, "retains_state_across_chunks", False))
self._pending_finish_reqs: list[Request] = []
connector_config = getattr(model_config, "stage_connector_config", None)
extra = (
connector_config.get("extra", connector_config)
if isinstance(connector_config, dict)
else getattr(connector_config, "extra", None)
) or {}
self._generation_min_batch_size = int(extra.get("generation_min_batch_size", 1))
self._generation_max_wait_s = float(extra.get("generation_max_wait_ms", 0)) / 1000
self._generation_coalescing_policy = extra.get("generation_coalescing_policy", "fixed")
self._generation_max_regular_batch = int(extra.get("generation_max_regular_batch", 0) or 0)
if self._generation_max_regular_batch < 0:
raise ValueError("generation_max_regular_batch must be nonnegative")
if self._generation_coalescing_policy not in ("fixed", "idle_wait"):
raise ValueError("generation_coalescing_policy must be fixed or idle_wait")
if not 1 <= self._generation_min_batch_size <= self.max_num_running_reqs:
raise ValueError("generation_min_batch_size must be in [1, max_num_seqs]")
if not math.isfinite(self._generation_max_wait_s) or self._generation_max_wait_s < 0:
raise ValueError("generation_max_wait_ms must be finite and nonnegative")
if self._generation_max_wait_s and self._generation_min_batch_size > 1:
parallel = self.vllm_config.parallel_config
if not self._native_data_plane:
# CUDA-oriented profiles can inherit a platform's V1 runner.
# Their connector extras survive that override, but batching
# must revert to the fallback runner's default scheduling.
logger.warning(
"Generation batch waiting and regular-batch limit ignored: "
"requires native MRV2; using default generation scheduling."
)
self._generation_min_batch_size = 1
self._generation_max_wait_s = 0.0
self._generation_max_regular_batch = 0
elif not (
self._retains_state_across_chunks
and parallel.tensor_parallel_size == parallel.pipeline_parallel_size == 1
):
raise ValueError("Generation batch waiting requires a stateful native MRV2 TP1/PP1 stage")
else:
logger.info(
"Generation input coalescing: target batch=%d, max wait=%.3f ms",
self._generation_min_batch_size,
self._generation_max_wait_s * 1000,
)
self._first_chunk_express = os.environ.get("VLLM_OMNI_CODEC_FIRST_CHUNK_EXPRESS") == "1"
self._last_step_express = False
# Streams that already had a chunk scheduled; the rest await their first.
@@ -75,6 +119,101 @@ class OmniGenerationScheduler(OmniSchedulerMixin, VLLMScheduler):
self._express_min_slack_s,
)
def _drain_omni_connector_outputs(self):
self._generation_defer_batch = False
outputs = super()._drain_omni_connector_outputs()
max_wait = getattr(self, "_generation_max_wait_s", 0)
target = getattr(self, "_generation_min_batch_size", 1)
coordinator = self.input_coordinator
if not max_wait or target <= 1 or coordinator is None or self._pause_state != PauseState.UNPAUSED:
return outputs
# Count distinct runnable streams, not notifications, padding rows, or
# chunks whose previous execution still owns their state. Waiting here
# precedes allocation and metadata replacement. Do not schedule a batch
# and then discard it to try to accumulate more input.
ready = set(coordinator.requests_with_ready_chunks)
terminal = set(coordinator.finished_requests) | set(coordinator.input_terminal_req_ids)
deadline = getattr(self, "_generation_batch_deadline", None)
if deadline is None:
deadline = time.monotonic() + max_wait
in_flight = any(
not request.is_finished() and request.num_in_flight_tokens > 0 for request in self.requests.values()
)
new_outputs = outputs
waited_without_input = False
while True:
for output in new_outputs:
ready.update(output.chunk_ready_req_ids)
terminal.update(output.chunk_finished_req_ids)
terminal.update(
req_id for req_id, metadata in output.request_metadata.items() if metadata.get("input_terminal")
)
runnable = {
req_id
for req_id in ready | terminal
if (request := self.requests.get(req_id)) is not None
and not request.is_finished()
and request.num_in_flight_tokens == 0
}
if runnable and waited_without_input:
# Idle waiting must not eat the ready batch's coalescing budget.
# Start that budget at its first arrival, once only. The two
# bounded windows can delay control processing by at most 2W.
deadline = time.monotonic() + max_wait
waited_without_input = False
# A first/final chunk participates in the same bounded window.
# At high concurrency almost every batch contains one; bypassing
# the entire batch in that case defeats coalescing. Low concurrency
# still dispatches immediately, including single-request tails.
if not runnable:
# With no output to retire and all receivers already parked,
# a wakeable inbox wait avoids repeatedly executing empty steps.
# Admission/registration/terminal work must reach the runner
# first; waiting for a not-yet-registered receiver would stall it.
can_wait_idle = (
getattr(self, "_generation_coalescing_policy", "fixed") == "idle_wait"
and not in_flight
and bool(self.requests)
and not getattr(coordinator, "pending_chunk_registrations", ())
and not getattr(coordinator, "pending_input_registrations", ())
and not getattr(self, "_pending_data_plane_terminal_req_ids", ())
and all(request.status == RequestStatus.WAITING_FOR_CHUNK for request in self.requests.values())
)
if not can_wait_idle:
break
waited_without_input = True
elif len(runnable) >= target or len(self.requests) < target:
break
remaining = deadline - time.monotonic()
if remaining <= 0:
break
if in_flight:
# Let EngineCore retire the prior output before waiting for
# more input. Blocking this thread also blocks the in-flight
# fence that makes those streams eligible for the next batch.
# Emit a normal empty scheduler step (including connector and
# cancellation bookkeeping), never discard an allocated step.
self._generation_defer_batch = True
self._generation_batch_deadline = deadline
return outputs
try:
output = self._omni_connector_output_inbox.get(timeout=remaining)
except queue.Empty:
break
outputs.append(output)
new_outputs = [output]
# Drain a burst before re-evaluating, retaining notification order.
while True:
try:
output = self._omni_connector_output_inbox.get_nowait()
except queue.Empty:
break
outputs.append(output)
new_outputs.append(output)
self._generation_batch_deadline = None
return outputs
def _build_generation_scheduler_output(
self,
new_reqs_data: list[OmniNewRequestData],
@@ -260,6 +399,11 @@ class OmniGenerationScheduler(OmniSchedulerMixin, VLLMScheduler):
self._drop_aborted_queued_requests()
self._requeue_completed_native_chunks()
self._process_pending_omni_inputs(model_mode="generation")
if getattr(self, "_generation_defer_batch", False):
token_budget = 0
max_regular = getattr(self, "_generation_max_regular_batch", 0)
if max_regular:
execution_batch_size = min(execution_batch_size, max_regular)
self._drop_aborted_queued_requests()
self._resync_streaming_input_counter()
async_chunk_transport = self._async_chunk_transport_enabled()
+12 -7
View File
@@ -274,12 +274,16 @@ class OmniSchedulerMixin(_SchedulerMixinBase):
# num_in_flight_tokens already includes any undrained stale share.
# Assign instead of accumulating so callers that fenced the same
# rollover before entering this helper do not count it twice.
session.drop_stale_output = True
session.num_stale_output_tokens = int(getattr(session, "num_in_flight_tokens", 0) or 0)
session.num_output_placeholders = 0
session.spec_token_ids = []
def _finish_streaming_session_update(self, session: Request, update: StreamingUpdate) -> None:
"""Install payload metadata and send an updated session to admission."""
cache = getattr(self, "_omits_kv_transfer_cache", None)
if cache is not None:
cache.pop(session.request_id, None)
session.additional_information = update.additional_information or None
session.model_intermediate_buffer = getattr(
update,
@@ -369,6 +373,7 @@ class OmniSchedulerMixin(_SchedulerMixinBase):
# The streaming update may already have fenced this same in-flight
# frame. Seed idempotently so the replacement does not count it twice.
request.num_stale_output_tokens = int(getattr(request, "num_in_flight_tokens", 0) or 0)
request.drop_stale_output = True
request.num_output_placeholders = 0
request.spec_token_ids = []
self._release_replaced_streaming_prompt_cache(request)
@@ -379,13 +384,8 @@ class OmniSchedulerMixin(_SchedulerMixinBase):
# ready-chunk marker remains until scheduler admission succeeds.
replaced_ids.discard(request_id)
def _consume_pending_connector_output(self, model_mode: str) -> None:
"""Drain ``self._latest_omni_connector_output`` into the coordinator.
Called at the top of every ``schedule()`` cycle. Identical between
AR and generation schedulers except for the ``model_mode`` argument
forwarded to ``update_request_metadata``.
"""
def _drain_omni_connector_outputs(self) -> list[OmniConnectorOutput]:
"""Collect control messages in order, without changing request state."""
connector_outputs: list[OmniConnectorOutput] = []
inbox = getattr(self, "_omni_connector_output_inbox", None)
if inbox is not None:
@@ -398,6 +398,11 @@ class OmniSchedulerMixin(_SchedulerMixinBase):
self._latest_omni_connector_output = None
if connector_output is not None:
connector_outputs.append(connector_output)
return connector_outputs
def _consume_pending_connector_output(self, model_mode: str) -> None:
"""Drain input notifications into the coordinator on the scheduler thread."""
connector_outputs = self._drain_omni_connector_outputs()
input_coordinator = getattr(self, "input_coordinator", None)
if input_coordinator is None:
return
@@ -379,14 +379,17 @@ class OmniSchedulingCoordinator:
)
if model_mode != "ar":
new_ids = self._flatten_prompt_token_ids(metadata.get("code_predictor_codes"))
new_codes = metadata.get("code_predictor_codes")
new_ids = self._flatten_prompt_token_ids(new_codes)
runtime_seed = None
if "left_context_size" in metadata:
runtime_seed = {
"meta": {"left_context_size": metadata["left_context_size"]},
}
request._omni_initial_model_buffer = runtime_seed
if new_ids:
# An explicit empty snapshot clears a previous codec chunk
# or prewarmed placeholder. Missing codes leave it intact.
if new_codes is not None:
request.prompt_token_ids = new_ids
request.num_prompt_tokens = len(new_ids)
request._all_token_ids.clear()
+10 -4
View File
@@ -20,14 +20,20 @@ enable_chunked_prefill: false
stages:
# Stage 0: Qwen2.5-Omni thinker as a frozen text encoder.
# Prefill only — one sampled token, discarded downstream. eager because the
# fused-hidden forward walks the decoder layers itself and so bypasses the
# compiled graph.
# Prefill only: one sampled token, discarded downstream. The fused-hidden
# forward walks the decoder layers itself, so torch.compile has nothing to
# hook (mode 0); the whole prefill is instead captured as FULL CUDA graphs
# padded to the sizes below, which removes most of its launch overhead
# (about 20 ms per request on H200). Longer prompts run eager.
- stage_id: 0
devices: "0"
max_num_seqs: 1
gpu_memory_utilization: 0.45
enforce_eager: true
enforce_eager: false
compilation_config:
mode: 0
cudagraph_mode: FULL
cudagraph_capture_sizes: [64, 128, 192, 256, 384, 512]
max_num_batched_tokens: 32768
# AuK takes text plus at most one clip; no image or video tower is built.
limit_mm_per_prompt: {audio: 1}
@@ -0,0 +1,24 @@
# Opt-in single-Hopper streaming throughput profile: AR32 / codec32, Flow10.
# Use --hf-overrides '{"cosyvoice3_sampling_mode": "standard"}' and stream=true.
# F0 and HiFT stay FP32; only deterministic HiFT decode is graphed.
# HiFT graphs capture on the third use of each exact shape, adding latency then.
# The codec graph pool is not capped by gpu_memory_utilization; leave memory headroom.
base_config: cosyvoice3_packed_streaming.yaml
stages:
- stage_id: 0
max_num_seqs: 32
default_sampling_params:
max_tokens: 2048
repetition_penalty: 1.21
temperature: 0.7
top_k: 20
top_p: 0.8
- stage_id: 1
max_num_seqs: 32
env:
COSYVOICE3_TRT: "0"
COSYVOICE3_BATCH_FLOW: "1"
COSYVOICE3_FULL_RESPONSE_OPTIMIZATIONS: "0"
COSYVOICE3_PACKED_STREAMING: "1"
COSYVOICE3_CACHED_ISTFT: "1"
COSYVOICE3_HIFT_GRAPH: "1"
+3
View File
@@ -18,6 +18,9 @@ connectors:
connector_get_max_wait: 300
initial_codec_chunk_frames: 1 # 1 frame * 80 ms/frame = 80 ms first audio chunk
codec_chunk_frames: 15 # 15 frames * 80 ms/frame = 1.2 s steady chunks
# Optional startup ramp; its first entry overrides initial chunk size.
# Extra graph shapes/calls trade codec cost and latency for less underrun.
# codec_chunk_ramp: [1, 4, 15]
codec_left_context_frames: 0
# Chunk ramp (opt-in): gradual chunk-size ladder replacing the
# initial_codec_chunk_frames -> codec_chunk_frames cliff. The last
+30
View File
@@ -0,0 +1,30 @@
# Opt-in CUDA MRV2 for MOSS-TTS-Local-Transformer-v1.5.
# Keep the base profile's batching, chunk geometry and V1 default.
base_config: moss_tts_local.yaml
model_runner: v2
platforms:
cuda:
stages:
- stage_id: 0
# Bound prefill work so new prompts do not monopolize streaming decode.
max_num_batched_tokens: 512
- stage_id: 1
compilation_config:
# The codec owns CUDA Graph capture/replay. Skip Inductor compilation
# of its stateful decoder; plain CUDA graphs remain enabled.
mode: 0
# FULL works without Inductor; the default FULL_AND_PIECEWISE would
# normalize to NONE and erase the codec's capture sizes.
cudagraph_mode: FULL
# Platform overrides replace compilation_config as a whole.
cudagraph_capture_sizes: [1, 2, 4, 8, 16, 32, 64]
cudagraph_num_of_warmups: 1
npu:
model_runner: v1
xpu:
model_runner: v1
rocm:
model_runner: v1
musa:
model_runner: v1
@@ -0,0 +1,53 @@
# Opt-in CUDA profile for MOSS-TTS-Local-Transformer-v1.5 on a large-memory GPU.
# Both stages share GPU 0. Validated with one H200 and Seed-TTS EN1088.
base_config: moss_tts_local_mrv2.yaml
connectors:
shm:
extra:
# Coalesce ready codec streams before submitting a small execution batch.
# This is a dispatch target, not a cap: execution capacity remains 128.
generation_min_batch_size: 32
generation_max_wait_ms: 12
# Optional; preserves the first/steady sizes while adding a middle chunk.
# codec_chunk_ramp: [1, 4, 15]
# Optional codec attention-ring headroom (see recipes/OpenMOSS/MOSS-TTS.md):
# sizes each ring as context + one chunk so chunked streaming matches
# whole-sequence decoding. Requires additional state/graph memory; evaluate
# throughput and audio quality before enabling for your context and capacity.
# Add codec_ring_headroom: 1 under connectors.shm.extra above.
platforms:
cuda:
stages:
- stage_id: 0
max_num_seqs: 128
max_num_batched_tokens: 512
hf_overrides:
mrv2_gpu_slot_state: true
# Throughput-oriented pair; measure first-packet latency for your load.
mrv2_batch_prefill: true
mrv2_direct_tokens: true
# Mixed prefill/decode fits the same bounded token budget as capture.
compilation_config:
mode: 3
cudagraph_mode: FULL
max_cudagraph_capture_size: 512
cudagraph_num_of_warmups: 1
# Leave room for the codec state pool and its batch-128 CUDA graphs.
kv_cache_memory_bytes: 34359738368 # 32 GiB
- stage_id: 1
max_num_seqs: 128
hf_overrides:
codec_attention_backend: triton_slot
compilation_config:
mode: 3
backend: inductor
# The codec owns capture/replay after Inductor compiles its decoder.
cudagraph_mode: FULL
cudagraph_capture_sizes: [1, 2, 4, 6, 8, 12, 16, 24, 32, 64, 128]
cudagraph_num_of_warmups: 1
inductor_compile_config:
combo_kernels: false
benchmark_combo_kernel: false
@@ -0,0 +1,66 @@
# Opt-in CUDA profile for MOSS-TTS-Local-Transformer-v1.5 tuned for first-packet
# latency at client concurrency 128 on one H200 (Seed-TTS EN1088, closed loop).
# Both stages share GPU 0. Use moss_tts_local_mrv2_high_concurrency.yaml for
# throughput-first serving.
#
# Optional launch-level settings (see the full command in recipes/OpenMOSS/MOSS-TTS.md):
# - CUDA MPS for the GPU (stage 0 below lowers its own MPS client priority);
# - VLLM_OMNI_EVENT_DRIVEN_ORCH=1;
# - --api-server-count 4 with VLLM_WORKER_MULTIPROC_METHOD=spawn and
# VLLM_OMNI_MOSS_REF_CODES_SHARED_DIR=/dev/shm/<dir> so frontends share reference codes;
# - VLLM_OMNI_CONNECTOR_RECV_POLL_MS=1 so Stage 1 picks up first chunks sooner.
base_config: moss_tts_local_mrv2.yaml
connectors:
shm:
extra:
generation_min_batch_size: 32
generation_max_wait_ms: 12
# Stage 1 decodes each first chunk from its receive thread on a small
# dedicated graph set and emits it directly, so first chunks no longer
# wait for the engine loop or a regular codec step.
codec_first_chunk_fast_path: 1
# Regular codec replays wait for an in-flight first-chunk decode.
codec_first_chunk_gate: 1
# Short regular codec steps bound contention with first-chunk decodes.
generation_max_regular_batch: 16
# Optional ramp. Both the first-chunk graph and regular graphs follow it.
# codec_chunk_ramp: [1, 4, 15]
platforms:
cuda:
stages:
- stage_id: 0
max_num_seqs: 128
# Larger prefill budget admits bursts of new requests in fewer steps.
max_num_batched_tokens: 2048
hf_overrides:
mrv2_gpu_slot_state: true
mrv2_batch_prefill: true
mrv2_direct_tokens: true
# Sample each frame right after the forward that produced it.
mrv2_eager_mtp: true
compilation_config:
mode: 3
cudagraph_mode: FULL
max_cudagraph_capture_size: 2048
cudagraph_num_of_warmups: 1
kv_cache_memory_bytes: 34359738368 # 32 GiB
env:
# Under MPS, let codec kernels schedule ahead of the talker's.
CUDA_MPS_CLIENT_PRIORITY: "1"
- stage_id: 1
max_num_seqs: 128
# Keep synchronous scheduling; benchmark async scheduling separately.
async_scheduling: false
hf_overrides:
codec_attention_backend: triton_slot
compilation_config:
mode: 3
backend: inductor
cudagraph_mode: FULL
cudagraph_capture_sizes: [1, 2, 4, 6, 8, 12, 16, 24, 32, 64, 128]
cudagraph_num_of_warmups: 1
inductor_compile_config:
combo_kernels: false
benchmark_combo_kernel: false
+50
View File
@@ -0,0 +1,50 @@
# Opt-in CUDA Model Runner V2 profile for Qwen3-Omni-MoE: the Thinker (stage 0),
# Talker (stage 1) and Code2Wav (stage 2) run on MRv2 with its native data
# plane. The base profile keeps every stage on V1.
#
# On MRv2 the Thinker hands each generated token's embedding to the Talker with
# the step that sampled it (one Thinker step earlier than its next forward would
# capture the same embedding) and replays decode in FULL CUDA graphs together
# with the layers it captures for the Talker.
#
# When the stages share one GPU, run them under NVIDIA MPS (see
# docs/configuration/stage_configs.md); startup warns when no MPS server is found.
#
# MRv2 downstream stages support turn-based sessions only: realtime (resumable)
# requests and duplex sessions need the V1 chunk adapter.
base_config: qwen3_omni_moe.yaml
connectors:
connector_of_shared_memory:
extra:
# First Code2Wav chunk is the stream's first frame alone, then chunks
# double up to the steady codec_chunk_frames (replaces the 4-frame
# initial chunk). Code2Wav re-decodes up to codec_left_context_frames
# before each chunk, so windows are 1, 3, 7, 15, 31, then 50 frames.
# A one-frame first chunk is also what lets the Talker deliver it
# itself. Opt in explicitly; VLLM_OMNI_TALKER_FIRST_AUDIO=0 disables it.
talker_first_audio: true
codec_chunk_ramp: [1, 2, 4, 8, 16, 25]
# Code2Wav decodes a streaming step as a few length groups on multi-row
# CUDA graphs instead of one eager call padded to its longest window
# (with the ramp, over half of those padded frames were waste).
codec_graph_batch_sizes: [2, 3, 4, 6, 8, 12, 16]
codec_fused_snake: true
stages:
- stage_id: 0
model_runner: v2
- stage_id: 1
model_runner: v2
- stage_id: 2
model_runner: v2
platforms:
npu:
model_runner: v1
xpu:
model_runner: v1
rocm:
model_runner: v1
musa:
model_runner: v1
@@ -0,0 +1,27 @@
# Opt-in single-H200 MRv2 profile, measured at client concurrency 32/64.
# Run all stages under NVIDIA MPS and warm representative shapes first.
# Retains Triton MoE and native nucleus reductions, fusing only the final
# sampling mask/Gumbel selection. cuDNN searches convolution algorithms
# during warmup; short-KV attention and convolution selection change rounding.
# This is not a claim of bitwise audio or universal P99/throughput improvement.
# Evidence: docs/design/qwen3_omni_mrv2_performance.md
base_config: qwen3_omni_moe_mrv2.yaml
connectors:
connector_of_shared_memory:
extra:
code_predictor_kv_cache: true
code_predictor_short_kv: true
code_predictor_fused_sampling: true
codec_cudnn_benchmark: true
stages:
- stage_id: 0
devices: "0"
gpu_memory_utilization: 0.60
- stage_id: 1
devices: "0"
gpu_memory_utilization: 0.22
- stage_id: 2
devices: "0"
gpu_memory_utilization: 0.10
@@ -0,0 +1,54 @@
# Single-stage Qwen3-TTS: the Talker decodes every frame with the stateful
# streaming codec decoder and returns PCM itself (no Code2Wav stage).
# reset_prefix_cache(reset_running_requests=True) returns False while requests
# are running; wait for completion or abort them before resetting the cache.
pipeline: qwen3_tts_fused
model_runner: v2
async_chunk: true
stages:
- stage_id: 0
devices: "0"
max_num_seqs: 128
# Exact conditioned Talker inputs for preemption replay are allocated
# after KV profiling: 16 MiB per active request at 1.7B/max_model_len=4096
# (~2 GiB at 128 requests), in addition to codec state and output buffers.
# Keep headroom when increasing gpu_memory_utilization or capacity.
gpu_memory_utilization: 0.5
trust_remote_code: true
enable_prefix_caching: false
# Model options that multi-stage deployments put on the connector: a
# single stage has no connector edge.
additional_config:
talker_stream_decode: true
# Opt in to first-frame delivery on its own queue for lower first-audio
# latency; leave disabled when prioritizing peak throughput.
talker_stream_first_audio: false
# Match the standard two-stage profile's voice-clone context boundary.
# A shorter context (e.g. 25) can change timbre continuity.
ref_code_context_frames: 72
code_predictor_kv_cache: true
code_predictor_fused_sampling: true
code_predictor_fused: true
# Talker decode contexts are short (~100-700 tokens): FA3 split-KV plus
# its combine kernel costs more than one pass per head.
attention_config:
flash_attn_max_num_splits_for_cuda_graph: 1
async_scheduling: true
max_num_batched_tokens: 512
max_model_len: 4096
compilation_config:
cudagraph_mode: FULL
cudagraph_capture_sizes: [1, 2, 4, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400, 416, 432, 448, 464, 480, 496, 512]
max_cudagraph_capture_size: 512
default_sampling_params:
temperature: 0.9
top_k: 50
max_tokens: 4096
min_tokens: 2
repetition_penalty: 1.05
subtalker_sampling_params:
do_sample: true
temperature: 0.9
top_k: 50
top_p: 1.0
+59
View File
@@ -0,0 +1,59 @@
# YuE2-3B deploy: single-stage native-AR text-to-music (~3.6B bf16 + VAE).
# Four-slot CUDA configuration; validate memory on the target card/workload.
#
# The architectures pin is required: the checkpoint's config.json names
# "YuE2ForCausalLM" with an auto_map that only makes sense under
# trust_remote_code, so the deploy pins vLLM to this package's model class.
# model_type stays "yue2" so the registered Yue2Config is used; no
# trust_remote_code is needed (the registered config/model win over the
# checkpoint's auto_map).
pipeline: yue2
async_chunk: false
dtype: bfloat16
stages:
- stage_id: 0
# A finishing song keeps its slot during side-stream synthesis,
# emitting HOLD tokens until its audio is ready.
max_num_seqs: 4
# Reserve synthesis headroom: NAR K/V, acoustic graph buffers and VAE
# activations are allocated beyond the engine budget. The earlier 4090
# memory measurement predates async/compiled synthesis; 0.70 is a starting
# point, not a guarantee for 9000 frames or long ABC prefixes. Lower it if
# the target workload needs more synthesis memory (see the model recipe).
gpu_memory_utilization: 0.70
# Keep AR capture off by default. Sampler/NAR graphs still need
# separate synthesis headroom.
enforce_eager: true
# Prefix caching stays off: the omni prefix-cache save snapshots the
# step's mm payload before the model-owned sampler runs, but YuE2
# appends the decoded waveform inside sample() (the terminal NAR/VAE
# pass), so the cached payload would ship empty audio. Re-enable once
# the runner saves post-sample.
enable_prefix_caching: false
max_num_batched_tokens: 24576
max_model_len: 24576
devices: "0"
# The checkpoint ships no HF tokenizer files (only qwen.tiktoken, which
# the offline driver reads itself); every prompt arrives as token ids,
# so the engine builds no tokenizer. Placeholder tokenizer dir follows
# the indextts2 precedent.
skip_tokenizer_init: true
tokenizer: gpt2
hf_overrides:
architectures: ["Yue2ForCausalLM"]
default_sampling_params:
# Defaults for callers that omit model-owned sampling options. The
# opt-in prepare_runner_inputs hook reads each request's extra_args;
# it does not depend on the forward's has_sampling_extra_args flag.
extra_args:
yue2_phase: "semantic"
yue2_seed: 831001
yue2_max_audio_frames: 200
yue2_max_hold_steps: 4096
detokenize: false
stop_token_ids: [151848, 151852] # ABC_END, MUSIC_END
# 200 frames + 4096 synthesis holds + the final end token. Callers
# changing the frame budget must also reserve this token headroom.
max_tokens: 4297
temperature: 1.0
@@ -7,6 +7,10 @@ from typing import Any, Generic, Literal, TypeVar
import torch
from vllm_omni.diffusion.attention.capabilities import (
ExecutionContext,
ExecutionPathResult,
)
from vllm_omni.platforms import current_omni_platform
@@ -132,6 +136,15 @@ class AttentionBackend(ABC):
return False
@classmethod
def resolve_capabilities(cls, context: ExecutionContext) -> ExecutionPathResult:
"""Return conservative pre-construction capabilities.
Unmigrated backends remain advisory so introducing the contract cannot
reject an execution path that works today.
"""
return ExecutionPathResult.unmigrated(cls.get_name(), context)
@dataclass(frozen=True, slots=True)
class QueryRange:
@@ -218,6 +231,9 @@ class AttentionMetadata:
# "valid_kv_length": int — contiguous valid K/V prefix length. Local
# backends may slice it directly; ring backends trim each circulated
# K/V block from the same global prefix.
# "attention_mask_mode": "none" | "padding" | "arbitrary" — producer
# assertion used by capability planning. "none" means a present mask
# is semantically a no-op; absent means its contents are runtime-dependent.
# "npu_attn_varlen": bool — model opt-in for the NPU packed varlen path
# (TND npu_fusion_attention driven by cu_seqlens, mask never read).
# Requires the [real, pad] two-document packing contract; see
@@ -277,6 +293,21 @@ class AttentionImpl(ABC, Generic[T]):
return True
return kv_cache_dtype in cls._supported_kv_cache_dtypes.get(platform_key, set())
def resolve_execution_path(
self,
context: ExecutionContext,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
attn_metadata: T | None,
) -> ExecutionPathResult:
"""Resolve the complete path after backend initialization.
Subclasses report verified results; this inspection API does not enforce them.
"""
del query, key, value, attn_metadata
return ExecutionPathResult.unmigrated(type(self).__name__, context)
def forward(
self,
query: torch.Tensor,
@@ -3,7 +3,9 @@
import os
from collections.abc import Callable
from dataclasses import replace
from functools import cache, partial
from typing import NamedTuple
import torch
from vllm.logger import init_logger
@@ -14,6 +16,16 @@ from vllm_omni.diffusion.attention.backends.utils.piecewise_attn import (
piecewise_attn,
run_paged_piecewise_plan,
)
from vllm_omni.diffusion.attention.capabilities import (
CapabilityResult,
CompilationMode,
ExecutionContext,
ExecutionPathResult,
MaskMode,
PackingMode,
ParallelStrategy,
SupportStatus,
)
from vllm_omni.diffusion.config import get_current_diffusion_config_or_none
from vllm_omni.platforms import current_omni_platform
@@ -29,6 +41,133 @@ def _get_npu_compressed_causal_mask(device: torch.device) -> torch.Tensor:
).contiguous()
if not hasattr(torch.ops.vllm_omni, "fa4_dense_attention"):
@torch.library.custom_op("vllm_omni::fa4_dense_attention", mutates_args=())
def _fa4_dense_attention_op(
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
softmax_scale: float,
causal: bool,
deterministic: bool,
) -> torch.Tensor:
from vllm_omni.diffusion.attention.backends.utils.fa import flash_attn_func
if flash_attn_func is None:
raise RuntimeError("CuTe FlashAttention-4 is unavailable")
kwargs = {
"causal": causal,
"softmax_scale": softmax_scale,
}
if deterministic:
kwargs["deterministic"] = True
out = flash_attn_func(query, key, value, **kwargs)
return out[0] if isinstance(out, tuple) else out
@_fa4_dense_attention_op.register_fake
def _fa4_dense_attention_fake(
query,
key,
value,
softmax_scale,
causal,
deterministic,
):
# FA4 returns contiguous output even when Q is noncontiguous; V sets its head dimension.
return query.new_empty((*query.shape[:-1], value.shape[-1]))
_fa4_dense_attention_op = torch.ops.vllm_omni.fa4_dense_attention
_PACKED_KEYS = ("cu_seqlens_q", "cu_seqlens_k", "max_seqlen_q", "max_seqlen_k")
class _FlashAttentionMetadataPlan(NamedTuple):
attention_mask: torch.Tensor | None
mask_mode: MaskMode
full_attn_spans: list[list[tuple[int, int]]] | None
extra: dict
packing_mode: PackingMode
def _normalize_flash_attention_metadata(
attn_metadata: AttentionMetadata | None,
) -> _FlashAttentionMetadataPlan:
attention_mask = attn_metadata.attn_mask if attn_metadata is not None else None
full_attn_spans = attn_metadata.full_attn_spans if attn_metadata is not None else None
extra = attn_metadata.extra if attn_metadata is not None else {}
published_mask_mode = extra.get("attention_mask_mode")
if attention_mask is None:
mask_mode = MaskMode.NONE
elif published_mask_mode is None:
mask_mode = MaskMode.UNKNOWN
else:
try:
mask_mode = MaskMode(published_mask_mode)
except ValueError as error:
raise ValueError(f"Unknown attention_mask_mode {published_mask_mode!r}") from error
if mask_mode is MaskMode.UNKNOWN:
raise ValueError("attention_mask_mode='unknown' is reserved for unpublished semantics")
# Piecewise dispatch takes precedence and does not consume packed metadata.
present_packed_keys = [key for key in _PACKED_KEYS if key in extra] if full_attn_spans is None else []
if present_packed_keys and len(present_packed_keys) != len(_PACKED_KEYS):
missing = sorted(set(_PACKED_KEYS) - set(present_packed_keys))
raise ValueError(f"Incomplete packed FlashAttention metadata; missing {missing}")
packing_mode = PackingMode.NONE
if present_packed_keys:
cu_seqlens_q = extra["cu_seqlens_q"]
packing_mode = PackingMode.MULTI_DOCUMENT if cu_seqlens_q.shape[0] > 3 else PackingMode.PACKED_PADDING
return _FlashAttentionMetadataPlan(
attention_mask=attention_mask,
mask_mode=mask_mode,
full_attn_spans=full_attn_spans,
extra=extra,
packing_mode=packing_mode,
)
def _flash_attention_execution_path(
context: ExecutionContext,
) -> ExecutionPathResult:
if context.mask_mode is MaskMode.UNKNOWN:
return ExecutionPathResult.unmigrated(
"FLASH_ATTN",
context,
path="runtime_mask_dependent",
)
if not (
context.platform == "cuda"
and context.kernel_variant == "fa4"
and context.dtype in {"bfloat16", "torch.bfloat16"}
and context.causal is False
and context.mask_mode is MaskMode.NONE
and context.packing_mode is PackingMode.NONE
and not context.piecewise
and not context.paged_kv
and context.kv_cache_dtype is None
and context.parallel_strategy is ParallelStrategy.NONE
and not context.outer_boundaries
):
return ExecutionPathResult.unmigrated(
"FLASH_ATTN",
context,
path="unverified",
)
return ExecutionPathResult(
backend="FLASH_ATTN",
path="fa4_dense",
support=CapabilityResult.supported(),
compilation_mode=CompilationMode.CUSTOM_OP,
platform=context.platform,
kernel_variant=context.kernel_variant,
parallel_strategy=context.parallel_strategy,
)
class FlashAttentionBackend(AttentionBackend):
accept_output_buffer: bool = True
supports_piecewise_spans: bool = True
@@ -74,6 +213,10 @@ class FlashAttentionBackend(AttentionBackend):
def get_impl_cls() -> type["FlashAttentionImpl"]:
return FlashAttentionImpl
@classmethod
def resolve_capabilities(cls, context: ExecutionContext) -> ExecutionPathResult:
return _flash_attention_execution_path(replace(context, kernel_variant=None))
class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
# Per-platform attention quantization support.
@@ -108,11 +251,135 @@ class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
self.softmax_scale = softmax_scale
self.qkv_layout = qkv_layout
self.is_cross_attn = role == "cross"
from vllm_omni.diffusion.attention.backends.utils.fa import IS_AITER, IS_FLASH_ATTN_4
self._kernel_variant = "fa4" if IS_FLASH_ATTN_4 else "aiter" if IS_AITER else None
cfg = get_current_diffusion_config_or_none()
self.fa_deterministic = bool(getattr(cfg, "fa_deterministic", False)) if cfg is not None else False
if backend_kwargs:
logger.warning("FlashAttentionImpl ignoring backend_kwargs: %s", list(backend_kwargs.keys()))
def resolve_execution_path(
self,
context: ExecutionContext,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
attn_metadata: AttentionMetadata | None,
) -> ExecutionPathResult:
if context.platform == "npu":
return self._resolve_npu_execution_path(context, query, key, attn_metadata)
metadata = _normalize_flash_attention_metadata(attn_metadata)
context = replace(
context,
kernel_variant=self._kernel_variant,
dtype=str(query.dtype).removeprefix("torch."),
causal=self.causal,
mask_mode=metadata.mask_mode,
packing_mode=metadata.packing_mode,
piecewise=metadata.full_attn_spans is not None,
kv_cache_dtype=metadata.extra.get("kv_cache_dtype"),
)
if context.platform == "rocm":
return self._resolve_rocm_execution_path(context)
result = _flash_attention_execution_path(context)
if result.support.status is not SupportStatus.SUPPORTED:
return result
from vllm_omni.diffusion.attention.backends.utils.fa import validate_fa4_head_dims
try:
if query.dtype != key.dtype or query.dtype != value.dtype:
raise ValueError("Q, K, and V dtypes must match")
if query.device != key.device or query.device != value.device:
raise ValueError("Q, K, and V devices must match")
if query.shape[-1] != key.shape[-1]:
raise ValueError("Q and K head dimensions must match")
verified = validate_fa4_head_dims(query.shape[-1], value.shape[-1], 16 // value.element_size())
except (AssertionError, ValueError) as error:
return replace(
result,
support=CapabilityResult.unsupported(f"FA4: {error} Select compatible inputs or another backend."),
)
if not verified:
return replace(
result,
support=CapabilityResult.unmigrated("Selected FA4 kernel has no available head-dimension validator"),
compilation_mode=CompilationMode.EAGER_ONLY,
)
return result
@staticmethod
def _resolve_rocm_execution_path(context: ExecutionContext) -> ExecutionPathResult:
"""Describe AITER dispatch; device and compiler validation is pending."""
result = ExecutionPathResult.unmigrated("FLASH_ATTN", context, path="rocm_unverified")
if (
context.kernel_variant != "aiter"
or context.parallel_strategy is not ParallelStrategy.NONE
or context.outer_boundaries
or context.paged_kv
or context.piecewise
or context.kv_cache_dtype is not None
):
return result
if context.packing_mode is not PackingMode.NONE:
path = "rocm_packed_varlen"
elif context.mask_mode is MaskMode.UNKNOWN:
path = "rocm_runtime_mask_dependent"
elif context.mask_mode is not MaskMode.NONE:
path = "rocm_masked_varlen"
else:
path = "rocm_dense"
return replace(
result,
path=path,
support=CapabilityResult.unmigrated("AITER routing only; kernel and compilation validation require ROCm"),
)
def _resolve_npu_execution_path(
self,
context: ExecutionContext,
query: torch.Tensor,
key: torch.Tensor,
attn_metadata: AttentionMetadata | None,
) -> ExecutionPathResult:
"""Describe existing MindIE routing without claiming hardware validation.
NPU packing has its own opt-in and [real, pad] contract. In particular,
incomplete packed metadata can fall back to a mask, so CUDA metadata
normalization must not run here. No tensor values are inspected.
"""
context = replace(context, kernel_variant="mindiesd")
result = ExecutionPathResult.unmigrated("FLASH_ATTN", context, path="npu_unverified")
extra = attn_metadata.extra if attn_metadata else {}
if (
context.parallel_strategy is not ParallelStrategy.NONE
or context.outer_boundaries
or context.paged_kv
or self.causal
or extra.get("kv_cache_dtype") not in (None, "float", "auto")
or (attn_metadata is not None and attn_metadata.full_attn_spans is not None)
):
return result
path = "npu_masked" if attn_metadata is not None and attn_metadata.attn_mask is not None else "npu_dense"
if extra.get("npu_attn_varlen", False):
if self._resolve_packed_seq_npu(query, key, extra) is not None:
path = (
"npu_prefix_kv_slice"
if os.environ.get("MINDIE_SD_FA_TYPE") == "ascend_laser_attention"
else "npu_packed_varlen"
)
else:
# This route may reject the request if neither an explicit mask
# nor usable valid_kv_length is supplied; it is not support.
path = "npu_masked_fallback"
return replace(
result,
path=path,
support=CapabilityResult.unmigrated("NPU routing only; kernel and compilation validation require Ascend"),
)
def _warn_fa_deterministic_non_dense(self, path: str) -> None:
if not self.fa_deterministic:
return
@@ -401,6 +668,7 @@ class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
"""CUDA/ROCm/MUSA flash attention implementation."""
from vllm_omni.diffusion.attention.backends.utils.fa import (
HAS_FLASH_ATTN,
IS_FLASH_ATTN_4,
flash_attn_func,
flash_attn_varlen_func,
)
@@ -412,9 +680,10 @@ class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
"Otherwise, use SDPA backend by setting DIFFUSION_ATTENTION_BACKEND=TORCH_SDPA"
)
attention_mask = attn_metadata.attn_mask if attn_metadata is not None else None
full_attn_spans = attn_metadata.full_attn_spans if attn_metadata is not None else None
extra = attn_metadata.extra if attn_metadata is not None else {}
metadata_plan = _normalize_flash_attention_metadata(attn_metadata)
attention_mask = metadata_plan.attention_mask
full_attn_spans = metadata_plan.full_attn_spans
extra = metadata_plan.extra
# Try piecewise attention
if full_attn_spans is not None:
@@ -443,12 +712,7 @@ class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
query_ranges=None if attn_metadata is None else attn_metadata.query_ranges,
)
packed_keys = ("cu_seqlens_q", "cu_seqlens_k", "max_seqlen_q", "max_seqlen_k")
present_packed_keys = [key for key in packed_keys if key in extra]
if present_packed_keys:
if len(present_packed_keys) != len(packed_keys):
missing = sorted(set(packed_keys) - set(present_packed_keys))
raise ValueError(f"Incomplete packed FlashAttention metadata; missing {missing}")
if metadata_plan.packing_mode is not PackingMode.NONE:
self._warn_fa_deterministic_non_dense("packed-varlen")
return self._forward_varlen_packed(
query,
@@ -460,7 +724,14 @@ class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
max_seqlen_k=extra["max_seqlen_k"],
)
if attention_mask is not None and torch.any(~attention_mask):
use_masked_path = False
if attention_mask is not None:
use_masked_path = (
torch.any(~attention_mask)
if metadata_plan.mask_mode is MaskMode.UNKNOWN
else metadata_plan.mask_mode is not MaskMode.NONE
)
if use_masked_path:
self._warn_fa_deterministic_non_dense("masked-varlen")
return self._forward_varlen_masked(
query,
@@ -470,6 +741,15 @@ class FlashAttentionImpl(AttentionImpl[AttentionMetadata]):
)
if flash_attn_func is not None:
if IS_FLASH_ATTN_4:
return _fa4_dense_attention_op(
query,
key,
value,
self.softmax_scale,
self.causal,
self.fa_deterministic,
)
fa_kwargs = {
"causal": self.causal,
"softmax_scale": self.softmax_scale,
@@ -59,6 +59,8 @@ def is_flash_attn_4_available() -> bool:
# Bind via aliases so mypy does not treat each candidate import as a redefinition.
flash_attn_func: FlashAttnFn | None = None
flash_attn_varlen_func: FlashAttnFn | None = None
IS_FLASH_ATTN_4 = False
IS_AITER = False
if current_omni_platform.is_rocm():
# ROCm: try Aiter first
@@ -71,6 +73,7 @@ if current_omni_platform.is_rocm():
flash_attn_func = _fa_func
flash_attn_varlen_func = _fa_varlen
IS_AITER = True
except (ImportError, ModuleNotFoundError):
pass
elif current_omni_platform.is_xpu():
@@ -102,6 +105,7 @@ else:
flash_attn_func = _fa_func
flash_attn_varlen_func = _fa_varlen
IS_FLASH_ATTN_4 = True
logger.info("Using CuTe FlashAttention-4 on Blackwell")
except Exception as exc:
# Optional FA4 dependencies may be present but ABI-incompatible
@@ -166,6 +170,27 @@ else:
HAS_FLASH_ATTN = flash_attn_func is not None or flash_attn_varlen_func is not None
def validate_fa4_head_dims(head_dim: int, head_dim_v: int, alignment: int) -> bool:
"""Use the selected FA4 implementation's constraints during path resolution.
Return False when its validator is unavailable; propagate kernel validation
errors for the backend to report. Keep the private FA4 API dependency here,
rather than duplicating architecture/version-specific dimension rules.
This is a planning-time call, never part of compiled tensor execution.
"""
try:
from flash_attn.cute.interface import _get_device_arch, _validate_head_dims
except ImportError:
return False
arch = _get_device_arch() // 10
# FA4 routes SM80/SM120 through separate kernels and bypasses this validator.
if arch not in (9, 10, 11):
return False
_validate_head_dims(head_dim, head_dim_v, arch, alignment)
return True
def _choose_vllm_flash_attn_version(
device_major: int,
requested: str | int | None,
@@ -0,0 +1,126 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
class SupportStatus(str, Enum):
SUPPORTED = "supported"
UNSUPPORTED = "unsupported"
UNMIGRATED = "unmigrated"
class CompilationMode(str, Enum):
TRACEABLE = "traceable"
CUSTOM_OP = "custom_op"
EAGER_ONLY = "eager_only"
class PackingMode(str, Enum):
NONE = "none"
PACKED_PADDING = "packed_padding"
MULTI_DOCUMENT = "multi_document"
class MaskMode(str, Enum):
NONE = "none"
PADDING = "padding"
ARBITRARY = "arbitrary"
UNKNOWN = "unknown"
class ParallelStrategy(str, Enum):
NONE = "none"
ULYSSES = "ulysses"
RING = "ring"
HYBRID_ULYSSES_RING = "hybrid_ulysses_ring"
ALLGATHER_KV = "allgather_kv"
class OuterBoundary(str, Enum):
HSDP = "hsdp"
@dataclass(frozen=True, slots=True)
class CapabilityResult:
status: SupportStatus
reason: str | None = None
def __post_init__(self) -> None:
if self.status is SupportStatus.UNSUPPORTED and not self.reason:
raise ValueError("UNSUPPORTED capability results require an actionable reason")
@classmethod
def supported(cls) -> CapabilityResult:
return cls(SupportStatus.SUPPORTED)
@classmethod
def unsupported(cls, reason: str) -> CapabilityResult:
return cls(SupportStatus.UNSUPPORTED, reason)
@classmethod
def unmigrated(cls, reason: str | None = None) -> CapabilityResult:
return cls(SupportStatus.UNMIGRATED, reason)
@dataclass(frozen=True, slots=True)
class ExecutionContext:
platform: str
kernel_variant: str | None = None
dtype: str | None = None
causal: bool | None = None
mask_mode: MaskMode = MaskMode.NONE
packing_mode: PackingMode = PackingMode.NONE
piecewise: bool = False
paged_kv: bool = False
kv_cache_dtype: str | None = None
parallel_strategy: ParallelStrategy = ParallelStrategy.NONE
require_fullgraph: bool = False
outer_boundaries: frozenset[OuterBoundary] = frozenset()
@dataclass(frozen=True, slots=True)
class ExecutionPathResult:
backend: str
path: str
support: CapabilityResult
compilation_mode: CompilationMode
platform: str
kernel_variant: str | None
parallel_strategy: ParallelStrategy
@classmethod
def unmigrated(
cls,
backend: str,
context: ExecutionContext,
*,
path: str = "unmigrated",
) -> ExecutionPathResult:
return cls(
backend=backend,
path=path,
support=CapabilityResult.unmigrated(),
compilation_mode=CompilationMode.EAGER_ONLY,
platform=context.platform,
kernel_variant=context.kernel_variant,
parallel_strategy=context.parallel_strategy,
)
def requested_support(self, context: ExecutionContext) -> CapabilityResult:
"""Validate request-level guarantees without changing path migration state."""
if context.require_fullgraph and self.support.status is SupportStatus.UNMIGRATED:
return CapabilityResult.unsupported(
f"{self.backend} path {self.path!r} has no verified fullgraph declaration; "
"use a verified path or disable the guaranteed-fullgraph request"
)
if self.support.status is not SupportStatus.SUPPORTED:
return self.support
if context.require_fullgraph and self.compilation_mode is CompilationMode.EAGER_ONLY:
return CapabilityResult.unsupported(
f"{self.backend} path {self.path!r} does not support guaranteed fullgraph compilation"
)
return CapabilityResult.supported()
+79 -14
View File
@@ -19,7 +19,13 @@ from vllm.model_executor.models.utils import extract_layer_index
from vllm.v1.kv_cache_interface import FullAttentionSpec, KVCacheSpec
from vllm_omni.diffusion.attention.backends.abstract import AttentionBackend, AttentionImpl, AttentionMetadata
from vllm_omni.diffusion.attention.backends.sdpa import SDPABackend
from vllm_omni.diffusion.attention.backends.sdpa import SDPABackend, SDPAImpl
from vllm_omni.diffusion.attention.capabilities import (
ExecutionContext,
ExecutionPathResult,
OuterBoundary,
ParallelStrategy,
)
from vllm_omni.diffusion.attention.parallel import build_parallel_attention_strategy
from vllm_omni.diffusion.attention.parallel.base import NoParallelAttention
from vllm_omni.diffusion.attention.parallel.ring import RingParallelAttention
@@ -119,6 +125,8 @@ class Attention(nn.Module):
config = get_current_diffusion_config_or_none()
attention_config = config.diffusion_attention_config if config is not None else None
parallel_config = getattr(config, "parallel_config", None)
self._hsdp_compile_boundary_enabled = bool(getattr(parallel_config, "use_hsdp", False))
from vllm_omni.diffusion.model_metadata import get_diffusion_model_metadata
@@ -131,6 +139,9 @@ class Attention(nn.Module):
is DiffusionKVCacheMode.PAGED_SCHEDULER
)
self._scheduler_paged_kv = scheduler_paged_kv
self.attn_backend: type[AttentionBackend] | None
self.attention: AttentionImpl | nn.Module
self.sdpa_fallback: SDPAImpl | None
if custom_attention is None:
attn_backend_cls, spec = get_attn_backend_for_role(
role=role,
@@ -166,7 +177,6 @@ class Attention(nn.Module):
attn_backend_cls.get_name(),
dense_backend_name,
)
parallel_config = getattr(config, "parallel_config", None)
allgather_degree = getattr(parallel_config, "allgather_degree", 1)
# TODO: Move AllGather-KV compatibility into an AttentionBackend capability
# so validation does not depend on backend names.
@@ -279,6 +289,7 @@ class Attention(nn.Module):
if self.paged_kv_cache_role is None:
return None
assert self.attn_backend is not None # Custom attention cannot opt into paged KV.
dtype = self.paged_kv_cache_dtype or vllm_config.model_config.dtype
# Keep backend layout discovery under the same config context used by
# upstream vLLM's attention-spec collector. vLLM 0.29 moved the
@@ -378,17 +389,68 @@ class Attention(nn.Module):
value: torch.Tensor,
attn_metadata: AttentionMetadata | None = None,
) -> torch.Tensor:
if torch.compiler.is_compiling() and is_forward_context_available():
od_config = get_forward_context().omni_diffusion_config
parallel_config = getattr(od_config, "parallel_config", None)
if getattr(parallel_config, "use_hsdp", False):
# Keep HSDP/FSDP2 parameter all-gather outside Inductor's
# attention graph; otherwise scheduler dependency analysis can
# fail on the fused attention region.
return self._forward_hsdp_compile_boundary(query, key, value, attn_metadata)
if torch.compiler.is_compiling() and self._uses_hsdp_compile_boundary():
# Keep HSDP/FSDP2 parameter all-gather outside Inductor's
# attention graph; otherwise scheduler dependency analysis can
# fail on the fused attention region.
return self._forward_hsdp_compile_boundary(query, key, value, attn_metadata)
return self._forward_impl(query, key, value, attn_metadata)
def _uses_hsdp_compile_boundary(self) -> bool:
if self._hsdp_compile_boundary_enabled:
return True
if not is_forward_context_available():
return False
od_config = get_forward_context().omni_diffusion_config
parallel_config = getattr(od_config, "parallel_config", None)
return bool(getattr(parallel_config, "use_hsdp", False))
def resolve_execution_path(
self,
context: ExecutionContext,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
attn_metadata: AttentionMetadata | None,
) -> ExecutionPathResult:
"""Compose backend capabilities with outer Attention boundaries."""
boundaries = set(context.outer_boundaries)
if self._uses_hsdp_compile_boundary():
boundaries.add(OuterBoundary.HSDP)
active_strategy = self._get_active_parallel_strategy()
strategy_name = active_strategy.name
if self.use_ring and active_strategy.enabled and strategy_name == "ulysses":
parallel_strategy = ParallelStrategy.HYBRID_ULYSSES_RING
elif self.use_ring and active_strategy.enabled:
parallel_strategy = ParallelStrategy.RING
else:
parallel_strategy = {
"allgather_kv": ParallelStrategy.ALLGATHER_KV,
"ulysses": ParallelStrategy.ULYSSES,
}.get(strategy_name, ParallelStrategy.NONE)
resolved_context = replace(
context,
outer_boundaries=frozenset(boundaries),
paged_kv=self.is_paged_kv_active(),
parallel_strategy=parallel_strategy,
)
resolver = getattr(self.attention, "resolve_execution_path", None)
if not callable(resolver):
return ExecutionPathResult.unmigrated(
type(self.attention).__name__,
resolved_context,
)
if not resolved_context.paged_kv:
attn_metadata = self._with_kv_cache_dtype(attn_metadata)
return resolver(
resolved_context,
query,
key,
value,
attn_metadata,
)
@torch.compiler.disable
def _forward_hsdp_compile_boundary(
self,
@@ -422,10 +484,11 @@ class Attention(nn.Module):
)
use_paged_attention = paged_adapter is not None and self.paged_kv_cache_role is not None
if use_paged_attention and not getattr(self.attn_backend, "supports_paged_kv", False):
assert self.attn_backend is not None
backend_name = (
self.attn_backend.get_name() if self.attn_backend is not None else type(self.attention).__name__
)
raise NotImplementedError(
f"Diffusion paged KV requires an Omni backend with paged support; "
f"selected {self.attn_backend.get_name()}"
f"Diffusion paged KV requires an Omni backend with paged support; selected {backend_name}"
)
if use_paged_attention and strategy is not self._no_parallel_strategy:
strategy_name = strategy.name
@@ -554,9 +617,11 @@ class Attention(nn.Module):
self._scheduler_paged_kv
and self.paged_kv_cache_role is not None
and in_kv_memory_profile
and cast(type[AttentionBackend], self.attn_backend).get_name() == "FLASH_ATTN"
and self.attn_backend is not None
and self.attn_backend.get_name() == "FLASH_ATTN"
and not current_omni_platform.supports_diffusion_dense_flash_attention()
):
assert self.sdpa_fallback is not None
logger.warning_once(
"The startup KV memory profile is using SDPA because dense FLASH_ATTN is unavailable. "
"Formal paged requests still use the platform-native paged attention backend."
+7
View File
@@ -983,6 +983,9 @@ class OmniDiffusionConfig:
enable_broadcast_weight_load: bool = False
num_weight_load_threads: int = 4
# Shard meta parameters before reading rank-local HF safetensors slices.
hsdp_weight_load_strategy: str = "full"
# Enable sleep mode
enable_sleep_mode: bool = False
@@ -1196,6 +1199,10 @@ class OmniDiffusionConfig:
)
def __post_init__(self):
if self.hsdp_weight_load_strategy not in {"full", "pre_sharded"}:
raise ValueError(
f"hsdp_weight_load_strategy must be 'full' or 'pre_sharded', got {self.hsdp_weight_load_strategy!r}"
)
from vllm_omni.diffusion.offloader.config import (
OffloadStrategy,
materialize_legacy_offload_flags,
+87 -29
View File
@@ -46,6 +46,14 @@ class HSDPInferenceConfig:
reshard_after_forward: bool = True
@dataclass
class HSDPShardContext:
"""Reusable state for incrementally sharding one HSDP model."""
hsdp_kwargs: dict[str, Any]
ignored_params: set[nn.Parameter] | None = None
def _create_hsdp_mesh(
device_type: str,
replicate_size: int,
@@ -111,6 +119,38 @@ def apply_hsdp_to_model(
Returns:
HSDP-wrapped model ready for inference
"""
context = prepare_hsdp_shard_context(
model,
hsdp_config,
target_device=target_device,
)
hsdp_shard_conditions = getattr(model, "_hsdp_shard_conditions", None)
if not hsdp_shard_conditions:
raise ValueError(f"Model {type(model).__name__} has no _hsdp_shard_conditions defined")
# Apply HSDP sharding, this will automatically handle weight distribution
shard_model(
model,
hsdp_shard_conditions=hsdp_shard_conditions,
context=context,
)
logger.info("HSDP applied to model: %s", type(model).__name__)
return model
def prepare_hsdp_shard_context(
model: nn.Module,
hsdp_config: HSDPInferenceConfig,
target_device: torch.device | None = None,
) -> HSDPShardContext:
"""Prepare mesh, precision, and ignored-parameter state for HSDP.
The returned context can be reused to shard completed child modules while
checkpoint weights are still streaming, then to shard the root once all
children have been loaded.
"""
if not hsdp_config.enabled:
raise ValueError("HSDP is not enabled in config")
@@ -160,7 +200,7 @@ def apply_hsdp_to_model(
)
hsdp_shard_conditions = getattr(model, "_hsdp_shard_conditions", None)
if not hsdp_shard_conditions or len(hsdp_shard_conditions) == 0:
if not hsdp_shard_conditions:
raise ValueError(f"Model {type(model).__name__} has no _hsdp_shard_conditions defined")
# Collect parameters of any modules the model wants excluded from FSDP sharding.
@@ -188,7 +228,11 @@ def apply_hsdp_to_model(
except AttributeError:
logger.warning("_hsdp_ignored_modules entry %r not found on model", mod_name)
continue
sub_mod.to(target_device)
has_meta_tensor = any(tensor.device.type == "meta" for tensor in (*sub_mod.parameters(), *sub_mod.buffers()))
if has_meta_tensor:
sub_mod.to_empty(device=target_device)
else:
sub_mod.to(target_device)
ignored_params.update(sub_mod.parameters())
if ignored_params:
logger.info(
@@ -224,21 +268,36 @@ def apply_hsdp_to_model(
target_device,
)
# Apply HSDP sharding, this will automatically handle weight distribution
shard_model(
model,
reshard_after_forward=hsdp_config.reshard_after_forward,
mp_policy=mp_policy,
mesh=device_mesh,
hsdp_shard_conditions=hsdp_shard_conditions,
ignored_params=ignored_params if ignored_params else None,
return HSDPShardContext(
hsdp_kwargs={
"reshard_after_forward": hsdp_config.reshard_after_forward,
"mesh": device_mesh,
"mp_policy": mp_policy,
},
ignored_params=ignored_params or None,
)
for param in model.parameters():
def shard_hsdp_module(module: nn.Module, context: HSDPShardContext) -> None:
"""Shard one completed child module with a prepared HSDP context."""
module_kwargs = dict(context.hsdp_kwargs)
if context.ignored_params:
module_ignored_params = context.ignored_params.intersection(module.parameters())
if module_ignored_params:
module_kwargs["ignored_params"] = module_ignored_params
fully_shard(module, **module_kwargs)
for param in module.parameters():
param.requires_grad = False
logger.info("HSDP applied to model: %s", type(model).__name__)
return model
def finalize_hsdp_root(model: nn.Module, context: HSDPShardContext) -> None:
"""Shard the model root after all selected child modules are sharded."""
root_kwargs = dict(context.hsdp_kwargs)
if context.ignored_params:
root_kwargs["ignored_params"] = context.ignored_params
fully_shard(model, **root_kwargs)
for param in model.parameters():
param.requires_grad = False
def shard_model(
@@ -249,6 +308,7 @@ def shard_model(
mesh: DeviceMesh | None = None,
hsdp_shard_conditions: list[Callable[[str, nn.Module], bool]],
ignored_params: set[nn.Parameter] | None = None,
context: HSDPShardContext | None = None,
) -> None:
"""Apply HSDP sharding to model modules based on shard conditions.
@@ -259,32 +319,30 @@ def shard_model(
This is required for packed integer parameters inside sharded transformer
blocks; the root wrap receives the full set for all remaining parameters.
"""
hsdp_kwargs: dict[str, Any] = {
"reshard_after_forward": reshard_after_forward,
"mesh": mesh,
"mp_policy": mp_policy,
}
if context is None:
context = HSDPShardContext(
hsdp_kwargs={
"reshard_after_forward": reshard_after_forward,
"mesh": mesh,
"mp_policy": mp_policy,
},
ignored_params=ignored_params,
)
elif any(value is not None for value in (mp_policy, mesh, ignored_params)) or not reshard_after_forward:
raise ValueError("Pass either context or individual HSDP shard options, not both")
num_sharded = 0
for name, module in reversed(list(model.named_modules())):
if any(cond(name, module) for cond in hsdp_shard_conditions):
module_kwargs = dict(hsdp_kwargs)
if ignored_params:
module_ignored_params = ignored_params.intersection(module.parameters())
if module_ignored_params:
module_kwargs["ignored_params"] = module_ignored_params
fully_shard(module, **module_kwargs)
shard_hsdp_module(module, context)
num_sharded += 1
if num_sharded == 0:
raise ValueError("No modules were sharded")
root_kwargs = dict(hsdp_kwargs)
if ignored_params:
root_kwargs["ignored_params"] = ignored_params
fully_shard(model, **root_kwargs)
finalize_hsdp_root(model, context)
logger.info(
"Sharded %d modules + root (ignored_params=%d)",
num_sharded,
len(ignored_params) if ignored_params else 0,
len(context.ignored_params) if context.ignored_params else 0,
)
@@ -16,6 +16,7 @@ import torch
from torch import nn
from vllm.config.load import LoadConfig
from vllm.logger import init_logger
from vllm.model_executor.layers.linear import UnquantizedLinearMethod
from vllm.model_executor.layers.quantization.base_config import QuantizeMethodBase
from vllm.model_executor.model_loader.weight_utils import (
download_weights_from_hf,
@@ -30,7 +31,14 @@ from vllm.utils.torch_utils import set_default_torch_dtype
from vllm_omni.diffusion.config import set_current_diffusion_config
from vllm_omni.diffusion.data import OmniDiffusionConfig
from vllm_omni.diffusion.distributed.hsdp import HSDPInferenceConfig, apply_hsdp_to_model
from vllm_omni.diffusion.distributed.hsdp import (
HSDPInferenceConfig,
HSDPShardContext,
apply_hsdp_to_model,
finalize_hsdp_root,
prepare_hsdp_shard_context,
shard_hsdp_module,
)
from vllm_omni.diffusion.lora.manager import LoRABackend
from vllm_omni.diffusion.model_loader.checkpoint_adapters import (
get_checkpoint_adapter,
@@ -38,6 +46,8 @@ from vllm_omni.diffusion.model_loader.checkpoint_adapters import (
from vllm_omni.diffusion.model_loader.host_weight_loader import HWRLoaderMixin, _HWRCommitError
from vllm_omni.diffusion.model_loader.host_weight_plan import (
HostWeightPlan,
TensorBinding,
build_checkpoint_binding_plan,
build_checkpoint_mmap_plan,
has_online_quantization,
)
@@ -49,7 +59,7 @@ from vllm_omni.diffusion.offloader.config import (
OffloadStrategy,
resolve_offload,
)
from vllm_omni.diffusion.offloader.module_collector import ModuleDiscovery
from vllm_omni.diffusion.offloader.module_collector import ModuleDiscovery, PipelineModules
from vllm_omni.diffusion.offloader.offload_plan import get_offload_plan
from vllm_omni.diffusion.registry import initialize_model
from vllm_omni.model_executor.model_loader.weight_utils import download_weights_from_hf_specific
@@ -138,6 +148,36 @@ def _resolve_custom_pipeline_cls(custom_pipeline_name: str | type | None) -> typ
)
def _is_distributed_rank_zero() -> bool:
return (
not torch.distributed.is_available()
or not torch.distributed.is_initialized()
or torch.distributed.get_rank() == 0
)
@dataclasses.dataclass(frozen=True)
class _HSDPLoadRoot:
name: str
module: nn.Module
context: HSDPShardContext
@dataclasses.dataclass(frozen=True)
class _HSDPModuleLoadGroup:
name: str
module: nn.Module
context: HSDPShardContext
@dataclasses.dataclass(frozen=True)
class _PreShardedHSDPLoadPlan:
roots: list[_HSDPLoadRoot]
groups: list[_HSDPModuleLoadGroup]
bindings: dict[str, TensorBinding]
binding_names: frozenset[str]
class DiffusersPipelineLoader(HWRLoaderMixin):
"""Model loader that can load diffusers pipeline components from disk."""
@@ -1222,6 +1262,451 @@ class DiffusersPipelineLoader(HWRLoaderMixin):
raise ValueError(f"Unknown load_format: {load_format}")
return model
def _load_model_with_pre_sharded_hsdp(
self,
model: nn.Module,
discovered_modules: PipelineModules,
hsdp_config: HSDPInferenceConfig,
target_device: torch.device,
) -> nn.Module:
"""Shard meta DiT parameters before reading their rank-local checkpoint slices."""
from torch.distributed.checkpoint import HuggingFaceStorageReader
from torch.distributed.checkpoint import load as dcp_load
if self.counter_before_loading_weights == 0.0:
self.counter_before_loading_weights = time.perf_counter()
load_plan = self._prepare_pre_sharded_hsdp_load(
model,
discovered_modules,
hsdp_config,
target_device,
)
self._check_no_unplanned_meta_tensors(model, load_plan.binding_names)
self._validate_pre_sharded_hsdp_bindings(load_plan)
self._process_pre_sharded_hsdp_groups_on_meta(load_plan)
for group in load_plan.groups:
logger.debug("Applying meta-first HSDP shard to %s", group.name)
shard_hsdp_module(group.module, group.context)
for root in load_plan.roots:
logger.debug("Finalizing meta-first HSDP root %s", root.name)
finalize_hsdp_root(root.module, root.context)
self._materialize_pre_sharded_hsdp_state(model, load_plan, target_device)
targets = self._get_pre_sharded_hsdp_targets(model, load_plan)
checkpoint_states, local_payload_bytes = self._prepare_pre_sharded_checkpoint_states(
load_plan,
targets,
)
thread_count = int(getattr(self.od_config, "num_weight_load_threads", 4))
if _is_distributed_rank_zero():
directory_label = "directory" if len(checkpoint_states) == 1 else "directories"
logger.info(
"Pre-sharded HSDP loading %.2f GiB of rank-local tensor slices directly into "
"FSDP-owned storage from %d HF safetensors %s with %d reader thread(s) per rank",
local_payload_bytes / 1024**3,
len(checkpoint_states),
directory_label,
thread_count,
)
for checkpoint_dir in sorted(checkpoint_states, key=str):
reader = HuggingFaceStorageReader(str(checkpoint_dir), thread_count=thread_count)
# Each worker has the full checkpoint available and each DTensor
# already describes its rank-local destination slice. Avoid DCP's
# coordinator collectives, which can leave rank 0 with less GPU
# memory headroom than the other HSDP workers.
dcp_load(checkpoint_states[checkpoint_dir], storage_reader=reader, no_dist=True)
remaining_meta = self._pre_sharded_meta_target_names(model, load_plan)
if remaining_meta:
raise ValueError(
"Pre-sharded HSDP checkpoint loading left tensors on meta; "
f"first missing tensors: {remaining_meta[:16]}"
)
self._finalize_pre_sharded_hsdp_loaded_weights(load_plan)
self.counter_after_loading_weights = time.perf_counter()
logger.info_once(
"Loading weights took %.2f seconds",
self.counter_after_loading_weights - self.counter_before_loading_weights,
)
self._move_non_hsdp_modules(discovered_modules, target_device)
return model
@staticmethod
def _validate_pre_sharded_hsdp_bindings(load_plan: _PreShardedHSDPLoadPlan) -> None:
unsupported = [name for name, binding in load_plan.bindings.items() if binding.transform is not None]
if unsupported:
raise ValueError(
"Pre-sharded HSDP requires checkpoint tensors in runtime layout; "
f"tensor transforms are unsupported: {unsupported[:16]}"
)
non_safetensors = sorted(
{
binding.file_path
for binding in load_plan.bindings.values()
if not binding.file_path.endswith(".safetensors")
}
)
if non_safetensors:
raise ValueError(
f"Pre-sharded HSDP requires existing HF safetensors files; unsupported files: {non_safetensors[:8]}"
)
@staticmethod
def _get_pre_sharded_hsdp_targets(
model: nn.Module,
load_plan: _PreShardedHSDPLoadPlan,
) -> dict[str, torch.Tensor]:
targets: dict[str, torch.Tensor] = {
**dict(model.named_buffers()),
**dict(model.named_parameters()),
}
missing_targets = sorted(load_plan.binding_names - targets.keys())
if missing_targets:
raise ValueError(
f"Pre-sharded HSDP changed checkpoint target names; first missing targets: {missing_targets[:16]}"
)
return targets
@staticmethod
def _materialize_pre_sharded_hsdp_state(
model: nn.Module,
load_plan: _PreShardedHSDPLoadPlan,
target_device: torch.device,
) -> None:
"""Materialize the installed FSDP rank-local state before checkpoint I/O.
``fully_shard`` supports meta initialization through ``Module.to_empty``:
its registered ``_apply`` hooks update the internal sharded storage to
point at the newly allocated rank-local tensors. Modules are materialized
in the same HSDP-group order and child-first traversal used by ordinary
``fully_shard`` initialization instead of the forward traversal used by
a recursive ``to_empty`` call.
PyTorch's swap-on-conversion mode preserves Parameter identity so that
post-load kernel objects created before sharding cannot retain stale
references to the original meta Parameters.
A complete checkpoint plan covers every parameter and persistent buffer
in each DiT. Non-persistent buffers are not checkpoint entries, so keep
their initialized values across ``to_empty``.
"""
unplanned_parameters: list[str] = []
saved_buffers: dict[str, torch.Tensor] = {}
for root in load_plan.roots:
prefix = f"{root.name}."
for local_name, _parameter in root.module.named_parameters():
full_name = prefix + local_name
if full_name not in load_plan.binding_names:
unplanned_parameters.append(full_name)
for local_name, buffer in root.module.named_buffers():
full_name = prefix + local_name
if full_name in load_plan.binding_names:
continue
if buffer.device.type == "meta":
raise ValueError(f"Pre-sharded HSDP cannot preserve an unplanned meta buffer: {full_name}")
saved_buffers[full_name] = buffer.detach().cpu().clone()
if unplanned_parameters:
raise ValueError(
"Pre-sharded HSDP requires checkpoint coverage for every DiT parameter; "
f"first unplanned parameters: {unplanned_parameters[:16]}"
)
previous_swap_mode = torch.__future__.get_swap_module_params_on_conversion()
torch.__future__.set_swap_module_params_on_conversion(True)
try:
sharded_group_roots = {group.module for group in load_plan.groups}
materialized_modules: set[nn.Module] = set()
for group in load_plan.groups:
for module in DiffusersPipelineLoader._hsdp_managed_modules_post_order(
group.module,
nested_hsdp_roots=sharded_group_roots - {group.module},
):
if module not in materialized_modules:
module.to_empty(device=target_device, recurse=False)
materialized_modules.add(module)
for root in load_plan.roots:
for module in DiffusersPipelineLoader._hsdp_managed_modules_post_order(
root.module,
nested_hsdp_roots=sharded_group_roots,
):
if module not in materialized_modules:
module.to_empty(device=target_device, recurse=False)
materialized_modules.add(module)
finally:
torch.__future__.set_swap_module_params_on_conversion(previous_swap_mode)
if saved_buffers:
materialized_buffers = dict(model.named_buffers())
with torch.no_grad():
for name, value in saved_buffers.items():
target = materialized_buffers[name]
target.copy_(value.to(device=target.device, dtype=target.dtype))
@staticmethod
def _hsdp_managed_modules_post_order(
root: nn.Module,
*,
nested_hsdp_roots: set[nn.Module],
) -> list[nn.Module]:
"""Mirror FSDP's child-first managed-module traversal.
Nested FSDP roots own their own parameters and are materialized in their
corresponding load group, so the enclosing traversal must not descend
into them.
"""
modules: list[nn.Module] = []
visited: set[nn.Module] = set()
def visit(module: nn.Module) -> None:
if module in visited:
return
visited.add(module)
for child in module.children():
if child not in nested_hsdp_roots:
visit(child)
modules.append(module)
visit(root)
return modules
@staticmethod
def _prepare_pre_sharded_checkpoint_states(
load_plan: _PreShardedHSDPLoadPlan,
targets: dict[str, torch.Tensor],
) -> tuple[dict[Path, dict[str, torch.Tensor]], int]:
from torch.distributed.tensor import DTensor
checkpoint_states: dict[Path, dict[str, torch.Tensor]] = {}
checkpoint_owners: dict[tuple[Path, str], str] = {}
local_payload_bytes = 0
for name, binding in load_plan.bindings.items():
target = targets[name]
checkpoint_dir = Path(binding.file_path).parent
owner_key = (checkpoint_dir, binding.checkpoint_key)
if previous_owner := checkpoint_owners.get(owner_key):
raise ValueError(
"Pre-sharded HSDP cannot bind one checkpoint tensor to multiple runtime tensors: "
f"{binding.checkpoint_key!r} maps to both {previous_owner!r} and {name!r}"
)
checkpoint_owners[owner_key] = name
if target.device.type == "meta":
raise ValueError(f"Pre-sharded HSDP target {name!r} was not materialized")
local_target = target.to_local() if isinstance(target, DTensor) else target
local_payload_bytes += local_target.numel() * local_target.element_size()
checkpoint_states.setdefault(checkpoint_dir, {})[binding.checkpoint_key] = target
return checkpoint_states, local_payload_bytes
@staticmethod
def _pre_sharded_meta_target_names(
model: nn.Module,
load_plan: _PreShardedHSDPLoadPlan,
) -> list[str]:
from torch.distributed.tensor import DTensor
targets = {
**dict(model.named_buffers()),
**dict(model.named_parameters()),
}
return [
name
for name in load_plan.binding_names
if (target := targets[name]).device.type == "meta"
or (isinstance(target, DTensor) and target.to_local().device.type == "meta")
]
@staticmethod
def _process_pre_sharded_hsdp_groups_on_meta(load_plan: _PreShardedHSDPLoadPlan) -> None:
modules_to_process: list[tuple[nn.Module, QuantizeMethodBase]] = []
for group in load_plan.groups:
for _, module in group.module.named_modules():
quant_method = getattr(module, "quant_method", None)
if quant_method is None or not isinstance(quant_method, QuantizeMethodBase):
continue
modules_to_process.append((module, quant_method))
if not modules_to_process:
return
for root in load_plan.roots:
if not getattr(root.module, "_hsdp_pre_sharded_meta_post_load", False):
raise ValueError(
f"Model {type(root.module).__name__} has not declared its post-load processing safe "
"for pre-sharded meta initialization"
)
for module, quant_method in modules_to_process:
if type(quant_method) is not UnquantizedLinearMethod:
raise ValueError(
"Pre-sharded HSDP does not support the selected post-load processing; "
f"{type(module).__name__} uses {type(quant_method).__name__}"
)
for module, quant_method in modules_to_process:
quant_method.process_weights_after_loading(module)
def _prepare_pre_sharded_hsdp_load(
self,
model: nn.Module,
discovered_modules: PipelineModules,
hsdp_config: HSDPInferenceConfig,
target_device: torch.device,
) -> _PreShardedHSDPLoadPlan:
outer_dit_names, outer_dits = discovered_modules.outermost_dits()
if not outer_dits:
raise ValueError("No DiT modules discovered for HSDP sharding")
sources = self._get_weight_sources(model)
plan_result = build_checkpoint_binding_plan(
model,
dit_modules=tuple(zip(outer_dit_names, outer_dits)),
sources=sources,
model_path=str(getattr(self.od_config, "model", "")) or None,
tensor_parallel_size=int(getattr(self.parallel_config, "tensor_parallel_size", 1)),
online_quantization=False,
)
if plan_result.plan is None:
raise ValueError(f"Pre-sharded HSDP checkpoint loading is incompatible: {plan_result.fallback_reason}")
plan = plan_result.plan
unsupported_sources = [source.prefix for source in sources if source.prefix not in plan.planned_source_prefixes]
if unsupported_sources:
raise ValueError(
"Pre-sharded HSDP checkpoint loading requires dedicated DiT sources; "
f"unsupported source prefixes: {unsupported_sources}"
)
roots: list[_HSDPLoadRoot] = []
groups: list[_HSDPModuleLoadGroup] = []
for outer_name, outer_dit in zip(outer_dit_names, outer_dits):
conditions = getattr(outer_dit, "_hsdp_shard_conditions", None)
if not conditions:
raise ValueError(f"Model {type(outer_dit).__name__} has no _hsdp_shard_conditions defined")
context = prepare_hsdp_shard_context(
outer_dit,
hsdp_config,
target_device=target_device,
)
roots.append(_HSDPLoadRoot(outer_name, outer_dit, context))
for local_name, module in reversed(list(outer_dit.named_modules())):
if not any(condition(local_name, module) for condition in conditions):
continue
if not local_name:
raise ValueError("_hsdp_shard_conditions must not select the DiT root")
full_name = f"{outer_name}.{local_name}" if local_name else outer_name
groups.append(_HSDPModuleLoadGroup(full_name, module, context))
parameters = dict(model.named_parameters())
targets: dict[str, torch.Tensor] = {**dict(model.named_buffers()), **parameters}
preserved_params = {param for root in roots for param in root.context.ignored_params or ()}
binding_names = frozenset(plan.bindings)
self._release_hsdp_checkpoint_targets_to_meta(
model,
targets,
parameters,
binding_names,
preserved_params,
)
return _PreShardedHSDPLoadPlan(
roots=roots,
groups=groups,
bindings=plan.bindings,
binding_names=binding_names,
)
@staticmethod
def _check_no_unplanned_meta_tensors(model: nn.Module, planned_names: frozenset[str]) -> None:
unplanned_meta = [
name
for name, tensor in (*model.named_parameters(), *model.named_buffers())
if tensor.device.type == "meta" and name not in planned_names
]
if unplanned_meta:
raise ValueError(
"Pre-sharded HSDP initialized tensors on meta that are not covered by the "
f"checkpoint plan. First unplanned tensors: {unplanned_meta[:16]}"
)
@staticmethod
def _release_hsdp_checkpoint_targets_to_meta(
model: nn.Module,
targets: dict[str, torch.Tensor],
parameters: dict[str, nn.Parameter],
binding_names: frozenset[str],
preserved_params: set[nn.Parameter],
) -> None:
released: dict[str, tuple[int, float]] = {}
for name in binding_names:
target = targets[name]
if target in preserved_params or target.device.type == "meta":
continue
logical_gib = target.numel() * target.element_size() / 1024**3
device_type = target.device.type
count, gib = released.get(device_type, (0, 0.0))
released[device_type] = (count + 1, gib + logical_gib)
if name in parameters:
replacement = DiffusersPipelineLoader._make_parameter_like(
target,
torch.empty_like(target, device="meta"),
)
torch.utils.swap_tensors(target, replacement)
else:
replacement = torch.empty_like(target, device="meta")
torch.utils.swap_tensors(target, replacement)
if released:
logger.info("Released HSDP checkpoint-covered tensors to meta before pre-sharded load: %s", released)
@staticmethod
def _make_parameter_like(template: nn.Parameter, tensor: torch.Tensor) -> nn.Parameter:
"""Create a Parameter on new storage while preserving loader metadata."""
if type(template) is nn.Parameter:
replacement = nn.Parameter(tensor, requires_grad=template.requires_grad)
replacement.__dict__.update(getattr(template, "__dict__", {}))
return replacement
replacement = template.__class__.__new__(template.__class__, tensor)
replacement.__dict__.update(getattr(template, "__dict__", {}))
replacement.requires_grad_(template.requires_grad)
return replacement
@staticmethod
def _finalize_pre_sharded_hsdp_loaded_weights(
load_plan: _PreShardedHSDPLoadPlan,
) -> None:
"""Run model post-load hooks for tensors materialized by pre-sharded loading."""
for root in load_plan.roots:
post_load = getattr(root.module, "post_load_weights", None)
if callable(post_load):
post_load()
root.module.eval()
validate = getattr(root.module, "validate_loaded_weights", None)
if callable(validate):
prefix = f"{root.name}."
validate({name.removeprefix(prefix) for name in load_plan.binding_names if name.startswith(prefix)})
@staticmethod
def _move_non_hsdp_modules(
discovered_modules: PipelineModules,
target_device: torch.device,
) -> None:
modules_to_move: list[nn.Module] = []
if discovered_modules.vaes is not None:
modules_to_move.extend(discovered_modules.vaes)
if discovered_modules.encoders is not None:
modules_to_move.extend(discovered_modules.encoders)
if discovered_modules.resident_modules is not None:
modules_to_move.extend(discovered_modules.resident_modules)
for module in modules_to_move:
module.to(target_device)
def _load_model_with_hsdp(
self,
target_device: torch.device,
@@ -1251,6 +1736,29 @@ class DiffusersPipelineLoader(HWRLoaderMixin):
# mapping (QKV fusion, etc.).
if load_format == "diffusers":
raise ValueError("HSDP is not supported with the diffusers adapter load format")
strategy = getattr(self.od_config, "hsdp_weight_load_strategy", "full")
if strategy == "pre_sharded":
if load_format != "default":
raise ValueError(
f"hsdp_weight_load_strategy={strategy!r} currently supports only diffusion_load_format='default'"
)
if self.quant_config is not None:
raise ValueError(f"hsdp_weight_load_strategy={strategy!r} does not support quantization yet")
if getattr(self.od_config, "lora_path", None):
raise ValueError(f"hsdp_weight_load_strategy={strategy!r} does not support LoRA yet")
elif strategy != "full":
raise ValueError(f"Unknown hsdp_weight_load_strategy: {strategy!r}")
if strategy == "pre_sharded":
model = self._init_from_load_format(load_format, target_device, custom_pipeline_name, is_hsdp=True)
discovered_modules = ModuleDiscovery.discover(model)
return self._load_model_with_pre_sharded_hsdp(
model,
discovered_modules,
hsdp_config,
target_device,
)
# Same host-fallback bound as the ordinary path: with a quant config the
# HSDP path initializes on the accelerator (hsdp_defer_to_cpu=False), so
# over-wide NPU-unquantizable fallback weights would otherwise round-trip
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Loader-owned host-weight plans shared with diffusion offload backends."""
"""Loader-owned host-weight plans shared by direct-checkpoint consumers."""
from __future__ import annotations
@@ -205,7 +205,7 @@ def _build_source_map(
if not model_to_ckpt:
raise _PlanIncompatibleError("no compatible safetensors entries were found in the loader's model sources")
logger.info(
"Indexed %d runtime tensor names from %d checkpoint keys for DLO preflight",
"Indexed %d runtime tensor names from %d checkpoint keys for checkpoint preflight",
len(model_to_ckpt),
indexed_keys,
)
@@ -313,28 +313,28 @@ def _validate_source_metadata(
)
def build_checkpoint_mmap_plan(
def build_checkpoint_binding_plan(
pipeline: nn.Module,
*,
dit_modules: Sequence[tuple[str, nn.Module]],
sources: Sequence[object],
model_path: str | None,
tensor_parallel_size: int,
use_hsdp: bool,
online_quantization: bool,
use_pipeline_key_remap: bool = True,
has_distilled_lora: bool = False,
) -> HostWeightPlanResult:
"""Build a complete direct-checkpoint plan or return a fallback reason."""
"""Build a complete binding plan for the pre-sharded HSDP consumer."""
if tensor_parallel_size != 1:
return HostWeightPlanResult(None, f"TP={tensor_parallel_size} requires the ordinary loader")
if use_hsdp:
return HostWeightPlanResult(None, "HSDP requires the ordinary loader")
if online_quantization:
return HostWeightPlanResult(None, "online quantization requires the ordinary loader")
if has_distilled_lora:
return HostWeightPlanResult(None, "distilled LoRA requires the ordinary loader")
remap_fn = getattr(type(pipeline), "_remap_ckpt_key", None)
if not callable(remap_fn) and use_pipeline_key_remap:
remap_fn = getattr(pipeline, "remap_checkpoint_key", None)
if not callable(remap_fn):
remap_fn = None
adapter = get_direct_mmap_adapter(pipeline)
@@ -387,10 +387,37 @@ def build_checkpoint_mmap_plan(
)
def build_checkpoint_mmap_plan(
pipeline: nn.Module,
*,
dit_modules: Sequence[tuple[str, nn.Module]],
sources: Sequence[object],
model_path: str | None,
tensor_parallel_size: int,
use_hsdp: bool,
online_quantization: bool,
has_distilled_lora: bool = False,
) -> HostWeightPlanResult:
"""Build the DLO mmap plan without changing its HSDP eligibility."""
if use_hsdp:
return HostWeightPlanResult(None, "HSDP requires the ordinary loader")
return build_checkpoint_binding_plan(
pipeline,
dit_modules=dit_modules,
sources=sources,
model_path=model_path,
tensor_parallel_size=tensor_parallel_size,
online_quantization=online_quantization,
use_pipeline_key_remap=False,
has_distilled_lora=has_distilled_lora,
)
__all__ = [
"HostWeightPlan",
"HostWeightPlanResult",
"TensorBinding",
"build_checkpoint_binding_plan",
"build_checkpoint_mmap_plan",
"has_online_quantization",
]
@@ -0,0 +1,148 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Triton kernels for the AuK codec's alias-free SnakeBeta activation.
The eager module oversamples with a grouped transposed convolution, applies
SnakeBeta, and decimates with a grouped strided convolution. Both grouped
convolutions carry one 12-tap filter shared by every channel, and cuDNN and
ATen serve them with generic depthwise kernels that dominate the decode (more
than half of a 12 s decode on H200). Here the oversampling is written as its
polyphase form fused with the activation, and the decimation as a direct
strided FIR, each one pass over memory per row. The arithmetic is the same
as the eager module in fp32; only the summation order of the taps differs.
The ops are registered with ``torch.library.triton_op`` so the compiled
decode buckets trace them instead of breaking the graph.
"""
from __future__ import annotations
import torch
from vllm.triton_utils import HAS_TRITON, tl, triton
__all__ = ["alias_free_snake"]
_BLOCK = 1024
if HAS_TRITON:
@triton.jit
def _upsample_snake_kernel(
x_ptr,
out_ptr,
taps_ptr,
exp_alpha_ptr,
inv_beta_ptr,
channels,
in_len,
out_len,
ratio: tl.constexpr,
taps: tl.constexpr,
pad: tl.constexpr,
crop: tl.constexpr,
block: tl.constexpr,
):
# out[m] = snake(ratio * sum_k taps[k] * xpad[(m + crop - k) / ratio]) over the k
# with (m + crop - k) divisible by ratio, where xpad is x replicate-padded by pad
# on both sides. That is conv_transpose1d(stride=ratio) followed by the crop.
row = tl.program_id(0)
offs = tl.program_id(1) * block + tl.arange(0, block)
mask = offs < out_len
x_row = x_ptr + row.to(tl.int64) * in_len
acc = tl.zeros((block,), dtype=tl.float32)
for k in tl.static_range(taps):
pos = offs + (crop - k)
hit = (pos % ratio) == 0
src = pos // ratio - pad
src = tl.minimum(tl.maximum(src, 0), in_len - 1)
w = tl.load(taps_ptr + k)
val = tl.load(x_row + src, mask=mask & hit, other=0.0)
acc += tl.where(hit, w * val, 0.0)
u = acc * ratio
channel = row % channels
alpha = tl.load(exp_alpha_ptr + channel)
inv_beta = tl.load(inv_beta_ptr + channel)
s = tl.sin(u * alpha)
u = u + inv_beta * (s * s)
tl.store(out_ptr + row.to(tl.int64) * out_len + offs, u, mask=mask)
@triton.jit
def _lowpass_kernel(
x_ptr,
out_ptr,
taps_ptr,
in_len,
out_len,
stride: tl.constexpr,
taps: tl.constexpr,
pad_left: tl.constexpr,
block: tl.constexpr,
):
# out[n] = sum_k taps[k] * x[clamp(stride * n + k - pad_left)], i.e. replicate
# padding followed by a strided conv1d.
row = tl.program_id(0)
offs = tl.program_id(1) * block + tl.arange(0, block)
mask = offs < out_len
x_row = x_ptr + row.to(tl.int64) * in_len
acc = tl.zeros((block,), dtype=tl.float32)
for k in tl.static_range(taps):
src = offs * stride + (k - pad_left)
src = tl.minimum(tl.maximum(src, 0), in_len - 1)
acc += tl.load(taps_ptr + k) * tl.load(x_row + src, mask=mask, other=0.0)
tl.store(out_ptr + row.to(tl.int64) * out_len + offs, acc, mask=mask)
@torch.library.triton_op("vllm_omni::auk_alias_free_snake", mutates_args=())
def alias_free_snake(
x: torch.Tensor,
up_taps: torch.Tensor,
down_taps: torch.Tensor,
exp_alpha: torch.Tensor,
inv_beta: torch.Tensor,
up_ratio: int,
up_pad: int,
up_crop: int,
down_stride: int,
down_pad_left: int,
down_pad_right: int,
) -> torch.Tensor:
"""Upsample by ``up_ratio``, SnakeBeta, low-pass and decimate: ``[B, C, T] -> [B, C, T_out]``."""
batch, channels, length = x.shape
x = x.contiguous()
up_len = length * up_ratio
oversampled = torch.empty((batch, channels, up_len), device=x.device, dtype=torch.float32)
rows = batch * channels
torch.library.wrap_triton(_upsample_snake_kernel)[(rows, triton.cdiv(up_len, _BLOCK))](
x,
oversampled,
up_taps,
exp_alpha,
inv_beta,
channels,
length,
up_len,
ratio=up_ratio,
taps=up_taps.numel(),
pad=up_pad,
crop=up_crop,
block=_BLOCK,
)
taps = down_taps.numel()
out_len = (up_len + down_pad_left + down_pad_right - taps) // down_stride + 1
out = torch.empty((batch, channels, out_len), device=x.device, dtype=torch.float32)
torch.library.wrap_triton(_lowpass_kernel)[(rows, triton.cdiv(out_len, _BLOCK))](
oversampled,
out,
down_taps,
up_len,
out_len,
stride=down_stride,
taps=taps,
pad_left=down_pad_left,
block=_BLOCK,
)
return out
else: # pragma: no cover
def alias_free_snake(*args, **kwargs):
raise RuntimeError("Triton is not available")
+126 -26
View File
@@ -153,6 +153,33 @@ class TimeEmbedding(nn.Module):
return self.time_mlp(hidden.to(self.time_mlp[0].weight.dtype))
def _grouped_conv1d_btc(x: torch.Tensor, conv: nn.Conv1d) -> torch.Tensor:
"""``conv`` applied to channels-last ``x`` ``[B, T, C]``, returning ``[B, T, C]``.
A same-padded grouped convolution written as one im2col copy and one
batched GEMM over the groups. cuDNN runs a grouped conv of this shape as
one kernel per group plus two layout transposes, which at AuK's sizes
(1536 channels in 16 groups, kernel 31, a few hundred frames) costs about
ten times the arithmetic.
"""
batch, frames, channels = x.shape
groups = conv.groups
kernel = conv.kernel_size[0]
per_group = channels // groups
pad = conv.padding[0]
padded = F.pad(x, (0, 0, pad, pad))
# unfold gives a [B, T, C, K] view; one copy lays it out as [B, T, G, C/G * K].
cols = padded.unfold(1, kernel, 1).reshape(batch * frames, groups, per_group * kernel)
# conv.weight [C_out, C/G, K] -> [G, C/G * K, C_out/G]; the reduction order matches the columns.
weight = conv.weight.reshape(groups, channels // groups, per_group * kernel).transpose(1, 2).to(cols.dtype)
# [G, B*T, C/G*K] @ [G, C/G*K, C_out/G] -> [G, B*T, C_out/G]
out = torch.bmm(cols.transpose(0, 1), weight)
out = out.transpose(0, 1).reshape(batch, frames, channels)
if conv.bias is not None:
out = out + conv.bias.to(out.dtype)
return out
class ConvPosEmbedding(nn.Module):
"""Depthwise-grouped conv stack that adds local position information."""
@@ -169,17 +196,19 @@ class ConvPosEmbedding(nn.Module):
)
def forward(self, x: torch.Tensor, mask: torch.Tensor | None = None) -> torch.Tensor:
keep = None if mask is None else mask.unsqueeze(1)
x = x.transpose(1, 2)
keep = None if mask is None else mask.unsqueeze(-1)
if keep is not None:
x = x.masked_fill(~keep, 0.0)
for layer in self.conv1d:
x = layer(x)
# Re-zero padding after each convolution so it cannot bleed into
# valid frames through the kernel window.
if keep is not None and isinstance(layer, nn.Conv1d):
x = x.masked_fill(~keep, 0.0)
return x.transpose(1, 2)
if isinstance(layer, nn.Conv1d):
x = _grouped_conv1d_btc(x, layer)
# Re-zero padding after each convolution so it cannot bleed into
# valid frames through the kernel window.
if keep is not None:
x = x.masked_fill(~keep, 0.0)
else:
x = layer(x)
return x
class AudioEmbedding(nn.Module):
@@ -205,9 +234,12 @@ class AdaLayerNorm(nn.Module):
self.norm = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
def forward(
self, x: torch.Tensor, emb: torch.Tensor
self, x: torch.Tensor, emb: torch.Tensor | None, mod: torch.Tensor | None = None
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.linear(self.silu(emb)).chunk(6, dim=1)
"""``mod`` is the precomputed ``linear(silu(emb))``; ``emb`` is then unused."""
if mod is None:
mod = self.linear(self.silu(emb))
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = mod.chunk(6, dim=1)
x = self.norm(x) * (1 + scale_msa[:, None]) + shift_msa[:, None]
return x, gate_msa, shift_mlp, scale_mlp, gate_mlp
@@ -221,8 +253,11 @@ class AdaLayerNormFinal(nn.Module):
self.linear = nn.Linear(dim, dim * 2)
self.norm = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
def forward(self, x: torch.Tensor, emb: torch.Tensor) -> torch.Tensor:
scale, shift = self.linear(self.silu(emb)).chunk(2, dim=1)
def forward(self, x: torch.Tensor, emb: torch.Tensor | None, mod: torch.Tensor | None = None) -> torch.Tensor:
"""``mod`` is the precomputed ``linear(silu(emb))``; ``emb`` is then unused."""
if mod is None:
mod = self.linear(self.silu(emb))
scale, shift = mod.chunk(2, dim=1)
return self.norm(x) * (1 + scale)[:, None, :] + shift[:, None, :]
@@ -383,9 +418,11 @@ class DoubleBlock(nn.Module):
c_rope: RopeCache,
c_mask: torch.Tensor | None,
bias: torch.Tensor | None = None,
mod_c: torch.Tensor | None = None,
mod_x: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor]:
norm_c, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.attn_norm_c(c, t)
norm_x, x_gate_msa, x_shift_mlp, x_scale_mlp, x_gate_mlp = self.attn_norm_x(x, t)
norm_c, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.attn_norm_c(c, t, mod_c)
norm_x, x_gate_msa, x_shift_mlp, x_scale_mlp, x_gate_mlp = self.attn_norm_x(x, t, mod_x)
x_attn, c_attn = self.attn(x=norm_x, c=norm_c, mask=mask, rope=rope, c_rope=c_rope, c_mask=c_mask, bias=bias)
@@ -416,8 +453,9 @@ class SingleBlock(nn.Module):
mask: torch.Tensor | None,
rope: RopeCache,
bias: torch.Tensor | None = None,
mod: torch.Tensor | None = None,
) -> torch.Tensor:
norm, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.attn_norm(x, t)
norm, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.attn_norm(x, t, mod)
x = x + gate_msa[:, None] * self.attn(x=norm, mask=mask, rope=rope, bias=bias)
norm = self.ff_norm(x) * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
return x + gate_mlp[:, None] * self.ff(norm)
@@ -491,6 +529,28 @@ class AuKTransformer(nn.Module):
self.norm_out = AdaLayerNormFinal(dim)
self.proj_out = nn.Linear(dim, latent_dim)
# Widths of the adaLN modulations in the order modulation_table() lays them out.
self._modulation_widths = [module.linear.out_features for module in self._adaln_modules()]
def _adaln_modules(self) -> list[nn.Module]:
"""Every timestep-conditioned norm, in the order :meth:`step` consumes them."""
modules: list[nn.Module] = []
for block in self.transformer_blocks:
modules += [block.attn_norm_c, block.attn_norm_x]
modules += [block.attn_norm for block in self.single_transformer_blocks]
modules.append(self.norm_out)
return modules
def modulation_table(self, timesteps: torch.Tensor) -> torch.Tensor:
"""adaLN modulations for every timestep, ``[len(timesteps), sum of widths]``.
The modulations depend only on the timestep, and a request's time grid
is known before its first step. Computing them for the whole grid at
once reads each modulation weight once per request instead of once per
step, which at batch 1 is what those matrix-vector products cost.
"""
emb = F.silu(self.time_embed(timesteps.reshape(-1).float()))
return torch.cat([module.linear(emb) for module in self._adaln_modules()], dim=1)
def project_text(self, text: torch.Tensor) -> torch.Tensor:
"""Project LLM hidden states ``[B, nt, text_hidden_dim]`` to model width."""
@@ -520,6 +580,7 @@ class AuKTransformer(nn.Module):
drop_text: bool = False,
cfg_infer: bool = False,
cache: bool = False,
timesteps: torch.Tensor | None = None,
) -> "AuKStepContext":
"""Everything a denoise step needs that does not depend on ``x`` or the time.
@@ -528,6 +589,9 @@ class AuKTransformer(nn.Module):
prompt embedding, the padding masks and biases and the rotary tables
are not recomputed on every step. Arguments are those of
:meth:`forward`; ``target_len`` is the target frame count ``n``.
``timesteps`` are the times the steps will run at (the grid without
its end point); when given, their adaLN modulations are precomputed
and :meth:`step` selects them by ``step_index``.
"""
batch = text.shape[0]
if c_mask is None:
@@ -593,25 +657,44 @@ class AuKTransformer(nn.Module):
rope_text=_rope_cos_sin(self.rotary_embed(text_len, c_mask)),
rope_single=_rope_cos_sin(self.rotary_embed(text_len + audio_len, single_mask)),
branches=branches,
modulation=None if timesteps is None else self.modulation_table(timesteps),
)
def step(self, x: torch.Tensor, time: torch.Tensor, ctx: "AuKStepContext") -> torch.Tensor:
def step(
self,
x: torch.Tensor,
time: torch.Tensor,
ctx: "AuKStepContext",
step_index: torch.Tensor | int | None = None,
) -> torch.Tensor:
"""Predict the velocity of ``x`` at ``time`` under a :meth:`prepare` context.
The target embedding does not depend on the CFG branch, so it is
computed once and shared by both branches.
computed once and shared by both branches. When the context carries a
modulation table, ``step_index`` selects this step's row of it and
``time`` is unused; the row broadcasts over the CFG branches.
"""
if time.ndim == 0:
time = time.repeat(x.shape[0])
t = self.time_embed(time)
target = self.audio_embed(x, mask=ctx.target_mask)
if ctx.modulation is not None and step_index is not None:
if isinstance(step_index, int):
row = ctx.modulation[step_index : step_index + 1]
else:
row = ctx.modulation.index_select(0, step_index.reshape(1))
mods = list(torch.split(row, self._modulation_widths, dim=1))
t = None
else:
if time.ndim == 0:
time = time.repeat(x.shape[0])
t = self.time_embed(time)
if ctx.branches == 2:
t = torch.cat((t, t), dim=0)
mods = [None] * len(self._modulation_widths)
if ctx.branches == 2:
t = torch.cat((t, t), dim=0)
target = torch.cat((target, target), dim=0)
audio = target if ctx.prompt is None else torch.cat([ctx.prompt, target], dim=1)
c = ctx.c
for block in self.transformer_blocks:
for i, block in enumerate(self.transformer_blocks):
c, audio = block(
audio,
c,
@@ -621,14 +704,24 @@ class AuKTransformer(nn.Module):
c_rope=ctx.rope_text,
c_mask=ctx.c_mask,
bias=ctx.joint_bias,
mod_c=mods[2 * i],
mod_x=mods[2 * i + 1],
)
h = torch.cat([c, audio], dim=1)
for block in self.single_transformer_blocks:
h = block(h, t, mask=ctx.single_mask, rope=ctx.rope_single, bias=ctx.single_bias)
offset = 2 * len(self.transformer_blocks)
for i, block in enumerate(self.single_transformer_blocks):
h = block(
h,
t,
mask=ctx.single_mask,
rope=ctx.rope_single,
bias=ctx.single_bias,
mod=mods[offset + i],
)
h = h[:, h.shape[1] - x.shape[1] :]
return self.proj_out(self.norm_out(h, t))
return self.proj_out(self.norm_out(h, t, mods[-1]))
def forward(
self,
@@ -706,6 +799,8 @@ class AuKStepContext:
rope_text: RopeCache
rope_single: RopeCache
branches: int
#: adaLN modulations per step, ``[steps, widths]``, when the time grid was known.
modulation: torch.Tensor | None = None
def tensors(self) -> list[torch.Tensor | None]:
"""Every tensor field in a fixed order, rope pairs flattened."""
@@ -721,6 +816,7 @@ class AuKStepContext:
*self.rope_audio,
*self.rope_text,
*self.rope_single,
self.modulation,
]
def copy_(self, other: "AuKStepContext") -> None:
@@ -752,6 +848,7 @@ class AuKStepContext:
rope_text=(self.rope_text[0].clone(), self.rope_text[1].clone()),
rope_single=(self.rope_single[0].clone(), self.rope_single[1].clone()),
branches=self.branches,
modulation=_clone(self.modulation),
)
@@ -881,6 +978,8 @@ def sample_latents(
timestep=t[i],
cfg_strength=cfg_strength,
new_request=i == 0,
timesteps=t[:-1],
step_index=i,
)
x = x + (t[i + 1] - t[i]) * velocity
finally:
@@ -922,9 +1021,10 @@ def _sample_latents(
ref_mask=ref_mask,
cfg_infer=guided,
cache=guided,
timesteps=timesteps[:-1],
)
for i in range(timesteps.shape[0] - 1):
v = dit.step(x, timesteps[i], ctx)
v = dit.step(x, timesteps[i], ctx, step_index=i)
if guided:
v_cond, v_uncond = v.chunk(2, dim=0)
v = v_cond + (v_cond - v_uncond) * cfg_strength
+39
View File
@@ -32,7 +32,9 @@ from torch import nn
from torch.nn.utils import remove_weight_norm as _fold_weight_norm
from torch.nn.utils import weight_norm as _apply_weight_norm
from vllm.logger import init_logger
from vllm.triton_utils import HAS_TRITON
from vllm_omni.diffusion.models.auk.alias_free_triton import alias_free_snake
from vllm_omni.model_executor.models.common.snake_activation import SnakeBeta as _SharedSnakeBeta
logger = init_logger(__name__)
@@ -319,8 +321,45 @@ class AliasFreeActivation(nn.Module):
self.downsample = Downsample(down_ratio, down_kernel_size, causal=causal)
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self._fused_eligible(x):
return self._fused_forward(x)
return self.downsample(self.act(self.upsample(x)))
def _fused_eligible(self, x: torch.Tensor) -> bool:
"""Whether ``x`` can take the Triton kernels in alias_free_triton (CUDA fp32, AuK's filter geometry)."""
up = self.upsample
taps = up.filter.numel()
return (
x.is_cuda
and x.dtype == torch.float32
and x.dim() == 3
and isinstance(self.act, SnakeBeta)
and HAS_TRITON
# The kernel indexes the crop from the start of the transposed
# conv output, so the crop must cover the filter's reach, and the
# cropped length must be exactly ratio * T.
and up.pad_left >= taps - 1
and (2 * up.pad - 1) * up.ratio + taps == up.pad_left + up.pad_right
)
def _fused_forward(self, x: torch.Tensor) -> torch.Tensor:
up, low, act = self.upsample, self.downsample.lowpass, self.act
if not act._cached:
act.precompute_exp_cache()
return alias_free_snake(
x,
up.filter.reshape(-1),
low.filter.reshape(-1),
act._exp_alpha,
act._inv_beta,
up.ratio,
up.pad,
up.pad_left,
low.stride,
low.pad_left,
low.pad_right,
)
class AmpBlock(nn.Module):
"""Anti-aliased multi-periodicity residual block: alternating alias-free snake and dilated convs."""
@@ -24,16 +24,19 @@ class _GraphEntry:
static_ctx: AuKStepContext
static_timestep: torch.Tensor
static_cfg: torch.Tensor | None
static_step: torch.Tensor | None
static_out: torch.Tensor
class AuKCUDAGraphWrapper:
"""Replay one DiT denoise step and leave Euler scheduling to the caller.
The graph is keyed by the target, text and reference sequence lengths plus
whether the CFG branch is enabled. Timestep and the CFG strength are
The graph is keyed by the target, text and reference sequence lengths,
whether the CFG branch is enabled and, when the caller passes the time
grid, its length. Timestep (or the step index), and the CFG strength are
mutable scalar buffers, so all Euler steps and CFG values within one path
reuse one graph.
reuse one graph. With the time grid, the adaLN modulations of every step
are part of the per-request context and the graph selects its row.
Only :meth:`AuKTransformer.step` is captured. The per-request context
(text projection, reference embedding, padding biases, rotary tables) is
@@ -85,8 +88,9 @@ class AuKCUDAGraphWrapper:
text: torch.Tensor,
ref: torch.Tensor,
uses_cfg: bool,
) -> tuple[int, int, int, bool]:
return (x.shape[1], text.shape[1], ref.shape[1], uses_cfg)
steps: int = 0,
) -> tuple[int, int, int, bool, int]:
return (x.shape[1], text.shape[1], ref.shape[1], uses_cfg, steps)
def _retire_graph_generation_if_full(self) -> None:
"""Retire all graphs together so none outlive shared workspaces."""
@@ -103,6 +107,7 @@ class AuKCUDAGraphWrapper:
ref: torch.Tensor,
ref_mask: torch.Tensor,
uses_cfg: bool,
timesteps: torch.Tensor | None = None,
) -> AuKStepContext:
return self.dit.prepare(
text,
@@ -112,6 +117,7 @@ class AuKCUDAGraphWrapper:
ref=ref,
ref_mask=ref_mask,
cfg_infer=uses_cfg,
timesteps=timesteps,
)
def _step(
@@ -120,9 +126,10 @@ class AuKCUDAGraphWrapper:
timestep: torch.Tensor,
ctx: AuKStepContext,
cfg_strength: torch.Tensor | float,
step_index: torch.Tensor | int | None = None,
) -> torch.Tensor:
"""One guided (or unguided) velocity; the CFG combine is part of the graph."""
velocity = self.dit.step(x, timestep, ctx)
velocity = self.dit.step(x, timestep, ctx, step_index=step_index)
if ctx.branches == 1:
return velocity
conditional, unconditional = velocity.chunk(2, dim=0)
@@ -140,6 +147,8 @@ class AuKCUDAGraphWrapper:
timestep: torch.Tensor,
cfg_strength: float,
new_request: bool = True,
timesteps: torch.Tensor | None = None,
step_index: int | None = None,
) -> torch.Tensor:
"""Velocity of ``x`` at ``timestep``.
@@ -147,26 +156,33 @@ class AuKCUDAGraphWrapper:
(``text``, ``c_mask``, ``ref``, ``ref_mask``) is then prepared anew; the
later steps of the request pass ``False`` and reuse it. The default
prepares on every call, which is always correct.
``timesteps`` (the times every step of the request runs at) and
``step_index`` (this step's position in them) let the adaLN
modulations be computed once per request instead of once per step.
"""
uses_cfg = cfg_strength >= 1e-5
if timesteps is None or step_index is None:
timesteps, step_index = None, None
if not self.enabled or x.device.type != "cuda" or torch.cuda.is_current_stream_capturing():
if new_request or self._eager_ctx is None:
self._eager_ctx = self._prepare(x, None, text, c_mask, ref, ref_mask, uses_cfg)
return self._step(x, timestep, self._eager_ctx, cfg_strength)
self._eager_ctx = self._prepare(x, None, text, c_mask, ref, ref_mask, uses_cfg, timesteps)
return self._step(x, timestep, self._eager_ctx, cfg_strength, step_index)
target_frames = x.shape[1]
x, x_mask, text, c_mask, ref, ref_mask = self._bucket_inputs(x, text, c_mask, ref, ref_mask)
key = self._key(x, text, ref, uses_cfg)
steps = 0 if timesteps is None else int(timesteps.numel())
key = self._key(x, text, ref, uses_cfg, steps)
entry = self._cache.get(key)
ctx = None
if entry is None or new_request or key != self._active_key:
ctx = self._prepare(x, x_mask, text, c_mask, ref, ref_mask, uses_cfg)
ctx = self._prepare(x, x_mask, text, c_mask, ref, ref_mask, uses_cfg, timesteps)
if entry is None:
# Same Retirement as #6587, details discussed in #7469
self._retire_graph_generation_if_full()
try:
assert ctx is not None
entry = self._capture(x, ctx, timestep, cfg_strength=cfg_strength)
entry = self._capture(x, ctx, timestep, cfg_strength=cfg_strength, step_index=step_index)
except Exception:
self._cache.clear()
self._active_key = None
@@ -186,6 +202,8 @@ class AuKCUDAGraphWrapper:
entry.static_x.copy_(x)
entry.static_timestep.copy_(timestep)
if entry.static_step is not None:
entry.static_step.fill_(step_index)
if entry.static_cfg is not None:
entry.static_cfg.fill_(cfg_strength)
entry.graph.replay()
@@ -198,10 +216,14 @@ class AuKCUDAGraphWrapper:
timestep: torch.Tensor,
*,
cfg_strength: float,
step_index: int | None = None,
) -> _GraphEntry:
static_x = x.clone()
static_ctx = ctx.clone()
static_timestep = timestep.clone()
static_step = None
if static_ctx.modulation is not None and step_index is not None:
static_step = torch.full((), step_index, device=static_x.device, dtype=torch.long)
static_cfg = None
if ctx.branches == 2:
static_cfg = torch.empty((), device=static_x.device, dtype=torch.float32)
@@ -209,13 +231,13 @@ class AuKCUDAGraphWrapper:
scale = static_cfg if static_cfg is not None else cfg_strength
# Warm-up runs any lazy torch.compile outside the capture.
for _ in range(3):
self._step(static_x, static_timestep, static_ctx, scale)
self._step(static_x, static_timestep, static_ctx, scale, static_step)
if self._pool_handle is None:
self._pool_handle = torch.cuda.graph_pool_handle()
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph, pool=self._pool_handle):
# The CFG scalar stays a mutable buffer, so cfg=2 and cfg=3 share this graph.
static_out = self._step(static_x, static_timestep, static_ctx, scale)
static_out = self._step(static_x, static_timestep, static_ctx, scale, static_step)
logger.info(
"Captured AuK DiT single-step CUDA graph: target_frames=%d text_tokens=%d ref_frames=%d cfg=%s",
@@ -230,6 +252,7 @@ class AuKCUDAGraphWrapper:
static_ctx=static_ctx,
static_timestep=static_timestep,
static_cfg=static_cfg,
static_step=static_step,
static_out=static_out,
)
+43 -3
View File
@@ -11,9 +11,11 @@ both, and the BigVGAN-flow decoder renders the waveform.
from __future__ import annotations
import hashlib
import json
import os
import time
from collections import OrderedDict
from collections.abc import Iterable
from typing import Any, ClassVar
@@ -27,7 +29,12 @@ from vllm.logger import init_logger
from vllm_omni.diffusion.compile import regionally_compile
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
from vllm_omni.diffusion.distributed.utils import get_local_device
from vllm_omni.diffusion.models.auk.auk_transformer import AuKTransformer, dit_state_dict, sample_latents
from vllm_omni.diffusion.models.auk.auk_transformer import (
AuKTransformer,
build_time_grid,
dit_state_dict,
sample_latents,
)
from vllm_omni.diffusion.models.auk.auk_vae import AuKVAE
from vllm_omni.diffusion.models.auk.cudagraph_wrapper import AuKCUDAGraphWrapper
from vllm_omni.diffusion.models.auk.vae_cudagraph import AuKVAEDecodeGraph
@@ -59,6 +66,10 @@ _FLASH_CFG = 0.0
_WARMUP_TARGET_FRAMES = (150, 300, 600)
_WARMUP_REF_FRAMES = (0, 150)
_WARMUP_TEXT_TOKENS = 96
# Reference clips whose posterior-mean latents are kept. Voice cloning reuses a
# small set of reference voices, so repeat requests skip the VAE encode.
# ``model_config.auk_ref_cache_size`` overrides it; 0 disables the cache.
_REF_CACHE_SIZE = 16
# Decorrelates the VAE posterior draw from the initial latent for the same request seed.
_VAE_SEED_OFFSET = 0x5EED_0A0C
@@ -152,6 +163,9 @@ class AuKPipeline(nn.Module, SupportAudioInput, SupportAudioOutput, SupportsComp
max_dit_graphs = od_config.model_config.get("max_dit_graphs", 32)
if isinstance(max_dit_graphs, bool) or not isinstance(max_dit_graphs, int) or max_dit_graphs < 1:
raise ValueError("AuK max_dit_graphs must be a positive integer")
ref_cache_size = od_config.model_config.get("auk_ref_cache_size", _REF_CACHE_SIZE)
if isinstance(ref_cache_size, bool) or not isinstance(ref_cache_size, int) or ref_cache_size < 0:
raise ValueError("AuK auk_ref_cache_size must be a non-negative integer (0 disables the cache)")
self.device = get_local_device()
self.dtype = getattr(od_config, "dtype", None) or torch.bfloat16
@@ -213,6 +227,8 @@ class AuKPipeline(nn.Module, SupportAudioInput, SupportAudioOutput, SupportsComp
if model_config.get("auk_vae_tile_frames") is not None:
vae_decode_kwargs["tile_frames"] = int(model_config["auk_vae_tile_frames"])
self.vae_decode = AuKVAEDecodeGraph(self.vae, enabled=not od_config.enforce_eager, **vae_decode_kwargs)
self._ref_cache_size = ref_cache_size
self._ref_cache: OrderedDict[str, torch.Tensor] = OrderedDict()
logger.info(
"AuK pipeline ready: variant=%s dtype=%s latent_dim=%d hop=%d sample_rate=%d",
@@ -267,6 +283,12 @@ class AuKPipeline(nn.Module, SupportAudioInput, SupportAudioOutput, SupportsComp
text = torch.zeros(1, _WARMUP_TEXT_TOKENS, self.text_hidden_dim, device=self.device, dtype=self.dtype)
c_mask = torch.ones(text.shape[:2], dtype=torch.bool, device=self.device)
timestep = torch.zeros((), device=self.device, dtype=torch.float32)
# The graphs are keyed by the number of steps, so warm the variant's default grid.
if self.is_flash:
grid = torch.tensor(self.flash_t_grid, device=self.device, dtype=torch.float32)
else:
sway = None if self.default_sway is None else float(self.default_sway)
grid = build_time_grid(nfe=self.default_nfe, sway_sampling_coef=sway, t_grid=None, device=self.device)
start = time.perf_counter()
with torch.inference_mode(), self._dit_autocast():
for ref_frames in _WARMUP_REF_FRAMES:
@@ -283,6 +305,8 @@ class AuKPipeline(nn.Module, SupportAudioInput, SupportAudioOutput, SupportsComp
timestep=timestep,
cfg_strength=cfg,
new_request=True,
timesteps=grid[:-1],
step_index=0,
)
if self.device.type != "cpu":
torch.accelerator.synchronize(self.device)
@@ -386,8 +410,24 @@ class AuKPipeline(nn.Module, SupportAudioInput, SupportAudioOutput, SupportsComp
if audio is None:
return torch.zeros(1, 0, self.latent_dim, device=self.device, dtype=torch.float32)
wav = self._prepare_waveform(_unwrap_single(audio)).to(self.device)
return self.vae.encode(wav, sample=sample, generator=generator)
wav = self._prepare_waveform(_unwrap_single(audio))
# The posterior mean is a pure function of the prepared waveform, so it
# is cached by content. A posterior draw depends on the generator and
# is always recomputed. Callers must not modify the returned tensor.
key = None
if not sample and self._ref_cache_size:
digest = hashlib.blake2b(wav.numpy().tobytes(), digest_size=16).hexdigest()
key = f"{wav.shape[-1]}:{digest}"
cached = self._ref_cache.get(key)
if cached is not None:
self._ref_cache.move_to_end(key)
return cached
latents = self.vae.encode(wav.to(self.device), sample=sample, generator=generator)
if key is not None:
self._ref_cache[key] = latents
while len(self._ref_cache) > self._ref_cache_size:
self._ref_cache.popitem(last=False)
return latents
def _target_frames(self, gen_seconds: Any, ref_frames: int) -> int:
"""Target latent length, capped by the remaining context."""
@@ -38,6 +38,7 @@ from vllm_omni.diffusion.data import OmniDiffusionConfig
from vllm_omni.diffusion.distributed.sp_plan import SequenceParallelInput, SequenceParallelOutput
from vllm_omni.diffusion.forward_context import get_forward_context, is_forward_context_available
from vllm_omni.diffusion.layers.norm import RMSNorm as _VllmRMSNorm
from vllm_omni.diffusion.models.utils import release_module_parameters_to_meta
from vllm_omni.diffusion.offloader.config import (
OffloadStrategy,
resolve_offload_strategy,
@@ -1084,6 +1085,7 @@ class Cosmos3LanguageModel(nn.Module):
rope_theta: float,
mrope_section: list[int],
quant_config: QuantizationConfig | None = None,
release_completed_blocks_to_meta: bool = False,
prefix: str = "",
) -> None:
super().__init__()
@@ -1093,21 +1095,21 @@ class Cosmos3LanguageModel(nn.Module):
rope_theta=rope_theta,
mrope_section=mrope_section,
)
self.layers = nn.ModuleList(
[
Cosmos3UndDecoderLayer(
hidden_size=hidden_size,
intermediate_size=intermediate_size,
num_attention_heads=num_attention_heads,
num_key_value_heads=num_key_value_heads,
head_dim=head_dim,
rms_norm_eps=rms_norm_eps,
quant_config=quant_config,
prefix=f"{prefix}.layers.{i}",
)
for i in range(num_hidden_layers)
]
)
self.layers = nn.ModuleList()
for i in range(num_hidden_layers):
layer = Cosmos3UndDecoderLayer(
hidden_size=hidden_size,
intermediate_size=intermediate_size,
num_attention_heads=num_attention_heads,
num_key_value_heads=num_key_value_heads,
head_dim=head_dim,
rms_norm_eps=rms_norm_eps,
quant_config=quant_config,
prefix=f"{prefix}.layers.{i}",
)
if release_completed_blocks_to_meta:
release_module_parameters_to_meta(layer)
self.layers.append(layer)
# TODO: Not used right now, will be used in the future for prompt upsampler.
self.norm = RMSNorm(hidden_size, eps=rms_norm_eps)
@@ -1228,6 +1230,10 @@ class Cosmos3VFMTransformer(nn.Module):
_hsdp_shard_conditions = [_is_transformer_block]
# Standard unquantized vLLM linears need no value-dependent post-load
# processing on accelerators. Edge inherits the same loading contract.
_hsdp_pre_sharded_meta_post_load = True
# Modules whose parameters must NOT be FSDP-sharded at the root level.
# time_embedder is cast to fp32 by post_load_weights for precision; if it
# were swept into the root flat-parameter under MixedPrecisionPolicy(param_dtype=bf16),
@@ -1346,6 +1352,10 @@ class Cosmos3VFMTransformer(nn.Module):
language_model_quant_config, gen_layers_quant_config = _resolve_cosmos3_quant_configs(
getattr(od_config, "quantization_config", None)
)
release_completed_blocks_to_meta = bool(
getattr(getattr(od_config, "parallel_config", None), "use_hsdp", False)
and getattr(od_config, "hsdp_weight_load_strategy", "full") == "pre_sharded"
)
mixed_precision_config, mixed_precision_source = resolve_mixed_precision_config(od_config)
if mixed_precision_config is None:
if mixed_precision_source == "additional_config_disabled":
@@ -1372,6 +1382,7 @@ class Cosmos3VFMTransformer(nn.Module):
rope_theta=self.rope_theta,
mrope_section=self.mrope_section,
quant_config=language_model_quant_config,
release_completed_blocks_to_meta=release_completed_blocks_to_meta,
prefix="language_model",
**self._language_model_kwargs(),
)
@@ -1399,24 +1410,24 @@ class Cosmos3VFMTransformer(nn.Module):
self.audio_proj_out = nn.Linear(self.hidden_size, self.sound_dim)
self.audio_modality_embed = nn.Parameter(torch.zeros(self.hidden_size))
self.gen_layers = nn.ModuleList(
[
Cosmos3GenDecoderLayer(
layer_idx=i,
hidden_size=self.hidden_size,
intermediate_size=self.intermediate_size,
num_attention_heads=self.num_attention_heads,
num_key_value_heads=self.num_key_value_heads,
head_dim=self.head_dim,
rms_norm_eps=self.rms_norm_eps,
quant_config=gen_layers_quant_config,
mlp_cls=self._gen_mlp_cls,
qk_norm=self.qk_norm_for_diffusion,
prefix=f"gen_layers.{i}",
)
for i in range(self.num_hidden_layers)
]
)
self.gen_layers = nn.ModuleList()
for i in range(self.num_hidden_layers):
layer = Cosmos3GenDecoderLayer(
layer_idx=i,
hidden_size=self.hidden_size,
intermediate_size=self.intermediate_size,
num_attention_heads=self.num_attention_heads,
num_key_value_heads=self.num_key_value_heads,
head_dim=self.head_dim,
rms_norm_eps=self.rms_norm_eps,
quant_config=gen_layers_quant_config,
mlp_cls=self._gen_mlp_cls,
qk_norm=self.qk_norm_for_diffusion,
prefix=f"gen_layers.{i}",
)
if release_completed_blocks_to_meta:
release_module_parameters_to_meta(layer)
self.gen_layers.append(layer)
self.mixed_precision_runtime: Cosmos3MixedPrecisionRuntime | None = None
if mixed_precision_config is not None:
@@ -14,6 +14,7 @@ from vllm.model_executor.layers.linear import ColumnParallelLinear, RowParallelL
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
from vllm_omni.diffusion.attention.layer import Attention as FrameworkAttention
from vllm_omni.diffusion.models.utils import release_module_parameters_to_meta
from .transformer_cosmos3 import (
Cosmos3VFMTransformer,
@@ -258,6 +259,7 @@ class Cosmos3EdgeLanguageModel(nn.Module):
mrope_section: list[int],
use_und_k_norm_for_gen: bool,
quant_config: QuantizationConfig | None = None,
release_completed_blocks_to_meta: bool = False,
prefix: str = "",
) -> None:
super().__init__()
@@ -267,22 +269,22 @@ class Cosmos3EdgeLanguageModel(nn.Module):
rope_theta=rope_theta,
mrope_section=mrope_section,
)
self.layers = nn.ModuleList(
[
Cosmos3EdgeUndDecoderLayer(
hidden_size=hidden_size,
intermediate_size=intermediate_size,
num_attention_heads=num_attention_heads,
num_key_value_heads=num_key_value_heads,
head_dim=head_dim,
rms_norm_eps=rms_norm_eps,
use_und_k_norm_for_gen=use_und_k_norm_for_gen,
quant_config=quant_config,
prefix=f"{prefix}.layers.{i}",
)
for i in range(num_hidden_layers)
]
)
self.layers = nn.ModuleList()
for i in range(num_hidden_layers):
layer = Cosmos3EdgeUndDecoderLayer(
hidden_size=hidden_size,
intermediate_size=intermediate_size,
num_attention_heads=num_attention_heads,
num_key_value_heads=num_key_value_heads,
head_dim=head_dim,
rms_norm_eps=rms_norm_eps,
use_und_k_norm_for_gen=use_und_k_norm_for_gen,
quant_config=quant_config,
prefix=f"{prefix}.layers.{i}",
)
if release_completed_blocks_to_meta:
release_module_parameters_to_meta(layer)
self.layers.append(layer)
self.norm = RMSNorm(hidden_size, eps=rms_norm_eps)
def forward(
@@ -288,13 +288,13 @@ def get_lingbot_video_post_process_func(od_config: OmniDiffusionConfig):
output_key = output_keys[0]
frames = frames[output_key]
output_type = getattr(sampling_params, "output_type", None) or "pt"
# The image serving path currently accepts PIL images or NumPy arrays,
# while LingBot decodes T2I outputs to tensors. Keep that compatibility
# conversion without discarding the model's image/video payload key.
# Decoded LingBot pixels are already in [0, 1]. The video API treats
# floating tensors as [-1, 1], so use its NumPy path to avoid normalizing
# twice. Images also need NumPy for serving; keep latent tensors intact.
if (
isinstance(frames, torch.Tensor)
and output_type != "latent"
and (output_type == "np" or output_key == "image")
and (output_type == "np" or output_key in {"image", "video"})
):
frames = frames.float().cpu().numpy()
return {output_key: frames} if output_key is not None else frames
+22
View File
@@ -168,6 +168,28 @@ def init_parameters(
init_parameters(child, dtype, device)
def release_module_parameters_to_meta(module: nn.Module) -> None:
"""Release a fully constructed module's parameter storage to meta.
The module must finish construction first so initialization code and vLLM
weight-loader metadata follow the ordinary CPU path. ``swap_tensors`` keeps
each registered Parameter object's identity while dropping only its storage.
Persistent buffers intentionally remain materialized.
"""
for param in module.parameters():
if param.device.type == "meta":
continue
meta_tensor = torch.empty_like(param, device="meta")
if type(param) is nn.Parameter:
meta_param = nn.Parameter(meta_tensor, requires_grad=param.requires_grad)
else:
meta_param = param.__class__.__new__(param.__class__, meta_tensor)
meta_param.requires_grad_(param.requires_grad)
meta_param.__dict__.update(getattr(param, "__dict__", {}))
torch.utils.swap_tensors(param, meta_param)
def init_parameters_preserving_vllm_linear(
module: nn.Module,
dtype: torch.dtype | None,
@@ -307,9 +307,13 @@ class SharedMemoryConnector(OmniConnectorBase):
self._metrics["gets"] += 1
return result
def cleanup(self, request_id: str) -> None:
"""Unlink the exact key passed to ``put()``, never a request-id prefix."""
def cleanup(self, request_id: str) -> bool:
"""Unlink the exact key passed to ``put()``, never a request-id prefix.
Returns True when an unconsumed segment was actually unlinked.
"""
key = request_id
unlinked = False
with self._pending_keys_lock:
self._pending_keys.pop(key, None)
try:
@@ -317,6 +321,7 @@ class SharedMemoryConnector(OmniConnectorBase):
seg.close()
seg.unlink()
logger.debug("cleanup: unlinked unconsumed SHM segment %s", key)
unlinked = True
except FileNotFoundError:
pass
except Exception as e:
@@ -327,6 +332,18 @@ class SharedMemoryConnector(OmniConnectorBase):
os.remove(lock_file)
except OSError:
pass
return unlinked
def cleanup_prefix(self, key_prefix: str) -> int:
"""Unlink every tracked key of the form ``{key_prefix}{chunk_id}``.
Only keys still in ``_pending_keys`` are considered, and the suffix
must be a bare integer chunk id, so another request whose id merely
shares the prefix is never matched. Returns the unlinked count.
"""
with self._pending_keys_lock:
keys = [k for k in self._pending_keys if k.startswith(key_prefix) and k[len(key_prefix) :].isdigit()]
return sum(self.cleanup(key) for key in keys)
def close(self) -> None:
"""Unlink all remaining tracked SHM segments."""
@@ -1169,7 +1169,12 @@ class _OmniConnectorPayloadTransportMixin(_OmniConnectorRuntimeMixin):
self._payload_finished(payload_data),
)
self._get_req_chunk[req_id] += 1
with self._lock:
if self._async_chunk and request is not None and req_id not in self._pending_load_reqs:
# A connector get can return after cancellation removed its
# receiver. Do not recreate delivery state for that request.
return False
self._get_req_chunk[req_id] += 1
if self._async_chunk:
is_finished = self._payload_finished(payload_data)
@@ -1180,8 +1185,16 @@ class _OmniConnectorPayloadTransportMixin(_OmniConnectorRuntimeMixin):
if not self._payload_value_has_content(new_ids) and not is_finished:
return False
payload_consumable = self._payload_is_consumable(payload_data)
first_chunk_hook = getattr(self, "_first_chunk_hook", None)
if first_chunk_hook is not None and chunk_id == 0 and not is_finished and payload_consumable:
first_chunk_hook(req_id, request, payload_data)
with self._lock:
if request is not None and req_id not in self._pending_load_reqs:
# The first-chunk hook may have overlapped cancellation.
# Its slot cleanup is ordered by the model; this payload
# must not republish readiness after receive teardown.
return False
if self._model_mode == "ar":
# Accumulation, staging, and model-side consume/ack share
# this lock. Keeping the transition atomic prevents the
@@ -1723,3 +1736,7 @@ class _OmniConnectorPayloadTransportMixin(_OmniConnectorRuntimeMixin):
sink: Callable[[OmniConnectorOutput], None] | None,
) -> None:
self._omni_connector_output_sink = sink
def set_first_chunk_hook(self, hook: Callable[[str, Any, OmniPayload], bool]) -> None:
"""Claim eligible first chunks before publishing them to the model loop."""
self._first_chunk_hook = hook
@@ -28,6 +28,8 @@ from .base import OmniTransferAdapterBase
logger = get_connector_logger(__name__)
_RECLAIM_WARN_INTERVAL = 1000
class _SenderGeneration:
"""Fence one external request generation without blocking cleanup."""
@@ -196,6 +198,8 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
self.request_payload = {}
self.code_prompt_token_ids: dict[str, list[torch.Tensor]] = defaultdict(list)
self.request_ids_mapping: dict[str, str] = {}
# Save-thread only: running count of segments reclaimed after finish.
self._reclaimed_shm_total = 0
self.waiting_for_chunk_waiting_requests: deque[Any] = deque()
self.waiting_for_chunk_running_requests: deque[Any] = deque()
@@ -688,6 +692,9 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
for chunk_id in range(num_puts):
self.connector.cleanup(f"{key_prefix}_{chunk_id}")
return
if "release_shm" in task:
self._release_shm_prefix(task["release_shm"])
return
request = task["request"]
external_req_id = request.external_req_id
sender_token = task.get("sender_token")
@@ -979,8 +986,9 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
Called after a terminal chunk is sent or when the scheduler aborts the
request before a terminal chunk can be produced. In-flight sends are
cancelled here and reclaim their own state in ``finally``; cleanup
never waits for connector I/O on the scheduler thread.
cancelled here and reclaim adapter state plus leftover SHM in
``finally`` after ``put()`` returns; cleanup never waits for connector
I/O on the scheduler thread.
Idempotent: calling with an already-cleaned or unknown id is safe.
"""
@@ -1042,6 +1050,42 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
if cached_ic is not None:
cached_ic.pop(external_req_id, None)
def release_shm_resources(self, request_id: str) -> None:
"""Queue reclaim of inter-stage segments this request left unconsumed.
Safe only once every stage has finished with the request: the producer
keeps writing until its own request ends, and a consumer that stopped
early never drains the remainder. Successful terminal chunks are kept
by ``_queue_shm_cleanup_locked`` for exactly that consumer, so the
orchestrator -- the only party that knows all stages are done -- drives
this (see ``Orchestrator._cleanup_request_ids``).
Like abort cleanup, the unlink runs on the save thread, so the
scheduler never performs SHM I/O.
"""
if not isinstance(self.connector, SharedMemoryConnector):
return
external_req_id = self.request_ids_mapping.get(request_id, request_id)
self._pending_save_reqs.append({"release_shm": f"{external_req_id}_{self.connector.stage_id}_"})
with self._save_cond:
self._save_cond.notify()
def _release_shm_prefix(self, key_prefix: str) -> None:
reclaimed = self.connector.cleanup_prefix(key_prefix)
if not reclaimed:
return
# Non-zero means a consumer stopped before draining the producer.
# Common on audio requests, so warn only when the running total
# crosses another _RECLAIM_WARN_INTERVAL boundary.
prev = self._reclaimed_shm_total
self._reclaimed_shm_total = prev + reclaimed
if prev // _RECLAIM_WARN_INTERVAL != self._reclaimed_shm_total // _RECLAIM_WARN_INTERVAL:
logger.warning(
"Reclaimed %d unconsumed inter-stage segments so far; "
"a downstream stage finished before draining its producer",
self._reclaimed_shm_total,
)
def cleanup(
self,
request_id: str,
+3
View File
@@ -198,6 +198,7 @@ class OmniEngineArgs(EngineArgs):
async_chunk: bool = False
session_mode: str = "turn"
retains_state_across_chunks: bool = False
supports_running_prefix_cache_reset: bool = True
use_v2_model_runner: bool = False
supports_native_mrv2_data_plane: bool = False
# WS-A: Stage-1 active stream slots. 0 = legacy preempt-everything.
@@ -432,6 +433,7 @@ class OmniEngineArgs(EngineArgs):
async_chunk=self.async_chunk,
session_mode=self.session_mode,
retains_state_across_chunks=self.retains_state_across_chunks,
supports_running_prefix_cache_reset=self.supports_running_prefix_cache_reset,
use_v2_model_runner=self.use_v2_model_runner,
supports_native_mrv2_data_plane=self.supports_native_mrv2_data_plane,
active_stream_window=self.active_stream_window,
@@ -560,6 +562,7 @@ class OrchestratorArgs:
# === Diffusion model config ===
num_gpus: int | None = None
model_class_name: str | None = None
hsdp_weight_load_strategy: str | None = None
diffusion_load_format: str | None = None
lora_path: list[str] | None = None
lora_backend: str | None = None
+36 -2
View File
@@ -301,6 +301,7 @@ class OrchestratorBase:
_transfer_emitter: Any = None
_prom_metrics: Any = None
_stat_logger: OmniPrometheusStatLogger | None = None
_transfer_release_tasks: set[asyncio.Task] = set()
def __init__(
self,
@@ -348,6 +349,8 @@ class OrchestratorBase:
self._pd_bootstrap_addr = pd_config.get("bootstrap_addr")
self._pd_prefill_engine_id = pd_config.get("prefill_engine_id")
self.request_states: dict[str, OrchestratorRequestState] = {}
# Strong refs for in-flight releases; the loop only weak-refs tasks, so dropping these risks mid-flight GC.
self._transfer_release_tasks: set[asyncio.Task] = set()
self._init_metrics_state(
stage_pools,
running_counter,
@@ -690,6 +693,36 @@ class OrchestratorBase:
output_msg.finished = index == last_index_by_req[output_msg.request_id]
return abort_outputs
def _release_stage_transfer_resources(self, request_ids: list[str]) -> None:
"""Drop each stage's inter-stage transfer resources for finished requests.
This is the only point that knows every stage is done with the request,
so it is the only safe place to reclaim segments a consumer never
drained. Scheduled rather than awaited: reclaim is best-effort and must
not add RPC latency to request teardown.
"""
if not request_ids:
return
async def _run() -> None:
results = await asyncio.gather(
*(pool.release_request_resources(request_ids) for pool in self.stage_pools),
return_exceptions=True,
)
for result in results:
if isinstance(result, Exception):
logger.warning("[Orchestrator] release transfer resources failed: %s", result)
try:
task = asyncio.get_running_loop().create_task(_run())
self._transfer_release_tasks.add(task)
task.add_done_callback(self._transfer_release_tasks.discard)
except RuntimeError:
logger.warning(
"[Orchestrator] no running event loop; skipped reclaim of transfer resources for %s",
request_ids,
)
def _release_request_bindings(self, request_ids: list[str]) -> None:
"""Release all stage-local route bindings for the given request ids."""
for pool in self.stage_pools:
@@ -898,7 +931,7 @@ class OrchestratorBase:
req_state is None
or req_state.upstream_first_audio
or req_state.pending_upstream_first_audio is not None
or stage_id + 1 > req_state.final_stage_id
or (stage_id if final_output else stage_id + 1) > req_state.final_stage_id
):
continue
audio = mm.get("model_outputs")
@@ -935,7 +968,7 @@ class OrchestratorBase:
if pending is None or self.request_states.get(req_state.request_id) is not req_state:
return
source_stage, first_output = pending
codec_stage = source_stage + 1
codec_stage = source_stage if self.stage_pools[source_stage].final_output else source_stage + 1
pool = self.stage_pools[codec_stage]
replica_id = pool.get_bound_replica_id(req_state.request_id)
if replica_id is None or req_state.request_id not in pool.output_processor.request_states:
@@ -1645,6 +1678,7 @@ class OrchestratorBase:
if abort:
abort_outputs = await self._abort_request_ids(cleanup_ids)
self._release_request_bindings(cleanup_ids)
self._release_stage_transfer_resources(cleanup_ids)
for request_id in cleanup_ids:
self._pd_kv_params.pop(request_id, None)
req_state = self.request_states.pop(request_id, None)
+37 -5
View File
@@ -81,19 +81,36 @@ def _signal_exit_code(signum: int) -> int:
def _bind_first_audio_sink(model_executor: Any, output_queue: Any, scheduler: Any) -> bool:
"""Let a TP1 in-process runner that decodes first audio deliver it as its own output."""
"""Bind in-process first audio to the generic engine output sink."""
if not isinstance(model_executor, UniProcExecutor):
return False
parallel_config = model_executor.vllm_config.parallel_config
if parallel_config.tensor_parallel_size != 1 or parallel_config.pipeline_parallel_size != 1:
return False
worker = getattr(getattr(model_executor, "driver_worker", None), "worker", None)
model_runner = getattr(worker, "model_runner", None)
model_state = getattr(model_runner, "model_state", None)
model = getattr(model_runner, "model", None)
if getattr(model, "first_frame_decoder", None) is None or not hasattr(model_state, "set_first_audio_sink"):
return False
decodes_audio = getattr(model, "first_frame_decoder", None) is not None or (
getattr(model, "stream_decoder", None) is not None and bool(getattr(model, "stream_first_audio", False))
)
from vllm_omni.worker_v2.first_audio_sender import engine_output_queue_sink
assert model_state is not None
model_state.set_first_audio_sink(engine_output_queue_sink(output_queue, scheduler))
if decodes_audio and hasattr(model_state, "set_first_audio_sink"):
assert model_state is not None
model_state.set_first_audio_sink(engine_output_queue_sink(output_queue, scheduler))
return True
data_plane = getattr(model_runner, "_omni_data_plane", None)
get_model = getattr(model_runner, "get_model", None)
if data_plane is None or not callable(get_model):
return False
bind = getattr(get_model(), "bind_first_chunk_fast_path", None)
if not callable(bind):
return False
hook = bind(engine_output_queue_sink(output_queue, scheduler, upstream_first_audio=False))
if hook is None:
return False
data_plane.set_first_chunk_hook(hook)
return True
@@ -129,6 +146,21 @@ class StageEngineCoreProc(EngineCoreProc):
if _bind_native_data_plane_ready_sink(self.model_executor, self.scheduler):
logger.info("Bound native MRv2 connector readiness directly to the scheduler inbox.")
def omni_release_request_resources(self, request_ids: list[str]) -> None:
"""Release this stage's inter-stage transfer resources for *request_ids*.
Invoked over the UTILITY channel by the orchestrator once every stage
has finished with the request. Idempotent and safe for unknown ids.
"""
adapter = getattr(getattr(self, "scheduler", None), "chunk_transfer_adapter", None)
if adapter is None:
return
for request_id in request_ids or ():
try:
adapter.release_shm_resources(request_id)
except Exception as e:
logger.debug("omni_release_request_resources(%s) failed: %s", request_id, e)
def preprocess_add_request(self, request: OmniEngineCoreRequest) -> tuple[Any, int]:
"""Preserve omni payloads when vLLM builds its scheduler request."""
scheduler_request, current_wave = super().preprocess_add_request(request)
+27
View File
@@ -99,6 +99,8 @@ class StagePool:
DISPATCH_WAIT_TIMEOUT_S: float = 10.0
DISPATCH_RETRY_INTERVAL_S: float = 0.1
# A replica that dies mid-release never answers; without a bound its background release never finishes.
RELEASE_RPC_TIMEOUT_S: float = 5.0
# Only these EngineCore helpers may skip collective_rpc_async. A generic
# ``{method}_async`` on AsyncMPClient must not silently drop timeout.
_CACHE_RESET_METHODS = frozenset({"reset_prefix_cache", "reset_encoder_cache", "reset_mm_cache"})
@@ -136,6 +138,9 @@ class StagePool:
self.clients: list[StagePoolClient | None] = list(normalized_clients)
self._output_processor = output_processor
self._stage_vllm_config = stage_vllm_config
self._has_chunk_transfer_adapter = bool(
getattr(getattr(stage_vllm_config, "model_config", None), "async_chunk", False)
)
self._next_replica_id = 0
self._request_bindings: dict[str, int] = {}
self._unavailable_replicas: set[int] = set()
@@ -1305,6 +1310,28 @@ class StagePool:
return abort_outputs
async def release_request_resources(self, request_ids: list[str]) -> None:
"""Ask every live replica to drop transfer resources for *request_ids*.
Broadcast rather than binding-routed: the orchestrator releases route
bindings as part of the same teardown, so a binding lookup here would
race it. The engine-core handler is idempotent for unknown ids.
"""
if not request_ids or not self._has_chunk_transfer_adapter:
return
ids = list(request_ids)
async def release(replica_id: int, call: Any) -> None:
try:
await asyncio.wait_for(call("omni_release_request_resources", ids), timeout=self.RELEASE_RPC_TIMEOUT_S)
except Exception as e:
logger.warning(
"[StagePool-%s] release_request_resources on replica %s failed: %r", self.stage_id, replica_id, e
)
calls = [(i, getattr(self.clients[i], "call_utility_async", None)) for i in self.live_replica_ids()]
await asyncio.gather(*(release(i, call) for i, call in calls if call is not None))
async def collective_rpc(
self,
replica_id: int,
+4
View File
@@ -130,6 +130,7 @@ class AsyncOmni(AsyncOmniBase, EngineClient):
reasoning_ended: bool | None = None,
reasoning_parser_kwargs: dict[str, Any] | None = None,
arrival_time: float | None = None,
submitted: asyncio.Future[None] | None = None,
) -> AsyncGenerator[OmniRequestOutput, None]:
"""Generate outputs for the given prompt(s) asynchronously.
@@ -300,6 +301,9 @@ class AsyncOmni(AsyncOmniBase, EngineClient):
if admitting:
await self._release_generate_admission()
admitting = False
# Callers wait on this before abort so Stage0 observes the sender cache.
if submitted is not None and not submitted.done():
submitted.set_result(None)
# Refresh gauges on arrival.
self._publish_request_gauges(len(self.request_states))
+9
View File
@@ -560,6 +560,15 @@ class OmniServeCommand(CLISubcommand):
"custom_pipeline, dummy, or diffusers for the HF diffusers adapter."
),
)
omni_config_group.add_argument(
"--hsdp-weight-load-strategy",
choices=["full", "pre_sharded"],
default=None,
help=(
"HSDP checkpoint loading strategy: 'full' loads then shards; "
"'pre_sharded' shards meta parameters first and reads rank-local HF safetensors slices."
),
)
omni_config_group.add_argument(
"--lora-path",
type=str,
+1 -1
View File
@@ -441,7 +441,7 @@ class OmniBase(PDDisaggregationMixin):
if self.log_stats:
# Emit per-request orchestrator timing (including e2e_total_ms)
# before dropping request state.
req_state.metrics.build_and_log_summary()
req_state.metrics.log_timing_summary()
except Exception:
logger.exception(
"[%s] Failed to build/log summary for req=%s",
+29 -1
View File
@@ -249,6 +249,33 @@ async def omni_run_server(args, **uvicorn_kwargs) -> None:
await omni_run_server_worker(listen_address, sock, args, **uvicorn_kwargs)
def _own_reuseport_socket(sock: socket.socket, client_count: int) -> socket.socket:
"""Give this API server its own SO_REUSEPORT listener on the shared address.
All API servers accepting from the one inherited socket lets whichever
wakes first take a whole burst of connections (the event loop accepts
in a loop), and keep-alive then pins that imbalance for the connections'
lifetime. Separate reuseport listeners let the kernel spread connections
by hash. The inherited socket is bound but never listened on, so it
receives nothing.
"""
if client_count <= 1:
return sock
if sock.family not in (socket.AF_INET, socket.AF_INET6):
return sock
try:
if not sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT):
return sock
own = socket.socket(family=sock.family, type=socket.SOCK_STREAM)
own.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
own.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
own.bind(sock.getsockname()[:2])
except OSError as e:
logger.warning("Per-server reuseport listener unavailable (%s); sharing the inherited socket", e)
return sock
return own
def run_omni_api_server_worker_proc(
listen_address: str,
sock: socket.socket,
@@ -278,6 +305,7 @@ def run_omni_api_server_worker_proc(
set_process_title("APIServer", str(client_index))
decorate_logs("APIServer", skip_if_decorated=True)
sock = _own_reuseport_socket(sock, int(omni_client_config["client_count"]))
uvloop.run(
omni_run_server_worker(
listen_address,
@@ -512,7 +540,7 @@ async def build_async_omni(
# Ensures everything is shutdown and cleaned up on error/exit
async with build_async_omni_from_stage_config(
args,
disable_frontend_multiprocessing=disable_frontend_multiprocessing,
disable_frontend_multiprocessing=bool(disable_frontend_multiprocessing),
client_config=client_config,
) as async_omni:
yield async_omni
@@ -46,6 +46,15 @@ except ImportError:
logger = init_logger(__name__)
def _float32_to_pcm16_bytes(audio: np.ndarray) -> bytes:
"""PCM_16 bytes of float32 samples, bit-identical to libsndfile's RAW writer.
libsndfile (1.2) converts with ``floor(x * 0x8000)`` clipped to int16.
"""
scaled = np.floor(audio.astype(np.float64) * 32768.0)
return np.clip(scaled, -32768, 32767).astype("<i2").tobytes()
class AudioMixin:
"""Mixin class to add audio-related utilities."""
@@ -93,9 +102,15 @@ class AudioMixin:
soundfile_format, media_type, kwargs = supported_formats[response_format]
with BytesIO() as buffer:
soundfile.write(buffer, audio_tensor, sample_rate, format=soundfile_format, **kwargs)
audio_data = buffer.getvalue()
audio_data: bytes | str
if response_format == "pcm" and isinstance(audio_tensor, np.ndarray) and audio_tensor.dtype == np.float32:
# Through BytesIO, soundfile makes a Python callback per block;
# streamed chunks made that the API workers' largest cost.
audio_data = _float32_to_pcm16_bytes(audio_tensor)
else:
with BytesIO() as buffer:
soundfile.write(buffer, audio_tensor, sample_rate, format=soundfile_format, **kwargs)
audio_data = buffer.getvalue()
if base64_encode:
import base64
@@ -2275,6 +2275,22 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
if ts is not None:
collect["word_timestamps"] = ts
# Let the adapter fold engine-side metadata (e.g. YuE2's
# meta.truncated) into ``collect`` for response headers.
if collect is not None and (adapter := self._get_tts_adapter()) is not None:
adapter.collect_response_metadata(audio_output, collect)
# A model can flag a per-request synthesis failure through the
# adapter (e.g. YuE2's terminal NAR/VAE pass OOMing on one
# request); answer 500 instead of shipping a zero-length WAV.
# Raising (not returning a Response) keeps this function's
# tuple contract; create_speech maps TTSGenerationError to 500.
if collect is not None and collect.get("audio_synthesis_error"):
raise TTSGenerationError(
"The model failed to synthesize audio for this request",
retryable=False,
)
audio_tensor = audio_output[audio_key]
sr_raw = audio_output.get("sr", 24000)
sr_val = sr_raw[-1] if isinstance(sr_raw, list) and sr_raw else sr_raw
@@ -2715,6 +2731,8 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
"(use the WebSocket streaming path for long transcripts)",
len(ts_json),
)
if collect.get("audio_truncated") is not None:
headers["X-Audio-Truncated"] = "true" if collect["audio_truncated"] else "false"
return Response(content=audio_bytes, media_type=media_type, headers=headers)
except asyncio.CancelledError:
@@ -66,29 +66,10 @@ class QwenOmniStreamingVideoHandler(OmniStreamingVideoHandlerBase):
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
prewarmed = prewarmed_frames or {}
if frame_indices is None:
frame_indices = self._sample_frame_indices(frame_buffer, config.num_frames, prewarmed)
user_content: list[dict] = []
for index in frame_indices:
frame_b64 = frame_buffer[index]
cached = prewarmed.get(frame_b64)
if cached is not None:
# The shared selector excludes failed decodes before this point.
assert isinstance(cached, tuple)
pil, pil_uuid = cached
user_content.append(
{
"type": "image_pil",
"image_pil": pil,
"uuid": pil_uuid,
}
)
else:
user_content.append(
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{frame_b64}"},
}
)
frame_indices = self._select_frame_indices(config, frame_buffer, prewarmed)
frames = [frame_buffer[index] for index in frame_indices]
user_content: list[dict] = self._build_frame_image_parts(frames, prewarmed)
if len(audio_buffer) > 0:
wav_b64 = self._pcm_to_wav_b64(bytes(audio_buffer))
@@ -107,14 +88,7 @@ class QwenOmniStreamingVideoHandler(OmniStreamingVideoHandlerBase):
user_message: dict[str, Any] = {"role": "user", "content": user_content}
messages: list[dict[str, Any]] = []
if config.system_prompt:
messages.append({"role": "system", "content": config.system_prompt})
recent_history = message_history[-2:] if len(message_history) > 2 else message_history
for hist_msg in recent_history:
messages.append(self._text_only_message(hist_msg))
messages = self._history_prefix_messages(config, message_history)
messages.append(user_message)
return messages, user_message
@@ -124,6 +124,7 @@ from vllm_omni.entrypoints.openai.tts_adapters import ( # noqa: E402,F401
step_audio2,
voxcpm2,
voxtral,
yue2,
)
__all__ = [
@@ -81,13 +81,24 @@ def conditioning_cache_salt(
This replaces the uploaded data URI in the hash without changing the request.
"""
h = hashlib.sha256()
ref_audio: str | list[str] | tuple[str, int] | None = request.ref_audio
if (
isinstance(ref_audio, str)
and len(ref_audio) > 256
and tts_params is not None
and tts_params.get("ref_audio_cache_key") is not None
):
# The resolve key below is already a content-aware digest of this
# locator. Hashing a repr of a large inline data URI again dominates
# the per-request serving cost without adding information.
ref_audio = ("ref_audio_cache_key", len(ref_audio))
for part in (
request.input,
request.task_type,
request.language,
request.voice,
request.ref_text,
registered_voice if registered_voice is not None else request.ref_audio,
registered_voice if registered_voice is not None else ref_audio,
request.instructions,
request.x_vector_only_mode,
request.speaker_embedding,
@@ -320,6 +331,15 @@ class TTSModelAdapter(ABC):
to unrelated TTS models.
"""
def collect_response_metadata(self, audio_output: Mapping[str, Any], collect: dict) -> None:
"""Fold engine-side metadata from the mm payload into ``collect``.
Non-streaming only. The orchestrator surfaces select ``collect``
entries as response headers (precedent: ``word_timestamps`` ->
``X-Word-Timestamps``, ``audio_truncated`` -> ``X-Audio-Truncated``);
adapters opt in by setting the keys it knows. Default: no-op.
"""
async def warmup(self) -> None:
return
@@ -32,6 +32,59 @@ if TYPE_CHECKING:
logger = init_logger(__name__)
def _local_continuation_prompt(proc: Any, user_kwargs: dict[str, Any], reference: Any) -> tuple[list[int], Any] | None:
"""Build the Local-v1.5 continuation prompt without the generic processor call.
Mirrors ``_build_continuation_codes`` of the model's processor: every
template segment is encoded separately there, so the constant segments
(keyed by ``language``) are cached and only the request text is
tokenized. Returns ``(text_ids, audio_codes)`` equal to
``unified[:, 0].tolist()`` / ``unified[:, 1:]``, or ``None`` when the
request or processor does not match that exact layout.
"""
import torch
if set(user_kwargs) - {"text", "language"}:
return None
if not isinstance(reference, list) or len(reference) != 1 or not isinstance(reference[0], torch.Tensor):
return None
cfg = getattr(proc, "model_config", None)
n_vq = getattr(cfg, "n_vq", None)
if cfg is None or not isinstance(n_vq, int) or n_vq <= 0:
return None
codes = reference[0]
if codes.ndim != 2 or int(codes.shape[1]) != n_vq or codes.shape[0] == 0:
return None
required = (
"_user_prompt_prefix_ids",
"_encode_text",
"_user_prompt_after_reference_ids",
"_assistant_prompt_prefix_ids",
)
if not all(callable(getattr(proc, name, None)) for name in required):
return None
language = user_kwargs.get("language")
cache = proc.__dict__.setdefault("_vllm_omni_continuation_segments", {})
segments = cache.get(language)
if segments is None:
fields = {} if language is None else {"language": language}
head = (
proc._user_prompt_prefix_ids()
+ proc._encode_text("None")
+ proc._user_prompt_after_reference_ids(language, fields)
)
tail = proc._assistant_prompt_prefix_ids() + [int(cfg.audio_start_token_id)]
segments = cache[language] = ([int(t) for t in head], [int(t) for t in tail])
head, tail = segments
text = user_kwargs.get("text")
prompt_ids = head + [int(t) for t in proc._encode_text("" if text is None else str(text))] + tail
num_audio = int(codes.shape[0])
text_ids = prompt_ids + [int(cfg.audio_assistant_slot_token_id)] * num_audio
audio_codes = torch.full((len(text_ids), n_vq), int(cfg.audio_pad_token_id), dtype=torch.int64)
audio_codes[len(prompt_ids) :] = codes.to(dtype=torch.int64, device="cpu")
return text_ids, audio_codes
class _MossTTSAdapterBase(ARTTSAdapter):
accumulate_nonstreaming: bool = False
@@ -82,6 +135,28 @@ class _MossTTSAdapterBase(ARTTSAdapter):
self._moss_ref_encoder = encoder
return encoder
async def warmup(self) -> None:
"""Set up the reference encoder at startup, not on the first cloning request.
Its CUDA graph capture takes seconds per API process (about a minute for
a shared encoder's workers).
"""
if self._moss_variant not in ("tts", "local", "ttsd"):
return
from vllm_omni.model_executor.models.moss_tts.reference_encoder import reference_graphs_enabled
from vllm_omni.model_executor.models.moss_tts.shared_reference_encoder import SharedReferenceEncoderStartupError
if not reference_graphs_enabled():
return
try:
await asyncio.to_thread(self._get_moss_ref_encoder().prepare)
except SharedReferenceEncoderStartupError:
# Do not advertise readiness and trigger concurrent local captures
# while the shared host is still compiling or has failed startup.
raise
except Exception: # noqa: BLE001 - requests still encode (and capture) on demand
logger.warning("MOSS reference encoder warmup failed", exc_info=True)
async def _encode_moss_references(
self,
request: "OpenAICreateSpeechRequest",
@@ -269,8 +344,16 @@ class _MossTTSAdapterBase(ARTTSAdapter):
proc = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
if hasattr(proc, "audio_tokenizer"):
device = self._resolve_ref_encoder_device()
proc.audio_tokenizer = proc.audio_tokenizer.to(device).eval()
logger.info("MOSS reference-audio encoder (audio_tokenizer) placed on %s", device)
proc._vllm_omni_ref_encoder_device = device
from vllm_omni.model_executor.models.moss_tts.reference_encoder import shared_encoder_role
if shared_encoder_role() == "client":
# Another API process hosts the shared encoder on the GPU.
proc.audio_tokenizer = proc.audio_tokenizer.eval()
logger.info("MOSS reference-audio encoder: using the shared encoder of another API process")
else:
proc.audio_tokenizer = proc.audio_tokenizer.to(device).eval()
logger.info("MOSS reference-audio encoder (audio_tokenizer) placed on %s", device)
self._moss_processor_cache = proc
return proc
@@ -405,18 +488,24 @@ class _MossTTSAdapterBase(ARTTSAdapter):
# Build the unified-codes prompt: (L, 1+n_vq) where col 0 is text/special
# tokens and cols 1..n_vq are the delay-pattern audio code grid (mostly
# audio_pad_code outside the reference block).
fast = None
if v == "local" and request.ref_text and request.ref_text.strip():
reference = user_kwargs.pop("reference")
user_kwargs["text"] = request.ref_text.strip() + " " + (request.input or "")
user_msg = proc.build_user_message(**user_kwargs)
assistant_msg = proc.build_assistant_message(audio_codes_list=reference)
batch = proc(conversations=[[user_msg, assistant_msg]], mode="continuation")
fast = _local_continuation_prompt(proc, user_kwargs, reference)
if fast is None:
user_msg = proc.build_user_message(**user_kwargs)
assistant_msg = proc.build_assistant_message(audio_codes_list=reference)
batch = proc(conversations=[[user_msg, assistant_msg]], mode="continuation")
else:
user_msg = proc.build_user_message(**user_kwargs)
batch = proc(conversations=[[user_msg]], mode="generation")
unified = batch["input_ids"][0] # torch.LongTensor (L, 1+n_vq)
text_ids: list[int] = unified[:, 0].tolist()
audio_codes: torch.Tensor = unified[:, 1:].contiguous().to(torch.int64)
if fast is not None:
text_ids, audio_codes = fast
else:
unified = batch["input_ids"][0] # torch.LongTensor (L, 1+n_vq)
text_ids = unified[:, 0].tolist()
audio_codes = unified[:, 1:].contiguous().to(torch.int64)
params = {
"prompt_token_ids": text_ids,
@@ -397,34 +397,60 @@ class Qwen3TTSAdapter(ARTTSAdapter):
return params
def _load_text_tokenizer(self) -> None:
server = self.ctx.server
if server._tts_tokenizer is not None:
return
import transformers
from transformers import AutoTokenizer
# Same construction as the engine's text tokenizer
# (Qwen3TTSPromptEmbedsBuilder.get_text_tokenizer), so the ids computed
# here can be handed to the engine.
kwargs: dict[str, Any] = dict(trust_remote_code=True, use_fast=True, padding_side="left")
if transformers.__version__ < "5":
kwargs["fix_mistral_regex"] = True
assert self.ctx.engine_client is not None
model_name = self.ctx.engine_client.model_config.model
server._tts_tokenizer = AutoTokenizer.from_pretrained(model_name, **kwargs)
def _estimate_prompt_len(self, tts_params: dict[str, Any]) -> int:
"""Estimate prompt length so the placeholder matches model-side embeddings."""
try:
from vllm_omni.model_executor.models.qwen3_tts.prompt_embeds_builder import (
PRECOMPUTED_TEXT_IDS_KEY,
Qwen3TTSPromptEmbedsBuilder,
build_assistant_text,
)
server = self.ctx.server
if server._tts_tokenizer is None:
from transformers import AutoTokenizer
model_name = self.ctx.engine_client.model_config.model
server._tts_tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=True,
padding_side="left",
)
self._load_text_tokenizer()
assert self.ctx.engine_client is not None
hf_config = self.ctx.engine_client.model_config.hf_config
talker_config = hf_config.talker_config
task_type = (tts_params.get("task_type") or ["CustomVoice"])[0]
return Qwen3TTSPromptEmbedsBuilder.estimate_prompt_len_from_additional_information(
text = (tts_params.get("text") or [""])[0]
assistant_text = build_assistant_text(text) if isinstance(text, str) else None
assistant_ids: list[list[int]] = []
def tokenize_prompt(t: str) -> list[int]:
ids = server._tts_tokenizer(t, padding=False)["input_ids"]
if t == assistant_text:
assistant_ids.append(ids)
return ids
prompt_len = Qwen3TTSPromptEmbedsBuilder.estimate_prompt_len_from_additional_information(
additional_information=tts_params,
task_type=task_type,
tokenize_prompt=lambda t: server._tts_tokenizer(t, padding=False)["input_ids"],
tokenize_prompt=tokenize_prompt,
codec_language_id=getattr(talker_config, "codec_language_id", None),
spk_is_dialect=getattr(talker_config, "spk_is_dialect", None),
estimate_ref_code_len=self._estimate_ref_code_len,
)
if assistant_ids:
# Spares the engine tokenizing the same text on its step thread.
tts_params[PRECOMPUTED_TEXT_IDS_KEY] = [assistant_ids[0]]
return prompt_len
except Exception as e:
logger.warning("Failed to estimate TTS prompt length, using fallback 2048: %s", e)
return 2048
@@ -0,0 +1,276 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""YuE2-3B serving adapter for ``/v1/audio/speech``.
This is a text-to-music model on a speech endpoint, so most of the speech
contract does not apply. ``input`` carries the lyrics and ``instructions`` the
style caption (genre, instrumentation, tempo, mood). There is no speaker to
select, no reference audio, and no sampling knobs: the request-local preset is
pinned by the checkpoint's reference config. Model-specific controls travel in
``extra_params``:
- ``cot``: ``"off"`` (default) | ``"melody"`` | ``"full"`` — how the ABC
score span in the prompt is handled.
- ``abc``: an ABC score as text, required for ``cot=melody|full``. Serving v1
does not run the model-generated ABC phase (that is two sequential engine
requests; use the offline example for it).
Unsupported parameters are rejected rather than silently ignored, following
the MiniMax Music 3 precedent.
"""
import secrets
from collections.abc import Mapping
from pathlib import Path
from typing import TYPE_CHECKING, Any
import torch
from vllm_omni.entrypoints.openai.tts_adapters import register_tts_adapter
from vllm_omni.entrypoints.openai.tts_adapters.base import (
ARTTSAdapter,
PreparedRequest,
resolve_stage_model_path,
)
from vllm_omni.model_executor.models.yue2.yue2 import (
CONTEXT,
FRAMES_PER_SECOND,
KEY_MAX_AUDIO_FRAMES,
KEY_MAX_HOLD_STEPS,
KEY_MIN_TOKENS,
KEY_PENALTY_WINDOW,
KEY_PHASE,
KEY_PREFIX_IDS,
KEY_REPETITION_PENALTY,
KEY_SEED,
KEY_SKIP_SYNTHESIS,
KEY_TEMPERATURE,
KEY_TOP_K,
KEY_TOP_P,
SEMANTIC_SAMPLING,
STOP_TOKEN_IDS,
SYNTHESIS_HOLD_STEPS,
)
from vllm_omni.tokenizers.yue2_prompt import semantic_prefix_ids
if TYPE_CHECKING:
from vllm_omni.entrypoints.openai.protocol.audio import OpenAICreateSpeechRequest
_COT_MODES = ("off", "melody", "full")
# Without max_new_tokens the budget is the preset ceiling, so the model's
# own end token sets the song length (upstream reference behavior).
_DEFAULT_MAX_FRAMES = int(SEMANTIC_SAMPLING["max_tokens"])
# Sampling is fixed by the checkpoint's reference preset. Accepting these
# would imply a control the model does not honor.
_UNSUPPORTED_SAMPLING_PARAMS = ("temperature", "top_p", "top_k", "repetition_penalty")
@register_tts_adapter
class Yue2Adapter(ARTTSAdapter):
"""Lyrics plus a style caption in, one 48 kHz stereo song out."""
name = "yue2"
stage_keys = frozenset({"yue2"})
model_archs = frozenset({"Yue2ForCausalLM"})
max_new_tokens_min = int(SEMANTIC_SAMPLING["min_tokens"])
max_new_tokens_max = int(SEMANTIC_SAMPLING["max_tokens"])
def __init__(self, ctx: Any) -> None:
super().__init__(ctx)
self._cached_tokenizer: Any = None
def validate(self, request: "OpenAICreateSpeechRequest") -> str | None:
if not request.input or not request.input.strip():
return "YuE2 requires non-empty 'input' carrying the lyrics"
instructions = request.instructions
if not isinstance(instructions, str) or not instructions.strip():
return (
"YuE2 requires 'instructions' describing the music "
"(genre, instrumentation, tempo, mood); it is what decides the arrangement"
)
extra: dict[str, Any] = request.extra_params or {}
cot = extra.get("cot", "off")
if cot not in _COT_MODES:
return f"extra_params.cot must be one of {_COT_MODES}; got {cot!r}"
abc = extra.get("abc")
if cot == "off":
if abc is not None:
return "extra_params.abc requires cot=melody|full"
elif not isinstance(abc, str) or not abc.strip():
return (
f"cot={cot} requires extra_params.abc carrying an ABC score. "
"Model-generated ABC is two engine requests and is not served "
"in v1; use examples/offline_inference/yue2 for that."
)
if request.max_new_tokens is not None:
if request.max_new_tokens < self.max_new_tokens_min:
return f"'max_new_tokens' must be at least {self.max_new_tokens_min}"
if request.max_new_tokens > self.max_new_tokens_max:
return (
f"'max_new_tokens' counts audio frames at {FRAMES_PER_SECOND} per second "
f"and cannot exceed {self.max_new_tokens_max}"
)
voice = (request.voice or "").strip().lower()
if voice not in ("", "default"):
return (
"YuE2 has no speaker to select; the vocal comes from "
f"'instructions'. Omit 'voice' or pass 'default'; got {request.voice!r}"
)
if request.ref_audio is not None or request.ref_text is not None:
return "YuE2 does not support reference-audio conditioning"
if request.language is not None:
return "YuE2 has no language tag; the language follows the lyrics"
if request.task_type is not None:
return "YuE2 does not support 'task_type'"
if request.speed is not None and float(request.speed) != 1.0:
return "YuE2 only supports speed=1.0; put the tempo in 'instructions', e.g. 'at 120 BPM'"
if request.is_streaming():
return (
"YuE2 does not support streaming: the song is decoded in one "
"terminal NAR/VAE pass after AR decoding finishes. Use stream=false."
)
rejected = sorted(key for key in _UNSUPPORTED_SAMPLING_PARAMS if extra.get(key) is not None)
if rejected:
return "YuE2 has fixed sampling and does not accept: " + ", ".join(rejected)
return None
async def warmup(self) -> None:
# Resolve (and possibly download) qwen.tiktoken at startup: build()
# runs on the request path and must not block on a hub download, and a
# bad path should surface before the first request.
self._tokenizer()
async def build(
self,
request: "OpenAICreateSpeechRequest",
sampling_params_list: list,
has_inline_ref_audio: bool,
) -> PreparedRequest:
del sampling_params_list, has_inline_ref_audio
extra: dict[str, Any] = request.extra_params or {}
cot = extra.get("cot", "off")
encode = self._tokenizer().encode
abc_ids = encode(extra["abc"]) if cot != "off" else None
# validate() has already rejected blank lyrics/caption.
instructions = request.instructions or ""
lyrics = request.input or ""
prefix = semantic_prefix_ids(encode, instructions, lyrics, cot, abc_ids=abc_ids)
max_frames = int(request.max_new_tokens or _DEFAULT_MAX_FRAMES)
# +1: the song is delivered on the step after its last frame (the
# model acts on its draws one step late), which also needs a slot.
if len(prefix) + max_frames + 1 > CONTEXT:
return_err = (
f"YuE2 prompt is {len(prefix)} tokens and the {max_frames}-frame budget "
f"exceeds the {CONTEXT}-token context. Shorten the lyrics/style or "
"lower max_new_tokens."
)
raise ValueError(return_err)
return PreparedRequest(
prompt={"prompt_token_ids": prefix},
tts_params={"max_audio_frames": [max_frames]},
model_type=self.name,
)
def apply_sampling_overrides(
self,
sampling_params_list: list,
request: "OpenAICreateSpeechRequest",
prompt: dict[str, Any] | None = None,
request_id: str | None = None,
) -> list:
import copy
sampling_params_list = copy.deepcopy(sampling_params_list)
params = sampling_params_list[0]
max_frames = int(request.max_new_tokens or _DEFAULT_MAX_FRAMES)
# A service must not give every caller the same song; without an
# explicit seed, draw one per request.
seed = request.seed if request.seed is not None else secrets.randbelow(2**31)
assert prompt is not None # the engine always passes the scheduled prompt
params.extra_args = {
**(params.extra_args or {}),
KEY_PHASE: "semantic",
KEY_SEED: seed,
KEY_TEMPERATURE: SEMANTIC_SAMPLING["temperature"],
KEY_TOP_P: SEMANTIC_SAMPLING["top_p"],
KEY_TOP_K: SEMANTIC_SAMPLING["top_k"],
KEY_REPETITION_PENALTY: SEMANTIC_SAMPLING["repetition_penalty"],
KEY_PENALTY_WINDOW: SEMANTIC_SAMPLING["penalty_window"],
KEY_MIN_TOKENS: SEMANTIC_SAMPLING["min_tokens"],
KEY_MAX_AUDIO_FRAMES: max_frames,
KEY_SKIP_SYNTHESIS: False,
# Full prompt ids: under a KV prefix-cache hit the engine schedules
# only the uncached tail, so the model cannot rebuild its NAR
# conditioning prefix from the scheduled tokens alone.
KEY_PREFIX_IDS: list(prompt["prompt_token_ids"]),
}
# The model needs one decode step beyond the last frame to deliver the
# terminal NAR/VAE pass, so do not cap the engine at exactly max_frames.
# Synthesize on a side stream while other songs keep decoding; the
# request may emit up to SYNTHESIS_HOLD_STEPS hold tokens meanwhile
# (see the model), so max_tokens leaves that much headroom.
prompt_len = len(prompt["prompt_token_ids"])
params.max_tokens = min(max_frames + 1 + SYNTHESIS_HOLD_STEPS, CONTEXT - prompt_len)
params.extra_args[KEY_MAX_HOLD_STEPS] = max(0, params.max_tokens - max_frames - 1)
params.stop_token_ids = list(STOP_TOKEN_IDS)
params.detokenize = False
return sampling_params_list
@staticmethod
def _last_meta_flag(audio_output: Mapping[str, Any], name: str) -> Any:
"""Read a per-request 0/1 meta flag in all three wire shapes.
The runner flattens nested meta.* to dotted keys (flatten_payload)
and the wire payload is tensor-only (_ensure_tensor_values), so by
serving time the flag is usually a 0-d "0"/"1" tensor under a dotted
key; accept the nested/offline shapes too.
"""
flags = audio_output.get(f"meta.{name}")
if flags is None:
meta = audio_output.get("meta")
if isinstance(meta, dict):
flags = meta.get(name)
if isinstance(flags, torch.Tensor):
flags = flags.reshape(-1)[-1].item() if flags.numel() else None
elif isinstance(flags, (list, tuple)):
flags = flags[-1] if flags else None
return flags
def collect_response_metadata(self, audio_output: Mapping[str, Any], collect: dict) -> None:
"""Surface meta.truncated / meta.error as response headers."""
truncated = self._last_meta_flag(audio_output, "truncated")
if truncated is not None:
collect["audio_truncated"] = truncated is True or str(truncated) == "1"
# meta.error=1 marks a failed NAR/VAE pass (empty clip); serving turns
# it into a 500 via this generic key. Absent/0 means success.
error = self._last_meta_flag(audio_output, "error")
if error is not None and (error is True or str(error) == "1"):
collect["audio_synthesis_error"] = True
def _tokenizer(self) -> Any:
"""The checkpoint-native tiktoken BPE, loaded once per process."""
if self._cached_tokenizer is None:
from vllm_omni.tokenizers.yue2_tokenizer import YuE2TextTokenizer
model_path = resolve_stage_model_path(self.ctx.engine_client)
if model_path is None:
raise RuntimeError("YuE2 tokenizer needs a resolvable stage model path")
merge_file = Path(model_path).expanduser() / "qwen.tiktoken"
if not merge_file.is_file():
# model_path is an HF repo id: pull just the merge file into
# the hub cache (vae.py does the same for the VAE snapshot).
from vllm_omni.transformers_utils.repo_utils import hf_api
merge_file = Path(hf_api().hf_hub_download(model_path, "qwen.tiktoken"))
self._cached_tokenizer = YuE2TextTokenizer(merge_file)
return self._cached_tokenizer
__all__ = ["Yue2Adapter"]
+599 -177
View File
@@ -38,7 +38,7 @@ import wave
from collections.abc import Mapping
from dataclasses import dataclass
from enum import Enum
from typing import Any, Final, Protocol, TypeAlias, runtime_checkable
from typing import Any, Final, Protocol, TypeAlias, TypeGuard, runtime_checkable
import torch
from fastapi import WebSocket, WebSocketDisconnect
@@ -77,6 +77,21 @@ def _decode_frame_bytes(raw_bytes: bytes) -> Any:
return Image.open(io.BytesIO(raw_bytes)).convert("RGB")
def _usable_prewarmed_frame(cached: PrewarmedFrame | None) -> TypeGuard[tuple[Any, str]]:
return cached is not None and cached is not _BAD_FRAME
def _stage0_prefix_caching_enabled(engine_client: Any) -> bool:
"""Read stage-0 (thinker) prefix caching from the engine. Called once
at handler init: incremental prefill is a server-side capability, not a
per-request choice."""
engine = getattr(engine_client, "engine", None)
stage_vllm_configs = getattr(engine, "stage_vllm_configs", None)
stage0 = stage_vllm_configs[0] if stage_vllm_configs else None
cache_config = getattr(stage0, "cache_config", None)
return bool(getattr(cache_config, "enable_prefix_caching", False))
@runtime_checkable
class VideoStreamPipelineHooks(Protocol):
"""Pipeline-specific hooks for streaming video handlers."""
@@ -108,6 +123,20 @@ class VideoStreamPipelineHooks(Protocol):
"""Update session state after a successful turn."""
...
def build_engine_prompt_prefix(
self,
config: "StreamingVideoSessionConfig",
frame_buffer: list[str],
message_history: list[dict[str, Any]],
prewarmed_frames: Mapping[str, PrewarmedFrame],
) -> list[dict[str, Any]] | None:
"""Prefix of the next :meth:`build_engine_prompt` through the last vision token.
History plus arriving video frames, without query text or input audio.
Return None when there are no usable frames (or to disable incremental prefill).
"""
...
@dataclass(frozen=True)
class VideoStreamTurnTrigger:
@@ -144,7 +173,10 @@ class StreamingVideoSessionConfig(BaseModel):
)
use_audio_in_video: bool = Field(
default=True,
description="Interleave audio chunks with video frames when audio input is present.",
description=(
"Pass use_audio_in_video to the processor when this is true and "
"(incremental prefill is active, or this query has input audio)."
),
)
sampling_params_list: list[dict[str, Any]] | None = Field(
default=None,
@@ -194,10 +226,77 @@ class OmniStreamingVideoHandler:
) -> None:
raise NotImplementedError
@staticmethod
def _build_frame_image_parts(
frames: list[str],
prewarmed_frames: Mapping[str, PrewarmedFrame] | None,
) -> list[dict[str, Any]]:
"""Decoded frame parts. Prefix and query both wait for PIL prewarm so
every frame is ``image_pil`` plus the arrival-time uuid."""
prewarmed = prewarmed_frames or {}
parts: list[dict[str, Any]] = []
for frame_b64 in frames:
cached = prewarmed.get(frame_b64)
if not _usable_prewarmed_frame(cached):
continue
pil, mm_uuid = cached
parts.append(
{
"type": "image_pil",
"image_pil": pil,
"uuid": mm_uuid,
}
)
return parts
def _history_prefix_messages(
self,
config: StreamingVideoSessionConfig,
message_history: list[dict[str, Any]],
) -> list[dict[str, Any]]:
"""Shared prompt prefix (system + compressed text-only history).
:meth:`build_engine_prompt_prefix` and :meth:`build_engine_prompt`
MUST build this identically: any divergence before the frame parts
voids the warmed KV from that token onward.
"""
messages: list[dict[str, Any]] = []
if config.system_prompt:
messages.append({"role": "system", "content": config.system_prompt})
recent_history = message_history[-2:] if len(message_history) > 2 else message_history
for hist_msg in recent_history:
messages.append(self._text_only_message(hist_msg))
return messages
def build_engine_prompt_prefix(
self,
config: StreamingVideoSessionConfig,
frame_buffer: list[str],
message_history: list[dict[str, Any]],
prewarmed_frames: Mapping[str, PrewarmedFrame],
) -> list[dict[str, Any]] | None:
"""Prefix of the next :meth:`build_engine_prompt` through the last vision token.
History plus arriving video frames, without query text or input audio.
A mismatch voids the warmed KV from the first diverging token.
Return None when there are no usable frames (or override to disable).
"""
frame_parts = self._build_frame_image_parts(frame_buffer, prewarmed_frames)
if not frame_parts:
return None
messages = self._history_prefix_messages(config, message_history)
messages.append({"role": "user", "content": frame_parts})
return messages
def create_message_history(self, config: StreamingVideoSessionConfig) -> Any:
"""Per-session conversation state (default: empty OpenAI-style list)."""
return []
def _incremental_prefill_active(self, config: StreamingVideoSessionConfig) -> bool:
"""Incremental frame prefill: stage-0 prefix caching on, text-only output."""
return self._incremental_prefill_supported and "audio" not in config.modalities
def on_frame_buffered(
self,
raw_bytes: bytes,
@@ -219,6 +318,7 @@ class OmniStreamingVideoHandler:
self._idle_timeout = idle_timeout
self._config_timeout = config_timeout
self._engine_client = engine_client
self._incremental_prefill_supported = _stage0_prefix_caching_enabled(engine_client)
async def handle_session(self, websocket: WebSocket) -> None:
"""Main session loop for a single WebSocket connection."""
@@ -244,6 +344,53 @@ class OmniStreamingVideoHandler:
interrupt_event = asyncio.Event()
prewarm_tasks: set[asyncio.Task[Any]] = set()
query_task: asyncio.Task[Any] | None = None
session_alive = True
# Session-owned decode readiness. Warmup/query wait on these
# Events, not on the prewarm tasks, so cancelling an engine
# request cannot cascade into JPEG decode.
frame_ready: dict[str, asyncio.Event] = {}
# In-flight warmup/query snapshots keep their PIL entries if FIFO
# evicts the live buffer while they await readiness.
pinned_frame_refs: dict[str, int] = {}
def _drop_frame_cache(frame_b64: str) -> None:
frame_pil_cache.pop(frame_b64, None)
frame_ready.pop(frame_b64, None)
def _pin_frames(frames: list[str]) -> None:
for frame_b64 in frames:
pinned_frame_refs[frame_b64] = pinned_frame_refs.get(frame_b64, 0) + 1
def _unpin_frames(frames: list[str]) -> None:
for frame_b64 in frames:
remaining = pinned_frame_refs.get(frame_b64, 0) - 1
if remaining <= 0:
pinned_frame_refs.pop(frame_b64, None)
# Settle the eviction this pin deferred: the frame left
# the buffer, so its cache entry is orphaned.
if frame_b64 not in frame_buffer:
_drop_frame_cache(frame_b64)
else:
pinned_frame_refs[frame_b64] = remaining
def _snapshot_prewarmed(frames: list[str]) -> dict[str, Any]:
return {frame_b64: frame_pil_cache[frame_b64] for frame_b64 in frames if frame_b64 in frame_pil_cache}
def _frames_ready(frames: list[str]) -> bool:
for frame_b64 in frames:
event = frame_ready.get(frame_b64)
if event is None or not event.is_set():
return False
return True
async def _await_frames_ready(frames: list[str]) -> None:
waits: list[Any] = []
for frame_b64 in dict.fromkeys(frames):
event = frame_ready.get(frame_b64)
if event is not None and not event.is_set():
waits.append(event.wait())
if waits:
await asyncio.gather(*waits)
msg_queue: asyncio.Queue[dict[str, Any] | None] = asyncio.Queue(maxsize=_MAX_MSG_QUEUE)
@@ -294,15 +441,95 @@ class OmniStreamingVideoHandler:
interrupt_event.set()
prev_was_interrupted = True
logger.info("Interrupt signaled for %s", active_request_id)
if query_task is not None and not query_task.done():
query_task.cancel()
await asyncio.gather(query_task, return_exceptions=True)
query_task = None
# Abort after the task reaches Stage0 so a cancel during preprocess still submits.
if abort_now and self._engine_client:
try:
await self._engine_client.abort(active_request_id)
except Exception:
logger.debug("Abort failed for %s", active_request_id, exc_info=True)
if query_task is not None and not query_task.done():
query_task.cancel()
await asyncio.gather(query_task, return_exceptions=True)
query_task = None
# --- Incremental prefill: keep the prefix cache warmed to the latest
# buffered frame so a query only pays for its own text suffix. ---
warmup_task: asyncio.Task[Any] | None = None
warmup_request_id: str | None = None
warmed_signature: tuple[Any, ...] | None = None
def _context_signature() -> tuple[Any, ...]:
history_len = len(message_history) if isinstance(message_history, list) else None
return (history_len, tuple(frame_buffer))
def _maybe_start_warmup() -> None:
nonlocal warmup_task, warmup_request_id
if not session_alive:
return
if self._engine_client is None or not self._incremental_prefill_active(config):
return
if active_request_id is not None:
return
if warmup_task is not None and not warmup_task.done():
return
if not frame_buffer or _context_signature() == warmed_signature:
return
frames = list(frame_buffer)
# Decode is session-owned: do not start prefill until PIL is
# already in cache, so this task is only engine work.
if not _frames_ready(frames):
return
signature = _context_signature()
request_id = f"video-warmup-{uuid.uuid4().hex[:12]}"
warmup_request_id = request_id
_pin_frames(frames)
async def _run_warmup() -> None:
nonlocal warmup_request_id, warmed_signature
cancelled = False
try:
messages = self.build_engine_prompt_prefix(
config, frames, message_history, _snapshot_prewarmed(frames)
)
if not messages:
warmed_signature = signature
return
await self._prefill_context(config, messages, request_id)
warmed_signature = signature
except asyncio.CancelledError:
cancelled = True
raise
except Exception:
# Mark attempted so a persistently failing state does
# not hot-loop; the next frame retriggers naturally.
warmed_signature = signature
logger.debug("Context warmup failed for %s", request_id, exc_info=True)
finally:
_unpin_frames(frames)
if warmup_request_id == request_id:
warmup_request_id = None
# Skip on cancel: query already owns the session and will
# call_soon after it finishes. Restarting here can spawn a
# second warmup with the same frames before active_request_id
# is set.
if not cancelled:
asyncio.get_running_loop().call_soon(_maybe_start_warmup)
warmup_task = asyncio.create_task(_run_warmup())
async def _cancel_warmup() -> None:
nonlocal warmup_task, warmup_request_id
request_id = warmup_request_id
if warmup_task is not None and not warmup_task.done():
warmup_task.cancel()
await asyncio.gather(warmup_task, return_exceptions=True)
warmup_task = None
warmup_request_id = None
if request_id and self._engine_client:
try:
await self._engine_client.abort(request_id)
except Exception:
logger.debug("Warmup abort failed for %s", request_id, exc_info=True)
async def _start_query_turn(*, query_text: str) -> None:
"""Schedule a new inference turn from the current buffers."""
@@ -311,28 +538,43 @@ class OmniStreamingVideoHandler:
await _cancel_active_query()
if not frame_buffer:
await _cancel_warmup()
await self._send_error(websocket, "No frames buffered")
return
# Claim the session before cancelling warmup so a leftover
# call_soon from a just-finished warmup hits the 405 gate.
request_id = f"video-{uuid.uuid4().hex[:12]}"
active_request_id = request_id
interrupt_event.clear()
# Free prefill capacity for the query; completed warmup blocks
# stay in the prefix cache and are reused by it.
await _cancel_warmup()
if prev_was_interrupted and prev_request_id and self._engine_client:
try:
await self._engine_client.abort(prev_request_id)
except Exception:
pass
prev_was_interrupted = False
request_id = f"video-{uuid.uuid4().hex[:12]}"
active_request_id = request_id
interrupt_event.clear()
query_frames = list(frame_buffer)
query_frame_metadata = list(frame_metadata)
query_audio_buffer = bytearray(audio_buffer)
audio_buffer.clear()
query_prewarmed_frames = dict(frame_pil_cache)
# Legacy stride positions ignore decode status. Waiting on the
# whole buffer would stall the query on a frame it will not use.
if self._incremental_prefill_active(config):
wait_indices = list(range(len(query_frames)))
else:
wait_indices = self._candidate_frame_indices(query_frames, config.num_frames)
wait_frames = [query_frames[index] for index in wait_indices]
_pin_frames(wait_frames)
async def _run_query() -> None:
nonlocal active_request_id, prev_request_id
try:
await _await_frames_ready(wait_frames)
query_prewarmed_frames = _snapshot_prewarmed(query_frames)
process_kwargs: dict[str, Any] = {}
if any(metadata.get("frame_id") for metadata in query_frame_metadata):
process_kwargs["frame_metadata"] = query_frame_metadata
@@ -349,19 +591,25 @@ class OmniStreamingVideoHandler:
**process_kwargs,
)
finally:
_unpin_frames(wait_frames)
if active_request_id == request_id:
prev_request_id = request_id
active_request_id = None
# Warm frames that arrived during the turn (fires after
# this task completes; gated on no active query).
asyncio.get_running_loop().call_soon(_maybe_start_warmup)
query_task = asyncio.create_task(_run_query())
async def _processor() -> None:
"""Process enqueued messages."""
nonlocal active_request_id, prev_request_id, prev_was_interrupted, query_task
nonlocal session_alive
while True:
msg = await msg_queue.get()
if msg is None:
session_alive = False
await _cancel_active_query(abort_now=True)
return
@@ -376,8 +624,8 @@ class OmniStreamingVideoHandler:
]
frame_buffer[:] = [frame_buffer[index] for index in retained_indices]
frame_metadata[:] = [frame_metadata[index] for index in retained_indices]
if frame_pil_cache.get(frame_data) is _BAD_FRAME:
frame_pil_cache.pop(frame_data, None)
if frame_pil_cache.get(frame_data) is _BAD_FRAME and frame_data not in pinned_frame_refs:
_drop_frame_cache(frame_data)
if removed:
await self._send_error(websocket, "Frame decode failed")
@@ -410,10 +658,24 @@ class OmniStreamingVideoHandler:
max_buf = config.max_frames
dropped_frame_id: str | None = None
if len(frame_buffer) >= max_buf:
dropped = frame_buffer.pop(0)
dropped_metadata = frame_metadata.pop(0)
dropped_frame_id = dropped_metadata.get("frame_id")
frame_pil_cache.pop(dropped, None)
# Legacy drops one frame. Incremental drops the oldest half so later frames append.
drop_count = max(1, max_buf // 2) if self._incremental_prefill_active(config) else 1
for _ in range(drop_count):
dropped = frame_buffer.pop(0)
dropped_metadata = frame_metadata.pop(0)
if dropped_frame_id is None:
dropped_frame_id = dropped_metadata.get("frame_id")
# Duplicate frames share one cache entry keyed by
# b64: keep it while any duplicate remains buffered.
if dropped not in pinned_frame_refs and dropped not in frame_buffer:
_drop_frame_cache(dropped)
if drop_count > 1:
logger.warning(
"incremental prefill dropped %d oldest frames at max_frames=%d; "
"buffered_frames is the remaining suffix",
drop_count,
max_buf,
)
frame_buffer.append(frame_data)
frame_metadata.append(
{
@@ -435,17 +697,23 @@ class OmniStreamingVideoHandler:
)
# Prewarm: decode PIL off the event loop so query-time chat_template
# can skip base64+Image.open. uuid=md5 lets mm_cache dedupe identical frames.
if frame_data not in frame_pil_cache:
if frame_data not in frame_pil_cache and frame_data not in frame_ready:
mm_uuid = hashlib.md5(raw_bytes, usedforsecurity=False).hexdigest()
frame_ready[frame_data] = asyncio.Event()
async def _prewarm(b64: str, b: bytes, u: str) -> None:
cancelled = False
try:
pil = await asyncio.to_thread(_decode_frame_bytes, b)
# The frame may have been evicted while decoding.
if b64 in frame_buffer:
# The frame may have been evicted (and unpinned)
# while decoding; frame_ready tracks liveness.
if b64 in frame_ready:
frame_pil_cache[b64] = (pil, u)
except asyncio.CancelledError:
cancelled = True
raise
except Exception:
if b64 not in frame_buffer:
if b64 not in frame_ready:
return
frame_pil_cache[b64] = _BAD_FRAME
logger.warning("prewarm decode failed for frame (len=%d)", len(b))
@@ -455,10 +723,18 @@ class OmniStreamingVideoHandler:
logger.warning(
"frame decode failure event dropped because message queue is full"
)
finally:
event = frame_ready.get(b64)
if event is not None:
event.set()
if not cancelled:
asyncio.get_running_loop().call_soon(_maybe_start_warmup)
task = asyncio.create_task(_prewarm(frame_data, raw_bytes, mm_uuid))
prewarm_tasks.add(task)
task.add_done_callback(prewarm_tasks.discard)
elif frame_data in frame_pil_cache:
frame_ready.setdefault(frame_data, asyncio.Event()).set()
is_generating = active_request_id is not None or (
query_task is not None and not query_task.done()
@@ -471,6 +747,8 @@ class OmniStreamingVideoHandler:
)
):
await _start_query_turn(query_text="")
else:
_maybe_start_warmup()
elif msg_type == "audio.chunk":
data_b64 = msg.get("data", "")
@@ -501,6 +779,7 @@ class OmniStreamingVideoHandler:
await _start_query_turn(query_text=query_text)
elif msg_type == "video.done":
session_alive = False
if query_task is not None and not query_task.done():
await asyncio.gather(query_task, return_exceptions=True)
query_task = None
@@ -520,6 +799,7 @@ class OmniStreamingVideoHandler:
try:
await _processor()
finally:
session_alive = False
reader_task.cancel()
try:
await reader_task
@@ -529,6 +809,7 @@ class OmniStreamingVideoHandler:
t.cancel()
if prewarm_tasks:
await asyncio.gather(*prewarm_tasks, return_exceptions=True)
await _cancel_warmup()
if query_task is not None and not query_task.done():
await _cancel_active_query(abort_now=True)
@@ -643,9 +924,10 @@ class OmniStreamingVideoHandler:
ChatCompletionRequest,
)
reuse_active = self._incremental_prefill_active(config)
# Select once from this turn's snapshot so prompt images and the
# consumed event refer to exactly the same frames, including decode failures.
frame_indices = self._sample_frame_indices(frame_buffer, config.num_frames, prewarmed_frames)
frame_indices = self._select_frame_indices(config, frame_buffer, prewarmed_frames)
messages, user_message = self.build_engine_prompt(
config,
frame_buffer,
@@ -665,7 +947,9 @@ class OmniStreamingVideoHandler:
"continue_final_message": False,
"add_special_tokens": False,
}
if config.use_audio_in_video and len(audio_buffer) > 0:
# Incremental: same kwargs as warmup so frame mm hashes stay a prefix.
# Legacy: only when this query actually has input audio.
if config.use_audio_in_video and (reuse_active or len(audio_buffer) > 0):
request_kwargs["mm_processor_kwargs"] = {
"use_audio_in_video": True,
}
@@ -680,97 +964,142 @@ class OmniStreamingVideoHandler:
await self._send_error(websocket, f"Failed to build request: {e}")
return
try:
engine_prompt = await self._preprocess_to_engine_prompt(chat_request)
except Exception as e:
await self._send_error(websocket, f"Preprocess failed: {e}")
return
decoded_ready_ts_ms = _time.monotonic() * 1000
selected_metadata = [frame_metadata[index] for index in frame_indices] if frame_metadata else []
model_selected_ts_ms = _time.monotonic() * 1000
submitted = asyncio.get_running_loop().create_future()
await websocket.send_json({"type": "response.start"})
text_parts: list[str] = []
text_done_sent = False
audio_chunk_count = 0
# Count emitted tensors only to identify the first audio emission.
# Default and explicit sampling parameters use DELTA audio outputs.
audio_chunks_drained = 0
previous_text = ""
interrupted = False
frames_consumed_sent = False
t_start = _time.monotonic()
t_first_text = None
t_first_audio = None
async def _drive_query() -> None:
decoded_ready_ts_ms = _time.monotonic() * 1000
selected_metadata = [frame_metadata[index] for index in frame_indices] if frame_metadata else []
model_selected_ts_ms = _time.monotonic() * 1000
# Wire-level async-chunk switch. "off" means
# buffer all deltas server-side and flush once at the end; the engine
# pipeline still overlaps internally.
async_chunk_mode = video_stream_envs.VLLM_VIDEO_ASYNC_CHUNK
streaming = async_chunk_mode == "on"
audio_tail_tensors: list[Any] = []
if not interrupt_event.is_set():
await websocket.send_json({"type": "response.start"})
text_parts: list[str] = []
text_done_sent = False
audio_chunk_count = 0
# Count emitted tensors only to identify the first audio emission.
# Default and explicit sampling parameters use DELTA audio outputs.
audio_chunks_drained = 0
previous_text = ""
interrupted = False
frames_consumed_sent = False
t_start = _time.monotonic()
t_first_text = None
t_first_audio = None
try:
result_gen = engine_client.generate(
prompt=engine_prompt,
request_id=request_id,
output_modalities=config.modalities,
**sampling_kwargs,
)
# Wire-level async-chunk switch. "off" means
# buffer all deltas server-side and flush once at the end; the engine
# pipeline still overlaps internally.
async_chunk_mode = video_stream_envs.VLLM_VIDEO_ASYNC_CHUNK
streaming = async_chunk_mode == "on"
audio_tail_tensors: list[Any] = []
async for output in result_gen:
# Soft interrupt: drain without sending
if interrupt_event.is_set():
if not interrupted:
logger.info("Generation interrupted — draining")
interrupted = True
continue
try:
assert engine_client is not None
result_gen = engine_client.generate(
prompt=engine_prompt,
request_id=request_id,
output_modalities=config.modalities,
submitted=submitted,
**sampling_kwargs,
)
if not isinstance(output, OmniRequestOutput):
continue
async for output in result_gen:
# Soft interrupt: drain without sending
if interrupt_event.is_set():
if not interrupted:
logger.info("Generation interrupted — draining")
interrupted = True
continue
if not frames_consumed_sent and frame_metadata:
await websocket.send_json(
{
"type": "video.frames.consumed",
"request_id": request_id,
"model_selected_ts_ms": model_selected_ts_ms,
"frame_ids": [
metadata["frame_id"]
for metadata in selected_metadata
if isinstance(metadata.get("frame_id"), str)
],
"frames": [
{
"frame_id": metadata.get("frame_id"),
"pts_ms": metadata.get("pts_ms"),
"source_pts_ms": metadata.get("source_pts_ms"),
"quality_profile": metadata.get("quality_profile"),
"receiver_received_ts_ms": metadata.get("receiver_received_ts_ms"),
"decoded_ready_ts_ms": decoded_ready_ts_ms,
}
for metadata in selected_metadata
],
"latest_pts_ms": selected_metadata[-1].get("pts_ms") if selected_metadata else None,
}
)
frames_consumed_sent = True
if not isinstance(output, OmniRequestOutput):
continue
out_type = getattr(output, "final_output_type", "text")
if not frames_consumed_sent and frame_metadata:
await websocket.send_json(
{
"type": "video.frames.consumed",
"request_id": request_id,
"model_selected_ts_ms": model_selected_ts_ms,
"frame_ids": [
metadata["frame_id"]
for metadata in selected_metadata
if isinstance(metadata.get("frame_id"), str)
],
"frames": [
{
"frame_id": metadata.get("frame_id"),
"pts_ms": metadata.get("pts_ms"),
"source_pts_ms": metadata.get("source_pts_ms"),
"quality_profile": metadata.get("quality_profile"),
"receiver_received_ts_ms": metadata.get("receiver_received_ts_ms"),
"decoded_ready_ts_ms": decoded_ready_ts_ms,
}
for metadata in selected_metadata
],
"latest_pts_ms": selected_metadata[-1].get("pts_ms") if selected_metadata else None,
}
)
frames_consumed_sent = True
if out_type == "audio":
if streaming and not text_done_sent:
full_text = "".join(text_parts)
await websocket.send_json({"type": "response.text.done", "text": full_text})
text_done_sent = True
out_type = getattr(output, "final_output_type", "text")
if t_first_audio is None:
t_first_audio = _time.monotonic()
audio_chunk_count += 1
if streaming:
b64, audio_chunks_drained = self._extract_audio_delta_b64(
if out_type == "audio":
if streaming and not text_done_sent:
full_text = "".join(text_parts)
await websocket.send_json({"type": "response.text.done", "text": full_text})
text_done_sent = True
if t_first_audio is None:
t_first_audio = _time.monotonic()
audio_chunk_count += 1
if streaming:
b64, audio_chunks_drained = self._extract_audio_delta_b64(
output,
audio_chunks_drained,
)
if b64:
await websocket.send_json(
{
"type": "response.output_audio.delta",
"data": b64,
"format": "wav",
}
)
else:
audio_data = self._get_audio_data(output)
if audio_data is not None:
if isinstance(audio_data, list):
audio_tail_tensors.extend(audio_data)
else:
audio_tail_tensors.append(audio_data)
else:
delta_text, previous_text = self._extract_text_delta(
output,
audio_chunks_drained,
previous_text,
)
if delta_text:
if t_first_text is None:
t_first_text = _time.monotonic()
text_parts.append(delta_text)
if streaming:
await websocket.send_json({"type": "response.text.delta", "delta": delta_text})
if not text_done_sent:
full_text = "".join(text_parts)
await websocket.send_json({"type": "response.text.done", "text": full_text})
text_done_sent = True
if not streaming and audio_tail_tensors:
try:
coalesced = audio_tail_tensors[0]
if len(audio_tail_tensors) != 1:
coalesced = torch.cat(audio_tail_tensors, dim=-1)
tail_np = self._tensor_to_1d_np(coalesced)
b64, _ = self._encode_tail(
tail_np,
0,
new_drained=len(audio_tail_tensors),
is_first=True,
)
if b64:
await websocket.send_json(
@@ -780,76 +1109,45 @@ class OmniStreamingVideoHandler:
"format": "wav",
}
)
else:
audio_data = self._get_audio_data(output)
if audio_data is not None:
if isinstance(audio_data, list):
audio_tail_tensors.extend(audio_data)
else:
audio_tail_tensors.append(audio_data)
else:
delta_text, previous_text = self._extract_text_delta(
output,
previous_text,
)
if delta_text:
if t_first_text is None:
t_first_text = _time.monotonic()
text_parts.append(delta_text)
if streaming:
await websocket.send_json({"type": "response.text.delta", "delta": delta_text})
except Exception:
logger.exception("Failed to coalesce off-path audio")
if not text_done_sent:
if audio_chunk_count > 0:
await websocket.send_json({"type": "response.output_audio.done"})
response_text = "".join(text_parts)
self.on_turn_complete(message_history, user_message, response_text)
t_end = _time.monotonic()
logger.info(
"[TIMING] mode=%s total=%.2fs first_text=%.2fs first_audio=%.2fs audio_chunks=%d",
async_chunk_mode,
t_end - t_start,
(t_first_text - t_start) if t_first_text else -1,
(t_first_audio - t_start) if t_first_audio else -1,
audio_chunk_count,
)
except Exception:
logger.exception("Engine query failed")
await self._send_error(websocket, "Query processing failed")
if not text_done_sent and not interrupt_event.is_set():
full_text = "".join(text_parts)
await websocket.send_json({"type": "response.text.done", "text": full_text})
text_done_sent = True
if not streaming and audio_tail_tensors:
try:
coalesced = (
audio_tail_tensors[0] if len(audio_tail_tensors) == 1 else torch.cat(audio_tail_tensors, dim=-1)
)
tail_np = self._tensor_to_1d_np(coalesced)
b64, _ = self._encode_tail(
tail_np,
0,
new_drained=len(audio_tail_tensors),
is_first=True,
)
if b64:
await websocket.send_json(
{
"type": "response.output_audio.delta",
"data": b64,
"format": "wav",
}
)
except Exception:
logger.exception("Failed to coalesce off-path audio")
if audio_chunk_count > 0:
await websocket.send_json({"type": "response.output_audio.done"})
response_text = "".join(text_parts)
self.on_turn_complete(message_history, user_message, response_text)
t_end = _time.monotonic()
logger.info(
"[TIMING] mode=%s total=%.2fs first_text=%.2fs first_audio=%.2fs audio_chunks=%d",
async_chunk_mode,
t_end - t_start,
(t_first_text - t_start) if t_first_text else -1,
(t_first_audio - t_start) if t_first_audio else -1,
audio_chunk_count,
)
except Exception:
logger.exception("Engine query failed")
await self._send_error(websocket, "Query processing failed")
if not text_done_sent:
full_text = "".join(text_parts)
await websocket.send_json({"type": "response.text.done", "text": full_text})
prompt_task = asyncio.ensure_future(self._preprocess_to_engine_prompt(chat_request))
try:
engine_prompt = await asyncio.shield(prompt_task)
except asyncio.CancelledError:
# Sender cache is updated in preprocess; submit before the caller aborts.
engine_prompt = await asyncio.shield(prompt_task)
await self._stop_after_submit(asyncio.ensure_future(_drive_query()), submitted)
raise
except Exception as e:
await self._send_error(websocket, f"Preprocess failed: {e}")
return
await self._await_until_submitted(asyncio.ensure_future(_drive_query()), submitted)
# ------------------------------------------------------------------
# Audio helpers
@@ -1009,6 +1307,105 @@ class OmniStreamingVideoHandler:
return text[len(previous_text) :], text
return text, text
# ------------------------------------------------------------------
# Incremental prefill (context warmup)
# ------------------------------------------------------------------
async def _stop_after_submit(self, drive: asyncio.Future[Any], submitted: asyncio.Future[None]) -> None:
"""Stage0 must observe the request before the caller aborts it."""
if not submitted.done():
submit_wait = asyncio.ensure_future(asyncio.shield(submitted))
await asyncio.wait({submit_wait, drive}, return_when=asyncio.FIRST_COMPLETED)
if not submitted.done():
submit_wait.cancel()
exc = drive.exception() if drive.done() else None
if exc is not None:
raise exc
if not drive.done():
drive.cancel()
await asyncio.gather(drive, return_exceptions=True)
async def _await_until_submitted(self, drive: asyncio.Future[Any], submitted: asyncio.Future[None]) -> None:
"""On cancel, wait until Stage0 has accepted the request before aborting."""
try:
# Shield so cancelling this task does not cancel generate before Stage0 submit.
await asyncio.shield(drive)
except asyncio.CancelledError:
await self._stop_after_submit(drive, submitted)
raise
async def _drive_warmup_generate(
self,
engine_client: Any,
engine_prompt: Any,
request_id: str,
modalities: list[str],
*,
stop_after_submit: bool = False,
) -> None:
"""max_tokens=1 generate; cancellation still submits before abort."""
from vllm import SamplingParams
submitted = asyncio.get_running_loop().create_future()
result_gen = engine_client.generate(
prompt=engine_prompt,
request_id=request_id,
sampling_params=SamplingParams(max_tokens=1, temperature=0.0),
output_modalities=modalities,
submitted=submitted,
)
async def _drive() -> None:
async for _ in result_gen:
pass
drive = asyncio.ensure_future(_drive())
if stop_after_submit:
await self._stop_after_submit(drive, submitted)
return
await self._await_until_submitted(drive, submitted)
async def _prefill_context(
self,
config: StreamingVideoSessionConfig,
messages: list[dict[str, Any]],
request_id: str,
) -> None:
"""max_tokens=1 generate so arriving frames land in the prefix cache."""
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
)
engine_client = self._engine_client
if engine_client is None:
return
request_kwargs: dict[str, Any] = {
"model": config.model or "default",
"messages": messages,
"stream": True,
"modalities": config.modalities,
"add_generation_prompt": True,
"continue_final_message": False,
"add_special_tokens": False,
}
if config.use_audio_in_video:
request_kwargs["mm_processor_kwargs"] = {
"use_audio_in_video": True,
}
chat_request = ChatCompletionRequest(**request_kwargs)
prompt_task = asyncio.ensure_future(self._preprocess_to_engine_prompt(chat_request))
try:
engine_prompt = await asyncio.shield(prompt_task)
except asyncio.CancelledError:
# Sender cache is updated in preprocess; submit before the caller aborts.
engine_prompt = await asyncio.shield(prompt_task)
await self._drive_warmup_generate(
engine_client, engine_prompt, request_id, config.modalities, stop_after_submit=True
)
raise
await self._drive_warmup_generate(engine_client, engine_prompt, request_id, config.modalities)
# ------------------------------------------------------------------
# Preprocessing
# ------------------------------------------------------------------
@@ -1043,6 +1440,18 @@ class OmniStreamingVideoHandler:
except Exception:
pass
@staticmethod
def _candidate_frame_indices(frame_buffer: list[str], num_frames: int) -> list[int]:
"""Stride positions for a legacy query, including the last frame.
Ignores decode status so a frame still decoding is waited on, not skipped.
"""
n_buf = len(frame_buffer)
if n_buf <= num_frames:
return list(range(n_buf))
stride = max(1, n_buf // num_frames)
return [index * stride for index in range(num_frames - 1)] + [n_buf - 1]
@staticmethod
def _sample_frame_indices(
frame_buffer: list[str],
@@ -1050,13 +1459,26 @@ class OmniStreamingVideoHandler:
prewarmed_frames: Mapping[str, PrewarmedFrame],
) -> list[int]:
"""Stride-sample with the last frame, then drop known bad frames without refilling."""
n_buf = len(frame_buffer)
if n_buf <= num_frames:
indices = list(range(n_buf))
else:
stride = max(1, n_buf // num_frames)
indices = [index * stride for index in range(num_frames - 1)] + [n_buf - 1]
return [index for index in indices if prewarmed_frames.get(frame_buffer[index]) is not _BAD_FRAME]
indices = OmniStreamingVideoHandler._candidate_frame_indices(frame_buffer, num_frames)
return [index for index in indices if _usable_prewarmed_frame(prewarmed_frames.get(frame_buffer[index]))]
def _select_frame_indices(
self,
config: StreamingVideoSessionConfig,
frame_buffer: list[str],
prewarmed_frames: Mapping[str, PrewarmedFrame],
) -> list[int]:
"""This turn's frame selection, shared by the prompt and the consumed event."""
if self._incremental_prefill_active(config):
# Frames are sampled at arrival time only (EVS + max_frames), so
# the buffer already is the warmed sequence. Query-time
# subsampling would pick a subset, which is not a prefix.
return [
index
for index, frame_b64 in enumerate(frame_buffer)
if _usable_prewarmed_frame(prewarmed_frames.get(frame_b64))
]
return self._sample_frame_indices(frame_buffer, config.num_frames, prewarmed_frames)
@staticmethod
async def _send_frame_ack(
@@ -42,6 +42,7 @@ from vllm_omni.experimental.ar_diffusion.kv_cache.paged import (
chunk_slot_mapping,
pool_write_chunk,
resident_block_ids,
visible_window_blocks,
)
_log = init_logger(__name__)
@@ -282,7 +283,12 @@ class ARDiffusionKVCache:
# forward needs one block per current frame plus space for any
# model-declared action/state tokens that coexist with video KV.
declared_scratch_blocks = (max_scratch_tokens_per_branch + block_size - 1) // block_size
minimum_scratch_blocks = self.frames_per_block + declared_scratch_blocks
# frames_per_block counts *frames*; convert to blocks rather than
# assuming one block per frame. A frame spans ceil(chunk_size /
# block_size) blocks once the two sizes are allowed to differ.
blocks_per_frame = -(-config.chunk_size // block_size)
self.blocks_per_frame = blocks_per_frame
minimum_scratch_blocks = self.frames_per_block * blocks_per_frame + declared_scratch_blocks
override = os.environ.get("AR_DIFFUSION_KV_SCRATCH_BLOCKS_PER_BRANCH")
override_blocks = int(override) if override is not None else 0
if override_blocks < 0:
@@ -313,8 +319,23 @@ class ARDiffusionKVCache:
)
def _required_managed_blocks(capacity: int) -> int:
resident_per_session = config.sink_chunks + config.window_chunks
return self.num_local_kv_branches * (capacity * resident_per_session + self.frames_per_block) + 2
"""Managed blocks needed to hold ``capacity`` resident sessions.
Every term here counts *frames*, so each is converted to blocks.
Before block size and frame size were allowed to differ this
conversion was the identity and the frame counts could be used
directly; they cannot once a frame spans several blocks.
Converting rather than rewriting keeps the budget the same number
of *tokens* it always was, so a finer block size does not inflate
the requirement -- it only stops the final block of each term from
rounding up a whole frame's worth of memory.
"""
resident_frames = config.sink_chunks + config.window_chunks
per_session_blocks = resident_frames * self.blocks_per_frame
in_flight_blocks = self.frames_per_block * self.blocks_per_frame
headroom_blocks = 2 * self.blocks_per_frame
return self.num_local_kv_branches * (capacity * per_session_blocks + in_flight_blocks) + headroom_blocks
# reuse_history_staging keeps one contiguous K and V buffer per layer,
# sized to the padded visible window (sink + window plus the
@@ -327,9 +348,15 @@ class ARDiffusionKVCache:
self.history_staging_tokens = 0
if config.reuse_history_staging:
if contiguous_kv_gather_enabled():
self.history_staging_tokens = (
self.spec.sliding_window + config.sink_chunks * config.chunk_size + block_size
# The same width build_block_table pads to: a sink or window
# that is not a whole number of blocks spans one more block.
window_blocks = visible_window_blocks(
chunk_size=config.chunk_size,
block_size=block_size,
sink_tokens=config.sink_chunks * config.chunk_size,
window_tokens=self.spec.sliding_window,
)
self.history_staging_tokens = (window_blocks + 1) * block_size
self.history_staging_reserved_bytes = int(
2 * num_layers * self.history_staging_tokens * num_kv_heads * head_size * dtype.itemsize
)
@@ -649,6 +676,16 @@ class ARDiffusionKVCache:
def block_table(self, adapter: ARDiffusionRequestAdapter) -> list[int]:
return list(self.manager.get_block_ids(adapter.request_id)[0])
def block_ids_at(self, adapter: ARDiffusionRequestAdapter, indices: Sequence[int]) -> list[int]:
"""Block ids at ``indices`` of the request's block table, without copying it.
The table keeps a null entry for every evicted position, so it grows with
the session. :meth:`block_table` reads every entry; this reads only the
ones asked for, straight from the manager's own block list.
"""
blocks = self.manager.get_blocks(adapter.request_id).blocks[0]
return [blocks[index].block_id for index in indices]
def chunk_write_slots(self, adapter: ARDiffusionRequestAdapter) -> torch.Tensor:
"""Slot mapping for the in-flight chunk — the K/V write target."""
return chunk_slot_mapping(
@@ -17,6 +17,7 @@ Cosmos port) reuses unchanged. Three concerns live here:
from __future__ import annotations
import math
from collections.abc import Sequence
from dataclasses import dataclass
@@ -196,6 +197,19 @@ def chunk_window_skipped_tokens(
return (skipped // chunk_size) * chunk_size
def visible_window_blocks(*, chunk_size: int, block_size: int, sink_tokens: int, window_tokens: int) -> int:
"""Most blocks a sink plus a recent window can span; see ``max_video_blocks``.
Each range is converted on its own: the sink ends at a fixed position, and
the recent range starts on a chunk boundary, so at most
``block_size - gcd(chunk_size, block_size)`` tokens past a block edge.
"""
worst_start_offset = block_size - math.gcd(chunk_size, block_size)
sink_blocks = -(-sink_tokens // block_size)
recent_blocks = -(-(window_tokens + worst_start_offset) // block_size)
return sink_blocks + recent_blocks
class ChunkWindowManager(SlidingWindowManager):
"""``SlidingWindowManager`` that evicts at chunk boundaries.
@@ -219,14 +233,26 @@ class ChunkWindowManager(SlidingWindowManager):
Call only after commit/eviction, then shift the request's storage
position by the returned amount. Physical pages and model positions
are unchanged, including any allocated but not yet committed tail.
The gap starts where :meth:`remove_skipped_blocks` starts freeing: after
the sink's blocks, which are ``sink_chunks`` only while a frame is a
block. Its removal is also rounded down to a whole number of chunks.
Eviction snaps to chunk boundaries and reads its position in storage
coordinates, and paged attention reads each block's offset there too;
shifting storage by whole chunks *and* whole blocks leaves both exactly
as they were in model positions. When a frame is a block, every gap
already is one.
"""
if self.enable_caching:
raise RuntimeError("AR block-table compaction requires prefix caching to be disabled")
blocks = self.req_to_blocks.get(request_id, [])
start = self.kv_cache_spec.sink_chunks
spec = self.kv_cache_spec
start = -(-(spec.sink_chunks * spec.chunk_size) // self.block_size)
end = start
while end < len(blocks) and blocks[end] == self._null_block:
end += 1
blocks_per_step = spec.chunk_size // math.gcd(spec.chunk_size, self.block_size)
end = start + (end - start) // blocks_per_step * blocks_per_step
if end == start:
return 0
del blocks[start:end]
@@ -241,17 +267,32 @@ class ChunkWindowManager(SlidingWindowManager):
total_computed_tokens: int,
num_prompt_tokens: int | None = None,
) -> None:
"""Free the middle gap while preserving the leading attention sink."""
"""Free the middle gap while preserving the leading attention sink.
``sink_chunks`` counts frames while the block table is indexed in
blocks, so the sink is converted before it is used as an index. The two
were interchangeable while a frame was a block; this commit is what
separates them.
Neither boundary is guaranteed to land on a block edge -- 832x480 is
1560 tokens per frame against a 16-token block -- so a block can hold
the end of one frame and the start of the next. The sink end rounds up
so a straddling block stays protected, and the freed range ends on a
rounded-down boundary so only wholly-skipped blocks are released. Both
are exact when the sizes divide, which is every geometry that paged one
frame per block before.
"""
del num_prompt_tokens
num_skipped_tokens = self.get_num_skipped_tokens(total_computed_tokens)
if num_skipped_tokens <= 0:
return
sink_blocks = self.kv_cache_spec.sink_chunks
num_skipped_blocks = num_skipped_tokens // self.block_size
sink_tokens = self.kv_cache_spec.sink_chunks * self.kv_cache_spec.chunk_size
first_live_block = -(-sink_tokens // self.block_size)
last_freed_block = (sink_tokens + num_skipped_tokens) // self.block_size
self._remove_blocks_in_range(
request_id,
sink_blocks,
sink_blocks + num_skipped_blocks,
first_live_block,
last_freed_block,
)
@@ -6,12 +6,15 @@ from __future__ import annotations
import weakref
from dataclasses import dataclass, field
from typing import Any, ClassVar, NamedTuple
from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple
import torch
from vllm_omni.experimental.ar_diffusion.kv_cache.config import contiguous_kv_gather_enabled
from vllm_omni.experimental.ar_diffusion.kv_cache.paged import compute_slot_mapping
from vllm_omni.experimental.ar_diffusion.kv_cache.paged import compute_slot_mapping, visible_window_blocks
if TYPE_CHECKING:
from vllm.v1.attention.backend import MultipleOf
_LAYER_IDX_TENSORS: dict[int, torch.Tensor] = {}
@@ -91,6 +94,21 @@ class ARDiffusionPagedForwardContext:
action_slot_mapping: torch.Tensor | None = None
query_len: int = 0
kv_len: int = 0
# Tokens already committed when this forward began. Recorded rather than
# derived from the block count: the last block of the history is only
# partly written when a chunk is not a whole number of blocks, so
# blocks * block_size overstates it and would make attention read past
# what was written.
_history_tokens: int = 0
# Scratch blocks the current video tokens occupy. Not the same as
# len(current_video_block_ids): when the history ends mid-block, the first
# entry there is the history's own tail block, which is managed, not
# scratch. Action K/V is placed after this, so it must not count it.
_scratch_blocks_used: int = 0
# Slot offset of this chunk's first token inside its first block. The
# chunk's blocks then hold positions ``history - offset + j * block_size``,
# which is how the read path places them among the history's blocks.
_current_offset: int = 0
_allocated_video: bool = False
_committed: bool = False
_action_len: int = 0
@@ -106,14 +124,77 @@ class ARDiffusionPagedForwardContext:
def block_size(self) -> int:
return int(self.kv_cache.block_size)
@property
def chunk_size(self) -> int:
"""Tokens in one chunk -- one latent frame. The eviction unit."""
return int(self.kv_cache.spec.chunk_size)
@property
def start_offset(self) -> int:
"""Where in its first block this forward's tokens begin.
Non-zero exactly when the committed history does not end on a block
boundary, which is the normal case once a frame is not a whole number
of blocks: 1560 tokens per frame against 16-token blocks leaves the
history 8 slots into its last block on every odd-numbered chunk.
"""
return int(self.adapter.num_computed_tokens) % self.block_size
@property
def sink_tokens(self) -> int:
"""Leading tokens the window keeps however far it has slid."""
return int(self.kv_cache.spec.sink_chunks) * self.chunk_size
@property
def window_tokens(self) -> int:
"""Most recent tokens the window keeps, this forward's own included."""
return self.max_video_tokens - self.sink_tokens
@property
def max_video_blocks(self) -> int:
"""Most blocks the visible window can span, which fixes the table width.
The window is two token ranges, the sink and the most recent tokens, and
each is converted to blocks on its own because each can straddle a block
edge on its own. Rounding the two ranges' *sum* up once was a block short
whenever both straddled: at 832x480 a 9-frame sink and a 9-frame window
are each 8 tokens past a 16-token edge but 28080 tokens together, a whole
number of blocks, so it was short on every tick once the window had
filled.
The sink ends at a fixed position, so its blocks are just its tokens
rounded up. The recent range starts at a whole number of chunks, so its
start sits at most ``block_size - gcd(chunk_size, block_size)`` tokens
past a block edge, and it can need that much more room. A chunk that is
a whole number of blocks never straddles, so every geometry that paged
one frame per block keeps exactly the width it had.
The width is a constant of the configuration, which is what keeps the
block table a fixed shape as the window fills.
"""
return visible_window_blocks(
chunk_size=self.chunk_size,
block_size=self.block_size,
sink_tokens=self.sink_tokens,
window_tokens=self.window_tokens,
)
@property
def num_current_video_blocks(self) -> int:
if self.seq_len % self.block_size != 0:
raise AssertionError(
"AR-Diffusion paged attention expects frame-aligned seq_len "
f"(multiple of block_size={self.block_size}), got {self.seq_len}"
)
return self.seq_len // self.block_size
"""Blocks this forward's tokens occupy, counted from where they start.
A chunk need not be a whole number of blocks, so the count depends on
the offset it begins at, not only on its length: thirty tokens
starting at position thirty span three sixteen-token blocks, not two.
Rounding up leaves the tail of the last block unwritten, which is only
safe because nothing reads past ``kv_len`` -- see
:meth:`video_block_table`.
Both paths count the same way. The committing path writes straight
after the history; the scratch path is made to line up with it by
:meth:`ensure_video_slots`.
"""
return -(-(self.start_offset + self.seq_len) // self.block_size)
def ensure_video_slots(self, device: torch.device) -> None:
"""Allocate/write targets for the current video tokens, once per KV branch."""
@@ -121,8 +202,10 @@ class ARDiffusionPagedForwardContext:
return
n_blocks = self.num_current_video_blocks
self._history_tokens = int(self.adapter.num_computed_tokens)
if self.commit_current:
start = int(self.adapter.num_computed_tokens)
self._current_offset = start % self.block_size
self.kv_cache.allocate_token_slots(self.adapter, self.seq_len)
table = self.kv_cache.block_table(self.adapter)
start_block = start // self.block_size
@@ -132,8 +215,27 @@ class ARDiffusionPagedForwardContext:
compute_slot_mapping(table, positions, self.block_size), device
)
else:
self.current_video_block_ids = self.kv_cache.scratch_block_ids(self.kv_branch, 0, n_blocks)
positions = torch.arange(self.seq_len, dtype=torch.long)
# The scratch region cannot simply start at slot zero. The kernel
# reads the history and this chunk as one contiguous run, so if the
# history stopped mid-block, starting here at zero would leave the
# rest of that block unwritten and the kernel would read those dead
# slots as if they were tokens, shifting the whole sequence.
#
# Instead the chunk begins where the history left off, spilling its
# first few tokens into the free tail of the history's own last
# block. That block is already this session's -- blocks are
# allocated whole -- and those slots hold nothing yet; the
# committing forward overwrites the same slots with the clean K/V
# later, so the committed state is unchanged either way.
# A history whose tail block is no longer resident has nothing to
# spill into; the window then starts at this chunk and zero is right.
offset = self.start_offset if self.history_block_ids else 0
self._current_offset = offset
self._scratch_blocks_used = n_blocks - (1 if offset else 0)
scratch_ids = self.kv_cache.scratch_block_ids(self.kv_branch, 0, self._scratch_blocks_used)
tail_block = [self.history_block_ids[-1]] if offset else []
self.current_video_block_ids = tail_block + scratch_ids
positions = torch.arange(offset, offset + self.seq_len, dtype=torch.long)
self.current_video_slot_mapping = _to_device_async(
compute_slot_mapping(self.current_video_block_ids, positions, self.block_size), device
)
@@ -152,7 +254,9 @@ class ARDiffusionPagedForwardContext:
return
action_blocks = (action_len + self.block_size - 1) // self.block_size
scratch_offset = 0 if self.commit_current else len(self.current_video_block_ids)
# Count scratch blocks, not entries: the video list may lead with the
# history's tail block, which lives in the managed pool.
scratch_offset = 0 if self.commit_current else self._scratch_blocks_used
self.action_scratch_block_ids = self.kv_cache.scratch_block_ids(
self.kv_branch,
scratch_offset,
@@ -165,23 +269,75 @@ class ARDiffusionPagedForwardContext:
self._action_len = action_len
def video_block_table(self, device: torch.device) -> tuple[list[int], int]:
"""Blocks the attention reads, and how many of their slots it reads.
The kernel reads the listed blocks as one contiguous run and stops after
the returned length, so two things must hold: every block holding a
token the window keeps is listed, in token order, and only the last
listed block may be partly written.
Blocks are chosen by the token positions they hold rather than counted
off the ends of the resident list. The window is the sink ``[0, S)``
plus the most recent tokens, and each of those can straddle a block
edge on its own. Taking a fixed number of blocks off the tail dropped
the block holding the window's first tokens exactly when both
straddled, and a length capped by a block count rather than counted
from what was written then ran past the last written slot.
Whole blocks are read, so each boundary block contributes up to
``block_size - 1`` tokens the window has already let go of. They are
real K/V at their own positions; a slightly wider window is a far
smaller error than a truncated one.
"""
self.ensure_video_slots(device)
if self.max_video_tokens % self.block_size != 0:
raise AssertionError(
"AR-Diffusion paged attention requires max_video_tokens to be block-aligned, "
f"got max_video_tokens={self.max_video_tokens}, block_size={self.block_size}"
)
all_video_blocks = self.history_block_ids + self.current_video_block_ids
max_video_blocks = self.max_video_tokens // self.block_size
sink_blocks = int(self.kv_cache.spec.sink_chunks)
if len(all_video_blocks) <= max_video_blocks:
visible_video_blocks = all_video_blocks
else:
tail_blocks = max_video_blocks - sink_blocks
visible_video_blocks = all_video_blocks[:sink_blocks]
if tail_blocks:
visible_video_blocks += all_video_blocks[-tail_blocks:]
video_len = len(visible_video_blocks) * self.block_size
block_size = self.block_size
history = self._history_tokens
end = history + self.seq_len
sink_end = self.sink_tokens
recent_start = end - self.window_tokens
# Position of each block's first slot. History comes from the block
# table, where an evicted block is a null placeholder, so a block's index
# is its position -- in storage positions, which is what
# num_computed_tokens counts once compaction has dropped an evicted gap.
# Compaction shifts storage by whole chunks and whole blocks, so the sink,
# the window and every block offset sit exactly where they would in model
# positions. Reading the table after ensure_video_slots means a block
# this chunk's own allocation evicted is never listed. The current
# chunk's blocks follow from where its first token was placed; its first
# block is the history's own tail block whenever the history stopped
# part way through one, and keeps the history's position.
first_position: dict[int, int] = {}
#
# Only two index ranges can hold a token the window keeps: the sink's
# blocks, and the blocks from the recent window's start onward. Evicted
# positions stay in the table as null entries until compaction drops
# them a whole number of chunks at a time, so the table can still be
# much longer than the window; reading just these two ranges, without
# copying the table, keeps this at the window's size.
history_blocks = -(-history // block_size)
sink_blocks = min(-(-sink_end // block_size), history_blocks)
recent_first_block = max(max(recent_start, 0) // block_size, sink_blocks)
indices = [*range(sink_blocks), *range(recent_first_block, history_blocks)]
for index, block in zip(indices, self.kv_cache.block_ids_at(self.adapter, indices)):
if block != self.kv_cache.null_block_id:
first_position.setdefault(block, index * block_size)
current_start = history - self._current_offset
for index, block in enumerate(self.current_video_block_ids):
first_position.setdefault(int(block), current_start + index * block_size)
visible_video_blocks: list[int] = []
video_len = 0
for block, first in sorted(first_position.items(), key=lambda item: item[1]):
if first >= sink_end and first + block_size <= recent_start:
continue # wholly between the sink and the recent window
if video_len % block_size:
raise RuntimeError(
"AR-Diffusion paged attention would read past a partly written block: "
f"block {visible_video_blocks[-1]} is followed by block {block}."
)
visible_video_blocks.append(block)
video_len += min(block_size, end - first)
return visible_video_blocks, video_len
def build_block_table(
@@ -200,6 +356,21 @@ class ARDiffusionPagedForwardContext:
``ceil(seq_lens/block_size)`` entries, so padding is never read.
"""
video_blocks, video_len = self.video_block_table(device)
# Action K/V is listed after the video blocks and the kernel reads the
# table as one run, so the video run has to end on a block edge. If it
# stops part way through its last block, the run reads that block's
# unwritten slots as the first action tokens and never reaches the last
# ones -- a wrong attention with every shape intact. A frame that is a
# whole number of blocks always ends on an edge, so this refuses only
# geometries that could not page at all before the paging unit was
# separated from the frame.
if action_len > 0 and video_len % self.block_size:
raise ValueError(
"AR-Diffusion paged attention cannot place action tokens after a partly written video block: "
f"the video run ends {video_len % self.block_size} slots into block {video_blocks[-1]}, so the "
"kernel would read its unwritten slots as action tokens. Action tokens need tokens_per_frame "
f"to be a whole number of blocks (block_size={self.block_size})."
)
self.ensure_action_slots(action_len, device)
action_blocks = self.action_scratch_block_ids if action_len > 0 else []
block_ids = video_blocks + action_blocks
@@ -208,12 +379,19 @@ class ARDiffusionPagedForwardContext:
# Fixed capacity: full visible video window + one action-capacity block.
action_capacity_blocks = max(1, (action_len + self.block_size - 1) // self.block_size)
width = max(self.max_video_tokens // self.block_size + action_capacity_blocks, len(block_ids))
# Both of these count in blocks. Deriving them from the token count by
# flooring would understate the capacity whenever the window is not a
# whole number of blocks: the width would fall back on len(block_ids)
# and start tracking how full the window is -- the exact shape churn
# the fixed width exists to prevent -- and max_seq_len could come out
# below the kv_len actually being passed, by up to block_size - 1.
capacity_blocks = self.max_video_blocks + action_capacity_blocks
width = max(capacity_blocks, len(block_ids))
padded = block_ids + [0] * (width - len(block_ids))
self.query_len = int(query_len)
self.kv_len = int(video_len + action_len)
max_seq_len = int(self.max_video_tokens + action_capacity_blocks * self.block_size)
max_seq_len = int(capacity_blocks * self.block_size)
# Built on the host once per AR block; the copies go through the same
# pinned, non-blocking path as the slot mappings above so the CPU does
# not wait for the stream to drain three times before the first layer.
@@ -281,7 +459,11 @@ class ARDiffusionPagedForwardContext:
int(self.seq_len),
)
same_session = state.adapter is not None and state.adapter() is self.adapter
reuse = same_session and state.signature == signature
# A chunk that is not a whole number of blocks shares its first block with the history's tail (see
# ensure_video_slots), and the attention op restages the current chunk in whole blocks of its own,
# so such a forward restages the whole window instead.
block_aligned = self.start_offset == 0 and self.seq_len % self.block_size == 0
reuse = same_session and state.signature == signature and block_aligned
state.adapter = weakref.ref(self.adapter)
state.signature = signature
self.staging_enabled = True
@@ -461,6 +643,36 @@ def _resolve_fa_version(head_size: int) -> int:
return version
def supported_kernel_block_sizes() -> list[int | MultipleOf]:
"""Block sizes the kernel this module dispatches to will accept.
Same shape as vLLM's ``AttentionBackend.get_supported_kernel_block_sizes``
-- a plain int is that exact size, ``MultipleOf(b)`` is any positive
multiple of ``b`` -- but answered here rather than read off a backend,
because AR-Diffusion does not go through backend selection. It calls
``flash_attn_varlen_func`` itself, choosing between vLLM's CUDA build and
ROCm's AITER a few lines below, and only this module knows which.
It lives next to that choice so there is one place to change. The
constraint is a property of the kernel, not of the card, and the kernels
reachable from here agree on 16 today: vLLM's CUDA FlashAttention, ROCm
AITER, and upstream ``flash_attn`` all advertise ``MultipleOf(16)``. Other
backends in the same tree do not -- ``hpc_attn`` accepts only 64, and
FlashInfer advertises pages of 128 or more solely on Blackwell -- so a
caller must treat this as data to be queried, never as the number 16.
vLLM's FlashAttention backend does advertise a single 128-token page when FA4
runs its dedicated head-size-256 kernel on SM100/SM110. That kernel is not
reachable from here: ``get_flash_attn_version`` selects it only when the
caller passes ``supports_fa4_hd256=True``, and :func:`_resolve_fa_version`
does not, so a head size of 256 falls back to FA2, which pages at any
multiple of 16. Passing that flag later has to change this answer as well.
"""
from vllm.v1.attention.backend import MultipleOf
return [MultipleOf(16)]
def _rocm_flash_attn_varlen_func():
"""Resolve a ROCm varlen kernel, preferring AITER when available.
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Model-facing session state backed by paged AR-Diffusion KV storage."""
from __future__ import annotations
+55 -2
View File
@@ -5,8 +5,9 @@ from __future__ import annotations
import dataclasses
import time
from collections import OrderedDict
from collections.abc import Iterator
from collections.abc import Iterator, Sequence
from contextlib import contextmanager
from typing import TYPE_CHECKING
import torch
from vllm.logger import init_logger
@@ -29,9 +30,61 @@ from vllm_omni.experimental.ar_diffusion.kv_cache.state import ARDiffusionKVStat
from vllm_omni.experimental.ar_diffusion.tick_protocol import ARDiffusionTickRequest
from vllm_omni.platforms import current_omni_platform
if TYPE_CHECKING:
from vllm.v1.attention.backend import MultipleOf
logger = init_logger(__name__)
def paging_block_size(tokens_per_frame: int, supported: Sequence[int | MultipleOf] | None = None) -> int:
"""Choose the paging unit, which is not the eviction unit.
AR-Diffusion evicts by frame; the attention kernel tiles by a fixed number
of tokens. Using the frame as the block binds every output resolution to
that kernel constraint, so the resolutions that run today do so by
arithmetic accident -- and the LingBot World v2 checkpoint's own default
832x480 is not one of them:
(480/16) x (832/16) = 30 x 52 = 1560 tokens per frame, 1560 % 16 = 8
A frame that is already a legal block stays one, so every resolution that
works today keeps precisely the paging it has now. Anything else pages at
the finest legal unit, which costs a longer block table and gives back the
rounding waste of a block one whole frame wide -- at 832x480 that block is
over a gigabyte.
What counts as legal is asked, not assumed. ``supported`` carries vLLM's
own vocabulary: a plain int is that exact size, ``MultipleOf(b)`` is any
positive multiple of ``b``. The kernels AR-Diffusion reaches agree on
multiples of 16, but others in the same tree do not -- ``hpc_attn`` takes
only 64, and FlashInfer offers pages of 128 and up on Blackwell alone. A
constant here would be a silently wrong block size on the first machine
whose kernel differs, and a wrong block size is not an error: it is a
kernel reading tokens that were never written.
"""
if supported is None:
# Imported here, not at module scope: paged_attention registers a
# custom op on import, and the tests that pop it from sys.modules to
# re-import it depend on nothing else holding it first.
from vllm_omni.experimental.ar_diffusion.kv_cache.paged_attention import supported_kernel_block_sizes
supported = supported_kernel_block_sizes()
if not supported:
raise ValueError("The attention kernel advertises no legal block size.")
def legal(size: int) -> bool:
return size > 0 and any(
size == entry if isinstance(entry, int) else size % entry.base == 0 for entry in supported
)
if legal(tokens_per_frame):
return tokens_per_frame
finest = min(entry if isinstance(entry, int) else entry.base for entry in supported)
if not legal(finest):
raise ValueError(f"No legal paging unit among {supported!r}.")
return finest
def resolve_ar_diffusion_kv_config(od_config: OmniDiffusionConfig) -> ARDiffusionKVConfig:
"""Resolve optional deployment overrides and enable AR-Diffusion KV."""
raw = getattr(od_config, "ar_diffusion_kv_config", None)
@@ -139,7 +192,7 @@ class ARDiffusionModelRunner(DiffusionModelRunner):
num_kv_heads=spec.num_kv_heads,
head_size=spec.head_size,
dtype=self.od_config.dtype,
block_size=spec.tokens_per_frame,
block_size=paging_block_size(spec.tokens_per_frame),
max_model_len=spec.max_model_len,
available_bytes=self._available_memory_bytes() if available_bytes is None else available_bytes,
kv_branches=spec.kv_branches,
+63 -36
View File
@@ -4,6 +4,7 @@
from __future__ import annotations
import copy
import logging
import time
from collections import defaultdict
from collections.abc import Callable, Sequence
@@ -851,9 +852,62 @@ class OrchestratorAggregator:
for cached_key in [k for k in self._replica_cache if k[1] == rid_key]:
self._replica_cache.pop(cached_key, None)
def log_timing_summary(self) -> None:
"""Emit only the concise per-request ``[OmniTiming]`` lines.
The serving path uses this unless DEBUG tables are requested; it skips
the table rows that ``build_and_log_summary`` builds for its result.
"""
if not self.log_stats:
return
if logger.isEnabledFor(logging.DEBUG):
self.build_and_log_summary()
return
for rid in sorted(set(self.stage_events.keys()) | {e.request_id for e in self.e2e_events}):
e2e_evt = next((e for e in self.e2e_events if e.request_id == rid), None)
stage_evts = sorted(
self.stage_events.get(rid, []),
key=lambda e: e.stage_id if e.stage_id is not None else -1,
)
self._log_omni_timing(rid, e2e_evt, stage_evts)
def _log_omni_timing(self, rid: str, e2e_evt: RequestE2EStats | None, stage_evts: list[StageRequestStats]) -> None:
pt: dict[str, float] = {}
if stage_evts:
pt = stage_evts[-1].pipeline_timings or {}
if pt or e2e_evt:
parts = [f"req={rid}"]
if e2e_evt:
parts.append(f"total={e2e_evt.e2e_total_ms / 1000.0:.2f}s")
if "preprocess_ms" in pt:
parts.append(f"preprocess={pt['preprocess_ms'] / 1000.0:.2f}s")
if e2e_evt:
engine_ms = e2e_evt.e2e_total_ms - pt.get("preprocess_ms", 0.0)
parts.append(f"engine={engine_ms / 1000.0:.2f}s")
stage_parts = []
for evt in stage_evts:
sid = evt.stage_id if evt.stage_id is not None else "?"
t = evt.stage_gen_time_ms / 1000.0
stage_parts.append(f"{sid}:{t:.2f}s")
if stage_parts:
parts.append(f"stages=[{','.join(stage_parts)}]")
transfer_parts = []
for te in self.transfer_events.values():
if te.request_id == rid:
transfer_parts.append(f"{te.from_stage}->{te.to_stage}={te.tx_time_ms:.2f}ms")
if transfer_parts:
parts.append(f"transfers=[{','.join(transfer_parts)}]")
if "ar2diffusion_ms" in pt:
parts.append(f"ar2diffusion={pt['ar2diffusion_ms']:.2f}ms")
logger.info("[OmniTiming] %s", " ".join(parts))
def build_and_log_summary(self) -> dict[str, Any]:
if not self.log_stats:
return {}
# Per-request tables are diagnostic. Formatting them on the serving
# event loop costs more than the request's own bookkeeping, so they
# are emitted at DEBUG; the concise [OmniTiming] line stays at INFO.
log_tables = logger.isEnabledFor(logging.DEBUG)
wall_time_ms = max(0.0, (self.last_finish_ts - self.wall_start_ts) * 1000.0)
e2e_avg_req = (wall_time_ms / self.e2e_count) if self.e2e_count > 0 else 0.0
e2e_avg_tok = (self.e2e_total_tokens * 1000.0 / wall_time_ms) if wall_time_ms > 0 else 0.0
@@ -895,8 +949,8 @@ class OrchestratorAggregator:
v = overall_summary.get(k, None)
if v not in (0, 0.0, 0.000, None, ""):
overall_fields.append(k)
if overall_fields:
logger.info(
if log_tables and overall_fields:
logger.debug(
"\n%s",
_format_table("Overall Summary", overall_summary, overall_fields),
)
@@ -921,8 +975,8 @@ class OrchestratorAggregator:
nonzero_e2e_fields.add(k)
value_fields_e2e = sorted(nonzero_e2e_fields)
if value_fields_e2e:
logger.info(
if log_tables and value_fields_e2e:
logger.debug(
"\n%s",
_format_table(
f"RequestE2EStats [request_id={rid}]",
@@ -936,34 +990,7 @@ class OrchestratorAggregator:
self.stage_events.get(rid, []),
key=lambda e: e.stage_id if e.stage_id is not None else -1,
)
pt = {}
if stage_evts:
pt = stage_evts[-1].pipeline_timings or {}
if pt or e2e_evt:
parts = [f"req={rid}"]
if e2e_evt:
parts.append(f"total={e2e_evt.e2e_total_ms / 1000.0:.2f}s")
if "preprocess_ms" in pt:
parts.append(f"preprocess={pt['preprocess_ms'] / 1000.0:.2f}s")
if e2e_evt:
engine_ms = e2e_evt.e2e_total_ms - pt.get("preprocess_ms", 0.0)
parts.append(f"engine={engine_ms / 1000.0:.2f}s")
stage_parts = []
for evt in stage_evts:
sid = evt.stage_id if evt.stage_id is not None else "?"
t = evt.stage_gen_time_ms / 1000.0
stage_parts.append(f"{sid}:{t:.2f}s")
if stage_parts:
parts.append(f"stages=[{','.join(stage_parts)}]")
transfer_parts = []
for te in self.transfer_events.values():
if te.request_id == rid:
transfer_parts.append(f"{te.from_stage}->{te.to_stage}={te.tx_time_ms:.2f}ms")
if transfer_parts:
parts.append(f"transfers=[{','.join(transfer_parts)}]")
if "ar2diffusion_ms" in pt:
parts.append(f"ar2diffusion={pt['ar2diffusion_ms']:.2f}ms")
logger.info("[OmniTiming] %s", " ".join(parts))
self._log_omni_timing(rid, e2e_evt, stage_evts)
# === Stage table (columns = stage_id) ===
# if any stage has diffusion_metrics, remove postprocess_time_ms field
@@ -987,7 +1014,7 @@ class OrchestratorAggregator:
result_stage_table.append({"request_id": rid, "stages": stage_rows})
if stage_rows:
if log_tables and stage_rows:
# filter out all-zero fields for logging
all_value_fields = set()
for row in stage_rows:
@@ -1006,7 +1033,7 @@ class OrchestratorAggregator:
value_fields_list.append(field)
if value_fields_list:
logger.info(
logger.debug(
"\n%s",
_format_table(
f"StageRequestStats [request_id={rid}]",
@@ -1027,7 +1054,7 @@ class OrchestratorAggregator:
]
result_trans_table.append({"request_id": rid, "transfers": transfer_rows})
if transfer_rows:
if log_tables and transfer_rows:
# filter out all-zero fields for logging
all_value_fields = set()
for row in transfer_rows:
@@ -1046,7 +1073,7 @@ class OrchestratorAggregator:
value_fields_list.append(field)
if value_fields_list:
logger.info(
logger.debug(
"\n%s",
_format_table(
f"TransferEdgeStats [request_id={rid}]",
@@ -16,6 +16,14 @@ from vllm.platforms import current_platform
from vllm_omni.platforms import current_omni_platform
def _depth_layer_compile_options() -> dict[str, bool]:
"""Keep ROCm away from the unstable Triton GEMM autotune path."""
return {
"epilogue_fusion": False,
"max_autotune": not current_platform.is_rocm(),
}
def sample_logits(
logits: torch.Tensor,
temperature: float,
@@ -283,7 +291,7 @@ class BreezeDepthDecoder(nn.Module):
BreezeDepthLayer.forward,
fullgraph=True,
dynamic=True,
options={"epilogue_fusion": False, "max_autotune": True},
options=_depth_layer_compile_options(),
)
self._compiled_sampler = torch.compile(sample_graph_logits, fullgraph=True, dynamic=False)
entry = BreezeDepthGraph(
@@ -0,0 +1,97 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Fused Gumbel selection after native nucleus probability reductions.
Keep ATen's top-k threshold, sort, softmax and cumulative sum, including
boundary ties. Random numbers remain indexed by original token id. Only the
final mask, Gumbel transform and argmax are fused; no vocabulary scatter is
needed. This avoids changing which tokens survive a nucleus boundary through
different reduction orders.
"""
import torch
from vllm.triton_utils import tl, triton
@triton.jit
def _sorted_nucleus_gumbel(
logits,
indices,
probabilities,
cumulative,
uniforms,
output,
vocab: tl.constexpr,
logits_stride: tl.constexpr,
token_stride: tl.constexpr,
uniform_stride: tl.constexpr,
top_p: tl.constexpr,
block: tl.constexpr,
):
row = tl.program_id(0)
offsets = tl.arange(0, block)
valid = offsets < vocab
values = tl.load(logits + row * logits_stride + offsets * token_stride, valid, other=-float("inf")).to(tl.float32)
if top_p < 1.0:
ids = tl.load(indices + row * vocab + offsets, valid, other=0)
probs = tl.load(probabilities + row * vocab + offsets, valid, other=0)
cdf = tl.load(cumulative + row * vocab + offsets, valid, other=0)
values = tl.where((cdf - probs) >= top_p, -float("inf"), values)
else:
ids = offsets
noise = tl.load(uniforms + row * uniform_stride + ids, valid, other=0.5)
score = values - tl.extra.cuda.libdevice.log(-tl.extra.cuda.libdevice.log(noise))
# torch.argmax returns the first original token for a tie or NaN.
first_nan = tl.min(tl.where(valid & (score != score), ids, 2147483647), 0)
maximum = tl.max(tl.where(valid, score, -float("inf")), 0)
winner = tl.min(tl.where(valid & (score == maximum), ids, 2147483647), 0)
tl.store(output + row, tl.where(first_nan < 2147483647, first_nan, winner))
def sample_top_k_top_p_gumbel(
logits: torch.Tensor, uniforms: torch.Tensor, *, top_k: int, top_p: float
) -> torch.Tensor:
"""Sample CUDA rows without scattering sorted logits back to the vocabulary.
Uniforms are caller-owned FP32 values with the same shape as logits; their
generation and generator advancement are deliberately outside this kernel.
Softmax/scan use the reference ATen operations. The Gumbel logarithms
still use libdevice, so this is not a universal bitwise-equivalence claim.
"""
if logits.ndim != 2 or not logits.is_cuda or uniforms.device != logits.device or torch.version.hip is not None:
raise ValueError("fused sampling requires two-dimensional CUDA logits and uniforms")
if uniforms.shape != logits.shape or uniforms.dtype != torch.float32 or uniforms.stride(1) != 1:
raise ValueError("uniforms must be FP32 with the logits shape and contiguous rows")
rows, vocab = logits.shape
# The stored-mode caller disables top-k for both 0 and negative values
# (vLLM commonly uses -1). Keep that behavior when fusion is enabled.
top_k = max(top_k, 0)
if vocab == 0 or top_k > vocab or not 0 < top_p <= 1:
raise ValueError("invalid top-k/top-p sampling parameters")
if top_k:
threshold = logits.topk(top_k, dim=-1).values[:, -1:]
logits = logits.masked_fill(logits < threshold, -float("inf"))
indices = probabilities = cumulative = None
if top_p < 1:
logits, indices = logits.sort(dim=-1, descending=True)
probabilities = logits.softmax(dim=-1, dtype=torch.float32)
cumulative = probabilities.cumsum(-1)
result = torch.empty((rows, 1), dtype=torch.long, device=logits.device)
if rows:
_sorted_nucleus_gumbel[(rows,)](
logits,
indices,
probabilities,
cumulative,
uniforms,
result,
vocab,
logits.stride(0),
logits.stride(1),
uniforms.stride(0),
top_p,
triton.next_power_of_2(vocab),
num_warps=4,
enable_fp_fusion=False,
)
return result
@@ -219,7 +219,8 @@ class ISTFTHead(FourierHead):
# phase = torch.atan2(y, x)
# S = mag * torch.exp(phase * 1j)
# better directly produce the complex value
S = mag * (x + 1j * y)
# float32 operands: bf16 promotes to bcomplex32, which torch.fft cannot handle
S = mag.float() * (x.float() + 1j * y.float())
audio, audio_buffer, window_buffer = self.istft(
S, audio_buffer=audio_buffer, window_buffer=window_buffer, streaming=streaming, last_chunk=last_chunk
)
@@ -18,6 +18,7 @@ Shared by Qwen3-Omni and Qwen3-TTS talker models.
from __future__ import annotations
import dataclasses
import time
from collections.abc import Iterable, Sequence
import torch
@@ -158,8 +159,8 @@ class CodePredictorAttention(nn.Module):
"""Multi-head self-attention for code predictor.
Uses ``F.scaled_dot_product_attention`` with HF-compatible RoPE and RMSNorm.
No KV cache -- the code predictor always re-prefills the full (short)
sequence each AR step.
The default path re-prefills the short sequence; the opt-in cached path
appends only new positions.
Input : [B, seq_len, hidden_size]
Output: [B, seq_len, hidden_size]
@@ -316,6 +317,53 @@ class CodePredictorAttention(nn.Module):
attn_out = attn_out.transpose(1, 2).reshape(bsz, seq_len, -1)
return self.o_proj(attn_out)
def forward_cached(
self,
hidden_states: torch.Tensor,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
k_cache: torch.Tensor,
v_cache: torch.Tensor,
write_positions: torch.Tensor,
key_mask: torch.Tensor,
) -> torch.Tensor:
"""Attend only the new positions, reusing earlier positions' keys and values.
``k_cache``/``v_cache`` are ``[B, num_kv_heads, max_seq, head_dim]``;
the new positions' keys and values are written at ``write_positions``
(``[n]``). ``key_mask`` (``[1, 1, n, max_seq]``, bool) keeps key
positions up to each query's, so slots not yet written in this call are
never read and every single-position step has the same shapes.
"""
bsz, seq_len, _ = hidden_states.shape
qkv = self.qkv_proj(hidden_states)
q_raw, k_raw, v_raw = self._split_qkv(qkv)
q = self.q_norm(q_raw.view(bsz, seq_len, self.num_heads, self.head_dim)).transpose(1, 2)
k = self.k_norm(k_raw.view(bsz, seq_len, self.num_kv_heads, self.head_dim)).transpose(1, 2)
v = v_raw.view(bsz, seq_len, self.num_kv_heads, self.head_dim).transpose(1, 2)
cos, sin = position_embeddings
cos = cos.unsqueeze(1)
sin = sin.unsqueeze(1)
q = (q * cos) + (_rotate_half(q) * sin)
k = (k * cos) + (_rotate_half(k) * sin)
k_cache.index_copy_(2, write_positions, k.to(k_cache.dtype))
v_cache.index_copy_(2, write_positions, v.to(v_cache.dtype))
if getattr(self, "use_short_kv_attention", False):
from .short_kv_attention import short_kv_attention
attn_out = short_kv_attention(q, k_cache, v_cache, write_positions, self.scaling)
else:
attn_out = F.scaled_dot_product_attention(
q,
k_cache,
v_cache,
attn_mask=key_mask,
scale=self.scaling,
enable_gqa=self.is_gqa,
)
attn_out = attn_out.transpose(1, 2).reshape(bsz, seq_len, -1)
return self.o_proj(attn_out)
# ===================================================================
# MLP
@@ -351,7 +399,7 @@ class CodePredictorMLP(nn.Module):
class CodePredictorDecoderLayer(nn.Module):
"""Transformer decoder layer (SDPA, no KV cache)."""
"""Transformer decoder layer with default re-prefill and opt-in KV reuse."""
def __init__(self, config, *, prefix: str = "") -> None:
super().__init__()
@@ -383,9 +431,29 @@ class CodePredictorDecoderLayer(nn.Module):
hidden_states = residual + hidden_states
return hidden_states
def forward_cached(
self,
hidden_states: torch.Tensor,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
k_cache: torch.Tensor,
v_cache: torch.Tensor,
write_positions: torch.Tensor,
key_mask: torch.Tensor,
) -> torch.Tensor:
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
hidden_states = self.self_attn.forward_cached(
hidden_states, position_embeddings, k_cache, v_cache, write_positions, key_mask
)
hidden_states = residual + hidden_states
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
return residual + hidden_states
# ===================================================================
# Base Transformer Model (re-prefill, no KV cache)
# Base Transformer Model (re-prefill, or incremental with a K/V buffer)
# ===================================================================
@@ -454,6 +522,29 @@ class CodePredictorBaseModel(nn.Module):
hidden_states = self.norm(hidden_states)
return hidden_states.to(input_dtype)
def forward_cached(
self,
inputs_embeds: torch.Tensor,
position_ids: torch.Tensor,
kv_caches: Sequence[tuple[torch.Tensor, torch.Tensor]],
write_positions: torch.Tensor,
key_mask: torch.Tensor,
) -> torch.Tensor:
"""``forward`` for the new positions only (``position_ids`` ``[B, n]``), reusing earlier K/V."""
input_dtype = inputs_embeds.dtype
use_fp32 = input_dtype == torch.float16 and inputs_embeds.device.type not in ("cpu", "npu")
if use_fp32:
inputs_embeds = inputs_embeds.float()
hidden_states = inputs_embeds
with torch.amp.autocast(inputs_embeds.device.type, enabled=use_fp32, dtype=torch.float32):
position_embeddings = self.rotary_emb(hidden_states, position_ids)
for layer, (k_cache, v_cache) in zip(self.layers, kv_caches):
hidden_states = layer.forward_cached(
hidden_states, position_embeddings, k_cache, v_cache, write_positions, key_mask
)
hidden_states = self.norm(hidden_states)
return hidden_states.to(input_dtype)
# Mapping from HF checkpoint shard name to the (fused parameter,
# slice index) pair used to assemble the fused tensor. See
# ``load_weights`` below.
@@ -626,8 +717,14 @@ class CodePredictorWrapper(nn.Module):
4. torch.compile on inner transformer.
5. Inline sampling (top-k + top-p) -- no custom op overhead.
6. Optional manual CUDA graph capture per batch-size bucket.
``code_predictor_kv_cache`` (CUDA, opt-in) switches to incremental K/V
decoding: at large batches the re-prefill FLOPs are not negligible.
"""
_kv_cache_enabled: bool = False
_fused_sampling_enabled: bool = False
def __init__(
self,
*,
@@ -672,6 +769,7 @@ class CodePredictorWrapper(nn.Module):
# Sampling defaults for "stored" mode
self._top_k: int = 50
self._top_p: float = 0.8
self._do_sample: bool = True
# Lazily initialised state
self._proj_buf: torch.Tensor | None = None
@@ -686,6 +784,7 @@ class CodePredictorWrapper(nn.Module):
# ``None`` keeps the legacy power-of-two bucket derivation.
self._execution_batch_buckets: list[int] | None = None
prefix_graph_cfg = self._stage_connector_extra_config(vllm_config)
self._fused_sampling_enabled = self._parse_bool_config(prefix_graph_cfg.get("code_predictor_fused_sampling"))
prefix_graphs_requested = self._parse_bool_config(prefix_graph_cfg.get("code_predictor_prefix_graphs"))
is_npu = current_omni_platform.is_npu()
# MRv2 captures the whole Talker MTP call. It can therefore capture
@@ -705,15 +804,28 @@ class CodePredictorWrapper(nn.Module):
prefix_graph_cfg.get("code_predictor_prefix_graph_seq_lens")
)
self._prefix_reprefill_seq_lens = tuple(self._prefix_seq_lens(self._num_groups + 1))
# Incremental decoding (opt-in, CUDA): each AR step runs only its new
# position against a static K/V buffer instead of re-prefilling the
# whole prefix (at batch 64 the re-prefill runs 17x the positions).
self._kv_cache_enabled = (
self._parse_bool_config(prefix_graph_cfg.get("code_predictor_kv_cache")) and current_omni_platform.is_cuda()
)
self._short_kv_requested = self._parse_bool_config(prefix_graph_cfg.get("code_predictor_short_kv"))
self._short_kv_enabled = False
self._kv_buf: torch.Tensor | None = None
self._compiled_kv_fwd = None
# Per AR step: (new positions [n], key mask [1, 1, n, max_seq]).
self._kv_steps: dict[int, tuple[torch.Tensor, torch.Tensor]] = {}
def get_input_embeddings(self) -> nn.ModuleList:
return self.model.get_input_embeddings()
def set_sampling_params(self, top_k: int = 50, top_p: float = 0.8) -> None:
"""Configure sampling parameters to maintain consistency with previous implementation."""
def set_sampling_params(self, top_k: int = 50, top_p: float = 0.8, do_sample: bool = True) -> None:
"""Configure "stored"-mode sampling; ``do_sample=False`` selects greedy argmax."""
self._top_k = top_k
self._top_p = top_p
logger.debug("Sampling parameters updated: top_k=%d, top_p=%.2f", top_k, top_p)
self._do_sample = do_sample
logger.debug("Sampling parameters updated: top_k=%d, top_p=%.2f, do_sample=%s", top_k, top_p, do_sample)
# ------------------------------------------------------------------
# Lazy-init helpers
@@ -730,6 +842,35 @@ class CodePredictorWrapper(nn.Module):
):
return
self._proj_buf = torch.zeros(bsz, max_seq, self._cp_hidden, dtype=dtype, device=device)
if self._kv_cache_enabled:
cfg = self.config
head_dim = int(getattr(cfg, "head_dim", None) or cfg.hidden_size // cfg.num_attention_heads)
self._kv_buf = torch.zeros(
int(cfg.num_hidden_layers),
2,
bsz,
int(cfg.num_key_value_heads),
max_seq,
head_dim,
dtype=dtype,
device=device,
)
def _kv_views(self, bsz: int) -> list[tuple[torch.Tensor, torch.Tensor]]:
kv_buf = self._kv_buf
assert kv_buf is not None
return [(kv_buf[i, 0, :bsz], kv_buf[i, 1, :bsz]) for i in range(kv_buf.shape[0])]
def _ensure_kv_steps(self, device: torch.device) -> None:
if self._kv_steps:
return
max_seq = self._num_groups + 1
keys = torch.arange(max_seq, device=device)
for step in range(1, self._num_groups):
# Step 1 runs positions 0 (talker hidden) and 1 (CB0); step s > 1 runs position s.
positions = torch.arange(0, 2, device=device) if step == 1 else torch.tensor([step], device=device)
key_mask = (keys[None, :] <= positions[:, None]).view(1, 1, positions.numel(), max_seq)
self._kv_steps[step] = (positions, key_mask)
def _setup_compile(self) -> None:
"""Lazily set up torch.compile with optional device graph capture."""
@@ -742,6 +883,9 @@ class CodePredictorWrapper(nn.Module):
self._model_dtype = next(self.model.parameters()).dtype
self._lm_heads_list = list(self.lm_head)
self._codec_embeds_list = list(self.model.codec_embedding)
if self._model_dtype == torch.float16 or not current_omni_platform.supports_torch_inductor():
# The fp16 body runs in fp32 under autocast; keep re-prefill there.
self._kv_cache_enabled = False
# Torch 2.13 XPU Dynamo can double-register built-in handlers when
# spawned workers compile this predictor. Keep this narrow path eager
@@ -768,10 +912,28 @@ class CodePredictorWrapper(nn.Module):
dynamic=False,
options={"epilogue_fusion": False},
)
self._short_kv_enabled = (
self._kv_cache_enabled
and self._short_kv_requested
and current_omni_platform.is_cuda()
and self._model_dtype == torch.bfloat16
)
for layer in self.model.layers:
layer.self_attn.use_short_kv_attention = self._short_kv_enabled
if self._kv_cache_enabled:
self._compiled_kv_fwd = torch.compile(
self.model.forward_cached,
dynamic=self._short_kv_enabled,
options={"epilogue_fusion": False},
)
with torch._dynamo.config.patch(cache_size_limit=self._compile_cache_size_limit()):
self._warmup_buckets()
if self._wrapper_config.use_cuda_graphs:
if self._kv_cache_enabled:
# Fifteen short steps per call: they rely on the runner capturing
# the whole MTP call (MRv2) rather than on per-step inner graphs.
logger.info("code_predictor: torch.compile + incremental K/V decoding")
elif self._wrapper_config.use_cuda_graphs:
self._capture_cuda_graphs()
logger.info("code_predictor: torch.compile (no epilogue fusion) + CUDA graphs")
else:
@@ -792,7 +954,13 @@ class CodePredictorWrapper(nn.Module):
extra_cfg = connector_cfg.get("extra", connector_cfg)
else:
extra_cfg = getattr(connector_cfg, "extra", None)
return extra_cfg if isinstance(extra_cfg, dict) else {}
extra_cfg = extra_cfg if isinstance(extra_cfg, dict) else {}
# A single-stage deployment has no connector edge to carry model
# options; it sets them in the stage's ``additional_config``.
additional = getattr(vllm_config, "additional_config", None)
if isinstance(additional, dict) and additional:
return {**additional, **extra_cfg}
return extra_cfg
@staticmethod
def _parse_bool_config(value: object) -> bool:
@@ -840,13 +1008,13 @@ class CodePredictorWrapper(nn.Module):
return row_generators
@classmethod
def _sample_codes_gumbel(
def _sampling_uniforms(
cls,
logits: torch.Tensor,
generator: _GeneratorLike = None,
uniforms: torch.Tensor | None = None,
) -> torch.Tensor:
"""Sample ``logits`` via Gumbel-max with optional precomputed noise."""
"""Draw one FP32 uniform per original token, preserving generator state."""
if uniforms is None:
row_generators = cls._normalize_generators(generator, int(logits.shape[0]))
u = torch.empty_like(logits, dtype=torch.float32)
@@ -862,6 +1030,17 @@ class CodePredictorWrapper(nn.Module):
f"logits={tuple(logits.shape)}"
)
u = uniforms
return u
@classmethod
def _sample_codes_gumbel(
cls,
logits: torch.Tensor,
generator: _GeneratorLike = None,
uniforms: torch.Tensor | None = None,
) -> torch.Tensor:
"""Sample ``logits`` via Gumbel-max with optional precomputed noise."""
u = cls._sampling_uniforms(logits, generator, uniforms)
return (logits.float() - torch.log(-torch.log(u))).argmax(dim=-1, keepdim=True)
def _prefix_seq_lens(self, max_seq: int) -> list[int]:
@@ -924,6 +1103,8 @@ class CodePredictorWrapper(nn.Module):
if needs_full_graph:
full_graph_entries += len(prefix_buckets)
required_entries = full_graph_entries + len(prefix_buckets) * len(prefix_seq_lens)
if self._kv_cache_enabled:
required_entries = 4 # {first step, later steps} x {batch 1, dynamic batch}
return max(torch._dynamo.config.cache_size_limit, required_entries)
def _warmup_buckets(self) -> None:
@@ -938,7 +1119,29 @@ class CodePredictorWrapper(nn.Module):
self._ensure_buffers(device, self._model_dtype, max(self._bucket_sizes))
proj_buf = self._proj_buf
if self._prefix_reprefill_enabled:
if self._kv_cache_enabled:
self._ensure_kv_steps(device)
started = time.perf_counter()
for bsz in self._bucket_sizes:
caches = self._kv_views(bsz)
for step in (1, 2):
positions, key_mask = self._kv_steps[step]
lo = 0 if step == 1 else step
hidden = proj_buf[:bsz, lo : step + 1, :]
pos_ids = positions.unsqueeze(0).expand(bsz, -1)
if bsz > 1:
# One graph for every batch >= 2 per step shape (4 compiles, not 2 per bucket):
# a cold per-bucket compile outlasted the stage handshake timeout.
for tensor in (hidden, pos_ids, *(t for kv in caches for t in kv)):
torch._dynamo.mark_dynamic(tensor, 0)
for _ in range(2):
self._compiled_kv_fwd(hidden, pos_ids, caches, positions, key_mask)
logger.info(
"code_predictor: incremental K/V decoding warmed for buckets %s in %.1f s",
self._bucket_sizes,
time.perf_counter() - started,
)
elif self._prefix_reprefill_enabled:
prefix_seq_lens = self._prefix_reprefill_seq_lens
needs_full_graph = set(prefix_seq_lens) != set(range(2, max_seq))
for bsz in self._bucket_sizes:
@@ -1135,6 +1338,21 @@ class CodePredictorWrapper(nn.Module):
scaled = scaled.masked_fill(scaled < topk_vals[:, -1:], float("-inf"))
return self._sample_codes_gumbel(scaled, generator=generator, uniforms=uniforms)
def _validate_sampling_inputs(
self,
bsz: int,
generators: Sequence[torch.Generator | None] | None,
sample_uniforms: torch.Tensor | None,
) -> None:
if generators is not None and len(generators) != bsz:
raise ValueError(f"generators must have one entry per row: got {len(generators)} for batch {bsz}")
if sample_uniforms is not None:
expected_shape = (bsz, self._num_groups - 1, int(self.config.vocab_size))
if tuple(sample_uniforms.shape) != expected_shape:
raise ValueError(
f"sample_uniforms must have shape {expected_shape}, got {tuple(sample_uniforms.shape)}"
)
@torch.inference_mode()
def forward(
self,
@@ -1152,14 +1370,7 @@ class CodePredictorWrapper(nn.Module):
"""Predict residual codebooks 1..G-1 autoregressively via re-prefill."""
bsz = int(layer0_code.shape[0])
num_groups = self._num_groups
if generators is not None and len(generators) != bsz:
raise ValueError(f"generators must have one entry per row: got {len(generators)} for batch {bsz}")
if sample_uniforms is not None:
expected_shape = (bsz, num_groups - 1, int(self.config.vocab_size))
if tuple(sample_uniforms.shape) != expected_shape:
raise ValueError(
f"sample_uniforms must have shape {expected_shape}, got {tuple(sample_uniforms.shape)}"
)
self._validate_sampling_inputs(bsz, generators, sample_uniforms)
sample_generator: _GeneratorLike = generators if generators is not None else generator
device = layer0_code.device
@@ -1206,9 +1417,29 @@ class CodePredictorWrapper(nn.Module):
all_codes = torch.empty(bsz, num_groups, dtype=torch.long, device=device)
all_codes[:, 0] = layer0_code.reshape(bsz)
kv_caches = None
if self._kv_cache_enabled:
# Masked slots still enter the attention matmul with zero weight, so
# clear them: a non-finite value left by an earlier call would leak.
if not self._short_kv_enabled:
self._kv_buf[:, :, :padded_bsz].zero_()
kv_caches = self._kv_views(padded_bsz)
# Autoregressive loop: predict layers 1..G-1
for step in range(1, num_groups):
logits = self._predict_step_logits(proj_buf, bsz, padded_bsz, step, is_npu_capturing)
if kv_caches is not None:
# Step 1 runs positions 0-1; later steps append one position.
positions, key_mask = self._kv_steps[step]
lo = 0 if step == 1 else step
new_hidden = self._compiled_kv_fwd(
proj_buf[:padded_bsz, lo : step + 1, :],
positions.unsqueeze(0).expand(padded_bsz, -1),
kv_caches,
positions,
key_mask,
)
logits = self._lm_heads_list[step - 1](new_hidden[:bsz, -1, :])
else:
logits = self._predict_step_logits(proj_buf, bsz, padded_bsz, step, is_npu_capturing)
# Sample next code via Gumbel-max.
#
@@ -1222,7 +1453,15 @@ class CodePredictorWrapper(nn.Module):
# defensive than ``multinomial`` around fully-masked/NaN inputs),
# and the helper below can honor either one batch generator or one
# generator per seeded row.
if stored_mode:
if stored_mode and not self._do_sample:
code = logits.argmax(dim=-1, keepdim=True)
elif stored_mode and self._fused_sampling_enabled and logits.is_cuda and torch.version.hip is None:
from .fused_sampling import sample_top_k_top_p_gumbel
step_uniforms = sample_uniforms[:, step - 1, :] if sample_uniforms is not None else None
uniforms = self._sampling_uniforms(logits, sample_generator, step_uniforms)
code = sample_top_k_top_p_gumbel(logits, uniforms, top_k=s_top_k, top_p=s_top_p)
elif stored_mode:
# "stored" mode: top-k -> top-p -> Gumbel-max
if s_top_k > 0:
topk_vals, _ = logits.topk(s_top_k, dim=-1)
@@ -0,0 +1,29 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Shared eligibility; each model separately controls its first-audio opt-in."""
from typing import Any
import torch
from vllm_omni.platforms import current_omni_platform
def supports_talker_first_audio(vllm_config: Any) -> bool:
"""Require CUDA MRv2 streaming, local single-rank execution and no prefix replay.
Other configurations retain the regular codec path. These conditions do
not enable the feature: model-specific options must also accept it.
"""
model = vllm_config.model_config
if not (getattr(model, "use_v2_model_runner", False) and getattr(model, "async_chunk", False)):
return False
parallel = vllm_config.parallel_config
return (
current_omni_platform.is_cuda()
and torch.device(vllm_config.device_config.device).type == "cuda"
and parallel.tensor_parallel_size == 1
and parallel.pipeline_parallel_size == 1
and parallel.distributed_executor_backend in (None, "uni")
and not vllm_config.cache_config.enable_prefix_caching
)
@@ -966,7 +966,20 @@ class CausalHiFTGenerator(HiFTGenerator):
self.conv_pre_look_right = conv_pre_look_right
self.f0_predictor = f0_predictor
def enable_decode_graphs(self) -> None:
"""Opt into exact-shape CUDA replay after frozen weights are loaded."""
from .hift_graph import HiFTDecodeGraphs
self.enable_cached_istft()
self._decode_graphs = HiFTDecodeGraphs(self._decode_eager)
def decode(self, x: torch.Tensor, s: torch.Tensor = torch.zeros(1, 1, 0), finalize: bool = True) -> torch.Tensor:
runner = getattr(self, "_decode_graphs", None)
if runner is not None:
return runner.run(x, s, finalize)
return self._decode_eager(x, s, finalize=finalize)
def _decode_eager(self, x: torch.Tensor, s: torch.Tensor, finalize: bool = True) -> torch.Tensor:
s_stft_real, s_stft_imag = self._stft(s.squeeze(1))
if finalize is True:
x = self.conv_pre(x)
@@ -0,0 +1,85 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Bounded CUDA graph replay for HiFT's deterministic waveform decoder.
F0, sine/noise generation, phase carry, and per-request streaming state remain
outside the graph. Each replay consumes current mel and source tensors and
returns an owned waveform, because multiple requests can reuse one shape.
"""
from collections import OrderedDict
from collections.abc import Callable
from dataclasses import dataclass
import torch
@dataclass
class _DecodeGraph:
graph: torch.cuda.CUDAGraph
mel: torch.Tensor
source: torch.Tensor
output: torch.Tensor
class HiFTDecodeGraphs:
"""Capture recurrent exact shapes, with at most 32 resident graphs.
``decode`` must have warmed its ISTFT envelope before capture and remain
deterministic for its mel, source and finalize arguments. Uncommon shapes,
CPU execution and nested CUDA capture use that same eager implementation.
"""
def __init__(self, decode: Callable[..., torch.Tensor], max_graphs: int = 32) -> None:
self.decode = decode
self.max_graphs = max_graphs
self.graphs: dict[tuple, _DecodeGraph] = {}
self.seen: OrderedDict[tuple, int] = OrderedDict()
self.pool = None
@torch.inference_mode()
def run(self, mel: torch.Tensor, source: torch.Tensor, finalize: bool) -> torch.Tensor:
"""Replay a recurring layout without aliasing another request's output."""
if not mel.is_cuda or not source.is_cuda or torch.cuda.is_current_stream_capturing():
return self.decode(mel, source, finalize=finalize)
autocast = torch.is_autocast_enabled("cuda")
key = (
mel.shape,
source.shape,
mel.device,
mel.dtype,
source.dtype,
finalize,
torch.backends.cudnn.allow_tf32,
torch.backends.cuda.matmul.allow_tf32,
autocast,
torch.get_autocast_dtype("cuda") if autocast else None,
)
state = self.graphs.get(key)
if state is None:
if len(self.graphs) >= self.max_graphs:
return self.decode(mel, source, finalize=finalize)
count = self.seen.pop(key, 0) + 1
self.seen[key] = count
if len(self.seen) > 128:
self.seen.popitem(last=False)
if count < 3:
return self.decode(mel, source, finalize=finalize)
static_mel, static_source = mel.clone(), source.clone()
stream = torch.cuda.Stream(device=mel.device)
stream.wait_stream(torch.cuda.current_stream(mel.device))
with torch.cuda.stream(stream):
self.decode(static_mel, static_source, finalize=finalize)
torch.cuda.current_stream(mel.device).wait_stream(stream)
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph, pool=self.pool):
output = self.decode(static_mel, static_source, finalize=finalize)
if self.pool is None:
self.pool = graph.pool()
state = _DecodeGraph(graph, static_mel, static_source, output)
self.graphs[key] = state
state.mel.copy_(mel)
state.source.copy_(source)
state.graph.replay()
# A later request may replay this shape before the caller consumes it.
return state.output.clone()
@@ -170,7 +170,7 @@ class PackedDiT:
t,
rows,
attention,
max_seqlen_q=attention.page_table.shape[1],
max_seqlen_q=attention.max_seqlen_q,
block_modulation=block_modulation,
final_modulation=final_modulation,
rope=rope,
@@ -771,7 +771,9 @@ class FlowGraphRunner:
attention.heads, attention.head_dim = self.heads, self.head_dim
attention.cache_seqlens, attention.cu_seqlens_q = state.cache_seqlens, state.cu_seqlens_q
attention.page_table = state.page_table
attention.max_seqlen_q = state.page_table.shape[1]
# Streaming queries cover one training chunk, even when their KV
# prefix and the packed graph allocation contain thousands of frames.
attention.max_seqlen_q = estimator.dit.static_chunk_size if key[0] else state.page_table.shape[1]
mark_packed_compile_metadata(rows, attention)
mu_cfg = torch.cat((state.mu, torch.zeros_like(state.mu)), dim=1)
cond_cfg = torch.cat((state.cond, torch.zeros_like(state.cond)), dim=1)
@@ -769,4 +769,11 @@ class CosyVoice3Code2Wav(nn.Module):
if f0_folded == 0:
logger.warning("HiFT F0 predictor had no weight-norm layers to fold; check config drift")
self.hift.eval()
if device.type == "cuda" and cosyvoice3_packed_streaming_enabled():
if os.getenv("COSYVOICE3_CACHED_ISTFT", "0") == "1":
self.hift.enable_cached_istft()
logger.info("CosyVoice3 streaming HiFT: cached ISTFT enabled")
if os.getenv("COSYVOICE3_HIFT_GRAPH", "0") == "1":
self.hift.enable_decode_graphs()
logger.info("CosyVoice3 streaming HiFT: decode CUDA graphs enabled")
logger.info(f"Loaded hift weights from {hift_path}")
@@ -539,11 +539,15 @@ class RingKVCache:
end_offset = self.end_offset.index_select(0, slots)
row_cache = self.cache.index_select(1, slots)
indexes = torch.arange(T, device=end_offset.device, dtype=end_offset.dtype)
# Upsampling can make a chunk longer than the ring. Only the last
# capacity tokens survive; writing all T tokens gives scatter_
# duplicate destinations and nondeterministic cache contents.
write_length = min(T, self.capacity)
indexes = torch.arange(T - write_length, T, device=end_offset.device, dtype=end_offset.dtype)
indexes = (indexes + end_offset.view(-1, 1)) % self.capacity
scatter_indexes = indexes.view(B, 1, T, 1).expand(-1, H, T, D)
row_cache[0].scatter_(2, scatter_indexes, k)
row_cache[1].scatter_(2, scatter_indexes, v)
scatter_indexes = indexes.view(B, 1, write_length, 1).expand(-1, H, write_length, D)
row_cache[0].scatter_(2, scatter_indexes, k[:, :, -write_length:])
row_cache[1].scatter_(2, scatter_indexes, v[:, :, -write_length:])
# Live and graph-padding rows always map to distinct slots. The
# latter map only to scratch state, so this write cannot corrupt a
# request even though dense graph operators still execute it.
@@ -729,7 +733,13 @@ class MossAudioTokenizerMultiheadAttention(StreamingModule):
if self.context is None:
capacity = self.weights_per_step if self.weights_per_step else 1024
else:
capacity = self.context
# RingKVCache.complete writes a whole chunk before attending, so a
# ring of exactly ``context`` entries lets a T-token chunk evict up
# to T tokens of history that the chunk's own queries should still
# see (and, for T > context, evict part of the chunk itself).
# ``_ring_headroom`` (max chunk tokens at this layer) keeps every
# in-window key resident; 0 preserves the legacy truncation.
capacity = self.context + int(getattr(self, "_ring_headroom", 0))
kv_cache = RingKVCache(
batch_size,
@@ -791,29 +801,63 @@ class MossAudioTokenizerMultiheadAttention(StreamingModule):
if self.rope:
q, k = self.rope(q, k, offset, time_before_heads=False)
k, v, pos_k = self._complete_kv(k, v, execution_context)
pos_k = pos_k[:, None]
if self.causal:
pos_q = offset.view(-1, 1, 1) + torch.arange(T, device=q.device, dtype=torch.long).view(-1, 1)
delta = pos_q - pos_k
attn_bias = (pos_k >= 0) & (delta >= 0)
if self.context is not None:
attn_bias = attn_bias & (delta < self.context)
attn_bias = attn_bias[:, None]
slot_attention = getattr(self, "_slot_attention", None)
slot_attention_rows = getattr(self, "_slot_attention_rows", None)
advance_kv_offset = False
slot_path = state is not None and execution_context is not None and self.causal
if slot_path and slot_attention_rows is not None:
# One launch: the gathered MHA offset equals the ring's end_offset
# for every slot, and both advance together below.
valid_lengths = execution_context.valid_rows.to(dtype=torch.int32) * T
x = slot_attention_rows(
q,
k,
v,
state.kv_cache.cache,
offset,
execution_context.state_slot_ids,
valid_lengths,
self.context if self.context is not None else -1,
)
advance_kv_offset = True
elif slot_attention is not None and slot_path:
# Current streaming batches use one exact T. Padding rows own
# scratch slots and must not advance persistent attention state.
valid_lengths = execution_context.valid_rows.to(dtype=torch.int32) * T
x = slot_attention(
q,
k,
v,
state.kv_cache.cache,
state.kv_cache.end_offset,
execution_context.state_slot_ids,
valid_lengths,
self.context if self.context is not None else -1,
)
else:
attn_bias = None
k, v, pos_k = self._complete_kv(k, v, execution_context)
pos_k = pos_k[:, None]
streaming_attention = getattr(self, "_streaming_attention", None)
if (
streaming_attention is not None
and attn_bias is not None
and q.dtype == torch.bfloat16
and q.shape[-1] == 64
):
x = streaming_attention(q, k, v, attn_bias)
else:
x = F.scaled_dot_product_attention(q, k, v, attn_bias, dropout_p=0.0)
if self.causal:
pos_q = offset.view(-1, 1, 1) + torch.arange(T, device=q.device, dtype=torch.long).view(-1, 1)
delta = pos_q - pos_k
attn_bias = (pos_k >= 0) & (delta >= 0)
if self.context is not None:
attn_bias = attn_bias & (delta < self.context)
attn_bias = attn_bias[:, None]
else:
attn_bias = None
streaming_attention = getattr(self, "_streaming_attention", None)
if (
streaming_attention is not None
and attn_bias is not None
and q.dtype == torch.bfloat16
and q.shape[-1] == 64
):
x = streaming_attention(q, k, v, attn_bias)
else:
x = F.scaled_dot_product_attention(q, k, v, attn_bias, dropout_p=0.0)
x = x.transpose(1, 2).reshape(B, T, self.embed_dim)
x = apply_weights_per_step(self.out_projs, self.weights_per_step_schedule, x, offset_cpu)
@@ -824,6 +868,8 @@ class MossAudioTokenizerMultiheadAttention(StreamingModule):
else:
next_offset = torch.where(execution_context.valid_rows, offset + T, offset)
state.offset.index_copy_(0, execution_context.state_slot_ids, next_offset)
if advance_kv_offset:
state.kv_cache.end_offset.index_copy_(0, execution_context.state_slot_ids, next_offset)
return x
@@ -1624,6 +1670,7 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
decoder_kwargs_list = copy.deepcopy(config.decoder_kwargs)
self.decoder = nn.ModuleList()
decoder_input_frame_rate = current_frame_rate
for decoder_kwargs_i in decoder_kwargs_list:
decoder_kwargs_i = dict(decoder_kwargs_i)
if decoder_kwargs_i["module_type"] == "PatchedPretransform":
@@ -1638,6 +1685,11 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
context=int(round(current_frame_rate * context_duration)),
)
)
# Tokens this transformer sees per input code frame. Streaming
# ring headroom is sized from it (see initialize_decoder_state_pool).
self.decoder[-1].tokens_per_input_frame = max(
1, int(round(current_frame_rate / decoder_input_frame_rate))
)
current_frame_rate *= self.decoder[-1].downsample_ratio
expected_output_frame_rate = float(self.sampling_rate * channel_interleave_factor)
@@ -1731,10 +1783,29 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
self._decoder_state_capacity = 0
self._decoder_slot_offsets = None
def initialize_decoder_state_pool(self, state_capacity: int, scratch_capacity: int = 0) -> None:
"""Allocate persistent decoder state independently of execution B."""
if state_capacity <= 0 or scratch_capacity < 0:
raise ValueError(f"Invalid decoder state capacities: state={state_capacity}, scratch={scratch_capacity}.")
def initialize_decoder_state_pool(
self, state_capacity: int, scratch_capacity: int = 0, chunk_frames: int = 0
) -> None:
"""Allocate persistent decoder state independently of execution B.
``chunk_frames`` > 0 sizes every decoder attention ring as
``context + chunk_frames * tokens_per_input_frame`` so that a chunk of
up to ``chunk_frames`` code frames never evicts keys inside its own
causal window; chunked streaming then matches whole-sequence decoding.
``chunk_frames`` = 0 keeps the legacy ``context``-sized rings.
"""
if state_capacity <= 0 or scratch_capacity < 0 or chunk_frames < 0:
raise ValueError(
"Invalid decoder state capacities: "
f"state={state_capacity}, scratch={scratch_capacity}, chunk_frames={chunk_frames}."
)
for module in self.decoder:
if not isinstance(module, MossAudioTokenizerProjectedTransformer):
continue
tokens_per_frame = int(getattr(module, "tokens_per_input_frame", 1))
for attention in module.modules():
if isinstance(attention, MossAudioTokenizerMultiheadAttention):
attention._ring_headroom = chunk_frames * tokens_per_frame
self._start_streaming(state_capacity + scratch_capacity, decoder_only=True)
self._decoder_state_capacity = state_capacity + scratch_capacity
@@ -81,15 +81,31 @@ class CUDAGraphStreamingDecoderWrapper:
frame_sizes: list[int],
num_quantizers: int,
vllm_config: VllmConfig,
scratch_base: int | None = None,
private_pool: bool = False,
compiled_decode: nn.Module | None = None,
) -> None:
self.codec = codec
self.state_capacity = int(state_capacity)
# Graph padding rows use scratch slots [scratch_base, scratch_base+B).
# A wrapper replaying concurrently with another needs its own scratch
# range and graph memory pool.
self.scratch_base = self.state_capacity if scratch_base is None else int(scratch_base)
self._private_pool = bool(private_pool)
# Graphs bake in the cuBLAS workspace of the stream they were captured
# on, and torch.cuda.graph shares one default capture stream. Split-K
# GEMMs synchronize through that workspace, so two graphs replaying
# concurrently from one capture stream can spin on each other forever.
self._capture_stream: torch.cuda.Stream | None = None
self.batch_sizes = sorted({int(size) for size in batch_sizes if 0 < int(size) <= state_capacity})
self.frame_sizes = sorted({int(size) for size in frame_sizes if int(size) > 0})
self.num_quantizers = int(num_quantizers)
self.graphs: dict[tuple[int, int], _CapturedStreamingDecodeGraph] = {}
self._pool = None
self._warmed_up = False
if compiled_decode is not None:
self._compiled_decode: nn.Module | None = compiled_decode
return
# vLLM owns Inductor compilation; this wrapper remains the sole owner
# of CUDA Graph capture/replay because it understands persistent codec
# state slots. Disable vLLM's CUDA Graph layer to avoid nested graphs.
@@ -98,7 +114,7 @@ class CUDAGraphStreamingDecoderWrapper:
compile_config.compilation_config.cudagraph_mode = CUDAGraphMode.NONE
compile_config.compilation_config.static_forward_context = {}
with set_current_vllm_config(compile_config):
self._compiled_decode: nn.Module | None = _MossStreamingDecodeCompileAdapter(
self._compiled_decode = _MossStreamingDecodeCompileAdapter(
codec,
vllm_config=compile_config,
)
@@ -175,7 +191,7 @@ class CUDAGraphStreamingDecoderWrapper:
frame_size,
exc_info=True,
)
scratch_slots = self.state_capacity + torch.arange(
scratch_slots = self.scratch_base + torch.arange(
batch_size,
dtype=torch.long,
device=device,
@@ -206,10 +222,13 @@ class CUDAGraphStreamingDecoderWrapper:
device=device,
)
lengths = torch.zeros(batch_size, dtype=torch.long, device=device)
scratch_slots = self.state_capacity + torch.arange(batch_size, dtype=torch.long, device=device)
scratch_slots = self.scratch_base + torch.arange(batch_size, dtype=torch.long, device=device)
valid_rows = torch.zeros(batch_size, dtype=torch.bool, device=device)
stream = torch.cuda.Stream()
if self._private_pool and self._capture_stream is None:
self._capture_stream = torch.cuda.Stream(device=device)
# Warm up on the capture stream so its workspace exists before capture.
stream = self._capture_stream or torch.cuda.Stream()
stream.wait_stream(torch.cuda.current_stream())
with torch.cuda.stream(stream):
for _ in range(3):
@@ -219,9 +238,11 @@ class CUDAGraphStreamingDecoderWrapper:
self.codec.reset_decoder_state_slots(scratch_slots)
if self._pool is None:
self._pool = current_platform.get_global_graph_pool()
self._pool = (
torch.cuda.graph_pool_handle() if self._private_pool else current_platform.get_global_graph_pool()
)
graph = CUDAGraph()
with torch.cuda.graph(graph, pool=self._pool, capture_error_mode="thread_local"):
with torch.cuda.graph(graph, pool=self._pool, stream=self._capture_stream, capture_error_mode="thread_local"):
audio, audio_lengths = decode(codes, lengths, scratch_slots, valid_rows)
self.graphs[(batch_size, frame_size)] = _CapturedStreamingDecodeGraph(
@@ -272,7 +293,7 @@ class CUDAGraphStreamingDecoderWrapper:
entry.static_lengths.zero_()
entry.static_lengths[:actual_batch_size].fill_(int(frame_size))
entry.static_state_slot_ids.copy_(
self.state_capacity + torch.arange(batch_size, dtype=torch.long, device=entry.static_state_slot_ids.device)
self.scratch_base + torch.arange(batch_size, dtype=torch.long, device=entry.static_state_slot_ids.device)
)
entry.static_state_slot_ids[:actual_batch_size].copy_(state_slot_ids, non_blocking=True)
entry.static_valid_rows.zero_()
@@ -0,0 +1,333 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Decode a stream's first codec chunk as soon as Stage 1 receives it.
The Stage-1 engine loop only sees a new chunk between steps, and a step waits
for its codec graph to finish. A first chunk that arrives mid-step therefore
waits for the rest of that step, then for its own replay. This path takes the
first chunk from the receive thread, replays a small dedicated graph on its
own stream while the main step runs, and emits the audio straight to the
engine output queue. The main path later receives the same chunk, finds it
already decoded, and continues the stream from the state written here.
"""
from __future__ import annotations
import math
import queue
import threading
import time
from dataclasses import dataclass
from typing import Any
import torch
from vllm.logger import init_logger
from vllm_omni.platforms import current_omni_platform
from vllm_omni.worker_v2.first_audio_sender import FirstAudioSink, _PreparedDelivery
logger = init_logger(__name__)
class _SlotHandoff:
"""Orders the main path's first use of a slot after the fast decode."""
__slots__ = ("done", "event", "error")
def __init__(self) -> None:
self.done = threading.Event()
self.event: torch.cuda.Event | None = None
self.error: Exception | None = None
@dataclass
class _Job:
request_id: str
request_key: str
delivery: _PreparedDelivery
slot: int
codes: torch.Tensor # (n_vq, T) int64 on host
handoff: _SlotHandoff
class MossFirstChunkFastPath:
def __init__(
self,
session: Any,
wrapper: Any,
*,
n_vq: int,
frames: int,
codebook_size: int,
samples_per_frame: int,
n_channels: int,
sample_rate: torch.Tensor,
device: torch.device,
gate_main: bool = False,
handoff_timeout_s: float = 30.0,
) -> None:
if not math.isfinite(handoff_timeout_s) or handoff_timeout_s <= 0:
raise ValueError("handoff_timeout_s must be positive and finite")
self._handoff_timeout_s = float(handoff_timeout_s)
self._session = session
# When set, the main codec stream waits for an in-flight fast decode
# before its next replay, so the small first-chunk graph runs mostly
# uncontended instead of interleaving with a large regular replay.
self._gate_main = bool(gate_main)
self._inflight: torch.cuda.Event | None = None
self._wrapper = wrapper
self._n_vq = int(n_vq)
self.frames = int(frames)
self._codebook_size = int(codebook_size)
self._samples = int(frames) * int(samples_per_frame)
self._n_channels = int(n_channels)
self._sr = sample_rate
if device.index is None:
device = torch.device(device.type, torch.accelerator.current_device_index())
self._device = device
self._max_batch = max(wrapper.batch_sizes)
self._stream = torch.cuda.Stream(device=device, priority=-1)
self._host_codes = torch.zeros((self._n_vq, self._max_batch, self.frames), dtype=torch.long).pin_memory()
self._host_slots = torch.zeros(self._max_batch, dtype=torch.long).pin_memory()
audio_shape = (self._max_batch, self._n_channels, self._samples)
self._host_audio = torch.empty(audio_shape, dtype=torch.float32).pin_memory()
self._copied = torch.cuda.Event()
self._sink: FirstAudioSink | None = None
# Request key -> slot for chunks decoded here and not yet seen by the main path.
self._decoded: dict[str, int] = {}
# A failed output-queue handoff must be replayed from the already
# decoded samples. Re-decoding would advance the streaming state twice.
self._unsent_audio: dict[str, torch.Tensor] = {}
self._handoffs: dict[int, _SlotHandoff] = {}
self._lock = threading.Lock()
self._jobs: queue.SimpleQueue[_Job | None] = queue.SimpleQueue()
self._thread: threading.Thread | None = None
self._closed = False
self._worker_error: Exception | None = None
self._emitted = 0
self._batches = 0
self._decode_s = 0.0
def bind(self, sink: FirstAudioSink) -> None:
if not callable(getattr(sink, "prepare", None)):
raise TypeError("The codec first-chunk sink requires prepare(request_ids)")
with self._lock:
if self._closed:
raise RuntimeError("The codec first-chunk decoder is closed")
self._sink = sink
if self._thread is None:
self._thread = threading.Thread(target=self._run, name="moss-codec-first-chunk", daemon=True)
self._thread.start()
def close(self) -> None:
with self._lock:
if not self._closed:
self._closed = True
self._jobs.put(None)
thread = self._thread
if thread is not None:
thread.join(timeout=5.0)
# A stalled decode may still own GPU state: keep its thread and
# handoff, rather than permitting a restart or slot reuse.
if thread.is_alive():
logger.warning("Codec first-chunk decoder is still stopping; slot waits remain bounded")
# ------------------------------------------------------------------
# Receive thread
# ------------------------------------------------------------------
def submit(
self,
request_id: str,
request_key: str,
codes_flat: Any,
req_slots: dict[str, int],
) -> bool:
"""Claim a first chunk for fast decode; False leaves it to the main path."""
# Own the codes: the job outlives this call and the payload it came from.
codes = torch.as_tensor(codes_flat, dtype=torch.long).reshape(-1).clone()
if codes.numel() != self._n_vq * self.frames:
return False
with self._lock:
if (
self._closed
or self._sink is None
or self._thread is None
or not self._thread.is_alive()
or request_key in req_slots
):
return False
delivery = self._sink.prepare([request_id])
if request_id not in delivery.routes:
return False
slot = self._session.acquire(oldest=True)
if slot is None:
return False
handoff = _SlotHandoff()
self._handoffs[slot] = handoff
self._decoded[request_key] = slot
req_slots[request_key] = slot
codes = codes.reshape(self._n_vq, self.frames)
# Enqueue under the admission lock: close's sentinel cannot
# overtake an accepted job.
self._jobs.put(_Job(request_id, request_key, delivery, slot, codes, handoff))
return True
# ------------------------------------------------------------------
# Main (engine) thread
# ------------------------------------------------------------------
def get_request_slot(self, request_key: str, req_slots: dict[str, int]) -> int | None:
"""Observe a completed admission before deciding whether cleanup is needed.
The scheduler removes cancelled routes before the runner's finish hook.
A submit that already froze its route must finish leasing its slot
before that hook checks it. Waiting for decode happens outside this
lock, so the worker can still complete the slot handoff.
"""
with self._lock:
return req_slots.get(request_key)
def take_decoded(self, request_key: str) -> bool:
"""True once per request whose first chunk this path already decoded."""
with self._lock:
return self._decoded.pop(request_key, None) is not None
def take_unsent_audio(self, request_key: str) -> torch.Tensor | None:
"""Return first-chunk samples when the out-of-band sink rejected them."""
with self._lock:
return self._unsent_audio.pop(request_key, None)
def forget(self, request_key: str) -> None:
with self._lock:
self._decoded.pop(request_key, None)
self._unsent_audio.pop(request_key, None)
def gate(self) -> None:
"""Queue the current stream behind the latest fast decode, if still running."""
event = self._inflight
if self._gate_main and event is not None and not event.query():
torch.cuda.current_stream(self._device).wait_event(event)
def order_after(self, slot: int) -> None:
"""Make the current stream's next use of ``slot`` follow the fast decode."""
with self._lock:
handoff = self._handoffs.get(slot)
if handoff is None:
return
if not handoff.done.wait(timeout=self._handoff_timeout_s):
# Keep the handoff. The worker may still be writing the slot, so
# neither cancellation nor a retry may release/re-decode it.
raise TimeoutError(f"Codec first-chunk slot {slot} did not complete within {self._handoff_timeout_s:g}s")
if handoff.error is not None:
raise RuntimeError(f"Codec first-chunk decode failed for slot {slot}") from handoff.error
if handoff.event is not None:
torch.cuda.current_stream(self._device).wait_event(handoff.event)
with self._lock:
self._handoffs.pop(slot, None)
# ------------------------------------------------------------------
# Decode thread
# ------------------------------------------------------------------
def _run(self) -> None:
try:
current_omni_platform.set_device(self._device)
self._run_jobs()
except Exception as error:
logger.exception("MOSS codec first-chunk worker failed")
self._worker_error = error
finally:
with self._lock:
self._closed = True
for handoff in self._handoffs.values():
if not handoff.done.is_set():
handoff.error = self._worker_error or RuntimeError("Codec first-chunk worker stopped")
handoff.done.set()
# Accepted, unprocessed routes must not be left waiting for PCM.
while True:
try:
job = self._jobs.get_nowait()
except queue.Empty:
break
if job is not None:
self._fail_delivery(job)
@staticmethod
def _fail_delivery(job: _Job) -> None:
try:
job.delivery.fail([job.request_id])
except Exception:
logger.exception("Codec first-chunk error delivery failed for %s", job.request_id)
def _run_jobs(self) -> None:
while True:
job = self._jobs.get()
if job is None:
return
jobs = [job]
while len(jobs) < self._max_batch:
try:
extra = self._jobs.get_nowait()
except queue.Empty:
break
if extra is None:
self._jobs.put(None)
break
jobs.append(extra)
try:
start = time.perf_counter()
self._decode(jobs)
self._decode_s += time.perf_counter() - start
self._batches += 1
before, self._emitted = self._emitted, self._emitted + len(jobs)
if self._emitted // 1000 != before // 1000:
logger.info(
"MOSS codec first-chunk fast path: %d chunks in %d batches, %.2f ms/batch",
self._emitted,
self._batches,
1e3 * self._decode_s / self._batches,
)
except Exception as error:
logger.exception("MOSS codec first-chunk fast decode failed for %s", [j.request_id for j in jobs])
# A partial graph replay may have advanced the slot. Surface
# the failure instead of re-decoding and advancing it twice.
for item in jobs:
item.handoff.error = error
item.handoff.done.set()
self._fail_delivery(item)
raise
@torch.no_grad()
def _decode(self, jobs: list[_Job]) -> None:
n = len(jobs)
for row, item in enumerate(jobs):
self._host_codes[:, row].copy_(item.codes)
self._host_slots[row] = item.slot
stream = self._stream
with torch.cuda.stream(stream):
for item in jobs:
self._session.order_after_reset(item.slot, stream)
codes = self._host_codes[:, :n].to(self._device, non_blocking=True).clamp_(0, self._codebook_size - 1)
slots = self._host_slots[:n].to(self._device, non_blocking=True)
result = self._wrapper.decode(codes, slots)
if result is None:
raise RuntimeError(f"no first-chunk codec graph for B={n}, T={self.frames}")
audio = result[0][:n, ..., : self._samples].float()
state_written = torch.cuda.Event()
state_written.record(stream)
self._inflight = state_written
self._host_audio[:n].view_as(audio).copy_(audio, non_blocking=True)
self._copied.record(stream)
for item in jobs:
item.handoff.event = state_written
self._copied.synchronize()
for row, item in enumerate(jobs):
wav = self._host_audio[row].clone()
if self._n_channels == 1:
wav = wav.reshape(-1)
try:
item.delivery([item.request_id], [wav], self._sr)
except Exception:
logger.exception("MOSS codec first-chunk output failed for %s", item.request_id)
with self._lock:
self._unsent_audio[item.request_key] = wav
finally:
item.handoff.done.set()
@@ -0,0 +1,447 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""MOSS Local request-slot state, independent of each step's batch order.
State writes, gathers and MTP replay run on the runner stream. Output gathers
own their storage; the runner retains its normal asynchronous output snapshot.
No request dictionary holds a view into a mutable slot or CUDA graph buffer.
"""
import numpy as np
import torch
from vllm.config import CUDAGraphMode
from vllm.forward_context import set_forward_context
from vllm.logger import init_logger
from vllm.utils.torch_utils import async_tensor_h2d
from vllm.v1.worker.gpu.input_batch import get_num_sampled_and_rejected
from vllm.v1.worker.gpu.sample.output import SamplerOutput
from vllm.v1.worker.gpu.sample.sampler import Sampler
from vllm_omni.model_executor.output_snapshot import PackedOutputSnapshot
from vllm_omni.worker_v2.model_states.omni_model_state import OmniModelState
logger = init_logger(__name__)
def _metadata_to_device(data: np.ndarray, device: torch.device) -> torch.Tensor:
# CPU state tests need no CUDA allocator. The serving CUDA path retains
# vLLM's asynchronous pinned upload and its ownership/lifetime handling.
if device.type == "cpu":
return torch.from_numpy(data)
return async_tensor_h2d(data, device=device)
class _CodeRowsSnapshot(PackedOutputSnapshot):
"""Owned per-request code rows backed by one gathered device tensor.
The generic snapshot flattens every per-request leaf, concatenates them
into a slab and slices each back, then slices each again after the D2H.
Here the gathered ``[decode_rows, n_vq]`` tensor is already owned, so the
copy is a single D2H and the per-request views come from one ``split``.
"""
def __init__(self, codes: torch.Tensor, decode_rows: list[int], num_reqs: int) -> None:
dict.__init__(self, {"codes": {"audio": self._rows(codes, decode_rows, num_reqs)}})
self._codes = codes
self._decode_rows = decode_rows
self._num_reqs = num_reqs
self.producer_event = None
@staticmethod
def _rows(codes: torch.Tensor, decode_rows: list[int], num_reqs: int) -> list[torch.Tensor]:
outputs = [codes.new_empty((0, codes.shape[-1]))] * num_reqs
for batch_row, row in zip(decode_rows, codes.split(1), strict=True):
outputs[batch_row] = row
return outputs
def copy_to_cpu(self, copy_tensor):
cpu = copy_tensor(self._codes)
return {"codes": {"audio": self._rows(cpu, self._decode_rows, self._num_reqs)}}
class MossLocalModelState(OmniModelState):
def __init__(self, vllm_config, model, encoder_cache, device):
super().__init__(vllm_config, model, encoder_cache, device)
self._init_slot_buffers(self.scheduler_config.max_num_seqs, model.hidden_size, device, self.dtype)
self._batch_prefill = bool(getattr(model.config, "mrv2_batch_prefill", False))
self._direct_tokens = bool(getattr(model.config, "mrv2_direct_tokens", False))
self._local_eager_mtp = bool(getattr(model.config, "mrv2_eager_mtp", False))
logger.info(
"MOSS Local MRV2 batch prefill=%s, determined tokens=%s, eager MTP=%s",
self._batch_prefill,
self._direct_tokens,
self._local_eager_mtp,
)
logger.info("MOSS Local MRV2 GPU slot state enabled: capacity=%d", self.scheduler_config.max_num_seqs)
def _init_slot_buffers(self, capacity, hidden_size, device, dtype):
self._hidden_pool = torch.zeros((capacity, hidden_size), dtype=dtype, device=device)
self._codes_pool = torch.full(
(capacity, self.model.n_vq), self.model.audio_pad_token_id, dtype=torch.long, device=device
)
# Execution eligibility mirrors audio_state.is_stopping. Binary MTP
# continuation is a per-step output, not a sticky request-state update.
self._active_pool = torch.zeros(capacity, dtype=torch.bool, device=device)
self._active_host = [False] * capacity
self._decode_rows = []
self._prefill_staging = []
self._batch_prefill = False
self._direct_tokens = False
# Eager MTP: a frame is sampled right after the forward that produced
# its hidden state, one step earlier than the canonical preprocess
# path. Its input-embedding contribution (audio embedding times emit
# mask) is added to the next step's text embedding; ``_keep_pool``
# chains emission so a stopped stream cannot emit again.
self._local_eager_mtp = False
self._eager_emb_pool = torch.zeros((capacity, hidden_size), dtype=dtype, device=device)
self._keep_pool = torch.zeros(capacity, dtype=torch.bool, device=device)
self._completing_rows = []
self._local_eager_rows = []
# A slot takes the eager path only after its final prefill chunk ran
# an eager MTP in this state; others keep the canonical path, so a
# reused slot never reads a previous request's frame state.
self._eager_host = [False] * capacity
self._mtp_dispatcher = None
def sample_determined_tokens(self, batch, sampler):
"""Skip full-vocabulary sampling only for unconstrained Local tokens.
The audio/binary MTP sampler has already made the random decision. The
outer text distribution has exactly one finite logit. Keep the original
path for processors that can mask it or request distribution metadata.
"""
if not self._direct_tokens or type(sampler) is not Sampler:
return None
rows = batch.idx_mapping_np
if (
sampler.compute_nans
or sampler.return_sampling_mask
or sampler.trace_replay_state is not None
or sampler.get_logprobs_dims(rows) is not None
or np.any(sampler.logit_bias_state.use_logit_bias[rows])
or np.any(sampler.penalties_state.use_penalty[rows])
or np.any(sampler.bad_words_state.num_bad_words.np[rows])
or (
sampler.thinking_budget_state.enabled
and np.any(sampler.thinking_budget_state.use_thinking_budget[rows])
)
):
return None
keep = self.model._batch_should_continue
if keep is None or keep.numel() != batch.num_reqs:
return None
# Owned storage: subsequent forwards can replace the continue mask
# while these token IDs are being copied or delivered asynchronously.
tokens = torch.where(keep, self.model.audio_assistant_slot_token_id, self.model.im_end_token_id)
num_sampled, num_rejected = get_num_sampled_and_rejected(
batch.seq_lens.new_ones(batch.num_reqs),
batch.seq_lens,
batch.cu_num_logits,
batch.idx_mapping,
sampler.req_states.prefill_len.gpu,
)
return SamplerOutput(tokens.reshape(-1, 1), None, None, num_sampled, num_rejected)
def _apply_reference_batch(self, embeds, references):
"""One owned pinned upload for reference codes and their token positions.
Host storage may be reused only after its H2D event completes. Device
storage is consumed and overwritten in runner-stream order. If all
cached slots are busy, allocate a transient snapshot instead of waiting.
"""
if not references:
return
count = sum(codes.shape[0] for _, codes in references)
capacity = max(count, self._static_inputs_embeds.shape[0])
staging = None
for item in self._prefill_staging:
if item[0].shape[0] >= count and (item[2] is None or item[2].query()):
staging = item
break
if staging is None:
host = torch.empty((capacity, self.model.n_vq + 1), dtype=torch.long, pin_memory=embeds.is_cuda)
device = torch.empty_like(host, device=embeds.device) if embeds.is_cuda else host
staging = [host, device, torch.cuda.Event() if embeds.is_cuda else None]
if len(self._prefill_staging) < 3:
self._prefill_staging.append(staging)
host, device, event = staging
offset = 0
for start, codes in references:
end = offset + codes.shape[0]
host[offset:end, 0].numpy()[:] = np.arange(start, start + codes.shape[0])
host[offset:end, 1:].copy_(codes)
offset = end
if embeds.is_cuda:
device[:count].copy_(host[:count], non_blocking=True)
event.record(torch.cuda.current_stream(embeds.device))
packed = device[:count]
positions = packed[:, 0]
audio = self.model._audio_embed(packed[:, 1:])
embeds.index_copy_(0, positions, embeds.index_select(0, positions) + audio)
def add_request(self, req_index, new_req_data):
super().add_request(req_index, new_req_data)
self._hidden_pool[req_index].zero_()
self._codes_pool[req_index].fill_(self.model.audio_pad_token_id)
self._active_pool[req_index].fill_(True)
self._active_host[req_index] = True
self._eager_host[req_index] = False
self._keep_pool[req_index].fill_(False)
self._eager_emb_pool[req_index].zero_()
def remove_request(self, req_index_or_id):
# Freeing is CPU bookkeeping. Reinitialization happens on admission,
# in stream order, so pending output snapshots remain independent.
super().remove_request(req_index_or_id)
def _select_rows(self, tensor, rows):
if rows == list(range(len(rows))):
return tensor[: len(rows)]
indices = _metadata_to_device(np.asarray(rows, dtype=np.int64), tensor.device)
return tensor.index_select(0, indices)
def run_preprocess(self, input_batch, model_inputs, req_states=None, mtp_batch_descriptor_dispatcher=None):
input_ids = model_inputs.get("input_ids")
if input_ids is None:
input_ids = input_batch.input_ids
embeds = model_inputs.get("inputs_embeds")
if embeds is None:
embeds = self.model.embed_input_ids(input_ids[: input_batch.num_tokens])
model_inputs["inputs_embeds"] = embeds
elif self._static_inputs_embeds is not None and embeds.data_ptr() == self._static_inputs_embeds.data_ptr():
embeds[: input_batch.num_tokens].copy_(self.model.embed_input_ids(input_ids[: input_batch.num_tokens]))
decode_rows = []
completing_rows = []
references = []
for row in range(input_batch.num_reqs):
slot = int(input_batch.idx_mapping_np[row])
buf = self.intermediate_buffer.buffers[slot]
if not buf or str(buf.get("req_id", "")).startswith("_warmup_"):
continue
start = int(input_batch.query_start_loc_np[row])
count = int(input_batch.num_scheduled_tokens[row])
prompt_len = (
None if req_states is None else self._get_req_state_value(getattr(req_states, "prompt_len", None), slot)
)
computed = (
None
if req_states is None
else self._get_req_state_value(getattr(req_states, "num_computed_tokens", None), slot)
)
if computed is None:
computed = self._get_input_batch_num_computed(input_batch, slot, row)
prefill = computed < prompt_len if computed is not None and prompt_len is not None else count > 1
if prefill and computed is not None and prompt_len is not None and computed + count >= prompt_len:
completing_rows.append(row)
if count == 1 and not prefill and isinstance(buf.get("audio_state"), dict):
active = not bool(buf["audio_state"].get("is_stopping"))
if active != self._active_host[slot]:
self._active_pool[slot].fill_(active)
self._active_host[slot] = active
decode_rows.append(row)
continue
if self._batch_prefill:
ref = (buf.get("codes", {}) or {}).get("ref")
ref_offset = int(buf.get("ref_offset", 0))
# Device-side references keep the canonical path; do not add
# an implicit D2H just to place them in a host staging buffer.
if not isinstance(ref, torch.Tensor) or ref.device.type == "cpu":
if isinstance(ref, torch.Tensor) and ref.numel():
if ref.dim() == 1 and ref.numel() % self.model.n_vq == 0:
ref = ref.view(-1, self.model.n_vq)
if ref.dim() == 2:
chunk = ref[ref_offset : ref_offset + count]
if chunk.numel() and chunk.shape[0] == count:
references.append((start, chunk))
# Text embeddings have already been computed for all input
# tokens. Match the canonical prefill state transition.
self.intermediate_buffer.update(
slot,
{
"audio_state": {"is_stopping": False},
"ref_offset": ref_offset + count,
},
self.model.gpu_resident_buffer_keys,
)
if not self._active_host[slot]:
self._active_pool[slot].fill_(True)
self._active_host[slot] = True
continue
# Reference conditioning and chunked-prefill coordinates retain
# the model's canonical scalar implementation.
info = dict(buf)
info["_omni_is_prefill"] = prefill
if prompt_len is not None:
info["_omni_prompt_len"] = prompt_len
if computed is not None:
info["_omni_num_computed_tokens"] = computed
ids = input_ids[start : start + count]
previous = embeds[start : start + count]
new_ids, new_embeds, updates = self.model.preprocess(ids, previous, **info)
if "mtp_inputs" in updates:
raise RuntimeError("MOSS slot state requires an admitted prefill before decode")
if self._preprocess_result_needs_writeback(ids, new_ids):
ids.copy_(new_ids)
if self._preprocess_result_needs_writeback(previous, new_embeds):
previous.copy_(new_embeds)
self.intermediate_buffer.update(slot, updates, self.model.gpu_resident_buffer_keys)
active = not bool(updates.get("audio_state", {}).get("is_stopping", False))
if active != self._active_host[slot]:
self._active_pool[slot].fill_(active)
self._active_host[slot] = active
self._apply_reference_batch(embeds, references)
self._decode_rows = decode_rows
if self._local_eager_mtp:
self._completing_rows = completing_rows
self._mtp_dispatcher = mtp_batch_descriptor_dispatcher
eager_rows = [row for row in decode_rows if self._eager_host[int(input_batch.idx_mapping_np[row])]]
canonical_rows = [row for row in decode_rows if not self._eager_host[int(input_batch.idx_mapping_np[row])]]
self._local_eager_rows = eager_rows
if eager_rows:
# Their frame was sampled after the previous forward.
slots = self._select_rows(input_batch.idx_mapping, eager_rows)
offsets = self._mtp_offsets[: len(eager_rows)]
offsets.copy_(self._select_rows(input_batch.query_start_loc, eager_rows))
embeds.index_copy_(
0, offsets, embeds.index_select(0, offsets) + self._eager_emb_pool.index_select(0, slots)
)
if canonical_rows:
self._run_slot_mtp(input_batch, input_ids, embeds, canonical_rows, mtp_batch_descriptor_dispatcher)
elif decode_rows:
self._run_slot_mtp(input_batch, input_ids, embeds, decode_rows, mtp_batch_descriptor_dispatcher)
def _run_slot_mtp(self, batch, input_ids, embeds, rows, dispatcher):
bsz = len(rows)
slots = self._select_rows(batch.idx_mapping, rows)
# Upstream query_start_loc is int32; index_copy_ requires int64.
offsets = self._mtp_offsets[:bsz]
offsets.copy_(self._select_rows(batch.query_start_loc, rows))
ids, emb = self._mtp_input_ids[:bsz], self._mtp_input_embeds[:bsz]
hidden, step = self._mtp_hidden[:bsz], self._mtp_text_step[:bsz]
ids.copy_(input_ids.index_select(0, offsets)) # MRV2 token inputs are int32.
torch.index_select(embeds, 0, offsets, out=emb)
torch.index_select(self._hidden_pool, 0, slots, out=hidden)
step[:, 0].copy_(self._active_pool.index_select(0, slots))
buffers = [self.intermediate_buffer.buffers[int(batch.idx_mapping_np[row])] for row in rows]
req_ids = [str(buf["req_id"]) for buf in buffers]
generators = [
self._get_mtp_generator(req_id, buf.get("sampling_params"), ids.device)
for req_id, buf in zip(req_ids, buffers, strict=True)
]
can_graph = self._is_mtp_graph_runner() and not any(g is not None for g in generators)
desc = dispatcher(bsz) if can_graph and dispatcher is not None else None
mode = getattr(desc, "cg_mode", CUDAGraphMode.FULL) if can_graph else CUDAGraphMode.NONE
num_tokens = int(desc.num_tokens) if desc is not None else bsz
with set_forward_context(
None, self.vllm_config, num_tokens=num_tokens, cudagraph_runtime_mode=mode, batch_descriptor=desc
):
new_emb, codes = self._call_mtp_with_sampling(
self._mtp_input_ids[:num_tokens],
self._mtp_input_embeds[:num_tokens],
self._mtp_hidden[:num_tokens],
self._mtp_text_step[:num_tokens],
buffers=buffers,
req_ids=req_ids,
generators=generators,
)
embeds.index_copy_(0, offsets, new_emb[:bsz])
self._codes_pool.index_copy_(0, slots, codes[:bsz])
def _run_local_eager_mtp(self, batch, rows, completing):
"""Sample the next frame from this forward's hidden state.
Same MTP inputs as the canonical next-step preprocess (hidden state
and active flag); the input embedding is zero, so the returned
embedding is exactly the audio contribution the next step adds to its
text embedding. A decode row emits only if its previous frame did.
"""
bsz = len(rows)
slots = self._select_rows(batch.idx_mapping, rows)
ids, emb = self._mtp_input_ids[:bsz], self._mtp_input_embeds[:bsz]
hidden, step = self._mtp_hidden[:bsz], self._mtp_text_step[:bsz]
ids.zero_() # the MTP does not read token ids
emb.zero_()
torch.index_select(self._hidden_pool, 0, slots, out=hidden)
gate = self._keep_pool.index_select(0, slots)
if completing:
first = _metadata_to_device(
np.asarray([row in completing for row in rows], dtype=np.bool_),
gate.device,
)
gate = gate | first
step[:, 0].copy_(self._active_pool.index_select(0, slots) & gate)
buffers = [self.intermediate_buffer.buffers[int(batch.idx_mapping_np[row])] for row in rows]
req_ids = [str(buf["req_id"]) for buf in buffers]
generators = [
self._get_mtp_generator(req_id, buf.get("sampling_params"), ids.device)
for req_id, buf in zip(req_ids, buffers, strict=True)
]
dispatcher = self._mtp_dispatcher
can_graph = self._is_mtp_graph_runner() and not any(g is not None for g in generators)
desc = dispatcher(bsz) if can_graph and dispatcher is not None else None
mode = getattr(desc, "cg_mode", CUDAGraphMode.FULL) if can_graph else CUDAGraphMode.NONE
num_tokens = int(desc.num_tokens) if desc is not None else bsz
with set_forward_context(
None, self.vllm_config, num_tokens=num_tokens, cudagraph_runtime_mode=mode, batch_descriptor=desc
):
contribution, codes = self._call_mtp_with_sampling(
self._mtp_input_ids[:num_tokens],
self._mtp_input_embeds[:num_tokens],
self._mtp_hidden[:num_tokens],
self._mtp_text_step[:num_tokens],
buffers=buffers,
req_ids=req_ids,
generators=generators,
)
codes = codes[:bsz]
self._eager_emb_pool.index_copy_(0, slots, contribution[:bsz])
self._codes_pool.index_copy_(0, slots, codes)
self._keep_pool.index_copy_(0, slots, codes.ne(self.model.audio_pad_token_id).any(dim=-1))
def run_postprocess(self, hidden_states, input_batch):
# query_start_loc is already device-resident and authoritative for
# mixed/chunked-prefill batches. index_copy snapshots graph outputs.
last = input_batch.query_start_loc[1 : input_batch.num_reqs + 1] - 1
self._hidden_pool.index_copy_(
0, input_batch.idx_mapping[: input_batch.num_reqs], hidden_states.index_select(0, last)
)
if self._local_eager_mtp:
completing = self._completing_rows
rows = sorted(self._local_eager_rows + completing)
# Canonical decode rows emit their preprocess frame; eager rows
# and completed prefills emit the frame sampled below.
self._decode_rows = sorted(set(self._decode_rows) | set(completing))
if rows:
self._run_local_eager_mtp(input_batch, rows, set(completing))
for row in completing:
self._eager_host[int(input_batch.idx_mapping_np[row])] = True
def postprocess_model_output(self, model_output, input_batch, req_states):
# Local backbone returns a tensor; keep the standard runner's output
# snapshot/materializer contract and omit hidden payload transport.
if not isinstance(model_output, torch.Tensor):
raise TypeError("MOSS Local slot state requires a tensor backbone output")
model = self.model
model._batch_state = None
model._batch_state_spans = None
model._batch_should_continue = torch.ones(input_batch.num_reqs, device=model_output.device, dtype=torch.bool)
if not self._decode_rows:
return model_output, {}
# This gather is an owned snapshot. Later slot writes cannot change
# data already handed to the runner, even before its D2H completes.
slots = self._select_rows(input_batch.idx_mapping, self._decode_rows)
codes = self._codes_pool.index_select(0, slots)
keep = codes.ne(model.audio_pad_token_id).any(dim=-1)
if self._decode_rows == list(range(input_batch.num_reqs)):
model._batch_should_continue = keep
else:
rows = _metadata_to_device(
np.asarray(self._decode_rows, dtype=np.int64),
codes.device,
)
model._batch_should_continue.index_copy_(0, rows, keep)
return model_output, _CodeRowsSnapshot(codes, list(self._decode_rows), input_batch.num_reqs)
@@ -0,0 +1,100 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Single-query attention for the frame-local MOSS depth transformer.
The caller has already written this token's K/V and provides only its valid
prefix. There is no persistent state here. BF16 probability rounding matches
the FlashAttention P/V multiply, but reduction order is not bit-identical.
"""
import torch
from vllm.triton_utils import tl, triton
@triton.jit
def _short_attention(
q_ptr,
k_ptr,
v_ptr,
out_ptr,
qs0: tl.constexpr,
qs1: tl.constexpr,
ks0: tl.constexpr,
ks1: tl.constexpr,
ks2: tl.constexpr,
vs0: tl.constexpr,
vs1: tl.constexpr,
vs2: tl.constexpr,
heads: tl.constexpr,
dim: tl.constexpr,
tokens: tl.constexpr,
block_d: tl.constexpr,
block_t: tl.constexpr,
):
row = tl.program_id(0)
batch, head = row // heads, row % heads
d, t = tl.arange(0, block_d), tl.arange(0, block_t)
query = tl.load(q_ptr + batch * qs0 + head * qs1 + d, d < dim, 0).to(tl.float32)
keys = tl.load(
k_ptr + batch * ks0 + head * ks1 + t[:, None] * ks2 + d[None, :],
(t[:, None] < tokens) & (d[None, :] < dim),
0,
).to(tl.float32)
logits = tl.sum(keys * query[None, :], axis=1) * (dim**-0.5 * 1.4426950408889634)
logits = tl.where(t < tokens, logits, float("-inf"))
probs = tl.exp2(logits - tl.max(logits, axis=0))
denominator = tl.sum(probs, axis=0)
values = tl.load(
v_ptr + batch * vs0 + head * vs1 + t[:, None] * vs2 + d[None, :],
(t[:, None] < tokens) & (d[None, :] < dim),
0,
)
probs = probs.to(values.dtype).to(tl.float32)
output = tl.sum(values.to(tl.float32) * probs[:, None], axis=0) / denominator
tl.store(out_ptr + row * dim + d, output, d < dim)
@torch.library.custom_op("vllm_omni::moss_local_short_attention", mutates_args=())
def local_short_attention(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor) -> torch.Tensor:
batch, heads, seq_len, dim = query.shape
if not (
query.is_cuda
and key.device == query.device
and value.device == query.device
and query.dtype == key.dtype == value.dtype == torch.bfloat16
and seq_len == 1
and 0 < dim <= 128
and key.shape == value.shape
and key.shape[:2] == query.shape[:2]
and key.shape[-1] == dim
and 0 < key.shape[2] <= 16
and query.stride(-1) == key.stride(-1) == value.stride(-1) == 1
):
raise ValueError("MOSS short attention requires CUDA BF16 single-query inputs and 1..16 valid keys")
output = query.new_empty((batch, heads, 1, dim))
_short_attention[(batch * heads,)](
query,
key,
value,
output,
query.stride(0),
query.stride(1),
key.stride(0),
key.stride(1),
key.stride(2),
value.stride(0),
value.stride(1),
value.stride(2),
heads,
dim,
key.shape[2],
triton.next_power_of_2(dim),
triton.next_power_of_2(key.shape[2]),
num_warps=1,
)
return output
@local_short_attention.register_fake
def _fake_local_short_attention(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor) -> torch.Tensor:
return query.new_empty(query.shape)
@@ -8,7 +8,10 @@
from __future__ import annotations
import threading
from collections.abc import Iterable
from contextlib import nullcontext
from functools import partial
from typing import Any
import torch
@@ -32,11 +35,16 @@ from vllm_omni.model_executor.models.moss_tts.configuration_moss_audio_tokenizer
from vllm_omni.model_executor.models.moss_tts.cuda_graph_streaming_decoder_wrapper import (
CUDAGraphStreamingDecoderWrapper,
)
from vllm_omni.model_executor.models.moss_tts.first_chunk_fast_path import (
MossFirstChunkFastPath,
)
from vllm_omni.model_executor.models.moss_tts.moss_codec_cudagraph import (
MossTTSCUDAGraphCodecWrapper,
)
from vllm_omni.model_executor.models.output_templates import OmniOutput
from vllm_omni.model_executor.output_snapshot import PackedOutputSnapshot, pack_output_snapshot
from vllm_omni.model_executor.stage_input_processors.chunk_size_utils import parse_chunk_ramp
from vllm_omni.worker_v2.first_audio_sender import FirstAudioSink
logger = init_logger(__name__)
@@ -53,18 +61,46 @@ class _MossCodecStreamSession:
vllm_config: VllmConfig,
graph_batch_sizes: list[int] | None = None,
graph_frame_sizes: list[int] | None = None,
gpu_output: bool = False,
chunk_frames: int = 0,
ring_headroom: bool = False,
fast_batch_sizes: list[int] | None = None,
fast_frames: int = 0,
) -> None:
self._codec = codec
self._state_capacity = int(state_capacity)
self._chunk_frames = int(chunk_frames)
self._ring_headroom = bool(ring_headroom)
self._n_vq = int(n_vq)
self._device = next(codec.parameters()).device
self._gpu_output = gpu_output and self._device.type == "cuda"
self._free_stream_slots = list(reversed(range(self._state_capacity)))
self._leased_slots: set[int] = set()
# The first-chunk fast path leases slots from its decode thread.
self._slot_lock = threading.Lock()
self._slot_reset_events: dict[int, torch.cuda.Event] = {}
self._closed = False
self._metadata_index = 0
self._metadata_ring = []
if self._gpu_output:
# The metadata DMA is tiny, but a short ring forces the producer
# CPU thread to synchronize with the codec stream while it waits
# for a host slab to become reusable. Keep enough entries for a
# scheduler burst so staging remains enqueue-only in practice.
for _ in range(64):
host = torch.empty(2 * self._state_capacity, dtype=torch.long, pin_memory=True)
gpu = torch.empty_like(host, device=self._device)
self._metadata_ring.append((host, host.numpy(), gpu, torch.cuda.Event(), False))
self._cudagraph_wrapper: CUDAGraphStreamingDecoderWrapper | None = None
self._fast_wrapper: CUDAGraphStreamingDecoderWrapper | None = None
batch_sizes = sorted({int(size) for size in (graph_batch_sizes or []) if 0 < int(size) <= self._state_capacity})
frame_sizes = sorted({int(size) for size in (graph_frame_sizes or []) if int(size) > 0})
scratch_capacity = max(batch_sizes, default=0) if self._device.type in ("cuda", "npu") else 0
fast_batch_sizes = sorted({int(size) for size in (fast_batch_sizes or []) if 0 < int(size)})
if not (fast_frames > 0 and fast_batch_sizes and batch_sizes and frame_sizes and self._device.type == "cuda"):
fast_batch_sizes = []
fast_scratch_base = self._state_capacity + scratch_capacity
scratch_capacity += max(fast_batch_sizes, default=0)
self._total_state_capacity = self._state_capacity + scratch_capacity
self._state_slot_ids = torch.arange(
self._total_state_capacity,
@@ -76,7 +112,14 @@ class _MossCodecStreamSession:
if not callable(initialize_state_pool):
raise RuntimeError("The streaming codec does not implement a decoder state pool.")
with torch.no_grad():
initialize_state_pool(self._state_capacity, scratch_capacity)
if self._ring_headroom:
if self._chunk_frames <= 0:
raise ValueError("codec_ring_headroom requires a positive codec chunk frame count.")
# Size every attention ring for context + one full chunk so a
# chunk write never evicts keys inside its own causal window.
initialize_state_pool(self._state_capacity, scratch_capacity, chunk_frames=self._chunk_frames)
else:
initialize_state_pool(self._state_capacity, scratch_capacity)
if batch_sizes and frame_sizes and self._device.type in ("cuda", "npu"):
if self._device.type == "npu":
from vllm_omni.platforms.npu.models.moss_tts_streaming_decode_wrapper import (
@@ -101,16 +144,34 @@ class _MossCodecStreamSession:
vllm_config=vllm_config,
)
self._cudagraph_wrapper.warmup(self._device)
if fast_batch_sizes and self._cudagraph_wrapper.is_ready:
main_compiled = self._cudagraph_wrapper._compiled_decode
self._fast_wrapper = CUDAGraphStreamingDecoderWrapper(
codec,
state_capacity=self._state_capacity,
batch_sizes=fast_batch_sizes,
frame_sizes=[int(fast_frames)],
num_quantizers=self._n_vq,
vllm_config=vllm_config,
scratch_base=fast_scratch_base,
private_pool=True,
compiled_decode=main_compiled if main_compiled is not None else codec.decode_streaming_tensors,
)
self._fast_wrapper.warmup(self._device)
if not self._fast_wrapper.is_ready:
self._fast_wrapper = None
self.reset_slots(list(range(self._state_capacity + scratch_capacity)))
if not self._cudagraph_wrapper.is_ready:
self._cudagraph_wrapper = None
def acquire(self) -> int | None:
if not self._free_stream_slots:
return None
slot = self._free_stream_slots.pop()
self._leased_slots.add(slot)
return slot
def acquire(self, *, oldest: bool = False) -> int | None:
"""Lease a slot; ``oldest`` takes the one whose reset was queued first."""
with self._slot_lock:
if not self._free_stream_slots:
return None
slot = self._free_stream_slots.pop(0 if oldest else -1)
self._leased_slots.add(slot)
return slot
def release(self, slot: int, *, state_already_reset: bool = False) -> None:
if self._closed:
@@ -119,8 +180,23 @@ class _MossCodecStreamSession:
return
if not state_already_reset:
self.reset_slots([slot])
self._leased_slots.remove(slot)
self._free_stream_slots.append(slot)
reset_event = None
if self._fast_wrapper is not None:
# The fast path decodes on another stream; it must not read this
# slot before the reset queued on the current stream has run.
reset_event = torch.cuda.Event()
reset_event.record(torch.cuda.current_stream(self._device))
with self._slot_lock:
if reset_event is not None:
self._slot_reset_events[slot] = reset_event
self._leased_slots.remove(slot)
self._free_stream_slots.append(slot)
def order_after_reset(self, slot: int, stream: torch.cuda.Stream) -> None:
with self._slot_lock:
event = self._slot_reset_events.pop(slot, None)
if event is not None and not event.query():
stream.wait_event(event)
def _reset_slot_ids(self, slot_ids: torch.Tensor) -> None:
reset_streaming_slots = getattr(self._codec, "reset_decoder_state_slots", None)
@@ -139,9 +215,29 @@ class _MossCodecStreamSession:
raise ValueError(f"Decoder state slots must be contiguous, got {slots}")
self._reset_slot_ids(self._state_slot_ids[start : start + len(slots)])
def _stage_slot_ids(self, slots: list[int], terminal_slots: set[int]) -> tuple[torch.Tensor, torch.Tensor]:
# Host slabs cannot be overwritten until their DMA completes. Device
# slabs are reused in producer-stream order, after all previous reads.
index = self._metadata_index
host, view, gpu, event, recorded = self._metadata_ring[index]
if recorded and not event.query():
event.synchronize()
terminal_ids = [slot for slot in slots if slot in terminal_slots]
n, end = len(slots), len(slots) + len(terminal_ids)
view[:n] = slots
view[n:end] = terminal_ids
gpu[:end].copy_(host[:end], non_blocking=True)
event.record(torch.cuda.current_stream(self._device))
self._metadata_ring[index] = (host, view, gpu, event, True)
self._metadata_index = (index + 1) % len(self._metadata_ring)
return gpu[:n], gpu[n:end]
def close(self) -> None:
if self._closed:
return
for _, _, _, event, recorded in self._metadata_ring:
if recorded:
event.synchronize()
close_state_pool = getattr(self._codec, "close_decoder_state_pool", None)
with torch.no_grad():
if callable(close_state_pool):
@@ -154,6 +250,7 @@ class _MossCodecStreamSession:
slot_codes: dict[int, torch.Tensor],
*,
terminal_slots: set[int] | None = None,
output_buffers: dict[int, torch.Tensor] | None = None,
) -> dict[int, torch.Tensor]:
if not slot_codes:
return {}
@@ -178,17 +275,22 @@ class _MossCodecStreamSession:
],
dim=1,
)
state_slot_ids = torch.tensor(slots, device=self._device, dtype=torch.long)
staged_terminal_ids = None
if self._metadata_ring:
state_slot_ids, staged_terminal_ids = self._stage_slot_ids(slots, terminal_slots)
else:
state_slot_ids = torch.tensor(slots, device=self._device, dtype=torch.long)
graph_output: tuple[torch.Tensor, torch.Tensor, int] | None = None
if self._cudagraph_wrapper is not None:
# A terminal tail can safely pad T to a larger graph: decoder
# attention is causal, audio_lengths trims the padding, and every
# affected state slot is reset immediately after this step.
# An all-terminal group may borrow the next larger frame graph.
# The decoder is causal, so cropping the padded tail on the time
# axis recovers the exact-T waveform up to attention-ring
# eviction (see codec_ring_headroom); the state is reset below.
graph_output = self._cudagraph_wrapper.decode(
codes_step,
state_slot_ids,
allow_frame_padding=len(terminal_slots) == len(slots),
allow_frame_padding=bool(terminal_slots) and len(terminal_slots) == len(slots),
)
used_cudagraph = graph_output is not None
@@ -214,11 +316,69 @@ class _MossCodecStreamSession:
audio_tensor = result.audio
if terminal_slots:
terminal_rows = [row for row, slot in enumerate(slots) if slot in terminal_slots]
terminal_slot_ids = state_slot_ids if len(terminal_rows) == len(slots) else state_slot_ids[terminal_rows]
if staged_terminal_ids is not None:
terminal_slot_ids = staged_terminal_ids
else:
terminal_rows = [row for row, slot in enumerate(slots) if slot in terminal_slots]
terminal_slot_ids = (
state_slot_ids if len(terminal_rows) == len(slots) else state_slot_ids[terminal_rows]
)
self._reset_slot_ids(terminal_slot_ids)
audio = audio_tensor.detach().to("cpu", torch.float32)
if self._gpu_output and output_buffers is not None:
# Write directly into the caller-owned per-forward slab. This
# preserves graph output before the next replay without creating
# a temporary waveform tensor that would be packed again later.
out: dict[int, torch.Tensor] = {}
row = 0
while row < len(slots):
destination = output_buffers.get(slots[row])
if destination is None:
row += 1
continue
end = row + 1
if end < len(slots) and destination.is_contiguous():
storage = destination.untyped_storage().data_ptr()
offset = destination.storage_offset()
row_elements = destination.numel()
while end < len(slots):
following = output_buffers.get(slots[end])
if not (
following is not None
and following.shape == destination.shape
and following.dtype == destination.dtype
and following.device == destination.device
and following.is_contiguous()
and following.untyped_storage().data_ptr() == storage
and following.storage_offset() == offset + (end - row) * row_elements
):
break
end += 1
if end > row + 1:
# A mixed-frame forward can leave gaps between equal-sized
# rows in its slab. Copy each adjacent run as one batch,
# without overwriting the intervening requests' output.
destination = destination.as_strided(
(end - row, *destination.shape), (destination.numel(), *destination.stride())
)
source = audio_tensor[row:end]
else:
source = audio_tensor[row]
# Crop time before reshaping: flattening first mixes stereo
# channels whenever a row is shorter than the decode batch.
destination.copy_(source[..., : destination.shape[-1]].view_as(destination))
out.update((slot, output_buffers[slot]) for slot in slots[row:end])
row = end
return out
if self._gpu_output:
# A graph's static output is overwritten on every replay, including
# multiple replays within one forward. Snapshot on the producer
# stream before returning; record_stream alone cannot prevent this
# overwrite. MRV2 owns the subsequent pinned, asynchronous D2H.
audio = audio_tensor.detach().to(dtype=torch.float32, copy=True)
else:
audio = audio_tensor.detach().to("cpu", torch.float32)
out: dict[int, torch.Tensor] = {}
for row, slot in enumerate(slots):
audio_length = int(slot_codes[slot].shape[1]) * self._samples_per_frame
@@ -270,6 +430,7 @@ class MossTTSCodecDecoder(nn.Module):
requires_exact_input_shape: bool = True
_OUTPUT_SAMPLE_RATE: int = 24_000
_first_chunk_fast_path: MossFirstChunkFastPath | None = None
def __init__(self, *, vllm_config: VllmConfig, prefix: str = "") -> None:
super().__init__()
@@ -292,20 +453,38 @@ class MossTTSCodecDecoder(nn.Module):
self._n_channels: int = 1
self._sr_tensor = torch.tensor(self._OUTPUT_SAMPLE_RATE, dtype=torch.int32)
self._stream_session: _MossCodecStreamSession | None = None
self._codec_stream: torch.cuda.Stream | None = None
scheduler_cfg = getattr(self.vllm_config, "scheduler_config", None)
self._stream_state_capacity = max(1, int(getattr(scheduler_cfg, "max_num_seqs", 1) or 1))
self._initial_stream_chunk_frames: int = self._connector_int("initial_codec_chunk_frames", default=0)
self._stream_chunk_frames: int = self._connector_int("codec_chunk_frames", default=0)
self._stream_max_step_frames: int = self._stream_chunk_frames or 100
# Opt-in: size codec attention rings as context + one chunk so chunked
# streaming reproduces whole-sequence decoding (costs ring memory).
self._stream_ring_headroom: bool = bool(self._connector_int("codec_ring_headroom", default=0))
self._stream_req_slots: dict[str, int] = {}
# Opt-in: decode each stream's first chunk from the receive thread.
self._first_chunk_fast = bool(self._connector_int("codec_first_chunk_fast_path", default=0))
self._first_chunk_fast_path: MossFirstChunkFastPath | None = None
self._async_chunk = bool(getattr(self.vllm_config.model_config, "async_chunk", False))
self._gpu_stream_output = (
self._async_chunk
and bool(getattr(vllm_config.model_config, "use_v2_model_runner", False))
and getattr(cfg, "model_type", None) == "moss_tts_local"
# Diagnostic switch: 0 copies each decode group's batch to the host synchronously.
and bool(self._connector_int("codec_gpu_stream_output", default=1))
)
self._streaming_graph_batch_sizes = self._streaming_graph_batch_sizes_from_compilation_config()
# codec_chunk_ramp ladder sizes join the streaming graph frame-size
# set so every ladder (B, T) combination is captured during warmup.
ramp = parse_chunk_ramp(self._connector_extra(), steady=self._stream_chunk_frames)
if ramp:
# The sender gives the ramp precedence over initial chunk size.
# The dedicated first-chunk graph must use that same first shape.
self._initial_stream_chunk_frames = ramp[0]
self._stream_max_step_frames = max(
self._stream_max_step_frames, self._initial_stream_chunk_frames, *(ramp or [])
)
self._streaming_graph_frame_sizes = _resolve_streaming_graph_frame_sizes(
self._initial_stream_chunk_frames,
self._stream_chunk_frames,
self._connector_extra_cfg(),
self._initial_stream_chunk_frames, self._stream_chunk_frames, self._connector_extra()
)
# ------------------------------------------------------------------
@@ -337,6 +516,41 @@ class MossTTSCodecDecoder(nn.Module):
inputs_embeds: torch.Tensor | None = None,
runtime_additional_information: list[dict[str, Any]] | None = None,
**kwargs: Any,
) -> OmniOutput:
if self._gpu_stream_output and input_ids is not None and input_ids.is_cuda:
if self._codec_stream is None:
self._codec_stream = torch.cuda.Stream(device=input_ids.device)
main_stream = torch.cuda.current_stream(input_ids.device)
# Runner input storage is reused by the next batch on main_stream.
# Own it before allowing that preparation to overlap codec decode.
owned_input_ids = input_ids.clone()
with torch.cuda.stream(self._codec_stream):
self._codec_stream.wait_stream(main_stream)
owned_input_ids.record_stream(self._codec_stream)
output = self._forward(
owned_input_ids,
positions,
intermediate_tensors,
inputs_embeds,
runtime_additional_information,
**kwargs,
)
if isinstance(output.multimodal_outputs, PackedOutputSnapshot):
output.multimodal_outputs.record_producer_event(self._codec_stream)
return output
return self._forward(
input_ids, positions, intermediate_tensors, inputs_embeds, runtime_additional_information, **kwargs
)
@torch.no_grad()
def _forward(
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:
"""Decode audio VQ codes to waveform.
@@ -493,13 +707,37 @@ class MossTTSCodecDecoder(nn.Module):
audios[i] = wav.reshape(-1) if wav.ndim == 1 or int(wav.shape[0]) == 1 else wav
if streaming_work:
for i, wav in self._decode_streaming_batch(streaming_work).items():
stream_output_slab = None
stream_output_buffers: dict[int, torch.Tensor] = {}
if self._gpu_stream_output and device.type == "cuda":
total_elements = 0
for output_index, _, codes_nq_t, _ in streaming_work:
samples = int(codes_nq_t.shape[1]) * self._codec.downsample_rate
total_elements += samples * (self._n_channels if self._n_channels > 1 else 1)
if total_elements:
stream_output_slab = torch.empty(total_elements, dtype=torch.float32, device=device)
offset = 0
for output_index, _, codes_nq_t, _ in streaming_work:
samples = int(codes_nq_t.shape[1]) * self._codec.downsample_rate
elements = samples * (self._n_channels if self._n_channels > 1 else 1)
shape = (self._n_channels, samples) if self._n_channels > 1 else (samples,)
stream_output_buffers[output_index] = stream_output_slab.narrow(0, offset, elements).view(shape)
offset += elements
for i, wav in self._decode_streaming_batch(
streaming_work,
output_buffers=stream_output_buffers or None,
).items():
audios[i] = wav.reshape(-1) if wav.ndim == 1 or int(wav.shape[0]) == 1 else wav
return OmniOutput(
text_hidden_states=None,
multimodal_outputs={"model_outputs": audios, "sr": srs},
)
payload = {"model_outputs": audios, "sr": srs}
if self._gpu_stream_output and device.type == "cuda":
# One transfer per dtype/device preserves ragged request lengths
# without issuing one D2H per waveform. A fresh slot owns each
# output until the runner completes its copy; no reusable ring.
packed = pack_output_snapshot(payload, {}, max_buckets=0, reuse_existing_storage=True)
if packed is not None:
payload = packed
return OmniOutput(text_hidden_states=None, multimodal_outputs=payload)
@staticmethod
def _normalize_seq_token_counts(value: Any) -> list[int] | None:
@@ -546,25 +784,104 @@ class MossTTSCodecDecoder(nn.Module):
vllm_config=self.vllm_config,
graph_batch_sizes=self._streaming_graph_batch_sizes,
graph_frame_sizes=self._streaming_graph_frame_sizes,
gpu_output=self._gpu_stream_output,
chunk_frames=self._stream_max_step_frames,
ring_headroom=self._stream_ring_headroom,
fast_batch_sizes=[1, 2, 4, 8] if self._first_chunk_fast else None,
fast_frames=self._initial_stream_chunk_frames if self._first_chunk_fast else 0,
)
if self._stream_session._fast_wrapper is not None:
self._first_chunk_fast_path = MossFirstChunkFastPath(
self._stream_session,
self._stream_session._fast_wrapper,
n_vq=self._n_vq,
frames=self._initial_stream_chunk_frames,
codebook_size=int(self._codec.config.codebook_size),
samples_per_frame=int(self._codec.downsample_rate),
n_channels=self._n_channels,
sample_rate=self._sr_tensor,
device=next(self._codec.parameters()).device,
gate_main=bool(self._connector_int("codec_first_chunk_gate", default=0)),
)
logger.info(
"MOSS-TTS codec first-chunk fast path ready: B=%s T=%d",
self._stream_session._fast_wrapper.capture_sizes,
self._initial_stream_chunk_frames,
)
elif self._first_chunk_fast:
logger.warning("MOSS-TTS codec first-chunk fast path requested but unavailable; using the main path.")
if self._stream_ring_headroom:
logger.info(
"MOSS-TTS codec attention rings sized with headroom for %d-frame chunks",
self._stream_max_step_frames,
)
return self._stream_session
def bind_first_chunk_fast_path(self, sink: FirstAudioSink) -> Any:
"""Start the fast path with an output sink; returns the receive hook."""
fast = self._first_chunk_fast_path
if fast is None:
return None
fast.bind(sink)
return self._submit_first_chunk
def _submit_first_chunk(self, request_id: str, handle: Any, payload: Any) -> bool:
fast = self._first_chunk_fast_path
if fast is None or not isinstance(payload, dict):
return False
codes = payload.get("codes")
codes = codes.get("audio") if isinstance(codes, dict) else None
if codes is None:
return False
meta = payload.get("meta") if isinstance(payload.get("meta"), dict) else {}
key = meta.get("req_id")
if isinstance(key, (list, tuple)):
key = key[0] if key else None
if key is None:
key = getattr(handle, "external_req_id", None) or request_id
return fast.submit(request_id, str(key), codes, self._stream_req_slots)
def _decode_streaming_batch(
self,
items: list[tuple[int, str, torch.Tensor, bool]],
*,
output_buffers: dict[int, torch.Tensor] | None = None,
) -> dict[int, torch.Tensor]:
session = self._ensure_stream_session()
if session is None:
return {}
outputs: dict[int, torch.Tensor] = {}
unsent_first_audio: dict[int, torch.Tensor] = {}
grouped: dict[int, list[tuple[int, str, int, torch.Tensor, bool]]] = {}
max_step_frames = max(1, int(self._stream_max_step_frames))
graph = session._cudagraph_wrapper
# Terminal tails join the regular padded frame bucket when every
# execution batch width has a graph, so a step stays one replay
# instead of one eager or plain-graph decode per distinct tail length.
coalesce_tails = graph is not None and max(graph.batch_sizes, default=0) >= self._stream_state_capacity
fast = self._first_chunk_fast_path
for output_index, request_id, codes_nq_t, finished in items:
slot = self._stream_req_slots.get(request_id)
if fast is not None and slot is not None:
fast.order_after(slot)
if fast.take_decoded(request_id):
unsent = fast.take_unsent_audio(request_id)
if unsent is not None:
unsent_first_audio[output_index] = unsent
# The fast path already emitted this chunk's audio and
# advanced the slot state past it.
codes_nq_t = codes_nq_t[:, fast.frames :]
if codes_nq_t.shape[1] == 0:
if unsent is not None:
outputs[output_index] = unsent
if finished:
self._finish_stream_request(request_id, session, slot)
continue
if output_buffers is not None:
# Sized for the untrimmed chunk; decode into a fresh tensor.
output_buffers.pop(output_index, None)
if slot is None:
slot = session.acquire()
if slot is None:
@@ -580,9 +897,11 @@ class MossTTSCodecDecoder(nn.Module):
slot,
codes_nq_t,
terminal=finished,
output_buffer=output_buffers.get(output_index) if output_buffers is not None else None,
)
if wav is not None:
outputs[output_index] = wav
prefix = unsent_first_audio.pop(output_index, None)
outputs[output_index] = torch.cat((prefix, wav), dim=-1) if prefix is not None else wav
if finished:
self._finish_stream_request(
request_id,
@@ -593,21 +912,33 @@ class MossTTSCodecDecoder(nn.Module):
continue
frame_count = int(codes_nq_t.shape[1])
# Only combine tails that already used the same padded graph.
# Preserve exact-size graphs (including T=1) and the eager path.
group_frames = frame_count
if coalesce_tails and finished:
# Only terminal rows may be padded: their state is reset after
# the step, and the output is cropped to the exact frame count.
group_frames = graph._select_frame_size(frame_count, allow_padding=True) or frame_count
grouped.setdefault(group_frames, []).append((output_index, request_id, slot, codes_nq_t, finished))
if fast is not None and grouped:
fast.gate()
for group in grouped.values():
plan = {slot: codes_nq_t for _, _, slot, codes_nq_t, _ in group}
terminal_slots = {slot for _, _, slot, _, finished in group if finished}
decoded = session.step(plan, terminal_slots=terminal_slots)
step_buffers = (
{
slot: output_buffers[output_index]
for output_index, _, slot, _, _ in group
if output_buffers is not None and output_index in output_buffers
}
if output_buffers is not None
else None
)
decoded = session.step(plan, terminal_slots=terminal_slots, output_buffers=step_buffers)
for output_index, request_id, slot, _, finished in group:
wav = decoded.get(slot)
if wav is not None:
outputs[output_index] = wav
prefix = unsent_first_audio.pop(output_index, None)
outputs[output_index] = torch.cat((prefix, wav), dim=-1) if prefix is not None else wav
if finished:
self._finish_stream_request(
request_id,
@@ -625,6 +956,7 @@ class MossTTSCodecDecoder(nn.Module):
codes_nq_t: torch.Tensor,
*,
terminal: bool = False,
output_buffer: torch.Tensor | None = None,
) -> torch.Tensor | None:
if codes_nq_t.numel() == 0:
return None
@@ -633,13 +965,24 @@ class MossTTSCodecDecoder(nn.Module):
for start in range(0, int(codes_nq_t.shape[1]), max_step_frames):
chunk = codes_nq_t[:, start : start + max_step_frames]
is_terminal_chunk = terminal and start + max_step_frames >= int(codes_nq_t.shape[1])
chunk_samples = int(chunk.shape[1]) * self._codec.downsample_rate
chunk_buffer = (
output_buffer[
..., start * self._codec.downsample_rate : start * self._codec.downsample_rate + chunk_samples
]
if output_buffer is not None
else None
)
decoded = session.step(
{slot: chunk},
terminal_slots={slot} if is_terminal_chunk else None,
output_buffers={slot: chunk_buffer} if chunk_buffer is not None else None,
)
wav = decoded.get(slot)
if wav is not None:
parts.append(wav)
if output_buffer is not None:
return output_buffer
if not parts:
return None
return torch.cat(parts, dim=-1) if len(parts) > 1 else parts[0]
@@ -665,29 +1008,30 @@ class MossTTSCodecDecoder(nn.Module):
terminal payload, so the runner calls this hook from its finished-request
path to avoid leaking stream slots and buffered codes.
"""
session = self._stream_session
for req_id in finished_req_ids:
request_id = str(req_id)
slot = self._stream_req_slots.get(request_id)
if slot is None:
continue
if session is not None:
self._finish_stream_request(request_id, session, slot)
else:
self._stream_req_slots.pop(request_id, None)
# Cancellation and terminal cleanup share the decoder's stream order;
# a reused slot must not be reset ahead of an outstanding decode.
context = torch.cuda.stream(self._codec_stream) if self._codec_stream is not None else nullcontext()
with context:
session = self._stream_session
for req_id in finished_req_ids:
request_id = str(req_id)
fast = self._first_chunk_fast_path
slot = (
fast.get_request_slot(request_id, self._stream_req_slots)
if fast is not None
else self._stream_req_slots.get(request_id)
)
if slot is None:
continue
if fast is not None:
fast.order_after(slot)
fast.forget(request_id)
if session is not None:
self._finish_stream_request(request_id, session, slot)
else:
self._stream_req_slots.pop(request_id, None)
def _connector_int(self, name: str, default: int = 0) -> int:
model_cfg = getattr(self.vllm_config, "model_config", None)
connector_cfg = getattr(model_cfg, "stage_connector_config", None)
if isinstance(connector_cfg, dict):
extra_cfg: dict | None = connector_cfg.get("extra", connector_cfg)
else:
extra_cfg = getattr(connector_cfg, "extra", None)
if isinstance(extra_cfg, dict) and name in extra_cfg:
return int(extra_cfg[name])
return default
def _connector_extra_cfg(self) -> dict:
def _connector_extra(self) -> dict[str, Any]:
model_cfg = getattr(self.vllm_config, "model_config", None)
connector_cfg = getattr(model_cfg, "stage_connector_config", None)
if isinstance(connector_cfg, dict):
@@ -696,6 +1040,9 @@ class MossTTSCodecDecoder(nn.Module):
extra_cfg = getattr(connector_cfg, "extra", None)
return extra_cfg if isinstance(extra_cfg, dict) else {}
def _connector_int(self, name: str, default: int = 0) -> int:
return int(self._connector_extra().get(name, default))
def _streaming_graph_batch_sizes_from_compilation_config(self) -> list[int]:
if getattr(self.vllm_config.model_config, "enforce_eager", True):
return []
@@ -816,15 +1163,33 @@ class MossTTSCodecDecoder(nn.Module):
codec.decoder.to(dtype=torch.bfloat16)
attention_backend = getattr(self.vllm_config.model_config.hf_config, "codec_attention_backend", "sdpa")
if attention_backend != "sdpa":
if attention_backend != "triton" or device.type != "cuda":
if attention_backend not in {"triton", "triton_slot"} or device.type != "cuda":
raise ValueError(f"Unsupported codec attention backend/device: {attention_backend}/{device.type}")
from vllm_omni.model_executor.models.moss_tts.audio_tokenizer_v2 import MossAudioTokenizerMultiheadAttention
from vllm_omni.model_executor.models.moss_tts.streaming_attention import masked_attention
if attention_backend == "triton_slot":
from vllm_omni.model_executor.models.moss_tts.slot_attention import (
slot_ring_attention,
slot_ring_attention_rows,
)
skip_empty_tiles = bool(self._connector_int("codec_skip_empty_attention_tiles", default=0))
fused_slots = bool(self._connector_int("codec_fused_slot_attention", default=0))
if skip_empty_tiles and fused_slots:
raise ValueError("codec_skip_empty_attention_tiles requires unfused slot attention")
if skip_empty_tiles:
slot_ring_attention = partial(slot_ring_attention, skip_empty_tiles=True)
logger.info("MOSS codec slot attention: skipping empty tiles for T <= 32")
for module in codec.decoder.modules():
if isinstance(module, MossAudioTokenizerMultiheadAttention):
module._streaming_attention = masked_attention
logger.info("Enabled Triton masked attention for the streaming codec decoder")
if attention_backend == "triton_slot":
module._slot_attention = slot_ring_attention
if self._connector_int("codec_fused_slot_attention", default=0):
module._slot_attention_rows = slot_ring_attention_rows
logger.info("Enabled codec attention backend=%s", attention_backend)
build_decode_lut = getattr(codec.quantizer, "build_decode_lut", None)
if callable(build_decode_lut):
lut_dtype = torch.bfloat16 if device.type == "cuda" else torch.float32
@@ -21,7 +21,8 @@ checkpoint 1:1 so ``load_weights()`` needs no remapping.
from __future__ import annotations
from collections.abc import Sequence
import os
from collections.abc import Callable, Sequence
import torch
import torch.nn as nn
@@ -52,6 +53,7 @@ class _MossTTSLocalAttention(nn.Module):
self.n_head = n_head
self.head_dim = hidden_size // n_head
self.embed_dim = hidden_size
self._short_attention = os.environ.get("VLLM_OMNI_MOSS_LOCAL_SHORT_ATTN", "0") == "1"
self.c_attn = nn.Linear(hidden_size, 3 * hidden_size, bias=True)
self.c_proj = nn.Linear(hidden_size, hidden_size, bias=True)
inv_freq = 1.0 / (rope_base ** (torch.arange(0, self.head_dim, 2, dtype=torch.float32) / self.head_dim))
@@ -134,7 +136,19 @@ class _MossTTSLocalAttention(nn.Module):
v_cache[:, :, position : position + 1].copy_(value)
key = k_cache[:, :, : position + 1]
value = v_cache[:, :, : position + 1]
attn_output = F.scaled_dot_product_attention(query, key, value, is_causal=kv_cache is None)
if (
self._short_attention
and kv_cache is not None
and query.is_cuda
and query.dtype == torch.bfloat16
and self.head_dim <= 128
and key.shape[2] <= 16
):
from vllm_omni.model_executor.models.moss_tts.local_short_attention import local_short_attention
attn_output = local_short_attention(query, key, value)
else:
attn_output = F.scaled_dot_product_attention(query, key, value, is_causal=kv_cache is None)
attn_output = attn_output.transpose(1, 2).reshape(batch_size, seq_len, self.embed_dim)
return self.c_proj(attn_output)
@@ -193,6 +207,7 @@ class MossTTSLocalDepthTransformer(nn.Module):
self.h = nn.ModuleList([_MossTTSLocalBlock(self.hidden_size, n_head, inner_size, rope_base, eps)])
self.ln_f = nn.LayerNorm(self.hidden_size, eps=eps)
self._compiled_forward_prefix = None
self._compiled_audio_sampler = None
def _forward_prefix(
self,
@@ -216,6 +231,14 @@ class MossTTSLocalDepthTransformer(nn.Module):
dynamic=True,
options={"epilogue_fusion": False},
)
if os.environ.get("VLLM_OMNI_MOSS_LOCAL_COMPILE_AUDIO_SAMPLER", "0") == "1":
# Keep the existing top-k/top-p algorithm and torch RNG. Explicit
# per-request generators use the original helper below. The
# binary continue/stop head is deliberately unchanged.
self._compiled_audio_sampler = torch.compile(
_sample_token, fullgraph=True, dynamic=True, options={"fallback_random": True}
)
logger.info("MOSS-TTS Local compiled audio-channel sampler enabled")
logger.info("MOSS-TTS local depth frame-local KV execution enabled with torch.compile")
def _run_prefix(
@@ -239,6 +262,7 @@ class MossTTSLocalDepthTransformer(nn.Module):
do_sample: bool,
generator: torch.Generator | None,
generators: Sequence[torch.Generator | None] | None = None,
sample_token: Callable[..., torch.Tensor] | None = None,
) -> torch.Tensor:
"""Compute channel logits, apply repetition penalty, sample, store."""
channel_logits = audio_lm_heads[channel_index](local_hidden).float()
@@ -250,7 +274,7 @@ class MossTTSLocalDepthTransformer(nn.Module):
pos = sel > 0
sel = torch.where(pos, sel / repetition_penalty, sel * repetition_penalty)
channel_logits.index_copy_(-1, hist_t, sel)
channel_token = _sample_token(
channel_token = (sample_token or _sample_token)(
channel_logits,
temperature,
top_k,
@@ -351,6 +375,7 @@ class MossTTSLocalDepthTransformer(nn.Module):
do_sample=do_sample,
generator=generator,
generators=generators,
sample_token=self._compiled_audio_sampler if generator is None and generators is None else None,
)
if channel_index + 1 < n_vq:
@@ -37,6 +37,7 @@ from vllm_omni.model_executor.models.moss_tts.modeling_moss_tts_local_depth impo
)
from vllm_omni.model_executor.models.output_templates import OmniOutput
from vllm_omni.platforms import current_omni_platform
from vllm_omni.worker.sampling_utils import get_tts_local_seed
logger = init_logger(__name__)
@@ -1675,6 +1676,31 @@ class MossTTSLocalTalkerForGeneration(nn.Module):
return input_embeds_out, output_codes
# ------------------------------------------------------------------
# MRV2 reads model-owned capabilities; retain the V1 hook and platform
# graph-safety setting as the canonical implementation.
mtp = talker_mtp
get_mtp_seed = staticmethod(get_tts_local_seed)
def create_mrv2_model_state(self, vllm_config, encoder_cache, device):
from vllm_omni.worker_v2.model_states.omni_model_state import OmniModelState
state_cls = OmniModelState
if getattr(self.config, "mrv2_gpu_slot_state", False):
from .local_model_state import MossLocalModelState
state_cls = MossLocalModelState
return state_cls(vllm_config, self, encoder_cache, device)
@property
def mtp_output_key(self) -> tuple[str, str]:
"""Return the request-buffer destination for one local audio frame."""
return self.talker_mtp_output_key
@property
def mtp_graph_safe(self) -> bool:
"""Honor the same platform graph-safety override as the V1 runner."""
return self.talker_mtp_graph_safe
# Package runner-generated audio frames
# ------------------------------------------------------------------
@@ -97,6 +97,7 @@ MOSS_TTS_LOCAL_PIPELINE = PipelineConfig(
StagePipelineConfig(
stage_id=0,
model_stage="moss_tts_local",
supports_native_mrv2_data_plane=True,
execution_type=StageExecutionType.LLM_AR,
input_sources=(),
owns_tokenizer=True,
@@ -114,6 +115,7 @@ MOSS_TTS_LOCAL_PIPELINE = PipelineConfig(
StagePipelineConfig(
stage_id=1,
model_stage="moss_tts_local_codec",
supports_native_mrv2_data_plane=True,
execution_type=StageExecutionType.LLM_GENERATION,
input_sources=(0,),
final_output=True,
@@ -25,7 +25,10 @@ so importing it from the API-server process does not pull the talker/codec.
from __future__ import annotations
import asyncio
import contextlib
import hashlib
import os
from collections import OrderedDict
from collections.abc import Awaitable, Callable
from functools import lru_cache
from typing import Any
@@ -35,22 +38,93 @@ import torch
from numpy.typing import NDArray
from vllm.logger import init_logger
from vllm_omni.model_executor.models.moss_tts.reference_encoder_graphs import (
FINE_BATCH_SIZES,
FINE_BUCKET_SECONDS,
MossReferenceEncoderGraphs,
install_windowed_attention,
)
from vllm_omni.model_executor.models.moss_tts.shared_reference_encoder import (
SharedReferenceEncoderClient,
SharedReferenceEncoderHost,
SharedReferenceEncoderStartupError,
elect_host,
)
logger = init_logger(__name__)
# Coalescing defaults. Kept as module constants (not env/CLI) to match the
# ``_REF_AUDIO_RESOLVE_CACHE_MAX_*`` convention in serving_speech.py.
_REF_ENCODE_BATCH_WINDOW_MS = 10.0
_REF_ENCODE_MAX_BATCH = 8
_REF_ENCODE_MAX_BATCH = 32
# Experiment switches for cold-reference latency (see the A/B notes):
# batch window in ms; batches in flight per API process with a shared encoder;
# "high" puts reference encodes on the highest-priority CUDA stream.
_REF_ENCODE_WINDOW_ENV = "VLLM_OMNI_MOSS_REF_BATCH_WINDOW_MS"
_REF_ENCODE_INFLIGHT_ENV = "VLLM_OMNI_MOSS_REF_INFLIGHT"
_REF_ENCODE_PRIORITY_ENV = "VLLM_OMNI_MOSS_REF_STREAM_PRIORITY"
# "1" resamples and loudness-normalizes clips on the encoder's GPU instead of
# the API process's CPU (not bit-identical to the CPU path).
_REF_ENCODE_GPU_PREP_ENV = "VLLM_OMNI_MOSS_REF_GPU_PREP"
_INT32_MAX = 2**31
# Inline (data: URI) references already resolved and encoded, keyed by the
# serving resolve key. Codes are small; the URI itself is not retained.
_INLINE_INDEX_MAX_ENTRIES = 4096
# Optional directory (e.g. under /dev/shm) that API processes of one server
# share, so an inline reference encoded by any frontend is reused by all.
_SHARED_CODES_DIR_ENV = "VLLM_OMNI_MOSS_REF_CODES_SHARED_DIR"
# "0" keeps one reference encoder per API process even with a shared directory.
_SHARED_ENCODER_ENV = "VLLM_OMNI_MOSS_REF_SHARED_ENCODER"
_shared_role: str | None = None
# Concurrent encode workers in the shared host, each with its own CUDA graphs.
_SHARED_WORKERS_ENV = "VLLM_OMNI_MOSS_REF_ENCODER_WORKERS"
_SHARED_WORKER_BATCH_SECONDS = 96.0
def shared_encoder_role() -> str:
"""``"host"``, ``"client"`` or ``""`` (no shared encoder) for this process.
Decided once, before the processor places its tokenizer on the GPU, so
client processes can leave it on the CPU.
"""
global _shared_role
if _shared_role is None:
shared_dir = os.environ.get(_SHARED_CODES_DIR_ENV)
if not shared_dir or os.environ.get(_SHARED_ENCODER_ENV, "1") == "0":
_shared_role = ""
else:
os.makedirs(shared_dir, exist_ok=True)
_shared_role = "host" if elect_host(shared_dir) else "client"
return _shared_role
# Opt in to reference graphs; existing deployments keep the eager encoder.
_REF_ENCODE_GRAPHS_ENV = "VLLM_OMNI_MOSS_REF_GRAPHS"
def reference_graphs_enabled() -> bool:
return os.environ.get(_REF_ENCODE_GRAPHS_ENV, "0") == "1"
def reference_stream_priority() -> int:
"""CUDA stream priority for reference encodes (lower is more urgent)."""
if os.environ.get(_REF_ENCODE_PRIORITY_ENV, "") == "high" and torch.cuda.is_available():
return torch.cuda.Stream.priority_range()[1]
return 0
# "sdpa" keeps the tokenizer's dense masked SDPA attention. By default the
# reference encoder attends through a local-window flash kernel, the backend
# the tokenizer config selects (flash_attention_2) when flash-attn is present.
_REF_ENCODE_ATTN_ENV = "VLLM_OMNI_MOSS_REF_ATTN"
# "0" captures the encoder's eager kernels. By default the captured encoder is
# compiled first, which fuses its many small elementwise kernels.
_REF_ENCODE_COMPILE_ENV = "VLLM_OMNI_MOSS_REF_COMPILE"
_Waveform = NDArray[np.float32] | list[float]
def _sha1(s: str) -> str:
return hashlib.sha1((s or "").encode("utf-8")).hexdigest()
@lru_cache(maxsize=16)
def _reference_resampler(sr: int, sr_target: int) -> torch.nn.Module:
import torchaudio
@@ -72,6 +146,13 @@ def _prep_wav_sync(waveform: _Waveform, sr: int, sr_target: int) -> torch.Tensor
return wav
def _inline_reference_key(ref_str: str) -> str | None:
"""SHA-1 of a ``data:`` URI, matching the serving resolve key for it."""
if not isinstance(ref_str, str) or ref_str[:5].lower() != "data:":
return None
return hashlib.sha1(ref_str.encode("utf-8")).hexdigest()
def _to_compact_codes(codes: torch.Tensor) -> torch.Tensor:
"""Downcast RVQ codes to int32 for compact caching."""
codes = codes.detach().cpu().contiguous()
@@ -119,18 +200,26 @@ class _RefEncodeBatcher:
*,
window_ms: float,
max_batch: int,
max_inflight: int = 1,
):
# encode_batch_fn: sync, takes [(waveform, sr), ...], returns a list of
# (codes_tensor | Exception) aligned to the input order.
self._encode_batch_fn = encode_batch_fn
self._window_s = max(0.0, float(window_ms) / 1000.0)
self._max_batch = max(1, int(max_batch))
# Batches encoded at once. Clips that arrive while every slot is busy
# are queued and form the next batch.
self._max_inflight = max(1, int(max_inflight))
self._queue: asyncio.Queue | None = None
self._slots: asyncio.Semaphore | None = None
self._inflight: set[asyncio.Task] = set()
self._drainer: asyncio.Task | None = None
def _ensure_started(self) -> None:
if self._queue is None:
self._queue = asyncio.Queue()
if self._slots is None:
self._slots = asyncio.Semaphore(self._max_inflight)
if self._drainer is None or self._drainer.done():
self._drainer = asyncio.create_task(self._drain_loop())
@@ -141,16 +230,27 @@ class _RefEncodeBatcher:
return await fut
async def _drain_loop(self) -> None:
assert self._queue is not None
assert self._queue is not None and self._slots is not None
while True:
first = await self._queue.get()
jobs = None
await self._slots.acquire()
try:
jobs = await self._coalesce(first)
await self._run_batch(jobs)
finally:
# Do not retain the completed batch while waiting for new work.
del first, jobs
jobs = await self._coalesce(await self._queue.get())
except BaseException:
self._slots.release()
raise
task = asyncio.create_task(self._run_and_release(jobs))
self._inflight.add(task)
task.add_done_callback(self._inflight.discard)
# Do not retain the batch here while waiting for new work.
del jobs
async def _run_and_release(self, jobs: list[tuple[_Waveform, int, asyncio.Future]]) -> None:
try:
await self._run_batch(jobs)
finally:
del jobs
assert self._slots is not None
self._slots.release()
async def _coalesce(self, first: tuple) -> list[tuple]:
"""Group ``first`` with jobs arriving within the batch window."""
@@ -200,6 +300,8 @@ class _RefEncodeBatcher:
except (asyncio.CancelledError, Exception): # noqa: BLE001
pass
self._drainer = None
for task in list(self._inflight):
task.cancel()
class MossReferenceEncoder:
@@ -213,7 +315,7 @@ class MossReferenceEncoder:
n_vq: int,
sr_target: int,
speaker_cache: Any,
window_ms: float = _REF_ENCODE_BATCH_WINDOW_MS,
window_ms: float | None = None,
max_batch: int = _REF_ENCODE_MAX_BATCH,
):
self._processor = processor
@@ -224,8 +326,33 @@ class MossReferenceEncoder:
# model_type namespaces the whole family so a moss_tts server never
# collides with another model's speaker-cache entries.
self._model_type = f"moss_tts_{variant}_nq{int(n_vq)}"
self._inflight: dict[str, asyncio.Task] = {}
self._batcher = _RefEncodeBatcher(self._encode_batch_sync, window_ms=window_ms, max_batch=max_batch)
self._inflight: dict[str | tuple[str, str], asyncio.Task] = {}
self._inline_index: OrderedDict[str, torch.Tensor] = OrderedDict()
self._shared_codes_dir = os.environ.get(_SHARED_CODES_DIR_ENV) or None
if self._shared_codes_dir:
os.makedirs(self._shared_codes_dir, exist_ok=True)
# Realtime encodes with its own codec and never shares the encoder.
self._shares_encoder = bool(self._shared_codes_dir) and type(self)._encode_prepared is (
MossReferenceEncoder._encode_prepared
)
self._shared_host: SharedReferenceEncoderHost | None = None
self._shared_client: SharedReferenceEncoderClient | None = None
self._graphs: MossReferenceEncoderGraphs | None = None
self._attention_installed = False
self._graphs_enabled = reference_graphs_enabled()
if window_ms is None:
window_ms = float(os.environ.get(_REF_ENCODE_WINDOW_ENV, _REF_ENCODE_BATCH_WINDOW_MS))
# A shared encoder serves several batches at once (one per host
# worker); a process's own graphs replay one batch at a time.
inflight = 1
if self._shares_encoder and os.environ.get(_SHARED_ENCODER_ENV, "1") != "0":
inflight = max(1, int(os.environ.get(_REF_ENCODE_INFLIGHT_ENV, "1")))
self._local_stream: torch.cuda.Stream | None = None
self._gpu_prep = os.environ.get(_REF_ENCODE_GPU_PREP_ENV, "0") == "1"
self._gpu_resamplers: dict[torch.device, torch.nn.Module] = {}
self._batcher = _RefEncodeBatcher(
self._encode_batch_sync, window_ms=window_ms, max_batch=max_batch, max_inflight=inflight
)
def _make_cache_key(self, name: str, created_at: int) -> tuple:
return self._speaker_cache.make_cache_key(name, model_type=self._model_type, created_at=int(created_at))
@@ -251,6 +378,7 @@ class MossReferenceEncoder:
caller salts those requests with ``voice_created_at`` instead).
"""
voice_name, created_at = _registered_voice(voice_name, voice_created_at)
inline_key: str | None = None
if voice_name:
# A named voice has a stable key that does not depend on the
@@ -260,6 +388,16 @@ class MossReferenceEncoder:
if cached is not None:
return _clone_out(cached["codes"]), None
else:
inline_key = _inline_reference_key(ref_str)
if inline_key is not None:
codes = self._inline_index.get(inline_key)
if codes is None:
codes = self._load_shared_codes(inline_key)
if codes is not None:
# A data: URI is immutable content, and its serving
# resolve key is this same digest: skip the resolve hop.
self._inline_index.move_to_end(inline_key)
return _clone_out(codes), inline_key
# Anonymous refs have no pre-resolve hot path: the content key must
# come from the resolve itself (mtime/size from a single stat) so an
# on-disk edit invalidates the cached codes; the flight body
@@ -267,17 +405,55 @@ class MossReferenceEncoder:
# when the resolve cache is warm. The flight key is the request-side
# reference (not the content hash), so concurrent requests for the
# same ref_str also share the resolve/download, not just the encode.
flight_key = "ref:" + _sha1(ref_str)
# Key by the locator itself: equality is exact, and hashing a
# large inline data URI once per request object is much cheaper
# than a separate SHA-1 of its UTF-8 copy on the event loop.
flight_key = ("ref", ref_str)
codes, resolve_key = await self._single_flight(
flight_key,
lambda: self._resolve_and_encode(ref_str, resolve_ref_audio, get_artifact_key, voice_name, created_at),
)
if not voice_name and inline_key is not None and resolve_key == inline_key:
# Only index when the resolver's key is exactly this digest.
self._remember_inline(inline_key, codes)
self._store_shared_codes(inline_key, codes)
return _clone_out(codes), resolve_key
def _remember_inline(self, key: str, codes: torch.Tensor) -> None:
self._inline_index[key] = codes
self._inline_index.move_to_end(key)
while len(self._inline_index) > _INLINE_INDEX_MAX_ENTRIES:
self._inline_index.popitem(last=False)
def _load_shared_codes(self, key: str) -> torch.Tensor | None:
if not self._shared_codes_dir:
return None
try:
array = np.load(os.path.join(self._shared_codes_dir, key + ".npy"), allow_pickle=False)
except (OSError, ValueError):
return None
if array.ndim != 2 or array.shape[1] != self._n_vq or array.dtype not in (np.int32, np.int64):
return None
codes = torch.from_numpy(array)
self._remember_inline(key, codes)
return codes
def _store_shared_codes(self, key: str, codes: torch.Tensor) -> None:
if not self._shared_codes_dir:
return
path = os.path.join(self._shared_codes_dir, key + ".npy")
tmp = f"{path}.{os.getpid()}.tmp"
try:
with open(tmp, "wb") as handle:
np.save(handle, codes.detach().cpu().numpy(), allow_pickle=False)
os.replace(tmp, path)
except OSError:
logger.warning("MOSS ref codes: could not write shared entry %s", path, exc_info=True)
async def _single_flight(
self,
flight_key: str,
flight_key: str | tuple[str, str],
start_flight: Callable[[], Awaitable[tuple[torch.Tensor, str]]],
) -> tuple[torch.Tensor, str]:
"""Join the in-flight encode for ``flight_key``, starting one if absent.
@@ -300,7 +476,7 @@ class MossReferenceEncoder:
# a duplicate resolve/encode instead of joining this one. Identity
# guard: only drop the slot if it still holds *our* task (a later
# request may have replaced it after ours completed).
def _retire(t: asyncio.Task, key: str = flight_key) -> None:
def _retire(t: asyncio.Task, key: str | tuple[str, str] = flight_key) -> None:
if self._inflight.get(key) is t:
self._inflight.pop(key, None)
@@ -350,29 +526,228 @@ class MossReferenceEncoder:
prepared_idx: list[int] = []
for i, (waveform, sr) in enumerate(payload):
try:
prepared.append(_prep_wav_sync(waveform, sr, self._sr_target))
prepared.append(self._encoder_input(_prep_wav_sync(waveform, sr, self._sr_target)))
prepared_idx.append(i)
except Exception as exc: # noqa: BLE001 — isolate this clip's failure
results[i] = exc
if not prepared:
return results
try:
codes_list = self._encode_prepared(prepared)
for local_i, orig_i in enumerate(prepared_idx):
results[orig_i] = codes_list[local_i]
except Exception: # noqa: BLE001 — batch forward failed; retry item-by-item
logger.warning("MOSS ref batch encode (n=%d) failed; falling back to per-item", len(prepared))
for local_i, orig_i in enumerate(prepared_idx):
try:
results[orig_i] = self._encode_prepared([prepared[local_i]])[0]
except Exception as exc: # noqa: BLE001 — isolate this clip's failure
results[orig_i] = exc
for orig_i, codes in zip(prepared_idx, self._encode_shared_or_local(prepared)):
results[orig_i] = codes
return results
def _encode_prepared(self, prepared: list[torch.Tensor]) -> list[torch.Tensor]:
with torch.no_grad():
return self._processor.encode_audios_from_wav(prepared, sampling_rate=self._sr_target, n_vq=self._n_vq)
def prepare(self) -> None:
"""Capture the CUDA graphs (or start the shared encoder) before the first request."""
role = shared_encoder_role() if self._shares_encoder else ""
if role == "host":
self._host().wait_until_ready()
elif role == "client":
if self._shared_client is None:
self._shared_client = SharedReferenceEncoderClient(self._shared_codes_dir)
self._shared_client.wait_until_ready()
elif role == "":
self._reference_graphs()
def _host(self) -> SharedReferenceEncoderHost:
if self._shared_host is None:
# Graph capture runs in the background; clips queue until it ends.
self._shared_host = SharedReferenceEncoderHost(
self._shared_codes_dir,
self._host_workers(),
stream_priority=reference_stream_priority(),
batch_window_ms=float(os.environ.get("VLLM_OMNI_MOSS_REF_HOST_WINDOW_MS", "0")),
)
return self._shared_host
def _encode_shared_or_local(self, prepared: list[torch.Tensor]) -> list:
role = shared_encoder_role() if self._shares_encoder else ""
if role == "host":
return self._host().encode(prepared)
if role == "client":
if self._shared_client is None:
self._shared_client = SharedReferenceEncoderClient(self._shared_codes_dir)
try:
return self._shared_client.encode(prepared)
except SharedReferenceEncoderStartupError:
raise
except Exception: # noqa: BLE001 — the host is unreachable: encode here
logger.warning("MOSS shared reference encoder unavailable; encoding locally", exc_info=True)
self._place_tokenizer_for_local_encode()
with self._local_stream_context():
return self._encode_local(prepared)
def _local_stream_context(self):
tokenizer = getattr(self._processor, "audio_tokenizer", None)
params = next(tokenizer.parameters(), None) if isinstance(tokenizer, torch.nn.Module) else None
if params is None or params.device.type != "cuda":
return contextlib.nullcontext()
if self._local_stream is None:
self._local_stream = torch.cuda.Stream(device=params.device, priority=reference_stream_priority())
return torch.cuda.stream(self._local_stream)
def _host_workers(self) -> list:
"""Encode workers of the shared host, each with its own graphs and stream."""
count = max(1, int(os.environ.get(_SHARED_WORKERS_ENV, "4")))
if not self._graph_capable():
return [(self._encode_local, None)] * count
workers = []
for _ in range(count):
graphs = MossReferenceEncoderGraphs(
self._processor.audio_tokenizer,
n_vq=self._n_vq,
batch_sizes=FINE_BATCH_SIZES,
bucket_seconds=FINE_BUCKET_SECONDS,
max_batch_seconds=_SHARED_WORKER_BATCH_SECONDS,
compile_core=self._compile_graphs(),
batched_transfer=os.environ.get("VLLM_OMNI_MOSS_REF_BATCHED_TRANSFER", "0") == "1",
singleton_bucket_seconds=self._singleton_bucket_seconds(),
)
workers.append(((lambda wavs, g=graphs: self._encode_local(wavs, g)), graphs.capture))
return workers
def _encode_local(self, prepared: list[torch.Tensor], graphs: MossReferenceEncoderGraphs | None = None) -> list:
"""One batched forward, retried per clip so one bad clip fails alone."""
try:
return list(self._encode_prepared(prepared, graphs))
except Exception: # noqa: BLE001 — batch forward failed; retry item-by-item
logger.warning("MOSS ref batch encode (n=%d) failed; falling back to per-item", len(prepared))
results: list = []
for wav in prepared:
try:
results.append(self._encode_prepared([wav], graphs)[0])
except Exception as exc: # noqa: BLE001 — isolate this clip's failure
results.append(exc)
return results
def _place_tokenizer_for_local_encode(self) -> None:
"""A client process keeps the tokenizer off the GPU until it must encode."""
tokenizer = getattr(self._processor, "audio_tokenizer", None)
device = getattr(self._processor, "_vllm_omni_ref_encoder_device", None)
if tokenizer is not None and device is not None and next(tokenizer.parameters()).device != device:
self._processor.audio_tokenizer = tokenizer.to(device)
def _encode_prepared(
self, prepared: list[torch.Tensor], graphs: MossReferenceEncoderGraphs | None = None
) -> list[torch.Tensor]:
# ``prepared`` clips come from _encoder_input, at ``_clip_rate()``.
graphs = graphs or self._reference_graphs()
if graphs is None:
with torch.no_grad():
return self._processor.encode_audios_from_wav(
prepared, sampling_rate=self._clip_rate(), n_vq=self._n_vq
)
codes = graphs.encode([self._finish_prepare(wav) for wav in prepared])
missing = [i for i, c in enumerate(codes) if c is None]
if missing:
with torch.no_grad():
eager = self._processor.encode_audios_from_wav(
[prepared[i] for i in missing], sampling_rate=self._clip_rate(), n_vq=self._n_vq
)
for i, c in zip(missing, eager):
codes[i] = c
return codes
def _graph_capable(self) -> bool:
"""True when the tokenizer can run graphs; installs windowed attention once."""
if not self._graphs_enabled:
return False
tokenizer = getattr(self._processor, "audio_tokenizer", None)
device = next(tokenizer.parameters()).device if tokenizer is not None else None
if device is None or device.type != "cuda" or not hasattr(tokenizer, "_codec_inference_autocast"):
self._graphs_enabled = False
return False
if not self._attention_installed and os.environ.get(_REF_ENCODE_ATTN_ENV, "flash") == "flash":
logger.info(
"MOSS reference encoder: %d attention layers use local-window flash attention",
install_windowed_attention(
tokenizer,
fa_version=int(os.environ.get("VLLM_OMNI_MOSS_REF_FA_VERSION", "2")),
cache_rope=os.environ.get("VLLM_OMNI_MOSS_REF_CACHE_ROPE", "0") == "1",
skip_padded_query_mask=os.environ.get("VLLM_OMNI_MOSS_REF_SKIP_PADDED_QUERY_MASK", "0") == "1",
),
)
self._attention_installed = True
return True
@staticmethod
def _compile_graphs() -> bool:
return os.environ.get(_REF_ENCODE_COMPILE_ENV, "1") != "0"
@staticmethod
def _singleton_bucket_seconds() -> tuple[float, ...] | None:
step_ms = int(os.environ.get("VLLM_OMNI_MOSS_REF_SINGLETON_BUCKET_MS", "0"))
if step_ms == 0:
return None
if not 80 <= step_ms <= 1000:
raise ValueError("MOSS reference singleton bucket step must be 80..1000 ms, or 0 to disable")
return tuple(ms / 1000 for ms in range(3000, 10001, step_ms))
def _reference_graphs(self) -> MossReferenceEncoderGraphs | None:
"""Capture on first use, on the encode worker thread that replays them."""
if self._graphs is None:
if not self._graph_capable():
return None
graphs = MossReferenceEncoderGraphs(
self._processor.audio_tokenizer,
n_vq=self._n_vq,
compile_core=self._compile_graphs(),
batched_transfer=os.environ.get("VLLM_OMNI_MOSS_REF_BATCHED_TRANSFER", "0") == "1",
singleton_bucket_seconds=self._singleton_bucket_seconds(),
)
graphs.capture()
self._graphs = graphs
return self._graphs
def _tokenizer_rate(self) -> int:
return int(getattr(getattr(self._processor, "model_config", None), "sampling_rate", self._sr_target))
def _encoder_input(self, wav: torch.Tensor) -> torch.Tensor:
"""A prepped clip at the tokenizer rate, one or two channels.
This is the costly half of the processor's per-clip preparation; it runs
in the process that received the request, even with a shared encoder.
"""
import torchaudio
if wav.ndim == 1:
wav = wav.unsqueeze(0)
if wav.shape[0] > 2:
wav = wav[:2]
if self._clip_rate() != self._sr_target:
# Channels resample independently, so a mono clip is resampled
# once before it is duplicated; the result is the same.
wav = torchaudio.functional.resample(wav, self._sr_target, self._tokenizer_rate())
return wav
def _clip_rate(self) -> int:
"""Sample rate of the clips _encoder_input hands to the encoder."""
return self._sr_target if self._gpu_prep else self._tokenizer_rate()
def _finish_prepare(self, wav: torch.Tensor) -> torch.Tensor:
"""The rest of the processor's preparation: two channels, normalized loudness."""
if self._gpu_prep:
device = next(self._processor.audio_tokenizer.parameters()).device
wav = wav.to(device)
if self._clip_rate() != self._tokenizer_rate():
wav = self._gpu_resampler(device)(wav)
if wav.shape[0] == 1:
wav = wav.repeat(2, 1)
return self._processor.loudness_normalize(wav)
def _gpu_resampler(self, device: torch.device) -> torch.nn.Module:
resampler = self._gpu_resamplers.get(device)
if resampler is None:
import torchaudio
# The same windowed-sinc filter as torchaudio.functional.resample's defaults.
resampler = torchaudio.transforms.Resample(self._sr_target, self._tokenizer_rate()).to(device)
self._gpu_resamplers[device] = resampler
return resampler
def _processor_prepare(self, wav: torch.Tensor) -> torch.Tensor:
"""Per-clip preparation of the processor's ``encode_audios_from_wav``."""
return self._finish_prepare(self._encoder_input(wav))
async def aclose(self) -> None:
"""Release the batcher drainer + any in-flight encodes (tests/shutdown)."""
@@ -385,7 +760,10 @@ class MossReferenceEncoder:
class MossRealtimeReferenceEncoder(MossReferenceEncoder):
"""Use the realtime codec with the shared reference cache and batcher."""
def _encode_prepared(self, prepared: list[torch.Tensor]) -> list[torch.Tensor]:
def _encoder_input(self, wav: torch.Tensor) -> torch.Tensor:
return wav # the realtime codec takes the clip at the working rate
def _encode_prepared(self, prepared: list[torch.Tensor], graphs: Any = None) -> list[torch.Tensor]:
with torch.no_grad():
encoded = self._processor.batch_encode([wav.squeeze(0) for wav in prepared], num_quantizers=self._n_vq)
return [
@@ -0,0 +1,399 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""CUDA graphs for batched MOSS reference-audio encoding.
Reference clips are encoded by the upstream MOSS audio tokenizer that the
processor loads. Its eager forward is dominated by kernel launches for the
small batches that serving produces. This runner replays graphs of the same
encoder and quantizer modules, captured per ``(batch, length)`` bucket. For a
given padded input the codes are bit-identical to the tokenizer's own
``_encode_frame``. Only the per-clip crop, which needs host values, runs
outside the graph.
The encoder is causal, so padding a clip to its length bucket leaves its valid
frames unchanged up to kernel selection for the padded shape, the same effect
that batching already has when the tokenizer pads a batch to its longest clip.
"""
from __future__ import annotations
import math
import types
from dataclasses import dataclass
import torch
from vllm.logger import init_logger
logger = init_logger(__name__)
DEFAULT_BATCH_SIZES = (1, 2, 4, 8, 16, 32)
DEFAULT_BUCKET_SECONDS = (4.0, 6.0, 8.0, 10.0, 12.0, 16.0, 20.0)
# Fine steps keep padding small: a padded row or padded time is encoded in
# full. Worth the extra graphs where several encoders share one GPU.
FINE_BATCH_SIZES = (1, 2, 3, 4, 5, 6, 8)
FINE_BUCKET_SECONDS = (3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 10.0, 12.0, 14.0, 16.0, 20.0)
# Graphs are captured only while batch * bucket stays within this much audio,
# which bounds the shared graph memory pool by its largest batch.
DEFAULT_MAX_BATCH_SECONDS = 256.0
@torch.compile(dynamic=False)
def _reference_rope_factors(seqlen: int, head_dim: int, max_period: float, device: torch.device):
# Use the same compiled FP32 arithmetic as the encoder. Eager libdevice
# sin/cos and compiled sin/cos can round differently near a VQ boundary.
ds = torch.arange(head_dim // 2, device=device, dtype=torch.float32)
freqs = torch.exp(ds * (-math.log(max_period) * 2 / head_dim))
angles = torch.arange(seqlen, device=device, dtype=torch.float32)[:, None] * freqs[None, :]
return torch.cos(angles), torch.sin(angles)
def _cached_reference_rope(self, q: torch.Tensor, k: torch.Tensor, cache: dict):
if self.rope is None:
return q, k
batch, heads, seqlen, dim = q.shape
key = (dim, self.rope.max_period, q.device)
if key not in cache or cache[key][0].shape[0] < seqlen:
cache[key] = _reference_rope_factors(seqlen, dim, self.rope.max_period, q.device)
cos, sin = cache[key]
cos, sin = cos[:seqlen], sin[:seqlen]
qr, qi = q.view(batch, heads, seqlen, dim // 2, 2).unbind(-1)
kr, ki = k.view(batch, heads, seqlen, dim // 2, 2).unbind(-1)
qr, qi, kr, ki = qr.float(), qi.float(), kr.float(), ki.float()
# Preserve the original arithmetic, casts, and B/H/T/D output layout.
qo = torch.stack(((qr * cos - qi * sin).to(q.dtype), (qr * sin + qi * cos).to(q.dtype)), dim=-1)
ko = torch.stack(((kr * cos - ki * sin).to(k.dtype), (kr * sin + ki * cos).to(k.dtype)), dim=-1)
return qo.view(batch, heads, seqlen, dim), ko.view(batch, heads, seqlen, dim)
def _windowed_attention(
self,
x: torch.Tensor,
input_lengths: torch.Tensor,
*,
sdpa,
fa_version: int = 2,
rope_cache: dict | None = None,
skip_padded_query_mask: bool = False,
) -> torch.Tensor:
"""The tokenizer's non-streaming attention with a local-window flash kernel.
Same projection, RoPE and padded-row zeroing as the tokenizer's SDPA path.
That path masks each query to ``context`` keys (distance 0 .. context-1)
inside a dense block; the flash kernel visits only that window.
"""
batch, seqlen, _ = x.shape
q, k, v = self._project_qkv(x)
q, k = self._apply_dense_rope(q, k) if rope_cache is None else _cached_reference_rope(self, q, k, rope_cache)
if q.dtype not in (torch.bfloat16, torch.float16):
return sdpa(x, input_lengths)
from vllm.vllm_flash_attn import flash_attn_varlen_func
heads, dim = q.shape[1], q.shape[-1]
def packed(t: torch.Tensor) -> torch.Tensor:
return t.transpose(1, 2).reshape(batch * seqlen, heads, dim)
starts = torch.arange(0, (batch + 1) * seqlen, seqlen, device=x.device, dtype=torch.int32)
window = [self.context - 1, 0] if self.causal and self.context is not None else [-1, -1]
out = flash_attn_varlen_func(
packed(q),
packed(k),
packed(v),
max_seqlen_q=seqlen,
cu_seqlens_q=starts,
max_seqlen_k=seqlen,
cu_seqlens_k=starts,
causal=self.causal,
window_size=window,
fa_version=fa_version,
).view(batch, seqlen, heads, dim)
if not skip_padded_query_mask:
valid = (torch.arange(seqlen, device=x.device).view(1, seqlen) < input_lengths.view(-1, 1)).view(
batch, seqlen, 1, 1
)
out = torch.where(valid, out, torch.zeros((), device=out.device, dtype=out.dtype))
return out.reshape(batch, seqlen, self.embed_dim)
def _validate_unmasked_reference_encoder(tokenizer: torch.nn.Module) -> None:
"""Restrict the optimization to the audited HF encoder's valid-prefix contract.
Causal attention and pointwise transformer operations cannot read future
padded rows. Its patch-downsample floors lengths, so every valid output
group consists entirely of valid input rows. The quantizer is pointwise
and the caller crops codes by those lengths. Padded *outputs* may differ.
This argument does not apply to the streaming decoder or to upsampling.
"""
transformers = 0
for module in tokenizer.encoder:
name = type(module).__name__
if name == "MossAudioTokenizerPatchedPretransform" and module.is_downsample:
continue
if name == "MossAudioTokenizerProjectedTransformer":
layers = module.transformer.layers
if layers and all(layer.self_attn.causal for layer in layers):
transformers += 1
continue
raise ValueError("Unmasked reference queries require the causal MOSS patch-downsample encoder")
if not transformers:
raise ValueError("Unmasked reference queries require at least one causal MOSS transformer")
if type(tokenizer.quantizer).__name__ not in {
"MossAudioTokenizerResidualVQ",
"MossAudioTokenizerResidualLFQ",
}:
raise ValueError("Unmasked reference queries require the pointwise MOSS quantizer")
for module in tokenizer.quantizer.modules():
if isinstance(module, torch.nn.Conv1d) and module.kernel_size != (1,):
raise ValueError("Unmasked reference queries do not support temporal quantizer convolutions")
def install_windowed_attention(
tokenizer: torch.nn.Module,
*,
fa_version: int = 2,
cache_rope: bool = False,
skip_padded_query_mask: bool = False,
) -> int:
"""Route the encoder's non-streaming attention through a local-window flash kernel."""
if skip_padded_query_mask:
_validate_unmasked_reference_encoder(tokenizer)
logger.info("MOSS reference encoder: skipping padded query masks (valid-prefix outputs only)")
count = 0
rope_cache = {} if cache_rope else None
if cache_rope:
tokenizer._moss_reference_rope_cache = rope_cache
tokenizer._moss_reference_rope_prepared_samples = 0
for module in tokenizer.encoder.modules():
sdpa = getattr(module, "_forward_non_streaming_sdpa", None)
if sdpa is None or not hasattr(module, "_project_qkv") or not getattr(module, "causal", False):
continue
def attention(self, x, input_lengths, _sdpa=sdpa):
return _windowed_attention(
self,
x,
input_lengths,
sdpa=_sdpa,
fa_version=fa_version,
rope_cache=rope_cache,
skip_padded_query_mask=skip_padded_query_mask,
)
module._forward_non_streaming_sdpa = types.MethodType(attention, module)
count += 1
return count
@dataclass
class _Graph:
graph: torch.cuda.CUDAGraph
audio: torch.Tensor # (B, C, T) float32 input
lengths: torch.Tensor # (B,) int64 input, in samples per channel
codes: torch.Tensor # (n_vq, B, frames) output
code_lengths: torch.Tensor # (B,) output
class MossReferenceEncoderGraphs:
"""Encode prepared reference waveforms with captured tokenizer graphs."""
def __init__(
self,
tokenizer: torch.nn.Module,
*,
n_vq: int,
batch_sizes: tuple[int, ...] = DEFAULT_BATCH_SIZES,
bucket_seconds: tuple[float, ...] = DEFAULT_BUCKET_SECONDS,
max_batch_seconds: float = DEFAULT_MAX_BATCH_SECONDS,
compile_core: bool = False,
batched_transfer: bool = False,
singleton_bucket_seconds: tuple[float, ...] | None = None,
) -> None:
self._tokenizer = tokenizer
self._compile_core = compile_core
self._batched_transfer = batched_transfer
self._n_vq = int(n_vq)
self._device = next(tokenizer.parameters()).device
self._channels = int(tokenizer.number_channels)
frame = int(tokenizer.downsample_rate)
sampling_rate = int(tokenizer.sampling_rate)
# Whole frames, so the tokenizer's own frame padding is a no-op.
self._buckets = sorted({int(math.ceil(s * sampling_rate / frame)) * frame for s in bucket_seconds})
self._singleton_buckets = sorted(
set(self._buckets)
| {int(math.ceil(s * sampling_rate / frame)) * frame for s in (singleton_bucket_seconds or ())}
)
self._batch_sizes = sorted({int(b) for b in batch_sizes if int(b) > 0})
self._max_batch_samples = float(max_batch_seconds) * sampling_rate
self._graphs: dict[tuple[int, int], _Graph] = {}
self._pool = None
self._transfer_buffers = None
@property
def max_batch(self) -> int:
return self._batch_sizes[-1]
@property
def captured(self) -> list[tuple[int, int]]:
return sorted(self._graphs)
def _core(self, audio: torch.Tensor, lengths: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
# Mirrors the tokenizer's _encode_frame without its host-side crop.
tokenizer = self._tokenizer
values, value_lengths = tokenizer._flatten_channels_for_codec(audio, lengths)
with tokenizer._codec_inference_autocast():
hidden, hidden_lengths = values, value_lengths
for module in tokenizer.encoder:
hidden, hidden_lengths = module(hidden, hidden_lengths)
_, codes, code_lengths = tokenizer.quantizer(hidden.float(), hidden_lengths, self._n_vq)
return codes, code_lengths
@torch.no_grad()
def capture(self) -> None:
if self._device.type != "cuda" or self._graphs:
return
self._pool = torch.cuda.graph_pool_handle()
stream = torch.cuda.Stream(device=self._device)
prepared = getattr(self._tokenizer, "_moss_reference_rope_prepared_samples", None)
max_samples = max(self._singleton_buckets)
if prepared is not None and prepared < max_samples:
# Prime immutable FP32 factors at the largest length before any
# capture or compilation. Smaller lengths use a prefix of the
# same table; the dictionary stays fixed throughout capture and
# workers share tables without introducing dynamic-shape guards
# for a separate dictionary key at each sequence length.
audio = torch.zeros(1, self._channels, max_samples, device=self._device)
lengths = torch.full((1,), max_samples, dtype=torch.long, device=self._device)
self._core(audio, lengths)
self._tokenizer._moss_reference_rope_prepared_samples = max_samples
core = self._core
if self._compile_core:
# Fuses the encoder's many small elementwise kernels (casts, RoPE,
# residuals). Only capture runs the compiled code; replays do not.
core = torch.compile(self._core, dynamic=True)
# Each encoder stage is its own module shape: allow a compile per stage.
with torch._dynamo.config.patch(recompile_limit=64):
self._capture_all(core, stream)
if self._batched_transfer and self._graphs:
# One reusable slab per worker, rather than one per graph bucket.
entries = list(self._graphs.values())
self._transfer_buffers = (
torch.empty(max(e.audio.numel() for e in entries), dtype=entries[0].audio.dtype, pin_memory=True),
torch.empty(max(e.lengths.numel() for e in entries), dtype=entries[0].lengths.dtype, pin_memory=True),
torch.empty(max(e.codes.numel() for e in entries), dtype=entries[0].codes.dtype, pin_memory=True),
torch.empty(
max(e.code_lengths.numel() for e in entries), dtype=entries[0].code_lengths.dtype, pin_memory=True
),
torch.cuda.Event(),
)
logger.info(
"MOSS reference encoder CUDA graphs: batch=%s buckets(s)=%s compiled=%s captured=%d",
self._batch_sizes,
[round(s / int(self._tokenizer.sampling_rate), 2) for s in self._buckets],
self._compile_core,
len(self._graphs),
)
if self._singleton_buckets != self._buckets:
logger.info(
"MOSS reference encoder singleton buckets(s): %s",
[round(s / int(self._tokenizer.sampling_rate), 2) for s in self._singleton_buckets],
)
def _capture_all(self, core, stream: torch.cuda.Stream) -> None:
for batch in reversed(self._batch_sizes):
buckets = self._singleton_buckets if batch == 1 else self._buckets
for samples in reversed(buckets):
if batch > 1 and batch * samples > self._max_batch_samples:
continue
audio = torch.zeros(batch, self._channels, samples, device=self._device)
lengths = torch.full((batch,), samples, dtype=torch.long, device=self._device)
try:
stream.wait_stream(torch.cuda.current_stream(self._device))
with torch.cuda.stream(stream):
# Lazy library setup must happen before capture; one run does it.
core(audio, lengths)
torch.cuda.current_stream(self._device).wait_stream(stream)
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph, pool=self._pool, stream=stream):
codes, code_lengths = core(audio, lengths)
except Exception:
logger.warning(
"MOSS reference encoder graph (B=%d, samples=%d) not captured; using eager",
batch,
samples,
exc_info=True,
)
continue
self._graphs[(batch, samples)] = _Graph(graph, audio, lengths, codes, code_lengths)
@torch.no_grad()
def encode(self, wavs: list[torch.Tensor]) -> list[torch.Tensor | None]:
"""Codes ``(frames, n_vq)`` int64 on CPU per clip; None where no graph fits.
``wavs`` are prepared the way the processor prepares them: ``(C, T)``
float32 after channel handling, resampling and loudness normalization.
Clips are grouped longest first, so each replay pads to the bucket of
similar-length clips.
"""
out: list[torch.Tensor | None] = [None] * len(wavs)
order = sorted(range(len(wavs)), key=lambda i: -int(wavs[i].shape[-1]))
pos = 0
while pos < len(order):
longest = int(wavs[order[pos]].shape[-1])
# Fine singleton buckets reduce padding without fragmenting an
# already assembled batch into separate graph replays.
buckets = self._singleton_buckets if len(order) - pos == 1 else self._buckets
samples = next((s for s in buckets if s >= longest and (1, s) in self._graphs), None)
if samples is None:
pos += 1 # longer than every bucket: left to the caller
continue
widest = max(b for b in self._batch_sizes if (b, samples) in self._graphs)
rows = order[pos : pos + widest]
batch = next(b for b in self._batch_sizes if b >= len(rows) and (b, samples) in self._graphs)
for row, codes in zip(rows, self._replay(self._graphs[(batch, samples)], [wavs[i] for i in rows])):
out[row] = codes
pos += len(rows)
return out
def _replay(self, entry: _Graph, wavs: list[torch.Tensor]) -> list[torch.Tensor]:
if self._transfer_buffers is not None and all(w.device.type == "cpu" for w in wavs):
return self._replay_batched_transfer(entry, wavs)
count, batch, samples = len(wavs), entry.audio.shape[0], entry.audio.shape[-1]
# Unused rows encode full-length silence: a fully masked row would be
# all-NaN inside attention, and rows never mix.
entry.audio.zero_()
lengths = [int(w.shape[-1]) for w in wavs] + [samples] * (batch - count)
entry.lengths.copy_(torch.tensor(lengths, dtype=torch.long))
for row, wav in enumerate(wavs):
entry.audio[row, :, : wav.shape[-1]].copy_(wav)
entry.graph.replay()
codes = entry.codes[:, :count].cpu()
code_lengths = entry.code_lengths[:count].tolist()
return [codes[:, row, : int(code_lengths[row])].transpose(0, 1).contiguous().long() for row in range(count)]
def _replay_batched_transfer(self, entry: _Graph, wavs: list[torch.Tensor]) -> list[torch.Tensor]:
"""Stage one batch on pinned host memory, then wait once for its outputs."""
audio_slab, lengths_slab, codes_slab, code_lengths_slab, copied = self._transfer_buffers
count, batch, samples = len(wavs), entry.audio.shape[0], entry.audio.shape[-1]
audio = audio_slab[: entry.audio.numel()].view_as(entry.audio)
lengths = lengths_slab[:batch]
audio.zero_()
lengths.fill_(samples)
for row, wav in enumerate(wavs):
audio[row, :, : wav.shape[-1]].copy_(wav)
lengths[row] = wav.shape[-1]
entry.audio.copy_(audio, non_blocking=True)
entry.lengths.copy_(lengths, non_blocking=True)
entry.graph.replay()
result = entry.codes[:, :count]
codes = codes_slab[: result.numel()].view_as(result)
code_lengths = code_lengths_slab[:count]
codes.copy_(result, non_blocking=True)
code_lengths.copy_(entry.code_lengths[:count], non_blocking=True)
copied.record(torch.cuda.current_stream(self._device))
copied.synchronize()
# Returned codes must outlive this worker's next replay, including
# one-frame clips whose transpose is already contiguous.
return [
codes[:, row, : int(code_lengths[row])].transpose(0, 1).to(dtype=torch.long, copy=True).contiguous()
for row in range(count)
]
@@ -0,0 +1,334 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""One reference-audio encoder shared by the API processes of a server.
Each API process otherwise loads its own copy of the audio tokenizer on the
GPU and encodes only the clips it receives. When the processes share a
directory (``VLLM_OMNI_MOSS_REF_CODES_SHARED_DIR``), the first one to take the lock in it
hosts the encoder and serves the others over a Unix socket in that directory.
Requests from all processes are merged into batches, and only the host keeps
the tokenizer and its CUDA graphs on the GPU.
Messages carry raw arrays and a JSON header; nothing is unpickled. A client
that cannot reach the host encodes locally instead.
"""
from __future__ import annotations
import collections
import fcntl
import json
import os
import threading
import time
from collections.abc import Callable
from contextlib import nullcontext
from multiprocessing.connection import Client, Connection, Listener
import numpy as np
import torch
from vllm.logger import init_logger
logger = init_logger(__name__)
_LOCK_NAME = "ref-encoder.lock"
_SOCKET_NAME = "ref-encoder.sock"
# Clips one worker merges into an encode, across all API processes. Several
# workers encode concurrently on their own streams: one serial queue of large
# batches makes a burst of new references wait behind each other. A free
# worker takes what is queued without waiting for more: API processes already
# gather their clips before sending them.
_MAX_BATCH = 8
# A client gives up on the host after this long and encodes locally.
_CONNECT_TIMEOUT_S = 120.0
_REQUEST_TIMEOUT_S = 120.0
# First-time compilation can outlast a normal encode request. Readiness has a
# separate deadline and must finish before the API starts accepting requests.
_STARTUP_TIMEOUT_S = 1200.0
EncodeFn = Callable[[list[torch.Tensor]], list]
# Open for the life of the process: closing it would release the host lock.
_host_lock_handle = None
class SharedReferenceEncoderStartupError(RuntimeError):
"""The shared encoder cannot safely accept requests yet."""
def elect_host(shared_dir: str) -> bool:
"""True in the one process that holds the encoder lock for ``shared_dir``.
The lock is held for the life of the process, so a restarted server
elects a new host even if the old socket file is still present.
"""
global _host_lock_handle
path = os.path.join(shared_dir, _LOCK_NAME)
handle = open(path, "a+") # noqa: SIM115 - kept open to hold the lock
try:
fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
handle.close()
return False
_host_lock_handle = handle
return True
def _send(conn: Connection, header: dict, arrays: list[np.ndarray]) -> None:
conn.send_bytes(json.dumps(header).encode())
for array in arrays:
conn.send_bytes(np.ascontiguousarray(array).tobytes())
def _recv(conn: Connection) -> tuple[dict, list[bytes]]:
header = json.loads(conn.recv_bytes().decode())
return header, [conn.recv_bytes() for _ in range(int(header["count"]))]
class _Job:
__slots__ = ("wav", "done", "result", "queued_at")
def __init__(self, wav: torch.Tensor) -> None:
self.wav = wav
self.done = threading.Event()
self.result: torch.Tensor | BaseException | None = None
self.queued_at = time.monotonic()
class SharedReferenceEncoderHost:
"""Serves reference encodes for every API process sharing ``shared_dir``.
``workers`` pairs each worker's encode function with an optional warmup
(for example, capturing that worker's CUDA graphs).
"""
def __init__(
self,
shared_dir: str,
workers: list[tuple[EncodeFn, Callable[[], object] | None]],
*,
stream_priority: int = 0,
batch_window_ms: float = 0.0,
) -> None:
self._stream_priority = stream_priority
self._batch_window_s = max(0.0, float(batch_window_ms)) / 1000
self._pending: collections.deque[_Job] = collections.deque()
# Includes jobs already taken by a worker, so shutdown can release
# every caller without waiting for an in-flight GPU operation.
self._outstanding: set[_Job] = set()
self._ready = threading.Condition()
self._path = os.path.join(shared_dir, _SOCKET_NAME)
if os.path.exists(self._path):
os.unlink(self._path) # left by a previous host; we hold the lock
self._listener = Listener(self._path, family="AF_UNIX")
os.chmod(self._path, 0o600)
self._closed = False
self._started = threading.Event()
self._startup_error: Exception | None = None
threading.Thread(
target=self._start_workers, args=(workers,), name="moss-ref-encoder-start", daemon=True
).start()
threading.Thread(target=self._accept_loop, name="moss-ref-encoder-accept", daemon=True).start()
logger.info("MOSS shared reference encoder serving at %s with %d workers", self._path, len(workers))
def encode(self, wavs: list[torch.Tensor]) -> list:
"""Encode on behalf of the host process itself."""
self.wait_until_ready()
jobs = [_Job(wav) for wav in wavs]
with self._ready:
if self._closed:
raise RuntimeError("shared reference encoder is closed")
self._outstanding.update(jobs)
self._pending.extend(jobs)
self._ready.notify(len(jobs))
for job in jobs:
job.done.wait()
return [job.result for job in jobs]
def close(self) -> None:
with self._ready:
self._closed = True
self._started.set()
for job in self._outstanding:
job.result = RuntimeError("shared reference encoder closed before encoding completed")
job.done.set()
self._outstanding.clear()
self._pending.clear()
self._ready.notify_all()
try:
self._listener.close()
except OSError:
pass
def wait_until_ready(self) -> None:
if not self._started.wait(_STARTUP_TIMEOUT_S):
raise SharedReferenceEncoderStartupError("shared reference encoder warmup timed out")
if self._closed or self._startup_error is not None:
raise SharedReferenceEncoderStartupError("shared reference encoder startup failed") from self._startup_error
def _accept_loop(self) -> None:
while not self._closed:
try:
conn = self._listener.accept()
except OSError:
return
threading.Thread(target=self._serve, args=(conn,), name="moss-ref-encoder-conn", daemon=True).start()
def _serve(self, conn: Connection) -> None:
with conn:
while True:
try:
header, payloads = _recv(conn)
except (EOFError, OSError):
return
if header.get("op") == "ready":
try:
self.wait_until_ready()
error = None
except SharedReferenceEncoderStartupError as exc:
error = str(exc)
try:
_send(conn, {"count": 0, "ready": error is None, "error": error}, [])
except OSError:
return
continue
wavs = [
torch.from_numpy(np.frombuffer(data, dtype=np.float32).reshape(shape).copy())
for data, shape in zip(payloads, header["shapes"])
]
results = self.encode(wavs)
errors = [None if isinstance(r, torch.Tensor) else repr(r) for r in results]
codes = [r.to(torch.int32).numpy() for r in results if isinstance(r, torch.Tensor)]
reply = {"count": len(codes), "shapes": [list(c.shape) for c in codes], "errors": errors}
try:
_send(conn, reply, codes)
except OSError:
return
def _start_workers(self, workers: list[tuple[EncodeFn, Callable[[], object] | None]]) -> None:
# Every warmup (graph capture) finishes before any worker encodes: CUDA
# work in another thread would invalidate a capture in progress.
try:
for _, warmup in workers:
if warmup is not None:
warmup()
if self._closed:
return
for index, (encode_fn, _) in enumerate(workers):
threading.Thread(
target=self._encode_loop, args=(encode_fn,), name=f"moss-ref-encoder-{index}", daemon=True
).start()
logger.info("MOSS shared reference encoder ready: %d workers", len(workers))
except Exception as exc: # noqa: BLE001 - wake readiness waiters with the failure
self._startup_error = exc
logger.exception("MOSS shared reference encoder startup failed")
finally:
self._started.set()
def _encode_loop(self, encode_fn: EncodeFn) -> None:
stream = torch.cuda.Stream(priority=self._stream_priority) if torch.cuda.is_available() else None
with torch.cuda.stream(stream) if stream is not None else nullcontext():
self._serve_jobs(encode_fn)
def _serve_jobs(self, encode_fn: EncodeFn) -> None:
while True:
with self._ready:
while True:
if self._closed:
return
if not self._pending:
self._ready.wait()
continue
remaining = self._pending[0].queued_at + self._batch_window_s - time.monotonic()
if remaining > 0 and len(self._pending) < _MAX_BATCH:
# Keep clips in the common queue while collecting;
# workers must not each reserve a singleton first.
self._ready.wait(remaining)
continue
break
jobs = [self._pending.popleft() for _ in range(min(_MAX_BATCH, len(self._pending)))]
try:
results = encode_fn([job.wav for job in jobs])
if len(results) != len(jobs):
raise RuntimeError(
f"shared reference encoder returned {len(results)} results for {len(jobs)} clips"
)
except Exception as error: # noqa: BLE001 - every waiter gets the failure
results = [error] * len(jobs)
with self._ready:
for job, result in zip(jobs, results, strict=True):
# close() may already have completed this caller with an
# error. A late GPU result must not replace that outcome.
if not job.done.is_set():
job.result = result
job.done.set()
self._outstanding.discard(job)
class SharedReferenceEncoderClient:
"""Sends reference clips to the host process of ``shared_dir``."""
def __init__(self, shared_dir: str) -> None:
self._path = os.path.join(shared_dir, _SOCKET_NAME)
# One connection per request in flight; idle ones are reused.
self._idle: list[Connection] = []
self._lock = threading.Lock()
self._connected = False
def _connect(self) -> Connection:
# The host may still be starting until the first connection succeeds;
# later reconnects are quick.
deadline = time.monotonic() + (1.0 if self._connected else _CONNECT_TIMEOUT_S)
while True:
try:
conn = Client(self._path, family="AF_UNIX")
self._connected = True
break
except (FileNotFoundError, ConnectionRefusedError):
if time.monotonic() >= deadline:
raise
time.sleep(0.2)
try:
_send(conn, {"op": "ready", "count": 0}, [])
if not conn.poll(_STARTUP_TIMEOUT_S):
raise SharedReferenceEncoderStartupError("shared reference encoder readiness timed out")
header, _ = _recv(conn)
if not header.get("ready"):
raise SharedReferenceEncoderStartupError(header.get("error", "shared reference encoder not ready"))
return conn
except BaseException:
conn.close()
raise
def wait_until_ready(self) -> None:
"""Startup handshake, without encoding or populating the reference cache."""
try:
conn = self._connect()
except Exception as exc:
raise SharedReferenceEncoderStartupError("shared reference encoder is not ready") from exc
with self._lock:
self._idle.append(conn)
def encode(self, wavs: list[torch.Tensor]) -> list:
"""Codes (or an exception) per clip; raises when the host is unreachable."""
with self._lock:
conn = self._idle.pop() if self._idle else None
try:
if conn is None:
conn = self._connect()
arrays = [w.detach().to(torch.float32).cpu().numpy() for w in wavs]
_send(conn, {"count": len(arrays), "shapes": [list(a.shape) for a in arrays]}, arrays)
if not conn.poll(_REQUEST_TIMEOUT_S):
raise TimeoutError(f"no reply from the shared reference encoder in {_REQUEST_TIMEOUT_S}s")
header, payloads = _recv(conn)
except BaseException:
if conn is not None:
conn.close()
raise
with self._lock:
self._idle.append(conn)
codes = iter(
torch.from_numpy(np.frombuffer(data, dtype=np.int32).reshape(shape).copy()).long()
for data, shape in zip(payloads, header["shapes"])
)
return [next(codes) if error is None else RuntimeError(error) for error in header["errors"]]
@@ -0,0 +1,475 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Direct-slot ring attention for MOSS streaming codec.
Inputs q/k/v are (execution_batch, heads, padded_T, head_dim). cache is
contiguous (2, state_capacity, heads, ring_capacity, head_dim). end_offset
contains absolute *pre-call* positions per state slot. slot_ids and valid_lengths
contain one entry per execution row. valid_lengths=0 is a scratch/padding row.
This op writes only valid tokens, attends to the resulting ring with the same
position semantics as RingKVCache.complete, then advances only active offsets.
Slots must be unique and in bounds; 0 <= valid_lengths <= padded_T. Chunks
longer than the ring retain only their final ring_capacity tokens.
These device-value preconditions are caller-owned to avoid host synchronization.
The op deliberately preserves chunk-complete ring overwrite semantics; it does
not recover entries overwritten by later tokens of the same input chunk.
"""
import torch
from vllm.triton_utils import tl, triton
@triton.jit
def _write(
k_ptr,
v_ptr,
cache_ptr,
end_ptr,
slots_ptr,
lengths_ptr,
ks0: tl.constexpr,
ks1: tl.constexpr,
ks2: tl.constexpr,
ks3: tl.constexpr,
vs0: tl.constexpr,
vs1: tl.constexpr,
vs2: tl.constexpr,
vs3: tl.constexpr,
end_stride: tl.constexpr,
slot_stride: tl.constexpr,
length_stride: tl.constexpr,
state_capacity: tl.constexpr,
num_heads: tl.constexpr,
num_frames: tl.constexpr,
ring_capacity: tl.constexpr,
head_dim: tl.constexpr,
block_size: tl.constexpr,
):
row = tl.program_id(0)
slot = tl.load(slots_ptr + row * slot_stride)
length = tl.load(lengths_ptr + row * length_stride)
start = tl.load(end_ptr + slot * end_stride)
x = tl.program_id(1) * block_size + tl.arange(0, block_size)
dim = x % head_dim
token = (x // head_dim) % num_frames
head = x // (num_frames * head_dim)
# For chunks longer than the ring, write only the final C tokens. This
# avoids duplicate physical addresses (and write races) in one launch.
skip = tl.maximum(length - ring_capacity, 0)
source_token = token + skip
mask = (head < num_heads) & (source_token < length)
pos = (start + source_token) % ring_capacity
dst = ((slot * num_heads + head) * ring_capacity + pos) * head_dim + dim
kval = tl.load(k_ptr + row * ks0 + head * ks1 + source_token * ks2 + dim * ks3, mask, 0)
vval = tl.load(v_ptr + row * vs0 + head * vs1 + source_token * vs2 + dim * vs3, mask, 0)
tl.store(cache_ptr + dst, kval, mask)
tl.store(cache_ptr + state_capacity * num_heads * ring_capacity * head_dim + dst, vval, mask)
@triton.jit
def _attend(
q_ptr,
cache_ptr,
end_ptr,
slots_ptr,
lengths_ptr,
out_ptr,
qs0: tl.constexpr,
qs1: tl.constexpr,
qs2: tl.constexpr,
qs3: tl.constexpr,
end_stride: tl.constexpr,
slot_stride: tl.constexpr,
length_stride: tl.constexpr,
state_capacity: tl.constexpr,
num_heads: tl.constexpr,
num_frames: tl.constexpr,
ring_capacity: tl.constexpr,
head_dim: tl.constexpr,
context_size: tl.constexpr,
block_m: tl.constexpr,
block_n: tl.constexpr,
skip_empty_tiles: tl.constexpr = False,
):
rows = tl.program_id(0) * block_m + tl.arange(0, block_m)
bh = tl.program_id(1)
batch, head = bh // num_heads, bh % num_heads
slot = tl.load(slots_ptr + batch * slot_stride)
length = tl.load(lengths_ptr + batch * length_stride)
start = tl.load(end_ptr + slot * end_stride)
end = start + length
dims = tl.arange(0, head_dim)
q = tl.load(
q_ptr + batch * qs0 + head * qs1 + rows[:, None] * qs2 + dims[None, :] * qs3, rows[:, None] < num_frames, 0
)
acc = tl.full((block_m, head_dim), 0.0, tl.float32)
maximum = tl.full((block_m,), -float("inf"), tl.float32)
denominator = tl.full((block_m,), 0.0, tl.float32)
# Padding rows own scratch slots and must emit zeros without reading KV.
# Keep valid-row accumulation and the ring overwrite semantics unchanged.
if skip_empty_tiles:
block_start = tl.program_id(0) * block_m
live = (length > 0) & (block_start < length)
live = live & (block_start + block_m > tl.maximum(length - ring_capacity, 0))
else:
live = length > 0
if live:
# Physical-ring order matches the existing masked_attention accumulation.
last = end - 1
last_index = last % ring_capacity
if skip_empty_tiles:
# Before the first wrap, only physical keys [0, end) exist.
# Omitted blocks have zero probability for every query; keep the
# physical order and arithmetic of all remaining blocks unchanged.
key_blocks = tl.cdiv(tl.minimum(end, ring_capacity), block_n)
else:
key_blocks = tl.cdiv(ring_capacity, block_n)
for first in range(key_blocks):
cols = first * block_n + tl.arange(0, block_n)
delta = cols - last_index
positions = tl.where(delta <= 0, last + delta, last + delta - ring_capacity)
distance = start + rows[:, None] - positions[None, :]
allowed = (
(rows[:, None] < length)
& (cols[None, :] < ring_capacity)
& (positions[None, :] >= 0)
& (positions[None, :] < end)
& (distance >= 0)
)
if context_size > 0:
allowed = allowed & (distance < context_size)
base = (slot * num_heads + head) * ring_capacity * head_dim
k = tl.load(cache_ptr + base + cols[None, :] * head_dim + dims[:, None], cols[None, :] < ring_capacity, 0)
score = tl.dot(q, k).to(tl.float32) * (head_dim**-0.5)
score = tl.where(allowed, score, -float("inf"))
next_max = tl.maximum(maximum, tl.max(score, 1))
safe_max = tl.where(next_max == -float("inf"), 0.0, next_max)
rescale = tl.exp(maximum - safe_max)
p = tl.exp(score - safe_max[:, None])
denominator = denominator * rescale + tl.sum(p, 1)
v = tl.load(
cache_ptr
+ state_capacity * num_heads * ring_capacity * head_dim
+ base
+ cols[:, None] * head_dim
+ dims[None, :],
cols[:, None] < ring_capacity,
0,
)
acc = acc * rescale[:, None] + tl.dot(p.to(v.dtype), v)
maximum = next_max
out = acc / tl.where(denominator > 0, denominator, 1.0)[:, None]
tl.store(
out_ptr + ((batch * num_heads + head) * num_frames + rows[:, None]) * head_dim + dims[None, :],
out,
rows[:, None] < num_frames,
)
@triton.jit
def _advance(
end_ptr,
slots_ptr,
lengths_ptr,
end_stride: tl.constexpr,
slot_stride: tl.constexpr,
length_stride: tl.constexpr,
batch_size: tl.constexpr,
block_size: tl.constexpr,
):
row = tl.arange(0, block_size)
slot = tl.load(slots_ptr + row * slot_stride, row < batch_size, 0)
length = tl.load(lengths_ptr + row * length_stride, row < batch_size, 0)
active = (row < batch_size) & (length > 0)
start = tl.load(end_ptr + slot * end_stride, active, 0)
tl.store(end_ptr + slot * end_stride, start + length, active)
@triton.jit
def _write_attend(
q_ptr,
k_ptr,
v_ptr,
cache_ptr,
starts_ptr,
slots_ptr,
lengths_ptr,
out_ptr,
qs0: tl.constexpr,
qs1: tl.constexpr,
qs2: tl.constexpr,
qs3: tl.constexpr,
ks0: tl.constexpr,
ks1: tl.constexpr,
ks2: tl.constexpr,
ks3: tl.constexpr,
vs0: tl.constexpr,
vs1: tl.constexpr,
vs2: tl.constexpr,
vs3: tl.constexpr,
start_stride: tl.constexpr,
slot_stride: tl.constexpr,
length_stride: tl.constexpr,
state_capacity: tl.constexpr,
num_heads: tl.constexpr,
num_frames: tl.constexpr,
ring_capacity: tl.constexpr,
head_dim: tl.constexpr,
context_size: tl.constexpr,
write_frames: tl.constexpr,
block_m: tl.constexpr,
block_n: tl.constexpr,
block_w: tl.constexpr,
):
rows = tl.program_id(0) * block_m + tl.arange(0, block_m)
bh = tl.program_id(1)
batch, head = bh // num_heads, bh % num_heads
slot = tl.load(slots_ptr + batch * slot_stride)
length = tl.load(lengths_ptr + batch * length_stride)
start = tl.load(starts_ptr + batch * start_stride)
end = start + length
dims = tl.arange(0, head_dim)
base = (slot * num_heads + head) * ring_capacity * head_dim
v_base = state_capacity * num_heads * ring_capacity * head_dim + base
# Every program of this (row, head) writes the whole chunk for its head,
# so it only reads ring entries it wrote itself (duplicate writes across
# query blocks store identical values). Chunks longer than the ring keep
# only their final ring_capacity tokens, as in _write.
skip = tl.maximum(length - ring_capacity, 0)
for first in range(tl.cdiv(write_frames, block_w)):
token = first * block_w + tl.arange(0, block_w)
source = token + skip
mask = (source < length)[:, None]
pos = (start + source) % ring_capacity
kval = tl.load(k_ptr + batch * ks0 + head * ks1 + source[:, None] * ks2 + dims[None, :] * ks3, mask, 0)
vval = tl.load(v_ptr + batch * vs0 + head * vs1 + source[:, None] * vs2 + dims[None, :] * vs3, mask, 0)
dst = pos[:, None] * head_dim + dims[None, :]
tl.store(cache_ptr + base + dst, kval, mask)
tl.store(cache_ptr + v_base + dst, vval, mask)
tl.debug_barrier()
q = tl.load(
q_ptr + batch * qs0 + head * qs1 + rows[:, None] * qs2 + dims[None, :] * qs3, rows[:, None] < num_frames, 0
)
acc = tl.full((block_m, head_dim), 0.0, tl.float32)
maximum = tl.full((block_m,), -float("inf"), tl.float32)
denominator = tl.full((block_m,), 0.0, tl.float32)
if length > 0:
last = end - 1
last_index = last % ring_capacity
for first in range(tl.cdiv(ring_capacity, block_n)):
cols = first * block_n + tl.arange(0, block_n)
delta = cols - last_index
positions = tl.where(delta <= 0, last + delta, last + delta - ring_capacity)
distance = start + rows[:, None] - positions[None, :]
allowed = (
(rows[:, None] < length)
& (cols[None, :] < ring_capacity)
& (positions[None, :] >= 0)
& (positions[None, :] < end)
& (distance >= 0)
)
if context_size > 0:
allowed = allowed & (distance < context_size)
k = tl.load(cache_ptr + base + cols[None, :] * head_dim + dims[:, None], cols[None, :] < ring_capacity, 0)
score = tl.dot(q, k).to(tl.float32) * (head_dim**-0.5)
score = tl.where(allowed, score, -float("inf"))
next_max = tl.maximum(maximum, tl.max(score, 1))
safe_max = tl.where(next_max == -float("inf"), 0.0, next_max)
rescale = tl.exp(maximum - safe_max)
p = tl.exp(score - safe_max[:, None])
denominator = denominator * rescale + tl.sum(p, 1)
v = tl.load(
cache_ptr + v_base + cols[:, None] * head_dim + dims[None, :],
cols[:, None] < ring_capacity,
0,
)
acc = acc * rescale[:, None] + tl.dot(p.to(v.dtype), v)
maximum = next_max
out = acc / tl.where(denominator > 0, denominator, 1.0)[:, None]
tl.store(
out_ptr + ((batch * num_heads + head) * num_frames + rows[:, None]) * head_dim + dims[None, :],
out,
rows[:, None] < num_frames,
)
@torch.library.custom_op("vllm_omni::moss_slot_ring_attention", mutates_args=("cache", "end_offset"))
def slot_ring_attention(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
cache: torch.Tensor,
end_offset: torch.Tensor,
slot_ids: torch.Tensor,
valid_lengths: torch.Tensor,
context: int = -1,
skip_empty_tiles: bool = False,
) -> torch.Tensor:
"""Fused address/mask path, with three ordered launches and no KV gather.
q/k are already rotary-position encoded. The caller still advances its
separate MHA/RoPE offset state; this op advances only end_offset in place.
context=-1 means unbounded causal attention over available ring entries.
Outputs beyond each row's valid_lengths are exactly zero.
skip_empty_tiles skips wholly masked tiles for small (T <= 32) chunks.
Larger chunks keep the static loop, which benchmarks faster after wrap.
"""
b, h, t, d = q.shape
if k.shape != q.shape or v.shape != q.shape:
raise ValueError("q/k/v must have identical shapes")
if q.dtype not in (torch.bfloat16, torch.float16) or k.dtype != q.dtype or v.dtype != q.dtype:
raise ValueError("q/k/v must share bf16 or fp16 dtype")
if d not in (32, 64, 128) or t < 1:
raise ValueError("requires head_dim 32/64/128 and positive padded_T")
if cache.ndim != 5 or cache.shape[0] != 2 or cache.shape[2] != h or cache.shape[4] != d:
raise ValueError("cache must be (2, state_capacity, heads, capacity, head_dim)")
s, c = cache.shape[1], cache.shape[3]
if not cache.is_contiguous() or cache.dtype != q.dtype:
raise ValueError("cache must be contiguous and share q dtype")
if end_offset.shape != (s,) or slot_ids.shape != (b,) or valid_lengths.shape != (b,):
raise ValueError("invalid metadata shapes")
if any(x.dtype not in (torch.int32, torch.int64) for x in (end_offset, slot_ids, valid_lengths)):
raise ValueError("metadata must be int32/int64")
if q.device.type != "cuda" or any(x.device != q.device for x in (k, v, cache, end_offset, slot_ids, valid_lengths)):
raise ValueError("all tensors must share one CUDA device")
if context == 0 or context < -1:
raise ValueError("context must be -1 or positive")
out = torch.empty(q.shape, device=q.device, dtype=q.dtype)
if b == 0:
return out
strides = (end_offset.stride(0), slot_ids.stride(0), valid_lengths.stride(0))
_write[(b, triton.cdiv(h * t * d, 256))](
k, v, cache, end_offset, slot_ids, valid_lengths, *k.stride(), *v.stride(), *strides, s, h, t, c, d, 256
)
bm = 16 if t <= 32 else 64
_attend[(triton.cdiv(t, bm), b * h)](
q,
cache,
end_offset,
slot_ids,
valid_lengths,
out,
*q.stride(),
*strides,
s,
h,
t,
c,
d,
context,
bm,
64,
skip_empty_tiles=skip_empty_tiles and t <= 32,
num_warps=8 if c <= 256 else 4,
num_stages=2,
)
_advance[(1,)](end_offset, slot_ids, valid_lengths, *strides, b, triton.next_power_of_2(b))
return out
@slot_ring_attention.register_fake
def _(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
cache: torch.Tensor,
end_offset: torch.Tensor,
slot_ids: torch.Tensor,
valid_lengths: torch.Tensor,
context: int = -1,
skip_empty_tiles: bool = False,
) -> torch.Tensor:
return torch.empty(q.shape, device=q.device, dtype=q.dtype)
def _check_inputs(q, k, v, cache, metadata, context):
b, h, t, d = q.shape
if k.shape != q.shape or v.shape != q.shape:
raise ValueError("q/k/v must have identical shapes")
if q.dtype not in (torch.bfloat16, torch.float16) or k.dtype != q.dtype or v.dtype != q.dtype:
raise ValueError("q/k/v must share bf16 or fp16 dtype")
if d not in (32, 64, 128) or t < 1:
raise ValueError("requires head_dim 32/64/128 and positive padded_T")
if cache.ndim != 5 or cache.shape[0] != 2 or cache.shape[2] != h or cache.shape[4] != d:
raise ValueError("cache must be (2, state_capacity, heads, capacity, head_dim)")
if not cache.is_contiguous() or cache.dtype != q.dtype:
raise ValueError("cache must be contiguous and share q dtype")
if any(x.dtype not in (torch.int32, torch.int64) for x in metadata):
raise ValueError("metadata must be int32/int64")
if q.device.type != "cuda" or any(x.device != q.device for x in (k, v, cache, *metadata)):
raise ValueError("all tensors must share one CUDA device")
if context == 0 or context < -1:
raise ValueError("context must be -1 or positive")
@torch.library.custom_op("vllm_omni::moss_slot_ring_attention_rows", mutates_args=("cache",))
def slot_ring_attention_rows(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
cache: torch.Tensor,
starts: torch.Tensor,
slot_ids: torch.Tensor,
valid_lengths: torch.Tensor,
context: int = -1,
) -> torch.Tensor:
"""``slot_ring_attention`` as one launch, with per-row start positions.
``starts`` holds each execution row's pre-call absolute position (the
caller's gathered offset). The ring is written and attended exactly as by
``slot_ring_attention``; no offset is advanced here, so the caller owns
every offset update and can fuse them with its own.
"""
b, h, t, d = q.shape
_check_inputs(q, k, v, cache, (starts, slot_ids, valid_lengths), context)
if starts.shape != (b,) or slot_ids.shape != (b,) or valid_lengths.shape != (b,):
raise ValueError("invalid metadata shapes")
out = torch.empty(q.shape, device=q.device, dtype=q.dtype)
if b == 0:
return out
s, c = cache.shape[1], cache.shape[3]
bm = 16 if t <= 32 else 64
_write_attend[(triton.cdiv(t, bm), b * h)](
q,
k,
v,
cache,
starts,
slot_ids,
valid_lengths,
out,
*q.stride(),
*k.stride(),
*v.stride(),
starts.stride(0),
slot_ids.stride(0),
valid_lengths.stride(0),
s,
h,
t,
c,
d,
context,
min(t, c),
bm,
64,
16,
num_warps=8 if c <= 256 else 4,
num_stages=2,
)
return out
@slot_ring_attention_rows.register_fake
def _(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
cache: torch.Tensor,
starts: torch.Tensor,
slot_ids: torch.Tensor,
valid_lengths: torch.Tensor,
context: int = -1,
) -> torch.Tensor:
return torch.empty(q.shape, device=q.device, dtype=q.dtype)
@@ -0,0 +1,99 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""First codec frame -> PCM inside the Qwen3-Omni Talker process.
Code2Wav decodes each streaming chunk statelessly from its codes and left
context. With a one-frame first chunk (``codec_chunk_ramp`` starting at 1),
chunk 0 is a context-free decode of the stream's first frame, which the Talker
can run as soon as eager MTP completes that frame, removing the connector hop
and the Code2Wav step from time to first audio. The orchestrator then trims
Code2Wav's copy of those samples.
The decoder is a second ``Qwen3OmniMoeCode2Wav`` built and loaded exactly like
the Code2Wav stage's, and captured with the same one-frame input shape as that
stage's ``(batch=1, frames=1)`` graph. Optional cuDNN autotuning can select
different algorithms in the two processes and change floating-point rounding.
Each bucket gets its own graph pool because it replays on a side stream while
the Talker's own graphs may still run.
"""
from __future__ import annotations
import os
from collections.abc import Iterable, Sequence
from typing import Any
import torch
import torch.nn as nn
from torch.cuda import CUDAGraph
from vllm.logger import init_logger
from vllm_omni.model_executor.models.common.qwen3_code_predictor import CodePredictorWrapper
from vllm_omni.model_executor.models.common.talker_first_audio import supports_talker_first_audio
from vllm_omni.model_executor.stage_input_processors.chunk_size_utils import parse_chunk_ramp
logger = init_logger(__name__)
def talker_first_audio_enabled(vllm_config: Any) -> bool:
"""Opt in only when chunk 0 carries the first frame in the same step."""
extra = CodePredictorWrapper._stage_connector_extra_config(vllm_config)
ramp = parse_chunk_ramp(extra)
initial_frames = ramp[0] if ramp is not None else int(extra.get("initial_codec_chunk_frames") or 0)
return (
CodePredictorWrapper._parse_bool_config(extra.get("talker_first_audio"))
and initial_frames == 1
and os.environ.get("VLLM_OMNI_TALKER_FIRST_AUDIO", "1") == "1"
and supports_talker_first_audio(vllm_config)
)
class Qwen3OmniFirstFrameDecoder(nn.Module):
def __init__(self, code2wav: nn.Module, sample_rate: int) -> None:
super().__init__()
self.code2wav = code2wav
self.num_quantizers = int(code2wav.config.num_quantizers)
self.sample_rate = int(sample_rate)
self._graphs: dict[int, tuple[CUDAGraph, torch.Tensor, torch.Tensor]] = {}
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
"""Load the checkpoint's ``code2wav.*`` weights as the Code2Wav stage does."""
loaded = self.code2wav.load_weights(weights)
if hasattr(self.code2wav, "precompute_snake_caches"):
self.code2wav.precompute_snake_caches()
return loaded
@torch.inference_mode()
def capture(self, batch_sizes: Sequence[int] = (1, 2, 4, 8)) -> None:
device = next(self.code2wav.parameters()).device
if device.type != "cuda":
return
for batch_size in sorted(set(batch_sizes)):
static_input = torch.zeros(batch_size, self.num_quantizers, 1, dtype=torch.long, device=device)
self.code2wav(static_input)
torch.accelerator.synchronize(device)
graph = CUDAGraph()
with torch.cuda.graph(graph, pool=torch.cuda.graph_pool_handle()):
static_output = self.code2wav(static_input)
self._graphs[batch_size] = (graph, static_input, static_output)
logger.info("Captured Qwen3-Omni Talker first-frame decoder graphs for batch sizes %s", sorted(self._graphs))
@torch.inference_mode()
def decode(self, codes: torch.Tensor) -> torch.Tensor:
"""``codes`` [n, num_quantizers] long on device -> float32 PCM [n, samples] (new tensor)."""
if not self._graphs:
return self.code2wav(codes.to(torch.long).unsqueeze(-1))[:, 0, :].float()
outputs = []
largest = max(self._graphs)
for start in range(0, int(codes.shape[0]), largest):
chunk = codes[start : start + largest]
rows = int(chunk.shape[0])
batch_size = min(size for size in self._graphs if size >= rows)
graph, static_input, static_output = self._graphs[batch_size]
static_input.zero_()
static_input[:rows, :, 0].copy_(chunk)
graph.replay()
# A replay overwrites graph-owned storage; .float() aliases FP32 outputs.
outputs.append(static_output[:rows, 0, :].to(dtype=torch.float32, copy=True))
return torch.cat(outputs, dim=0)
@@ -45,6 +45,8 @@ QWEN3_OMNI_PIPELINE = PipelineConfig(
engine_output_type="latent",
custom_process_next_stage_input_func=(f"{_PROC}.thinker2talker_full_payload"),
async_chunk_process_next_stage_input_func=(f"{_PROC}.thinker2talker_async_chunk"),
# Takes effect only when the deploy selects model_runner v2 for this stage.
supports_native_mrv2_data_plane=True,
sampling_constraints={"detokenize": True},
),
StagePipelineConfig(
@@ -57,6 +59,8 @@ QWEN3_OMNI_PIPELINE = PipelineConfig(
sync_process_input_func=f"{_PROC}.thinker2talker_token_only",
custom_process_next_stage_input_func=(f"{_PROC}.talker2code2wav_full_payload"),
async_chunk_process_next_stage_input_func=(f"{_PROC}.talker2code2wav_async_chunk"),
# Each stage can use MRv2 when its deploy selects model_runner v2.
supports_native_mrv2_data_plane=True,
sampling_constraints={
"detokenize": False,
"stop_token_ids": [2150],
@@ -72,6 +76,7 @@ QWEN3_OMNI_PIPELINE = PipelineConfig(
final_output_type="audio",
hf_config_name="thinker_config",
engine_output_type="audio",
supports_native_mrv2_data_plane=True,
sampling_constraints={"detokenize": True},
requires_full_payload_input=True,
),
@@ -4,7 +4,7 @@
"""Inference-only Qwen3-Omni-Moe unified model (thinker + talker + code2wav)."""
import asyncio
from collections.abc import AsyncGenerator, Iterable
from collections.abc import AsyncGenerator, Iterable, Mapping
from functools import cached_property
from typing import Any
@@ -50,6 +50,7 @@ from vllm_omni.data_entry_keys import Embeddings, HiddenStates, Ids, OmniPayload
from vllm_omni.metrics import definitions as defs
from vllm_omni.model_executor.custom_process_mixin import CustomProcessMixin
from vllm_omni.model_executor.models.output_templates import OmniOutput
from vllm_omni.model_executor.models.qwen3_omni.first_frame_decoder import talker_first_audio_enabled
from vllm_omni.model_executor.models.qwen3_omni.quantization import (
apply_outer_quant_config_mapping,
)
@@ -61,7 +62,9 @@ from vllm_omni.model_executor.models.qwen3_omni.qwen3_omni_moe_thinker import (
Qwen3OmniMoeThinkerProcessingInfo,
)
from vllm_omni.model_executor.models.utils import add_prefix_to_loaded_weights, safe_tensor_reshape
from vllm_omni.model_executor.stage_input_processors.chunk_size_utils import parse_chunk_ramp, ramp_decode_windows
from vllm_omni.platforms import current_omni_platform
from vllm_omni.utils.device_copy import to_device_nonblocking
# Special token IDs for Qwen3 Omni MoE
# Reference: https://huggingface.co/Qwen/Qwen3-Omni-30B-A3B-Instruct/blob/main/tokenizer_config.json
@@ -88,6 +91,13 @@ TALKER_CODEC_THINK_EOS_ID = 4205 # Think mode end
logger = init_logger(__name__)
def _cpu_long(values: Any) -> torch.Tensor:
"""Token ids as a host tensor (payload ids arrive as lists or CPU tensors)."""
if isinstance(values, torch.Tensor):
return values.detach().to("cpu", torch.long)
return torch.as_tensor(values, dtype=torch.long)
@MULTIMODAL_REGISTRY.register_processor(
Qwen3OmniMoeThinkerMultiModalProcessor,
info=Qwen3OmniMoeThinkerProcessingInfo,
@@ -135,6 +145,11 @@ class Qwen3OmniMoeForConditionalGeneration(
realtime_max_tokens = 64
# Set by the MRv2 model state once eager Talker-MTP frames are enabled.
eager_frames_active = False
# Postprocess only stores the last hidden for the deferred decode MTP.
eager_frames_need_postprocess = False
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
self.have_multimodal_outputs = True
@@ -180,9 +195,18 @@ class Qwen3OmniMoeForConditionalGeneration(
# thinker layers and the forward must return what stock vLLM expects.
self.is_staged_run = getattr(vllm_config.model_config, "model_stage", None) is not None
self._returns_tuple = self.model_stage == "thinker" and self.is_staged_run
# The captured layers are a fixed-structure tensor dict on the token
# axis, so MRv2 may replay decode in FULL graphs and return them too.
self.supports_mrv2_full_graph_aux_outputs = self._returns_tuple
if self.model_stage == "thinker":
self.use_async_omni_output = True
# MRv2: the Talker's prompt ends with the first generated token's
# embedding; publishing it with the prefill output lets the first
# Thinker->Talker chunk leave one decode step earlier.
# Speculative sampling can change the token width between steps.
# Keep the existing capture-stream handoff for the entire request.
self.publishes_sampled_embeddings = self.is_staged_run and vllm_config.speculative_config is None
# Initialize thinker model (multimodal processing + text generation)
# Create a new vllm_config with thinker_config as the hf_config
thinker_vllm_config = vllm_config.with_hf_config(
@@ -233,6 +257,19 @@ class Qwen3OmniMoeForConditionalGeneration(
)
self.model = self.talker
self.code2wav = None
# Residual codebooks (MTP) output key, shared by the V1 runner
# (talker_mtp_*) and the MRv2 model state (mtp_*).
self.talker_mtp_output_key = ("codes", "audio")
# MRv2: the step that samples CB0 may complete the frame itself,
# since the residual codebooks depend only on CB0 and that step's
# last hidden, and the next input is their embedding sum plus the
# next text step (see ``talker_mtp``).
self.mtp_eager_frames = True
self._codec_codebook_size = int(talker_config.code_predictor_config.vocab_size)
self._apply_subtalker_sampling_params(getattr(vllm_config.model_config, "subtalker_sampling_params", None))
# Decode each stream's first frame here (see talker_first_audio_enabled).
self.first_frame_decoder = self._build_first_frame_decoder(vllm_config, code2wav_config, prefix)
# for CI: Initialize special tokens embeddings early to avoid AttributeError when loading dummy weights
self._init_special_tokens_embeddings()
@@ -247,6 +284,9 @@ class Qwen3OmniMoeForConditionalGeneration(
# talker MTP codec codes must stay on GPU to avoid a per-step D2H
# sync stall; build_mm_cpu handles the eventual D2H at payload time.
("codes", "audio"),
# The growing thinker-embed cache is read and extended every
# decode step; on CPU it cost a blocking D2H per request per step.
("embed", "cached_decode"),
}
elif self.model_stage == "code2wav":
@@ -285,6 +325,74 @@ class Qwen3OmniMoeForConditionalGeneration(
["hidden_states", "residual", *incoming_captures], thinker_config.text_config.hidden_size
)
@staticmethod
def _build_first_frame_decoder(
vllm_config: VllmConfig,
code2wav_config: Qwen3OmniMoeCode2WavConfig,
prefix: str,
) -> nn.Module | None:
"""A Code2Wav copy for the Talker's first-frame audio, or None when disabled.
Only a streaming (async-chunk) Talker hands frames to Code2Wav chunk by
chunk, so only there is a stream's first chunk a context-free decode.
The copy is built like the Code2Wav stage's module (same class, config,
dtype and device context). Optional cuDNN autotuning is also shared,
although per-process algorithm selection can change PCM rounding.
"""
if not talker_first_audio_enabled(vllm_config):
return None
from vllm_omni.model_executor.models.qwen3_omni.first_frame_decoder import Qwen3OmniFirstFrameDecoder
code2wav = init_vllm_registered_model(
vllm_config=vllm_config.with_hf_config(code2wav_config, architectures=["Qwen3OmniMoeCode2Wav"]),
prefix=maybe_prefix(prefix, "first_frame_decoder.code2wav"),
hf_config=code2wav_config,
architectures=["Qwen3OmniMoeCode2Wav"],
)
logger.info("Qwen3-Omni Talker decodes first-frame audio in-stage")
return Qwen3OmniFirstFrameDecoder(code2wav, defs.resolve_audio_sample_rate(code2wav_config))
def capture_first_frame_graphs(self) -> None:
"""Called by the MRv2 runner after its own graph capture."""
decoder = getattr(self, "first_frame_decoder", None)
if decoder is not None:
decoder.capture()
def _apply_subtalker_sampling_params(self, params: Any) -> None:
"""Apply the deploy's ``subtalker_sampling_params`` to the residual-codebook predictor.
The predictor samples in its "stored" mode (top-k 50, top-p 0.8 by
default) and ignores per-call sampling arguments; ``do_sample: false``
makes it greedy.
"""
if not isinstance(params, Mapping) or not params:
return
predictor = self.talker.code_predictor
predictor.set_sampling_params(
top_k=int(params.get("top_k", predictor._top_k)),
top_p=float(params.get("top_p", predictor._top_p)),
do_sample=bool(params.get("do_sample", True)),
)
# MRv2 capability names read the V1 canonical values, so a platform patch
# of ``talker_mtp_graph_safe`` applies to both runners.
@property
def mtp(self):
"""MRv2 name for ``talker_mtp``; only the Talker stage has an MTP."""
return self.talker_mtp if self.model_stage == "talker" else None
@property
def mtp_output_key(self) -> tuple[str, str]:
return getattr(self, "talker_mtp_output_key", ("codes", "audio"))
@property
def mtp_graph_safe(self) -> bool:
return bool(getattr(self, "talker_mtp_graph_safe", False))
def mtp_frame_valid(self, layer0: torch.Tensor) -> torch.Tensor:
"""Rows whose CB0 is a codec id rather than codec EOS (or another special id)."""
return (layer0 >= 0) & (layer0 < self._codec_codebook_size)
def _thinker_capture_layer_indices(self) -> list[int]:
accept_layer = getattr(self.talker_config, "accept_hidden_layer", None)
return [0, int(accept_layer)] if self.is_staged_run and accept_layer is not None else []
@@ -496,38 +604,47 @@ class Qwen3OmniMoeForConditionalGeneration(
# ========== Stage 3: Code2Wav ==========
elif self.model_stage == "code2wav":
seq_token_counts: list[int] | None = kwargs.get("seq_token_counts")
num_quantizers = int(self.code2wav.config.num_quantizers)
# Extract codec codes from input
if input_ids.shape[0] % 16 == 0:
if input_ids.shape[0] % num_quantizers == 0:
if seq_token_counts is not None:
max_seq_len = max(seq_token_counts) // 16
max_seq_len = max(seq_token_counts) // num_quantizers
batch_size = len(seq_token_counts)
split_codes = torch.split(input_ids, seq_token_counts, dim=0)
codes = torch.zeros((batch_size, 16, max_seq_len), device=input_ids.device, dtype=input_ids.dtype)
codes = torch.zeros(
(batch_size, num_quantizers, max_seq_len), device=input_ids.device, dtype=input_ids.dtype
)
for idx, code in enumerate(split_codes):
seq_len = code.shape[0] // 16
codes[idx, :, :seq_len] = code.reshape(16, seq_len)
seq_len = code.shape[0] // num_quantizers
codes[idx, :, :seq_len] = code.reshape(num_quantizers, seq_len)
else:
codes = input_ids.reshape(1, 16, -1)
codes = input_ids.reshape(1, num_quantizers, -1)
else:
if seq_token_counts is None:
logger.debug(
"Code2Wav warmup input length %s is not divisible by 16; padding with zeros.",
"Code2Wav warmup input length %s is not divisible by %s; padding with zeros.",
input_ids.shape[0],
num_quantizers,
)
else:
logger.warning_once(
"Code2Wav input length is not divisible by 16; padding with zeros. "
"This is expected only during cudagraph warmup."
"Code2Wav input length is not divisible by %s; padding with zeros. "
"This is expected only during cudagraph warmup.",
num_quantizers,
)
input_ids_flatten = input_ids.reshape(-1)
input_ids_flatten = torch.cat(
[
input_ids_flatten,
torch.zeros(16 - input_ids.shape[0] % 16, dtype=torch.long, device=input_ids.device),
torch.zeros(
num_quantizers - input_ids.shape[0] % num_quantizers,
dtype=torch.long,
device=input_ids.device,
),
]
)
codes = input_ids_flatten.reshape(1, 16, -1)
codes = input_ids_flatten.reshape(1, num_quantizers, -1)
# Generate audio from codec codes
# Get every request's left_context_size from runtime_additional_information (passed via kwargs)
@@ -541,6 +658,38 @@ class Qwen3OmniMoeForConditionalGeneration(
logger.debug("No additional_information provided to code2wav stage.")
audio_tensors = self.generate_audio(codes, left_context_size, seq_token_counts)
# Keep the skip decision attached to this forward's output, not
# mutable model state: asynchronous materialization can overlap steps.
from vllm_omni.data_entry_keys import FIRST_AUDIO_REQUIRED_KEY
flags = [
bool(info.get("meta", {}).get("first_audio", False)) for info in (runtime_additional_information or [])
]
if flags and any(flags):
if len(flags) != len(audio_tensors):
raise ValueError("First-audio flags must align with Code2Wav requests")
sample_rate = defs.resolve_audio_sample_rate(self.code2wav_config)
frame_counts = (
[count // num_quantizers for count in seq_token_counts] if seq_token_counts else [codes.shape[-1]]
)
# The causal decoder withholds a right-edge tail. The first
# frame has fewer samples than total_upsample; trim its actual
# prefix length, including when chunk 0 contains several frames.
prefix_lengths = [
max(0, audio.numel() - (frames - 1) * int(self.code2wav.total_upsample)) if skip else 0
for audio, frames, skip in zip(audio_tensors, frame_counts, flags, strict=True)
]
return OmniOutput(
text_hidden_states=None,
multimodal_outputs={
"model_outputs": [
audio.reshape(1, -1)[..., prefix:]
for audio, prefix in zip(audio_tensors, prefix_lengths, strict=True)
],
"sr": [torch.tensor(sample_rate, dtype=torch.int32) for _ in audio_tensors],
FIRST_AUDIO_REQUIRED_KEY: [torch.tensor(skip) for skip in flags],
},
)
return audio_tensors
# Fallback (shouldn't reach here)
@@ -605,7 +754,13 @@ class Qwen3OmniMoeForConditionalGeneration(
if "runtime_additional_information" in kwargs and "model_intermediate_buffer" not in kwargs:
logger.warning_once("runtime_additional_information is deprecated, use model_intermediate_buffer")
# MRv2 passes each request's token span.
spans = kwargs.get("request_token_spans")
if spans is not None and self.eager_frames_active and self.vllm_config.model_config.async_chunk:
return self._make_eager_talker_output(talker_hidden, spans)
code_predictor_codes = [info.get("codes", {}).get("audio") for info in info_dicts]
if spans is not None:
code_predictor_codes = self._align_codes_to_spans(code_predictor_codes, spans, talker_hidden.device)
audio_codes = torch.cat(code_predictor_codes, dim=0)
multimodal_outputs: OmniPayload = {"codes": {"audio": audio_codes}}
span_len = audio_codes.shape[0]
@@ -627,6 +782,44 @@ class Qwen3OmniMoeForConditionalGeneration(
return model_outputs
def _align_codes_to_spans(
self,
codes: list[Any],
spans: list[tuple[int, int]],
device: torch.device,
) -> list[torch.Tensor]:
"""One ``codes.audio`` block per request with exactly its span's rows.
MRv2 also runs requests that never went through preprocess (vLLM's
``_warmup_`` requests), whose buffers carry no codes; they get zero
rows, which the downstream processor skips like prefill rows.
"""
q = int(self.talker.num_code_groups)
aligned: list[torch.Tensor] = []
for block, (start, end) in zip(codes, spans, strict=True):
if isinstance(block, torch.Tensor) and block.ndim == 2 and block.shape[0] == end - start:
aligned.append(block)
else:
aligned.append(torch.zeros((end - start, q), dtype=torch.long, device=device))
return aligned
def _make_eager_talker_output(self, hidden: torch.Tensor, spans: list[tuple[int, int]]) -> OmniOutput:
"""Async-chunk output when the MRv2 runner completes frames after sampling.
``run_eager_mtp`` writes each sampled row's frame and validity into the
last token row of its request span; every other row carries no frame.
"""
num_tokens = spans[-1][1] if spans else 0
q = int(self.talker.num_code_groups)
mm: OmniPayload = {
"codes": {"audio": torch.zeros((num_tokens, q), dtype=torch.long, device=hidden.device)},
"meta": {
"codec_frame_valid": torch.zeros((num_tokens,), dtype=torch.int8, device=hidden.device),
"first_audio": torch.zeros((num_tokens,), dtype=torch.int8, device=hidden.device),
},
}
return OmniOutput(text_hidden_states=hidden, multimodal_outputs=mm)
# ==================== Audio Generation ====================
def generate_audio(
@@ -849,7 +1042,7 @@ class Qwen3OmniMoeForConditionalGeneration(
def _proj_from_thinker(x_opt: torch.Tensor | None) -> torch.Tensor:
if isinstance(x_opt, torch.Tensor) and x_opt.numel() > 0:
xin = _ensure_1x1(x_opt).to(module_device)
xin = to_device_nonblocking(_ensure_1x1(x_opt), module_device)
else:
xin = torch.zeros(
(1, thinker_embed.shape[-1]),
@@ -882,27 +1075,26 @@ class Qwen3OmniMoeForConditionalGeneration(
voice_type = str(voice_type).lower().strip()
start_index = meta.get("num_processed_tokens", 0)
end_index = start_index + input_embeds.shape[0]
# Read thinker outputs for prefill
thinker_sequence_embeds = embed["prefill"].to(
device=self._module_device(self.talker), dtype=torch.bfloat16
# Read thinker outputs for prefill. Every copy here is H2D from the
# connector payload; pageable copies would stall the whole Talker batch.
talker_device = self._module_device(self.talker)
thinker_sequence_embeds = to_device_nonblocking(embed["prefill"], talker_device).to(
torch.bfloat16
) # Tensor [P,H]
thinker_hidden_states = hs["output"].to(
device=self._module_device(self.talker), dtype=torch.bfloat16
) # Tensor [K,H]
thinker_hidden_states = to_device_nonblocking(hs["output"], talker_device).to(torch.bfloat16) # Tensor [K,H]
# Token ids stay on CPU too: prefill parsing reads them on the host.
thinker_sequences_cpu = None if ids.get("all") is None else _cpu_long(ids["all"])
thinker_chatml_ids_cpu = None if ids.get("prompt") is None else _cpu_long(ids["prompt"])
thinker_sequences = (
ids.get("all")
if ids.get("all") is None
else torch.as_tensor(ids["all"], device=self._module_device(self.talker))
None if thinker_sequences_cpu is None else to_device_nonblocking(thinker_sequences_cpu, talker_device)
)
thinker_chatml_ids = (
ids.get("prompt")
if ids.get("prompt") is None
else torch.as_tensor(ids["prompt"], device=self._module_device(self.talker))
None if thinker_chatml_ids_cpu is None else to_device_nonblocking(thinker_chatml_ids_cpu, talker_device)
)
tts_bos_thinker = embed["tts_bos"].to(device=self._module_device(self.talker), dtype=torch.bfloat16)
tts_eos_thinker = embed["tts_eos"].to(device=self._module_device(self.talker), dtype=torch.bfloat16)
tts_pad_thinker = embed["tts_pad"].to(device=self._module_device(self.talker), dtype=torch.bfloat16)
tts_bos_thinker = to_device_nonblocking(embed["tts_bos"], talker_device).to(torch.bfloat16)
tts_eos_thinker = to_device_nonblocking(embed["tts_eos"], talker_device).to(torch.bfloat16)
tts_pad_thinker = to_device_nonblocking(embed["tts_pad"], talker_device).to(torch.bfloat16)
if thinker_sequence_embeds is None or thinker_hidden_states is None:
raise ValueError(
@@ -924,6 +1116,8 @@ class Qwen3OmniMoeForConditionalGeneration(
)
if ids_chatml.ndim == 1:
ids_chatml = ids_chatml.unsqueeze(0)
if thinker_chatml_ids_cpu is not None and thinker_chatml_ids_cpu.ndim == 1:
thinker_chatml_ids_cpu = thinker_chatml_ids_cpu.unsqueeze(0)
else:
# Fallback: create dummy ids if not provided
ids_chatml = torch.zeros(
@@ -932,6 +1126,7 @@ class Qwen3OmniMoeForConditionalGeneration(
device=self._module_device(self.talker),
)
thinker_sequences = ids_chatml
thinker_chatml_ids_cpu = thinker_sequences_cpu = None
speaker_id = self._get_text_spk_token_id(voice_type)
req_input_ids, req_embeds, trailing_text_hidden = self._thinker_to_talker_prefill(
@@ -944,6 +1139,8 @@ class Qwen3OmniMoeForConditionalGeneration(
tts_bos_thinker=tts_bos_thinker,
tts_eos_thinker=tts_eos_thinker,
tts_pad_thinker=tts_pad_thinker,
input_ids_cpu=thinker_chatml_ids_cpu,
thinker_result_ids_cpu=thinker_sequences_cpu,
)
# Queue trailing_text_hidden for decode (drop first for next steps),
@@ -993,12 +1190,11 @@ class Qwen3OmniMoeForConditionalGeneration(
if cached_thinker_decode_embeds is None:
update_dict.setdefault("embed", {})["cached_decode"] = thinker_decode_embeds
else:
cached_thinker_decode_embeds = cached_thinker_decode_embeds.to(
device=self._module_device(self.talker), dtype=torch.bfloat16
)
thinker_decode_embeds = thinker_decode_embeds.to(
device=self._module_device(self.talker), dtype=torch.bfloat16
talker_device = self._module_device(self.talker)
cached_thinker_decode_embeds = to_device_nonblocking(cached_thinker_decode_embeds, talker_device).to(
torch.bfloat16
)
thinker_decode_embeds = to_device_nonblocking(thinker_decode_embeds, talker_device).to(torch.bfloat16)
update_dict.setdefault("embed", {})["cached_decode"] = torch.cat(
[cached_thinker_decode_embeds, thinker_decode_embeds], dim=0
)
@@ -1015,26 +1211,31 @@ class Qwen3OmniMoeForConditionalGeneration(
tts_bos_thinker: torch.Tensor | None = None,
tts_eos_thinker: torch.Tensor | None = None,
tts_pad_thinker: torch.Tensor | None = None,
input_ids_cpu: torch.Tensor | None = None,
thinker_result_ids_cpu: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]:
"""
Project thinker outputs to talker inputs during prefill stage.
``input_ids_cpu`` / ``thinker_result_ids_cpu`` (host copies of the same
ids) move segment parsing off the GPU: each ``.item()`` / ``nonzero`` /
mask test on device ids is a host sync that stalls the Talker batch.
Returns:
(input_ids, input_embeds) for talker
"""
target_len = thinker_result_ids.shape[-1]
im_start_indexes = torch.cat(
(
torch.nonzero(input_ids[0] == self.config.im_start_token_id).squeeze(-1),
torch.tensor([target_len], device=input_ids.device, dtype=input_ids.dtype),
),
dim=-1,
) # Shape [n_starts + 1]; Take batch 0 since batched inference is not supported here.
# Take batch 0 since batched inference is not supported here.
parse_ids = input_ids_cpu if input_ids_cpu is not None else input_ids
result_ids = thinker_result_ids_cpu if thinker_result_ids_cpu is not None else thinker_result_ids
im_start_indexes = torch.nonzero(parse_ids[0] == self.config.im_start_token_id).flatten().tolist()
im_start_indexes.append(target_len)
multimodal_mask = (
(thinker_result_ids == self.thinker_config.audio_token_id) |
(thinker_result_ids == self.thinker_config.image_token_id) |
(thinker_result_ids == self.thinker_config.video_token_id)
).to(input_ids.device) # [t] # fmt: skip
(result_ids == self.thinker_config.audio_token_id) |
(result_ids == self.thinker_config.image_token_id) |
(result_ids == self.thinker_config.video_token_id)
) # [t] # fmt: skip
role_tokens = parse_ids[0].tolist()
tts_bos_embed, tts_eos_embed, tts_pad_embed = self._get_tts_embed(
thinker_embed, tts_bos_thinker, tts_eos_thinker, tts_pad_thinker
@@ -1045,21 +1246,21 @@ class Qwen3OmniMoeForConditionalGeneration(
trailing_text_hidden_all: torch.Tensor | None = None
# For every chatml parts
for i in range(len(im_start_indexes) - 1):
im_start_index = im_start_indexes[i].item()
segment_end_index = im_start_indexes[i + 1].item()
role_token = input_ids[0][im_start_index + 1]
im_start_index = im_start_indexes[i]
segment_end_index = im_start_indexes[i + 1]
role_token = role_tokens[im_start_index + 1]
# Talker should ignore thinker system prompt
if (role_token == self.config.system_token_id).item():
if role_token == self.config.system_token_id:
continue
# Talker takes word embeddings for tokens and hidden state from `accept_hidden_layer` for multimodal inputs
elif (role_token == self.config.user_token_id).item():
elif role_token == self.config.user_token_id:
talker_user_part = self._get_talker_user_parts(
im_start_index, segment_end_index, multimodal_mask, thinker_hidden, thinker_embed
)
talker_input_embeds.append(talker_user_part)
talker_input_ids.append(thinker_result_ids[im_start_index:segment_end_index])
# Take assistant output (for now)
elif (role_token == self.config.assistant_token_id).item() and i == len(im_start_indexes) - 2:
elif role_token == self.config.assistant_token_id and i == len(im_start_indexes) - 2:
talker_assistant_embeds, talker_assistant_ids, trailing_text_hidden = self._get_talker_assistant_parts(
im_start_index,
segment_end_index,
@@ -1078,7 +1279,7 @@ class Qwen3OmniMoeForConditionalGeneration(
except Exception:
pass
# History assistant output (ignore for now)
elif (role_token == self.config.assistant_token_id).item() and i != len(im_start_indexes) - 2:
elif role_token == self.config.assistant_token_id and i != len(im_start_indexes) - 2:
continue
else:
raise AssertionError("Expect role id after <|im_start|> (assistant, user, system)")
@@ -1103,20 +1304,22 @@ class Qwen3OmniMoeForConditionalGeneration(
cached_thinker_decode_embeds = embed.get("cached_decode", None)
thinker_decode_embed = embed.get("decode", None)
if getattr(self.vllm_config.model_config, "use_v2_model_runner", False) and not meta.get("resumable", False):
return self._next_thinker_decode_text_step(
cached_thinker_decode_embeds, thinker_decode_embed, meta, device, update_dict
)
start_index = meta.get("num_processed_tokens", 0)
if cached_thinker_decode_embeds is not None and start_index < cached_thinker_decode_embeds.shape[0]:
cached_thinker_decode_embeds = cached_thinker_decode_embeds.to(device)
cached_thinker_decode_embeds = to_device_nonblocking(cached_thinker_decode_embeds, device)
thinker_embed = cached_thinker_decode_embeds[start_index]
if thinker_decode_embed is not None:
thinker_decode_embed = thinker_decode_embed.to(device)
thinker_decode_embed = to_device_nonblocking(thinker_decode_embed, device)
cached_thinker_decode_embeds = torch.cat([cached_thinker_decode_embeds, thinker_decode_embed], dim=0)
update_dict.setdefault("embed", {})["cached_decode"] = cached_thinker_decode_embeds
elif thinker_decode_embed is not None:
thinker_embed = thinker_decode_embed
if thinker_embed.device != device:
thinker_embed = thinker_embed.to(device)
thinker_embed = to_device_nonblocking(thinker_decode_embed, device)
else:
# When the tokens output by the thinker are exhausted, an EOS token needs to be appended.
@@ -1129,6 +1332,41 @@ class Qwen3OmniMoeForConditionalGeneration(
update_dict.setdefault("embed", {})["decode"] = None
return self.talker.text_projection(thinker_embed).to(device)
def _next_thinker_decode_text_step(
self,
pending: torch.Tensor | None,
incoming: torch.Tensor | None,
meta: OmniPayloadMeta,
device: torch.device,
update_dict: OmniPayload,
) -> torch.Tensor:
"""Take the oldest unconsumed Thinker decode row as this step's text.
Rows arrive in order but not one per Talker step: the MRv2 receiver
hands over every row that arrived since the previous step (the V1
adapter delivers one chunk per step). New rows are appended to the
pending queue ``embed.cached_decode`` and each step consumes one. Once
the queue is empty the Thinker's text is exhausted: EOS once, then pad.
"""
embed_update = update_dict.setdefault("embed", {})
embed_update["decode"] = None
if isinstance(incoming, torch.Tensor) and incoming.numel() > 0:
incoming = to_device_nonblocking(incoming, device).reshape(-1, incoming.shape[-1])
if isinstance(pending, torch.Tensor) and pending.shape[0] > 0:
pending = to_device_nonblocking(pending, device)
pending = torch.cat([pending, incoming.to(pending.dtype)], dim=0)
else:
pending = incoming
if not isinstance(pending, torch.Tensor) or pending.shape[0] == 0:
embed_update["cached_decode"] = None
if meta.get("eos_emitted", False):
return self.tts_pad_embed.to(device)
update_dict.setdefault("meta", {})["eos_emitted"] = True
return self.tts_eos_embed.to(device)
pending = to_device_nonblocking(pending, device)
embed_update["cached_decode"] = pending[1:] if pending.shape[0] > 1 else None
return self.talker.text_projection(pending[:1]).to(device)
def talker_preprocess_decode(
self, input_ids: torch.Tensor, input_embeds: torch.Tensor, update_dict: OmniPayload, payload: OmniPayload
):
@@ -1203,6 +1441,21 @@ class Qwen3OmniMoeForConditionalGeneration(
)
user_mm_mask = multimodal_mask[im_start_index:segment_end_index]
if user_mm_mask.device.type == "cpu":
# Host mask: no device sync for the test, and a text-only segment
# (the common TTS case) needs no masked gather at all.
if not bool(user_mm_mask.any()):
user_thinker_embed = thinker_embed[im_start_index:segment_end_index]
return self.talker.text_projection(user_thinker_embed).to(thinker_hidden.device, torch.bfloat16)
mm_index = to_device_nonblocking(torch.nonzero(user_mm_mask).flatten(), thinker_hidden.device)
text_index = to_device_nonblocking(torch.nonzero(~user_mm_mask).flatten(), thinker_hidden.device)
segment_hidden = thinker_hidden[im_start_index:segment_end_index]
segment_embed = thinker_embed[im_start_index:segment_end_index]
mm_hidden = self.talker.hidden_projection(segment_hidden.index_select(0, mm_index))
user_talker_part.index_copy_(0, mm_index, mm_hidden.to(user_talker_part.dtype))
text_hidden = self.talker.text_projection(segment_embed.index_select(0, text_index))
user_talker_part.index_copy_(0, text_index, text_hidden.to(user_talker_part.dtype))
return user_talker_part
# Multimodal data exists
if user_mm_mask.any():
user_thinker_hidden_mm = thinker_hidden[im_start_index:segment_end_index][user_mm_mask]
@@ -1236,17 +1489,19 @@ class Qwen3OmniMoeForConditionalGeneration(
),
dim=0,
)
codec_special_tokens = torch.tensor(
[
self.config.talker_config.codec_nothink_id,
self.config.talker_config.codec_think_bos_id,
self.config.talker_config.codec_think_eos_id,
speaker_id,
self.config.talker_config.codec_pad_id,
self.config.talker_config.codec_bos_id,
],
device=tts_pad_embed.device,
dtype=torch.long,
codec_special_tokens = to_device_nonblocking(
torch.tensor(
[
self.config.talker_config.codec_nothink_id,
self.config.talker_config.codec_think_bos_id,
self.config.talker_config.codec_think_eos_id,
speaker_id,
self.config.talker_config.codec_pad_id,
self.config.talker_config.codec_bos_id,
],
dtype=torch.long,
),
tts_pad_embed.device,
)
embed_input_ids = self.talker.embed_input_ids(codec_special_tokens).to(
device=tts_pad_embed.device, dtype=torch.bfloat16
@@ -1381,18 +1636,31 @@ class Qwen3OmniMoeForConditionalGeneration(
# ==================== Weight Loading ====================
def _get_codec_frame_config(self) -> tuple[int, int]:
"""Extract codec_chunk_frames and codec_left_context_frames from stage connector config."""
def _get_codec_connector_extra(self) -> dict[str, Any]:
model_cfg = getattr(self.vllm_config, "model_config", None)
connector_cfg = getattr(model_cfg, "stage_connector_config", None)
if isinstance(connector_cfg, dict):
extra = connector_cfg.get("extra", {})
else:
extra = getattr(connector_cfg, "extra", None) or {}
return extra if isinstance(extra, Mapping) else {}
def _get_codec_frame_config(self) -> tuple[int, int]:
"""Extract codec_chunk_frames and codec_left_context_frames from stage connector config."""
extra = self._get_codec_connector_extra()
chunk_frames = int(extra.get("codec_chunk_frames", 0) or 0)
left_frames = int(extra.get("codec_left_context_frames", 0) or 0)
return chunk_frames, left_frames
def _get_codec_ramp_windows(self) -> list[int]:
"""Decode windows of the connector's ``codec_chunk_ramp`` (empty without a ramp)."""
extra = self._get_codec_connector_extra()
chunk_frames = int(extra.get("codec_chunk_frames", 25) or 25)
ramp = parse_chunk_ramp(dict(extra), steady=chunk_frames)
if not ramp:
return []
return ramp_decode_windows(ramp, int(extra.get("codec_left_context_frames", 25) or 0))
def _maybe_enable_code2wav_cudagraph(self) -> None:
"""Enable the inner Code2Wav CUDA graph unless this stage runs in eager mode."""
if not self.code2wav or not hasattr(self.code2wav, "enable_cudagraph"):
@@ -1404,9 +1672,15 @@ class Qwen3OmniMoeForConditionalGeneration(
return
chunk_frames, left_frames = self._get_codec_frame_config()
streaming = bool(self.vllm_config.model_config.async_chunk)
# Opt-in multi-row graphs for streaming windows (connector extra
# ``codec_graph_batch_sizes``, e.g. [2, 3, 4, 6, 8, 12, 16]).
batch_sizes = self._get_codec_connector_extra().get("codec_graph_batch_sizes") or ()
self.code2wav.enable_cudagraph(
codec_chunk_frames=chunk_frames,
codec_left_context_frames=left_frames,
extra_capture_sizes=self._get_codec_ramp_windows() if streaming else (),
streaming_batch_sizes=[int(b) for b in batch_sizes] if streaming else (),
)
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
@@ -1439,6 +1713,12 @@ class Qwen3OmniMoeForConditionalGeneration(
loaded_weights.update(talker_loaded)
loaded_weights.update(self._init_special_tokens_embeddings())
# The Talker's first-frame decoder is a Code2Wav copy (same code2wav.* weights).
first_frame_decoder = getattr(self, "first_frame_decoder", None)
if first_frame_decoder is not None and code2wav_weights:
decoder_loaded = first_frame_decoder.load_weights(code2wav_weights)
loaded_weights.update(add_prefix_to_loaded_weights(decoder_loaded, "first_frame_decoder.code2wav"))
# Load code2wav weights
if self.code2wav and code2wav_weights:
code2wav_loaded = self.code2wav.load_weights(code2wav_weights)
@@ -5,7 +5,8 @@
from __future__ import annotations
from collections.abc import Iterable
from collections.abc import Callable, Iterable, Sequence
from contextlib import nullcontext
import numpy as np
import torch
@@ -19,6 +20,7 @@ from transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe import (
Qwen3OmniMoeCode2WavDecoderBlock,
Qwen3OmniMoeCode2WavTransformerModel,
Qwen3OmniMoeConvNeXtBlock,
Qwen3OmniMoeSnakeBeta,
)
from vllm.config import VllmConfig # type: ignore
from vllm.logger import init_logger # type: ignore
@@ -31,9 +33,93 @@ from vllm_omni.model_executor.models.common.snake_activation import SnakeBeta
from vllm_omni.model_executor.models.qwen3_omni.quantization import (
Qwen3OmniNestedSupportsQuant,
)
from vllm_omni.platforms import current_omni_platform
logger = init_logger(__name__)
# Fixed cost of one streaming decode call (graph replay or eager launch), in
# decoded-frame equivalents: a call on a single frame costs about as much GPU
# time as decoding this many extra frames inside a larger call.
_DECODE_CALL_COST_FRAMES = 32
def use_fused_snake(module: nn.Module) -> int:
"""Replace HF ``Qwen3OmniMoeSnakeBeta`` activations under ``module`` with the fused one.
The HF decoder blocks recompute ``exp(alpha)`` / ``exp(beta)`` and launch
about seven broadcast kernels per call; the shared ``SnakeBeta`` computes the
same ``x + 1/b * sin^2(a*x)`` in one kernel from precomputed caches.
Parameter names and shapes match, so this runs before weight loading.
"""
count = 0
for parent in list(module.modules()):
for name, child in list(parent.named_children()):
if isinstance(child, Qwen3OmniMoeSnakeBeta):
setattr(parent, name, SnakeBeta(child.in_features))
count += 1
return count
def plan_decode_groups(
lengths: Sequence[int],
bucket_of: Callable[[int], int | None],
batch_sizes_for: Callable[[int], Sequence[int]],
call_cost_frames: int = _DECODE_CALL_COST_FRAMES,
) -> list[tuple[list[int], int]]:
"""Split a streaming batch into decode calls of rows with similar lengths.
Decoding every row at the batch's longest window wastes most of the work
when short ramp chunks (1-15 frames) share a step with steady 50-frame
windows. Rows are sorted by length and cut into contiguous groups of
graph buckets (``bucket_of``) so that the modelled cost, per call
``call_cost_frames`` plus padded rows (to the next captured row count of
that bucket, ``batch_sizes_for``) times the bucket length, is smallest.
Returns ``(row indices, decode length)`` per call; the decode length is
the group's longest row.
"""
count = len(lengths)
if count == 0:
return []
order = sorted(range(count), key=lambda row: lengths[row])
buckets = [bucket_of(int(lengths[row])) or int(lengths[row]) for row in order]
distinct = sorted(set(buckets))
members = [[row for row, bucket in zip(order, buckets) if bucket == size] for size in distinct]
def sizes_for(size: int) -> list[int]:
return sorted({int(b) for b in batch_sizes_for(size) if int(b) > 0} | {1})
def cost(rows: int, size: int) -> int:
batch_sizes = sizes_for(size)
calls, rest = divmod(rows, batch_sizes[-1])
padded = calls * batch_sizes[-1]
if rest:
calls += 1
padded += next(b for b in batch_sizes if b >= rest)
return calls * call_cost_frames + padded * size
best = [0] + [None] * len(distinct)
cut = [0] * (len(distinct) + 1)
for end in range(1, len(distinct) + 1):
rows = 0
for start in range(end - 1, -1, -1):
rows += len(members[start])
total = best[start] + cost(rows, distinct[end - 1])
if best[end] is None or total < best[end]:
best[end], cut[end] = total, start
groups: list[tuple[list[int], int]] = []
end = len(distinct)
while end > 0:
start = cut[end]
groups.append(([row for bucket_rows in members[start:end] for row in bucket_rows], distinct[end - 1]))
end = start
calls: list[tuple[list[int], int]] = []
for rows, size in reversed(groups):
max_batch = sizes_for(size)[-1]
for offset in range(0, len(rows), max_batch):
part = rows[offset : offset + max_batch]
calls.append((part, max(int(lengths[row]) for row in part)))
return calls
class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
"""
@@ -74,6 +160,14 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
super().__init__()
self.config: Qwen3OmniMoeCode2WavConfig = vllm_config.model_config.hf_config
connector = getattr(vllm_config.model_config, "stage_connector_config", None)
extra = connector.get("extra", {}) if isinstance(connector, dict) else getattr(connector, "extra", {})
# Both the separate stage and the Talker's first-frame copy receive
# this connector config. Keep algorithm selection consistent between
# them, and leave other models' cuDNN settings unchanged.
self._cudnn_benchmark = (extra or {}).get("codec_cudnn_benchmark", False)
if not isinstance(self._cudnn_benchmark, bool):
raise ValueError("codec_cudnn_benchmark must be a boolean")
# Calculate total upsampling factor
self.total_upsample = np.prod(self.config.upsample_rates + self.config.upsampling_ratios)
@@ -122,10 +216,19 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
Qwen3OmniMoeCausalConvNet(output_dim, 1, kernel_size=7),
]
self.decoder = nn.ModuleList(decoder)
# Keep existing V1 and non-CUDA decoder blocks unchanged.
if (
(extra or {}).get("codec_fused_snake", False)
and current_omni_platform.is_cuda()
and torch.device(vllm_config.device_config.device).type == "cuda"
):
use_fused_snake(self.decoder)
# CUDA Graph support — reuses CUDAGraphDecoderWrapper from Qwen3-TTS
self._cudagraph_enabled = False
self._cudagraph_wrapper = None
# Captured row counts per graph size; empty keeps one padded decode per batch.
self._streaming_batch_sizes: dict[int, list[int]] = {}
def precompute_snake_caches(self):
"""Precompute exp(alpha) and 1/(exp(beta)+eps) for all SnakeBeta modules."""
@@ -142,8 +245,19 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
device: torch.device | None = None,
codec_chunk_frames: int = 0,
codec_left_context_frames: int = 0,
extra_capture_sizes: Iterable[int] = (),
streaming_batch_sizes: Iterable[int] = (),
):
"""Enable CUDA graph acceleration (same pattern as Qwen3-TTS Code2Wav)."""
"""Enable CUDA graph acceleration (same pattern as Qwen3-TTS Code2Wav).
``extra_capture_sizes`` adds exact-size graphs (e.g. a chunk ramp's
decode windows) to the default buckets, so those chunks replay without
zero padding. ``streaming_batch_sizes`` (e.g. ``[2, 4, 8, 16]``) also
captures multi-row graphs for every bucket up to one streaming window
(chunk + left context); a streaming batch is then decoded as a few
length groups (``plan_decode_groups``) instead of one eager call
padded to its longest row.
"""
from vllm_omni.model_executor.models.qwen3_tts.cuda_graph_decoder_wrapper import (
CUDAGraphDecoderWrapper,
)
@@ -154,8 +268,30 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
logger.warning("Cannot enable CUDA Graph: not on CUDA device (got %s)", device)
return
extra = {int(size) for size in extra_capture_sizes if int(size) > 0}
batch_sizes = sorted({int(b) for b in streaming_batch_sizes if int(b) > 1})
capture_sizes = None
extra_shapes: list[tuple[int, int]] = []
if extra or batch_sizes:
capture_sizes = sorted(
set(
CUDAGraphDecoderWrapper.compute_capture_sizes(
codec_chunk_frames=codec_chunk_frames,
codec_left_context_frames=codec_left_context_frames,
)
)
| extra
)
window = codec_chunk_frames + codec_left_context_frames
streaming_sizes = [size for size in capture_sizes if window <= 0 or size <= window]
# A multi-row graph never holds more frames than the largest
# single-row one, so the shared graph pool does not grow.
largest = max(capture_sizes)
extra_shapes = [(b, size) for b in batch_sizes for size in streaming_sizes if b * size <= largest]
wrapper = CUDAGraphDecoderWrapper(
decoder=self,
capture_sizes=capture_sizes,
extra_capture_shapes=extra_shapes,
num_quantizers=self.config.num_quantizers,
enabled=True,
)
@@ -172,10 +308,18 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
raise
self._cudagraph_wrapper = wrapper
self._cudagraph_enabled = True
if extra_shapes and torch.cuda.is_available():
# Return the eager warm-up activations: this stage shares the GPU.
torch.accelerator.empty_cache()
self._streaming_batch_sizes = {
size: [1, *sorted(b for b, shape_size in extra_shapes if shape_size == size)]
for size in {shape_size for _b, shape_size in extra_shapes}
}
logger.info(
"CUDA Graph enabled for Code2Wav: num_quantizers=%d, sizes=%s",
"CUDA Graph enabled for Code2Wav: num_quantizers=%d, sizes=%s, multi-row shapes=%s",
self.config.num_quantizers,
self._cudagraph_wrapper.capture_sizes,
extra_shapes,
)
def forward(self, codes: torch.Tensor) -> torch.Tensor:
@@ -200,6 +344,30 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
hidden = self.pre_transformer(inputs_embeds=hidden).last_hidden_state
# Shape: [batch, seq_len, hidden_size]
# Algorithm search happens during eager warmup before graph capture.
# Restore process settings afterward, including on a failed decode.
# Captured replays use the selected kernels without this Python path.
cudnn = torch.backends.cudnn
context = (
cudnn.flags(
enabled=cudnn.enabled,
benchmark=True,
benchmark_limit=10,
deterministic=cudnn.deterministic,
allow_tf32=cudnn.allow_tf32,
)
if (
self._cudnn_benchmark
and hidden.is_cuda
and torch.version.hip is None
and not torch.cuda.is_current_stream_capturing()
)
else nullcontext()
)
with context:
return self._decode_waveform(hidden)
def _decode_waveform(self, hidden: torch.Tensor) -> torch.Tensor:
# Stage 3: Upsampling
hidden = hidden.permute(0, 2, 1) # [batch, hidden_size, seq_len]
for blocks in self.upsample:
@@ -305,28 +473,38 @@ class Qwen3OmniMoeCode2Wav(nn.Module, Qwen3OmniNestedSupportsQuant):
"defaulting to left_context_size=zeros(len(codes)). This is expected during cudagraph warmup."
)
left_context_size = [0] * codes.shape[0]
# Decode chunk
wavs = []
if self._cudagraph_enabled and self._cudagraph_wrapper is not None:
batch_wav = self._cudagraph_wrapper.decode(codes)
else:
batch_wav = self(codes)
if seq_token_counts is not None:
code_seq_lens = [n // self.config.num_quantizers for n in seq_token_counts]
else:
# Fallback: assume all batch elements share the same sequence length.
code_seq_lens = [codes.shape[-1]] * codes.shape[0]
# The decoder trims the same right-edge tail from every batch row.
# Infer it from the decoded window, not a shorter request's length:
# padding that request with codec zeros does not provide valid context.
tail = max(0, int(codes.shape[-1] * self.total_upsample) - batch_wav.shape[-1])
for idx, code_seq_len in enumerate(code_seq_lens):
# Refill the previous chunk's tail using the re-decoded left context,
# and exclude the current row's padded tail just as singleton decode does.
start = max(0, left_context_size[idx] * self.total_upsample - tail)
end = max(0, code_seq_len * self.total_upsample - tail)
wav_chunk = batch_wav[idx, :, start:end]
wavs.append(wav_chunk)
batch, width = int(codes.shape[0]), int(codes.shape[-1])
graphs = self._cudagraph_wrapper if self._cudagraph_enabled else None
batch_sizes = getattr(self, "_streaming_batch_sizes", None)
if graphs is not None and batch_sizes and batch > 1:
groups = plan_decode_groups(
code_seq_lens, graphs._get_padded_size, lambda size: batch_sizes.get(size, (1,))
)
else:
groups = [(list(range(batch)), width)]
wavs: list[torch.Tensor | None] = [None] * batch
for rows, length in groups:
if len(rows) == batch and length == width:
group_codes = codes
else:
# Basic indexing only: a device index tensor would need a host copy.
group_codes = torch.stack([codes[row, :, :length] for row in rows])
batch_wav = graphs.decode(group_codes) if graphs is not None else self(group_codes)
# The decoder trims the same right-edge tail from every batch row.
# Infer it from the decoded window, not a shorter request's length:
# padding that request with codec zeros does not provide valid context.
tail = max(0, int(length * self.total_upsample) - batch_wav.shape[-1])
for position, row in enumerate(rows):
# Refill the previous chunk's tail using the re-decoded left context,
# and exclude the current row's padded tail just as singleton decode does.
start = max(0, left_context_size[row] * self.total_upsample - tail)
end = max(0, code_seq_lens[row] * self.total_upsample - tail)
wavs[row] = batch_wav[position, :, start:end]
return wavs
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
@@ -18,8 +18,7 @@ from vllm_omni.model_executor.models.common.qwen3_code_predictor import (
CodePredictorDecoderLayer,
_rotate_half,
)
from .short_kv_attention import short_kv_attention
from vllm_omni.model_executor.models.common.short_kv_attention import short_kv_attention
def _attention_cached(
@@ -4,9 +4,7 @@
from typing import Any
import torch
from vllm_omni.platforms import current_omni_platform
from vllm_omni.model_executor.models.common.talker_first_audio import supports_talker_first_audio
def talker_first_audio_enabled(vllm_config: Any) -> bool:
@@ -18,16 +16,4 @@ def talker_first_audio_enabled(vllm_config: Any) -> bool:
from .qwen3_tts_code_predictor_vllm import Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM as Predictor
extra = Predictor._stage_connector_extra_config(vllm_config)
model = vllm_config.model_config
parallel = vllm_config.parallel_config
return (
Predictor._parse_bool_config(extra.get("talker_first_audio"))
and current_omni_platform.is_cuda()
and torch.device(vllm_config.device_config.device).type == "cuda"
and bool(getattr(model, "use_v2_model_runner", False))
and bool(getattr(model, "async_chunk", False))
and parallel.tensor_parallel_size == 1
and parallel.pipeline_parallel_size == 1
and parallel.distributed_executor_backend in (None, "uni")
and not vllm_config.cache_config.enable_prefix_caching
)
return Predictor._parse_bool_config(extra.get("talker_first_audio")) and supports_talker_first_audio(vllm_config)
@@ -0,0 +1,312 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
# ruff: noqa: N803 - Triton constexpr parameters use kernel-style capitals.
"""Fused residual-codebook predictor for Qwen3-TTS (CUDA, BF16).
Same computation as the frame-local-KV predictor with per-call top-k Gumbel
sampling, restructured to issue few kernels per residual step, since at serving
batch sizes each of the 15 dependent steps is launch- and latency-bound:
- Per layer: RMSNorm, one QKV GEMM, one attention kernel (q/k RMSNorm, RoPE,
this call's K/V write and attention over the frame's keys), O projection
accumulated into the residual stream (``addmm_``), RMSNorm, gate/up GEMM,
SiLU-mul, down projection accumulated into the stream.
- The embedding of each sampled code followed by ``small_to_mtp_projection``
is linear, so it folds at load into one ``[vocab, hidden]`` table per
codebook; the sampling kernel writes the next step's input row directly.
- Sampling finds the top-k threshold by bisection over the BF16 order keys
(exact k-th largest, ties kept) instead of a sort.
"""
from __future__ import annotations
import torch
from vllm.triton_utils import tl, tldevice, triton
from .tokenizer_12hz.streaming_decoder import _silu_mul_kernel
@triton.jit
def _rms_rows_kernel(x_ptr, x_stride, w_ptr, out_ptr, eps, N: tl.constexpr):
row = tl.program_id(0).to(tl.int64)
offs = tl.arange(0, N)
x = tl.load(x_ptr + row * x_stride + offs).to(tl.float32)
var = tl.sum(x * x, axis=0) / N
dt = out_ptr.dtype.element_ty
y = (x * tl.rsqrt(var + eps)).to(dt).to(tl.float32)
w = tl.load(w_ptr + offs).to(tl.float32)
tl.store(out_ptr + row * N + offs, (w * y).to(dt))
@triton.jit
def _bf(x):
return x.to(tl.bfloat16).to(tl.float32)
@triton.jit
def _head_norm_rope(x1, x2, w1, w2, c1, c2, s1, s2, eps, HD: tl.constexpr):
"""HF RMSNorm (``weight * normed.to(bf16)``) then RoPE in BF16 elementwise steps, on the two halves."""
var = (tl.sum(x1 * x1, axis=0) + tl.sum(x2 * x2, axis=0)) / HD
r = tl.rsqrt(var + eps)
n1 = _bf(w1 * _bf(x1 * r))
n2 = _bf(w2 * _bf(x2 * r))
# rotate_half(n) = [-n2, n1]
o1 = _bf(_bf(n1 * c1) + _bf(-n2 * s1))
o2 = _bf(_bf(n2 * c2) + _bf(n1 * s2))
return o1, o2
@triton.jit
def _cp_attention_kernel(
qkv_ptr, # [B * NQ, (H + 2 * KVH) * HD]
kc_ptr, # [Bmax, KVH, MAXP, HD]
vc_ptr,
qw_ptr,
kw_ptr,
cos_ptr, # [MAXP, HD] bf16
sin_ptr,
out_ptr, # [B * NQ, H * HD]
first,
eps,
scale,
NQ: tl.constexpr,
H: tl.constexpr,
KVH: tl.constexpr,
HD: tl.constexpr,
MAXP: tl.constexpr,
BK: tl.constexpr,
):
b = tl.program_id(0)
h = tl.program_id(1)
qi = tl.program_id(2)
kvh = h // (H // KVH)
W: tl.constexpr = (H + 2 * KVH) * HD
HALF: tl.constexpr = HD // 2
d = tl.arange(0, HALF)
p = first + qi
row = (b * NQ + qi).to(tl.int64)
qw1 = tl.load(qw_ptr + d).to(tl.float32)
qw2 = tl.load(qw_ptr + HALF + d).to(tl.float32)
kw1 = tl.load(kw_ptr + d).to(tl.float32)
kw2 = tl.load(kw_ptr + HALF + d).to(tl.float32)
qb = qkv_ptr + row * W + h * HD
q1, q2 = _head_norm_rope(
tl.load(qb + d).to(tl.float32), tl.load(qb + HALF + d).to(tl.float32), qw1, qw2,
tl.load(cos_ptr + p * HD + d).to(tl.float32), tl.load(cos_ptr + p * HD + HALF + d).to(tl.float32),
tl.load(sin_ptr + p * HD + d).to(tl.float32), tl.load(sin_ptr + p * HD + HALF + d).to(tl.float32),
eps, HD,
) # fmt: skip
j = tl.arange(0, BK)
cache = ((b * KVH + kvh) * MAXP).to(tl.int64) * HD
old = (j < first)[:, None]
kp = kc_ptr + cache + j[:, None] * HD + d[None, :]
vp = vc_ptr + cache + j[:, None] * HD + d[None, :]
k1 = tl.load(kp, mask=old, other=0.0).to(tl.float32)
k2 = tl.load(kp + HALF, mask=old, other=0.0).to(tl.float32)
v1 = tl.load(vp, mask=old, other=0.0).to(tl.float32)
v2 = tl.load(vp + HALF, mask=old, other=0.0).to(tl.float32)
for n in tl.static_range(NQ):
# this call's position first + n, computed from its own QKV row
on = first + n
kb = qkv_ptr + (b * NQ + n).to(tl.int64) * W + (H + kvh) * HD
vb = qkv_ptr + (b * NQ + n).to(tl.int64) * W + (H + KVH + kvh) * HD
kn1, kn2 = _head_norm_rope(
tl.load(kb + d).to(tl.float32), tl.load(kb + HALF + d).to(tl.float32), kw1, kw2,
tl.load(cos_ptr + on * HD + d).to(tl.float32), tl.load(cos_ptr + on * HD + HALF + d).to(tl.float32),
tl.load(sin_ptr + on * HD + d).to(tl.float32), tl.load(sin_ptr + on * HD + HALF + d).to(tl.float32),
eps, HD,
) # fmt: skip
vn1 = tl.load(vb + d).to(tl.float32)
vn2 = tl.load(vb + HALF + d).to(tl.float32)
sel = (j == on)[:, None]
k1 = tl.where(sel, kn1[None, :], k1)
k2 = tl.where(sel, kn2[None, :], k2)
v1 = tl.where(sel, vn1[None, :], v1)
v2 = tl.where(sel, vn2[None, :], v2)
wm = (d < HALF) & (qi == n) & (h % (H // KVH) == 0)
dtc = kc_ptr.dtype.element_ty
tl.store(kc_ptr + cache + on * HD + d, kn1.to(dtc), mask=wm)
tl.store(kc_ptr + cache + on * HD + HALF + d, kn2.to(dtc), mask=wm)
tl.store(vc_ptr + cache + on * HD + d, vn1.to(dtc), mask=wm)
tl.store(vc_ptr + cache + on * HD + HALF + d, vn2.to(dtc), mask=wm)
km = j <= p
s = (tl.sum(k1 * q1[None, :], axis=1) + tl.sum(k2 * q2[None, :], axis=1)) * scale
s = tl.where(km, s, -float("inf"))
e = tl.exp(s - tl.max(s, axis=0))
e = tl.where(km, e, 0.0)
w = e / tl.sum(e, axis=0)
ob = out_ptr + row * (H * HD) + h * HD
dto = out_ptr.dtype.element_ty
tl.store(ob + d, tl.sum(v1 * w[:, None], axis=0).to(dto))
tl.store(ob + HALF + d, tl.sum(v2 * w[:, None], axis=0).to(dto))
@triton.jit
def _cp_sample_kernel(
logits_ptr, # [B, V] bf16
u_ptr, # uniforms, row stride u_stride, contiguous over V
u_stride,
codes_ptr, # [B, G] int64
step,
G: tl.constexpr,
table_ptr, # [V, HID] next-step input table, or dummy
next_ptr, # [B, HID]
inv_temperature,
V: tl.constexpr,
TOPK: tl.constexpr,
HID: tl.constexpr,
HAS_NEXT: tl.constexpr,
):
row = tl.program_id(0).to(tl.int64)
idx = tl.arange(0, V)
value = tl.load(logits_ptr + row * V + idx)
scaled = (value.to(tl.float32) * inv_temperature).to(value.dtype)
if TOPK > 0:
# order-preserving 16-bit keys of the BF16 values
bits = scaled.to(tl.int16, bitcast=True).to(tl.int32) & 0xFFFF
# Top-k keeps all numeric ties at the cutoff, including both zeros.
bits = tl.where((bits & 0x7FFF) == 0, 0, bits)
key = tl.where(bits >= 0x8000, 0xFFFF - bits, bits | 0x8000)
the = 0
for bit in tl.static_range(15, -1, -1):
cand = the | (1 << bit)
cnt = tl.sum((key >= cand).to(tl.int32), axis=0)
the = tl.where(cnt >= TOPK, cand, the)
sf = tl.where(key >= the, scaled.to(tl.float32), -float("inf"))
else:
sf = scaled.to(tl.float32)
u = tl.load(u_ptr + row * u_stride + idx)
score = sf - tldevice.log(-tldevice.log(u))
max_score = tl.max(score, 0)
chosen = tl.min(tl.where(score == max_score, idx, V), 0)
first_nan = tl.min(tl.where(score != score, idx, V), 0)
code = tl.where(first_nan < V, first_nan, chosen)
tl.store(codes_ptr + row * G + step, code.to(tl.int64))
if HAS_NEXT:
c = tl.arange(0, HID)
tl.store(next_ptr + row * HID + c, tl.load(table_ptr + code.to(tl.int64) * HID + c))
class FusedCodePredictor:
"""Execution object built from a loaded ``Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM``."""
def __init__(self, predictor, max_batch: int) -> None:
model = predictor.model
weight = next(model.parameters())
dev, dt = weight.device, weight.dtype
if dt != torch.bfloat16:
raise ValueError("fused code predictor requires BF16 weights")
cfg = predictor.config
self.groups = int(cfg.num_code_groups)
self.vocab = int(cfg.vocab_size)
self.hidden = int(cfg.hidden_size)
attn0 = model.layers[0].self_attn
self.heads, self.kv_heads, self.head_dim = attn0.num_heads, attn0.num_kv_heads, attn0.head_dim
self.scale = attn0.scaling
self.eps = float(model.norm.variance_epsilon)
self.max_pos = attn0.max_seq
self.max_batch = max_batch
f32 = lambda t: t.detach().to(device=dev, dtype=torch.float32) # noqa: E731
c = lambda t: t.to(dt).contiguous() # noqa: E731
proj = predictor.small_to_mtp_projection
if isinstance(proj, torch.nn.Identity):
pw, pb = None, None
else:
pw, pb = f32(proj.weight), f32(proj.bias)
self.proj_wt = c(pw.t()) if pw is not None else None
self.proj_b = c(pb) if pb is not None else None
tables = []
for emb in model.codec_embedding:
e = f32(emb.weight)
tables.append(c(e @ pw.t() + pb) if pw is not None else c(e))
self.tables = tables
self.layers = []
for layer in model.layers:
a = layer.self_attn
self.layers.append(
dict(
ln1=c(f32(layer.input_layernorm.weight)),
ln2=c(f32(layer.post_attention_layernorm.weight)),
eps1=float(layer.input_layernorm.variance_epsilon),
eps2=float(layer.post_attention_layernorm.variance_epsilon),
qkv=c(f32(a.qkv_proj.weight).t()),
qn=c(f32(a.q_norm.weight)),
kn=c(f32(a.k_norm.weight)),
head_eps=float(a.q_norm.variance_epsilon),
o=c(f32(a.o_proj.weight).t()),
gu=c(f32(layer.mlp.gate_up_proj.weight).t()),
down=c(f32(layer.mlp.down_proj.weight).t()),
)
)
if a.qkv_proj.bias is not None:
raise ValueError("fused code predictor assumes bias-free attention")
self.norm_w = c(f32(model.norm.weight))
self.lm_heads = [c(f32(h.weight).t()) for h in predictor.lm_head]
rot = model.rotary_emb
self.cos = rot.cos_cached.to(device=dev, dtype=dt).contiguous()
self.sin = rot.sin_cached.to(device=dev, dtype=dt).contiguous()
kv = (len(self.layers), 2, max_batch, self.kv_heads, self.max_pos, self.head_dim)
self.cache = torch.zeros(kv, device=dev, dtype=dt)
self._dummy = torch.zeros(1, device=dev, dtype=dt)
def _layer(self, x: torch.Tensor, li: int, B: int, nq: int, first: int) -> None:
L = self.layers[li]
rows = x.shape[0]
h = torch.empty_like(x)
_rms_rows_kernel[(rows,)](x, x.stride(0), L["ln1"], h, L["eps1"], N=self.hidden)
qkv = torch.mm(h, L["qkv"])
att = torch.empty(rows, self.heads * self.head_dim, device=x.device, dtype=x.dtype)
_cp_attention_kernel[(B, self.heads, nq)](
qkv, self.cache[li, 0], self.cache[li, 1], L["qn"], L["kn"], self.cos, self.sin, att,
first, L["head_eps"], self.scale,
NQ=nq, H=self.heads, KVH=self.kv_heads, HD=self.head_dim, MAXP=self.max_pos,
BK=triton.next_power_of_2(self.max_pos), num_warps=1,
) # fmt: skip
x.addmm_(att, L["o"])
_rms_rows_kernel[(rows,)](x, x.stride(0), L["ln2"], h, L["eps2"], N=self.hidden)
gu = torch.mm(h, L["gu"])
inter = gu.shape[1] // 2
a = torch.empty(rows, inter, device=x.device, dtype=x.dtype)
_silu_mul_kernel[(rows, triton.cdiv(inter, 1024))](gu, a, N=inter, BN=1024)
x.addmm_(a, L["down"])
@torch.inference_mode()
def __call__(
self,
layer0_code: torch.Tensor,
layer0_embed: torch.Tensor,
last_talker_hidden: torch.Tensor,
inv_temperature: float,
top_k: int,
sample_uniforms: torch.Tensor,
) -> torch.Tensor:
B = int(layer0_code.shape[0])
if B > self.max_batch:
raise ValueError("fused code predictor batch exceeds its capacity")
dev, dt = layer0_embed.device, self.norm_w.dtype
inp = torch.stack([last_talker_hidden.reshape(B, -1), layer0_embed.reshape(B, -1)], 1).to(dt)
inp = inp.reshape(2 * B, -1)
x = torch.addmm(self.proj_b, inp, self.proj_wt) if self.proj_wt is not None else inp.clone()
codes = torch.empty(B, self.groups, device=dev, dtype=torch.int64)
codes[:, 0] = layer0_code.reshape(B)
u = sample_uniforms
nq, first = 2, 0
for step in range(1, self.groups):
for li in range(len(self.layers)):
self._layer(x, li, B, nq, first)
last = x[nq - 1 :: nq] if nq > 1 else x
hf = torch.empty(B, self.hidden, device=dev, dtype=dt)
_rms_rows_kernel[(B,)](last, last.stride(0), self.norm_w, hf, self.eps, N=self.hidden)
logits = torch.mm(hf, self.lm_heads[step - 1])
has_next = step < self.groups - 1
nxt = torch.empty(B, self.hidden, device=dev, dtype=dt) if has_next else self._dummy
us = u[:, step - 1]
_cp_sample_kernel[(B,)](
logits, us, us.stride(0), codes, step, self.groups,
self.tables[step - 1] if has_next else self._dummy, nxt, inv_temperature,
V=self.vocab, TOPK=top_k, HID=self.hidden, HAS_NEXT=has_next, num_warps=4,
) # fmt: skip
x = nxt
first = step + 1
nq = 1
return codes
@@ -72,3 +72,33 @@ QWEN3_TTS_PIPELINE = PipelineConfig(
),
),
)
# Single-stage variant: the Talker decodes each frame with the stateful
# streaming codec decoder and emits PCM as the final output (enable with the
# ``talker_stream_decode`` model option). No Code2Wav stage, no connector.
QWEN3_TTS_FUSED_PIPELINE = PipelineConfig(
model_type="qwen3_tts_fused",
default_deploy_config_name="qwen3_tts_fused_single_gpu.yaml",
single_stage_async_chunk=True,
model_arch="Qwen3TTSTalkerForConditionalGeneration",
stages=(
StagePipelineConfig(
stage_id=0,
model_stage="qwen3_tts",
execution_type=StageExecutionType.LLM_AR,
input_sources=(),
owns_tokenizer=True,
final_output=True,
final_output_type="audio",
# The API output processor tags ``model_outputs`` by this modality.
engine_output_type="audio",
supports_running_prefix_cache_reset=False,
supports_native_mrv2_data_plane=True,
sampling_constraints={
"detokenize": False,
"stop_token_ids": [2150],
},
),
),
)
@@ -42,6 +42,7 @@ from vllm.logger import init_logger
from vllm.multimodal.audio import AudioResampler
from vllm_omni.utils.audio import mel_filter_bank
from vllm_omni.utils.device_copy import index_to_device, to_device_nonblocking
if TYPE_CHECKING:
from .configuration_qwen3_tts import Qwen3TTSConfig, Qwen3TTSTalkerConfig
@@ -160,7 +161,7 @@ def coerce_token_ids(value: object, *, device: torch.device) -> torch.Tensor | N
ids = ids.unsqueeze(0)
if ids.ndim != 2 or ids.numel() == 0:
return None
return ids.to(device=device, dtype=torch.long).contiguous()
return to_device_nonblocking(ids.to(dtype=torch.long).contiguous(), device)
# ---------------------------------------------------------------------------
@@ -366,6 +367,9 @@ class Qwen3TTSPromptEmbedsBuilder:
# Reusing them avoids launching the embedding/projection pair once per
# request during prompt construction.
self._projected_token_cache: dict[tuple[str, tuple[int, ...]], torch.Tensor] = {}
# CustomVoice prompt pieces that do not depend on the text, per
# (device, speaker, language): bounded by the model's voices.
self._custom_voice_prompt_cache: dict[tuple, tuple[torch.Tensor, torch.Tensor, torch.Tensor]] = {}
self._ref_audio_artifact_cache_max_entries = int(ref_audio_artifact_cache_max_entries)
self._ref_audio_artifact_cache: OrderedDict[str, dict[str, torch.Tensor | bool]] = OrderedDict()
@@ -1024,7 +1028,9 @@ class Qwen3TTSPromptEmbedsBuilder:
dev = self._device()
input_ids = coerce_token_ids(info_dict.pop(PRECOMPUTED_TEXT_IDS_KEY, None), device=dev)
if input_ids is None:
input_ids = tok(build_assistant_text(text), return_tensors="pt", padding=False)["input_ids"].to(device=dev)
input_ids = to_device_nonblocking(
tok(build_assistant_text(text), return_tensors="pt", padding=False)["input_ids"], dev
)
# Optional instruct prefix.
instruct = (info_dict.get("instruct") or [""])[0]
@@ -1411,35 +1417,44 @@ class Qwen3TTSPromptEmbedsBuilder:
raise ValueError(f"Unsupported speaker: {speaker}")
spk_id = spk_id_map[speaker]
# Keep it at least 1D; embedding on a 0-d tensor can return 1D.
spk_tensor = torch.tensor([spk_id], device=input_ids.device, dtype=torch.long)
spk_tensor = index_to_device([spk_id], input_ids.device)
spk_embed = codec_embed(spk_tensor)
if spk_embed.ndim in (1, 2):
spk_embed = spk_embed.view(1, 1, -1)
speaker_embed = spk_embed
codec_input = torch.cat([codec_input_0, speaker_embed, codec_input_1], dim=1)
role_embed = text_projection(text_embedding(input_ids[:, :3]))
codec_prefix = torch.cat((tts_pad_embed.expand(-1, codec_input.shape[1] - 2, -1), tts_bos_embed), dim=1)
codec_prefix = codec_prefix + codec_input[:, :-1]
talker_prompt = torch.cat((role_embed, codec_prefix), dim=1)
# The role tokens, speaker/language codec prefix, codec pad row and
# closing row do not depend on the text: cache them per speaker and
# language so a request only embeds and projects its own text.
# A text starting with whitespace could merge with the template's
# newline token, so it keeps the per-request role embedding.
cache_key = None
if non_streaming_mode and instruct_embed is None and not text[:1].isspace():
cache_key = (
str(input_ids.device),
int(spk_id),
codec_prefill_list[0][2] if len(codec_prefill_list[0]) == 4 else None,
)
cv_cache = self._custom_voice_prompt_cache
cached = cv_cache.get(cache_key) if cache_key is not None else None
if cached is None:
role_embed = text_projection(text_embedding(input_ids[:, :3]))
codec_prefix = torch.cat((tts_pad_embed.expand(-1, codec_input.shape[1] - 2, -1), tts_bos_embed), dim=1)
codec_prefix = codec_prefix + codec_input[:, :-1]
prefix = torch.cat((role_embed, codec_prefix), dim=1)
pad_row = codec_embed(self._long_tensor([talker_config.codec_pad_id], input_ids.device)).view(1, 1, -1)
tail = tts_pad_embed + codec_embed(self._long_tensor([talker_config.codec_bos_id], input_ids.device))
if cache_key is not None:
cv_cache[cache_key] = (prefix, pad_row, tail)
else:
prefix, pad_row, tail = cached
talker_prompt = prefix
if non_streaming_mode:
text_all = text_projection(text_embedding(input_ids[:, 3:-5]))
text_all = torch.cat([text_all, tts_eos_embed], dim=1)
pad_ids = torch.full(
(1, int(text_all.shape[1])),
int(talker_config.codec_pad_id),
device=input_ids.device,
dtype=torch.long,
)
talker_prompt = torch.cat(
[
talker_prompt,
text_all + codec_embed(pad_ids),
tts_pad_embed + codec_embed(self._long_tensor([talker_config.codec_bos_id], input_ids.device)),
],
dim=1,
)
talker_prompt = torch.cat([talker_prompt, text_all + pad_row, tail], dim=1)
trailing_text_hidden = tts_pad_embed
else:
first_text = text_projection(text_embedding(input_ids[:, 3:4])) + codec_input[:, -1:]
@@ -55,6 +55,9 @@ class Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM(CodePredictorWrapp
talker_hidden_size=int(talker_config.hidden_size),
prefix=prefix,
)
# TTS owns its frame-local cache and overrides _predict_step_logits.
# Do not also activate the shared wrapper's Omni incremental loop.
self._kv_cache_enabled = False
# Store talker_config for backward compat (accessed by some callers)
self.talker_config = talker_config
self._vllm_config = vllm_config
@@ -62,6 +65,8 @@ class Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM(CodePredictorWrapp
self._kv_requested = self._parse_bool_config(extra.get("code_predictor_kv_cache"))
self._fused_sampling = self._parse_bool_config(extra.get("code_predictor_fused_sampling"))
self._frame_cache: FrameLocalKVCache | None = None
self._fused_requested = self._parse_bool_config(extra.get("code_predictor_fused"))
self._fused = None
def _setup_compile(self) -> None:
if self._compiled_model_fwd is not None:
@@ -97,11 +102,73 @@ class Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM(CodePredictorWrapp
cache(self._proj_buf, batch, step)
self._synchronize_warmup(weight.device)
self._frame_cache = cache
if self._fused_requested:
from .fused_code_predictor import FusedCodePredictor
self._fused = FusedCodePredictor(self, max_batch)
# Compile every kernel variant outside any graph capture.
hidden = int(self.talker_config.hidden_size)
for batch in range(1, min(2, max_batch) + 1):
zeros = torch.zeros(batch, 1, hidden, device=weight.device, dtype=weight.dtype)
uniforms = torch.full(
(batch, self._num_groups - 1, int(self.config.vocab_size)), 0.5, device=weight.device
)
codes = torch.zeros(batch, 1, device=weight.device, dtype=torch.long)
self._fused(codes, zeros, zeros, 1.0, 50, uniforms)
self._synchronize_warmup(weight.device)
logger.info("Qwen3-TTS fused code predictor enabled (capacity %d)", max_batch)
# Preserve the shared wrapper's initialization sentinel. Its full
# re-prefill callable is unused while _frame_cache is active.
self._compiled_model_fwd = self.model.forward
logger.info("Qwen3-TTS frame-local KV warmed for batches %s", self._bucket_sizes)
@torch.inference_mode()
def forward(
self,
layer0_code: torch.Tensor,
layer0_embed: torch.Tensor,
last_talker_hidden: torch.Tensor,
do_sample: bool = True,
temperature: float = 0.9,
top_k: int = 50,
top_p: float = 1.0,
generator: torch.Generator | None = None,
generators: Sequence[torch.Generator | None] | None = None,
sample_uniforms: torch.Tensor | None = None,
):
self._validate_sampling_inputs(int(layer0_code.shape[0]), generators, sample_uniforms)
if self._fused_requested:
self._setup_compile()
if (
self._fused is not None
and do_sample
and temperature > 0
and top_p == 1.0
and sample_uniforms is not None
and sample_uniforms.dtype == torch.float32
and sample_uniforms.is_contiguous()
):
return self._fused(
layer0_code,
layer0_embed,
last_talker_hidden,
1.0 / temperature,
int(top_k),
sample_uniforms,
)
return super().forward(
layer0_code,
layer0_embed,
last_talker_hidden,
do_sample=do_sample,
temperature=temperature,
top_k=top_k,
top_p=top_p,
generator=generator,
generators=generators,
sample_uniforms=sample_uniforms,
)
def _predict_step_logits(
self,
proj_buf: torch.Tensor,
@@ -27,6 +27,7 @@ from vllm.sequence import IntermediateTensors
from vllm_omni.data_entry_keys import OmniPayload
from vllm_omni.model_executor.models.output_templates import OmniOutput, OwnedBatchTensor
from vllm_omni.utils.device_copy import index_to_device, to_device_nonblocking
from vllm_omni.utils.speaker_cache import (
get_speaker_cache,
iter_custom_voice_profiles,
@@ -39,6 +40,7 @@ from .configuration_qwen3_tts import Qwen3TTSConfig, Qwen3TTSSpeakerEncoderConfi
from .first_audio import talker_first_audio_enabled
from .prompt_embeds_builder import PRECOMPUTED_TEXT_IDS_KEY, Qwen3TTSPromptEmbedsBuilder, resolve_x_vector_only
from .qwen3_tts_code_predictor_vllm import Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM
from .stream_decode import stream_ref_context_frames, talker_stream_decode_enabled
from .tokenizer_12hz.configuration_qwen3_tts_tokenizer_v2 import Qwen3TTSTokenizerV2Config
from .tokenizer_12hz.modeling_qwen3_tts_tokenizer_v2 import Qwen3TTSTokenizerV2Encoder
@@ -411,7 +413,21 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
# A frame's residual codebooks depend only on the Talker hidden and
# CB0 of the step that sampled it, so MRV2 may complete the frame at
# the end of that step instead of in the next step's preprocess.
self.mtp_eager_frames = talker_first_audio_enabled(vllm_config)
self.stream_decode = talker_stream_decode_enabled(vllm_config)
predictor = Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM
extra = predictor._stage_connector_extra_config(vllm_config)
self.stream_first_audio = self.stream_decode and predictor._parse_bool_config(
extra.get("talker_stream_first_audio")
)
# Model-local voice-clone priming boundary; each deployment selects
# its reference context independently of the two-stage codec profile.
self.stream_ref_context_frames = stream_ref_context_frames(vllm_config) if self.stream_decode else 0
self.mtp_eager_frames = talker_first_audio_enabled(vllm_config) or self.stream_decode
self.stream_decoder = None
self.stream_graphs = None
self.stream_prime_graphs = None
self.stream_sample_rate = 0
self.stream_chunk_frames = 25
# The runners bypass only the outer whole-MTP graph when explicit
# generators are present, so seeded requests can still share one raw
# batched MTP call with independent per-row streams.
@@ -718,6 +734,11 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
# -------------------- Omni multimodal output plumbing --------------------
@property
def mm_outputs_fresh_per_step(self) -> bool:
"""Stream-decode outputs come from :meth:`_make_eager_omni_output`, allocated per step."""
return self.stream_decoder is not None and self.eager_frames_active
def make_omni_output(self, model_outputs: torch.Tensor | OmniOutput, **kwargs: Any) -> OmniOutput:
if isinstance(model_outputs, OmniOutput):
return model_outputs
@@ -895,6 +916,13 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
"first_audio": torch.zeros((num_tokens,), dtype=torch.int8, device=hidden.device),
},
}
if self.stream_decoder is not None:
# Filled per sampled row by the runner's eager MTP; token-major so
# each request's slice is its frames of this step.
spf = int(self.stream_decoder.spf)
mm["model_outputs"] = torch.zeros((num_tokens, spf), dtype=torch.float32, device=hidden.device)
sr = torch.tensor(self.stream_sample_rate, dtype=torch.int32)
mm["sr"] = [sr] * len(info_dicts)
ref_rows = [
index
for index, info in enumerate(info_dicts)
@@ -1442,8 +1470,72 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
self.first_frame_decoder = Qwen3TTSFirstFrameDecoder(self.model_path)
decoder_loaded = self.first_frame_decoder.load(self.vllm_config)
loaded = set(loaded) | {f"first_frame_decoder.{name}" for name in decoder_loaded}
elif self.stream_decode:
# The in-stage decoder owns every frame. Optional first-audio
# delivery uses its PCM; later chunks use regular step outputs.
from .first_frame_decoder import Qwen3TTSFirstFrameDecoder
from .tokenizer_12hz.streaming_decoder import StreamingCodecDecoder
holder = Qwen3TTSFirstFrameDecoder(self.model_path)
decoder_loaded = holder.load(self.vllm_config)
loaded = set(loaded) | {f"stream_decoder.{name}" for name in decoder_loaded}
slots = int(self.vllm_config.scheduler_config.max_num_seqs)
self.stream_decoder = StreamingCodecDecoder(holder.decoder, num_slots=slots, dtype=torch.bfloat16)
self.stream_sample_rate = int(holder.sample_rate)
logger.info("Qwen3-TTS Talker stream decode enabled (%d slots)", slots)
return loaded
def capture_stream_decode_graphs(self, batch_sizes: list[int]) -> None:
if self.stream_decoder is None or self.stream_graphs is not None:
return
from .tokenizer_12hz.streaming_decoder import StreamingDecodeGraphs
with torch.inference_mode():
self.stream_graphs = StreamingDecodeGraphs(self.stream_decoder, batch_sizes)
frames = min(self.stream_ref_context_frames, self.stream_chunk_frames, self.stream_decoder.max_frames)
if self.config.tts_model_type == "base" and frames > 1:
# Bound the multi-frame graph workspace by the configured
# token budget; larger priming groups keep the eager path.
budget = max(1, self.vllm_config.scheduler_config.max_num_batched_tokens // frames)
prime_sizes = sorted({1, *(size for size in batch_sizes if size <= budget)})
self.stream_prime_graphs = StreamingDecodeGraphs(self.stream_decoder, prime_sizes, frames=frames)
logger.info("Captured Talker stream decode graphs for batch sizes %s", self.stream_graphs.sizes)
if self.stream_prime_graphs is not None:
logger.info(
"Captured Talker reference priming graphs for %d frames and batch sizes %s",
self.stream_prime_graphs.frames,
self.stream_prime_graphs.sizes,
)
def get_stream_ref_context(self, info: OmniPayload) -> torch.Tensor | None:
"""Model-owned reference-code layout and context boundary for a PCM slot."""
codes = info.get("codes")
ref = codes.get("ref") if isinstance(codes, dict) else None
if not isinstance(ref, torch.Tensor) or not ref.numel():
return None
return ref.reshape(-1, int(self.talker_config.num_code_groups))[-self.stream_ref_context_frames :]
def prime_stream_decoder(self, primes: list[tuple[int, torch.Tensor]]) -> None:
"""Prime equal-length reference groups without padding or aliasing live slots."""
if not primes:
return
stream = self.stream_decoder
if stream is None:
raise RuntimeError("Reference priming requires the single-stage PCM decoder")
groups: dict[int, list[tuple[int, torch.Tensor]]] = {}
for req_idx, ref in primes:
groups.setdefault(int(ref.shape[0]), []).append((req_idx, ref))
for items in groups.values():
codes = to_device_nonblocking(torch.stack([ref for _idx, ref in items]), stream.device).to(torch.int32)
n = len(items)
slots = index_to_device([idx for idx, _ref in items], stream.device, dtype=torch.int32)
for t0 in range(0, int(codes.shape[1]), self.stream_chunk_frames):
pos = index_to_device([t0] * n, stream.device, dtype=torch.int32)
chunk = codes[:, t0 : t0 + self.stream_chunk_frames].contiguous()
graphs = self.stream_prime_graphs
decode = graphs if graphs is not None and chunk.shape[1] == graphs.frames else stream
decode(chunk, slots, pos)
def _build_stacked_codec_embed(self) -> None:
embeds = self.code_predictor.get_input_embeddings()
if not embeds:
@@ -0,0 +1,60 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Eligibility and model options for the single-stage CUDA PCM pipeline."""
import torch
from vllm.config import VllmConfig
from vllm_omni.platforms import current_omni_platform
def talker_stream_decode_enabled(vllm_config: VllmConfig) -> bool:
"""The Talker decodes every frame itself and emits PCM as its final output."""
from .qwen3_tts_code_predictor_vllm import Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM as Predictor
extra = Predictor._stage_connector_extra_config(vllm_config)
model = vllm_config.model_config
parallel = vllm_config.parallel_config
requested = Predictor._parse_bool_config(extra.get("talker_stream_decode"))
if not requested:
if getattr(model, "engine_output_type", None) == "audio":
raise ValueError("The single-stage Qwen3-TTS pipeline requires talker_stream_decode=True")
if Predictor._parse_bool_config(extra.get("talker_stream_first_audio")):
raise ValueError("talker_stream_first_audio requires the single-stage talker_stream_decode path")
return False
if getattr(model, "engine_output_type", None) != "audio" or not getattr(model, "final_output", False):
raise ValueError(
"talker_stream_decode requires a final audio-output Talker; "
"select qwen3_tts_fused_single_gpu.yaml instead of enabling it on the two-stage pipeline"
)
if getattr(model, "supports_running_prefix_cache_reset", True):
raise ValueError(
"The single-stage PCM pipeline must disable running prefix-cache reset; "
"select qwen3_tts_fused_single_gpu.yaml"
)
if not (
current_omni_platform.is_cuda()
and torch.device(vllm_config.device_config.device).type == "cuda"
and bool(getattr(model, "use_v2_model_runner", False))
and bool(getattr(model, "async_chunk", False))
and parallel.tensor_parallel_size == 1
and parallel.pipeline_parallel_size == 1
and parallel.distributed_executor_backend in (None, "uni")
and not vllm_config.cache_config.enable_prefix_caching
):
raise ValueError(
"talker_stream_decode requires CUDA, MRv2, async_chunk=True, TP=PP=1, "
"an in-process worker and enable_prefix_caching=False; "
"use qwen3_tts_fused_single_gpu.yaml"
)
if Predictor._parse_bool_config(extra.get("talker_first_audio")):
raise ValueError("talker_stream_decode and talker_first_audio are mutually exclusive")
return True
def stream_ref_context_frames(vllm_config: VllmConfig) -> int:
"""Reference-code context frames for voice-clone priming in the Talker."""
from .qwen3_tts_code_predictor_vllm import Qwen3TTSTalkerCodePredictorForConditionalGenerationVLLM as Predictor
extra = Predictor._stage_connector_extra_config(vllm_config)
return int(extra.get("ref_code_context_frames") or extra.get("codec_left_context_frames", 25))
@@ -0,0 +1,799 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
# ruff: noqa: N803 - Triton constexpr parameters use kernel-style capitals.
"""Stateful, fused streaming decoder for the Qwen3-TTS 12 Hz codec.
Every layer of the codec decoder is causal, so what a new frame needs from the
past is small and fixed: each causal conv's last ``(k - 1) * dilation`` input
rows, each overlapping transposed conv's last GEMM row, and each attention
layer's last ``sliding_window`` keys and values. A request owns a slot of
per-layer state holding exactly that, and a call decodes ``T`` new frames for
each of ``B`` requests, reading and advancing the state. The output equals the
whole-utterance decode (``Qwen3TTSTokenizerV2Decoder._forward_exact``) frame by
frame, up to floating point rounding: nothing is recomputed and nothing is
truncated.
Layout and fusion:
- Activations are time-major ``[rows, C]``; every dense conv is a tap-major
im2col (one Triton kernel that also applies the pending bias and SnakeBeta,
and reads/writes the history) followed by one cuBLAS GEMM.
- Linear chains fold at load: the RVQ output projections, ``pre_conv`` and the
transformer's ``input_proj`` become one ``k = 3`` conv over the concatenated
codebook embeddings; LayerScale folds into ``o_proj``/``down_proj``; ConvNeXt
``gamma`` folds into ``pwconv2``.
- A bias that feeds a residual stream is carried to that stream's consumer
(the next im2col or transposed-conv GEMM) instead of being added in place.
- Residual branches accumulate into their stream with in-place ``addmm_``.
History buffers are double-buffered per slot: a call reads half ``parity[slot]``
and writes the other half, then flips the parity, so no kernel overwrites rows
another program of the same launch still reads. A row whose first frame index
``pos`` is 0 reads zeros, which are the convs' causal padding, and attention
only looks back ``min(pos + 1, window)`` positions, so a new request needs no
state reset.
"""
from __future__ import annotations
from dataclasses import dataclass
import torch
from torch import nn
from vllm.triton_utils import tl, triton
from vllm_omni.platforms import current_omni_platform
_KERNEL = 7
# --------------------------------------------------------------------------- kernels
@triton.jit
def _rvq_gather_kernel(codes_ptr, books_ptr, out_ptr, NQ: tl.constexpr, CB: tl.constexpr, HALF: tl.constexpr):
row = tl.program_id(0)
offs = tl.arange(0, HALF)
c0 = tl.load(codes_ptr + row * NQ).to(tl.int64)
c0 = tl.where((c0 >= 0) & (c0 < CB), c0, 0) # EOS/special rows decode as code 0; callers drop them
first = tl.load(books_ptr + c0 * HALF + offs).to(tl.float32)
acc = tl.zeros([HALF], dtype=tl.float32)
for q in range(1, NQ):
c = tl.load(codes_ptr + row * NQ + q).to(tl.int64)
c = tl.where((c >= 0) & (c < CB), c, 0)
acc += tl.load(books_ptr + (q * CB + c) * HALF + offs).to(tl.float32)
dt = out_ptr.dtype.element_ty
tl.store(out_ptr + row * 2 * HALF + offs, first.to(dt))
tl.store(out_ptr + row * 2 * HALF + HALF + offs, acc.to(dt))
@triton.jit
def _fast_sin(y):
# Two-step reduction to [-pi, pi], then the hardware approximation
# (absolute error ~4e-7 there): accurate sin's instruction count, not
# memory, bounds every kernel that applies SnakeBeta.
k = tl.extra.cuda.libdevice.rint(y * 0.15915494309189535)
r = tl.fma(-k, -1.7484555314695172e-07, tl.fma(-k, 6.2831854820251465, y))
return tl.inline_asm_elementwise("sin.approx.f32 $0, $1;", "=r,r", [r], dtype=tl.float32, is_pure=True, pack=1)
@triton.jit
def _snake(v, a, ib):
s = _fast_sin(v * a)
return v + ib * s * s
@triton.jit
def _im2col_kernel(
x_ptr, # [B * T, C] layer input rows
bias_ptr, # [C] pending bias (added to x rows only) or dummy
alpha_ptr, # [C] exp(alpha) or dummy
ibeta_ptr, # [C] 1 / (exp(beta) + eps) or dummy
hist_ptr, # [2, S, H, C] transformed history rows
slots_ptr,
pos_ptr,
par_ptr,
col_ptr, # [B * T, K * C]
T,
S,
C: tl.constexpr,
K: tl.constexpr,
D: tl.constexpr,
H: tl.constexpr,
HAS_BIAS: tl.constexpr,
HAS_SNAKE: tl.constexpr,
BU: tl.constexpr,
BC: tl.constexpr,
EXT: tl.constexpr = False,
):
"""Source row ``u`` of ``[history (H) | x (T)]``: scatter to every tap that reads it, save the last H."""
b = tl.program_id(0)
u = tl.program_id(1) * BU + tl.arange(0, BU)
c = tl.program_id(2) * BC + tl.arange(0, BC)
cm = c < C
n_src = H + T
um = u < n_src
slot = tl.load(slots_ptr + b).to(tl.int64)
live = tl.load(pos_ptr + b) != 0
par = tl.load(par_ptr + slot).to(tl.int64)
# history rows
from_hist = u < H
hrow = tl.where(from_hist, u, 0)
hv = tl.load(
hist_ptr + ((par * S + slot) * H + hrow)[:, None] * C + c[None, :],
mask=(from_hist & um & live)[:, None] & cm[None, :],
other=0.0,
).to(tl.float32)
xrow = tl.where(from_hist, 0, u - H)
xv = tl.load(
x_ptr + (b * T + xrow).to(tl.int64)[:, None] * C + c[None, :],
mask=((~from_hist) & um)[:, None] & cm[None, :],
other=0.0,
).to(tl.float32)
if HAS_BIAS:
xv += tl.load(bias_ptr + c, mask=cm, other=0.0).to(tl.float32)[None, :]
if HAS_SNAKE:
a = tl.load(alpha_ptr + c, mask=cm, other=0.0).to(tl.float32)[None, :]
ib = tl.load(ibeta_ptr + c, mask=cm, other=0.0).to(tl.float32)[None, :]
xv = _snake(xv, a, ib)
dt = col_ptr.dtype.element_ty
v = tl.where(from_hist[:, None], hv, xv.to(dt).to(tl.float32)).to(dt)
if EXT:
tl.store(col_ptr + (b * n_src + u).to(tl.int64)[:, None] * C + c[None, :], v, mask=um[:, None] & cm[None, :])
else:
for j in tl.static_range(K):
t = u - j * D
tm = (t >= 0) & (t < T) & um
tl.store(
col_ptr + (b * T + tl.where(tm, t, 0)).to(tl.int64)[:, None] * (K * C) + j * C + c[None, :],
v,
mask=tm[:, None] & cm[None, :],
)
if H > 0:
hm = (u >= T) & um
tl.store(
hist_ptr + (((1 - par) * S + slot) * H + tl.where(hm, u - T, 0))[:, None] * C + c[None, :],
v,
mask=hm[:, None] & cm[None, :],
)
@triton.jit
def _rms_norm_kernel(x_ptr, w_ptr, out_ptr, eps, N: tl.constexpr):
row = tl.program_id(0).to(tl.int64)
offs = tl.arange(0, N)
x = tl.load(x_ptr + row * N + offs).to(tl.float32)
var = tl.sum(x * x, axis=0) / N
dt = out_ptr.dtype.element_ty
y = (x * tl.rsqrt(var + eps)).to(dt).to(tl.float32)
w = tl.load(w_ptr + offs).to(tl.float32)
tl.store(out_ptr + row * N + offs, (w * y).to(dt))
@triton.jit
def _rope_kv_write_kernel(
qkv_ptr, # [B * T, 3 * NH * HD]
q_ptr, # [B * T, NH * HD]
k_ring_ptr, # [S, RING, NH * HD]
v_ring_ptr,
slots_ptr,
pos_ptr,
T,
theta,
NH: tl.constexpr,
HD: tl.constexpr,
RING: tl.constexpr,
):
r = tl.program_id(0)
h = tl.program_id(1)
b = r // T
p = tl.load(pos_ptr + b) + r % T
slot = tl.load(slots_ptr + b).to(tl.int64)
half: tl.constexpr = HD // 2
i = tl.arange(0, half)
inv_freq = tl.exp(-(2.0 * i.to(tl.float32) / HD) * tl.log(theta))
ang = p.to(tl.float32) * inv_freq
dt = q_ptr.dtype.element_ty
cos = tl.cos(ang).to(dt).to(tl.float32)
sin = tl.sin(ang).to(dt).to(tl.float32)
W: tl.constexpr = NH * HD
base = qkv_ptr + r.to(tl.int64) * 3 * W + h * HD
q1 = tl.load(base + i).to(tl.float32)
q2 = tl.load(base + half + i).to(tl.float32)
k1 = tl.load(base + W + i).to(tl.float32)
k2 = tl.load(base + W + half + i).to(tl.float32)
vv = tl.arange(0, HD)
v = tl.load(base + 2 * W + vv)
qo = q_ptr + r.to(tl.int64) * W + h * HD
tl.store(qo + i, (q1 * cos - q2 * sin).to(dt))
tl.store(qo + half + i, (q2 * cos + q1 * sin).to(dt))
ring = (slot * RING + p % RING) * W + h * HD
tl.store(k_ring_ptr + ring + i, (k1 * cos - k2 * sin).to(dt))
tl.store(k_ring_ptr + ring + half + i, (k2 * cos + k1 * sin).to(dt))
tl.store(v_ring_ptr + ring + vv, v)
@triton.jit
def _attend_kernel(
q_ptr, # [B * T, NH * HD]
k_ring_ptr,
v_ring_ptr,
out_ptr, # [B * T, NH * HD]
slots_ptr,
pos_ptr,
T,
scale,
NH: tl.constexpr,
HD: tl.constexpr,
RING: tl.constexpr,
WINDOW: tl.constexpr,
BK: tl.constexpr,
):
r = tl.program_id(0)
h = tl.program_id(1)
b = r // T
p = tl.load(pos_ptr + b) + r % T
slot = tl.load(slots_ptr + b).to(tl.int64)
W: tl.constexpr = NH * HD
d = tl.arange(0, HD)
q = tl.load(q_ptr + r.to(tl.int64) * W + h * HD + d).to(tl.float32)
j = tl.arange(0, BK)
n = tl.minimum(p + 1, WINDOW)
km = j < n
kp = p - j # key positions p, p-1, ...
idx = (slot * RING + tl.where(km, kp, 0) % RING) * W + h * HD
k = tl.load(k_ring_ptr + idx[:, None] + d[None, :], mask=km[:, None], other=0.0).to(tl.float32)
s = tl.sum(k * q[None, :], axis=1) * scale
s = tl.where(km, s, -float("inf"))
m = tl.max(s, axis=0)
e = tl.exp(s - m)
e = tl.where(km, e, 0.0)
w = e / tl.sum(e, axis=0)
dt = out_ptr.dtype.element_ty
w = w.to(dt).to(tl.float32)
v = tl.load(v_ring_ptr + idx[:, None] + d[None, :], mask=km[:, None], other=0.0).to(tl.float32)
o = tl.sum(v * w[:, None], axis=0)
tl.store(out_ptr + r.to(tl.int64) * W + h * HD + d, o.to(dt))
@triton.jit
def _silu_mul_kernel(gu_ptr, out_ptr, N: tl.constexpr, BN: tl.constexpr):
row = tl.program_id(0).to(tl.int64)
offs = tl.program_id(1) * BN + tl.arange(0, BN)
m = offs < N
g = tl.load(gu_ptr + row * 2 * N + offs, mask=m).to(tl.float32)
u = tl.load(gu_ptr + row * 2 * N + N + offs, mask=m).to(tl.float32)
dt = out_ptr.dtype.element_ty
sg = (g / (1.0 + tl.exp(-g))).to(dt).to(tl.float32)
tl.store(out_ptr + row * N + offs, (sg * u).to(dt), mask=m)
@triton.jit
def _dwconv_ln_kernel(
z_ptr, # [B * T, C] ConvNeXt input rows (true values)
w_ptr, # [K, C]
b_ptr, # [C]
lnw_ptr,
lnb_ptr,
hist_ptr, # [2, S, K - 1, C]
slots_ptr,
pos_ptr,
par_ptr,
out_ptr, # [B * T, C] LayerNorm output
T,
S,
eps,
C: tl.constexpr,
K: tl.constexpr,
):
b = tl.program_id(0)
t = tl.program_id(1) # 0 .. max(T, K - 1)
H: tl.constexpr = K - 1
c = tl.arange(0, C)
slot = tl.load(slots_ptr + b).to(tl.int64)
live = tl.load(pos_ptr + b) != 0
par = tl.load(par_ptr + slot).to(tl.int64)
dt = out_ptr.dtype.element_ty
hbase = hist_ptr + (par * S + slot) * H * C
acc = tl.load(b_ptr + c).to(tl.float32)
for j in tl.static_range(K):
u = t + j # index into [hist (H) | z (T)]
fh = u < H
hv = tl.load(hbase + tl.where(fh, u, 0) * C + c, mask=(c < C) & fh & live, other=0.0).to(tl.float32)
zrow = (b * T + tl.where(fh, 0, u - H)).to(tl.int64)
zv = tl.load(z_ptr + zrow * C + c, mask=(c < C) & (~fh) & (t < T), other=0.0).to(tl.float32)
acc += tl.where(fh, hv, zv) * tl.load(w_ptr + j * C + c).to(tl.float32)
h = acc.to(dt).to(tl.float32)
mean = tl.sum(h, axis=0) / C
hc = h - mean
var = tl.sum(hc * hc, axis=0) / C
y = hc * tl.rsqrt(var + eps) * tl.load(lnw_ptr + c).to(tl.float32) + tl.load(lnb_ptr + c).to(tl.float32)
tl.store(out_ptr + (b * T + t).to(tl.int64) * C + c, y.to(dt), mask=(c < C) & (t < T))
# new history row t: source row T + t of [hist | z]
u = T + t
fh = u < H
hv = tl.load(hbase + tl.where(fh, u, 0) * C + c, mask=(c < C) & fh & live & (t < H), other=0.0)
zrow = (b * T + tl.where(fh, 0, u - H)).to(tl.int64)
zv = tl.load(z_ptr + zrow * C + c, mask=(c < C) & (~fh) & (t < H), other=0.0)
tl.store(
hist_ptr + (((1 - par) * S + slot) * H + tl.where(t < H, t, 0)) * C + c,
tl.where(fh, hv, zv).to(dt),
mask=(c < C) & (t < H),
)
@triton.jit
def _bias_gelu_kernel(x_ptr, b_ptr, N: tl.constexpr, BN: tl.constexpr):
row = tl.program_id(0).to(tl.int64)
offs = tl.program_id(1) * BN + tl.arange(0, BN)
m = offs < N
x = tl.load(x_ptr + row * N + offs, mask=m).to(tl.float32) + tl.load(b_ptr + offs, mask=m).to(tl.float32)
dt = x_ptr.dtype.element_ty
x = x.to(dt).to(tl.float32)
y = 0.5 * x * (1.0 + tl.erf(x * 0.7071067811865476))
tl.store(x_ptr + row * N + offs, y.to(dt), mask=m)
@triton.jit
def _col2im_kernel(
z_ptr, # [B * T, 2 * R, C] transposed-conv GEMM rows
bias_ptr, # [C]
prev_ptr, # [2, S, R, C] previous call's last GEMM row, second half
slots_ptr,
pos_ptr,
par_ptr,
out_ptr, # [B * T * R, C]
T,
S,
R: tl.constexpr,
C: tl.constexpr,
BC: tl.constexpr,
):
row = tl.program_id(0) # b * T + t
j = tl.program_id(1) # 0 .. R - 1
c = tl.program_id(2) * BC + tl.arange(0, BC)
cm = c < C
b = row // T
t = row % T
slot = tl.load(slots_ptr + b).to(tl.int64)
live = tl.load(pos_ptr + b) != 0
par = tl.load(par_ptr + slot).to(tl.int64)
zr = z_ptr + row.to(tl.int64) * 2 * R * C
cur = tl.load(zr + j * C + c, mask=cm).to(tl.float32)
prev_in = tl.load(zr - 2 * R * C + (R + j) * C + c, mask=cm & (t > 0), other=0.0).to(tl.float32)
prev_st = tl.load(prev_ptr + ((par * S + slot) * R + j) * C + c, mask=cm & (t == 0) & live, other=0.0).to(
tl.float32
)
y = cur + tl.where(t > 0, prev_in, prev_st) + tl.load(bias_ptr + c, mask=cm).to(tl.float32)
dt = out_ptr.dtype.element_ty
tl.store(out_ptr + (row.to(tl.int64) * R + j) * C + c, y.to(dt), mask=cm)
last = tl.load(zr + (R + j) * C + c, mask=cm & (t == T - 1))
tl.store(prev_ptr + (((1 - par) * S + slot) * R + j) * C + c, last, mask=cm & (t == T - 1))
@triton.jit
def _conv_out_kernel(
ext_ptr, # [B, H + T, C] transformed rows: history then this call's
w_ptr, # [K, C]
wb, # conv bias (float)
out_ptr, # [B, T] float32 waveform
T,
C: tl.constexpr,
K: tl.constexpr,
BT: tl.constexpr,
CP: tl.constexpr,
):
b = tl.program_id(0)
t = tl.program_id(1) * BT + tl.arange(0, BT)
c = tl.arange(0, CP)
cm = c < C
tm = t < T
base = ext_ptr + b.to(tl.int64) * (T + K - 1) * C
acc = tl.zeros([BT], dtype=tl.float32)
for j in tl.static_range(K):
v = tl.load(base + (t + j)[:, None] * C + c[None, :], mask=tm[:, None] & cm[None, :], other=0.0).to(tl.float32)
acc += tl.sum(v * tl.load(w_ptr + j * C + c, mask=cm, other=0.0).to(tl.float32)[None, :], axis=1)
dt = ext_ptr.dtype.element_ty
y = acc.to(dt).to(tl.float32) + wb
y = tl.minimum(tl.maximum(y.to(dt).to(tl.float32), -1.0), 1.0)
tl.store(out_ptr + b.to(tl.int64) * T + t, y, mask=tm)
@triton.jit
def _flip_kernel(par_ptr, slots_ptr):
slot = tl.load(slots_ptr + tl.program_id(0)).to(tl.int64)
tl.store(par_ptr + slot, 1 - tl.load(par_ptr + slot))
# --------------------------------------------------------------------------- module
@dataclass
class _ConvSpec:
weight: torch.Tensor # [K * C_in, C_out] tap-major
c_in: int
k: int
d: int
hist: torch.Tensor | None = None # [2, S, H, C_in]
def _conv_taps(w: torch.Tensor) -> torch.Tensor:
"""``[C_out, C_in, K]`` -> ``[K * C_in, C_out]``."""
c_out, c_in, k = w.shape
return w.permute(2, 1, 0).reshape(k * c_in, c_out)
def _snake_params(module) -> tuple[torch.Tensor, torch.Tensor]:
# SnakeBeta: x + 1 / (exp(beta) + 1e-9) * sin(exp(alpha) * x) ** 2
alpha = module.alpha.detach().float()
beta = module.beta.detach().float()
return torch.exp(alpha).contiguous(), (1.0 / (torch.exp(beta) + 1e-9)).contiguous()
class StreamingCodecDecoder(nn.Module):
"""Frame-exact stateful decoder built from a loaded ``Qwen3TTSTokenizerV2Decoder``."""
RING = 128 # KV ring positions; a call may carry up to RING - window + 1 frames
def __init__(self, decoder: nn.Module, num_slots: int, dtype: torch.dtype | None = None) -> None:
super().__init__()
cfg = decoder.config
dev = next(decoder.parameters()).device
dtype = dtype or next(decoder.parameters()).dtype
self.dtype, self.device = dtype, dev
self.num_slots = num_slots + 1 # last slot: scratch for padding rows
self.scratch_slot = num_slots
S = self.num_slots
self.nq = int(cfg.num_quantizers)
self.cb = int(cfg.codebook_size)
self.half = int(cfg.codebook_dim) // 2
self.hidden = int(cfg.hidden_size)
self.heads = int(cfg.num_attention_heads)
self.head_dim = int(cfg.head_dim)
self.window = int(cfg.sliding_window)
self.theta = float(cfg.rope_theta)
self.eps = float(cfg.rms_norm_eps)
self.max_frames = self.RING - self.window + 1
self.spf = int(decoder.total_upsample)
if self.heads != int(cfg.num_key_value_heads):
raise ValueError("streaming codec decoder assumes no grouped KV")
f32 = lambda t: t.detach().to(device=dev, dtype=torch.float32) # noqa: E731
cast = lambda t: t.to(dtype).contiguous() # noqa: E731
def hist(h: int, c: int) -> torch.Tensor:
return torch.zeros(2, S, h, c, device=dev, dtype=dtype)
# RVQ books, pre-divided by usage: [NQ * CB, HALF]
q = decoder.quantizer
books = []
for vq in [q.rvq_first.vq.layers[0]] + list(q.rvq_rest.vq.layers):
cbk = vq._codebook
books.append(f32(cbk.embedding_sum) / f32(cbk.cluster_usage).clamp(min=cbk.epsilon)[:, None])
self.books = cast(torch.cat(books))
w_rvq = torch.cat(
[f32(q.rvq_first.output_proj.weight)[:, :, 0], f32(q.rvq_rest.output_proj.weight)[:, :, 0]], 1
)
# pre_conv (k=3) and input_proj folded onto the embedding concat
pre_w, pre_b = f32(decoder.pre_conv.conv.weight), f32(decoder.pre_conv.conv.bias)
pt = decoder.pre_transformer
in_w, in_b = f32(pt.input_proj.weight), f32(pt.input_proj.bias)
taps = torch.stack([in_w @ pre_w[:, :, j] @ w_rvq for j in range(pre_w.shape[2])], dim=2) # [hid, 2H, 3]
self.pre = _ConvSpec(cast(_conv_taps(taps)), 2 * self.half, 3, 1, hist(2, 2 * self.half))
self.pre_b = cast(in_w @ pre_b + in_b)
# transformer
self.layers = []
qd = self.heads * self.head_dim
for layer in pt.layers:
a = layer.self_attn
qkv = torch.cat([f32(a.q_proj.weight), f32(a.k_proj.weight), f32(a.v_proj.weight)], 0)
o = f32(a.o_proj.weight) * f32(layer.self_attn_layer_scale.scale)[:, None]
gu = torch.cat([f32(layer.mlp.gate_proj.weight), f32(layer.mlp.up_proj.weight)], 0)
down = f32(layer.mlp.down_proj.weight) * f32(layer.mlp_layer_scale.scale)[:, None]
self.layers.append(
dict(
ln1=cast(f32(layer.input_layernorm.weight)),
ln2=cast(f32(layer.post_attention_layernorm.weight)),
qkv=cast(qkv.t()),
o=cast(o.t()),
gu=cast(gu.t()),
down=cast(down.t()),
k_ring=torch.zeros(S, self.RING, qd, device=dev, dtype=dtype),
v_ring=torch.zeros(S, self.RING, qd, device=dev, dtype=dtype),
)
)
self.norm_w = cast(f32(pt.norm.weight))
self.out_w = cast(f32(pt.output_proj.weight).t())
self.out_b = cast(f32(pt.output_proj.bias))
# upsample: transposed conv (kernel = stride, no overlap) + ConvNeXt
self.ups = []
latent = int(cfg.latent_dim)
pending = torch.zeros(latent, device=dev, dtype=torch.float32)
for trans, block in decoder.upsample:
tw = f32(trans.conv.weight) # [C_in, C_out, r]
r = tw.shape[2]
w = tw.permute(0, 2, 1).reshape(latent, r * latent) # [C_in, r * C_out]
bias = (pending @ w).view(r, latent) + f32(trans.conv.bias)[None, :]
dw = block.dwconv.conv
gamma = f32(block.gamma)
self.ups.append(
dict(
r=r,
w=cast(w),
b=cast(bias.reshape(-1)),
dw_w=cast(f32(dw.weight)[:, 0, :].t()),
dw_b=cast(f32(dw.bias)),
ln_w=cast(f32(block.norm.weight)),
ln_b=cast(f32(block.norm.bias)),
ln_eps=float(block.norm.eps),
pw1=cast(f32(block.pwconv1.weight).t()),
pw1_b=cast(f32(block.pwconv1.bias)),
pw2=cast((f32(block.pwconv2.weight) * gamma[:, None]).t()),
hist=hist(_KERNEL - 1, latent),
)
)
pending = f32(block.pwconv2.bias) * gamma
# conv_in (pending bias of the last ConvNeXt added on load)
dec = decoder.decoder
cin = dec[0].conv
self.conv_in = _ConvSpec(cast(_conv_taps(f32(cin.weight))), latent, _KERNEL, 1, hist(_KERNEL - 1, latent))
self.conv_in_pb = cast(pending)
pending = f32(cin.bias)
# decoder blocks
self.blocks = []
for i, blk in enumerate(dec[1:-2]):
snake, trans, *units = blk.block
c_in = trans.conv.in_channels
c_out = trans.conv.out_channels
rate = trans.conv.stride[0]
tw = f32(trans.conv.weight) # [C_in, C_out, 2 * rate]
entry = dict(
pb=cast(pending),
snake=_snake_params(snake),
rate=rate,
c_in=c_in,
c_out=c_out,
w=cast(tw.permute(0, 2, 1).reshape(c_in, 2 * rate * c_out)),
b=cast(f32(trans.conv.bias)),
prev=torch.zeros(2, S, rate, c_out, device=dev, dtype=dtype),
units=[],
)
pending = torch.zeros(c_out, device=dev, dtype=torch.float32)
for u in units:
c1, c2 = u.conv1.conv, u.conv2.conv
entry["units"].append(
dict(
pb=cast(pending),
act1=_snake_params(u.act1),
conv1=_ConvSpec(
cast(_conv_taps(f32(c1.weight))),
c_out,
_KERNEL,
c1.dilation[0],
hist((_KERNEL - 1) * c1.dilation[0], c_out),
),
b1=cast(f32(c1.bias)),
conv1_w4=(
cast(f32(c1.weight)).unsqueeze(2).contiguous(memory_format=torch.channels_last)
if c1.dilation[0] == 1
else None
),
act2=_snake_params(u.act2),
w2=cast(f32(c2.weight)[:, :, 0].t()),
)
)
pending = pending + f32(c2.bias)
self.blocks.append(entry)
self.out_pb = cast(pending)
self.out_snake = _snake_params(dec[-2])
co = dec[-1].conv
self.out_c = co.in_channels
self.conv_out_w = cast(f32(co.weight)[0].t()) # [K, C]
self.conv_out_b = float(f32(co.bias)[0])
self.conv_out = _ConvSpec(self.conv_out_w, self.out_c, _KERNEL, 1, hist(_KERNEL - 1, self.out_c))
self.parity = torch.zeros(S, device=dev, dtype=torch.int32)
self._dummy = torch.zeros(1, device=dev, dtype=dtype)
def _slot_state(self, slot: int) -> list[torch.Tensor]:
state = [self.parity[slot : slot + 1]]
for layer in self.layers:
state.extend((layer["k_ring"][slot], layer["v_ring"][slot]))
for spec in (self.pre, self.conv_in, self.conv_out):
assert spec.hist is not None
state.append(spec.hist[:, slot])
for up in self.ups:
state.append(up["hist"][:, slot])
for block in self.blocks:
state.append(block["prev"][:, slot])
for unit in block["units"]:
state.append(unit["conv1"].hist[:, slot])
return state
def save_slot(self, slot: int) -> list[torch.Tensor]:
"""Offload a preempted request before its runner slot is recycled."""
return [tensor.to(device="cpu", copy=True) for tensor in self._slot_state(slot)]
def restore_slot(self, slot: int, state: list[torch.Tensor]) -> None:
for target, saved in zip(self._slot_state(slot), state, strict=True):
target.copy_(saved)
# ------------------------------------------------------------------ helpers
def _im2col(self, x, spec: _ConvSpec, T, slots, pos, bias=None, snake=None, ext=False):
k = spec.k
d = spec.d
c = x.shape[1]
h = (k - 1) * d
B = slots.shape[0]
if ext:
col = torch.empty(B * (h + T), c, device=x.device, dtype=self.dtype)
else:
col = torch.empty(B * T, k * c, device=x.device, dtype=self.dtype)
bu = 32
bc = min(128, triton.next_power_of_2(c))
grid = (B, triton.cdiv(h + T, bu), triton.cdiv(c, bc))
a, ib = snake if snake is not None else (self._dummy, self._dummy)
_im2col_kernel[grid](
x, bias if bias is not None else self._dummy, a, ib,
spec.hist if h > 0 else self._dummy, slots, pos, self.parity, col,
T, self.num_slots, C=c, K=k, D=d, H=h,
HAS_BIAS=bias is not None, HAS_SNAKE=snake is not None, BU=bu, BC=bc, EXT=ext,
) # fmt: skip
return col
def _conv_cudnn(self, x, u, T, slots, pos):
"""Undilated unit conv: one source-row pass, then cuDNN on its channels-last view.
The im2col path writes a ``K``-times patch matrix; cuDNN reads the
``[history | new]`` rows directly (same result up to summation order).
"""
spec = u["conv1"]
ext = self._im2col(x, spec, T, slots, pos, bias=u["pb"], snake=u["act1"], ext=True)
b = slots.shape[0]
c = x.shape[1]
x4 = ext.view(b, -1, c).permute(0, 2, 1).unsqueeze(2) # [B, C, 1, H + T], channels-last memory
y = torch.nn.functional.conv2d(x4, u["conv1_w4"]) # [B, N, 1, T], channels-last memory
return y.permute(0, 2, 3, 1).reshape(b * T, -1)
def _snake(self, x, snake, bias):
n, c = x.shape
out = torch.empty_like(x)
bu, bc = 32, min(128, triton.next_power_of_2(c))
one = torch.zeros(1, device=x.device, dtype=torch.int32)
_im2col_kernel[(1, triton.cdiv(n, bu), triton.cdiv(c, bc))](
x, bias, snake[0], snake[1], self._dummy, one, one, self.parity, out,
n, self.num_slots, C=c, K=1, D=1, H=0, HAS_BIAS=True, HAS_SNAKE=True, BU=bu, BC=bc,
) # fmt: skip
return out
# ------------------------------------------------------------------ forward
@torch.no_grad()
def forward(self, codes: torch.Tensor, slots: torch.Tensor, pos: torch.Tensor) -> torch.Tensor:
"""``codes`` [B, T, NQ] int, ``slots``/``pos`` [B] int32 -> waveform [B, T * spf] float32.
``pos[b]`` is the index of the row's first frame in its stream; 0 starts
a new stream in ``slots[b]``. Frames beyond ``max_frames`` per call must
be split by the caller.
"""
B, T, _ = codes.shape
if T > self.max_frames:
raise ValueError(f"at most {self.max_frames} frames per call, got {T}")
dev, dt = codes.device, self.dtype
codes = codes.reshape(B * T, self.nq).to(torch.int32).contiguous()
emb = torch.empty(B * T, 2 * self.half, device=dev, dtype=dt)
_rvq_gather_kernel[(B * T,)](codes, self.books, emb, NQ=self.nq, CB=self.cb, HALF=self.half)
x = torch.addmm(self.pre_b, self._im2col(emb, self.pre, T, slots, pos), self.pre.weight)
hid, qd = self.hidden, self.heads * self.head_dim
rows = B * T
h = torch.empty(rows, hid, device=dev, dtype=dt)
q = torch.empty(rows, qd, device=dev, dtype=dt)
att = torch.empty(rows, qd, device=dev, dtype=dt)
a = torch.empty(rows, self.layers[0]["down"].shape[0], device=dev, dtype=dt)
inter = a.shape[1]
scale = self.head_dim**-0.5
for L in self.layers:
_rms_norm_kernel[(rows,)](x, L["ln1"], h, self.eps, N=hid)
qkv = torch.mm(h, L["qkv"])
_rope_kv_write_kernel[(rows, self.heads)](
qkv, q, L["k_ring"], L["v_ring"], slots, pos, T, self.theta,
NH=self.heads, HD=self.head_dim, RING=self.RING,
) # fmt: skip
_attend_kernel[(rows, self.heads)](
q, L["k_ring"], L["v_ring"], att, slots, pos, T, scale,
NH=self.heads, HD=self.head_dim, RING=self.RING, WINDOW=self.window,
BK=triton.next_power_of_2(self.window),
) # fmt: skip
x.addmm_(att, L["o"])
_rms_norm_kernel[(rows,)](x, L["ln2"], h, self.eps, N=hid)
gu = torch.mm(h, L["gu"])
_silu_mul_kernel[(rows, triton.cdiv(inter, 1024))](gu, a, N=inter, BN=1024)
x.addmm_(a, L["down"])
_rms_norm_kernel[(rows,)](x, self.norm_w, h, self.eps, N=hid)
y = torch.addmm(self.out_b, h, self.out_w) # [rows, latent]
t_rows = T
for U in self.ups:
r = U["r"]
z = torch.addmm(U["b"], y, U["w"]).view(rows * r, -1)
rows, t_rows = rows * r, t_rows * r
c = z.shape[1]
n = torch.empty_like(z)
_dwconv_ln_kernel[(B, max(t_rows, _KERNEL - 1))](
z, U["dw_w"], U["dw_b"], U["ln_w"], U["ln_b"], U["hist"], slots, pos, self.parity, n,
t_rows, self.num_slots, U["ln_eps"], C=c, K=_KERNEL,
) # fmt: skip
p1 = torch.mm(n, U["pw1"])
_bias_gelu_kernel[(rows, triton.cdiv(p1.shape[1], 1024))](p1, U["pw1_b"], N=p1.shape[1], BN=1024)
z.addmm_(p1, U["pw2"])
y = z
x = torch.mm(self._im2col(y, self.conv_in, t_rows, slots, pos, bias=self.conv_in_pb), self.conv_in.weight)
for blk in self.blocks:
s = self._snake(x, blk["snake"], blk["pb"])
zt = torch.mm(s, blk["w"]) # [rows, 2 * rate * c_out]
r, c_out = blk["rate"], blk["c_out"]
x = torch.empty(rows * r, c_out, device=dev, dtype=dt)
bc = min(128, triton.next_power_of_2(c_out))
_col2im_kernel[(rows, r, triton.cdiv(c_out, bc))](
zt, blk["b"], blk["prev"], slots, pos, self.parity, x, t_rows, self.num_slots,
R=r, C=c_out, BC=bc,
) # fmt: skip
rows, t_rows = rows * r, t_rows * r
for u in blk["units"]:
if u.get("conv1_w4") is not None:
c1 = self._conv_cudnn(x, u, t_rows, slots, pos)
else:
col = self._im2col(x, u["conv1"], t_rows, slots, pos, bias=u["pb"], snake=u["act1"])
c1 = torch.mm(col, u["conv1"].weight)
s2 = self._snake(c1, u["act2"], u["b1"])
x.addmm_(s2, u["w2"])
ext = self._im2col(x, self.conv_out, t_rows, slots, pos, bias=self.out_pb, snake=self.out_snake, ext=True)
wav = torch.empty(B, t_rows, device=dev, dtype=torch.float32)
_conv_out_kernel[(B, triton.cdiv(t_rows, 256))](
ext, self.conv_out_w, self.conv_out_b, wav, t_rows,
C=self.out_c, K=_KERNEL, BT=256, CP=triton.next_power_of_2(self.out_c),
) # fmt: skip
_flip_kernel[(B,)](self.parity, slots)
return wav
class StreamingDecodeGraphs:
"""CUDA graphs of ``StreamingCodecDecoder`` for a fixed frame count per batch bucket."""
def __init__(self, sd: StreamingCodecDecoder, batch_sizes: list[int], frames: int = 1) -> None:
self.sd, self.frames = sd, frames
self.graphs: dict[int, tuple] = {}
dev = sd.device
pool = torch.cuda.graph_pool_handle()
for bsz in sorted(set(batch_sizes)):
codes = torch.zeros(bsz, frames, sd.nq, dtype=torch.int32, device=dev)
slots = torch.full((bsz,), sd.scratch_slot, dtype=torch.int32, device=dev)
pos = torch.zeros(bsz, dtype=torch.int32, device=dev)
sd(codes, slots, pos)
current_omni_platform.synchronize()
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph, pool=pool):
out = sd(codes, slots, pos)
self.graphs[bsz] = (graph, codes, slots, pos, out)
self.sizes = sorted(self.graphs)
def __call__(self, codes: torch.Tensor, slots: torch.Tensor, pos: torch.Tensor) -> torch.Tensor:
"""``codes`` [n, frames, NQ], ``slots``/``pos`` [n] on device -> waveform view [n, frames * spf]."""
n = int(codes.shape[0])
size = next((b for b in self.sizes if b >= n), None)
if size is None:
return self.sd(codes, slots.to(torch.int32), pos.to(torch.int32))
graph, s_codes, s_slots, s_pos, out = self.graphs[size]
s_codes[:n].copy_(codes)
s_slots[:n].copy_(slots)
s_pos[:n].copy_(pos)
if n < size:
s_slots[n:].fill_(self.sd.scratch_slot)
s_pos[n:].zero_()
graph.replay()
return out[:n]
@@ -483,6 +483,12 @@ _OMNI_MODELS = {
"acoustic",
"MiniMaxMusic3AcousticForConditionalGeneration",
),
## YuE2-3B (text-to-music; single-stage native-AR with terminal NAR/VAE)
"Yue2ForCausalLM": (
"yue2",
"yue2",
"Yue2ForCausalLM",
),
}
Loaded 100 of 252 files, more files were not shown because too many files have changed in this diff. Show more