Repository navigation
DeepSeek-V3: populate kvHeads and update the KV cache once per attention call - #457
Merged
davidkoski merged 1 commit intoJul 22, 2026
Conversation
…ion call Two bugs on the cached path, so DeepSeek-V3 could not generate. 1. `kvHeads` was declared `[Int] = []` and never assigned. `newCache` derives the layer count from `kvHeads.count`, so it returned an empty array, and the first `cache?[i]` in the layer loop trapped with "Index out of range". Any generation crashed; only a `cache: nil` forward pass worked. Populate it the way the other models do: one entry per layer. 2. Attention called `cache.update(...)` explicitly and then passed the resulting keys/values to `attentionWithCacheUpdate`, which performs the update itself. The second update appended the already-returned history back into the cache, so a prompt of L tokens left the cache at 2L and every subsequent token attended over duplicated keys. Drop the explicit update and let the helper own the single one, matching Llama/Qwen3/Cohere and the Python reference. This also routes the quantized-cache path correctly, which the manual update bypassed. Adds DeepseekV3Tests with a cache-growth regression test: a 5-token forward pass must leave every layer's cache at offset 5. With (1) unfixed the test crashes; with (1) fixed and (2) unfixed it reports 10. Reported by @davidkoski in ml-explore#379, where the same double-update was found in the DeepSeek-V2 port. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
davidkoski
approved these changes
Jul 22, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Fix looks great, thank you!
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
…ion call (ml-explore#457) Two bugs on the cached path, so DeepSeek-V3 could not generate. 1. `kvHeads` was declared `[Int] = []` and never assigned. `newCache` derives the layer count from `kvHeads.count`, so it returned an empty array, and the first `cache?[i]` in the layer loop trapped with "Index out of range". Any generation crashed; only a `cache: nil` forward pass worked. Populate it the way the other models do: one entry per layer. 2. Attention called `cache.update(...)` explicitly and then passed the resulting keys/values to `attentionWithCacheUpdate`, which performs the update itself. The second update appended the already-returned history back into the cache, so a prompt of L tokens left the cache at 2L and every subsequent token attended over duplicated keys. Drop the explicit update and let the helper own the single one, matching Llama/Qwen3/Cohere and the Python reference. This also routes the quantized-cache path correctly, which the manual update bypassed. Adds DeepseekV3Tests with a cache-growth regression test: a 5-token forward pass must leave every layer's cache at offset 5. With (1) unfixed the test crashes; with (1) fixed and (2) unfixed it reports 10. Reported by @davidkoski in ml-explore#379, where the same double-update was found in the DeepSeek-V2 port. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This was referenced Sep 30, 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
Two bugs on DeepSeek-V3's cached path. Together they mean the model cannot generate — only a
cache: nilforward pass works.This came out of @davidkoski's review on #379, which flagged the same double-update in the DeepSeek-V2 port and noted "DeepseekV3 would need the same fix". It does — plus a more basic one that hides it.
1.
kvHeadswas never populated (generation crashes)newCachederives the layer count fromkvHeads.count, so it returned an empty array. The layer loop then doescache?[i], which traps immediately:Fixed by populating it as the other models do — one entry per layer:
2. The KV cache was updated twice per attention call
Attention called
cache.update(...)explicitly and then passed the resulting keys/values toattentionWithCacheUpdate, which performs the update itself. The second update appended the already-returned history back into the cache, so a prompt of L tokens left the cache at 2L and every subsequent token attended over duplicated keys.Dropped the explicit update and let the helper own the single one, matching
Llama/Qwen3/Cohereand the Python reference. This also routes the quantized-cache path correctly, which the manual update bypassed.Test
Adds
DeepseekV3Tests(there were none) with a cache-growth regression test — a tiny config, no downloads. A 5-token forward pass must leave every layer's cache at offset 5.The two bugs isolate cleanly, which is how I verified the test actually discriminates:
kvHeadsempty (currentmain)Index out of range, test cannot runkvHeadsfixed, double-update presentcache advanced by 10 for 5 tokensFull
MLXLMTestsrun is green (223 XCTest, 0 failures). Note:Gemma4ChunkedPrefillTestsfails on my machine both before and after this change — a pre-existing tight-tolerance numeric issue unrelated to DeepSeek.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes🤖 Generated with Claude Code