Skip to content

fix: trace and fix the last two speculative verify mismatches - #3420

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3418-verify-mismatches
Nov 25, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3418-verify-mismatches

Conversation

@inureyes

@inureyes inureyes commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Traces the two block-versus-chain mismatches the exactness survey (#3415) left open, fixes Gemma 4 26B A4B, and adds an opt-in exact verify block for the token-only round on the MLA families.

What changed

  • Gemma 4 26B A4B. The narrow-kernel divergence starts at layer 0 MoE experts (shared MLP and router agree): Experts::forward picks its kernel by token count. A verify block now runs the experts per position (MLXCEL_GEMMA4_MOE_VERIFY_PER_POSITION, on). The next divergence was the batched block's attention, so the 16/8/2 geometry joins row-wise verify on Metal. The probe is now equal on the narrow kernel at widths 2, 4, 8, at startup, 600 and 2100 tokens, so the gate engages as on 12B and 31B.
  • Token-only rounds. On DeepSeekV3Model one query takes the fused absorbed MLA kernel and a block takes SDPA. MLXCEL_MLA_ROW_INVARIANT=1 runs each block row as that single query, uses the row-invariant matvec kernel for dense products and per-position float experts, covers Xing4.0 (including its hyper connection's two products), and routes the GLM-4.7-Flash engine block to its row-wise verify forward. Result: equal on gigachat3 bf16; equal with MLXCEL_QMV_WIDE=0 on gigachat3 8-bit, Kimi-VL, moonlight, Xing4.0 8-bit and optiq, and GLM. Prompt-lookup text then matched plain decode on 6 of 6 pairs (4 of 6 without).
  • Off by default. Prompt lookup on a copy-heavy reply drops from 261.7 to 148.5 tok/s on bf16 and from 343.8 to 260.8 on 8-bit. Plain decode does not regress. No gate was added.
  • Arcee, K2 Horizon and OLMoE are located (layer 0 output) and kept as documented.

Not met

  • No 26B A4B drafter is on disk, so MTP throughput there is unmeasured.
  • Item 2's equal holds only with the opt-in (plus MLXCEL_QMV_WIDE=0 on quantized checkpoints).
  • DeepSeek V3.2 and Kimi Linear share the cause and are unchanged.

Test plan

  • New tiny-model Metal tests: gemma4::moe_verify_tests, deepseek_v3::verify_block_tests, a GLM routing test. Each has a companion asserting the pre-change path differs.
  • Narrow lib suites under the GPU lock: gemma4, deepseek_v3, xing4_0, glm4_moe_lite, llama3, arcee, olmoe, youtu, speculative_exactness, coverage_tests.
  • survey_block_vs_chain on the checkpoints above; ABAB decode (8 pairs) and prompt lookup (4 cycles) in docs/benchmark_results/speculative-exactness-followup-m3ultra-2026-11-25.md.
  • cargo clippy --lib --tests --bins and -p mlxcel-core --lib --tests with -D warnings (narrow stand-ins for the issue's --all-targets), cargo fmt --check, contract scripts.

Closes #3418

The exactness survey (#3415) left two places where a K-row verify block is not the K decode steps it stands for.

Gemma 4 26B A4B declined the MTP probe on both quantized kernels. The narrow divergence starts at the layer 0 expert output, because `Experts::forward` picks its kernel by token count. A verify block now runs the routed experts per position, and the 26B A4B geometry joins the row-wise verify attention set on Metal. The probe is equal on the narrow kernel at widths 2, 4 and 8, up to 2100 prompt tokens. Classic decode is unchanged (132.65 against 132.90 tok/s).

The token-only verify round (prompt lookup, a full-model drafter) differs on the DeepSeek V3 family because one query takes the fused absorbed MLA kernel and a block takes SDPA. `MLXCEL_MLA_ROW_INVARIANT=1` runs each block row as that single query on `DeepSeekV3Model` and Xing4.0, and routes the GLM-4.7-Flash engine block to its row-wise verify forward. It is off by default: byte identity costs prompt lookup 24% to 43% of its throughput, and quantized checkpoints also need `MLXCEL_QMV_WIDE=0`. No gate was added. Arcee, K2 Horizon and OLMoE are located and kept as documented.

Validated with narrow lib suites under the GPU lock, both narrow clippy commands and the contract checks. Measurements: docs/benchmark_results/speculative-exactness-followup-m3ultra-2026-11-25.md.

Closes #3418
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 25, 2026
Review of the #3418 change found two gaps. `linear_whole_block` took the dense route outside a scope, which skips a named LoRA adapter; it is now the layer's own forward there, so a model that enters the scope only on request is untouched. And Xing4.0 still parted from the chain in one optiq cell (width 8, 2100 tokens, layer 38), because its hyper connection mixes through a dense `matmul` and a batched matmul over the positions. The mixer now takes `row_invariant::dense` and `hc_expand` multiplies each position alone inside a verify block; both Xing4.0 checkpoints are equal in 9 of 9 cells with `MLXCEL_MLA_ROW_INVARIANT=1` and `MLXCEL_QMV_WIDE=0`.

The tiny DeepSeek V3 test now also runs a rope width the fused MLA kernel takes, and counts the MLA paths so every block row is shown to take the single-query path.

Refs #3418
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 25, 2026
@inureyes
inureyes marked this pull request as ready for review November 25, 2026 02:13
@inureyes
inureyes merged commit 7994c1e into main Nov 25, 2026
34 checks passed
@inureyes
inureyes deleted the fix/issue-3418-verify-mismatches branch November 25, 2026 02:14
inureyes added a commit that referenced this pull request Nov 25, 2026
…ilies (#3424)

## Summary

Extends the opt-in exact token-only verify scope (`MLXCEL_MLA_ROW_INVARIANT=1`, #3420) to the families #3418 left open. With the variable unset no forward enters a row-invariant scope, so the default path is unchanged (decode ABAB below).

- DeepSeek V3.2: every verify-block row runs the decode step's single DSA query over its prefix of the stored `[latent | indexer key]` window with its own indexer top-k; projections (indexer included), gate and LM head row-invariant; float experts per position.
- DeepSeek V2 (in): a verify block appends and attends one row at a time on both the latent and the decompressed layout.
- `Llama3Model` (K2 Horizon) and Arcee: the fused QKV split goes through `row_invariant::linear_whole_block` (the layer's own forward outside a scope); inside one the fused rope and compiled MLP launchers give way to the composed graph, and a block attends one row at a time.
- OLMoE: a verify block runs routing and experts per position (the fused single-token MoE kernel) and attends per row.
- MLA path stats count a per-row verify block once per layer (`mla::stats::as_one_call`).

Open points: Kimi Linear, Kimi K3 and LongCat are out. The token-only round refuses them (model-owned KDA or n-gram state without a verify rewind), so no block forms and the KDA layers never see one; the survey reports "not run" on every cell and a test per family pins the refusal. DeepSeek V2 is in: it takes the round and carries the same split.

## Measurements (M3 Ultra)

Survey, engine path, widths 2/4/8 at 8/600/2100 tokens: off, every cell differs on both kernels (first difference layer 0 output). On: afm-4.5b and k2-horizon-0.9b equal on bf16 (9/9); deepseek-v2-lite and olmoe equal with `MLXCEL_QMV_WIDE=0` (9/9). Decode ABAB against main, variable unset, same tokens: K2 Horizon 211.50 vs 211.80, OLMoE 410.21 (421.24 without a cold first run) vs 421.21, V2-Lite 173.28 vs 174.58 tok/s. Details in `docs/benchmark_results/speculative-exactness-followup-m3ultra-2026-11-25.md` section 3.

Not done: `deepseek-v3.2-4bit` not loaded (tiny-model test only); the diagnostic names the first layer, not the op inside it; prompt-lookup throughput with the variable on was not measured for these families.

## Test plan

- [x] Tiny-model block-versus-chain tests for V3.2, V2, Llama 3, Arcee, OLMoE (float and 4-bit), widths 2/4/8/12; each fails without the scope
- [x] Extended path-count test in `deepseek_v3_verify_block_tests.rs` plus V3.2 and V2 equivalents
- [x] Narrow selectors: `models::llama3 models::arcee models::olmoe models::deepseek_v2 models::deepseek_v32 models::deepseek_v3 models::kimi_linear models::longcat coverage_tests`, core `mla::stats row_invariant qkv`
- [x] Contract checks, clippy (lib/tests/bins and mlxcel-core), fmt; broad gate left to the orchestrator

Closes #3421
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:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Trace the two remaining speculative verify mismatches: Gemma 4 26B A4B MoE and token-only verify rounds

1 participant