[Feature][TTS] adaptive chunk ramp (Phase 2 buffer-feedback controller) (#6001)
Signed-off-by: Wallbreazzz <110282866+Wallbreazzz@users.noreply.github.com>
This commit is contained in:
1 parent
da94c3ebe1
commit
4ad455e61e
7 files changed
+871
-8
No files matched your search
@@ -8,9 +8,12 @@ import pytest
|
||||
import torch
|
||||
|
||||
from vllm_omni.model_executor.stage_input_processors.chunk_size_utils import (
|
||||
AdaptiveChunkController,
|
||||
compute_adaptive_emit,
|
||||
compute_dynamic_initial_chunk_size,
|
||||
compute_ramp_emit,
|
||||
max_ic_for_chunk_size,
|
||||
parse_adaptive_config,
|
||||
parse_chunk_ramp,
|
||||
ramp_chunk_size,
|
||||
ramp_cumulative,
|
||||
@@ -41,7 +44,17 @@ def _req(rid, *, finished, initial_codec_chunk_frames=None):
|
||||
)
|
||||
|
||||
|
||||
def _tm(*, chunk_frames=25, left_context=25, max_num_seqs=1, initial_chunk_frames=0, chunk_ramp=None):
|
||||
def _tm(
|
||||
*,
|
||||
chunk_frames=25,
|
||||
left_context=25,
|
||||
max_num_seqs=1,
|
||||
initial_chunk_frames=0,
|
||||
chunk_ramp=None,
|
||||
adaptive=False,
|
||||
adaptive_min=2,
|
||||
adaptive_margin=200.0,
|
||||
):
|
||||
extra = {
|
||||
"codec_chunk_frames": chunk_frames,
|
||||
"codec_left_context_frames": left_context,
|
||||
@@ -49,11 +62,16 @@ def _tm(*, chunk_frames=25, left_context=25, max_num_seqs=1, initial_chunk_frame
|
||||
}
|
||||
if chunk_ramp is not None:
|
||||
extra["codec_chunk_ramp"] = chunk_ramp
|
||||
if adaptive:
|
||||
extra["codec_chunk_adaptive"] = True
|
||||
extra["codec_chunk_min_frames"] = adaptive_min
|
||||
extra["codec_chunk_safety_margin_ms"] = adaptive_margin
|
||||
return SimpleNamespace(
|
||||
code_prompt_token_ids=defaultdict(list),
|
||||
scheduler_max_num_seqs=max_num_seqs,
|
||||
put_req_chunk=defaultdict(int),
|
||||
ramp_chunk_count=defaultdict(int),
|
||||
_adaptive_states={},
|
||||
request_payload={},
|
||||
connector=SimpleNamespace(config={"extra": extra}),
|
||||
)
|
||||
@@ -605,18 +623,32 @@ class TestRampHelpers:
|
||||
def test_parse_ramp_warns_on_tail_mismatch(self, caplog):
|
||||
import logging
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
target_logger = logging.getLogger("vllm_omni.model_executor.stage_input_processors.chunk_size_utils")
|
||||
target_logger.addHandler(caplog.handler)
|
||||
prev_level = target_logger.level
|
||||
target_logger.setLevel(logging.WARNING)
|
||||
try:
|
||||
result = parse_chunk_ramp({"codec_chunk_ramp": [4, 4, 8]}, steady=25)
|
||||
finally:
|
||||
target_logger.removeHandler(caplog.handler)
|
||||
target_logger.setLevel(prev_level)
|
||||
assert result == [4, 4, 8]
|
||||
assert any("reintroduces" in r.message for r in caplog.records)
|
||||
assert "reintroduces" in caplog.text
|
||||
|
||||
def test_parse_ramp_no_warn_on_tail_match(self, caplog):
|
||||
import logging
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
target_logger = logging.getLogger("vllm_omni.model_executor.stage_input_processors.chunk_size_utils")
|
||||
target_logger.addHandler(caplog.handler)
|
||||
prev_level = target_logger.level
|
||||
target_logger.setLevel(logging.WARNING)
|
||||
try:
|
||||
result = parse_chunk_ramp({"codec_chunk_ramp": [4, 4, 8, 16, 25]}, steady=25)
|
||||
finally:
|
||||
target_logger.removeHandler(caplog.handler)
|
||||
target_logger.setLevel(prev_level)
|
||||
assert result == [4, 4, 8, 16, 25]
|
||||
assert not any("reintroduces" in r.message for r in caplog.records)
|
||||
assert "reintroduces" not in caplog.text
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"index,ramp,steady,expected",
|
||||
@@ -941,3 +973,488 @@ class TestChunkRampEmission:
|
||||
p_seg2_0 = self._emit(tm, rid, 4)
|
||||
assert p_seg2_0 is not None
|
||||
assert p_seg2_0.meta.left_context_size == 0
|
||||
|
||||
|
||||
class TestAdaptiveController:
|
||||
def test_compute_next_chunk_size_basic(self):
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=20.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=10,
|
||||
)
|
||||
now = 0.5
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=100.0)
|
||||
assert 2 <= size <= 25
|
||||
|
||||
def test_compute_next_chunk_size_clamp_min(self):
|
||||
"""min_frames floor holds when ramp_floor (last_target + delta) is
|
||||
below min_frames. Uses a small EWMA so delta=2 and last_target=0,
|
||||
i.e. ramp_floor=2 < min_frames=4 -> greedy=min_frames wins."""
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=10.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=1,
|
||||
)
|
||||
# buffer = 80 - 70 = 10ms (positive but < margin=200)
|
||||
# greedy: available=0, max_n=0, greedy=min_frames=4
|
||||
# ramp_floor: last_target=0 + delta=max(2,int(10/15))=2 -> 2 < 4
|
||||
now = 0.07
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=4, max_frames=25, safety_margin_ms=200.0)
|
||||
assert size == 4 # min_frames floor dominates ramp_floor=2
|
||||
|
||||
def test_compute_next_chunk_size_clamp_max(self):
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=10.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=100,
|
||||
)
|
||||
now = 1.0
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert size == 25
|
||||
|
||||
def test_hysteresis_locks_steady(self):
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=10.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=50,
|
||||
)
|
||||
now = 0.1
|
||||
size1 = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert ctrl.steady_state_reached
|
||||
assert size1 == 25
|
||||
size2 = ctrl.compute_next_chunk_size(now + 10.0, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert size2 == 25
|
||||
|
||||
def test_ramp_floor_overrides_greedy_when_buffer_low(self):
|
||||
"""When buffer is negative (overloaded), ramp_floor still forces
|
||||
gradual growth to avoid stagnating at min_frames (which would cause
|
||||
too many Code2Wav decode passes and RTF explosion)."""
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=50.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=2,
|
||||
last_target=4,
|
||||
)
|
||||
# buffer = 160 - 200 = -40ms (negative)
|
||||
# greedy: available=0, max_n=0, greedy=2
|
||||
# ramp_floor: last_target=4 + delta=max(2,int(50/15))=3 -> 7
|
||||
now = 0.2
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert not ctrl.steady_state_reached
|
||||
assert size == 7 # ramp_floor (unconditional) dominates greedy=2
|
||||
|
||||
def test_ramp_floor_dominates_when_last_target_set(self):
|
||||
"""Once last_target is set (post-emit), the unconditional ramp_floor
|
||||
(last_target + ewma-scaled delta) dominates greedy whenever greedy is
|
||||
smaller. This is intended: chunk size grows monotonically toward max
|
||||
to minimize Code2Wav decode passes (pre-W5; see W5 coupling)."""
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=50.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=4,
|
||||
last_target=4,
|
||||
)
|
||||
# buffer = 320 - 100 = 220ms (positive)
|
||||
# greedy: available=170, max_n=int(170/50)=3, greedy=3
|
||||
# ramp_floor: 4 + delta=max(2,int(50/15))=3 -> 7
|
||||
now = 0.1
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert size == 7 # ramp_floor (7) dominates greedy (3)
|
||||
|
||||
def test_greedy_dominates_when_ramp_floor_low(self):
|
||||
"""When last_target=0 (fresh, no prior emit) and buffer is positive
|
||||
but below the hysteresis threshold, greedy selects an intermediate
|
||||
size larger than ramp_floor and greedy dominates. This is the only
|
||||
regime where greedy (not ramp_floor or hysteresis) sets the size."""
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=50.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=8,
|
||||
last_target=0,
|
||||
)
|
||||
# buffer = 640 - 100 = 540ms (positive, < hysteresis 1300ms)
|
||||
# greedy: available=490, max_n=int(490/50)=9, greedy=9
|
||||
# ramp_floor: 0 + delta=max(2,int(50/15))=3 -> 3
|
||||
now = 0.1
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert not ctrl.steady_state_reached
|
||||
assert size == 9 # greedy (9) dominates ramp_floor (3)
|
||||
|
||||
def test_record_emit_ewma_update(self):
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=20.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=4,
|
||||
)
|
||||
ctrl.record_emit(now=0.5, n_frames=8, accumulated=12, target_size=8)
|
||||
assert ctrl.frames_emitted_this_segment == 12
|
||||
assert ctrl.accumulated_at_last_emit == 12
|
||||
assert ctrl.last_emit_time == 0.5
|
||||
assert ctrl.last_target == 8 # D6: stores target, not actual
|
||||
measured = (0.5 - 0.0) * 1000.0 / 8
|
||||
expected = 0.3 * measured + 0.7 * 20.0
|
||||
assert abs(ctrl.frame_time_ewma_ms - expected) < 0.01
|
||||
|
||||
def test_record_emit_first_then_second(self):
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=0.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=0,
|
||||
)
|
||||
ctrl.record_emit(now=0.4, n_frames=4, accumulated=4, target_size=4)
|
||||
assert ctrl.frame_time_ewma_ms == 0.0
|
||||
assert ctrl.frames_emitted_this_segment == 4
|
||||
assert ctrl.last_target == 4
|
||||
|
||||
ctrl.record_emit(now=0.8, n_frames=4, accumulated=8, target_size=4)
|
||||
expected = (0.8 - 0.4) * 1000.0 / 4
|
||||
assert abs(ctrl.frame_time_ewma_ms - expected) < 0.01
|
||||
|
||||
def test_record_emit_collects_underrun(self):
|
||||
"""record_emit mirrors stream_client.py's playback-cursor underrun
|
||||
model: per_chunk_gap_ms records each chunk's lateness, total_gap_s
|
||||
accumulates gaps > 5ms. Deterministic given explicit `now`."""
|
||||
ctrl = AdaptiveChunkController(
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=0,
|
||||
)
|
||||
# chunk0: now=0.0 (= first_emit_time), n=2 -> 0.16s audio
|
||||
ctrl.record_emit(now=0.0, n_frames=2, accumulated=2, target_size=2)
|
||||
assert ctrl.chunk_sizes == [2]
|
||||
assert ctrl.per_chunk_gap_ms == [0.0]
|
||||
assert ctrl.total_gap_s == 0.0
|
||||
assert abs(ctrl.playback_cursor_s - 0.16) < 1e-9
|
||||
# chunk1: now=0.2 (200ms wall) -> arrival 0.2, late by 0.2-0.16=0.04s
|
||||
ctrl.record_emit(now=0.2, n_frames=2, accumulated=4, target_size=2)
|
||||
assert ctrl.chunk_sizes == [2, 2]
|
||||
assert abs(ctrl.per_chunk_gap_ms[1] - 40.0) < 1e-6
|
||||
assert abs(ctrl.total_gap_s - 0.04) < 1e-9
|
||||
assert abs(ctrl.playback_cursor_s - 0.36) < 1e-9
|
||||
|
||||
def test_ewma_zero_skips_hysteresis(self):
|
||||
"""When EWMA=0, hysteresis must not trigger (steady_gen_ms=0
|
||||
would make any positive buffer satisfy the condition)."""
|
||||
ctrl = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=0.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=10,
|
||||
)
|
||||
now = 0.001
|
||||
size = ctrl.compute_next_chunk_size(now, min_frames=2, max_frames=25, safety_margin_ms=50.0)
|
||||
assert not ctrl.steady_state_reached
|
||||
assert size == 2 # min_frames (EWMA=0 → max_n=min_frames)
|
||||
|
||||
def test_ramp_divisor_configurable(self):
|
||||
"""ramp_divisor/ramp_delta_min are controller fields wired from config.
|
||||
Defaults (15/2) preserve the original formula; changing them changes
|
||||
delta. Lets a deploy tune the ramp via yaml without code changes, while
|
||||
the unit tests (which use defaults) stay green."""
|
||||
# default divisor=15: ewma=50 -> delta=max(2,int(50/15))=3 -> floor=4+3=7
|
||||
c1 = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=50.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=2,
|
||||
last_target=4,
|
||||
)
|
||||
assert c1.compute_next_chunk_size(0.2, 2, 25, 50.0) == 7
|
||||
# divisor=10: delta=max(2,int(50/10))=5 -> floor=4+5=9
|
||||
c2 = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=50.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=2,
|
||||
last_target=4,
|
||||
ramp_divisor=10,
|
||||
)
|
||||
assert c2.compute_next_chunk_size(0.2, 2, 25, 50.0) == 9
|
||||
# delta_min=4 + ewma=10 (int(10/15)=0): delta=max(4,0)=4 -> floor=4+4=8
|
||||
c3 = AdaptiveChunkController(
|
||||
frame_time_ewma_ms=10.0,
|
||||
first_emit_time=0.0,
|
||||
last_emit_time=0.0,
|
||||
frames_emitted_this_segment=2,
|
||||
last_target=4,
|
||||
ramp_delta_min=4,
|
||||
)
|
||||
assert c3.compute_next_chunk_size(0.2, 2, 25, 50.0) == 8
|
||||
|
||||
|
||||
class TestAdaptiveEmitHelper:
|
||||
@pytest.mark.parametrize(
|
||||
"length,accumulated,target,finished,expected_emit,expected_ctx",
|
||||
[
|
||||
(4, 0, 4, False, True, 4),
|
||||
(3, 0, 4, False, False, 0),
|
||||
(12, 4, 8, False, True, 8),
|
||||
(10, 4, 8, False, False, 0),
|
||||
# D3: finished flushes ALL remaining frames, not just target_size
|
||||
(6, 4, 8, True, True, 2),
|
||||
(4, 4, 8, True, True, 0),
|
||||
(20, 4, 8, True, True, 16), # new_frames=16 > target=8 → flush 16
|
||||
(30, 0, 25, True, True, 30), # finished, new_frames=30 > target=25 → flush 30
|
||||
# B1: non-finished path also ships ALL accumulated frames (new_frames),
|
||||
# not just target_size. When new_frames > target_size (e.g. scheduler
|
||||
# stall dropped target below queued frames), returning target_size
|
||||
# would cause the [-context_length:] slice to skip surplus frames.
|
||||
(10, 0, 4, False, True, 10), # new_frames=10 > target=4 → ship 10
|
||||
(8, 2, 4, False, True, 6), # new_frames=6 > target=4 → ship 6
|
||||
(100, 0, 25, False, True, 100), # new_frames=100 > target=25 → ship 100
|
||||
],
|
||||
)
|
||||
def test_compute_adaptive_emit(self, length, accumulated, target, finished, expected_emit, expected_ctx):
|
||||
emit, ctx = compute_adaptive_emit(length, accumulated, target, finished)
|
||||
assert emit == expected_emit
|
||||
assert ctx == expected_ctx
|
||||
|
||||
|
||||
class TestAdaptiveConfigParsing:
|
||||
def test_disabled_by_default(self):
|
||||
enabled, min_f, margin, divisor, delta_min = parse_adaptive_config({})
|
||||
assert enabled is False
|
||||
assert min_f == 2
|
||||
assert margin == 50.0
|
||||
assert divisor == 15
|
||||
assert delta_min == 2
|
||||
|
||||
def test_enabled(self):
|
||||
cfg = {"codec_chunk_adaptive": True}
|
||||
enabled, *_ = parse_adaptive_config(cfg)
|
||||
assert enabled is True
|
||||
|
||||
def test_custom_values(self):
|
||||
cfg = {
|
||||
"codec_chunk_adaptive": True,
|
||||
"codec_chunk_min_frames": 4,
|
||||
"codec_chunk_safety_margin_ms": 300.0,
|
||||
"codec_chunk_ramp_divisor": 10,
|
||||
"codec_chunk_ramp_delta_min": 3,
|
||||
}
|
||||
enabled, min_f, margin, divisor, delta_min = parse_adaptive_config(cfg)
|
||||
assert enabled is True
|
||||
assert min_f == 4
|
||||
assert margin == 300.0
|
||||
assert divisor == 10
|
||||
assert delta_min == 3
|
||||
|
||||
def test_min_frames_clamped(self):
|
||||
cfg = {"codec_chunk_adaptive": True, "codec_chunk_min_frames": 0}
|
||||
_, min_f, _, _, _ = parse_adaptive_config(cfg)
|
||||
assert min_f == 1
|
||||
|
||||
def test_ramp_divisor_clamped(self):
|
||||
cfg = {"codec_chunk_adaptive": True, "codec_chunk_ramp_divisor": 0}
|
||||
_, _, _, divisor, _ = parse_adaptive_config(cfg)
|
||||
assert divisor == 1
|
||||
|
||||
def test_ramp_delta_min_clamped(self):
|
||||
cfg = {"codec_chunk_adaptive": True, "codec_chunk_ramp_delta_min": 0}
|
||||
_, _, _, _, delta_min = parse_adaptive_config(cfg)
|
||||
assert delta_min == 1
|
||||
|
||||
|
||||
class TestAdaptiveEmission:
|
||||
def _emit(self, tm, rid, n_frames, finished=False):
|
||||
tm.code_prompt_token_ids[rid] = [_FRAME[:] for _ in range(n_frames)]
|
||||
return talker2code2wav_async_chunk(
|
||||
transfer_manager=tm,
|
||||
multimodal_output={"codes": {"audio": torch.zeros((0,))}},
|
||||
request=_req(rid, finished=finished),
|
||||
is_finished=finished,
|
||||
)
|
||||
|
||||
def test_adaptive_chunk0_uses_dynamic_ic(self):
|
||||
tm = _tm(adaptive=True, adaptive_min=2, max_num_seqs=8)
|
||||
rid = "adaptive-ic"
|
||||
|
||||
p0 = self._emit(tm, rid, 2)
|
||||
assert p0 is not None
|
||||
assert len(p0.codes.audio) == _Q * 2
|
||||
|
||||
def test_adaptive_chunk0_holds_until_ic(self):
|
||||
tm = _tm(adaptive=True, adaptive_min=2, max_num_seqs=8)
|
||||
rid = "adaptive-hold"
|
||||
p = self._emit(tm, rid, 1)
|
||||
assert p is None
|
||||
|
||||
p0 = self._emit(tm, rid, 2)
|
||||
assert p0 is not None
|
||||
assert len(p0.codes.audio) == _Q * 2
|
||||
|
||||
def test_adaptive_finished_flush(self):
|
||||
tm = _tm(adaptive=True, adaptive_min=2, max_num_seqs=8)
|
||||
rid = "adaptive-flush"
|
||||
|
||||
p0 = self._emit(tm, rid, 2)
|
||||
assert p0 is not None
|
||||
tm.ramp_chunk_count[rid] = 1
|
||||
|
||||
p_fin = self._emit(tm, rid, 5, finished=True)
|
||||
assert p_fin is not None
|
||||
assert p_fin.meta.finished.item() is True
|
||||
|
||||
def test_adaptive_finished_no_new_frames(self):
|
||||
tm = _tm(adaptive=True, adaptive_min=2, max_num_seqs=8)
|
||||
rid = "adaptive-no-new"
|
||||
|
||||
p0 = self._emit(tm, rid, 2)
|
||||
assert p0 is not None
|
||||
tm.ramp_chunk_count[rid] = 1
|
||||
|
||||
p_fin = self._emit(tm, rid, 2, finished=True)
|
||||
assert p_fin is not None
|
||||
assert p_fin.meta.finished.item() is True
|
||||
assert p_fin.codes.audio.numel() == 0
|
||||
|
||||
def test_adaptive_precedence_over_fixed_ramp(self):
|
||||
tm = _tm(chunk_ramp=[4, 4, 8, 16, 25], adaptive=True, adaptive_min=2, max_num_seqs=8)
|
||||
rid = "adaptive-precedence"
|
||||
|
||||
p = self._emit(tm, rid, 1)
|
||||
assert p is None
|
||||
|
||||
p0 = self._emit(tm, rid, 2)
|
||||
assert p0 is not None
|
||||
assert len(p0.codes.audio) == _Q * 2
|
||||
|
||||
def test_adaptive_backward_compat(self):
|
||||
tm = _tm(initial_chunk_frames=1)
|
||||
rid = "no-adaptive"
|
||||
|
||||
p0 = self._emit(tm, rid, 1)
|
||||
assert p0 is not None
|
||||
assert len(p0.codes.audio) == _Q * 1
|
||||
|
||||
def test_adaptive_segment_reset(self):
|
||||
tm = _tm(adaptive=True, adaptive_min=2, max_num_seqs=8)
|
||||
rid = "adaptive-segment"
|
||||
|
||||
p0 = self._emit(tm, rid, 2)
|
||||
assert p0 is not None
|
||||
tm.ramp_chunk_count[rid] = 1
|
||||
tm.put_req_chunk[rid] = 1
|
||||
|
||||
tm.code_prompt_token_ids[rid] = []
|
||||
tm.ramp_chunk_count.pop(rid, None)
|
||||
tm._adaptive_states.pop(rid, None)
|
||||
|
||||
p_seg2 = self._emit(tm, rid, 2)
|
||||
assert p_seg2 is not None
|
||||
assert p_seg2.meta.left_context_size == 0
|
||||
|
||||
def test_adaptive_with_ref_code_first_chunk(self):
|
||||
tm = _tm(adaptive=True, adaptive_min=2, left_context=25, max_num_seqs=8)
|
||||
rid = "adaptive-ref"
|
||||
tm.code_prompt_token_ids[rid] = [_FRAME[:] for _ in range(2)]
|
||||
ref_code = torch.tensor([[9, 9, 9, 9], [8, 8, 8, 8]], dtype=torch.long)
|
||||
|
||||
payload = talker2code2wav_async_chunk(
|
||||
transfer_manager=tm,
|
||||
multimodal_output={"codes": {"audio": torch.zeros((0,)), "ref": ref_code}},
|
||||
request=_req(rid, finished=False),
|
||||
is_finished=False,
|
||||
)
|
||||
|
||||
assert payload is not None
|
||||
assert payload.meta.ref_context_size == 2
|
||||
assert payload.meta.ref_context_included is True
|
||||
assert payload.meta.left_context_size == 2
|
||||
|
||||
|
||||
class TestAdaptiveAccumulationPath:
|
||||
"""Regression test for the frame-skipping bug (PR #6001 review B1).
|
||||
|
||||
Appends one frame per call with a controlled clock, asserting that
|
||||
the concatenation of every emitted chunk equals the accumulated
|
||||
frame sequence, in order, with no gaps. Each frame has a distinct
|
||||
value so the assertion can detect a skipped frame rather than just
|
||||
a short total.
|
||||
"""
|
||||
|
||||
def test_no_frames_skipped_when_target_drops(self, monkeypatch):
|
||||
"""When greedy target_size is high (large buffer) and then drops
|
||||
(clock jumps), new_frames > target_size at the emit point.
|
||||
compute_adaptive_emit now returns new_frames (not target_size),
|
||||
so all accumulated frames are shipped and the [-context_length:]
|
||||
slice stays contiguous with the previous emit. last_target is set
|
||||
to target_size (not n_frames) to prevent the ramp floor from
|
||||
ratcheting up on transient overshoot.
|
||||
|
||||
Clock schedule (max_num_seqs=4 gives IC=2 via dynamic IC):
|
||||
t=0.0 chunk 0 (IC=2), emit 2. ewma=0.
|
||||
t=0.05 emit target=4 (ramp_floor). ewma=12.5 (direct assign).
|
||||
t=0.05 greedy dominates (target=18), 17 frames accumulate (holds).
|
||||
t=100.0 clock jumps -> greedy collapses, target drops to
|
||||
ramp_floor=6. new_frames=18 > 6 -> emit 18 (all new frames).
|
||||
last_target=6 (not 18, preventing floor ratchet).
|
||||
"""
|
||||
tm = _tm(
|
||||
adaptive=True,
|
||||
adaptive_min=2,
|
||||
max_num_seqs=4,
|
||||
)
|
||||
rid = "adaptive-accum"
|
||||
tm.code_prompt_token_ids[rid] = []
|
||||
|
||||
clock = [0.0]
|
||||
monkeypatch.setattr(
|
||||
"vllm_omni.model_executor.stage_input_processors.qwen3_tts.time.monotonic",
|
||||
lambda: clock[0],
|
||||
)
|
||||
|
||||
emitted_indices: list[int] = []
|
||||
total_frames = 40
|
||||
|
||||
def append_and_emit(frame_idx, finished=False):
|
||||
tm.code_prompt_token_ids[rid].append([frame_idx] * _Q)
|
||||
p = talker2code2wav_async_chunk(
|
||||
transfer_manager=tm,
|
||||
multimodal_output={"codes": {"audio": torch.zeros((0,))}},
|
||||
request=_req(rid, finished=finished),
|
||||
is_finished=finished,
|
||||
)
|
||||
if p is not None:
|
||||
if p.codes.audio.numel() > 0:
|
||||
n = p.codes.audio.numel() // _Q
|
||||
emitted_indices.extend(p.codes.audio[:n].tolist())
|
||||
tm.ramp_chunk_count[rid] += 1
|
||||
return p
|
||||
|
||||
# Phase 1 (t=0.0): chunk 0, IC=2
|
||||
for i in range(2):
|
||||
append_and_emit(i)
|
||||
|
||||
# Phase 2 (t=0.05): build EWMA, emit target=4 at length=6
|
||||
clock[0] = 0.05
|
||||
for i in range(2, 6):
|
||||
append_and_emit(i)
|
||||
|
||||
# Phase 3 (t=0.05): greedy target=18 (large buffer), accumulate
|
||||
# 17 frames while holding (new_frames 1..17 < 18)
|
||||
for i in range(6, 23):
|
||||
append_and_emit(i)
|
||||
|
||||
# Phase 4 (t=100.0): clock jumps, greedy collapses, target drops
|
||||
# to ramp_floor=6. new_frames=18 > 6 -> bug trigger point.
|
||||
clock[0] = 100.0
|
||||
for i in range(23, total_frames):
|
||||
append_and_emit(i, finished=(i == total_frames - 1))
|
||||
|
||||
# Assert: all frames 0..39 emitted, in order, no gaps.
|
||||
expected = list(range(total_frames))
|
||||
assert emitted_indices == expected, (
|
||||
f"Frame loss detected! "
|
||||
f"Missing: {sorted(set(expected) - set(emitted_indices))}, "
|
||||
f"Extra: {sorted(set(emitted_indices) - set(expected))}"
|
||||
)
|
||||
Reference in new issue
Block a user