Repository navigation
fix(xla): replace the reduce_window cumsum in the Mamba2 SSD scan - #3434
Conversation
The chunked SSD prefill returned NaN y and a wrong final state on IREE-Metal at f32 while local-task passed. IREE rewrites a left-padded prefix-sum stablehlo.reduce_window into iree_linalg_ext.scan, and the Metal target returns unrelated values for that op on any tensor of rank 2 or more (a [3, 3] input reproduces it outside mlxcel with iree-run-module). Both cumulative sums of the scan, the per-chunk log decay and the masked segment sum, went through it. The -inf mask in exp_segsum was not the cause and is unchanged. The scan now builds its prefix sums as a log-depth chain of shifted adds (slice, pad, add) in f32. The form is device-agnostic, so the emitted StableHLO changes on every backend: the summation is pairwise instead of a sequential fold, which moves results in the last bits only. Builder::reduce_window_prefix_add had no other caller and is removed. The nemotron_h and falcon_h1 prefill goldens are regenerated because those hybrids share the scan. New emitter tests pin that no prefill form emits reduce_window and that the prefix sum is a log-depth f32 chain. Narrow mlxcel-xla tests (validation, ssm, numeric_ops) and clippy pass. Refs #3343
Reduced-precision runs (f16 on IREE-Metal) are judged by a near-tie rule: only the first divergence from the HF fp32 oracle counts, and it passes when the oracle's own top-2 logit gap there is below 0.01. No tool recorded that gap. oracle_continuation.py takes an opt-in --top2 that adds ref_top2_ids and ref_top2_gaps to the oracle JSON; the default output is unchanged. near_tie_check.py judges a stream given as ids or as generated text against such an oracle (exit 0 exact, 3 near-tie only, 1 otherwise). xla_oracle_check prints the gap at its first divergence when the oracle carries it and keeps its strict exit status. Refs #3343
FINDINGS_ssm.md gains the Metal section for #3343: the root cause (IREE-Metal returns unrelated values for iree_linalg_ext.scan at rank 2 and above, reproduced standalone), the fix, the probe table before and after at f32 and f16, compile times, throughput, and the model gates. f32 is exact on every gate. f16 has one divergence from the HF oracle, a near-tie with a top-2 logit gap of 0.004593. near_tie_check.py learns --mlxcel-cli to read captured mlxcel generate stdout, and its text matcher no longer strips a trailing whitespace token from the oracle prefix, which had moved a divergence at a newline token two positions later. Refs #3343
The Used-by comment on prefix_add listed only Mamba2, but the regenerated goldens show Nemotron-H and Falcon-H1 emit it too. The Metal findings table now carries the digits check.json reports, drops a claim that f16 Metal equals f16 local-task to three digits (the K-1 case differs by 0.4%), and records that the hybrids were not executed and what the local-task probe run time did. Refs #3343
Implementation Review SummaryIntent
Findings AddressedNone needed fixing at CRITICAL or HIGH. Remaining Items
Verification
|
…s row near_tie_check.py now reports an empty stream as a usage error instead of TOKEN-EXACT, and reports a text divergence that falls inside a word as ambiguous (exit 2, use --ids), since text alone cannot tell whether the stream token extended the previous oracle token. FINDINGS_ssm.md: the pre-fix Metal f16 padded-prefill row carried the full-prefill numbers; split it into the measured padded (NaN) and full (3.311 / 8.674e28) rows. The prefix_add doc comment no longer claims lower rounding error than the removed reduce_window.
Security and performance reviewNo CRITICAL or HIGH findings, so nothing was auto-fixed and no commits were added. Narrow checks pass on the branch head: Performance
Security
|
near_tie_check.py loaded the tokenizer with a bare AutoTokenizer.from_pretrained, which could fall back to a Hub download when --model was not a local directory. It now exits 2 unless --model is a local directory and passes local_files_only=True. judge() indexed ref_top2_gaps and ref_top2_ids without checking their length, so a truncated or malformed oracle raised IndexError. It now exits 2 with a message when either array does not match ref_token_ids in length, the index is out of range, or a gap is not a number. Add test_near_tie_check.py, a model-free unittest file using a fake decode, covering the ids, text, empty stream, ambiguous divergence and malformed-oracle paths. Document the --top2 flag, near_tie_check.py and the IREE-Metal Mamba2 validation in spike/openxla/README.md and the mlxcel-xla README. Validation: - python3 -m unittest spike/openxla/test_near_tie_check.py: 12 passed - cargo test --profile test-fast -p mlxcel-xla --lib emitter::ssm: 16 passed - cargo fmt --check and cargo clippy -p mlxcel-xla --lib --tests -- -D warnings: clean Refs #3343
|
Merge is held until the one unchecked test-plan item passes: the shifted-add Remaining checks at this head (
Merge after those pass and their results are added to |
ROCm gfx1151 validation at
|
| check | result |
|---|---|
ssm_gate.sh OUT local-task hip |
11/11 on each device; worst on hip y 1.43e-6, h 1.19e-6, conv 3.6e-7 (tol 5e-5) |
validate_arch.sh --batch 4 --requests 8 --maxcap 24 |
exit 0: structural, HF token-exact, serve reference-exact (125.9 tok/s batched, 78.3 sequential) |
| HF fp32 oracle, five #3340 prompts | regenerated; prompt and continuation ids identical to the #3340 oracles |
mlxcel generate -n 40 and xla_oracle_check, five prompts |
5/5 token-exact (CLI 135 to 149 tok/s) |
xla_batch_bench B=4, 8 requests, 64-token prompt |
REFERENCE-EXACT PASS, 128.1 tok/s |
same, oracle prompt, --cancel 2 |
PASS, 2/2 cancelled, 127.2 tok/s |
same, oracle prompt, --buckets 256,512 |
PASS, every bucket routed, 102.3 tok/s |
B=8, 16 requests (--maxcap 32, as in #3340) |
PASS, 111.4 tok/s |
server, 4 concurrent /v1/completions, temperature 0, 24 tokens |
4/4 equal their CLI streams, 24/24 completion tokens each, requests overlapped, prompt cache/APC disabled |
"In computer science, a linked list is" has a minimum oracle top-2 gap of 0.0046 (the Metal f16 near-tie); it is exact at f32 here.
Prefill at capacity 4096 (before 66bf600a, after f3c771e3)
Fresh MLXCEL_CACHE_DIR per arm, f32.
| measurement | before | after |
|---|---|---|
whole model, uncached prefill compile (xla_ssm_model_check --compile-times) |
2.52 s | 3.51 s |
| whole model, uncached decode compile | 1.51 s | 1.50 s |
| whole model, 3584-token prefill, 3 runs | 3.514 to 3.520 s (1019 tok/s) | 3.626 to 3.645 s (986 tok/s) |
| single-layer probe 4096/3584, uncached compile | 0.75 s | 0.85 s |
| single-layer probe run, 4 runs | 0.089 to 0.092 s (39.4K tok/s) | 0.094 to 0.096 s (38.1K tok/s) |
The prefill is 3.2% slower on the whole model and about 4% slower per layer. Against each other the two arms differ by at most 1.07e-4 on last-token logits (max 93.6), with the same argmax; decode logits are bitwise identical. Probe error against HF is unchanged (y 5.96e-6 on both). The split-prefill state difference in xla_ssm_model_check went from 9.9e-5 to 5.3e-4, while whole versus token-by-token decode logits stayed at 1.0e-3 (before) and 0.97e-3 (after).
Nemotron-H 4B
| check | result |
|---|---|
HF fp32 oracle (--prompt 'The capital of France is' --max-new 40) |
ids identical to the committed nemotron_h_reference/hf-f32-oracle.json |
xla_hybrid_nemotron_h (release, ignored) |
5/5 |
validate_arch.sh --oracle <that file> --batch 4 --requests 8 --maxcap 24 |
exit 0: structural, token-exact 40/40, serve reference-exact (170 tokens) |
CLI with MLXCEL_PRINT_TOKEN_IDS=1 |
40/40 ids equal the HF oracle |
server, 4 concurrent /v1/completions, 40 tokens |
4/4 equal their CLI references in text and completion token count, prompt cache/APC disabled |
xla_ssm_single_seq, xla_ssm_state_carry |
6/6, 5/5 |
Falcon-H1 Tiny 90M
| check | result |
|---|---|
xla_falcon_h1 (fixtures regenerated, committed oracle) |
2/2: synthetic parity, and the 40 original-checkpoint oracle tokens match on hip |
validate_arch.sh --batch 4 --requests 8 --maxcap 24 (HF oracle from the venv) |
exit 0: structural, token-exact, serve reference-exact (133.0 tok/s) |
falcon_h1_cli_server_check.py --device hip |
CLI 40/40 token-exact with the oracle; 8/8 non-stream and SSE responses match displayed CLI text and token count; the Germany completion keeps its " \n" suffix |
Commands, logs, binary hashes and outputs are in acceptance/pr3434/f3c771e3450890fd738eff73ba3a22d6f4519d17/ on the ROCm host. Nothing was pushed or merged.
CUDA GB10 validation at
|
| gate | result |
|---|---|
ssm_gate.sh OUT cuda (tiny mixer, 11 cases) |
11/11 PASS; worst y 1.24e-6, h 1.19e-6, conv 3.6e-7 |
| probe decode 1/1 | y 2.623e-6, h 2.384e-7, conv 6.557e-7 (compile 0.71 s) |
| probe padded 256/224 | y 2.503e-6, h 3.949e-7, conv 5.960e-7 (compile 1.45 s) |
| probe full 256/256 | y 2.503e-6, h 2.198e-7, conv 7.153e-7 |
probe K-1 256/2 |
y 2.027e-6, h 1.118e-8, conv 6.557e-7 |
validate_arch.sh --batch 4 --requests 8 --maxcap 24 |
exit 0: structural, HF token-exact, serve reference-exact; B=4 90.62 tok/s (84.17 sequential) |
| CLI 40 tokens vs HF fp32 oracle | 40/40 token-exact, 158.63 tok/s |
server, 4 concurrent /v1/completions, temperature 0, 24 tokens |
4/4 equal their CLI streams in text and token counts, both non-streaming and SSE (8/8); requests overlapped |
The probe y errors are identical to the #3342 table; h moves in the last bits (padded 1.58e-7 to 3.95e-7, full 1.12e-7 to 2.20e-7), all far inside 5e-5. Server used the #3342 prompts with --ctx-size 2048 --max-batch-size 4, prompt cache and APC disabled by the recurrent guard.
Prefill at capacity 4096 (real mixer, real_len 3584, 3 alternating runs, fresh cache each)
| arm | compile s | run s (first call) | run s (warm) | max-abs y |
|---|---|---|---|---|
before 66bf600a |
1.59 / 1.70 / 1.64 | 0.072 / 0.078 / 0.071 | 0.075 / 0.073 / 0.071 | 4.172e-6 |
after f3c771e3 |
1.75 / 2.14 / 1.62 | 0.088 / 0.088 / 0.090 | 0.089 / 0.095 / 0.089 | 4.172e-6 |
Single-layer prefill is about 22% slower (warm median 0.073 to 0.089 s); compile time is unchanged within noise. Correctness is identical.
Hybrids (sharing the scan)
| model | gate | result, equal to the recorded docs |
|---|---|---|
| Nemotron-H 4B | xla_oracle_check vs committed oracle |
40/40 token-exact |
xla_batch_bench B=4 N=8 maxcap 24 |
reference-exact, 170 tokens (4.93 tok/s batched, 5.87 sequential) | |
| CLI 40 tokens | same 40 ids as the oracle and the recorded CLI | |
| server, 4 concurrent, capacity 256 | 8/8 text and count exact (non-streaming and SSE); first 24 ids of each CLI reference equal the recorded ones | |
xla_ssm_single_seq, xla_ssm_state_carry, xla_hybrid_nemotron_h |
6, 5, 5 passed | |
| Falcon-H1 Tiny 90M | xla_falcon_h1 suite (--ignored) |
2/2: synthetic parity for all three norm modes, 40 original-checkpoint oracle tokens on cuda |
falcon_h1_cli_server_check.py --device cuda |
40/40 oracle, 8/8 displayed text and counts; every response and CLI id list equals the recorded integrated-cuda-displayed result, Germany keeps its " \n" |
|
xla_batch_bench B=4 N=8 |
reference-exact (97.64 tok/s batched, 64.52 sequential) |
Not run: the Nemotron-H CPU prompt-cache unit test (no device involved). The Nemotron-H server check used the Falcon harness's prompts (the ones the Nemotron-H record used) with 40-token responses instead of 24.
Both backends pass every Mamba2, Nemotron-H and Falcon-H1 gate at the PR head with results equal to the earlier records. Adds the capacity-4096 prefill cost of the shifted-add prefix sum: about 22% on a single CUDA layer, 3.2% on a whole-model ROCm prefill, decode unchanged.
The hold comment asked for the capacity-4096 prefill timing before (main) and after this head; the recorded CUDA pair used the merge-base 66bf600. Re-measured on GB10 with main d43915f as the before arm, three alternating runs each from an empty cache: warm median 0.076 to 0.096 s (+26%), compile 1.51 to 1.67 s against 1.75 to 1.88 s, identical probe error. The Mamba2 scan and probe are unchanged between the two bases, so the two pairs agree. Refs #3343
CUDA: PASSThe CUDA items from the hold comment pass on GB10 at
The main-based timing pair agrees with the merge-base pair already recorded (+22%); the Mamba2 scan and probe are unchanged between the two bases. The 7x memory-traffic estimate on the
|
…#3471) Main has been red in `mlxcel-xla` since #3434 (db3a231): `registered_fixtures_are_byte_exact` and `emit_all_reproduces_registered_goldens` fail on `plamo2/prefill.mlir`. #3434 replaced the `reduce_window` cumsum in the Mamba2 SSD scan with a shifted-add prefix sum and regenerated the nemotron_h and falcon_h1 goldens, but the plamo2 fixture added by #3449 was frozen against the old scan. This re-freezes the registered goldens with `MLXCEL_FREEZE_GOLDENS=1`. Only `plamo2/prefill.mlir` and `plamo2/prefill_logits.mlir` change, and the op counts differ only by the expected substitution: 6 `reduce_window` ops are replaced by shifted `pad`, `slice` and `add`, the same pattern as the nemotron_h regeneration in #3434. No emitter code changes. ## Validation - `cargo test -p mlxcel-xla --lib`: 585 passed, 0 failed (2 failed on main). - `scripts/xla/validate_arch.sh --model models/plamo-2-1b --device metal`: structural gate passes. The execution tier could not run on this host because the checkpoint ships `tokenizer.jsonl` only and the trajectory dump reads `tokenizer.json`, the same limit #3449 recorded. The scan change itself was validated in #3434.
Summary
The production chunked SSD prefill returned NaN
yand a wrong final state on IREE-Metal whilelocal-taskpassed. IREE rewrites a left-padded prefix-sumstablehlo.reduce_windowintoiree_linalg_ext.scan, and the Metal target returns unrelated values for that op at rank 2 and above (a[3,3]module reproduces it without mlxcel). The-infmask inexp_segsumwas not the cause and is unchanged.acumand the masked segment sum inexp_segsum) are now a log-depth chain ofslice,padandaddin f32 (prefix_add).Builder::reduce_window_prefix_addhad no other caller and is removed.reduce_windowbecome 16 shifted-add levels; 20 levels at capacity 4096). Decode graphs are byte-identical. Summation is pairwise instead of sequential: onlocal-taskf32 the probe output moves by at most 7.2e-7 and the error against HF stays at 3.3e-6 to 3.6e-6. No new tensor size class appears; the largest tensor is still[nc,H,q,q].nemotron_handfalcon_h1prefill goldens are regenerated deliberately (they share the scan); only the expectedreduce_windowto shifted-add replacement changed in them.oracle_continuation.py --top2records the oracle's per-step top-2 logit gap (opt-in; default output unchanged),near_tie_check.pyjudges a stream by its first divergence (exit 0 exact, 3 near-tie, 1 mismatch, 2 usage error, empty stream, malformed oracle, or a text divergence inside a word that only token ids can place), andxla_oracle_checkprints the gap at the first divergence while keeping its strict exit status. Covered byspike/openxla/test_near_tie_check.py(12 cases, no model needed) and documented inspike/openxla/README.md.spike/openxla/FINDINGS_ssm.mdgains the IREE-Metal section with the full tables;src/lib/mlxcel-xla/README.mdnotes that Mamba2 validates on IREE-Metal.Test plan
Apple M1 Ultra, IREE 3.12.0rc20260721,
AntonV/mamba2-130m-hfat05e8773f, devicemetal.K-1and 3584/4096 all within 5e-5 (worsty1.5e-6,h2.0e-6); before the fixyNaN,h0.74.ssm_gate.shtiny mixer: 6/11 before, 11/11 after.y7.7e-4 to 1.6e-3, within 1% oflocal-taskf16. The emitted f16 graph keeps all five scan contractions and all prefix-sum adds in f32.validate_arch.sh --model models/hf/mamba2-130m --device metal --batch 4 --requests 8 --maxcap 24: exit 0 at f32; exit 0 at f16 with--skip-structural(the goldens are f32 graphs).xla_oracle_checkandmlxcel generate -n 40, five prompts: 5/5 token-exact at f32; at f16 4/5 exact and one near-tie (token 14, top-2 logit gap 0.004593)./v1/completionsat temperature 0: 4/4 equal their CLI streams at f32; at f16 3/4 equal CLI and 4/4 are token-exact against the oracle (the differing CLI stream is the near-tie above).cargo test --workspace --profile test-fast --features metal,accelerate -- --test-threads=1): 17045 passed, 2 failed. Both failures are root-crate Metal byte-identity tests (deepseek_v2::verify_tests::without_the_scope_a_block_differs_from_the_chain_on_metal,inkling2::speculative::tests::the_verify_block_matches_the_chain_on_metal) that fail deterministically on this host on their own; that test binary does not linkmlxcel-xlain this feature set, so they are independent of this PR and tracked separately.cargo clippy -p mlxcel-xla --lib --tests -- -D warnings,cargo clippy --features xla-iree --example xla_oracle_check -- -D warnings,cargo fmt --check,python3 -m unittest spike/openxla/test_near_tie_check.py.hip) and CUDA re-validation of the changed prefill graph, including a before/after prefill timing at capacity 4096, and execution of the Nemotron-H and Falcon-H1 hybrids. These run on their own hosts before merge.Closes #3343