Skip to content

test: make three Metal-only nightly failures backend-correct - #3722

Open
Jaeyeong-CHOI wants to merge 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:test/metal-nightly-regressions
Open

Jaeyeong-CHOI wants to merge 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:test/metal-nightly-regressions

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #3652.

make verify-test on macOS fails three tests that pass on CUDA (GB10). They came in with #3497, #3500 and #3498. None of the three is a product bug: two assert a host/device agreement on subnormal inputs that a flush-to-zero backend cannot give, and one compares against a reference whose bit-identity depends on the backend's matmul. This PR changes the three tests and one doc comment; no code path changes.

Failing on main (beedecbb1), Apple M4, macOS 26.2, --features metal,accelerate:

Test Failure on main
mlxcel-core cache::turbo::codebook::tests::device_boundary_indices_match_host_at_the_edges codebook.rs:997 "bits 3 head_dim 64": device index 3, host index 4 at the two positions holding f32::from_bits(1)
mlxcel-core cache::turbo::quant::device_tests::device_pack_handles_boundary_ties_and_extremes quant/device_tests.rs:124 "boundary indices d=64, t=1"
mlxcel models::gemma3::gemma3_tests::quantized_snapshot::a_truncated_quantized_restore_reproduces_a_cold_prefill_of_the_prefix gemma3_quantized_snapshot_tests.rs:183 "Turbo4Asym, window 32, 32 tokens cut to 31: the suffix logits differ from a cold prefill of the prefix"

1 and 2: subnormals on Metal

boundary_indices_on_device counts the codebook midpoints strictly below each input with greater(x[..., None], boundaries). The symmetric Lloyd-Max codebooks (3- and 4-bit) have a midpoint at exactly 0.0. Metal's GPU compare flushes subnormals to zero, so f32::from_bits(1) > 0.0 is false on the device: the smallest positive subnormal sits on the zero midpoint and takes the lower index, while the host's partition_point(|b| b < v) gives it the upper one. The two tests put boundary.next_up() next to every midpoint (and the codebook test also has ±f32::from_bits(1) directly), so both hit this at the zero midpoint.

This does not matter numerically. The two centroids such an input can land on are equally far from it up to twice its magnitude, which is below f32::MIN_POSITIVE, so the quantization error is the same either way. Making the device bit-identical to the host here would mean comparing on integer bit patterns in the cache's hot quantize path to fix a case activations do not produce. The tests now accept exactly two outcomes:

  • the host rule on the inputs as given (CUDA, and any backend that keeps subnormals), or
  • the host rule on the inputs with every subnormal replaced by a zero of its sign (a flushing backend).

The whole index vector has to equal one of the two, so every non-subnormal input (both zeros, NaN, the infinities, the finite extremes, each boundary and the floats beside it) is still held to the host rule exactly. Each test also asserts the two expectations differ, so the subnormal case cannot quietly become vacuous. In the codebook test the hand-stated rule ("one float above a boundary takes the upper index") expects the lower index only for the subnormal above the zero midpoint on a backend that was seen to flush. The doc comment of boundary_indices_on_device said the device rule "agrees with the host function on every input"; it now names this exception. The existing bullets are kept.

3: Gemma 3 truncated restore

The failure is not caused by the snapshot, the truncation or Turbo4Asym. I ran the same comparison over more cases (a throwaway test, not committed) on the same fixture:

  • Fp16, which TABLES does not include, fails the same way: 32 cut to 31 and 64 cut to 63 differ, as do 32 to 30 and 64 to 62.
  • Every mode passes when the donor prefilled a length like 31 or 63 (31 to 30, 63 to 62), or when the cut is large (32 to 16, 64 to 32).
  • Int8 passes 32 to 31, and Turbo4Asym passes 64 to 63, though Fp16 fails both cuts. The quantized layers evidently absorb the difference in those two cases; I did not trace why.

So I compared cold prefills directly, with no snapshot at all. On the same Fp16 fixture, the logits that rows 0..short get from a cold prefill of long tokens and from a cold prefill of short tokens are:

long / short rows that differ (window 32, 64 and 4096 alike)
32 / 31 16 to 30
48 / 47 32 to 46
64 / 63 48 to 62
31 / 30, 33 / 32, 16 / 15, 32 / 16 none

A two-operand f32 matmul shows the same thing in isolation: for a lower-triangular P (L x L) and V (L x 2), the first L - 1 rows of P @ V differ from P[:L-1, :L-1] @ V[:L-1] in 21, 27 and 26 elements at L = 32, 48 and 64, and in none at L = 16. In f16, and for softmax, none differ. A row's result therefore depends on the total length of the prefill it was computed in, on Metal, by the last bits. This is the same effect a split prefill or any prompt-cache hit already has; the restore did not introduce it. The test's donor prefilled all stored tokens in one forward, so for (window, window - 1) the rows it kept came from a 32-token (or 64-token) forward, while the control computed them in a 31-token (63-token) forward. On CUDA those agree for these shapes, so the test passed there.

The donor now forwards the kept prefix (tokens[..target]) and the dropped tokens (tokens[target..stored]) as two calls. The kept rows are then computed by the same forward the control runs, so the bit-for-bit comparison measures the truncation and nothing else. The assertion is unchanged (byte equality of the suffix logits). The ring is still full at window tokens when the snapshot is taken, and the cut still has to drop rows that are in the buffer. A donor that took its rows from one forward is still covered for a restore without a cut by a_restored_quantized_state_reproduces_a_cold_split_prefill_bit_for_bit, which compares like with like.

Mutation check: with kv_snapshot::truncate_rotating changed to return before trimming (the sliding layer keeps every stored row), the reworked test fails at its logits assertion (gemma3_quantized_snapshot_tests.rs:195). The other five tests in the module still pass. The mutation was reverted before the commit.

Checked, per the review list

  • Boundaries: the subnormal tests keep both zeros, NaN, ±inf, f32::MIN/MAX, ±f32::MIN_POSITIVE and ±f32::from_bits(1), at head dims 64/128/256 and t = 1 and 4096. The Gemma 3 test keeps its four cuts, including 6 -> 6 (no second forward) and 6 -> 1.
  • Complexity: test-only; flush_subnormals is linear in the input.
  • Other callers: boundary_indices_on_device is used by quant3::quantize_v_turbo3 and quant::pack_rotated_turbo4; its behaviour is unchanged. The other device-vs-host tests in cache::turbo (including quant3's coordinates_on_every_boundary_take_the_lower_index and the dense sweep) have no subnormal input and pass on Metal unchanged. The Gemma 4 counterpart (a_truncated_gemma4_restore_cuts_every_populated_layer) feeds per-token K/V straight into the caches, with no model forward, so it does not depend on prefill length, and it passes on Metal.
  • CHANGELOG: not changed, since only tests and a doc comment change. There is no [Unreleased] section on main at the moment.
  • No explanatory comment was removed.

Verification

Apple M4 (10 cores, 16 GB), macOS 26.2, Xcode 26.6 (Metal 32023.883), rustc 1.97.1, MLX 5a48f9cc.

Before, on main beedecbb1:

cargo test -p mlxcel-core --profile test-fast --features metal,accelerate --lib -- --test-threads=1 cache::turbo::
  test result: FAILED. 122 passed; 2 failed  (the two tests above)
cargo test --profile test-fast --features metal,accelerate --lib -- --test-threads=1 models::gemma3::gemma3_tests::quantized_snapshot
  test result: FAILED. 5 passed; 1 failed  (the truncated-restore test)

After, on this commit:

cargo test -p mlxcel-core --profile test-fast --features metal,accelerate --lib -- --test-threads=1 cache::turbo::
  test result: ok. 124 passed; 0 failed
cargo test --profile test-fast --features metal,accelerate --lib -- --test-threads=1 models::gemma3
  test result: ok. 74 passed; 0 failed; 16 ignored
cargo fmt --all -- --check                      clean
python3 scripts/ci/check_used_by.py             used-by: OK
bash scripts/ci/check_used_by_test.sh           all used-by checker cases passed
git merge-tree --write-tree origin/main HEAD    clean onto ac5716b8f

On CUDA, NVIDIA GB10 (sm_121), CUDA 13.0, same commit tree (3861d88f):

cargo fmt --all -- --check                                                         clean
cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings     pass
cargo test -p mlxcel-core --profile test-fast --features cuda --lib -- --test-threads=1 cache::turbo
  test result: ok. 206 passed; 0 failed
cargo test --profile test-fast --features cuda --lib -- --test-threads=1 models::gemma3
  test result: ok. 74 passed; 0 failed; 16 ignored
python3 scripts/ci/check_used_by.py                                                used-by: OK

The two cache::turbo counts are not comparable: the CUDA filter cache::turbo also matches the cache::turbo_tests module, which cache::turbo:: on Metal does not (with the wider filter the Metal build lists 211 tests).

The 16 ignored tests in models::gemma3 carry #[ignore] attributes on main (serial-MLX, checkpoint or IREE gated); this change adds or removes no #[ignore].

Limits

  • I did not identify which MLX Metal matmul kernel or tile choice makes the result depend on the contraction length. The tables above are what was measured.
  • Only the three tests named in the nightly were investigated. The full make verify-test was not run on the Mac; the CUDA run above covers the two touched modules, not the whole suite.

Refs lablup#3652. The macOS nightly fails three tests that pass on CUDA; none is
a product bug.

The Turbo boundary-index tests (codebook edges, Turbo4 device pack) put
the smallest positive subnormal beside the codebook's zero midpoint.
Metal's GPU compare flushes subnormals to zero, so the device gives that
input the lower index where the host gives the upper one. The two
neighbouring centroids are equally far from such an input up to twice its
magnitude, so the quantization error is unchanged. The tests now require
the host rule on the inputs as given or, on a flushing backend, on the
inputs with subnormals flushed; every other input is still held to the
host rule exactly, and each test asserts the two expectations differ. The
doc comment of boundary_indices_on_device names the exception.

The Gemma 3 truncated-restore test compared rows its donor computed in a
32-token prefill with rows the control computed in a 31-token prefill. On
Metal a row's result depends on the total prefill length by the last bits
(32 against 31 tokens differ in rows 16 to 30 in plain Fp16 with no
snapshot; an f32 matmul shows the same at contraction length 32 against
31), so the comparison measured the matmul, not the truncation. The donor
now forwards the kept prefix and the dropped tokens as two calls, and the
byte-equality assertion is unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant