Skip to content

fix: cast the dequantized MLA latent to the query dtype in DeepSeek V2 and V3 - #3202

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/3177-dense-latent-dtype
Nov 22, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/3177-dense-latent-dtype

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A multi-token step over an int8 MLA latent in DeepSeek V2 and DeepSeekV3Attention dequantized 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 call mla_batched::dense_latent_in with the roped q_pe dtype, 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 fix

Change

  • src/models/deepseek_v2.rs (decompressed branch of forward_absorbed): the update_and_fetch result is bound first and goes through dense_latent_in(window, array_dtype(&q_pe)) instead of into_dense().
  • src/models/deepseek_v3.rs (DeepSeekV3Attention::forward): window => dense_latent_in(window, array_dtype(&q_pe)) instead of window.into_dense().
  • src/models/mla_batched.rs: the dense_latent_in doc comment and its Used by: line name DeepSeek V2 and the DeepSeekV3Attention families.
  • No other change to the attention math. deepseek_v32.rs and glm4_moe_lite_mtp_hooks.rs are 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 bf16 DeepSeekV3Attention over a KVCacheMode::Int8 cache 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 V2 MLAAttention, 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 main and the tests kept:

Build main call sites this PR
CPU-only (cargo test --lib, no backend feature) on the GB10 host both fail: left: 10 (float32), right: 12 (bf16) both pass
CUDA (--features cuda) both pass both pass

On CUDA the layer output is bf16 on main too. 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 and o_proj would 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.md and I have not touched that document or CHANGELOG.md.

Setup: release logit_trace built from d6ef8437f (main) and from this PR; mlx-community/Moonlight-16B-A3B-Instruct-4-bit @ 8503ff1a2 and mlx-community/Kimi-VL-A3B-Thinking-4bit @ c4f541be1; corpus = CHANGELOG.md at d6ef8437f (378697 bytes, 102121 tokens for Moonlight); the commands of that document (64 8 8 1024 and 64 4 8 4096, MLXCEL_TRACE_MLA_STATS=1 MLXCEL_TRACE_BATCH_ROWS=1 MLXCEL_LOGIT_TRACE_SCALAR_REFERENCE=1, the int8 arm with MLXCEL_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 main and this PR. The int8 traces did exercise the changed code: Moonlight behind 1024 records decompressed=216 absorbed_composed=13824.

int8 against fp16, the same on both builds:

Checkpoint Prefix Decided disagreements (gap >= 2) Top-1 disagreements Largest gap at a disagreement Perplexity fp16 / int8
Moonlight 1024 0 / 268 9 / 512 0.422 11.0305 / 11.0739 (+0.393%)
Moonlight 4096 0 / 87 8 / 256 0.664 27.3379 / 27.6741 (+1.230%)
Kimi-VL 1024 1 / 230 13 / 512 2.328 9.5883 / 9.3974 (-1.990%)
Kimi-VL 4096 0 / 70 9 / 256 1.062 30.9393 / 31.2235 (+0.918%)

The one decided disagreement on Kimi-VL behind 1024 is there on main as 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, tree 89cef6aea8dc) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings
  • cargo test --lib deepseek_v3_latent equivalent (--profile test-fast --features cuda --lib -- deepseek_v3_latent deepseek_v3::latent): 11 passed
  • cargo test --lib deepseek_v2 equivalent (same flags, deepseek_v2): 20 passed
  • --lib -- deepseek kimi mla glm4_moe_lite longcat youtu joyai: 552 passed
  • The two new tests on a CPU-only build: pass; with the call sites as on main: 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 the mla::turbo_latent tests that fail on unmodified main on 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)
  • generate on both checkpoints with --kv-cache-mode fp16 and int8, two prompts each: byte-identical between main and this PR (8 runs), and a text prompt on three unrelated checkpoints likewise
  • Self-hosted ci.yml jobs 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 refs
  • Metal: not built or run. The M3 Ultra gates of the issue are not re-measured, and the benchmark document and CHANGELOG.md are not updated
  • DeepSeek-VL2, kimi_k2, joyai_llm_flash, kimi_k25, youtu, youtu_llm, youtu_vl checkpoints: not run; they share the two changed sites
  • ROCm: not built or run

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not done, see above (no M3 Ultra numbers to record)
  • Updated // Used by: ... comments: dense_latent_in

…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.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 22, 2026
…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
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3177 and the latent options merged in #3189 (--mla-latent-quant, int8 and int4 latent widths).

The two call sites in this PR are right, and dense_latent_in covers every quantized width since int8 and int4 both come back as LatentWindow::Quantized. One multi-token path was still missed.

Finding: DeepSeek V2's absorbed multi-token step still promoted to float32

On DeepSeek V2 the cost model (use_absorbed_mla) absorbs a short multi-token step behind a long enough cache, for example a speculative verify or the tail of a chunked prefill. That step goes through view.attend, which calls MlaLatentCache::attend_preprojected. The quantized multi-query arm there dequantized the window to FP16 with into_dense() and ran absorbed_attention with the bf16 queries, so the step was promoted to float32, the same defect as the decompressed site. The issue only listed the decompressed site, and the PR's test only covered the cold prefill (decompressed) and single-token decode (fused or composed, already dtype-safe).

Fix (e2efc33): in src/lib/mlxcel-core/src/mla/cache.rs the quantized multi-query arm now casts the dequantized window to the q_pe dtype, the same rule dense_latent_in applies. The fp16 (Dense) arm is unchanged.

Test: the V2 a_bf16_layer_keeps_bf16_over_an_int8_latent now also runs a 2-token step behind the 5-token prefill, asserts it took AbsorbedPrefill, and asserts a bf16 output. On Metal it fails without the cast (left: 10 float32, right: 12 bf16) and passes with it. I also moved the /// One dense MLA layer over a 16-token vocabulary. doc comment back onto tiny_harness_model; the new test had been inserted below it, so the comment had attached to the test.

The branch was updated with a merge of current main (no force push), so the author's commit is kept.

Security and performance

No 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)

  • cargo fmt --all -- --check: clean
  • cargo clippy -p mlxcel --lib --tests -- -D warnings: clean
  • cargo test --profile test-fast --lib -- deepseek kimi mla glm4_moe_lite longcat youtu joyai --test-threads=1: 552 passed, 0 failed
  • cargo test -p mlxcel-core --profile test-fast --lib -- mla:: --test-threads=1: 117 passed, 0 failed
  • All four a_bf16_layer_keeps_bf16_over_an_int8_latent tests (Kimi Linear, Kimi K3, DeepSeek V3, DeepSeek V2) pass on Metal. This is the Metal "fails before, passes after" evidence the PR could not show on CUDA.

Not done here: the M3 Ultra teacher-forced gates from acceptance criterion 4.

@inureyes
inureyes merged commit e8809ed into lablup:main Nov 22, 2026
32 checks passed
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: cast the dequantized MLA latent to the query dtype in DeepSeek V2 and V3

2 participants