Repository navigation
fix: cast the dequantized MLA latent to the query dtype in DeepSeek V2 and V3 - #3202
Conversation
…2 and V3 A multi-token step over an int8 latent dequantized the window with LatentWindow::into_dense, which returns FP16. On a bf16 checkpoint those keys met bf16 queries, MLX promoted the attention to float32, and the float32 carried through o_proj into the residual stream for that step. Both sites now go through mla_batched::dense_latent_in with the step's q_pe dtype, as the Kimi and GLM-4.7-Flash sites do (lablup#3174, lablup#3176). A dense (fp16) window is returned untouched. Closes lablup#3177.
…n step A multi-token step that the cost model absorbs (a short chunk behind a long enough cache, such as a speculative verify or a chunked prefill tail) reaches MlaLatentCache::attend_preprojected, whose quantized arm dequantized the window to FP16 and ran absorbed_attention with the step's bf16 queries. On a bf16 checkpoint that promoted the step to float32, the same defect lablup#3177 fixes at the decompressed site. The window is now cast to the q_pe dtype there too, for every latent width (int8 and the int4 option of lablup#3189 both take this arm). The V2 test now also runs a 2-token absorbed step behind the 5-token prefill and checks it stays bf16; it fails without the cast (float32 output). It also restores the tiny_harness_model doc comment that the new test had absorbed. Refs lablup#3177, lablup#3202
Hardening reviewReviewed against #3177 and the latent options merged in #3189 ( The two call sites in this PR are right, and Finding: DeepSeek V2's absorbed multi-token step still promoted to float32On DeepSeek V2 the cost model ( Fix (e2efc33): in Test: the V2 The branch was updated with a merge of current Security and performanceNo new input surface. The cast runs only on the quantized multi-token arm, once per step, and is skipped when the dtypes already match. Validation (Apple Silicon, Metal)
Not done here: the M3 Ultra teacher-forced gates from acceptance criterion 4. |
Summary
A multi-token step over an int8 MLA latent in DeepSeek V2 and
DeepSeekV3Attentiondequantized the window to FP16 and attended it with the step's queries; on a bf16 checkpoint that promoted the step to float32. Both sites now callmla_batched::dense_latent_inwith the ropedq_pedtype, as the issue proposes and as the Kimi and GLM sites do. The fp16 arm is untouched.Related issues
Closes #3177. Context: epic #3158.
Type of change
fix— bug fixChange
src/models/deepseek_v2.rs(decompressed branch offorward_absorbed): theupdate_and_fetchresult is bound first and goes throughdense_latent_in(window, array_dtype(&q_pe))instead ofinto_dense().src/models/deepseek_v3.rs(DeepSeekV3Attention::forward):window => dense_latent_in(window, array_dtype(&q_pe))instead ofwindow.into_dense().src/models/mla_batched.rs: thedense_latent_indoc comment and itsUsed by:line name DeepSeek V2 and theDeepSeekV3Attentionfamilies.deepseek_v32.rsandglm4_moe_lite_mtp_hooks.rsare out of scope, as in the issue.Tests
Two tests named
a_bf16_layer_keeps_bf16_over_an_int8_latent, modeled on the Kimi Linear one:src/models/deepseek_v3_latent_tests.rs: a bf16DeepSeekV3Attentionover aKVCacheMode::Int8cache returns bf16 for a 5-token prefill and for the following decode step; the fp16 latent of the same layer is checked as a control.src/models/deepseek_v2_tests.rs: the same for the V2MLAAttention, with a check that the cold prefill took the decompressed path.Where they fail without the fix, which is not everywhere. With the two call sites put back as on
mainand the tests kept:maincall sitescargo test --lib, no backend feature) on the GB10 hostleft: 10(float32),right: 12(bf16)--features cuda)On CUDA the layer output is bf16 on
maintoo. My reading of why: the CUDA overlay's promotion table (overlays/cuda/mlx/dtype.cpp) keeps bf16 x float16 at float32 but makes bf16 x float32 give bf16, so the attention would still run promoted to float32 over the FP16 window ando_projwould bring it back to bf16. I did not trace the intermediate dtypes to confirm that. Either way the tests cannot tell the two apart on CUDA; I did not change them to look inside the attention. I have no Metal machine, so "fails on current main" is shown on the CPU build only.Re-measurement (acceptance criterion 4), on CUDA
The issue asks for the #3155 / #3160 teacher-forced gates on Moonlight-16B-A3B and Kimi-VL-A3B behind 1024 and 4096 tokens. I can only run them on a GB10, with a corpus of my own, so these are not the M3 Ultra rows of
mla-quantized-latent-v3-m3ultra-2026-11-19.mdand I have not touched that document orCHANGELOG.md.Setup: release
logit_tracebuilt fromd6ef8437f(main) and from this PR;mlx-community/Moonlight-16B-A3B-Instruct-4-bit@8503ff1a2andmlx-community/Kimi-VL-A3B-Thinking-4bit@c4f541be1; corpus =CHANGELOG.mdatd6ef8437f(378697 bytes, 102121 tokens for Moonlight); the commands of that document (64 8 8 1024and64 4 8 4096,MLXCEL_TRACE_MLA_STATS=1 MLXCEL_TRACE_BATCH_ROWS=1 MLXCEL_LOGIT_TRACE_SCALAR_REFERENCE=1, the int8 arm withMLXCEL_TRACE_KV_CACHE_MODE=int8);compare_logit_traces.py --decided 2.Nothing moves on CUDA. All eight traces (two checkpoints, two prefixes, fp16 and int8) are byte-identical between
mainand this PR. The int8 traces did exercise the changed code: Moonlight behind 1024 recordsdecompressed=216 absorbed_composed=13824.int8 against fp16, the same on both builds:
The one decided disagreement on Kimi-VL behind 1024 is there on
mainas well (the traces are identical), so it is not from this change; it is a different corpus and backend from the M3 Ultra rows, which had none. I mention it because the gate is zero.Since the traces are identical, the cast is a no-op for these two checkpoints on this backend, which fits their activations already being FP16 here; I did not verify the dtype directly. Whether the numbers move on Metal, where the issue expects the int8 arm to change, still needs someone with that hardware.
Test plan
Run on this PR's commit (one commit on
d6ef8437f, tree89cef6aea8dc) on a DGX Spark (GB10).cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warningscargo test --lib deepseek_v3_latentequivalent (--profile test-fast --features cuda --lib -- deepseek_v3_latent deepseek_v3::latent): 11 passedcargo test --lib deepseek_v2equivalent (same flags,deepseek_v2): 20 passed--lib -- deepseek kimi mla glm4_moe_lite longcat youtu joyai: 552 passedmain: both fail (table above)make verify-test-cuda(cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 16130 passed, 4 failed, 500 ignored. The four are themla::turbo_latenttests that fail on unmodifiedmainon this host (fix(cuda): reductions with a differing output dtype return garbage (sum over bool, any/all over non-bool) #3184, fixed by fix(cuda): write reductions through the output dtype #3185)generateon both checkpoints with--kv-cache-mode fp16andint8, two prompts each: byte-identical betweenmainand this PR (8 runs), and a text prompt on three unrelated checkpoints likewiseci.ymljobs a fork PR does not get, by hand:clippy(cargo clippy -p mlxcel --lib --tests -- -D warnings, default features); license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refsCHANGELOG.mdare not updatedChecklist
docs/: not done, see above (no M3 Ultra numbers to record)// Used by: ...comments:dense_latent_in