Repository navigation
Add DeepSeek-V2 model - #379
Conversation
|
End-to-end validation (real weights) — caught and fixed a load bug. Loading After the fix, forward-pass parity on a fixed token sequence
Byte-exact match (MLA + YaRN + group-routed MoE + quantized loading). |
6c8efeb to
7d7e1bb
Compare
davidkoski
left a comment
There was a problem hiding this comment.
Looks good, but take a look at the question on the double KVCache update. DeepseekV3 would need the same fix, assuming this is an issue.
Port of mlx-lm's deepseek_v2.py. Reuses the in-tree DeepSeek-V3 MLA machinery (q_a/q_b + kv_a_with_mqa/kv_b compression, qk nope/rope split, shared initializeRope YaRN path) and SwitchGLU MoE with shared experts. Differs from V3 in the router (DeepseekV2MoEGate): plain softmax scoring (no sigmoid + e_score_correction_bias), optional group_limited_greedy group masking by per-group max (vs V3's top-2-group-sum), and an unconditional routed_scaling_factor. Dense layers below first_k_dense_replace use the standard MLP. sanitize() stacks per-expert gate/up/down projections into switch_mlp. Registered as "deepseek_v2". Tests (DeepseekV2Tests, no model download): forward-pass logits shape, deterministic group-limited-greedy routing (winning group confines top-k), and sanitize() expert-stacking. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
DeepSeek-V2-Lite sets `q_lora_rank: null` — direct q_proj with no low-rank q compression — while full DeepSeek-V2 sets 1536. The config previously defaulted a null/absent value to 1536, which built the q_a/q_b LoRA path and failed to load V2-Lite checkpoints (q_proj weight present, q_a_proj/q_b_proj expected). Make qLoraRank `Int?`; nil ⇒ q_proj. Caught by end-to-end load of mlx-community/DeepSeek-V2-Lite-Chat-4bit-mlx. Adds a regression test (null → nil, 1536 → 1536) and switches the test config to the realistic null form. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The attention path called `cache.update(...)` explicitly and then passed the
resulting keys/values to `attentionWithCacheUpdate`, which performs the cache
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 `attentionWithCacheUpdate` 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.
The new test pins cache growth: a 5-token forward pass must leave every layer's
cache at offset 5. Before this change it reported 10.
Also fix the `topk_method` default spelling ("gready" -> "greedy"), carried over
from the Python port. It is cosmetic: the only comparison is against
"group_limited_greedy", and real configs set the key explicitly.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
7d7e1bb to
8e11ca5
Compare
davidkoski
left a comment
There was a problem hiding this comment.
New model looks good, thank you!
…ion call (#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 #379, where the same double-update was found in the DeepSeek-V2 port. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
…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>
Add DeepSeek-V2 model
Port of mlx-lm's deepseek_v2.py, reusing the in-tree V3 MLA machinery (q_a/q_b + kv_a_with_mqa/kv_b compression, qk nope/rope split, YaRN rope) and SwitchGLU MoE with shared experts. Router differs from V3: plain softmax scoring, optional group-limited-greedy masking, unconditional routed_scaling_factor. Dense layers below first_k_dense_replace use standard MLP.
Fixes along the way:
- Make `qLoraRank` optional (`Int?`); V2-Lite sets `q_lora_rank: null` and needs direct q_proj instead of the q_a/q_b LoRA path. Fixes loading mlx-community/DeepSeek-V2-Lite-Chat-4bit-mlx.
- Update the KV cache once per attention call instead of twice — the explicit `cache.update()` plus `attentionWithCacheUpdate`'s internal update was duplicating cached keys/values, doubling cache size and corrupting attention beyond the first token.
- Fix `topk_method` default typo ("gready" → "greedy"), cosmetic only.
Includes tests for logits shape, group-limited-greedy routing, expert-stacking in `sanitize()`, qLoraRank parsing, and KV cache growth.
…arallel Gemma 4: tensor-parallel hooks and CBv2 layer submission
Proposed changes
Adds the DeepSeek-V2 architecture (
model_type: "deepseek_v2") to MLXLLM — a port of mlx-lm'sdeepseek_v2.py. Covers DeepSeek-V2 / V2-Lite, which had no Swift implementation.It reuses the in-tree DeepSeek-V3 MLA machinery (
q_a/q_b+kv_a_proj_with_mqa/kv_blow-rank compression, qk nope/rope split, the sharedinitializeRopeYaRN path) andSwitchGLUMoE with shared experts.It differs from V3 only in the router (
DeepseekV2MoEGate):e_score_correction_bias),group_limited_greedygroup masking by each group's max score (vs V3's top-2-per-group sum),routed_scaling_factor.Dense layers below
first_k_dense_replaceuse the standard MLP.sanitize()stacks per-expert gate/up/down projections (weight/scales/biases) intoswitch_mlp.Tests (
DeepseekV2Tests, no model download): forward-pass logit shape; a deterministic group-limited-greedy routing test (synthetic gate weights → the winning group confines top-k); andsanitize()expert-stacking. Verified viaxcodebuild test ... -only-testing:MLXLMTests/DeepseekV2Tests→ 3 tests, 0 failures, TEST SUCCEEDED.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes