Repository navigation
fix(deepseekv4): make the layer cache not trimmable and copy its pooled caches - #275
Merged
Merged
Conversation
DeepseekV4Model did not implement newCache(parameters:), so the default of KVCacheDimensionProvider gave a KVCacheSimple for each layer, and makeCache(parameters:) was never called. Generation then ran without the sliding window and without the pooled caches. newCache now returns the caches of makeCache: a rotating window for each layer, and the pooled caches for the compressed attention layers. Fixes #194 (part: defect 3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed caches DeepseekV4LayerCache reported the trimmable flag of its rotating cache, and its trim moved back only the rotating window. The pooled state of the PoolingCache objects stayed, so after a trim it held tokens that the window no longer had. The cache is now not trimmable and trim does nothing, as in mlx-lm deepseek_v41.py. copy() passed the same PoolingCache objects to the new cache, so an update of the copy changed the original. It now copies each one. Fixes #238 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 2, 2026
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
Fixes #238
Line numbers are on
mainat 0871943, inLibraries/MLXLLM/Models/DeepseekV4.swift. The reference is mlx-lmmlx_lm/models/deepseek_v41.py(ml-explore/mlx-lm@53b9af3).Defect 1: the layer cache reports itself trimmable.
isTrimmable(249) andtrim(_:)(252) use the rotating cache only. The cache says it is trimmable while its window is not full, but a trim moves back only the rotating window. It does not undo the pooled state of thePoolingCacheobjects (closed compressed windows and buffered rows). After a trim, the pooled state holds tokens that the window no longer has.trimPromptCache(Libraries/MLXLMCommon/KVCache.swift, used by speculative decoding) checksisTrimmableand then trims, so it can reach this.Reference:
deepseek_v41.py:299-301:is_trimmablereturnsFalse, with the comment "Evicted window rows and closed compressed groups cannot be restored."Defect 2:
copy()shares the pooled state.copy()(254-256) copies the rotating cache but gives the samePoolingCacheobjects to the new cache. An update of the copy changes the original.Fix.
isTrimmableisfalse.trim(_:)trims nothing and returns 0, so a direct call also leaves the cache as it is.PoolingCache.copy()gives a new object with the same buffered rows and pooled entries.DeepseekV4LayerCache.copy()uses it for each pooled cache.Test.
Tests/MLXLMTests/DeepseekV4LayerCacheTests.swift, with the tiny random model of #229 (DeepseekV4TinyModel; no real weights). A 6-token prompt fills the caches ofnewCache(parameters:). It is shorter than the window of 8, so the rotating caches alone are trimmable. In the ratio 4 layer it closes 1 pooled window and leaves 2 buffered rows.layerCacheIsNotTrimmable: both layer caches reportisTrimmable == false,canTrimPromptCacheis false,trimPromptCache(cache, numTokens: 4)returns 0, every offset stays 6, and a directtrim(4)returns 0. The old code reports trimmable (offset 6 < window 8), andtrimPromptCachereturns 4, so the test fails on it.copyDoesNotShareThePooledState: the copies have their ownPoolingCacheobjects. One decode step on the copied caches gives the copy 3 buffered rows, and the original must keep 2 buffered rows and 1 pooled window (and 6 buffered rows in the ratio 128 layer). Then the same step on the original must give the same logits as on the copy. The old code shares the objects, so the identity check fails and the original gets 3 buffered rows.I did not build locally; I know both tests fail on the old code by tracing the old code above. The Fork Tests run on this PR is the check that they pass with the fix.
fork.yaml: the section "Nemotron-H 3.5, DeepSeek V4, Laguna, LFM2-MoE" names the fix. Its globTests/MLXLMTests/DeepseekV4*.swift(added by #229) covers the new test file.Merge order. This PR is based on
fix/deepseekv4-new-cache(#229). #229 makesnewCache(parameters:)giveDeepseekV4LayerCache, which makes these defects reachable from generation, and adds the tiny model that the tests use. This PR merges after #229. GitHub then moves the base tomain.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (I ranswift formaton the new test file.DeepseekV4.swiftis not formatted onmain, so I formatted only my own lines by hand.)fork.yaml)🤖 Generated with Claude Code
Review follow-up (2026-10-02)