From 8a3bc4babc71feaf80e399b4c7f7a97d504fd3b9 Mon Sep 17 00:00:00 2001 From: pedro-bento Date: Mon, 5 Oct 2026 21:42:28 +0100 Subject: [PATCH] fix(align): free the ASR model's VRAM before CTC alignment Whisper large-v3 (~4.5 GB) stayed resident while WhisperX loaded the Wav2Vec2 aligner, so on a 6 GB GPU cublasCreate failed with CUBLAS_STATUS_ALLOC_FAILED and alignment silently fell back to Whisper timestamps. Load the ASR model per input, drop it before aligning, and return its CUDA memory first. Cached transcriptions now skip the load entirely. --- cadence.py | 35 +++++++++++++++++++++----- tests/test_whisper_unload.py | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 tests/test_whisper_unload.py diff --git a/cadence.py b/cadence.py index 752e653..7214f9c 100755 --- a/cadence.py +++ b/cadence.py @@ -34,6 +34,7 @@ Features: from __future__ import annotations import argparse +import gc import hashlib import json import logging @@ -400,6 +401,26 @@ def _load_faster_whisper_model(model_size: str): return WhisperModel(model_size, device="cpu", compute_type="int8") +def _release_cuda_memory() -> None: + """Best-effort release of GPU memory held by a just-dropped ASR model. + + Whisper large-v3 needs ~4.5 GB; on a 6 GB card the Wav2Vec2 CTC aligner then + cannot allocate its cuBLAS workspace (``CUBLAS_STATUS_ALLOC_FAILED``). The + model object is out of scope by the time this runs, so this only nudges + Python's GC and, when torch is present, returns the freed blocks to CUDA. + """ + gc.collect() + try: + import torch + except Exception: + return + try: + if torch.cuda.is_available(): + torch.cuda.empty_cache() + except Exception: + pass + + def _transcribe_faster_whisper( wav: Path, model_size: str, @@ -496,7 +517,6 @@ def load_or_transcribe( track_idx: int, no_cache: bool, backend: str, - stt_model=None, align_cache: dict | None = None, extract: Callable[[], None] | None = None, ) -> list[Word]: @@ -516,11 +536,12 @@ def load_or_transcribe( if backend == "mlx": words, raw_segments = _transcribe_mlx(wav_16k, model, language=language) else: - words, raw_segments = _transcribe_faster_whisper( - wav_16k, model, language=language, model=stt_model - ) + words, raw_segments = _transcribe_faster_whisper(wav_16k, model, language=language) if raw_segments: + # The ASR model was local to _transcribe_faster_whisper and is already + # dropped; return its VRAM before the aligner loads. + _release_cuda_memory() aligned_words = _align_with_whisperx( raw_segments=raw_segments, wav_16k=wav_16k, @@ -1937,7 +1958,10 @@ def main() -> None: phrase_fillers = set(DISCOURSE_FILLERS) backend = detect_backend() - stt_model = _load_faster_whisper_model(args.model) if backend != "mlx" else None + # The ASR model is loaded lazily per input (inside load_or_transcribe) and + # released before CTC alignment: Whisper large-v3 (~4.5 GB) and the Wav2Vec2 + # aligner do not both fit on a 6 GB GPU, which previously made alignment fail + # with CUBLAS_STATUS_ALLOC_FAILED. Cached transcriptions skip the load. align_cache: dict = {} track_data_list: list[VideoTrackData] = [] @@ -1991,7 +2015,6 @@ def main() -> None: track_idx=resolved_track_idx, no_cache=args.no_cache, backend=backend, - stt_model=stt_model, align_cache=align_cache, extract=do_extract, ) diff --git a/tests/test_whisper_unload.py b/tests/test_whisper_unload.py new file mode 100644 index 0000000..d76a17c --- /dev/null +++ b/tests/test_whisper_unload.py @@ -0,0 +1,48 @@ +"""The Whisper -> CTC-aligner VRAM handoff. + +Whisper large-v3 (~4.5 GB) and the Wav2Vec2 aligner cannot both fit on a 6 GB +GPU, which made alignment fail with ``CUBLAS_STATUS_ALLOC_FAILED``. The ASR +model must therefore be dropped and its memory released *before* alignment. +""" + +import cadence + + +def test_release_cuda_memory_is_safe_without_gpu(): + # Must never raise when torch is absent or no CUDA device exists: it only + # nudges the GC and (when available) torch's CUDA cache. + assert cadence._release_cuda_memory() is None + + +def test_transcribe_releases_vram_before_aligning(monkeypatch, tmp_path): + events = [] + + def fake_transcribe(*args, **kwargs): + events.append("transcribe") + return [], [{"start": 0.0, "end": 1.0}] + + def fake_release(): + events.append("release") + + def fake_align(**kwargs): + events.append("align") + return [] + + monkeypatch.setattr(cadence, "_cache_path", lambda *a, **k: tmp_path / "c.json") + monkeypatch.setattr(cadence, "_transcribe_faster_whisper", fake_transcribe) + monkeypatch.setattr(cadence, "_release_cuda_memory", fake_release) + monkeypatch.setattr(cadence, "_align_with_whisperx", fake_align) + monkeypatch.setattr(cadence, "_write_cache", lambda *a, **k: None) + + cadence.load_or_transcribe( + video=tmp_path / "v.mp4", + wav_16k=tmp_path / "a.wav", + model="large-v3", + language="en", + track_idx=0, + no_cache=True, + backend="whisper", + ) + + # The model is gone and its memory released before the aligner is loaded. + assert events == ["transcribe", "release", "align"]