Skip to content

fix(deepseekv4): make newCache give the compressed caches - #229

Merged
Gajesh2007 merged 3 commits into
mainfrom
fix/deepseekv4-new-cache
Oct 2, 2026
Merged

Gajesh2007 merged 3 commits into
mainfrom
fix/deepseekv4-new-cache

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

Fixes #194 (part: defect 3).

Defect. Libraries/MLXLLM/Models/DeepseekV4.swift:1595-1626. DeepseekV4Model did not implement newCache(parameters:). The default of KVCacheDimensionProvider gave a KVCacheSimple for each layer, and makeCache(parameters:) (line 1626) was never called. Generation ran without the sliding window and without the pooled caches. On main, a prompt in chunks with these caches even stops with [broadcast_shapes] Shapes (1,1,66,130) and (1,2,66,66).

Reference. mlx-lm mlx_lm/models/deepseek_v41.py:1076-1077 (at a9bd8af): make_cache is the cache that generation uses.

Fix. newCache(parameters:) returns makeCache(parameters:). makeCache stays; newCache and the #184 test call it.

Tests (Tests/MLXLMTests/DeepseekV4NewCacheTests.swift):

  • newCacheGivesTheCompressedCaches (copied from test: add kernel forward-pass tests for served and fork-changed text models #184 without withKnownIssue): the cache types per layer.
  • New: newCacheHasTheWindowAndThePooledCaches: window 8 on each layer, 2 pooled caches of ratio 4 on layer 1, 1 of ratio 128 on layer 2.
  • New: newCacheGivesTheLogitsOfMakeCache: chunks and decode steps give the same logits with both caches.

This pull request does not depend on the pool mask fix: the tests compare the two caches with each other, not with the full pass. Local run: 3 tests pass; on main all 3 fail.

Tests/MLXLMTests/DeepseekV4TinyModel.swift is a small copy of the helper pieces of #183 (SyntheticModel, ForwardPassChecks) and of the model setup of #184, under another name so that nothing collides when those pull requests merge. Each DeepSeek V4 fix pull request adds the same file with the same content, so they merge without conflict.

Merge note

After this pull request and #184 merge, remove the withKnownIssue block in newCacheGivesTheCompressedCaches of DeepseekV4ForwardPassTests in #184. The pull request that merges second (this one or #184) must remove the named withKnownIssue block in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.

Lint. The Lint check is red. The linter formats DeepseekV4.swift in full, and that file is not formatted on main. None of the flagged lines is added by this pull request. A format-only pull request for DeepseekV4.swift follows after #227-#231 merge.

Checklist

  • I have read the CONTRIBUTING document
  • I have run swift format on the changed Swift files (the new test files in full; in DeepseekV4.swift only the changed lines, because the file on main is not formatted)
  • 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 now names Tests/MLXLMTests/DeepseekV4*.swift

🤖 Generated with Claude Code

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>
@Gajesh2007
Gajesh2007 merged commit fb10800 into main Oct 2, 2026
4 of 5 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: pool mask not causal, no newCache, FP8 sanitize keeps scale keys, hyper-connection ops path differs from the reference

2 participants