Repository navigation
fix(tests): bound fused MoE GeGLU parity by the reference's measured jitter - #1020
Merged
Merged
Conversation
…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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fused_moe_parity_tests::fused_moe_geglu_kernel_matches_references_gemma4_shapeassertednrms_gather < 5e-3for the fused kernel against the productiongather_qmmfallback. That bound sits below the fallback's own deviation from the dense f32 ground truth, a quantity the test already computed asnrms_ref_jitterand 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-packagecargo testdoes not buildmlxcel-coreat 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:
0e00e0gather_qmm1.1698e-24.5227e-2gather_qmmvs dense f32 (the reference's own error)1.1574e-24.2602e-2The fused kernel is bit-exact against ground truth, so its distance from
gather_qmmis forced to equalgather_qmm's own error. A5e-3bound 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 smaller320 / 192 / E 16 / K 4), M1 Ultra / Metal:nrms_dense(fused vs truth)nmax_densenrms_ref_jitter(gather_qmmvs truth)nmax_ref_jitternrms_gather / nrms_ref_jitternmax_gather / nmax_ref_jitter54 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_qmmcarries ~1.2e-2 of error and the dense reference carries none, so the dense assertion strictly dominates any fused-vs-gather_qmmbound. No threshold on the gather comparison can catch a fused-kernel defect thatnrms_dense < 5e-4has 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:
5e-4 / 2e-2, unchanged. This is the sharp correctness gate for the kernel.gather_qmmvs dense f32 gets an absolute ceiling (5e-2 / 2e-1), derived from the measured max of1.803e-2 / 5.429e-2with 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.gather_qmmis bounded atmax(margin * measured_jitter, historical_constant):1.5xthe measured nrms jitter or5e-3, and2.0xthe measured nmax jitter or5e-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):
fused vs bf16-rounded dense f32 reference deviates (seed 886): nrms=1.0000e-3 nmax=3.7524e-32.
gather_qmm-side perturbation, simulating a fallback regression (full test):gather_qmm fallback deviates from the dense f32 reference (seed 886): nrms=5.1002e-2 nmax=1.8972e-1Before 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):
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)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.rsonly, no production code:nmax_ref_jitterinstead of discarding it, and print it in the diagnostic lineGATHER_JITTER_{NRMS,NMAX}_CEILINGandGATHER_JITTER_{NRMS,NMAX}_MARGIN, with the measured values recorded in their doc commentsgather_qmm-vs-dense assertiongather_qmmconstants with the jitter-scaled limitsTest plan
cargo fmt --all -- --checkcargo clippy --release -p mlxcel-core --all-targets --features metal,accelerate -- -D warningscargo clippy --release --all-targets --features metal,accelerate -- -D warningscargo test --release -p mlxcel-core --features metal,accelerate --no-fail-fast -- --test-threads=1mainRefs #1007 (which flips the CI gate to
cargo test --workspace, and needs this landed first).Closes #964