Skip to content

[Metal] Fix precision loss in addmm - #4769

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
tetsuo:fix-metal-addmm-precision
Oct 29, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
tetsuo:fix-metal-addmm-precision

Conversation

@tetsuo

@tetsuo tetsuo commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Metal's addmm can lose precision prematurely during the alpha * (A @ B) + beta * C computation.

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:

import mlx.core as mx

a = mx.array([[1, 1 / 256]], dtype=mx.bfloat16)
b = mx.array([[1], [1]], dtype=mx.bfloat16)
c = mx.array([1 / 256], dtype=mx.bfloat16)

print(mx.addmm(c, a, b, stream=mx.gpu).item())

The current implementation returns 1.0. The corrected implementation returns the exactly representable result, 1.0078125.

This change:

  • Keeps scaling and bias addition in accumulator precision, then converts the final result to the output dtype.
  • Corrects the shared affine transforms. Regular dense GEMM already uses accumulator-typed transforms.
  • Adds gemv.h to the precompiled kernel's build dependencies.
  • Adds regression tests covering BF16, FP16, and F32, including batching, transposed inputs, strided bias, and overflow cases.
  • Adds a focused addmm benchmark.

Tested and validated on Apple M4 Pro.

@tetsuo

tetsuo commented Oct 28, 2026

Copy link
Copy Markdown
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
zcbenz force-pushed the fix-metal-addmm-precision branch from 80337ee to 9119753 Compare October 29, 2026 04:51
@zcbenz zcbenz changed the title Fix premature rounding in addmm [Metal] Fix precision loss in addmm Oct 29, 2026
@zcbenz
zcbenz merged commit 4936667 into ml-explore:main Oct 29, 2026
30 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants