cadence/tests/test_whisper_unload.py
pedro-bento 8a3bc4babc 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.
2026-10-05 21:42:28 +01:00

48 lines
1.6 KiB
Python

"""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"]