fix/whisper-vram-handoff (#5)
All checks were successful
CI / lint-and-test (push) Successful in 11s
All checks were successful
CI / lint-and-test (push) Successful in 11s
Reviewed-on: #5 Co-authored-by: pedro-bento <mail@pbento.pt> Co-committed-by: pedro-bento <mail@pbento.pt>
This commit is contained in:
parent
76724271d1
commit
1b0d902285
2 changed files with 78 additions and 7 deletions
37
cadence.py
37
cadence.py
|
|
@ -34,6 +34,7 @@ Features:
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import gc
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
@ -55,7 +56,7 @@ from typing import Literal
|
||||||
|
|
||||||
logger = logging.getLogger("cadence")
|
logger = logging.getLogger("cadence")
|
||||||
|
|
||||||
__version__ = "0.2.1"
|
__version__ = "0.2.2"
|
||||||
|
|
||||||
# Regex to catch elongated sounds like 'aaaa', 'uhhh', 'ummm', 'mmmm', 'eeeh', etc.
|
# Regex to catch elongated sounds like 'aaaa', 'uhhh', 'ummm', 'mmmm', 'eeeh', etc.
|
||||||
HESITATION_REGEX = re.compile(
|
HESITATION_REGEX = re.compile(
|
||||||
|
|
@ -400,6 +401,26 @@ def _load_faster_whisper_model(model_size: str):
|
||||||
return WhisperModel(model_size, device="cpu", compute_type="int8")
|
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(
|
def _transcribe_faster_whisper(
|
||||||
wav: Path,
|
wav: Path,
|
||||||
model_size: str,
|
model_size: str,
|
||||||
|
|
@ -496,7 +517,6 @@ def load_or_transcribe(
|
||||||
track_idx: int,
|
track_idx: int,
|
||||||
no_cache: bool,
|
no_cache: bool,
|
||||||
backend: str,
|
backend: str,
|
||||||
stt_model=None,
|
|
||||||
align_cache: dict | None = None,
|
align_cache: dict | None = None,
|
||||||
extract: Callable[[], None] | None = None,
|
extract: Callable[[], None] | None = None,
|
||||||
) -> list[Word]:
|
) -> list[Word]:
|
||||||
|
|
@ -516,11 +536,12 @@ def load_or_transcribe(
|
||||||
if backend == "mlx":
|
if backend == "mlx":
|
||||||
words, raw_segments = _transcribe_mlx(wav_16k, model, language=language)
|
words, raw_segments = _transcribe_mlx(wav_16k, model, language=language)
|
||||||
else:
|
else:
|
||||||
words, raw_segments = _transcribe_faster_whisper(
|
words, raw_segments = _transcribe_faster_whisper(wav_16k, model, language=language)
|
||||||
wav_16k, model, language=language, model=stt_model
|
|
||||||
)
|
|
||||||
|
|
||||||
if raw_segments:
|
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(
|
aligned_words = _align_with_whisperx(
|
||||||
raw_segments=raw_segments,
|
raw_segments=raw_segments,
|
||||||
wav_16k=wav_16k,
|
wav_16k=wav_16k,
|
||||||
|
|
@ -1937,7 +1958,10 @@ def main() -> None:
|
||||||
phrase_fillers = set(DISCOURSE_FILLERS)
|
phrase_fillers = set(DISCOURSE_FILLERS)
|
||||||
|
|
||||||
backend = detect_backend()
|
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 = {}
|
align_cache: dict = {}
|
||||||
|
|
||||||
track_data_list: list[VideoTrackData] = []
|
track_data_list: list[VideoTrackData] = []
|
||||||
|
|
@ -1991,7 +2015,6 @@ def main() -> None:
|
||||||
track_idx=resolved_track_idx,
|
track_idx=resolved_track_idx,
|
||||||
no_cache=args.no_cache,
|
no_cache=args.no_cache,
|
||||||
backend=backend,
|
backend=backend,
|
||||||
stt_model=stt_model,
|
|
||||||
align_cache=align_cache,
|
align_cache=align_cache,
|
||||||
extract=do_extract,
|
extract=do_extract,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
48
tests/test_whisper_unload.py
Normal file
48
tests/test_whisper_unload.py
Normal file
|
|
@ -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"]
|
||||||
Loading…
Reference in a new issue