Skip to content

test: compare Gemma 3 truncated-restore rows computed at the same prefill length - #3722

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:test/metal-nightly-regressions
Dec 4, 2026
Merged

inureyes merged 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.

On macOS, make verify-test fails models::gemma3::gemma3_tests::quantized_snapshot::a_truncated_quantized_restore_reproduces_a_cold_prefill_of_the_prefix, which passes on CUDA (GB10). The test came in with #3498. It is not a product bug: the test compares against a reference whose bit-identity depends on the backend's matmul. This PR changes that one test; no code path changes.

The nightly also failed two Turbo boundary-index tests in mlxcel-core (cache::turbo::codebook::tests::device_boundary_indices_match_host_at_the_edges and cache::turbo::quant::device_tests::device_pack_handles_boundary_ties_and_extremes). An earlier revision of this PR loosened those tests. #3686 has since fixed the cause in product code: boundary_indices_on_device now compares signed integer order keys, so Metal no longer flushes the subnormal inputs onto the zero midpoint. Both tests pass unchanged on current main on Metal, and their strict device == host assertion now guards that fix. That part has been dropped from this PR.

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

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

Cause

The failure is not caused by the snapshot, the truncation or Turbo4Asym. Comparing cold prefills directly, with no snapshot at all, on the same fixture in plain Fp16, 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

On Metal, a row's result therefore depends on the total length of the prefill it was computed in, by the last bits. A split prefill or any prompt-cache hit already has this effect; 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.

At the previous MLX pin (5a48f9cc) a two-operand f32 matmul showed 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 differed from P[:L-1, :L-1] @ V[:L-1] at L = 32, 48 and 64, and not at L = 16. That probe was not re-run after #3685; the table above was re-measured at the current pin and is unchanged.

Change

The donor now forwards the kept prefix (tokens[..target]) and the dropped tokens (tokens[target..stored]) as two calls. The kept rows then come out of 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.
  • The test keeps its four cuts, including 6 -> 6 (no second forward) and 6 -> 1.
  • 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.
  • 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. It passes on Metal.

Mutation check (MLX 5a48f9cc): 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 and the other five tests in the module still pass. The mutation was reverted before the commit.

CHANGELOG is not changed (test-only).

Verification

Apple M4 (10 cores, 16 GB), macOS 26.2, Xcode 26.6, rustc 1.97.1, MLX 6371f1ce.

Before, on main 5bf7b8628:

cargo test -p mlxcel --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)
cargo test -p mlxcel-core --profile test-fast --features metal,accelerate --lib -- --test-threads=1 cache::turbo::
  test result: ok. 124 passed; 0 failed  (both boundary-index tests pass since #3686)

After, on this commit:

cargo test -p mlxcel --profile test-fast --features metal,accelerate --lib -- --test-threads=1 models::gemma3
  test result: ok. 74 passed; 0 failed; 16 ignored
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 clippy -p mlxcel --profile test-fast --lib --tests --features metal,accelerate -- -D warnings
                                                pass
cargo fmt --all -- --check                      clean
python3 scripts/ci/check_used_by.py             used-by: OK
git merge-tree --write-tree origin/main HEAD    clean onto 5bf7b8628

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].

The earlier revision of the test change passed on CUDA (NVIDIA GB10, sm_121, CUDA 13.0): models::gemma3 74 passed, 0 failed, 16 ignored. The Gemma 3 hunk is byte-identical to that revision; the CUDA run was not repeated on this base.

Limits

  • I did not identify which MLX Metal matmul kernel or tile choice makes the result depend on the prefill length. The table above is what was measured.
  • The full make verify-test was not run on the Mac; only the two modules above.

@inureyes inureyes added status:review Under review type:test Test related changes priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Dec 4, 2026
…fill length

Refs lablup#3652. On Metal the Gemma 3 truncated-restore test fails for
Turbo4Asym at window 32, 32 tokens cut to 31, while it passes on CUDA.

The 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: with no snapshot at
all, in plain Fp16, a cold 32-token prefill gives rows 16 to 30 logits
that differ from a cold 31-token prefill of the same tokens (48 against
47 tokens differ in rows 32 to 46, 64 against 63 in rows 48 to 62), at
windows 32, 64 and 4096 alike. The comparison therefore measured the
matmul, not the truncation.

The donor now forwards the kept prefix and the dropped tokens as two
calls, so the kept rows come out of the same forward the control runs.
The byte-equality assertion is unchanged.
@Jaeyeong-CHOI
Jaeyeong-CHOI force-pushed the test/metal-nightly-regressions branch from 4a81964 to 2886959 Compare December 4, 2026 06:15
@Jaeyeong-CHOI Jaeyeong-CHOI changed the title test: make three Metal-only nightly failures backend-correct test: compare Gemma 3 truncated-restore rows computed at the same prefill length Dec 4, 2026
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Hardening review: no changes needed, merging as is.

Reviewed against the PR body (no linked issue; the failure is reported in #3652). The nightly evidence matches: runs 46937282079 and 47171225366 on the macOS runner fail a_truncated_quantized_restore_reproduces_a_cold_prefill_of_the_prefix at line 183 while the CUDA lane passes.

The change fixes a test defect, not a product regression. Truncated restore never promised bit-identity with a cold prefill whose rows came from a forward of a different length; that last-bit difference is the same one any prompt-cache hit carries. Splitting the donor's prefill at the cut makes the kept rows come from the same forward shape as the control, so the byte-equality assertion still isolates the truncation. The ring is still full at window tokens when the snapshot is taken, the cut still drops buffered rows, and for Turbo4Asym at window 32 the second forward still completes the quantized block before the snapshot, so block quantization and its truncation are exercised as before.

Validation on NVIDIA GB10 (CUDA, current main with no drift):

  • cargo test -p mlxcel --profile test-fast --features cuda --lib -- --test-threads=1 models::gemma3: 74 passed, 0 failed, 16 ignored.
  • Mutation check: making kv_snapshot::truncate_rotating skip the trim fails the reworked test (Int8, window 8, 6 tokens cut to 4) while the other five tests in the module pass. Reverted.
  • cargo fmt --all -- --check clean; cargo clippy -p mlxcel --all-targets -- -D warnings clean.

Metal could not be run here; the Metal pass relies on the author's run in the PR body.

@inureyes
inureyes merged commit d59ca56 into lablup:main Dec 4, 2026
36 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 4, 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 priority:medium Medium priority status:done Completed type:test Test related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants