Repository navigation
Commit bfc2462
feat: load glm_moe_dsa / deepseek_v3_2 with dense attention (stage 1) (#48)
* test: cover the all-KV-shared assistant and its chunked prefill
The MTP unit tests built their assistant from a config with
num_kv_shared_layers: 0, which no shipped assistant checkpoint uses — real
ones are entirely KV-shared and carry no K/V projections at all. So the
existing tests exercised a model that cannot occur in practice, and dual-model
MTP aborting on any chunk-prefilled prompt (#46) passed straight through them.
Adds a matching config (every layer shared) and two tests:
- a plain forward must reach the trunk, asserted by comparing logits against
the trunk directly rather than merely surviving the call
- MTPTokenIterator with prefillStepSize squeezed to 2, which forces the
chunked prefill path with a 5-token prompt
The second is the regression proper. prepare() only forwards a chunk when the
prompt exceeds prefillStepSize; below it the model is never called, which is
why a 512-token threshold hid the bug behind every short test prompt. Lowering
the threshold reproduces it without a multi-gigabyte model pair — the smallest
real one is 7.1 GB, more than GitHub's 10 GB per-repo cache budget can host
alongside the existing modalities.
Verified red-green: with the delegation in Gemma4AssistantModel.callAsFunction
removed, both abort with the production error
Layer 0 is a KV-shared layer but received no sharedKV
Note that a regression here surfaces as a crashed test run rather than a failed
assertion, since the guard is a fatalError. Full suite: 98 tests, 16 suites,
all passing.
Co-Authored-By: Claude Opus 5 <[email protected]>
* feat: load glm_moe_dsa / deepseek_v3_2 with dense attention (stage 1)
GLM-5.2 uses `glm_moe_dsa`, which in mlx-lm is 54 lines whose model class is
a bare `class Model(DSV32Model)` — it is DeepSeek V3.2, not its own
architecture. V3.2 in turn is V3 plus DeepSeek Sparse Attention: a lightning
indexer scores cached keys and attention is restricted to the top
`index_topk`. The V3 half of that already exists in this repo, so both model
types map to one new Swift model built on the V3 stack.
The indexer is not implemented here. That is not an approximation below
`index_topk`, because the reference returns no selection at all until the
cache is longer than it:
if k.shape[2] <= self.index_topk:
return None
GLM-5.2 ships index_topk 2048, so output is exact for the first 2048
positions and diverges past them — dense instead of top-2048 sparse, a
long-context quality question rather than a failure. Implementing the indexer
is stage 2 and also closes the DeepseekV4Compressor/Indexer TODO.
Config decoding differs from V3 in one way worth noting: GLM-5.2 carries no
top-level rope_theta, nesting it in `rope_parameters` (mlx-lm unpacks this in
__post_init__). Both layouts are accepted. `rope_type: "default"` is
deliberately not forwarded as rope_scaling — a non-nil dict there sends the V3
stack looking for yarn fields the config does not have.
Also generalises a trap in DeepseekV3.sanitize, which dropped
`model.layers.61` as a string literal. That number is just numHiddenLayers —
V3 has 61 layers and its MTP block sits at index 61 — but GLM-5.2 has 78, so
the literal deleted a real layer while keeping the actual MTP block. It now
filters on `layerIdx >= numHiddenLayers`, identical for V3 and correct for any
depth. Covered both ways: a 78-layer model keeps layer 61 and drops 78, a
61-layer model still drops 61. Verified red-green — under the old literal the
GLM case fails on both counts.
Tests are tiny random-init configs in the style of the Gemma 4 suite; they
establish shape, flow and weight handling, not numerics against a reference.
Nothing here is validated against real weights: the smallest glm_moe_dsa
checkpoint is 308 GB. Full suite: 104 tests, 17 suites, passing.
Refs SharpAI/SwiftLM#111
Co-Authored-By: Claude Opus 5 <[email protected]>
---------
Co-authored-by: Aegis AI Assistant <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>1 parent b1b348e commit bfc2462
4 files changed
Lines changed: 406 additions & 1 deletion
File tree
- Libraries/MLXLLM
- Models
- Tests/MLXLMTests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
53 | 58 | | |
54 | 59 | | |
55 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
437 | 437 | | |
438 | 438 | | |
439 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
440 | 452 | | |
441 | 453 | | |
442 | 454 | | |
| |||
480 | 492 | | |
481 | 493 | | |
482 | 494 | | |
483 | | - | |
| 495 | + | |
484 | 496 | | |
485 | 497 | | |
486 | 498 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
0 commit comments