Skip to content

fix(serving): batch=1 throughput collapse + 8 gpu_worker lifecycle bugs - #13

Merged
beastoin merged 8 commits into
mainfrom
fix/flush-pending-early-reset
Jun 30, 2026
Merged

beastoin merged 8 commits into
mainfrom
fix/flush-pending-early-reset

Conversation

@beastoin

@beastoin beastoin commented Jun 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fix a throughput collapse bug where every request was processed as batch_size=1 under concurrent load, plus 8 gpu_worker lifecycle bugs (races, leaks, crashes) found via CODEx audit. Also adds deterministic benchmark scripts with industry-standard WER evaluation.

What was wrong

Under concurrent load, _flush_pending was reset immediately after dequeue — before the GPU finished processing the current batch. This allowed the flush loop to fire during GPU work, flushing every waiting request as a single-item batch. The result: batch_size=1 regardless of load, destroying throughput.

A CODEx audit of gpu_worker.py then uncovered 8 additional lifecycle bugs — race conditions between stop()/submit(), futures silently hanging on shutdown, pool threads sharing mutable state, and more. All are concurrency bugs that surface under production load patterns (high request rate, graceful shutdown, pool mode).

Fixes

# Bug Severity Fix
1 Batch=1 collapse: _flush_pending reset before GPU completes P0 _batches_inflight counter — partial flushes only when GPU idle
2 _drain_queues crashes on cancelled futures P1 Use _safe_set_exception
3 stop()/submit() race — futures enqueued to dead queues P1 Shared _submit_lock
4 Pool readiness race — request before workers started P1 Start pool workers before _ready.set()
5 Pool shutdown skips queue drain — futures hang P1 Drain all queues after worker join
6 Prefetch shutdown misses _prefetch_queue P1 Drain + join prefetch thread
7 Prefetch in-flight item leak — invisible to drain P1 owned_item tracking with finally
8 _gc_counter shared across pool threads P2 Per-thread local counter
9 start() requires args — breaks pod migration P3 Optional args with defaults

Benchmark

Generated by examples/asr/serving/bench_batch.py — deterministic benchmark with Whisper English text normalization via wer_utils.py.

Model: nvidia/parakeet-tdt-0.6b-v3 · GPU: NVIDIA L4 (24GB) · Container: nvcr.io/nvidia/nemo:26.02
Config: auto attention, VRAM-aware batching, cuda_graphs=false
Dataset: LibriSpeech test-clean, 200 samples

Metric Value
WER 1.84%
Peak RPS 15.3 (c=32)
Peak RTFx 139x (c=32)
Max concurrency c=64 (0 failures)
Sustained RPS 19.1 (c=64, 1,600 files)
Sustained RTFx 174x

Concurrency scaling

c RPS RTFx sess/min p50 p99 Failures
1 2.8 25x 166 0.32s 1.12s 0
8 6.3 57x 375 1.24s 2.54s 0
16 12.6 114x 755 1.27s 2.06s 0
32 15.3 139x 919 2.03s 3.88s 0
64 14.6 133x 876 3.68s 5.23s 0

Files changed

  • examples/asr/serving/batch_engine.py — _batches_inflight counter for flush loop guard
  • examples/asr/serving/gpu_worker.py — 8 lifecycle/concurrency fixes + backward-compat interface
  • examples/asr/serving/test_batch_engine.py — 35 tests including batch=1 regression tests
  • examples/asr/serving/bench_batch.py — Deterministic batch benchmark with WER (new)
  • examples/asr/serving/bench_stream.py — Deterministic streaming benchmark with WER (new)

Test plan

  • 35 unit tests pass (1.4s)
  • CODEx review approved (R3)
  • WER 1.84% (Whisper normalization, no regression)
  • 0 failures across all concurrency levels (c=1 through c=64)
  • Sustained: 19.1 RPS / 174x RTFx over 1,600 files, 0 failures
  • Benchmark evidence

beastoin and others added 2 commits June 30, 2026 05:50
…ghput collapse

The early reset of _flush_pending inside _flush_batch() after dequeue
allowed the flush loop to immediately re-trigger while the GPU was still
processing, causing every new request to be flushed as batch=1 instead
of accumulating into efficient batches.

Fix: keep only the reset in _guarded_flush() finally block. Also fix
test_semaphore_before_dequeue batch_wait from 999s to 0.05s, and add
regression test verifying no batch=1 payloads during GPU processing.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
… guard

CODEx R1 correctly identified that simply removing the early
_flush_pending reset serializes GPU batches (max_inflight=2 becomes
effectively unused).

Fix: add _batches_inflight counter. The flush loop only fires partial
batches when GPU is idle (batches_inflight==0), preventing batch=1
collapse. Submit's full-batch trigger still works during GPU processing,
preserving the second inflight slot for full batches.

- Restore early _flush_pending reset after dequeue (enables full-batch
  dispatch while GPU busy)
- Add _batches_inflight counter (increment after sem acquire, decrement
  in finally)
- Guard flush loop: only fire when batches_inflight==0
- Add test_full_batch_uses_second_inflight_slot regression test

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@beastoin beastoin changed the title fix(serving): remove early _flush_pending reset causing batch=1 collapse fix(serving): prevent batch=1 throughput collapse with inflight-aware flush guard Jun 30, 2026
@beastoin

Copy link
Copy Markdown
Owner Author

CODEx Review — R2 APPROVED

R1 finding (resolved): Removing early _flush_pending reset serialized GPU batches, making max_inflight=2 ineffective.

R2 fix: Added _batches_inflight counter. Flush loop only fires partial batches when GPU is idle (batches_inflight==0). Submit's full-batch trigger still works during GPU processing, preserving the second inflight slot.

Verified locally:

  • python3 -m pytest → 40 passed in 1.71s
  • git diff --check → clean
  • Two new regression tests cover both sides: no batch=1 collapse + full batches use second slot

by CODEx for @beastoin

@beastoin beastoin changed the title fix(serving): prevent batch=1 throughput collapse with inflight-aware flush guard fix(serving): batch=1 collapse + 6 gpu_worker lifecycle bugs Jun 30, 2026
beastoin and others added 2 commits June 30, 2026 06:36
- _drain_queues: use _safe_set_exception (prevents InvalidStateError on
  cancelled futures during shutdown)
- submit: reject work after stop() (prevents futures hanging on dead queues)
- Pool readiness: create pool queues/threads before _ready.set() (prevents
  IndexError on fast first request)
- Pool shutdown: drain pool queues after worker threads join
- Prefetch shutdown: drain _prefetch_queue and join thread; use timeout
  on put() to prevent hang during shutdown
- Pool gc_counter: per-thread local counter (eliminates shared mutable
  state across pool threads)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- submit/stop: guard both with _submit_lock so enqueue cannot race
  behind shutdown drain (CODEx R1 finding)
- prefetch loop: track owned_item in finally block so items being
  prefetched during shutdown get their futures failed instead of
  hanging forever (CODEx R1 finding)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@beastoin beastoin changed the title fix(serving): batch=1 collapse + 6 gpu_worker lifecycle bugs fix(serving): batch=1 collapse + 8 gpu_worker lifecycle bugs Jun 30, 2026
beastoin and others added 4 commits June 30, 2026 07:16
…rror

Make gpu_worker.start() accept optional batch_cfg/stream_cfg args so
pod's main.py can call start() with no args during migration. Add
AudioDurationExceededError class that pod's main.py imports.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…ization

Add bench_batch.py and bench_stream.py that automate the full benchmark
suite: LibriSpeech download, warmup, WER evaluation (via wer_utils.py
Whisper normalization), concurrency sweep, sustained load, and markdown
report output. Replaces ad-hoc /tmp scripts that repeatedly computed WER
without text normalization.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…h.py

Add audio duration tracking per file, compute RTFx (total_audio/wall_time),
RTF (wall_time/total_audio), sess/min, and max concurrency with zero failures
to both the JSON report and markdown output.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…h.py

Manual WAV header parsing returned incorrect durations for LibriSpeech
files, making RTFx show 0.0x. Switch to wave.open() for reliable
frame count / sample rate computation.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@beastoin

Copy link
Copy Markdown
Owner Author

Batch Benchmark Results

WER: 1.84% (200 samples, whisper_english normalization)

Concurrency Sweep

c RPS RTFx RTF sess/min p50 p99 Failures
1 2.76 25.09x 0.04 165.5 0.315s 1.122s 0
8 6.25 56.86x 0.018 375.2 1.243s 2.54s 0
16 12.57 114.34x 0.009 754.5 1.271s 2.062s 0
32 15.31 139.19x 0.007 918.5 2.026s 3.881s 0
64 14.6 132.72x 0.008 875.8 3.675s 5.229s 0

Peak: 15.31 RPS / 139.19x RTFx at c=32 | Max concurrency (0 failures): c=64

Sustained Load

Metric Value
Concurrency 64
Total files 1600
RPS 19.11
RTFx 173.72x
sess/min 1146.4
p50 / p99 3.208s / 5.511s
Failures 0

@beastoin beastoin changed the title fix(serving): batch=1 collapse + 8 gpu_worker lifecycle bugs fix(serving): batch=1 throughput collapse + 8 gpu_worker lifecycle bugs + deterministic benchmarks Jun 30, 2026
@beastoin

Copy link
Copy Markdown
Owner Author

All Checkpoints Passed — Ready for Merge

CP Status Evidence
CP1 Problem ✅ batch=1 collapse + 8 lifecycle bugs
CP2 Workspace ✅ Branch fix/flush-pending-early-reset, L4 GPU pod
CP3 Root cause ✅ _flush_pending early reset + CODEx audit findings
CP4 CODEx consult ✅ 3 rounds, 9 total fixes
CP5 Implementation ✅ 8 commits, 35 tests passing
CP6 PR created ✅ PR #13
CP7 Review approved ✅ CODEx R3
CP8 Benchmark gate ✅ bench_batch.py — WER 1.84%, 139x RTFx, 0 failures
CP9 PR polished ✅ Description updated with final numbers
CP10 Merge gate ⏳ Awaiting manager approval

Key numbers: WER 1.84% · Peak 15.3 RPS / 139x RTFx at c=32 · Sustained 19.1 RPS / 174x RTFx · 0 failures across 1,600 files

@beastoin beastoin changed the title fix(serving): batch=1 throughput collapse + 8 gpu_worker lifecycle bugs + deterministic benchmarks fix(serving): batch=1 throughput collapse + 8 gpu_worker lifecycle bugs Jun 30, 2026
@beastoin

Copy link
Copy Markdown
Owner Author

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant