Repository navigation
Description
Problem
torch.compile and runtime attention switching (change_attention_model()) are fundamentally incompatible. This forces a choice:
attention_mode: full+ compile: Best throughput (178x RTFx), lower VRAM, but OOMs on audio >10-15minattention_mode: auto+ no compile: Handles long audio, but higher VRAM causes OOM under batch load (14 OOMs, 22% error rate in hiro PR Flashlight and Pyctcdecode decoders NVIDIA-NeMo/Speech#8428 Phase 2 rollback)
Why they are incompatible
change_attention_model() (conformer_encoder.py:1127-1254) replaces entire nn.Module objects in the model graph at runtime:
# For each of 24 ConformerLayers:
del m.self_attn # delete old module
m.self_attn = RelPositionMultiHeadAttentionLongformer(...) # new class, different forward()
del self.pos_enc # delete old positional encoding
self.pos_enc = LocalAttRelPositionalEncoding(...) # entirely different classtorch.compile traces and freezes the computation graph. When modules are deleted and replaced with different classes, the compiled graph references dead objects. Recompiling takes 30-60s, defeating the purpose.
This is not a bug — it is a fundamental constraint of ahead-of-time graph compilation vs runtime graph mutation.
Proposed Solution: Dual-Model Architecture
Load two instances of the model on the same GPU:
-
Compiled model (full attention) — for files < threshold (e.g., 600s)
- torch.compile enabled
- Lower VRAM per file (buffer reuse optimization)
- 20-30% higher throughput
-
Uncompiled model (local attention) — for files >= threshold
- No torch.compile
- Handles up to 1h audio
- O(T*W) VRAM scaling
VRAM budget
- Model weights: ~1.2 GB per instance = ~2.4 GB for two
- Baseline with two models: ~6 GB (vs ~4.9 GB with one)
- Remaining for inference: ~16 GB on L4
This is viable because:
- Long files are rare (p95 audio < 60s for omi)
- The uncompiled model only activates for rare long files
- Both models share the same weights (could potentially share via copy-on-write)
Routing logic in gpu_worker.py
def _batch_transcribe_with_model(self, payload):
max_dur = max(get_duration(p) for p in payload["audio_paths"])
if max_dur >= self._attn_auto_threshold_sec:
model = self._batch_model_local # uncompiled, local attention
else:
model = self._batch_model_compiled # compiled, full attention
return model.transcribe(...)Alternative considered: Lazy decompile (Option D)
Compile normally, torch._dynamo.reset() + reload for rare long files. Rejected because:
- Recompilation takes 30-60s (unacceptable latency)
- torch._dynamo.reset() clears ALL compiled graphs (affects other models if loaded)
- First request after recompile is also slow (tracing overhead)
Affected Files
examples/asr/serving/gpu_worker.py— dual model loading, routing logicexamples/asr/serving/conf/serving-batch.yaml— config for dual modelexamples/asr/serving/benchmark.py— benchmark both paths
Related
- Issue BatchEngine: add VRAM-aware batch sizing to prevent OOM under concurrent load #9 (VRAM-aware batch sizing — complementary fix)
- Issue Add duration-aware batch scheduling to prevent OOM on long audio #3 (duration-aware scheduling — broader scope)
- PR fix(serving): EOU detection + configurable attention mode for long audio #8 (original auto mode implementation — identified the compile incompatibility but only disabled compile as workaround)
Activity