[Bugfix] MOSS-TTS: fix cross-request audio corruption under batching (#4415)
Signed-off-by: leo.yang <leo.yang.engineer@gmail.com> Co-authored-by: Yueqian Lin <70319226+linyueqian@users.noreply.github.com>
This commit is contained in:
1 parent
8f28b969e9
commit
1228a109f0
3 files changed
+245
-24
No files matched your search
@@ -553,7 +553,19 @@ class MossTTSDelayTalkerForGeneration(nn.Module):
|
||||
# rows align with hidden rows, which align with info_dicts in order.
|
||||
self._batch_state = [(info["audio_state"] if isinstance(info, dict) else {}) for info in info_dicts]
|
||||
|
||||
audio_codes_list: list[torch.Tensor] = []
|
||||
# Per-request (start, end) hidden-row spans from the runner. In mixed
|
||||
# prefill+decode steps the per-request token counts differ, so an equal
|
||||
# rows-per-request split would sample codes from the wrong rows; fall
|
||||
# back to that split only when spans are unavailable.
|
||||
spans = kwargs.get("request_token_spans")
|
||||
|
||||
# One accumulated-codes tensor per request, in batch order. The generic
|
||||
# splitter (``to_payload_element``) routes ``per_req_codes[idx]`` to
|
||||
# request ``idx``, so Stage 1 decodes each request's own codes instead
|
||||
# of request 0's. Skipped requests get an empty placeholder so indices
|
||||
# stay aligned (a shorter list would silently fall back to entry 0).
|
||||
per_req_codes: list[torch.Tensor] = []
|
||||
have_codes = False
|
||||
|
||||
if hidden.numel() > 0:
|
||||
num_rows = hidden.shape[0]
|
||||
@@ -561,10 +573,16 @@ class MossTTSDelayTalkerForGeneration(nn.Module):
|
||||
|
||||
for i, info in enumerate(info_dicts):
|
||||
if not isinstance(info, dict):
|
||||
per_req_codes.append(hidden.new_empty((0, self.n_vq), dtype=torch.long))
|
||||
continue
|
||||
row_start = i * rows_per_req
|
||||
row_end = min(row_start + rows_per_req, num_rows)
|
||||
if spans is not None and i < len(spans):
|
||||
row_start, row_end = spans[i]
|
||||
row_end = min(int(row_end), num_rows)
|
||||
else:
|
||||
row_start = i * rows_per_req
|
||||
row_end = min(row_start + rows_per_req, num_rows)
|
||||
if row_start >= row_end:
|
||||
per_req_codes.append(hidden.new_empty((0, self.n_vq), dtype=torch.long))
|
||||
continue
|
||||
|
||||
# Sample new audio codes from the last hidden state of this request.
|
||||
@@ -582,21 +600,20 @@ class MossTTSDelayTalkerForGeneration(nn.Module):
|
||||
"current": new_codes,
|
||||
"accumulated": updated_acc,
|
||||
}
|
||||
audio_codes_list.append(updated_acc)
|
||||
per_req_codes.append(updated_acc)
|
||||
have_codes = True
|
||||
|
||||
if not audio_codes_list:
|
||||
if not have_codes:
|
||||
return OmniOutput(
|
||||
text_hidden_states=hidden,
|
||||
multimodal_outputs={},
|
||||
)
|
||||
|
||||
# Forward the accumulated audio codes (T, NQ) of the first request to
|
||||
# Stage 1. Multi-request batching for the codec is not yet supported
|
||||
# by this adapter; the existing code already takes the first stage
|
||||
# output anyway.
|
||||
# Emit one accumulated-codes tensor per request as a list; the splitter
|
||||
# picks ``element[idx]`` per request on the way to Stage 1.
|
||||
return OmniOutput(
|
||||
text_hidden_states=hidden,
|
||||
multimodal_outputs={"codes": {"audio": audio_codes_list[0]}},
|
||||
multimodal_outputs={"codes": {"audio": per_req_codes}},
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
@@ -933,15 +950,27 @@ class MossTTSRealtimeTalkerForGeneration(nn.Module):
|
||||
|
||||
self._batch_state = [(info["audio_state"] if isinstance(info, dict) else {}) for info in info_dicts]
|
||||
|
||||
audio_codes_list: list[torch.Tensor] = []
|
||||
# See delay talker: real per-request row spans from the runner, with the
|
||||
# equal-split as fallback when unavailable.
|
||||
spans = kwargs.get("request_token_spans")
|
||||
# Per-request accumulated codes in batch order, pre-filled with empty
|
||||
# placeholders so every skip path (stopped / eos / bos / pad) keeps
|
||||
# indices aligned for to_payload_element's per-request routing. A
|
||||
# shorter list would silently fall back to request 0's codes.
|
||||
per_req_codes: list[torch.Tensor] = [hidden.new_empty((0, self.n_vq), dtype=torch.long) for _ in info_dicts]
|
||||
have_codes = False
|
||||
if hidden.numel() > 0 and info_dicts:
|
||||
num_rows = hidden.shape[0]
|
||||
rows_per_req = max(1, num_rows // max(1, len(info_dicts) or 1))
|
||||
for i, info in enumerate(info_dicts):
|
||||
if not isinstance(info, dict):
|
||||
continue
|
||||
row_start = i * rows_per_req
|
||||
row_end = min(row_start + rows_per_req, num_rows)
|
||||
if spans is not None and i < len(spans):
|
||||
row_start, row_end = spans[i]
|
||||
row_end = min(int(row_end), num_rows)
|
||||
else:
|
||||
row_start = i * rows_per_req
|
||||
row_end = min(row_start + rows_per_req, num_rows)
|
||||
if row_start >= row_end:
|
||||
continue
|
||||
|
||||
@@ -1008,23 +1037,25 @@ class MossTTSRealtimeTalkerForGeneration(nn.Module):
|
||||
|
||||
info["audio_codes"] = {"current": new_codes, "accumulated": updated_acc}
|
||||
state["step"] = int(state.get("step", 0)) + 1
|
||||
audio_codes_list.append(updated_acc)
|
||||
per_req_codes[i] = updated_acc
|
||||
have_codes = True
|
||||
|
||||
if not audio_codes_list:
|
||||
if not have_codes:
|
||||
return OmniOutput(text_hidden_states=hidden, multimodal_outputs={})
|
||||
|
||||
# Forward the first request's accumulated codes (multi-request
|
||||
# batching for the codec is not yet wired up — same as delay).
|
||||
# The realtime variant emits raw codes (no delay pattern), so we
|
||||
# signal that to the chunk processor via a 1-D bool tensor in
|
||||
# ``meta.finished`` (already a tensor field on the schema). The
|
||||
# processor reads its truth value as "skip de-delay" only for
|
||||
# realtime; the delay path doesn't populate ``meta`` at all.
|
||||
device = audio_codes_list[0].device
|
||||
# Emit one accumulated-codes tensor per request as a list; the splitter
|
||||
# routes ``element[idx]`` to request idx, so each Stage-1 request decodes
|
||||
# its own codes instead of request 0's.
|
||||
# The realtime variant emits raw codes (no delay pattern), so we signal
|
||||
# that to the chunk processor via a 1-D bool tensor in ``meta.finished``
|
||||
# (already a tensor field on the schema). The processor reads its truth
|
||||
# value as "skip de-delay" only for realtime; the delay path doesn't
|
||||
# populate ``meta`` at all.
|
||||
device = hidden.device
|
||||
return OmniOutput(
|
||||
text_hidden_states=hidden,
|
||||
multimodal_outputs={
|
||||
"codes": {"audio": audio_codes_list[0]},
|
||||
"codes": {"audio": per_req_codes},
|
||||
"meta": {"finished": torch.tensor([True], dtype=torch.bool, device=device)},
|
||||
},
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user