Skip to content

Dual-model architecture: compiled model for short files, uncompiled for long audio #10

Closed as not planned

Description

@beastoin

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-15min
  • attention_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 class

torch.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:

  1. Compiled model (full attention) — for files < threshold (e.g., 600s)

    • torch.compile enabled
    • Lower VRAM per file (buffer reuse optimization)
    • 20-30% higher throughput
  2. 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 logic
  • examples/asr/serving/conf/serving-batch.yaml — config for dual model
  • examples/asr/serving/benchmark.py — benchmark both paths

Related

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