Repository navigation
test(vision): key bf16 oracles on the kernel each backend selects - #2631
Merged
Merged
Conversation
The bf16 addmm rounding oracles were captured on ROCm and x86_64 Linux CPU and failed on Metal. MLX's Metal AddMM routes a single-row product to gemv_axbpy, whose epilogue casts the F32 accumulator to bf16 before adding the bias, so at one row addmm rounds like a separate add. Upstream mlx 0.32.4 on an M1 Ultra shows the same split: GPU addmm gives the separate value at one row and the fused value at two or more rows, and the CPU stream is fused at every row count. - Run the addmm routing oracles (DINOv2 Objects linear, attention and MLP, SAM 3, SAM 3D Body and Sapiens2 linears) on two identical rows, where every backend rounds once, and pin the single-row value per backend through a shared test helper. - Key the Sapiens2 compiled RoPE oracle on ROCm. Metal and CPU produce the eager value for both graphs, as upstream does. - Keep the SAM 3D Body MHR CPU geometry oracle bit-exact on Linux. On other hosts, bound the four transcendental fields to 2 ULP, since macOS MLX CPU evaluates atan2, sin and cos through Apple's math library. - Hold the default-device lock in tests whose expectation or comparison depends on the default device, so a concurrent CPU guard cannot switch it mid-test.
Member
Author
|
Security and performance review of the test-only change: no CRITICAL, HIGH, or MEDIUM findings, no fixes pushed.
LOW, no change requested:
|
…le notes Review follow-up: state that the CUDA addmm key comes from reading its source, warn that one-column outputs also take Metal's gemv route, and say in the Sapiens2 fixture README that the compiled RoPE capture is ROCm-only.
4 tasks
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
The bf16 addmm rounding oracles in
dinov2_layers::policyandvision::densefail on Metal because they were captured on ROCm gfx1151 and x86_64 Linux CPU (#2551 and #2578 were validated on ROCm, not Metal). The failures come from which MLX kernel runs on each backend. This PR keys each oracle on a property the test can detect, without a production change. It also fixes a default-device race that made one failure vary between runs.Root cause
AddMM::eval_gpusendsmin(M, N) == 1togemv_axbpy. TheGEMVKernelepilogue inmlx/backend/metal/kernels/gemv.hcasts the F32 accumulator to bf16 before adding the bias, so the result equals a separate add. Two or more rows go to steel GEMM orgemv_wide(gemv_wide_configrejectsM <= 1on every architecture generation), and those apply the bias before the single rounding. CPU and ROCm copy C into the output and run one GEMM with beta, so they round once at every row count.mlx0.32.4 from PyPI on this M1 Ultra (applegpu_g13d): GPUmx.addmmat 1 row gives1.9375(separate), at 2, 5, 16 and 64 rows it gives1.9296875(fused); the CPU stream gives1.9296875at every row count. The fc2 halfway case gives1.0at[1,1,4]and1.0078125at[2,1,4],[1,2,4]and[2,4].gemv.handAddMM::eval_gpuare byte-identical between the previous pinbff10220and the current pin7ca40033. Upstreammainstill has the cast before the bias add, and the 0.32.4 wheel, which predates both pins, reproduces the failure. I found no existing ml-explore/mlx issue for it.-4.5597553e-6is a ROCm capture. Upstreammlx0.32.4 on this host gives-4.589557647705078e-6for both the compiled and the eager graph, on GPU and on CPU, and the repository generatortests/fixtures/sapiens2/generate_rope_oracle.pystops with "compiled and eager RoPE unexpectedly agree".mlx/backend/cpu/simd/accelerate_simd.h). Of the 388 elements inbody_params,hand_params,fk_stateandfk_global_rot, 27 differ by 1 or 2 ULP, and every other field matches bit for bit. Seven of the 27 are single atan2 calls. For those seven, upstreammx.arctan2on this host's CPU returns exactly mlxcel's values, and it differs from the fixture at exactly those seven.DefaultDeviceGuard::cpu(). Tests that read the default device withoutlock_default_device()could run on the CPU in the middle of their span. That is whycompiled_objects_gelu_tanh_matches_authentic_power_oraclefailed only in parallel, andsapiens2_bf16_linear_uses_fused_bias_epiloguepassed in parallel only when it happened to run on the CPU. Once the MHR test passes, it holds its guard longer, which also exposedmoge3_refiner_forced_cache_pressure_preserves_full_unet_output(12 of 24 parallel runs) and the batched-decode wrapper tests. Skipping the MHR test alone took that to 0 of 24.Changes
New
src/vision/addmm_oracle_test_support.rs(test-only) documents the routing and providesROUTING_ROWS,routing_rowsandsingle_row_addmm_rounds_twice(Metal GPU default device).The DINOv2 Objects linear, attention (qkv and proj), MLP (fc1 and fc2), SAM 3, SAM 3D Body and Sapiens2 linear oracles now run on two identical rows. There, the fused value is asserted on every backend and the separate value still differs, so the tests still prove the layer routes through addmm. The single-row value is also pinned per backend: separate on Metal GPU, and fused (the original authenticated oracle) elsewhere. No assertion was loosened to a tolerance.
The Sapiens2 RoPE oracle expects the ROCm capture on ROCm GPU and upstream's eager-equal value elsewhere.
The MHR geometry oracle stays bit-exact on Linux and for every non-transcendental field. Only on non-Linux hosts are the four transcendental fields bounded to 2 ULP.
These tests now hold
lock_default_device()(ormlx_test_guard()): every test touched above, the tanh GELU oracle and feed-forward tests, the MoGe3 focal solver oracle, the MoGe3 refiner cache-pressure test, and the three dense VLM batched-decode wrapper tests. The lock was not added insidebatched_decode_harness, becausevlm_family_batched_decode_testsalready calls it undermlx_test_guard()and the lock is not reentrant.tests/fixtures/sapiens2/README.mdnow says the compiled RoPE capture is ROCm-only and gives the Metal and CPU value.Changes during review
Validation
Done on an Apple M1 Ultra, release build with
--features metal,accelerate:cargo test --release --features metal,accelerate --lib vision::dense: 192 passed, 3 of 3 runs in default mode and 3 of 3 with--test-threads=1. A stress loop of the same binary passed 40 of 40 default-mode runs (main failed 5 per run).cargo test --release --features metal,accelerate --lib dinov2_layers::policy: 5 passed, 3 of 3 runs in each mode (main failed 5 of 5).vision::filter, one default-mode run: 882 passed.vision::encoders::dinov2: 20 of 20 runs passed.cargo fmt --check,cargo clippy --features metal,accelerate --lib --tests -- -D warnings,scripts/ci/check_cross_repo_refs.py.No real-checkpoint run was needed: no production code changed. The diff touches only
#[cfg(test)]code and test files, and the production layers keep callingmlxcel_core::addmmexactly as upstreamnn.Lineardoes on each backend.Not verified:
AddMM::eval_gpu(one rocBLAS or naive GEMM with beta for every M), not from a run.gemv_wide. That this path rounds once comes from its source (alpha * result + beta * biascomputed in F32, then one cast), not from a run.Closes #2626