All checks were successful
CI / lint-and-test (push) Successful in 11s
Forcing CT2_CUDA_ALLOCATOR=cub_caching pre-reserves a large working set (~2 GB extra measured on an RTX 3060), which pushes large-v3 over a 6 GB card's budget and causes the very OOM it was meant to avoid. Leave the default cuda_malloc_async allocator in place. Measured on an RTX 3060 Laptop (6 GB): large-v3 int8_float16 keeps ~1.9 GB resident with the default allocator and transcribes a 2750 s file at a ~3.1 GB peak; with cub_caching the same model needs ~3.9 GB before decoding. Reviewed-on: #9 Co-authored-by: pedro-bento <mail@pbento.pt> Co-committed-by: pedro-bento <mail@pbento.pt>
138 lines
5 KiB
Python
138 lines
5 KiB
Python
"""CUDA OOM fallback for faster-whisper transcription.
|
|
|
|
faster-whisper large-v3 in float16 needs ~4.5 GB, leaving a 6 GB card no
|
|
headroom for CTranslate2 decoder spikes, so a long transcription can die
|
|
partway through with ``CUDA failed with error out of memory``. The loader must
|
|
start memory-lean and ``_transcribe_faster_whisper`` must step down a fallback
|
|
ladder (dropping and releasing each model) instead of aborting.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import types
|
|
from pathlib import Path
|
|
|
|
import cadence
|
|
|
|
|
|
def test_is_cuda_oom_classifies_allocation_failures():
|
|
assert cadence._is_cuda_oom(RuntimeError("CUDA failed with error out of memory"))
|
|
assert cadence._is_cuda_oom(RuntimeError("out of memory"))
|
|
assert not cadence._is_cuda_oom(RuntimeError("boom"))
|
|
|
|
|
|
def test_load_model_defaults_to_memory_lean_cuda(monkeypatch):
|
|
monkeypatch.delenv("CT2_CUDA_ALLOCATOR", raising=False)
|
|
monkeypatch.setattr(cadence.platform, "system", lambda: "Linux")
|
|
|
|
calls: list[tuple[str, dict]] = []
|
|
|
|
class _FakeWhisperModel:
|
|
def __init__(self, model_size, **kwargs):
|
|
calls.append((model_size, kwargs))
|
|
|
|
fake_module = types.SimpleNamespace(WhisperModel=_FakeWhisperModel)
|
|
monkeypatch.setitem(sys.modules, "faster_whisper", fake_module)
|
|
|
|
cadence._load_faster_whisper_model("large-v3")
|
|
|
|
assert calls == [("large-v3", {"device": "cuda", "compute_type": "int8_float16"})]
|
|
# We must NOT force a CUDA allocator: cub_caching pre-reserves ~2 GB and
|
|
# makes large-v3 OOM on 6 GB cards, so the default (cuda_malloc_async) is
|
|
# left untouched.
|
|
assert "CT2_CUDA_ALLOCATOR" not in os.environ
|
|
|
|
|
|
def test_load_model_honours_explicit_device(monkeypatch):
|
|
monkeypatch.setattr(cadence.platform, "system", lambda: "Linux")
|
|
|
|
calls: list[tuple[str, dict]] = []
|
|
|
|
class _FakeWhisperModel:
|
|
def __init__(self, model_size, **kwargs):
|
|
calls.append((model_size, kwargs))
|
|
|
|
fake_module = types.SimpleNamespace(WhisperModel=_FakeWhisperModel)
|
|
monkeypatch.setitem(sys.modules, "faster_whisper", fake_module)
|
|
|
|
cadence._load_faster_whisper_model("large-v3", device="cpu", compute_type="int8")
|
|
|
|
# No silent substitution: the explicit config is passed straight through.
|
|
assert calls == [("large-v3", {"device": "cpu", "compute_type": "int8"})]
|
|
|
|
|
|
def test_transcribe_steps_down_ladder_on_cuda_oom(monkeypatch):
|
|
attempts: list[tuple[str, str]] = []
|
|
runs = {"n": 0}
|
|
|
|
def fake_load(model_size, device=None, compute_type=None):
|
|
attempts.append((device, compute_type))
|
|
return object()
|
|
|
|
def fake_run(wav, model, language, beam_size):
|
|
runs["n"] += 1
|
|
if runs["n"] == 1:
|
|
raise RuntimeError("CUDA failed with error out of memory")
|
|
return (
|
|
[cadence.Word("hi", 0.0, 1.0)],
|
|
[{"start": 0.0, "end": 1.0, "text": "hi"}],
|
|
)
|
|
|
|
monkeypatch.setattr(cadence, "_load_faster_whisper_model", fake_load)
|
|
monkeypatch.setattr(cadence, "_run_faster_whisper", fake_run)
|
|
monkeypatch.setattr(cadence, "_release_cuda_memory", lambda: None)
|
|
|
|
words, raw_segments = cadence._transcribe_faster_whisper(Path("a.wav"), "large-v3")
|
|
|
|
assert words == [cadence.Word("hi", 0.0, 1.0)]
|
|
assert raw_segments == [{"start": 0.0, "end": 1.0, "text": "hi"}]
|
|
assert attempts == [("cuda", "int8_float16"), ("cuda", "int8")]
|
|
|
|
|
|
def test_transcribe_propagates_non_oom_errors(monkeypatch):
|
|
attempts: list[tuple[str, str]] = []
|
|
|
|
def fake_load(model_size, device=None, compute_type=None):
|
|
attempts.append((device, compute_type))
|
|
return object()
|
|
|
|
def fake_run(wav, model, language, beam_size):
|
|
raise RuntimeError("unrelated")
|
|
|
|
monkeypatch.setattr(cadence, "_load_faster_whisper_model", fake_load)
|
|
monkeypatch.setattr(cadence, "_run_faster_whisper", fake_run)
|
|
monkeypatch.setattr(cadence, "_release_cuda_memory", lambda: None)
|
|
|
|
try:
|
|
cadence._transcribe_faster_whisper(Path("a.wav"), "large-v3")
|
|
except RuntimeError as e:
|
|
assert "unrelated" in str(e)
|
|
else:
|
|
raise AssertionError("expected RuntimeError")
|
|
|
|
# A non-OOM error must not be swallowed into a second attempt.
|
|
assert len(attempts) == 1
|
|
|
|
|
|
def test_transcribe_raises_after_every_fallback_fails(monkeypatch):
|
|
attempts: list[tuple[str, str]] = []
|
|
|
|
def fake_load(model_size, device=None, compute_type=None):
|
|
attempts.append((device, compute_type))
|
|
return object()
|
|
|
|
def fake_run(wav, model, language, beam_size):
|
|
raise RuntimeError("CUDA failed with error out of memory")
|
|
|
|
monkeypatch.setattr(cadence, "_load_faster_whisper_model", fake_load)
|
|
monkeypatch.setattr(cadence, "_run_faster_whisper", fake_run)
|
|
monkeypatch.setattr(cadence, "_release_cuda_memory", lambda: None)
|
|
|
|
try:
|
|
cadence._transcribe_faster_whisper(Path("a.wav"), "large-v3")
|
|
except RuntimeError as e:
|
|
assert "fallback" in str(e)
|
|
else:
|
|
raise AssertionError("expected RuntimeError")
|
|
|
|
assert len(attempts) == len(cadence.FASTER_WHISPER_FALLBACKS)
|