Repository navigation
Description
Summary
On A19 (iPhone 17 Pro Max, iOS 26.5.1) MLX produces deterministically wrong numerical results on the GPU in a real Hybrid-Transformer-Demucs source-separation pipeline. The separated audio stems contain +12 to +21 dB of spurious high-frequency energy (14–22 kHz) that is not present when the identical code, weights and parameters run on Apple Silicon Macs (M2). The corruption is:
- deterministic (not NaN, reproducible run to run),
- present in the output tensors themselves (we exported the raw WAVs from the device — it is not a playback/codec artifact),
- present in every output stem (drums/bass/other/vocals), growing toward Nyquist and broadband on vocals.
This matches the A19 GPU dot-product issue independently documented by Taras Zakharko's microbenchmark "Investigating the GPU Neural Accelerators on Apple A19/M5", which reports that "choosing certain matrix dimensions produces invalid results on A19", attributed to "a bug with masking out unused lanes in the dot product hardware."
We suspect the same root cause is surfacing through MLX's Neural-Accelerator matmul path, which #3083 enables for gen >= 18 phone architectures (i.e. A19).
Environment
- Device: iPhone 17 Pro Max (A19 Pro, 5 GPU cores), iOS 26.5.1, 12 GB
- mlx-swift 0.30.6 (also reproduced on 0.31.4)
- Model: Hybrid Transformer Demucs (
htdemucsandhtdemucs_ft), FP32 weights - Reference (clean): identical code/weights on M2 (both the app and the
demucs-mlx-swiftCLI)
Symptom (measured)
Per-band energy (dB relative to that stem's total) of each separated stem, A19 device vs. M2 CLI, identical model/params (htdemucs_ft, shifts=1, overlap=0.75, segment=5):
| stem | band | A19 device | M2 reference | delta |
|---|---|---|---|---|
| drums | 18–22 kHz | −59.4 | −71.8 | +12.4 dB |
| bass | 18–22 kHz | −63.5 | −76.1 | +12.5 dB |
| other | 14–18 / 18–22 kHz | −51.8 / −57.5 | −60.8 / −72.4 | +8.9 / +14.9 dB |
| vocals | 10–14 / 14–18 / 18–22 kHz | −12.2 / −14.7 / −12.4 | −25.2 / −29.3 / −33.0 | +13.0 / +14.6 / +20.6 dB |
Output peaks are not clipped (≈0.90 on both), ruling out output quantization/saturation. A third-party reference (a cloud separation of the same track) agrees closely with the M2 output, confirming the M2 result is the correct one.
What we tried (did not resolve)
MLX_ENABLE_TF32=0— verified actually applied (we loggedgetenv("MLX_ENABLE_TF32")at separation time and it reads"0"): no change, the spurious HF energy persists and the output is essentially identical to TF32 on. This is the documented control for the M5/A19 Neural-Accelerator reduced-precision path (cf. [BUG] M5 float32 precision issue since 0.30.0 #3534, closed as "expected behavior — Neural Accelerators trade precision for performance"), so what we are seeing is not the expected TF32 precision tradeoff but a separate correctness issue.- Forcing the high-level NAX flag off in the metal backend (
can_use_nax = false): the output changed but remained corrupted. - Forcing
rfft/irfftto the CPU stream (stream: .cpu): no change. - Upgrading mlx-swift 0.30.6 → 0.31.4: no change.
So the corruption is not the TF32 precision mode and not fully gated by the high-level NAX flag alone. We have not yet isolated the exact op (matmul / SDPA / conv / a custom Metal kernel).
Questions
- Is A19 (
gen >= 18phone) intended to use the Neural-Accelerator matmul path after Fix nax condition for iphone #3083? If so, is the dot-product correctness issue Zakharko documents for certain matrix shapes on A19 known to the team? - Is there a recommended workaround today to get numerically correct GPU results on A19 (e.g. a supported way to disable the NAX/tensor path on A19, or to avoid the affected matmul shapes)?
- Would a minimal matmul/conv GPU-vs-CPU reproducer on A19 be useful? We can build and share one, plus the full audio measurements and raw stems.
References
- A19 dot-product bug (microbenchmark): https://tzakharko.github.io/apple-neural-accelerators-benchmark/
- NAX gate for iPhone (
gen >= 18): Fix nax condition for iphone #3083 - M5 float32 precision via Neural Accelerators /
MLX_ENABLE_TF32(related, "expected behavior"): [BUG] M5 float32 precision issue since 0.30.0 #3534
Activity
IARFLOW commentedon Jun 23, 2026
Cross-linking a few related threads, since they bear directly on question 1 (whether A19 should be on the NAX path after #3083).
Direct precedent — the same mechanism was already patched once, for A18: ml-explore/mlx-swift-examples#462 reports gibberish LLM output on iPhone 16 Pro (A18) while iPhone 17 and Mac were fine — i.e. a Neural-Accelerator chip returning numerically corrupt results while other devices stay clean. That was addressed by #3083, which adjusted the NAX gate but kept it enabled for
gen >= 18(A19). So this exact symptom has already been acted on case by case, and A19 currently sits inside the gate. The path that produced corrupt output on A18 (and was disabled there) is still active on A19 for the matmul/conv shapes in our Demucs pipeline — which lines up with the lane-masking dot-product issue Zakharko documents.Independent evidence that MLX numerics already diverge across Apple Silicon generations: #3568 documents
mx.random.normalproducing different float32 output on M1 Max vs M3 Ultra/M5, traced to the FMA chain in the Metalerfinvkernel. A different op from ours, but it confirms that hardware-dependent numerical divergence in MLX Metal kernels is real and reproducible — consistent with a dot-product correctness issue on A19 rather than a precision tradeoff.Ruled out, for completeness: #3676 / #3477 (the
.at[slice].addoverlap-add spikes that surfaced indemucs-mlx) is not our root cause — it's an op-level regression in 0.31.2 that would also affect the Mac, whereas our Mac output is clean on both 0.30.6 and 0.31.4.Still happy to build the minimal matmul/conv GPU-vs-CPU reproducer on A19 (question 3) and share it alongside the raw stems and per-band measurements — just let us know if that would help triage.
zcbenz commentedon Jun 23, 2026
They should have the same output whether correct or corrupted, it is possible that
MLX_ENABLE_TF32=0is set too late. But I think the neutral accelerator is not relevant here since with or without it the results are still corrupted.To know what is happening I think the only way is to isolate the op that is outputting wrong results on A19, for example if the problem is in
matmulit would be nice to have code demonstrating that it outputs wrong result for given matrices in gpu stream compared to cpu stream.IARFLOW commentedon Jun 29, 2026
Follow-up with the minimal
matmulreproducer you asked for. I built a self-contained mlx-swift float32matmulGPU-vs-CPU comparison and ran it on the A19 (iPhone 17 Pro Max,iPhone18,2, iOS 26.5.1) and, as a control, on an M2 Mac — identical code, mlx-swift 0.30.6, default settings.The A19 GPU produces shape-dependent numerical divergence in float32
matmul. For affected shapes the.gpuresult differs from the.cpuresult by ~7e-4 to ~1e-3 relative (max-abs ~1.5e-2 to ~6.7e-2), where float32 should give ~1e-7. The M2 agrees with the CPU to ~1e-7 for every shape, and for the A19's unaffected shapes the GPU output is bit-identical to the M2. So this is specific to the A19 GPU and to certain shapes.Whether a shape is affected is decided by the GEMM dispatch, not a single dimension. The comparison reductions (
abs/max) are pinned to the CPU stream, so only the twomatmuls differ by stream.[A] M=N=64, K swept — diverges for K ≤ 127, clean (bit-identical to M2) for K ≥ 128:
[B] K=64, M=N swept 16…512 — diverges for every M=N:
[C] K=256, M=N swept 16…512 — clean for M=N ≤ 256, diverges for M=N ≥ 384:
M2 control (identical code): all clean, worst relative 8.3e-7 across all three sweeps.
Observations:
MLX_ENABLE_TF32=0did not fix the downstream audio corruption in our full (conv-heavy) Demucs pipeline. I'm happy to runMLX_ENABLE_TF32=0against this isolated matmul to disambiguate a reduced-precision path from a correctness bug — just say the word.Reproducer (mlx-swift; runs inside an iOS test/app on the device):
Happy to also build a
conv1dGPU-vs-CPU version (our audio pipeline is conv-heavy) or share the raw separated stems and per-band measurements. Just let me know which is most useful for triage.davidkoski commentedon Jun 30, 2026
This sounds exactly like NAX (A19) vs non-NAX (m2). The matmul is running in TF32, which is similar to bf16 I think. Well, it isn't float32, that is for sure. I don't know if you can easily set env vars on iOS, but MLX_ENABLE_TF32=0 would disable it. You can look for
enable_tf32()in the mlx source. The same issue should appear on M5.IARFLOW commentedon Jun 30, 2026
@davidkoski thanks — confirmed on A19 (iPhone 17 Pro Max, iOS 26.5.2). Setting
MLX_ENABLE_TF32=0viasetenv("MLX_ENABLE_TF32","0",1)at process start on iOS (before the first MLX op — verified it applies in-process: a canary matmul that diverges at ~1e-3 returns to ~1e-7) makes the isolated float32matmulbit-identical to the CPU stream for every previously-divergent shape (worst relative 9.7e-4 → 8.3e-7). So the matmul divergence is exactly the NAX/TF32 path, as you said.However,
MLX_ENABLE_TF32=0does not fix the actual workload. With TF32 verified off, the Hybrid-Transformer-Demucs separation on A19 still produces the same broadband high-frequency corruption in the output stems — confirmed both by per-band measurement and by ear (A/B on the same track, ON vs OFF; the artifact is unchanged). The isolated matmul is clean; the separated audio is not.I also built an isolated
conv1d/conv2dGPU-vs-CPU comparison on A19 with representative Demucs shapes (channels 2→48→96→192→384, k=8, stride 4, plus 2D analogues). It does not diverge — relative ~1e-6, with or without TF32. So neither the isolated matmul (once TF32 is off) nor these isolated conv shapes reproduce it, yet the full conv-heavy pipeline clearly still does.A few questions:
backend/metal/conv.cpphas noenable_tf32()gate. Conv that routes throughexplicit_gemm → steel_matmulshould fall off NAX when TF32 is off, but theimplicit_gemm/ winograd 2D kernels don't go through that gate. Is the A19 neural-accelerator path reachable from conv (or SDPA, or a custom kernel) in a wayMLX_ENABLE_TF32=0does not disable?conv repro (mlx-swift, runs in an iOS test on device; same harness as the matmul one above):
Happy to share the raw stems and the full per-band measurements.
zcbenz commentedon Jun 30, 2026
With
MLX_ENABLE_TF32=0the NAX would be turned off for everything, so your problem is not related to NAX.Sorry we can not really debug the issue for you, I think the best approach is to feed your model static input, compare the results between gpu/cpu layer by layer to find out the exact layer that caused the diverge, then compare results op by op in the layer to find out the exact op that outputs wrong result.
And you also mentioned custom kernel, which you should try to check first by replacing it with composited ops and see if results are still wrong.
IARFLOW commentedon Jul 1, 2026
Closing the loop — and thanks @zcbenz, @davidkoski, your advice was exactly right.
Following your suggestion to bisect and to check the custom kernel first, we isolated the standard ops on A19 (matmul with TF32 off, conv1d/conv2d, and rfft/irfft) — all numerically clean GPU-vs-CPU (~1e-6), matching the Mac. So this was never an MLX bug.
The root cause was in the third-party
demucs-mlx-swiftpackage's custom Metal kernels: the fused GroupNorm kernels were missing athreadgroup_barrierbetween reading the broadcast mean fromshared_sums[0]and overwriting the same shared memory for the per-simdgroup variance — a write-after-read race. The A19 scheduler exposed it (broadband high-frequency corruption in the separated stems); the M-series happened to order the accesses safely, so the exact same kernel source was clean on Mac. Forcing the kernels to their pure-MLX fallbacks removed the artifact; adding the barrier fixes it while keeping the fused-kernel performance.Fix: kylehowells/demucs-mlx-swift#4. Thanks again — the "replace the custom kernel with composited ops" suggestion is precisely what pinpointed it.
zcbenz commentedon Jul 3, 2026
Glad you found out the root cause, I'm closing this since it is not a mlx bug.