Repository navigation
fix(deepseekv4): make newCache give the compressed caches - #229
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>
This was referenced Oct 1, 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 #194 (part: defect 3).
Defect.
Libraries/MLXLLM/Models/DeepseekV4.swift:1595-1626.DeepseekV4Modeldid not implementnewCache(parameters:). The default ofKVCacheDimensionProvidergave aKVCacheSimplefor each layer, andmakeCache(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_cacheis the cache that generation uses.Fix.
newCache(parameters:)returnsmakeCache(parameters:).makeCachestays;newCacheand 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 withoutwithKnownIssue): the cache types per layer.newCacheHasTheWindowAndThePooledCaches: window 8 on each layer, 2 pooled caches of ratio 4 on layer 1, 1 of ratio 128 on layer 2.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.swiftis 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
withKnownIssueblock innewCacheGivesTheCompressedCachesofDeepseekV4ForwardPassTestsin #184. The pull request that merges second (this one or #184) must remove the namedwithKnownIssueblock 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.swiftin full, and that file is not formatted onmain. None of the flagged lines is added by this pull request. A format-only pull request forDeepseekV4.swiftfollows after #227-#231 merge.Checklist
swift formaton the changed Swift files (the new test files in full; inDeepseekV4.swiftonly the changed lines, because the file on main is not formatted)fork.yamlnow namesTests/MLXLMTests/DeepseekV4*.swift🤖 Generated with Claude Code