feat(qwen3-tts): Add CUDA Graph support for speech tokenizer decoder (#1205)
Signed-off-by: xulusjb <fdukeshik@gmail.com> Co-authored-by: xulusjb <fdukeshik@gmail.com>
This commit is contained in:
1 parent
443b80e06c
commit
c9b31a4c1a
5 files changed
+505
No files matched your search
@@ -0,0 +1,169 @@
|
||||
# Copyright 2026 The Alibaba Qwen team.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
"""
|
||||
CUDA Graph wrapper for Qwen3TTSTokenizerV2Decoder.
|
||||
|
||||
This module provides CUDA Graph acceleration for the speech tokenizer decoder,
|
||||
reducing kernel launch overhead during inference.
|
||||
"""
|
||||
|
||||
import torch
|
||||
from torch.cuda import CUDAGraph
|
||||
from vllm.logger import init_logger
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class CUDAGraphDecoderWrapper:
|
||||
"""
|
||||
CUDA Graph wrapper for Qwen3TTSTokenizerV2Decoder.
|
||||
|
||||
This wrapper captures the decoder forward pass for fixed input sizes
|
||||
and replays them during inference to reduce kernel launch overhead.
|
||||
|
||||
Usage:
|
||||
wrapper = CUDAGraphDecoderWrapper(decoder, capture_sizes=[25, 50, 100, 200, 300])
|
||||
wrapper.warmup(device)
|
||||
|
||||
# During inference:
|
||||
output = wrapper.decode(codes) # Automatically uses CUDA graph if possible
|
||||
"""
|
||||
|
||||
DEFAULT_CAPTURE_SIZES = [25, 50, 100, 150, 200, 250, 300]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
decoder: torch.nn.Module,
|
||||
capture_sizes: list[int] | None = None,
|
||||
num_quantizers: int = 8,
|
||||
enabled: bool = True,
|
||||
):
|
||||
self.decoder = decoder
|
||||
self.capture_sizes = capture_sizes or self.DEFAULT_CAPTURE_SIZES
|
||||
self.num_quantizers = num_quantizers
|
||||
self.enabled = enabled
|
||||
|
||||
self.graphs: dict[int, CUDAGraph] = {}
|
||||
self.static_inputs: dict[int, torch.Tensor] = {}
|
||||
self.static_outputs: dict[int, torch.Tensor] = {}
|
||||
|
||||
self._warmed_up = False
|
||||
self._device = None
|
||||
|
||||
def _get_padded_size(self, actual_size: int) -> int | None:
|
||||
for size in self.capture_sizes:
|
||||
if actual_size <= size:
|
||||
return size
|
||||
return None
|
||||
|
||||
def warmup(self, device: torch.device, dtype: torch.dtype = torch.long):
|
||||
if device.type != "cuda":
|
||||
logger.info("CUDA Graph warmup skipped: device %s is not CUDA", device)
|
||||
return
|
||||
|
||||
if not self.enabled:
|
||||
logger.info("CUDA Graph is disabled, skipping warmup")
|
||||
return
|
||||
|
||||
if self._warmed_up:
|
||||
logger.warning("CUDA Graph already warmed up, skipping")
|
||||
return
|
||||
|
||||
self._device = device
|
||||
self.decoder.eval()
|
||||
|
||||
logger.info("Starting CUDA Graph warmup for %d sizes: %s", len(self.capture_sizes), self.capture_sizes)
|
||||
|
||||
# Warmup runs to ensure CUDA memory is allocated
|
||||
for size in self.capture_sizes:
|
||||
dummy_codes = torch.zeros(
|
||||
1,
|
||||
self.num_quantizers,
|
||||
size,
|
||||
dtype=dtype,
|
||||
device=device,
|
||||
)
|
||||
with torch.no_grad():
|
||||
_ = self.decoder(dummy_codes)
|
||||
|
||||
torch.cuda.synchronize(device)
|
||||
|
||||
for size in self.capture_sizes:
|
||||
try:
|
||||
self._capture_graph_for_size(size, device, dtype)
|
||||
logger.info(" Captured CUDA Graph for size=%d", size)
|
||||
except Exception:
|
||||
logger.warning(" Failed to capture CUDA Graph for size=%d", size, exc_info=True)
|
||||
|
||||
self._warmed_up = True
|
||||
logger.info("CUDA Graph warmup complete. Captured %d graphs.", len(self.graphs))
|
||||
|
||||
def _capture_graph_for_size(self, size: int, device: torch.device, dtype: torch.dtype):
|
||||
static_input = torch.zeros(
|
||||
1,
|
||||
self.num_quantizers,
|
||||
size,
|
||||
dtype=dtype,
|
||||
device=device,
|
||||
)
|
||||
|
||||
with torch.no_grad():
|
||||
_ = self.decoder(static_input)
|
||||
|
||||
torch.cuda.synchronize(device)
|
||||
|
||||
graph = CUDAGraph()
|
||||
with torch.no_grad():
|
||||
with torch.cuda.graph(graph):
|
||||
static_output = self.decoder(static_input)
|
||||
|
||||
self.graphs[size] = graph
|
||||
self.static_inputs[size] = static_input
|
||||
self.static_outputs[size] = static_output
|
||||
|
||||
def decode(self, codes: torch.Tensor) -> torch.Tensor:
|
||||
if not self.enabled or not self._warmed_up:
|
||||
return self.decoder(codes)
|
||||
|
||||
if codes.shape[0] != 1:
|
||||
return self.decoder(codes)
|
||||
|
||||
actual_size = codes.shape[-1]
|
||||
padded_size = self._get_padded_size(actual_size)
|
||||
|
||||
if padded_size is None or padded_size not in self.graphs:
|
||||
return self.decoder(codes)
|
||||
|
||||
self.static_inputs[padded_size].zero_()
|
||||
self.static_inputs[padded_size][:, :, :actual_size] = codes
|
||||
|
||||
self.graphs[padded_size].replay()
|
||||
|
||||
output = self.static_outputs[padded_size]
|
||||
total_upsample = self.decoder.total_upsample
|
||||
actual_output_len = actual_size * total_upsample
|
||||
|
||||
return output[..., :actual_output_len].clone()
|
||||
|
||||
def chunked_decode_with_cudagraph(
|
||||
self,
|
||||
codes: torch.Tensor,
|
||||
chunk_size: int = 300,
|
||||
left_context_size: int = 25,
|
||||
) -> torch.Tensor:
|
||||
wavs = []
|
||||
start_index = 0
|
||||
total_len = codes.shape[-1]
|
||||
total_upsample = self.decoder.total_upsample
|
||||
|
||||
while start_index < total_len:
|
||||
end_index = min(start_index + chunk_size, total_len)
|
||||
context_size = left_context_size if start_index - left_context_size > 0 else start_index
|
||||
|
||||
codes_chunk = codes[..., start_index - context_size : end_index]
|
||||
wav_chunk = self.decode(codes_chunk)
|
||||
|
||||
wavs.append(wav_chunk[..., context_size * total_upsample :])
|
||||
start_index = end_index
|
||||
|
||||
return torch.cat(wavs, dim=-1)
|
||||
@@ -100,6 +100,33 @@ class Qwen3TTSModelForGeneration(nn.Module):
|
||||
# Store vllm_config for potential future use
|
||||
self.vllm_config = vllm_config
|
||||
|
||||
# Enable CUDA Graph for decoder
|
||||
self._enable_decoder_cudagraph()
|
||||
|
||||
def _enable_decoder_cudagraph(self):
|
||||
# Respect --enforce-eager flag
|
||||
model_cfg = getattr(self.vllm_config, "model_config", None)
|
||||
if model_cfg and getattr(model_cfg, "enforce_eager", False):
|
||||
logger.info("CUDA Graph not enabled: --enforce-eager is set")
|
||||
return
|
||||
try:
|
||||
inner_model = getattr(self.model, "model", None)
|
||||
if inner_model is None or not hasattr(inner_model, "speech_tokenizer"):
|
||||
return
|
||||
tokenizer = inner_model.speech_tokenizer
|
||||
if not (hasattr(tokenizer, "model") and hasattr(tokenizer.model, "decoder")):
|
||||
return
|
||||
decoder = tokenizer.model.decoder
|
||||
device = next(decoder.parameters()).device
|
||||
if device.type != "cuda":
|
||||
logger.info("CUDA Graph not enabled: decoder is on %s", device)
|
||||
return
|
||||
if hasattr(decoder, "enable_cudagraph"):
|
||||
decoder.enable_cudagraph()
|
||||
logger.info("CUDA Graph enabled for speech tokenizer decoder")
|
||||
except Exception:
|
||||
logger.warning("Failed to enable CUDA Graph for decoder", exc_info=True)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
input_ids: torch.Tensor | None = None,
|
||||
|
||||
+37
@@ -848,6 +848,38 @@ class Qwen3TTSTokenizerV2Decoder(Qwen3TTSTokenizerV2DecoderPreTrainedModel):
|
||||
|
||||
self.post_init()
|
||||
|
||||
# CUDA Graph support
|
||||
self._cudagraph_enabled = False
|
||||
self._cudagraph_wrapper = None
|
||||
|
||||
def enable_cudagraph(
|
||||
self,
|
||||
capture_sizes: list[int] | None = None,
|
||||
device: torch.device | None = None,
|
||||
):
|
||||
from ..cuda_graph_decoder_wrapper import CUDAGraphDecoderWrapper
|
||||
|
||||
if device is None:
|
||||
device = next(self.parameters()).device
|
||||
if device.type != "cuda":
|
||||
logger.warning("Cannot enable CUDA Graph: decoder is not on a CUDA device (got %s)", device)
|
||||
return
|
||||
self._cudagraph_wrapper = CUDAGraphDecoderWrapper(
|
||||
decoder=self,
|
||||
capture_sizes=capture_sizes,
|
||||
num_quantizers=self.config.num_quantizers,
|
||||
enabled=True,
|
||||
)
|
||||
self._cudagraph_wrapper.warmup(device, dtype=torch.long)
|
||||
self._cudagraph_enabled = True
|
||||
sizes = self._cudagraph_wrapper.capture_sizes
|
||||
logger.info("CUDA Graph enabled for decoder with sizes: %s", sizes)
|
||||
|
||||
def disable_cudagraph(self):
|
||||
self._cudagraph_enabled = False
|
||||
self._cudagraph_wrapper = None
|
||||
logger.info("CUDA Graph disabled for decoder")
|
||||
|
||||
def forward(self, codes):
|
||||
if codes.shape[1] != self.config.num_quantizers:
|
||||
raise ValueError(f"Expected {self.config.num_quantizers} layer of codes, got {codes.shape[1]}")
|
||||
@@ -866,6 +898,11 @@ class Qwen3TTSTokenizerV2Decoder(Qwen3TTSTokenizerV2DecoderPreTrainedModel):
|
||||
return wav.clamp(min=-1, max=1)
|
||||
|
||||
def chunked_decode(self, codes, chunk_size=300, left_context_size=25):
|
||||
# Use CUDA graph if enabled
|
||||
if self._cudagraph_enabled and self._cudagraph_wrapper is not None:
|
||||
return self._cudagraph_wrapper.chunked_decode_with_cudagraph(codes, chunk_size, left_context_size)
|
||||
|
||||
# Original implementation (eager mode)
|
||||
wavs = []
|
||||
start_index = 0
|
||||
while start_index < codes.shape[-1]:
|
||||
|
||||
Reference in new issue
Block a user