Repository navigation
Clear the MLX buffer cache after each prefill chunk - #710
Merged
Merged
Conversation
Each prefill chunk grows the KV cache into new, larger buffers. The buffers it outgrows return to the MLX buffer cache, but no later chunk can reuse them, so they stay there until generation clears the cache on its first token. On long prompts this cache grows by gigabytes. mlx-lm calls mx.clear_cache() after every prefill chunk. Do the same in PrefillParameters.forEachChunk, which drives the prefill of every LLM and VLM, so all models get the fix.
davidkoski
approved these changes
Oct 23, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good, thank you.
wballard
added a commit
to swissarmyhammer/mlx-swift-lm-old
that referenced
this pull request
Oct 24, 2026
Apply the upstream commits d02948b..991266a as a 3-way patch. CLAUDE.md is excluded. - 649f431 update for new TF32 comparison issues (ml-explore#721) - 5ae2635 switch to MLXLogger (ml-explore#702) - 260ada0 add ScriptedLogitsModel (ml-explore#719) - 0ea4465 MLXVLM: Fix Qwen3VL attention masks after cache rotation (ml-explore#706) - db2e7bf Expose request-scoped generation observations in ChatSession (ml-explore#714) - 991266a Clear the MLX buffer cache after each prefill chunk (ml-explore#710) Conflicts: - Package.swift: both sides add the same README exclude; kept ours. - ChatSession.swift: took upstream's transformer-based startGeneration and dropped the GenerationRun struct. Kept the fork's ReasoningHistory and thinkingEnabled capture. startGeneration takes an optional promptTokenCount, so the default path still counts the leading and checkpoint prefill tokens. No new hybrid or recurrent models, so the post-merge ArraysCache.advance checks do not apply. Tests: MLXLMTests 894 XCTest and 1753 Swift Testing tests pass. MLXGuidedGenerationTests 70, MLXFoundationModelsTests 374, CXGrammarTests 7, MLXHuggingFaceMacrosTests 5 pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Proposed changes
Each prefill chunk grows the KV cache into new, larger buffers. The buffers it outgrows return to the MLX buffer cache, but later chunks need larger ones, so they are never reused. They stay in the cache until the first generated token clears it. mlx-lm calls
mx.clear_cache()after every prefill chunk. This PR does the same inPrefillParameters.forEachChunk, which drives the prefill of every LLM and VLM.Peak process footprint on an M4 Pro (24 GB). Greedy output was identical, and time to first token and decode speed were unchanged or slightly better:
This does not change
Memory.cacheLimit. Apps keep control of that trade-off.Also covers
VarianceNormalizedKVCache(#329).Qwen3-14B-4bit, 8k prompt, M4 Pro 24 GB, same output:
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.