Skip to content

BatchEngine: add VRAM-aware batch sizing to prevent OOM under concurrent load #9

Closed

Description

@beastoin

Problem

The BatchEngine groups concurrent HTTP requests into a single model.transcribe() call with B>1 (up to max_batch_size=32). VRAM for attention scales linearly with batch size: VRAM = baseline + 136.6 * B * T_max^2 bytes, where T_max is the longest file in the batch.

This causes OOM on L4 (24GB) when multiple long files arrive within the 100ms flush window:

Batch Size File Duration Peak VRAM Status
B=1 5min 6.7 GB Safe
B=4 5min 12.2 GB Safe
B=8 5min 19.5 GB Tight
B=12 5min 26.8 GB OOM
B=32 30s 8.3 GB Safe
B=32 2min 14.2 GB Safe

In production (issue #5), this causes ~10 OOM events/hour with MAX_BATCH_SIZE=32.

Root cause

  1. BatchEngine._flush_batch() groups up to max_batch_size files regardless of their duration
  2. The auto-attention switch in gpu_worker.py:718 only checks max(file_duration) against threshold — does NOT account for cumulative batch VRAM
  3. No VRAM budget estimation exists in the batching path

Testing gap from PR #8

PR #8 benchmarked only with full + torch.compile and uniform audio durations. Never tested:

  • VRAM with compile OFF at various batch sizes
  • Mixed-duration batches (e.g., 4x 30s + 2x 5min in one batch)
  • Batch-aware OOM boundary

Proposed Fix

1. Dynamic batch size cap based on max file duration

In BatchEngine._flush_batch(), read audio duration from WAV headers and cap batch size:

def max_safe_batch(max_dur_sec, vram_budget_mb=17000):
    T = max_dur_sec / 0.08
    per_file_mb = 136.6 * T * T / (1024 * 1024)
    if per_file_mb <= 0:
        return 32
    return max(1, min(32, int(vram_budget_mb / per_file_mb)))

2. Pre-flight duration check in BatchEngine

Read audio duration when request enters the queue. Use duration metadata for batch formation.

3. VRAM profiling in benchmark suite

Add to benchmark.py: VRAM measurement at each concurrency level, batch size x duration matrix, compile-on vs compile-off comparison.

Affected Files

  • examples/asr/serving/batch_engine.py — batch formation with VRAM budget
  • examples/asr/serving/gpu_worker.py — expose VRAM estimation helper
  • examples/asr/serving/benchmark.py — add VRAM profiling
  • examples/asr/serving/server.py — pass duration metadata to BatchEngine

Related

Empirical Data

Scaling factor measured on L4 with nvidia-smi during inference (torch.compile OFF, full attention):

  • 30s (T=375): +106 MB per file
  • 5min (T=3750): +1,826 MB per file
  • 8min (T=6000): +4,720 MB per file
  • Formula: delta_MB = 136.6 * T-squared / (1024*1024), baseline ~4,862 MB

Activity

  1. added
    enhancement
    on Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions