[Perf] Enable async schedule for MOSS-TTS (#6241)
Signed-off-by: Sy03 <1370724210@qq.com>
This commit is contained in:
8 files changed
+278
-23
No files matched your search
@@ -128,32 +128,45 @@ def _sample_token(
|
||||
do_sample: bool,
|
||||
generator: torch.Generator | None = None,
|
||||
) -> torch.Tensor:
|
||||
"""Top-k + top-p sampling (matches upstream's ``sample_token`` for the
|
||||
inference branch).
|
||||
"""Top-k + top-p sampling for the upstream inference branch.
|
||||
|
||||
Nucleus filtering and multinomial sampling operate only on the retained
|
||||
top-k candidates before the result is mapped back to the original
|
||||
vocabulary. This preserves the categorical distribution when the top-k
|
||||
boundary has no ties, but it is not seed/bit equivalent to multinomial over
|
||||
a full-vocabulary tensor because random-number mapping depends on width.
|
||||
"""
|
||||
if not do_sample or temperature <= 0:
|
||||
return logits.argmax(dim=-1)
|
||||
|
||||
logits = logits / max(temperature, 1e-6)
|
||||
compact_indices = None
|
||||
if top_k and top_k > 0 and top_k < logits.shape[-1]:
|
||||
top_vals, _ = torch.topk(logits, top_k, dim=-1)
|
||||
thresh = top_vals[..., -1:].expand_as(logits)
|
||||
logits = torch.where(logits < thresh, torch.full_like(logits, float("-inf")), logits)
|
||||
top_vals, top_indices = torch.topk(logits, top_k, dim=-1, sorted=True)
|
||||
# topk is already descending: keep the following nucleus filter,
|
||||
# softmax, and multinomial at width k instead of the full vocab.
|
||||
logits = top_vals
|
||||
compact_indices = top_indices
|
||||
|
||||
if 0.0 < top_p < 1.0:
|
||||
sorted_logits, sorted_idx = torch.sort(logits, descending=True, dim=-1)
|
||||
probs = F.softmax(sorted_logits, dim=-1)
|
||||
sorted_indices = None
|
||||
if compact_indices is None:
|
||||
logits, sorted_indices = torch.sort(logits, descending=True, dim=-1)
|
||||
probs = F.softmax(logits, dim=-1)
|
||||
cum = probs.cumsum(dim=-1)
|
||||
# Drop tail beyond top_p (keep at least one token).
|
||||
drop = cum > top_p
|
||||
drop[..., 1:] = drop[..., :-1].clone()
|
||||
drop[..., 0] = False
|
||||
sorted_logits = sorted_logits.masked_fill(drop, float("-inf"))
|
||||
logits = torch.full_like(logits, float("-inf")).scatter_(-1, sorted_idx, sorted_logits)
|
||||
logits = logits.masked_fill(drop, float("-inf"))
|
||||
if sorted_indices is not None:
|
||||
logits = torch.full_like(logits, float("-inf")).scatter_(-1, sorted_indices, logits)
|
||||
|
||||
probs = F.softmax(logits, dim=-1)
|
||||
flat = probs.reshape(-1, probs.shape[-1])
|
||||
sampled = torch.multinomial(flat, num_samples=1, generator=generator).reshape(probs.shape[:-1])
|
||||
if compact_indices is not None:
|
||||
return compact_indices.gather(-1, sampled.unsqueeze(-1)).squeeze(-1)
|
||||
return sampled
|
||||
|
||||
|
||||
|
||||
@@ -52,15 +52,48 @@ class _MossTTSLocalAttention(nn.Module):
|
||||
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))
|
||||
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
||||
self.register_buffer("_rope_cos_cache", torch.empty(0), persistent=False)
|
||||
self.register_buffer("_rope_sin_cache", torch.empty(0), persistent=False)
|
||||
|
||||
def _rope_cos_sin(
|
||||
self, seq_len: int, device: torch.device, dtype: torch.dtype
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
position_ids = torch.arange(seq_len, device=device, dtype=torch.float32)
|
||||
freqs = torch.einsum("s,d->sd", position_ids, self.inv_freq.to(device=device))
|
||||
def prepare_rope_cache(
|
||||
self,
|
||||
max_seq_len: int,
|
||||
device: torch.device,
|
||||
dtype: torch.dtype,
|
||||
) -> None:
|
||||
"""Materialize frame-local RoPE values once per device/dtype.
|
||||
|
||||
Local Depth positions always restart at zero and never exceed the
|
||||
number of RVQ codebooks (12 for MOSS-TTS Local v1.5). Building
|
||||
arange/einsum/cos/sin inside every one of the 12 depth steps creates
|
||||
many tiny kernels and also bloats the enclosing talker CUDA graph.
|
||||
Keep one non-persistent cache and slice it for prefix execution.
|
||||
"""
|
||||
if max_seq_len <= 0:
|
||||
return
|
||||
cache = self._rope_cos_cache
|
||||
if cache.numel() > 0 and cache.device == device and cache.dtype == dtype and int(cache.shape[1]) >= max_seq_len:
|
||||
return
|
||||
|
||||
position_ids = torch.arange(max_seq_len, device=device, dtype=torch.float32)
|
||||
inv_freq = self.inv_freq.to(device=device, dtype=torch.float32)
|
||||
freqs = torch.einsum("s,d->sd", position_ids, inv_freq)
|
||||
cos = freqs.cos().repeat_interleave(2, dim=-1).to(dtype)
|
||||
sin = freqs.sin().repeat_interleave(2, dim=-1).to(dtype)
|
||||
return cos.view(1, seq_len, 1, self.head_dim), sin.view(1, seq_len, 1, self.head_dim)
|
||||
self._rope_cos_cache = cos.view(1, max_seq_len, 1, self.head_dim)
|
||||
self._rope_sin_cache = sin.view(1, max_seq_len, 1, self.head_dim)
|
||||
|
||||
def _rope_cos_sin(
|
||||
self,
|
||||
seq_len: int,
|
||||
device: torch.device,
|
||||
dtype: torch.dtype,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
self.prepare_rope_cache(seq_len, device, dtype)
|
||||
return (
|
||||
self._rope_cos_cache[:, :seq_len],
|
||||
self._rope_sin_cache[:, :seq_len],
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _rotate_half(x: torch.Tensor) -> torch.Tensor:
|
||||
@@ -200,9 +233,15 @@ class MossTTSLocalDepthTransformer(nn.Module):
|
||||
batch_size = backbone_last_hidden.shape[0]
|
||||
dtype = self.ln_f.weight.dtype
|
||||
|
||||
# Populate the complete [0, n_vq) table before torch.compile traces
|
||||
# _forward_prefix or the runner captures talker_mtp. The compiled /
|
||||
# captured body then contains only fixed-address cache slices rather
|
||||
# than cache construction or buffer replacement.
|
||||
for block in self.h:
|
||||
block.attn.prepare_rope_cache(n_vq, backbone_last_hidden.device, dtype)
|
||||
|
||||
embeds = backbone_last_hidden.new_zeros((batch_size, n_vq, self.hidden_size), dtype=dtype)
|
||||
embeds[:, 0, :] = backbone_last_hidden.to(dtype)
|
||||
|
||||
hidden = self._run_prefix(embeds[:, :1, :])
|
||||
local_hidden = hidden[:, 0, :]
|
||||
|
||||
|
||||
@@ -1323,7 +1323,11 @@ class MossTTSLocalTalkerForGeneration(nn.Module):
|
||||
self.mtp_hidden_size = hidden_size
|
||||
self.talker_mtp_graph_safe = True
|
||||
self.talker_mtp_output_key = ("audio_codes", "current")
|
||||
self.use_async_omni_output = False
|
||||
# ``make_omni_output`` keeps code rows fixed-shape and performs all
|
||||
# state updates eagerly, so the runner can safely pack/snapshot them
|
||||
# before CUDA-graph buffers are reused. This moves the pinned D2H and
|
||||
# output materialization off the Stage-0 execution thread.
|
||||
self.use_async_omni_output = True
|
||||
self.eager_omni_postprocess_before_async_output = True
|
||||
self.omni_pooler_payload_include_hidden = False
|
||||
self.postprocess_uses_multimodal_outputs = False
|
||||
@@ -1466,7 +1470,7 @@ class MossTTSLocalTalkerForGeneration(nn.Module):
|
||||
) -> tuple[torch.Tensor, torch.Tensor, dict[str, Any]]:
|
||||
"""Build per-step input embeddings (text + audio additive fusion).
|
||||
|
||||
Prefill: initialise the per-request ``{is_stopping, step}`` state.
|
||||
Prefill: initialise the per-request ``{is_stopping}`` state.
|
||||
Decode: combine the forced text token (from ``compute_logits``) with
|
||||
the audio codes the local transformer produced last step.
|
||||
"""
|
||||
@@ -1474,8 +1478,9 @@ class MossTTSLocalTalkerForGeneration(nn.Module):
|
||||
span_len = int(input_ids.shape[0])
|
||||
audio_state = info_dict.get("audio_state")
|
||||
is_first_call = not isinstance(audio_state, dict)
|
||||
is_prefill = bool(info_dict.get("_omni_is_prefill", False))
|
||||
|
||||
if span_len > 1 or is_first_call:
|
||||
if span_len > 1 or is_first_call or is_prefill:
|
||||
embeds = self.model.embed_tokens(input_ids)
|
||||
|
||||
ref_codes = (info_dict.get("codes", {}) or {}).get("ref")
|
||||
@@ -1668,9 +1673,14 @@ class MossTTSLocalTalkerForGeneration(nn.Module):
|
||||
should_continue = codes.ne(self.audio_pad_token_id).any(dim=-1)
|
||||
should_values.append(should_continue.reshape(-1)[:1])
|
||||
have_mtp_output = True
|
||||
emitted_codes = codes[should_continue]
|
||||
per_req_codes.append(emitted_codes)
|
||||
have_codes = have_codes or emitted_codes.numel() > 0
|
||||
# Do not boolean-index a CUDA tensor here. Even for the
|
||||
# common single-row case, ``codes[should_continue]`` must
|
||||
# discover a dynamic output size on the host and therefore
|
||||
# synchronizes the CUDA stream once per request. Keep the
|
||||
# fixed-shape row (including an all-pad stop row); the async
|
||||
# chunk processor already filters all-pad rows on CPU.
|
||||
per_req_codes.append(codes)
|
||||
have_codes = True
|
||||
else:
|
||||
should_values.append(torch.ones((1,), device=hidden.device, dtype=torch.bool))
|
||||
per_req_codes.append(hidden.new_empty((0, self.n_vq), dtype=torch.long))
|
||||
|
||||
Reference in new issue
Block a user