Compare commits
2 commits
76724271d1
...
5d9f86cd4d
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d9f86cd4d | |||
| 8a3bc4babc |
2 changed files with 78 additions and 7 deletions
37
cadence.py
37
cadence.py
|
|
@ -34,6 +34,7 @@ Features:
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import gc
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
|
|
@ -55,7 +56,7 @@ from typing import Literal
|
|||
|
||||
logger = logging.getLogger("cadence")
|
||||
|
||||
__version__ = "0.2.1"
|
||||
__version__ = "0.2.2"
|
||||
|
||||
# Regex to catch elongated sounds like 'aaaa', 'uhhh', 'ummm', 'mmmm', 'eeeh', etc.
|
||||
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")
|
||||
|
||||
|
||||
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,
|
||||
)
|
||||
|
|
|
|||
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