Repository navigation
feat: add an experimental MLA latent precision option - #3189
Conversation
Epic #3158 fixed in code which MLA families may store their latent quantized, and at 8 bits only. `--mla-latent-quant <auto|fp16|int8|int4>` (env `MLXCEL_MLA_LATENT_QUANT`, the CLI wins) lets an operator who accepts the measured risk choose. `auto` is the default and changes nothing. `mla::effective_latent_bits(model_type, requested, option)` is the single resolution point. The mode resolver builds a forced quantized latent in `int8` storage and an fp16 one in fp16, so no `KVCacheMode` variant is added; `mla::latent_cache_bits` picks the width inside that storage. Sizing, the banner, the load-time flags of GLM-4.7-Flash and LongCat and the server's `--kv-bits` table follow the option; the advisor and the registry keep describing the default gate. A 4-bit latent reuses the affine group-64 codec and always decodes on the composed path, since the fused Metal kernel reads 8-bit codes only. On the DSA families the indexer key stays 8-bit beside a 4-bit latent: the slot is two runs of whole groups in the same buffers. A cache's latent width is read back from its buffers, so it travels with detach, adopt and prompt-cache entries, and an entry saved at one precision is refused at another with `kv_mode_mismatch`. An explicit value prints each of its warnings once, with the measured result per family from one table. Validation: 292 mlxcel-core and 557 mlxcel lib tests covering the option, every MLA family's latent tests, sizing, the advisor, server startup and the adoption check pass on Metal, serially, under the test-fast profile. Refs #3179
Record what `--mla-latent-quant` opts an operator into, measured on an M3 Ultra, and document the option in `docs/turbo-kv-cache.md`, `docs/environment-variables.md` and the changelog. A 4-bit latent has 0 decided-position disagreements against the fp16 latent on Moonlight, LongCat-Flash-Lite, Kimi-Linear and DeepSeek-V2-Lite behind 1024 and 4096 tokens, and misses the 0.11-logit near-tie bar on all four (flips up to 1.13 logits). On GLM-4.7-Flash it shows 3 decided disagreements at each length. The int8 arms run through the option reproduce the epic's published rows, GLM's failing 1 of 193 included. Decode on the 4-bit latent is 3% to 9% faster than int8 where both are composed and 6% to 8% slower where int8 has the fused kernel. The two new measurements are added to `EXPERIMENTAL_LATENT_NOTES`, so the startup warning quotes them. The option's warnings now go to stderr only, since the CLI's tracing subscriber writes there too and the text printed twice. `mlxcel` validates the option only on the commands that take it, so a bad environment value does not fail `list` or `download`. DeepSeek-V3.2 was not traced under the option; its int4 split slot has a coherent generate run and a 4096-token sparse decode run behind it and no quality measurement. Refs #3179
The warning for an explicit `--mla-latent-quant` that disagrees with the requested KV cache mode named `--kv-cache-mode` only, so a server started with `--kv-bits 8 --mla-latent-quant int4` was told about a flag it never received. It now reads "the requested int8 KV cache mode (--kv-cache-mode or --kv-bits)". The results document states which commit each measurement ran on. Refs #3179
Implementation Review SummaryIntent
Findings AddressedNo CRITICAL, HIGH or MEDIUM findings, so no fix commits were needed. Remaining Items
Judgment calls reviewed and accepted
Verification
Checks run against real configs: inspect sizing of 416 B (Moonlight, GLM-4.7-Flash, V2-Lite) and 552 B (DeepSeek-V3.2) under int4. Moonlight int4 generate answered "Paris" with |
Security and performance reviewNo CRITICAL, HIGH or MEDIUM findings, so no fix commits were pushed. Checked and found sound:
LOW, not fixed:
Tests run locally (Metal, serialized): |
…a-latent-quant-option
Address the LOW items from the #3189 implementation and security reviews. - The rule "an explicit --mla-latent-quant decides this model" is one helper, `mla::option_decides_latent` (built on `holds_mla_latent`), used by the resolver, the startup notices, the DSA key-slot refusal and the --kv-bits table. The resolver's copy compared `deepseek_v2` case-sensitively; all four now share the trimmed, case-insensitive family match. - Each `EXPERIMENTAL_LATENT_NOTES` entry is keyed on the one width it was measured at. GLM-4.7-Flash under int4 no longer prints its int8 result, and the DSA families get an int4 note saying the 4-bit slot is unmeasured on the real checkpoint instead of the int8 slot's result. - The memory estimate's `KV dtype` line (`mlxcel inspect`, the preflights) reads `int4 (MLA latent, --mla-latent-quant int4)` when the option packs the latent at 4 bits, instead of the int8 storage mode. - `tune`, `predict`, `mlxcel-bench-engine`, `mlxcel-engine-parity` and `speculative_bench` fix the option from the environment at startup, so a bad value is a startup error there too. The lazy read left for library users prints a bad value on stderr instead of a tracing line, and a non-UTF-8 value is an error rather than `auto`. Validation: make verify-fmt, verify-clippy and verify-env-var-docs pass; targeted Metal tests for mla::, cache::turbo, the CLI option, memory_estimate, turbo_args, cli_input, kv_arch, the advisor, serve_preflight and inspect pass. Refs #3179
Bilingual report on the experimental --mla-latent-quant option: the single resolution point, the 4-bit latent and DSA split slot, the four judgment calls the review accepted, the M3 Ultra trace, decode and sizing results, and what was not measured (DeepSeek-V3.2 int4 quality on the real checkpoint, GLM-5, LongCat-Flash 560B, Kimi K3, CUDA, ROCm). Refs #3179
PR Finalization CompleteMerged origin/main (e427b7a, includes #3190) with a merge commit; Review LOW items:
Technical report: Validation: |
…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
…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]>
Summary
Adds
--mla-latent-quant <auto|fp16|int8|int4>(envMLXCEL_MLA_LATENT_QUANT, the CLI wins): an explicit, experimental choice of the precision every MLA latent cache of the loaded model is stored at.autois the default and changes nothing.int8reaches the families the #3158 gate keeps at fp16 (GLM-4.7-Flash, DeepSeek V3.2, GLM-5);int4stores a 4-bit latent on any MLA family.Design
mla::effective_latent_bits(model_type, requested, option). The mode resolver builds a forced quantized latent inint8storage and an fp16 one in fp16, so there is no newKVCacheMode;mla::latent_cache_bitspicks 8 or 4 inside that storage.stores_quantized_latent_pairandlatent_pair_supports_modeanswer through the option, which carries sizing,turbo_args, the banner and the load-time flags of GLM and LongCat. The advisor and the registry keep describing the default gate.int4the indexer key stays 8-bit, as[latent at 4 bits | indexer key at 8 bits]in the same buffers; the column reads, the row gather andinto_densetake each run at its own width.KvModeMismatch.--kv-bitssteps aside, or the last layer would stay fp16.--kv-cache-modeor--kv-bitsimplies, and the experimental notice quoting one table of measured results. Each note in that table is keyed on exactly one width, so GLM under int4 quotes its int4 result and DSA under int4 says the 4-bit slot is unmeasured on a real checkpoint.option_decides_latentinmla/latent_option.rs, used by the allowlist, the notices, the whole-groups refusal and the--kv-bitsstep-aside, case- and whitespace-insensitive throughout.run,generate,serve,inspect,tune,predict,mlxcel-server,mlxcel-bench-decode,mlxcel-bench-engine,mlxcel-engine-parityandspeculative_benchinitialize the option at startup, so a bad or non-UTF-8 value is a startup error. A forced latent whose rank is not whole 64-column groups falls back to fp16 at startup with a warning.inspectand--estimate-memorylabel the KV dtypeint4 (MLA latent, --mla-latent-quant int4)when the option packs the latent at 4 bits;inspect --jsonkeeps the storage mode inkv_cache_mode.Measurements (M3 Ultra, non-gating)
Tables:
docs/benchmark_results/mla-latent-quant-option-m3ultra-2026-11-20.md.Reports
TECHNICAL_REPORTS/3189-mla-latent-quant-option-20261121.en.mdand.ko.md: design, judgment calls, measurements, what was not measured, and one follow-up (packed_latent_bitsreads any packed width between 4 and 8 bits as 4, correct for the two widths a cache can hold).Test plan
make verify-fmt,make verify-clippy,make verify-env-var-docs,make verify-llama-compat.--profile test-fast --features metal,accelerate -- --test-threads=1) on the first commit: 16084 passed, 1 failed, 498 ignored. The one failure wasserve_preflight_kv_mode_on_deepseek_v2_follows_the_absorption_decision, stale on main since feat: store the absorbed MLA latent as affine int8 on DeepSeek V2 #3168 and fixed there by test: expect int8 from the absorbed DeepSeek V2 serve preflight #3190; after merging main it passes on this branch.mla::,cache::turbo,cache::latent_quant(295 passed); mlxcel lib option,memory_estimate,turbo_args,cli_input,kv_archand advisor tests (358 passed);--bin mlxcelserve_preflight(7) andinspect(8);cli_help_consistency(27). The three bench binaries build.packed_latent_bitsnote above.Closes #3179