Skip to content

test(mlx): MLX_ENABLE_TF32=0, so the float32 MLX tests pass on M5 GPUs - #76

Merged
rkinas merged 2 commits into
rkinas:mainfrom
agentGreg:fix/mlx-tests-tf32-m5
Nov 2, 2026
Merged

rkinas merged 2 commits into
rkinas:mainfrom
agentGreg:fix/mlx-tests-tf32-m5

Conversation

@agentGreg

@agentGreg agentGreg commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

tests/test_mlx.py::test_mlx_gemma4_equals_torch fails on an Apple M5 Max (basal 2.4.1, mlx 0.32.4, mlx-lm 0.32.2):

assert 0.0006879717111587524 < 0.0002

The cause is neither Gemma 4 nor the sliding window. On an M5 (NAX) GPU, MLX runs float32 matmuls in TF32 by default:

float32 matmul (128×64 @ 64×128) relative error vs float64
M5 GPU, default 7.7e-4
M5 GPU, MLX_ENABLE_TF32=0 2.3e-7
CPU (mx.cpu) 2.0e-7

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_shared and run give 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.
  • mlx 0.32.0/0.32.2/0.32.4 and mlx-lm 0.32.0/0.32.1/0.32.2: the same error everywhere.
  • PyTorch fp32 vs fp64: 2.6e-7, so the reference is exact.
  • On mx.cpu all 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 as allow_tf32 = False for CUDA in basal/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 passed
  • full pytest: 761 passed, 53 skipped

Summary by Sourcery

Disable MLX TF32 execution during tests so float32 comparisons remain within tolerance on Apple M5 GPUs.

Bug Fixes:

  • Ensure MLX float32 tests produce accurate results on Apple M5 GPUs by disabling TF32 before MLX is imported.

Tests:

  • Configure the MLX test environment to use non-TF32 float32 matmuls, restoring the expected MLX-versus-PyTorch test accuracy.

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).
@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds 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

Change Details Files
Configure MLX tests to disable TF32 before MLX initialization, restoring the expected float32 numerical precision on M5 GPUs.
  • Add pytest configuration that sets MLX_ENABLE_TF32 to 0 before test modules import MLX.
  • Use setdefault so an explicitly supplied environment value is preserved.
  • Document the M5-specific precision issue and why served models are unaffected.
tests/conftest.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/conftest.py Resolved
…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).
@rkinas

rkinas commented Oct 29, 2026

Copy link
Copy Markdown
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 --mode mlx's float32 paths on M5 in 2.5.1. The PR will close when 2.5 is published on main.

@rkinas
rkinas merged commit 2e3fa80 into rkinas:main Nov 2, 2026
1 check passed
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.
@agentGreg
agentGreg deleted the fix/mlx-tests-tf32-m5 branch November 16, 2026 19:55
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