Repository navigation
fix: periodically clear MLX cache in transcription loop to bound memory growth - #6
Merged
KalebJS merged 1 commit intoAug 7, 2026
Conversation
…ry 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
📝 WalkthroughWalkthroughThe transcription flow now imports MLX Core and clears the MLX cache after every 20 VAD segments. ChangesMLX Cache Management
Estimated code review effort: 1 (Trivial) | ~3 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Long transcriptions (many VAD segments) leak MLX GPU memory because
DiarizationPipeline/asr.py's per-segment loop never callsmx.clear_cache(). On a ~5 hour recording (1415 VAD segments) this pushed a 32 GB Apple Silicon Mac into ~10 GB of swap.Root cause
whispermlx/asr.py, the per-segment transcription loop, callsmlx_whisper.transcribe()once per VAD segment but never releases MLX's internal buffer cache between calls:MLX exposes
mx.clear_cache()/mx.get_cache_memory()/mx.set_cache_limit()specifically for long-running loops with varying input shapes (each VAD segment has a different duration → different array shape → MLX's allocator keeps caching new buffer sizes without bound).Reproduction / evidence
Isolated test: 300 synthetic segments (2–8s, random durations — mimicking real VAD segment variability),
mlx-community/whisper-large-v3-turbo, measuringmx.get_cache_memory():clear_everySomewhat surprising: periodic
clear_cache()isn't a memory/speed tradeoff here — every interval tested was faster than never clearing, on top of bounding peak cache. Single run per config (not averaged), so exact ranking among the clearing intervals is noisy, but "clearing beats never" held across all 5 configs.Correctness check: transcribing the same fixed input before vs. after 40 unrelated
transcribe()calls with periodicclear_cache()in between produces byte-identical output (textand per-segmenttextmatch exactly) —clear_cache()only affects buffer reuse, not computed results.Proposed fix
Interval (20) is a starting point, not a hard recommendation — happy to share the full tuning script if useful for picking a better default, or to make it configurable (e.g. a
clear_cache_everyparam onDiarizationPipeline/load_model, default on).Environment
--diarizeSummary by CodeRabbit