Skip to content

Clear the MLX buffer cache after each prefill chunk - #710

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/prefill-clear-cache
Oct 23, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/prefill-clear-cache

Conversation

@aleroot

@aleroot aleroot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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 in PrefillParameters.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:

Model Prompt Before After
Qwen3-14B-4bit 16k 18.4 GB 14.5 GB
Qwen3.5-9B-8bit 8k 17.3 GB 12.8 GB
Falcon-H1R-7B-8bit 8k 13.4 GB 10.5 GB
GLM-4-9B-0414-8bit 8k 14.1 GB 11.6 GB
gemma-4-e4b-it-4bit 8k 8.4 GB 6.1 GB

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:

main this PR
footprint peak 18.3 GB 11.8 GB
TTFT 42.6 s 39.3 s

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

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 davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, thank you.

@davidkoski
davidkoski merged commit 991266a into ml-explore:main Oct 23, 2026
4 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants