From 3a0de38f54dd6279af86b7c1ce9475d63f746b93 Mon Sep 17 00:00:00 2001 From: Dmitry Pimenov Date: Fri, 7 Aug 2026 13:15:03 +0300 Subject: [PATCH] fix: periodically clear MLX cache in transcription loop to bound memory growth - Each VAD segment has a different audio length/shape, and MLX's buffer cache grows unbounded across a long loop of transcribe() calls since nothing ever calls mx.clear_cache() between iterations - On a ~5 hour recording (1415 VAD segments) this pushed a 32GB Apple Silicon Mac into ~10GB of swap - Tested: 300-segment synthetic benchmark shows peak cache growing from 955MB (clear every 5) to 1429MB (never clearing) with no plateau; clearing every 20 segments also measured *faster* wall-clock time than never clearing (135.5s vs 161.9s), not just lower memory - Verified clear_cache() doesn't change output: same fixed input transcribed before/after 40 unrelated calls with periodic clearing produces byte-identical text and segments --- whispermlx/asr.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/whispermlx/asr.py b/whispermlx/asr.py index cd1e43215..bd730e2fc 100644 --- a/whispermlx/asr.py +++ b/whispermlx/asr.py @@ -1,6 +1,7 @@ import contextlib import io +import mlx.core as mx import mlx_whisper as _mlx_whisper_module import numpy as np from tqdm import tqdm @@ -138,6 +139,8 @@ def transcribe( chunk_text = mlx_result.get("text", "").strip() avg_logprob = _compute_avg_logprob(mlx_result.get("segments", [])) + if (idx + 1) % 20 == 0: + mx.clear_cache() pbar.update(1) if verbose: tqdm.write(