Compare commits
No commits in common. "5d9f86cd4d41c7934d0c79c5ef67547899668a06" and "76724271d1f48890c30d7b77ab3da9dd6ac52d5f" have entirely different histories.
5d9f86cd4d
...
76724271d1
2 changed files with 7 additions and 78 deletions
37
cadence.py
37
cadence.py
|
|
@ -34,7 +34,6 @@ 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
|
||||||
|
|
@ -56,7 +55,7 @@ from typing import Literal
|
||||||
|
|
||||||
logger = logging.getLogger("cadence")
|
logger = logging.getLogger("cadence")
|
||||||
|
|
||||||
__version__ = "0.2.2"
|
__version__ = "0.2.1"
|
||||||
|
|
||||||
# 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(
|
||||||
|
|
@ -401,26 +400,6 @@ 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,
|
||||||
|
|
@ -517,6 +496,7 @@ 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]:
|
||||||
|
|
@ -536,12 +516,11 @@ 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(wav_16k, model, language=language)
|
words, raw_segments = _transcribe_faster_whisper(
|
||||||
|
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,
|
||||||
|
|
@ -1958,10 +1937,7 @@ def main() -> None:
|
||||||
phrase_fillers = set(DISCOURSE_FILLERS)
|
phrase_fillers = set(DISCOURSE_FILLERS)
|
||||||
|
|
||||||
backend = detect_backend()
|
backend = detect_backend()
|
||||||
# The ASR model is loaded lazily per input (inside load_or_transcribe) and
|
stt_model = _load_faster_whisper_model(args.model) if backend != "mlx" else None
|
||||||
# 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] = []
|
||||||
|
|
@ -2015,6 +1991,7 @@ 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,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
"""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