From 1609a9f11c18db08ff70e09a83bc9c13241be740 Mon Sep 17 00:00:00 2001 From: pedro-bento Date: Sat, 17 Oct 2026 21:24:46 +0100 Subject: [PATCH] fix(whisper): don't force CTranslate2's cub_caching allocator 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. --- README.md | 13 +++++++------ cadence.py | 27 +++++++++------------------ tests/test_whisper_fallback.py | 5 ++++- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 18d01a5..694afc0 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,13 @@ Presets: `super-relaxed`, `relaxed`, `balanced`, `punchy`. See all options with ## GPU notes (Linux / NVIDIA) -Transcription is memory-lean and degrades instead of crashing: cadence starts -with `int8_float16` and, if CTranslate2 still runs out of VRAM partway through a -long file (common with `large-v3` on a 6 GB card), it drops and releases the -model and retries with `int8` + greedy decoding before finally falling back to -CPU. When unset, `CT2_CUDA_ALLOCATOR=cub_caching` is selected for a more robust -allocator. +Transcription is memory-lean and degrades instead of crashing. On CUDA it uses +`int8_float16` by default (`large-v3` in `float16` keeps ~3.7 GB resident and +runs out of VRAM on 6 GB cards). If CTranslate2 still reports an out-of-VRAM +error partway through a long file, cadence drops and releases the model and +retries with `int8` + greedy decoding, then CPU, instead of aborting. The +CTranslate2 CUDA allocator is left at its default: forcing `cub_caching` +pre-reserves a large working set and makes things worse. ## Development diff --git a/cadence.py b/cadence.py index 0153fdd..8f53086 100755 --- a/cadence.py +++ b/cadence.py @@ -56,7 +56,7 @@ from typing import Literal logger = logging.getLogger("cadence") -__version__ = "0.3.2" +__version__ = "0.3.3" # Regex to catch elongated sounds like 'aaaa', 'uhhh', 'ummm', 'mmmm', 'eeeh', etc. HESITATION_REGEX = re.compile( @@ -119,9 +119,9 @@ VERBATIM_PROMPT = ( ) # faster-whisper / CTranslate2 GPU configs tried in order when transcription -# runs out of VRAM. large-v3 in float16 needs ~4.5 GB, which leaves a 6 GB card -# no headroom for decoder spikes, so we start memory-lean and step down on OOM. -# Each tuple is (device, compute_type, beam_size). +# runs out of VRAM. large-v3 in float16 keeps ~3.7 GB resident, which leaves a +# 6 GB card no headroom for decoder spikes, so we start memory-lean and step +# down on OOM. Each tuple is (device, compute_type, beam_size). FASTER_WHISPER_FALLBACKS: tuple[tuple[str, str, int], ...] = ( ("cuda", "int8_float16", 5), ("cuda", "int8", 1), @@ -413,25 +413,16 @@ def _transcribe_mlx( return words, raw_segments -def _configure_ct2_cuda_allocator() -> None: - """Prefer CTranslate2's caching CUDA allocator. - - CTranslate2's default ``cuda_malloc_async`` allocator can surface spurious - "CUDA failed with error out of memory" errors under fragmentation or memory - pressure (OpenNMT/CTranslate2#690); ``cub_caching`` reuses freed blocks and - is more robust across a long transcription. Must be set before - ``ctranslate2``/``faster_whisper`` is imported. - """ - if platform.system() == "Linux": - os.environ.setdefault("CT2_CUDA_ALLOCATOR", "cub_caching") - - def _load_faster_whisper_model( model_size: str, device: str | None = None, compute_type: str | None = None, ): - _configure_ct2_cuda_allocator() + # Leave CTranslate2's CUDA allocator at its default (``cuda_malloc_async`` + # on modern GPUs). Forcing ``cub_caching`` pre-reserves a large working set + # -- measured ~2 GB extra on an RTX 3060 -- which pushes large-v3 over a + # 6 GB card's budget and *causes* the OOM it was meant to avoid, so + # CT2_CUDA_ALLOCATOR is deliberately never set here. preload_nvidia_libs() from faster_whisper import WhisperModel diff --git a/tests/test_whisper_fallback.py b/tests/test_whisper_fallback.py index f59fe81..68605c2 100644 --- a/tests/test_whisper_fallback.py +++ b/tests/test_whisper_fallback.py @@ -37,7 +37,10 @@ def test_load_model_defaults_to_memory_lean_cuda(monkeypatch): cadence._load_faster_whisper_model("large-v3") assert calls == [("large-v3", {"device": "cuda", "compute_type": "int8_float16"})] - assert os.environ["CT2_CUDA_ALLOCATOR"] == "cub_caching" + # 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):