Skip to content

fix(tests): bound fused MoE GeGLU parity by the reference's measured jitter - #1020

Merged
inureyes merged 1 commit into
mainfrom
fix/issue-964-moe-parity-jitter-threshold
Aug 4, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/issue-964-moe-parity-jitter-threshold

Conversation

@inureyes

@inureyes inureyes commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

fused_moe_parity_tests::fused_moe_geglu_kernel_matches_references_gemma4_shape asserted nrms_gather < 5e-3 for the fused kernel against the production gather_qmm fallback. That bound sits below the fallback's own deviation from the dense f32 ground truth, a quantity the test already computed as nrms_ref_jitter and then discarded. The assertion was measuring the reference's noise, not the kernel, and no implementation could satisfy it.

The failure is deterministic, not seed-dependent. Five consecutive runs of the unmodified test on main (a86e6340a) produced byte-identical numbers, and a 54-point sweep found the bound unreachable at every seed and both shapes. The "intermittent" reading is explained by #1007: the root-package cargo test does not build mlxcel-core at all, so a root run can observe this test neither passing nor failing.

Evidence

At seed 886 the test's own diagnostic line contains the diagnosis:

comparison nrms nmax
fused vs dense f32 (bf16-rounded) 0e0 0e0
fused vs gather_qmm 1.1698e-2 4.5227e-2
gather_qmm vs dense f32 (the reference's own error) 1.1574e-2 4.2602e-2

The fused kernel is bit-exact against ground truth, so its distance from gather_qmm is forced to equal gather_qmm's own error. A 5e-3 bound on that distance is unsatisfiable by construction.

Seed sweep

27 seeds (886..905, 1, 31, 77, 2026, 4242, 12345, 999999) across two shapes (the gemma-4 decode shape din 2816 / dff 704 / E 16 / K 8, and a smaller 320 / 192 / E 16 / K 4), M1 Ultra / Metal:

quantity min median max
nrms_dense (fused vs truth) 0 0 1.843e-4
nmax_dense 0 0 6.916e-3
nrms_ref_jitter (gather_qmm vs truth) 6.107e-3 1.179e-2 1.803e-2
nmax_ref_jitter 2.072e-2 4.077e-2 5.429e-2
ratio nrms_gather / nrms_ref_jitter 0.9939 1.0106 1.0368
ratio nmax_gather / nmax_ref_jitter 0.9315 1.0060 1.1919

54 of 54 points fail the old bound. The ratio pinned near 1.0 at every point is exactly what "fused agrees with truth, so its distance from the reference is the reference's own error" predicts.

Which bound was chosen, and why

Of the three options in the issue, this takes the second (bound relative to the measured baseline) and adds the piece the data demands.

The arithmetic driving the choice: comparing against a noisy reference can never be sharper than comparing against a clean one. gather_qmm carries ~1.2e-2 of error and the dense reference carries none, so the dense assertion strictly dominates any fused-vs-gather_qmm bound. No threshold on the gather comparison can catch a fused-kernel defect that nrms_dense < 5e-4 has not already caught. Widening the gather constant, tightening it, or making it relative does not change that.

The three comparisons are therefore assigned to what each can actually gate:

  1. Fused vs dense f32 stays at 5e-4 / 2e-2, unchanged. This is the sharp correctness gate for the kernel.
  2. gather_qmm vs dense f32 gets an absolute ceiling (5e-2 / 2e-1), derived from the measured max of 1.803e-2 / 5.429e-2 with headroom. This assertion is new: the test computed the quantity and discarded it, so the production fallback has never had a correctness gate here at all. It also stops the relative bound in (3) from silently widening along with a reference that has itself drifted.
  3. Fused vs gather_qmm is bounded at max(margin * measured_jitter, historical_constant): 1.5x the measured nrms jitter or 5e-3, and 2.0x the measured nmax jitter or 5e-2, whichever is larger. The floor keeps the project's standard fp16-parity allowance binding on any backend whose fallback is closer to exact than Metal's, so this is never looser than the old constant where the old constant was reachable.

The margins (1.5x / 2.0x) sit above the measured maxima (1.037 / 1.192) with deliberate headroom. This test also runs on CUDA per its module docs, and no CUDA node is reachable from this machine, so a snug margin would trade a deterministic failure for a flaky one on an unmeasured backend.

Negative controls

Perturbation applied through a temporary scaffold (an exact alternating-sign relative perturbation of magnitude eps), exercising the committed assertions unmodified. The scaffold is not part of this PR.

1. Fused-side perturbation, simulating a kernel regression (full test):

eps result
0, 1e-5, 1e-4, 2e-4, 5e-4 pass
1e-3 FAIL fused vs bf16-rounded dense f32 reference deviates (seed 886): nrms=1.0000e-3 nmax=3.7524e-3
5e-3, 1e-2, 2e-2, 5e-2 FAIL (same gate)

2. gather_qmm-side perturbation, simulating a fallback regression (full test):

eps result
0, 1e-2, 3e-2 pass
5e-2 FAIL gather_qmm fallback deviates from the dense f32 reference (seed 886): nrms=5.1002e-2 nmax=1.8972e-1
7e-2, 1e-1 FAIL (same gate)

Before this PR the test could not detect this class of regression at all.

3. Cross-path bound in isolation (dense gate temporarily disabled so assertion 3 is reachable):

eps result
0, 5e-3, 1e-2 pass
1.5e-2 FAIL fused and gather_qmm disagree beyond the fallback's own jitter (seed 886): nrms=1.9287e-2 (limit 1.7362e-2) nmax=7.9639e-2 (limit 8.5204e-2)
2e-2, 3e-2 FAIL (same gate)

The observed threshold matches the prediction eps > sqrt(margin^2 - 1) * jitter = 1.118 * 1.1574e-2 = 1.29e-2, so the relative bound is calibrated, not vacuous.

What changed

src/lib/mlxcel-core/src/fused_moe_parity_tests.rs only, no production code:

  • capture nmax_ref_jitter instead of discarding it, and print it in the diagnostic line
  • add GATHER_JITTER_{NRMS,NMAX}_CEILING and GATHER_JITTER_{NRMS,NMAX}_MARGIN, with the measured values recorded in their doc comments
  • add the gather_qmm-vs-dense assertion
  • replace the fixed fused-vs-gather_qmm constants with the jitter-scaled limits
  • update the module doc to describe the three-way structure

Test plan

  • cargo fmt --all -- --check
  • cargo clippy --release -p mlxcel-core --all-targets --features metal,accelerate -- -D warnings
  • cargo clippy --release --all-targets --features metal,accelerate -- -D warnings
  • cargo test --release -p mlxcel-core --features metal,accelerate --no-fail-fast -- --test-threads=1
  • Determinism established by 5 consecutive runs of the failing test on main
  • 54-point seed and shape sweep
  • Three negative controls above

Refs #1007 (which flips the CI gate to cargo test --workspace, and needs this landed first).

Closes #964

…jitter

`fused_moe_geglu_kernel_matches_references_gemma4_shape` asserted `nrms_gather < 5e-3` for the fused kernel against the production `gather_qmm` fallback. That bound sits below the fallback's own deviation from the dense f32 ground truth, a quantity the test already computed as `nrms_ref_jitter` and then threw away. At seed 886 fused-vs-gather is 1.1698e-2, gather-vs-dense is 1.1574e-2, and fused-vs-dense is exactly 0: the kernel is bit-exact against ground truth and the assertion was measuring the reference's noise, so no implementation could satisfy it.

A 54-point sweep (27 seeds across the gemma-4 decode shape and a smaller shape, M1 Ultra / Metal) shows the bound is unreachable everywhere rather than seed-dependent. All 54 points fail it. The gather-vs-dense baseline spans 6.107e-3 to 1.803e-2 nrms and 2.072e-2 to 5.429e-2 nmax, while the fused-vs-gather over gather-vs-dense ratio spans 0.994 to 1.037 (nrms) and 0.932 to 1.192 (nmax), which is what "fused agrees with truth, so its distance from the reference is the reference's own error" predicts.

The dense-reference assertion is unchanged. It is the sharp correctness gate, and it strictly dominates any fused-vs-gather bound because `gather_qmm` is the noisier of the two references. The two `gather_qmm` comparisons are now stated against measured quantities instead of constants pulled from nowhere: an absolute ceiling on the fallback's own deviation from dense f32, which is the first assertion this test has ever made about the production fallback, and a cross-path bound that scales with the jitter each run measures. The cross-path bound is floored at the project's 5e-3 / 5e-2 fp16-parity allowance so it never becomes looser than the historical constant on a backend whose fallback is closer to exact than Metal's.

Negative controls, all on the committed assertions with the perturbation applied through a temporary scaffold that was reverted before this commit:

- Perturbing the fused output by a relative eps: the test passes at eps <= 5e-4 and fails from eps = 1e-3 upward, caught by the dense gate.
- Perturbing the `gather_qmm` output: passes to eps = 3e-2 and fails from eps = 5e-2, caught by the new fallback ceiling. This is a regression class the test previously could not see at all.
- Disabling the dense gate so the cross-path bound is reachable: passes to eps = 1e-2 and fails from eps = 1.5e-2 (nrms 1.9287e-2 against a limit of 1.7362e-2), matching the predicted threshold sqrt(1.5^2 - 1) * 1.1574e-2 = 1.29e-2. The relative bound is calibrated, not vacuous.

Closes #964
@inureyes inureyes added type:test Test related changes priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers status:review Under review labels Aug 4, 2026
@inureyes
inureyes merged commit 2b3e5cb into main Aug 4, 2026
4 checks passed
@inureyes
inureyes deleted the fix/issue-964-moe-parity-jitter-threshold branch August 4, 2026 02:33
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…jitter (lablup#1020)

`fused_moe_geglu_kernel_matches_references_gemma4_shape` asserted `nrms_gather < 5e-3` for the fused kernel against the production `gather_qmm` fallback. That bound sits below the fallback's own deviation from the dense f32 ground truth, a quantity the test already computed as `nrms_ref_jitter` and then threw away. At seed 886 fused-vs-gather is 1.1698e-2, gather-vs-dense is 1.1574e-2, and fused-vs-dense is exactly 0: the kernel is bit-exact against ground truth and the assertion was measuring the reference's noise, so no implementation could satisfy it.

A 54-point sweep (27 seeds across the gemma-4 decode shape and a smaller shape, M1 Ultra / Metal) shows the bound is unreachable everywhere rather than seed-dependent. All 54 points fail it. The gather-vs-dense baseline spans 6.107e-3 to 1.803e-2 nrms and 2.072e-2 to 5.429e-2 nmax, while the fused-vs-gather over gather-vs-dense ratio spans 0.994 to 1.037 (nrms) and 0.932 to 1.192 (nmax), which is what "fused agrees with truth, so its distance from the reference is the reference's own error" predicts.

The dense-reference assertion is unchanged. It is the sharp correctness gate, and it strictly dominates any fused-vs-gather bound because `gather_qmm` is the noisier of the two references. The two `gather_qmm` comparisons are now stated against measured quantities instead of constants pulled from nowhere: an absolute ceiling on the fallback's own deviation from dense f32, which is the first assertion this test has ever made about the production fallback, and a cross-path bound that scales with the jitter each run measures. The cross-path bound is floored at the project's 5e-3 / 5e-2 fp16-parity allowance so it never becomes looser than the historical constant on a backend whose fallback is closer to exact than Metal's.

Negative controls, all on the committed assertions with the perturbation applied through a temporary scaffold that was reverted before this commit:

- Perturbing the fused output by a relative eps: the test passes at eps <= 5e-4 and fails from eps = 1e-3 upward, caught by the dense gate.
- Perturbing the `gather_qmm` output: passes to eps = 3e-2 and fails from eps = 5e-2, caught by the new fallback ceiling. This is a regression class the test previously could not see at all.
- Disabling the dense gate so the cross-path bound is reachable: passes to eps = 1e-2 and fails from eps = 1.5e-2 (nrms 1.9287e-2 against a limit of 1.7362e-2), matching the predicted threshold sqrt(1.5^2 - 1) * 1.1574e-2 = 1.29e-2. The relative bound is calibrated, not vacuous.

Closes lablup#964
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.

fix(tests): fused MoE GeGLU parity threshold is tighter than the gather_qmm reference's own jitter

1 participant