Repository navigation
feat: write the GLM and LongCat MLA latent through the latent view - #3176
Merged
Merged
Conversation
GLM-4.7-Flash and LongCat cached their (kv_latent, k_pe) pair through KVCache::update_and_fetch, so no quantized latent could reach them. Both now store it through mla::MlaLatentCache: GLM in its single-sequence forward, batched decode and MTP verify and rollback (the drafter cache follows the target mode table), LongCat in both MLA sub-layers per layer, with model-owned caches that take the per-layer mode table. A single-token int8 step reads the latent absorbed. Neither family joins mla::QUANTIZED_LATENT_FAMILIES, so int8 still resolves to fp16. GLM failed the gate on glm-4.7-flash-4bit: 2 decided-position disagreements behind 1024 and 2 behind 4096 tokens, with a zero fp16 kernel noise floor. LongCat was not traced because its checkpoint did not finish downloading. Tests opt each model in to cover the ported path. kv_arch now counts LongCat's two latent caches per layer (it sized LongCat at half), and logit_trace hands the requested mode to a model that reports a mode table, which had run GLM's int8 arm as fp16. Targeted lib tests pass (719), and clippy and fmt pass. Refs #3161
LatentWindow::into_dense returns the dequantized window in the cache's fp16 sidecar dtype. Under a bf16 model (GLM-4.7-Flash is bf16) a multi-token step then mixed bf16 queries with fp16 keys, MLX promoted the attention to f32, and the promotion carried through o_proj into the residual stream, so the int8 arm's whole context pass and its logits ran in f32 while the fp16 arm ran bf16. The GLM gate traces were taken on that arm, so their int8-vs-fp16 numbers conflate the codec with a compute-dtype change. mla_batched::dense_latent_window casts a quantized window to the queries' dtype; dense windows pass through untouched, so fp16 latent numerics are unchanged. GLM and LongCat multi-token steps use it. New tests build bf16 tiny models of both families and fail before the fix (int8 prefill logits came back f32). The results doc, CHANGELOG and turbo-kv-cache.md now say the gate must be re-measured, correct the noise-floor rows (GLM's fp16 latent never runs a second kernel on any trace arm, and the batched-row counters were swapped), and record that the two int8 kernels agree within bf16 rounding at GLM geometry yet move 33 positions. Refs #3161
Member
Author
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
The first GLM trace ran the int8 context pass in f32 (fixed in 4feb841), so the gate was re-run on the fixed build with glm4_moe_lite temporarily listed. It still fails: 1 decided-position disagreement behind 1024 tokens (gap 2.75) and 0 behind 4096, against a gate of zero. That count equals the int8 fused-vs-composed kernel-noise floor (1 decided, gap 6.5), and the composed int8 arm has 0 against fp16. The ppl delta is now -3.1% and -0.4%. glm4_moe_lite stays unlisted, so int8 resolves to fp16. The results doc, CSV, CHANGELOG and turbo-kv-cache.md carry the new numbers, with the pre-fix rows kept as superseded. Refs #3161
LongCat-Flash-Lite passed the #3161 gate on an M3 Ultra: 0 decided-position disagreements against the fp16 latent behind 1024 tokens (of 239) and 4096 (of 121), 9 and 5 top-1 flips above 0.11 (largest gap 0.375), perplexity -0.7% and -1.2%. The int8 kernel pair (fused against composed) also has 0 decided and 11 flips. longcat_flash and longcat_flash_ngram join mla::QUANTIZED_LATENT_FAMILIES and the registry int8 kv_modes. The 560B longcat_flash shares LongcatFlashNgramModel and is not measured on this host. Two defects that predate this issue blocked the checkpoint on every mode. The quantized word and n-gram embedding tables loaded as dense embeddings, so the first projection threw; they now load through UnifiedEmbedding. The MoE handed SwitchGLU [B, L, hidden], which works by accident below 64 positions and threw in the sorted path at 64 or more; tokens are flattened first, with a 64-token test. Decode on the int8 latent: +8.0% behind 512 tokens (fused), -15.5% behind 2048, -49.2% behind 16384. Narrow LongCat, registry, turbo_args, kv_arch, advisor, allowlist and mla tests pass serially under the GPU lock; clippy and fmt are clean. Refs #3161
The n-gram embedder lookups take ids modulo m + 2i + 1 and cast them to INT32, and MLX gather does not range-check a positive index, so a checkpoint whose embedders, word_embeddings or embed_tokens table was shorter or narrower than the config claims read past the end of the table on an ordinary prompt. Each table is now checked with the shared validate_embedding_table guard, which handles dense and quantized tables. The config arithmetic also panicked or overflowed on hostile values: emb_neighbor_num below 2, emb_split_num of 0, a hidden_size that is not a multiple of the embedder count, an overflowing ngram_vocab_size_ratio * vocab_size, and vocab sizes above i32::MAX. These are now Err results naming the config field, raised before compute_vocab_mods runs. LongcatFlashMoE::forward reads the router's configured top_k instead of unwrapping the last index axis, which is the same value. Tests cover a one-row-short embedders.0 table and emb_neighbor_num of 1. Refs #3161
Member
Author
Security and Performance ReviewFindings addressed (b8cf0c4)
Two tests were added (a one-row-short Checked, no change needed
|
The b8cf0c4 guards (embedding-table shape checks and config arithmetic validation at LongCat load) had no CHANGELOG entry. Add a Fixed entry naming the out-of-bounds gather risk and the config values that now fail with a named error. Validation: release build, then real loads of longcat-flash-lite-4bit at fp16 and int8 (both load and generate coherent text, int8 runs absorbed_fused=1092) and glm-4.7-flash-4bit with int8 (substitutes fp16 with the warning); cargo fmt --check, both clippy invocations with -D warnings, and the longcat_flash_ngram, glm4_moe_lite and mla_batched test selectors pass. Refs #3161
…m-longcat-quant-latent # Conflicts: # docs/supported-models.md # docs/turbo-kv-cache.md # examples/logit_trace/kv_mode.rs # src/cli/turbo_args_tests.rs # src/execution/kv_arch.rs # src/lib/mlxcel-core/src/cache/turbo/allowlist.rs # src/lib/mlxcel-core/src/mla/latent_pair.rs # src/models/mla_batched.rs # src/models/registry.rs
inureyes
marked this pull request as ready for review
November 19, 2026 17:40
This was referenced Nov 19, 2026
inureyes
added a commit
that referenced
this pull request
Nov 22, 2026
…2 and V3 (#3202) * fix: cast the dequantized MLA latent to the query dtype in DeepSeek V2 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 (#3174, #3176). A dense (fp16) window is returned untouched. Closes #3177. * fix: cast the dequantized latent on DeepSeek V2's absorbed multi-token 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 #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 #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 #3177, #3202 --------- Co-authored-by: Jeongkyu Shin <[email protected]>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3161
GLM-4.7-Flash and both LongCat types now write the MLA
(kv_latent, k_pe)pair throughmla::MlaLatentCache, and a single-token int8 step reads it absorbed (fused Metal kernel up to 2048 keys, composed beyond). GLM covers the single-sequence forward, batched decode, MTP verify (per-row quantized attention on prefix slices, byte-identical to the decode chain) and rollback, and the drafter cache follows the target mode table. LongCat covers both MLA sub-layers per layer, and its model-owned caches take the per-layer mode table.LongCat is enabled. GLM is not.
longcat_flashandlongcat_flash_ngramjoinmla::QUANTIZED_LATENT_FAMILIESand listfp16andint8in the registry.glm4_moe_litestays off the list, so its--kv-cache-mode int8still resolves to fp16 with the warning.absorbed_fusedbehind 1024 andabsorbed_composedbehind 4096, withdecompressedonly on the context pass. Decode int8 vs fp16 is +8.0%, -15.5% and -49.2% at 512, 2048 and 16384 tokens. The 560Blongcat_flashsharesLongcatFlashNgramModeland was not measured on this host.Also fixed:
[B, L, hidden]toSwitchGLU, which throws in its sorted path at 64 or more positions. A new 64-token test covers it.kv_archnow counts LongCat's two latent caches per layer. It used to size them at half.embed_tokensand n-gram embedding tables againstvocab_size,hidden_sizeand each embedder's modulus, and rejects n-gram config values that would underflow, divide by zero or overflow, with an error naming the field instead of an out-of-range gather or panic.logit_traceran GLM's int8 arm as fp16. It now hands the requested mode to the model, asDirectEnginedoes.Tests:
absorbed_composed=1008) and int8 (absorbed_fused=1092,decompressedonly on the context pass); GLM-4.7-Flash with--kv-cache-mode int8reports effective fp16 with the substitution warning.-D warnings) and fmt pass.Results:
docs/benchmark_results/mla-quantized-latent-glm-longcat-m3ultra-2026-11-19.md.