Repository navigation
Commit c97035c
Fix missing YaRN mscale softmax-scale correction in DeepSeek MLA (#548)
## Root cause
DeepSeek-V2/V3's MLA attention applies **two independent** YaRN
corrections in HuggingFace's reference implementation
(`modeling_deepseek_v2.py`):
1. `attention_scaling` on the RoPE cos/sin cache — rescales only the
*rope* portion of Q/K. Mobius's `YarnRope` already implements this
correctly.
2. A separate softmax-scale multiplier applied inside
`DeepseekV2Attention.__init__` (HF's `yarn_apply_mscale`): `scaling =
qk_head_dim**-0.5 * mscale**2`, where `mscale = 0.1 * mscale_all_dim *
log(factor) + 1.0`. This rescales the **entire** attention logit (rope +
nope portions alike).
`src/mobius/components/_deepseek_mla.py` implemented (1) but not (2), on
the mistaken assumption (captured in the removed code comment) that (1)
alone reproduces the needed effect. That's false whenever `mscale ==
mscale_all_dim` (as in `deepseek-ai/DeepSeek-V2-Lite`, both 0.707):
(1)'s ratio-based `attention_factor` collapses to exactly `1.0` (a
no-op), while (2) is still a real ~1.59x correction that HF applies.
`deepseek-ai/DeepSeek-V3`'s official config
(`mscale=mscale_all_dim=1.0`) has the same gap (missing ~1.87x
correction).
### Symptom
`DeepSeekMLA` reproduced HF logits almost exactly at **position 0** (a
single-key softmax is scale-invariant to any positive multiplier) but
diverged by up to **~0.9 absolute / 93% of elements at position ≥ 1**,
for both `DeepSeek-V2-Lite` and a real `DeepSeek-V3` config —
reproducible even in a single **dense** (non-MoE) layer, which isolated
the bug to attention/RoPE scaling rather than MoE routing.
## Fix
Added `yarn_apply_mscale()` in
`src/mobius/components/_rotary_embedding.py`, mirroring HF's helper of
the same name exactly (same guards: no-op when `rope_type == "default"`,
no-op when `mscale_all_dim` is falsy). `DeepSeekMLA.__init__` now calls
it to compute `self.scaling` whenever no explicit `scale=` override is
passed.
## Before / after evidence
Using real HF configs (random weights, single dense MLA layer / full
4-layer model):
| Config | Before (max abs diff, position ≥ 1) | After |
|---|---|---|
| `deepseek-ai/DeepSeek-V2-Lite` (factor=40,
mscale=mscale_all_dim=0.707) | up to ~0.9 | ~5e-6 (FP noise) |
| `deepseek-ai/DeepSeek-V3` config (factor=40,
mscale=mscale_all_dim=1.0) | missing ~1.87x scale (confirmed via config
inspection) | corrected |
## Tests
-
`tests/integration_test.py::test_deepseek_v2_lite_prefill_logits_match`
and `::test_deepseek_non_mla_decoder_prefill_logits_match` — **both
previously FAILED** with large divergence at prefill positions ≥ 1;
**now PASS** at `rtol=atol=1e-3`.
- `src/mobius/components/_deepseek_mla_test.py` — 4 new unit tests:
exact `mscale^2` correction value (mirroring `DeepSeek-V2-Lite`'s real
rope_scaling), the `rope_type == "default"` no-op guard, the
`mscale_all_dim == 0` no-op guard, and that an explicit `scale=`
override still wins.
- `tests/synthetic_parity_test.py`: removed the `deepseek_v3` atol
override of `0.04` (previously attributed to "MoE dispatch FP
accumulation" — actually this same bug being masked) and replaced it
with an empirically measured `0.0025`. Residual FP-order noise from
parallel MoE dispatch measured at ~3.5e-5 in a bare interpreter and a
reproducible ~1.2e-3 under this repo's pytest process (ORT intra-op
thread-schedule dependent, not seed-dependent); `0.0025` keeps ~2x
headroom over the higher measurement while being 16x tighter than the
old `0.04`.
### Commands run
```
python3 -m pytest src/mobius/components/_deepseek_mla_test.py src/mobius/components/_rotary_embedding_test.py -q
python3 -m pytest tests/integration_test.py -k "deepseek_v2_lite_prefill_logits_match or deepseek_non_mla_decoder_prefill_logits_match" -q
python3 -m pytest "tests/synthetic_parity_test.py::test_synthetic_parity" -k "deepseek or dots1 or glm4" -q
python3 -m pytest tests/synthetic_parity_test.py -q # full suite: only pre-existing, unrelated `modernbert-decoder` failure (confirmed present on origin/main too)
ruff check src/mobius/components/_deepseek_mla.py src/mobius/components/_deepseek_mla_test.py src/mobius/components/_rotary_embedding.py tests/synthetic_parity_test.py
ruff format --check <same files>
```
All pass (25/25 MLA unit tests incl. 4 new, 63/63 combined MLA+rotary
unit tests, 2/2 target integration tests, full synthetic-parity suite
green aside from the pre-existing unrelated `modernbert-decoder`
failure).
## Out of scope (not included in this PR)
- GLM-5.2 (`glm_moe_dsa`) registration/export — separate, unrelated
follow-up (tracked via PR #404).
- The `deepseek_v4` synthetic-parity config-override bug in
`tests/_test_configs.py` (`Weight shape mismatch for
model.layers.0.self_attn.q_b_proj.weight`) — separate, unrelated
fast-follow.
---
Working as Sapper (Systems Dev for Models & Preprocess).
Co-authored-by: Copilot <[email protected]>
---------
Signed-off-by: Justin Chu <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>1 parent b9c9043 commit c97035c
4 files changed
Lines changed: 115 additions & 10 deletions
File tree
- src/mobius/components
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
103 | 110 | | |
104 | 111 | | |
105 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
106 | 108 | | |
107 | 109 | | |
108 | 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 | + | |
109 | 171 | | |
110 | 172 | | |
111 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
313 | 313 | | |
314 | 314 | | |
315 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
316 | 343 | | |
317 | 344 | | |
318 | 345 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
167 | 176 | | |
168 | 177 | | |
169 | 178 | | |
| |||
0 commit comments