[Edge][WP5] Accumulate per-step codec delta rows (DELTA mode delivers only the newest row)

Measured after the DELTA fix: 53 engine outputs for 52 rows, but the client
still received everything at once because each per-step payload is the newest
row only and the row alignment treated it as a cumulative snapshot (one yield
at the first row, one at the end). accumulate_codec_rows folds delta rows and
the final consolidated matrix into one growing matrix; unit test drives the
generator with per-step deltas and asserts one yield per step.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
tzhouamandClaude Fable 5.1 committed 2026-09-09 16:45:24 +08:00
1 parent 380e8022dd
commit c893b638ad
3 files changed
+103 -8

No files matched your search

@@ -77,6 +77,38 @@ def unpack_codec_frame(buf: bytes, codebooks: int) -> tuple[int, int, int, np.nd
return seq, n_frames, flags, arr
def accumulate_codec_rows(acc: np.ndarray | None, step: Any) -> np.ndarray:
"""Fold a per-step ``codes.audio`` payload into the growing ``[rows, codebooks]`` matrix.
DELTA-mode outputs carry only the newest row(s); the final consolidated
output (and CUMULATIVE mode) carries the whole matrix, possibly with a
leading placeholder row the deltas never had. A payload whose tail equals
the rows already accumulated (and that is longer) replaces the matrix; an
identical payload is a duplicate; anything else is appended.
"""
if hasattr(step, "detach"):
step = step.detach().cpu().numpy()
arr = np.asarray(step)
if arr.ndim == 1:
arr = arr.reshape(1, -1)
if arr.ndim != 2 or arr.size == 0:
return acc if acc is not None else np.zeros((0, arr.shape[-1] if arr.ndim >= 1 else 0), dtype=np.int64)
arr = arr.astype(np.int64, copy=False)
if acc is None or acc.size == 0:
return arr
n = acc.shape[0]
if arr.shape[0] > n and arr.shape[1] == acc.shape[1]:
# A longer payload that contains the accumulated rows as a contiguous
# block is a cumulative snapshot (it may add a placeholder row in
# front and/or newer rows behind): replace.
for i in range(arr.shape[0] - n + 1):
if np.array_equal(arr[i : i + n], acc):
return arr
if arr.shape == acc.shape and np.array_equal(arr, acc):
return acc
return np.concatenate([acc, arr], axis=0)
def align_codec_rows(codes_cum: Any, token_ids_cum: list[int], codebook_size: int) -> np.ndarray:
"""Keep the codec rows that correspond to real codebook-0 tokens.
+13 -8
View File
@@ -3316,19 +3316,23 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
):
"""Yield ``(codes[int64, n, codebooks], eos)`` deltas from a talker-only pipeline.
The AR stage publishes the *cumulative* ``codes.audio`` tensor on every
step (``omni_client_multimodal_output_keys``); this generator aligns the
rows to the output token ids (dropping the prefill placeholder and the
EOS row) and emits only the new rows. The final item always carries
``eos=True`` (possibly with zero frames).
The AR stage publishes ``codes.audio`` on every step
(``omni_client_multimodal_output_keys``): in DELTA output mode only the
newest row(s), and a consolidated cumulative matrix on the final
output. ``accumulate_codec_rows`` folds both shapes into one growing
matrix; this generator aligns its rows to the output token ids
(dropping the prefill placeholder and the EOS row) and emits only the
new rows. The final item always carries ``eos=True`` (possibly with
zero frames).
"""
from vllm_omni.entrypoints.openai.codec_stream import align_codec_rows
from vllm_omni.entrypoints.openai.codec_stream import accumulate_codec_rows, align_codec_rows
adapter = self._get_tts_adapter()
spec = getattr(adapter, "codec_stream_spec", None) or {}
codebook_size = int(spec.get("codebook_size", 2048))
usage_acc = SpeechOutputTokenCounter() if (adapter is not None and adapter.validates_generation) else None
token_ids: list[int] = []
codes_cum: np.ndarray | None = None
sent = 0
finished = False
n_outputs = 0
@@ -3347,12 +3351,13 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
token_ids = list(new_ids)
else:
token_ids.extend(int(t) for t in new_ids)
codes_cum = self._extract_codec_output(res)
codes_step = self._extract_codec_output(res)
done = bool(getattr(res, "finished", False))
if codes_cum is not None:
if codes_step is not None:
n_with_codes += 1
if first_codes_at < 0:
first_codes_at = n_outputs
codes_cum = accumulate_codec_rows(codes_cum, codes_step)
aligned = align_codec_rows(codes_cum, token_ids, codebook_size)
delta = aligned[sent:]
if delta.shape[0] >= min_frames or (done and delta.shape[0] > 0):