Repository navigation
test(mlx): MLX_ENABLE_TF32=0, so the float32 MLX tests pass on M5 GPUs - #76
Merged
Merged
Conversation
On an M5 (NAX) GPU, MLX runs float32 matmuls in TF32 by default: 7.7e-4 relative error against float64 (2.3e-7 with MLX_ENABLE_TF32=0 or on the CPU). test_mlx_gemma4_equals_torch compares a float32 tiny Gemma 4 with PyTorch at 2e-4 and failed on an M5 Max (6.9e-4, on the one readout that is not saturated); with the variable it passes, as do the other MLX tests. MLX reads the variable at its first matmul, so it is set in tests/conftest.py. Served models are not affected: bf16 weights are exact in TF32 (5.2e-7 for an fp32 matmul of bf16 values on the M5 GPU).
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds an early test-environment setting to disable MLX TF32, preventing M5 GPU float32 matmul error from exceeding the PyTorch comparison tolerance; no production engine behavior changes. Verification reports all MLX tests passing and 761 full-suite tests passing with 53 skipped. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/conftest.py" line_range="7" />
<code_context>
+their weights are bf16, which TF32 holds exactly, and the readout accumulates in float32."""
+import os
+
+os.environ.setdefault("MLX_ENABLE_TF32", "0")
</code_context>
<issue_to_address>
**Gemma 4 comparison fails**
When the test process starts with `MLX_ENABLE_TF32=1`, `os.environ.setdefault` preserves the inherited value, so MLX uses TF32 for float32 matmuls and `test_mlx_gemma4_equals_torch` exceeds its `2e-4` PyTorch comparison tolerance on M5 GPUs; the test suite fails.
Set `os.environ["MLX_ENABLE_TF32"] = "0"` so the inherited value is overridden.
</issue_to_address>…me float32 Sourcery review on rkinas#76: setdefault kept MLX_ENABLE_TF32=1 from the environment, and test_mlx_gemma4_equals_torch failed on M5 GPUs again. With the assignment, the MLX tests pass with MLX_ENABLE_TF32=1 set (13 passed).
Owner
|
Thank you, Grzegorz! Planned for basal 2.5: it only touches the test setup, so we are merging it into the 2.5 release branch now, after our checks. Good catch on TF32 on M5; we will also look at what it means for |
rkinas
added a commit
that referenced
this pull request
Nov 2, 2026
… version CONTRIBUTING.md credits the 2.5 pull requests as the release notes' community line lists them (Paweł Kiszczak #52, #62, #64, #66, #68, #71, #73, #75; Marcin Brzeżański #56; Grzegorz Brzezinka #76, #78) and adds Dariusz Koryto (#53, #58, #59, #60 and #55); the README's list names him too. docs/changes/README.md and the changelog.py help use 2.5.1, the next release, as the example.
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.
Problem
tests/test_mlx.py::test_mlx_gemma4_equals_torchfails on an Apple M5 Max (basal 2.4.1, mlx 0.32.4, mlx-lm 0.32.2):The cause is neither Gemma 4 nor the sliding window. On an M5 (NAX) GPU, MLX runs float32 matmuls in TF32 by default:
MLX_ENABLE_TF32=0mx.cpu)The test compares a float32 tiny Gemma 4 with PyTorch at a 2e-4 tolerance. The 6.9e-4 difference is on the one readout that is not saturated (0.24/0.76).
Ruled out:
run_sharedandrungive identical results (difference 0.0), and the error does not depend on the prefix length (8–120 tokens, including below the window of 16). The shared prefix and the sliding window are fine.mx.cpuall 13 MLX tests pass.Change
New
tests/conftest.py:os.environ["MLX_ENABLE_TF32"] = "0", which also overrides an inherited value, since the tolerances assume float32. MLX reads the variable at its first matmul, so it has to be set before any test module imports mlx, not inside the test. It follows the same rule asallow_tf32 = Falsefor CUDA inbasal/server.py.No engine code changes. Served models are not affected: bf16 weights are exact in TF32 (an fp32 matmul of bf16 values on the M5 GPU: 5.2e-7), and the readout accumulates in float32.
Verification
Apple M5 Max, Python 3.12,
uv pip install -e .[test]:pytest tests/test_mlx.py: before, 1 failed and 12 passed; after, 13 passedpytest: 761 passed, 53 skippedSummary by Sourcery
Disable MLX TF32 execution during tests so float32 comparisons remain within tolerance on Apple M5 GPUs.
Bug Fixes:
Tests: