Skip to content

feat: read the int8 MLA latent inside the fused Metal decode kernel - #3173

Merged
inureyes merged 3 commits into
mainfrom
feature/issue-3156-fused-int8-latent-decode
Nov 19, 2026
Merged

inureyes merged 3 commits into
mainfrom
feature/issue-3156-fused-int8-latent-decode

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

--kv-cache-mode int8 on DeepSeek V2 decoded on the composed quantized_matmul path only (#3155). This adds a quantized variant of the fused absorbed-decode kernel that reads the packed latent in-kernel, and routes unmasked single-query steps on a quantized window to it first.

What changed

  • The kernel's partial pass is assembled from shared fragments with two window reads, not copied. The dense variant's assembled Metal source is character-identical to before. The quantized variant reads the codes as bytes and factors each group's scale and bias out of the score and the value sum.
  • Bridge pair mla_absorbed_decode_fused_quantized{,_available}; mla::decode::fused_absorbed_decode_quantized with the eligibility rules (8-bit only, a bits dispatch that refuses 4, group size 64, power-of-two rank, one query, no mask). quantized_absorbed_attention tries it and falls back to the composed path on None. MLXCEL_MLA_FUSED_DECODE=0 disables both variants.
  • FUSED_DECODE_QUANTIZED_MAX_KEYS = 2048, lower than the fp16 kernel's 8192. In-model on an M3 Ultra the kernel against the composed path read +6.5% behind 512 tokens, +0.4% behind 2048 and -4.6% behind 3840, so longer windows keep the composed path.
  • Results: docs/benchmark_results/mla-fused-quantized-decode-m3ultra-2026-11-19.md. On the shipped build int8 decode is 148.4 against 140.2 tok/s behind 512 (+5.9%), +1.5% behind 1792, and unchanged behind 2048 and 16384.

Deviations from the issue

  • The parity tests are in mla/fused_quant_decode_tests.rs to keep fused_decode_tests.rs under the file size limit. Parity at 8192 keys calls the kernel through the bridge, since the decode path no longer gives it that window.
  • The trace behind 4096 runs the composed path in both arms under the 2048 limit (the traces are byte-identical); a trace behind 1920 covers the kernel on a long window.
  • MLXCEL_MLA_FUSED_DECODE=0 --kv-cache-mode int8 alone records decompressed, because DeepSeek V2 defaults to the decompressed layout on Metal without the kernel (perf(mla): make absorbed decode the DeepSeek V2 default and validate Kimi-VL on the fused kernel #3131). With MLXCEL_MLA_ABSORBED=1 it records absorbed_composed.

Test plan

  • cargo test --profile test-fast --features metal,accelerate -p mlxcel-core --lib mla:: -- --test-threads=1: 79 passed. Fused quantized against composed quantized at 1, 255, 2048 and 8192 keys: 5e-7 (f32), 5.6e-4 (fp16), 6.25e-3 (bf16) relative, the last two being one unit in the last place of the output dtype.
  • Teacher-forced trace, int8 fused against int8 composed: 0 of 205 decided and 6 of 512 top-1 behind 1024 (largest gap 0.078), 0 of 113 and 3 of 256 behind 1920 (0.109). Against the fp16 latent: 0 decided, largest gap 0.078.
  • fp16 traces behind 1024 and 4096 byte-identical to a release build of 43f1c8d4.
  • cargo clippy -p mlxcel-core --lib --tests -- -D warnings, cargo fmt --check, make verify-kernel-dtype-keys verify-kernel-port-dispatch verify-env-var-docs.
  • make verify and workspace clippy were not run locally (deferred to CI at merge time). CUDA and ROCm were not measured; no port there.

Closes #3156

#3155 decodes an affine int8 latent on a composed quantized_matmul path, so a quantized cache lost the fused kernel's short-context speed. The kernel's partial pass is now assembled from shared fragments with two window reads: the dense one, whose assembled Metal source is character-identical to before, and a quantized one that reads the packed codes as bytes and factors each group's scale and bias out of the score and the value sum, so the window is never dequantized.

mla::decode::fused_absorbed_decode_quantized gates it (8-bit only with a bits dispatch that refuses 4, group size 64, power-of-two rank, unmasked single query) and quantized_absorbed_attention tries it before the composed path. MLXCEL_MLA_FUSED_DECODE=0 turns off both variants.

The window limit is FUSED_DECODE_QUANTIZED_MAX_KEYS = 2048, not the fp16 kernel's 8192: on an M3 Ultra, V2-Lite int8 decode on the kernel against the composed path measured +6.5% behind 512 tokens, +0.4% behind 2048 and -4.6% behind 3840. On the shipped build int8 decode is 148.4 against 140.2 tok/s behind 512 and unchanged from 2048 on.

Validated with parity tests against the composed quantized path (f32 5e-7, fp16 5.6e-4, bf16 6.25e-3 relative, bounds stated per dtype), a teacher-forced trace behind 1024 and 1920 tokens with 0 decided disagreements and top-1 flips only at gaps up to 0.109, and byte-identical fp16 traces against a build of 43f1c8d. cargo test -p mlxcel-core --lib mla:: (79 passed), clippy on mlxcel-core, fmt, verify-kernel-dtype-keys and verify-kernel-port-dispatch pass. CUDA and ROCm have no port and were not measured.

Refs #3156
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific labels Nov 19, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Route an unmasked single-query step over an int8 MLA latent to a quantized variant of the fused Metal decode kernel, falling back to the #3155 composed path.

Findings Addressed

None needed: no CRITICAL, HIGH, MEDIUM or LOW findings.

Deviations judged

  • FUSED_DECODE_QUANTIZED_MAX_KEYS = 2048: justified. The in-model sweep puts fused against composed at +6.5% (512), +0.4% (2048) and -4.6% (3840), so the in-model crossover is near 2048 even though the isolated microbenchmark crosses near 6000.
  • 4096 trace exercises only the fallback, 1920 trace added: justified. The 4096 int8 trace is byte-identical to the composed arm (shows the fallback engages), and the 1920 trace (windows 1921 to 1984 keys) covers the kernel's long-window range and meets the gate (0 decided, largest gap 0.109).
  • Power-of-two rank 64..1024: justified. Each lane's R / 32 elements must sit inside one group of 64 so the affine pair factors out; the target models use rank 512. Rust and the launcher both refuse other ranks, and a test covers rank 192.
  • Tests in mla/fused_quant_decode_tests.rs: matches the decode_tests.rs sibling-file convention.
  • mla_absorbed_decode.cpp at 501 lines: no 500-line limit exists. docs/code-guidelines.md says under 800 lines is fine as a single file, so no change.

Verification

  • All stated requirements implemented (int8 kernel variant, bits dispatch that refuses 4, bridge pair, Rust gate, parity at 1/255/2048/8192 keys in f32, fp16 and bf16, traces, decode table, docs, CHANGELOG)
  • No placeholder/mock code remaining
  • Integrated into project code flow (MlaLatentCache::attend_preprojected, then quantized_absorbed_attention, then the fused gate; the strided cache-window test and mlxcel generate counters confirm it)
  • Project conventions followed
  • Existing modules reused where applicable (one kernel body built from shared fragments, shared launch and merge, select_kernel_port)
  • No unintended structural changes (dense partial and merge Metal sources character-identical to origin/main; no non-MLA KV path touched)
  • Tests pass: 79 mla:: tests on Metal, clippy -D warnings, fmt, verify-kernel-dtype-keys, verify-kernel-port-dispatch, verify-env-var-docs

MLXCEL_MLA_FUSED_DECODE=0 gates both variants through fused_decode_enabled(). A refused input reaches Rust as Err because the bridge is declared -> Result and select_kernel_port and the shape checks throw, so it never aborts; the 4-bit launcher test covers this.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 19, 2026
@inureyes
inureyes marked this pull request as ready for review November 19, 2026 10:18
@inureyes
inureyes merged commit a00629a into main Nov 19, 2026
32 checks passed
@inureyes
inureyes deleted the feature/issue-3156-fused-int8-latent-decode branch November 19, 2026 10:56
@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:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Read an int8 MLA latent inside the fused Metal decode kernel

1 participant