Repository navigation
[Metal] Fix precision loss in addmm - #4769
Merged
Merged
Conversation
zcbenz
force-pushed
the
fix-metal-addmm-precision
branch
from
October 27, 2026 09:29
70c75c3 to
80337ee
Compare
Contributor
Author
|
@zcbenz The new regression test exposed a separate, pre-existing CUDA precision error. I reproduced the same failures on unchanged upstream code, so the Metal change did not cause them. Both errors involve an early loss of precision, but they have different causes in each backend. I opened #4784 to fix the CUDA issue. |
zcbenz
force-pushed
the
fix-metal-addmm-precision
branch
from
October 29, 2026 04:51
80337ee to
9119753
Compare
zcbenz
approved these changes
Oct 29, 2026
1 task
inureyes
added a commit
to lablup/mlxcel
that referenced
this pull request
Nov 10, 2026
## Summary Four BF16 `addmm` oracle tests failed on Metal because they expected a single-row product to round twice. That expectation came from the old `gemv.h` epilogue, which cast the F32 accumulator to BF16 before adding the bias. Upstream ml-explore/mlx#4769 ("[Metal] Fix precision loss in addmm", commit `49366677`, 2026-10-29) adds the bias to the accumulator first in both `GEMVKernel` and `GEMVTKernel`. That commit has been in our MLX pin since #2876 (`9f806b36`), so `gemv_axbpy` now rounds once like every other route. The kernel change is intended upstream and no vision layer diverges from upstream `mlx.nn.Linear`, so no routing change is needed. ## Evidence (Mac Studio, Apple M3 Ultra) 1x4 @ 4x3 BF16 `addmm` with bias, expected fused `[1.296875, -0.50390625, 1.9296875]`, separate add `[..., 1.9375]`: | MLX | GPU, 1 row | GPU, 2 rows | CPU, 1 and 2 rows | |---|---|---|---| | released `mlx` 0.32.4 (PyPI) | separate | fused | fused | | upstream `mlx` built at `9f806b36` | fused | fused | fused | | mlxcel at `main` | fused | fused | fused | Released 0.32.4 shows the old split on M3 Ultra too, so the change comes from the pin, not from the GPU generation. ## Changes - `src/vision/addmm_oracle_test_support.rs`: remove `single_row_addmm_rounds_twice`, and rewrite the module doc around the pinned kernels and #4769. - The four oracles (sam3, sam3d_body, sapiens2, dinov2) assert the fused value at one row on every backend and keep the `ROUTING_ROWS` assertions. If a pin change brings the double rounding back, the one-row assertion fails. - The Sapiens2 oracle also asserts that plain `matmul + add` still gives the separate value at one row. ## Verification - The four tests pass on M3 Ultra: `cargo test -p mlxcel --lib --profile test-fast --features metal,accelerate -- --test-threads=1` filtered to them, 4 passed. - `cargo clippy -p mlxcel --lib --tests --features metal,accelerate -- -D warnings` is clean. - Hosts checked: M3 Ultra only. M1 Ultra, CUDA and ROCm were not run. CPU, ROCm and CUDA already expected the fused value, and the one-row assertion now expects the same value everywhere. Fixes #2916
1 task
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.
Metal's
addmmcan lose precision prematurely during thealpha * (A @ B) + beta * Ccomputation.Currently, the GEMV and GEMVT kernels cast the accumulator and coefficients down to the output data type before applying scaling and bias. Similarly, the split-K output transform computes the scaled bias using output precision. For BF16 and FP16, these early conversions can cause incorrect rounding or avoidable overflow.
For example:
The current implementation returns
1.0. The corrected implementation returns the exactly representable result,1.0078125.This change:
gemv.hto the precompiled kernel's build dependencies.addmmbenchmark.Tested and validated on Apple M4 Pro.