Skip to content

fix(models): keep Inkling 2 verify blocks exact on M1-class GPUs - #3475

Merged
inureyes merged 6 commits into
mainfrom
fix/issue-3468-m1-block-chain-tests
Nov 28, 2026
Merged

inureyes merged 6 commits into
mainfrom
fix/issue-3468-m1-block-chain-tests

Conversation

@inureyes

@inureyes inureyes commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Cause

Both tests fail on M1 Ultra because of the matmul overlay's few-row GEMV route (#2587, src/lib/mlxcelverse/overlays/metal/mlx/backend/metal/matmul.cpp). On GPU generations 13 and 14 (M1, M2), a dense product with at most MLXCEL_DENSE_GEMV_ROWS rows (default 6) in a single batch runs each row through the one-row GEMV. Larger products take the steel GEMM tile, which sums in a different order. Generation 15 and later never take that route, which is why the M3 measurements behind #3405 and #3424 missed it. On M1, with the unchanged tests on main:

MLXCEL_DENSE_GEMV_ROWS DeepSeek control Inkling 2 equality
unset (6) fails fails at passes 1, width 2
0 or 1 passes passes
16 fails fails at passes 1, width 8

Turning off MLXCEL_INKLING2_KERNELS does not help (it still diverges at width 2), so the Inkling 2 Metal kernels are not the cause.

Changes

Inkling 2 (runtime). The op is AttentionResidual::forward (src/models/inkling2/residual.rs). [B, L, S, H] @ [H, Q] shares its right operand, so MLX folds it into M = L * S, and the value mix runs L batches where a decode step runs one, so a decode step and a verify block dispatch different kernels on M1. The mix moves into mix(), and inside a verify block on generations where the few-row route exists, each position runs alone through row_invariant::per_position at the decode step's own shapes. On the tiny model the per-visit trace showed the first divergence at the final visit input, after the last layer's residual mix; with the change every visit and the logits are byte-identical.

Generation gate. AppleSiliconGen::few_row_dense_gemv() (src/lib/mlxcel-core/src/hardware.rs) is true for M1, M2 and unidentified chips, mirroring the overlay's get_architecture_gen() < 15, with a unit test. M3 and later keep the single mix, which already matched the chain there. It reads the brand-string generation rather than MLX's architecture generation; if the two ever disagree, the block-versus-chain probe declines the drafter, so output never changes. It does not read MLXCEL_DENSE_GEMV_ROWS: with the route disabled the split is still exact, only slower. row_invariant::per_position itself is unchanged.

Why gate it: the split costs every verify round. M1 Ultra, Inkling-2-Mini-4bit, verify_round_timing, three interleaved runs per arm, argmax digests equal:

width split single mix cost
4 75.1 to 75.4 ms 69.5 to 69.9 ms +7.7%
8 111.5 to 112.1 ms 101.7 to 101.8 ms +9.8%

On M1 and M2 that cost is the price of exactness (without it the drafter is declined); M3 and later no longer pay it.

DeepSeek V2 (test only). Widths 2 and 4 fall inside the GEMV route, so on M1 the decompressed layout's projections already match the chain without the row-invariant scope. The control in src/models/deepseek_v2_verify_tests.rs now also runs widths 8 and 12. Differing rows on M1, scope off:

layout w2 w4 w8 w12
absorbed 2 4 8 12
decompressed 0 0 8 12

With the scope forced on (Some(false) and Some(true) in place of None), both layouts differ in 0 rows at every width and the control panics with the block already equals the chain without the scope, so it still discriminates. Under MLXCEL_DENSE_GEMV_ROWS=16 width 12 still differs in all 12 rows. On other generations the width set contains the old [2, 4], so the control cannot weaken where it passed before.

Diagnostics and docs. The ignored verify_block_vs_chain_per_visit diagnostic now prints the probe verdict the DSpark gate reads. The DSpark row in docs/supported-models.md notes the per-position residual mix on M1 and M2 and the M1 Ultra result below.

Real checkpoint (M1 Ultra, mlx-community/Inkling-2-Mini-4bit)

probe_block_chain_exactness:

width main this PR
2 Diverges at position 0 (432163/800232 bytes) Equal
4 not run Equal
8 (mtp_config.block_size) Diverges at position 0 (545634/800232 bytes) Equal

On main the DSpark gate therefore declined on M1 without MLXCEL_MTP_ALLOW_INEXACT=1. The mlx-community conversion does not bundle mtp.safetensors, so the drafter check used the upstream sidecar (thinkingmachines/Inkling-2-Mini mtp.safetensors and config.json, in a separate directory passed as --draft-model). With this PR, without the override, mlxcel generate --temp 0 -n 96 logs MTP exactness probe passed ... block_size=8 and the drafter engages (50 rounds, 13 of 39 proposals accepted; the drafter was trained against the BF16 target, not this 4-bit conversion). The 96 tokens are byte-identical to the same run without --draft-model.

Test plan

  • make verify-test on M1 Ultra: 17201 passed, 2 failed. Both failures are mlxcel-xla plamo2 prefill goldens (emit_all_reproduces_registered_goldens, registered_fixtures_are_byte_exact) that fail on main at the merge base and are fixed on main by fix(xla): refresh the plamo2 prefill goldens for the shifted-add scan #3471; the two tests of test(models): two Metal block-vs-chain byte-identity tests fail on M1 Ultra #3468 pass.
  • deepseek_v2::, inkling2, dense_verify_block filters on M1 Ultra, all six (passes, width) combinations; both tests also pass under MLXCEL_INKLING2_KERNELS=0, MLXCEL_DENSE_GEMV_ROWS=0 and MLXCEL_DENSE_GEMV_ROWS=16
  • Real-checkpoint probe Equal at widths 2, 4 and 8 with the generation gate in place
  • cargo clippy --features metal,accelerate --lib --tests -- -D warnings (root and mlxcel-core), cargo fmt --check
  • M3-class re-run of both tests and of the verify timing (the single-mix path there is the one that was already Equal on M3 Ultra; pending on another host)

Closes #3468

Both block-vs-chain tests failed on M1 Ultra because of the matmul overlay's few-row GEMV route (#2587): on GPU generations 13 and 14 a dense product of at most `MLXCEL_DENSE_GEMV_ROWS` (default 6) rows in one batch runs each row through the one-row GEMV, and anything larger takes the steel GEMM tile. Setting the variable to 0 makes both tests pass on M1; setting it to 16 moves the Inkling failure to width 8.

Inkling 2: the attention-residual mix (`AttentionResidual::forward`) multiplies `[B, L, S, H]` by a shared `[H, Q]` query, which MLX folds into M = L * S, and then mixes over L batches. A decode step and a verify block therefore took different kernels on M1. Inside a verify block the mix now runs per position through `row_invariant::per_position`, at the decode step's shapes; decode steps and prefill are unchanged. On a real Inkling 2 Mini 4-bit checkpoint on M1 Ultra the probe goes from Diverges to Equal at widths 2, 4 and 8.

DeepSeek V2: the negative control's widths 2 and 4 fall inside the GEMV route, so the decompressed layout already matched the chain on M1. The control now also runs widths 8 and 12, where the decompressed layout differs at every row, and it still fails with the row-invariant scope forced on.

The ignored real-checkpoint diagnostic also prints the probe verdict the DSpark gate reads.

Refs #3468
@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 platform:macos macOS (Apple Silicon) specific type:test Test related changes labels Nov 27, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Make the DeepSeek V2 negative control and the Inkling 2 verify-vs-chain test hold on M1-class GPUs, and make the Inkling 2 verify block byte-identical to the chain there (#3468).

Findings Addressed

None needed: no CRITICAL or HIGH findings.

Remaining Items

  • Verify-round cost of the per-position residual mix (MEDIUM). On M1 Ultra, mlx-community/Inkling-2-Mini-4bit, verify_round_timing at width 8: median 111.8 ms (min 111.1) with the split, 104.2 ms (min 101.1) with forward calling self.mix(&states) directly, about 7 to 10 percent slower per verify. Argmax digests match between arms. The split is unconditional, so M3-class hosts pay it too although the probe was already Equal there. Worth stating in the PR body, and re-measuring in the pending M3 run.
  • docs/supported-models.md DSpark row (MEDIUM). Its list of exactness mechanisms does not name the per-position residual mix, and it states the probe passes at widths 2, 4 and 8 on an M3 Ultra only. A short addition covering the mechanism (test(models): two Metal block-vs-chain byte-identity tests fail on M1 Ultra #3468) and the M1 Ultra result, scoped to what was measured (Mini 4-bit, widths 2, 4, 8), would keep it current.
  • M3-class re-run of both tests (pending on another host, as the PR body says).

Checked

  • per_position is a no-op outside a verify block: in_verify_block needs the speculative append scope, and is_verify_block needs B = 1 and 2 to 16 positions, so decode, batched decode, prefill and the image prefill call mix once, op for op the old body. Splitting on axis 1 is exact math (the softmax runs over the S axis) and the concatenated output keeps [B, L, Q, H] f32.
  • DeepSeek control: the positive test already asserts 0 differing rows at widths 2, 4, 8 and 12 for both layouts. With the decompressed layout run first and the scope forced on (Some(false), then Some(true)), the control panics with absorbed false: the block already equals the chain without the scope, so it discriminates on the layout the M1 fix relies on.
  • per_position has 4 caller files, under the 5-file threshold in docs/code-guidelines.md, so no Used by: annotation is required.

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass (deepseek_v2::verify_tests, inkling2::speculative::tests: 11 passed on M1 Ultra)

The per-position attention-residual mix that keeps an Inkling 2 verify block exact is needed only where the matmul overlay's few-row GEMV route runs (GPU generations 13 and 14). On M3 and later the block already matched the chain with a single mix, so the split only added cost there.

`AppleSiliconGen::few_row_dense_gemv` names the generation half of the overlay predicate (true for M1, M2 and `Unknown`), and `AttentionResidual::forward` splits only where it holds. `MLXCEL_DENSE_GEMV_ROWS` is not read: with the route disabled the split is still exact, just slower. A misread generation can only switch the drafter off through the exactness probe, never change output.

Measured on M1 Ultra, Inkling 2 Mini 4-bit, `verify_round_timing`, three interleaved runs per arm: verify width 4 median 75.1 to 75.4 ms with the split against 69.5 to 69.9 ms without (+7.7%), width 8 111.5 to 112.1 ms against 101.7 to 101.8 ms (+9.8%), argmax digests identical. That is the cost M3 and later no longer pay. On M1 Ultra the split stays on and `probe_block_chain_exactness` on the real checkpoint is Equal at widths 2, 4 and 8. The M3-class run of the no-split branch is still pending.

Refs #3468
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

Addressed

  • MEDIUM, per-position residual mix cost (src/models/inkling2/residual.rs): now gated in 351cd3a. The split runs only where AppleSiliconGen::few_row_dense_gemv() holds (M1, M2, and Unknown), the generation half of the overlay's few-row GEMV predicate (get_architecture_gen() < 15). M3 and later keep the single mix, which is op for op the pre-PR path that was already Equal there. row_invariant::per_position itself is unchanged, so its other callers are unaffected.

Measured on M1 Ultra, Inkling 2 Mini 4-bit, verify_round_timing, three interleaved runs per arm (temporary local switch, not committed):

Width Split (median, 3 runs) No split (median, 3 runs) Cost
4 75.4 / 75.1 / 75.2 ms 69.8 / 69.9 / 69.5 ms +7.7%
8 112.1 / 111.6 / 111.5 ms 101.7 / 101.8 / 101.7 ms +9.8%

Spread within an arm is under 1 ms, and argmax digests match between arms. This is the cost M3 and later no longer pay.

Exactness on M1 Ultra with the gate: deepseek_v2::verify_tests and inkling2::speculative::tests pass (11 tests), and the real-checkpoint probe_block_chain_exactness is Equal at widths 2, 4 and 8. The new predicate has a unit test. Clippy and fmt are clean.

MLXCEL_DENSE_GEMV_ROWS is deliberately not read. With the route disabled the split is still exact, only slower, and mirroring the overlay's atoi parsing would add an env-parsing surface for a debug knob. The gate reads the brand-string generation, not MLX's get_architecture_gen(). If the two disagree (MLX_METAL_GPU_ARCH, an unrecognised chip), the exactness probe declines the drafter. Output is never affected.

Remaining

  • M3-class run of both tests and the real-checkpoint probe on the no-split branch (pending, as the PR body says).
  • LOW, pre-existing and out of scope: the overlay parses MLXCEL_DENSE_GEMV_ROWS with atoi, so a malformed value silently disables the route. Not a safety issue.

No security findings: the change adds no input parsing and no new panic paths.

The Inkling 2 DSpark row listed the verify-block exactness mechanisms but not the per-position attention residual mix that runs on Apple GPU generations 13 and 14 (M1, M2), and it stated the block-versus-chain probe result on an M3 Ultra only.

Add the mechanism and the measured M1 Ultra result, scoped to Mini 4-bit at widths 2, 4 and 8.

Refs #3468
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 27, 2026
The generation predicate reads `AppleSiliconGen::Unknown` as true so an unidentified Apple chip keeps the exact split, but CUDA and ROCm also report `Unknown`, so every Inkling 2 verify block there paid the per-position residual mix for a few-row GEMV route that exists only in the Metal matmul overlay. `HardwareCapabilities::few_row_dense_gemv` now also requires the Apple vendor tag, and the residual mix asks it instead of the bare generation. Non-Apple backends keep the single mix they ran before #3475.

Validated on M1 Ultra: the new vendor test, `deepseek_v2::`, `inkling2` and `dense_verify_block` filters (97 passed), and the real-checkpoint probe on `Inkling-2-Mini-4bit` at width 8 (Equal). The CUDA arm is covered by the unit test only; no CUDA host was used.

Refs #3468
#3462 added `unix_micros`, `rfc3339`, `unix_micros_now` and `rfc3339_now` to `src/server/clock.rs`, but every caller outside the clock tests sits behind the `webui` feature. Builds without it, such as the OpenXLA feature compile job under `RUSTFLAGS=-D warnings`, failed on dead code, and that job has been red on main since #3462. The two `_now` wrappers now compile with `webui` only and the two formatters with `webui` or tests.

Validated with `cargo clippy --no-default-features --features metal,accelerate --lib --tests -- -D warnings`, the workspace clippy at default features, and the `server::clock` tests.
@inureyes

Copy link
Copy Markdown
Member Author

Implementation hardening

Reviewed this PR against #3468 on the branch merged with current main.

Findings and fixes

  • Per-position split also ran on CUDA and ROCm (fixed, e4fe530). AppleSiliconGen::few_row_dense_gemv() reads Unknown as true so an unidentified Apple chip keeps the exact split, but non-Apple backends also report Unknown, and the Inkling 2 verify scope (is_verify_block) is not Metal-gated. Every CUDA/ROCm verify block therefore paid the split for a route that exists only in the Metal matmul overlay. HardwareCapabilities::few_row_dense_gemv() now also requires GpuVendor::Apple, residual.rs calls it, and few_row_dense_gemv_needs_the_metal_vendor pins Nvidia/Amd/Unknown-vendor false, Apple+Unknown true, M1/M2 true, M3/M5 false. Non-Apple backends are back to the pre-PR single mix.
  • OpenXLA feature compile red (fixed, e5355cd; pre-existing on main since update(deps): replace the unmaintained chrono crate with jiff #3462). unix_micros, rfc3339, unix_micros_now and rfc3339_now in src/server/clock.rs are only called behind webui, so builds without that feature failed -D warnings on dead code. The _now wrappers now compile with webui only, the formatters with webui or tests.

Security: no new input parsing or panic paths. Performance: the fix removes the verify-round cost off Apple; Apple behavior is unchanged.

Validation (M1 Ultra)

  • cargo clippy --no-default-features --features metal,accelerate --lib --tests -- -D warnings (reproduces the XLA failure before the fix, clean after), cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings, cargo fmt --check
  • deepseek_v2::, inkling2, dense_verify_block, server::clock: 97 passed; few_row_dense_gemv tests in mlxcel-core: 2 passed
  • Real checkpoint Inkling-2-Mini-4bit, probe_block_chain_exactness(8): Equal

Remaining

  • M3-class re-run (still pending on another host). The M3 path is op for op the pre-PR single mix and the DeepSeek width set is a superset of the old one, so this does not block merge.
  • The CUDA arm of the vendor gate is covered by the unit test only; no CUDA host was used.

@inureyes
inureyes merged commit efd557d into main Nov 28, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3468-m1-block-chain-tests branch November 28, 2026 11:04
@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:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions type:test Test related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(models): two Metal block-vs-chain byte-identity tests fail on M1 Ultra

1 participant