Skip to content

fix(deepseekv4): make the layer cache not trimmable and copy its pooled caches - #275

Merged
Gajesh2007 merged 6 commits into
mainfrom
fix/deepseekv4-layer-cache
Oct 2, 2026
Merged

Gajesh2007 merged 6 commits into
mainfrom
fix/deepseekv4-layer-cache

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

Fixes #238

Line numbers are on main at 0871943, in Libraries/MLXLLM/Models/DeepseekV4.swift. The reference is mlx-lm mlx_lm/models/deepseek_v41.py (ml-explore/mlx-lm@53b9af3).

Defect 1: the layer cache reports itself trimmable. isTrimmable (249) and trim(_:) (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 the PoolingCache objects (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) checks isTrimmable and then trims, so it can reach this.
Reference: deepseek_v41.py:299-301: is_trimmable returns False, 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 same PoolingCache objects to the new cache. An update of the copy changes the original.

Fix.

  • isTrimmable is false. trim(_:) trims nothing and returns 0, so a direct call also leaves the cache as it is.
  • A new 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 of newCache(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 report isTrimmable == false, canTrimPromptCache is false, trimPromptCache(cache, numTokens: 4) returns 0, every offset stays 6, and a direct trim(4) returns 0. The old code reports trimmable (offset 6 < window 8), and trimPromptCache returns 4, so the test fails on it.
  • copyDoesNotShareThePooledState: the copies have their own PoolingCache objects. 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 glob Tests/MLXLMTests/DeepseekV4*.swift (added by #229) covers the new test file.

Merge order. This PR is based on fix/deepseekv4-new-cache (#229). #229 makes newCache(parameters:) give DeepseekV4LayerCache, 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 to main.

Checklist

  • 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 ran swift format on the new test file. DeepseekV4.swift is not formatted on main, so I formatted only my own lines by hand.)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml)

🤖 Generated with Claude Code

Review follow-up (2026-10-02)

davidtai and others added 2 commits September 30, 2026 21:00
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>
@Gajesh2007
Gajesh2007 changed the base branch from fix/deepseekv4-new-cache to main October 2, 2026 23:36
@Gajesh2007
Gajesh2007 merged commit 5746eb4 into main Oct 2, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeepseekV4: the layer cache reports itself trimmable and copy() shares pooled state

2 participants