Option to opt out of TF32 for fp32 conv: small-kD conv3d is ~5000× less precise on M5 since 0.32
☑️ I understand it is strictly prohibited to use AI to write issues. What happens
On an M5 Max, fp32 mx.conv3d with batch 1, a small kernel depth and no depth padding is much less
precise on the GPU in 0.32 than in 0.31. The relative L2 error against the CPU result goes from about
1e-6 to about 6.4e-3. With batch 2, or with depth padding, the error stays at about 1e-6.
Repro
import mlx.core as mx
mx.random.seed(7)
def rel(a, b):
d = a - b
return (mx.sqrt((d * d).sum()) / mx.sqrt((b * b).sum())).item()
def gpu_vs_cpu(f):
with mx.stream(mx.cpu):
c = f()
mx.eval(c)
g = f()
mx.eval(g)
return rel(g, c)
x = mx.random.normal((1, 5, 64, 64, 128))
w = mx.random.normal((128, 3, 3, 3, 128)) * 0.03
print(mx.__version__)
print("conv3d N=1, pad (0,1,1):", gpu_vs_cpu(lambda: mx.conv3d(x, w, padding=(0, 1, 1))))
print("conv3d N=2, pad (0,1,1):", gpu_vs_cpu(lambda: mx.conv3d(mx.concatenate([x, x]), w, padding=(0, 1, 1))))
print("conv3d N=1, pad (1,1,1):", gpu_vs_cpu(lambda: mx.conv3d(x, w, padding=(1, 1, 1))))| relative L2, GPU vs CPU | 0.31.2 | 0.32.3 | 0.32.3, MLX_ENABLE_TF32=0 |
|---|---|---|---|
| conv3d N=1, pad (0,1,1) | 1.31e-06 | 6.40e-03 | 4.52e-06 |
| conv3d N=2, pad (0,1,1) | 1.31e-06 | 1.31e-06 | 1.31e-06 |
| conv3d N=1, pad (1,1,1) | 1.24e-06 | 1.24e-06 | 1.24e-06 |
Cause
#3785 sends small kernel-depth 3D convs to per-frame 2D convs, so 3×3 kernels use Winograd.
Winograd does its product with steel_matmul. On NAX, steel_matmul runs fp32 as TF32 unless
MLX_ENABLE_TF32=0. The implicit GEMM conv path (used before #3785, and still used with batch 2
or depth padding) does not use TF32. I understand the change is intended for speed
(see ml-explore/mlx-swift#492).
Request
A way to turn off the NAX/TF32 path for one call or one scope, for example a parameter on the conv
functions (and possibly matmul / SDPA). Today the only way is MLX_ENABLE_TF32=0. It applies to the
whole process, is read once at startup, and makes other fp32 work much slower:
| op (fp32, M5 Max) | TF32 on | TF32 off |
|---|---|---|
| matmul 4096×4096×4096 | 3.5 ms | 9.7 ms |
| matmul (7800×1536)×(1536×8960) | 4.7 ms | 14.3 ms |
| scaled_dot_product_attention B1 H12 L7800 D128 | 18.4 ms | 35.0 ms |
| conv3d kD=3, 384 ch, input (5, 64, 64) | 3.2 ms | 3.7 ms |
So a model that needs exact conv results (for example a video VAE) cannot get them without slowing down all other fp32 work in the same process (for example an fp32 DiT).
Environment
macOS 27.2, Apple M5 Max. Precision: mlx 0.31.2 / 0.32.2 / 0.32.3 from pip. Timings: same machine, measured through mlx-swift 0.32.2 (same core).
NOTE: This draft was submitted by a human in the loop but I did have AI assistance in verification of the actual issue and the wording and contents of the message.
Source: ml-explore/mlx