Skip to content

A19 (iPhone 17 Pro) GPU returns numerically wrong results in float32 workloads — deterministic HF corruption in a Demucs separation pipeline; M2/M3 clean #3702

Closed

Description

@IARFLOW

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 (htdemucs and htdemucs_ft), FP32 weights
  • Reference (clean): identical code/weights on M2 (both the app and the demucs-mlx-swift CLI)

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 logged getenv("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/irfft to 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

  1. Is A19 (gen >= 18 phone) 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?
  2. 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)?
  3. 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

Activity

  1. IARFLOW commented on Jun 23, 2026

    @IARFLOW
    Author

    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.normal producing different float32 output on M1 Max vs M3 Ultra/M5, traced to the FMA chain in the Metal erfinv kernel. 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].add overlap-add spikes that surfaced in demucs-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.

  2. zcbenz commented on Jun 23, 2026

    @zcbenz
    Member
    • MLX_ENABLE_TF32=0 — verified actually applied (we logged getenv("MLX_ENABLE_TF32") at separation time and it reads "0"): no change,
    • Forcing the high-level NAX flag off in the metal backend (can_use_nax = false): the output changed but remained corrupted.

    They should have the same output whether correct or corrupted, it is possible that MLX_ENABLE_TF32=0 is 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 matmul it would be nice to have code demonstrating that it outputs wrong result for given matrices in gpu stream compared to cpu stream.

  3. IARFLOW commented on Jun 29, 2026

    @IARFLOW
    Author

    Follow-up with the minimal matmul reproducer you asked for. I built a self-contained mlx-swift float32 matmul GPU-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 .gpu result differs from the .cpu result 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 two matmuls differ by stream.

    [A] M=N=64, K swept — diverges for K ≤ 127, clean (bit-identical to M2) for K ≥ 128:

           MxKxN        maxAbs      relative
       64x  16x64      1.507e-02   7.366e-04   <-- diverges
       64x  24x64      1.507e-02   7.542e-04   <-- diverges
       64x  32x64      1.928e-02   7.795e-04   <-- diverges
       64x  48x64      2.035e-02   7.752e-04   <-- diverges
       64x  64x64      2.468e-02   9.023e-04   <-- diverges
       64x  96x64      2.995e-02   6.952e-04   <-- diverges
       64x 120x64      2.962e-02   7.277e-04   <-- diverges
       64x 124x64      3.292e-02   7.223e-04   <-- diverges
       64x 126x64      3.292e-02   8.050e-04   <-- diverges
       64x 127x64      3.270e-02   8.870e-04   <-- diverges
       64x 128x64      1.526e-05   3.308e-07
       64x 129x64      1.526e-05   3.616e-07
       64x 192x64      3.052e-05   5.756e-07
       64x 256x64      3.433e-05   5.929e-07
       64x 512x64      6.866e-05   8.325e-07
    

    [B] K=64, M=N swept 16…512 — diverges for every M=N:

       16x  64x16      2.253e-02   7.633e-04   <-- diverges
       32x  64x32      2.187e-02   7.810e-04   <-- diverges
       48x  64x48      3.000e-02   9.710e-04   <-- diverges
       64x  64x64      2.447e-02   7.489e-04   <-- diverges
       96x  64x96      3.256e-02   9.345e-04   <-- diverges
      128x  64x128     2.564e-02   7.560e-04   <-- diverges
      192x  64x192     2.994e-02   9.290e-04   <-- diverges
      256x  64x256     3.233e-02   7.850e-04   <-- diverges
      384x  64x384     3.220e-02   7.801e-04   <-- diverges
      512x  64x512     3.278e-02   8.189e-04   <-- diverges
    

    [C] K=256, M=N swept 16…512 — clean for M=N ≤ 256, diverges for M=N ≥ 384:

       16x 256x16      2.003e-05   4.443e-07
       64x 256x64      3.052e-05   4.331e-07
      128x 256x128     3.815e-05   5.698e-07
      256x 256x256     4.196e-05   5.733e-07
      384x 256x384     6.051e-02   8.503e-04   <-- diverges
      512x 256x512     6.686e-02   8.298e-04   <-- diverges
    

    M2 control (identical code): all clean, worst relative 8.3e-7 across all three sweeps.

    Observations:

    • The error magnitude (~1e-3) is in the range of reduced-precision (bf16/tf32-like) accumulation, but it is shape-gated, not global — on the affected shapes the A19 GPU loses ~10 bits vs the CPU; on the others it is exact float32 and bit-identical to the M2.
    • Earlier (issue body) we verified MLX_ENABLE_TF32=0 did not fix the downstream audio corruption in our full (conv-heavy) Demucs pipeline. I'm happy to run MLX_ENABLE_TF32=0 against this isolated matmul to disambiguate a reduced-precision path from a correctness bug — just say the word.
    • Unaffected A19 shapes are bit-identical to the M2, which rules out the comparison harness.

    Reproducer (mlx-swift; runs inside an iOS test/app on the device):

    import MLX
    
    func matmulGpuVsCpu(m: Int, k: Int, n: Int) -> (maxAbs: Float, rel: Float) {
        let a = MLXRandom.normal([m, k], stream: .cpu)
        let b = MLXRandom.normal([k, n], stream: .cpu)
        eval(a, b)
        let g = matmul(a, b, stream: .gpu)   // suspect
        let c = matmul(a, b, stream: .cpu)   // reference
        eval(g, c)
        let diff   = abs(g - c, stream: .cpu)
        let maxAbs = max(diff, stream: .cpu).item(Float.self)
        let scale  = max(abs(c, stream: .cpu), stream: .cpu).item(Float.self)
        return (maxAbs, scale > 0 ? maxAbs / scale : maxAbs)
    }
    
    // A19: rel ~1e-3 for the affected shapes; ~1e-7 for K>=128 at M=N=64. M2: ~1e-7 for all.
    for (m, k, n) in [(64,64,64), (64,127,64), (64,128,64), (512,64,512), (512,256,512)] {
        let r = matmulGpuVsCpu(m: m, k: k, n: n)
        print("\(m)x\(k)x\(n)  maxAbs=\(r.maxAbs)  rel=\(r.rel)")
    }

    Happy to also build a conv1d GPU-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.

  4. davidkoski commented on Jun 30, 2026

    @davidkoski
    Member

    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.

  5. IARFLOW commented on Jun 30, 2026

    @IARFLOW
    Author

    @davidkoski thanks — confirmed on A19 (iPhone 17 Pro Max, iOS 26.5.2). Setting MLX_ENABLE_TF32=0 via setenv("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 float32 matmul bit-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=0 does 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/conv2d GPU-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:

    1. On Metal, backend/metal/conv.cpp has no enable_tf32() gate. Conv that routes through explicit_gemm → steel_matmul should fall off NAX when TF32 is off, but the implicit_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 way MLX_ENABLE_TF32=0 does not disable?
    2. Is there a supported way to force the entire graph off the A19 NAX/tensor path, so I can confirm whether that fully eliminates the corruption?
    3. Would the raw stems + per-band spectra (A19 vs M2) help triage?

    conv repro (mlx-swift, runs in an iOS test on device; same harness as the matmul one above):

    import MLX
    
    func conv1dGpuVsCpu(cin: Int, cout: Int, k: Int, len: Int, stride: Int) -> Float {
        let x = MLXRandom.normal([1, len, cin], stream: .cpu)
        let w = MLXRandom.normal([cout, k, cin], stream: .cpu)
        eval(x, w)
        let g = conv1d(x, w, stride: stride, stream: .gpu)   // suspect
        let c = conv1d(x, w, stride: stride, stream: .cpu)   // reference
        eval(g, c)
        let maxAbs = max(abs(g - c, stream: .cpu), stream: .cpu).item(Float.self)
        let scale  = max(abs(c, stream: .cpu), stream: .cpu).item(Float.self)
        return scale > 0 ? maxAbs / scale : maxAbs
    }
    
    // A19: rel ~1e-6 (clean) for all of these, with or without MLX_ENABLE_TF32=0:
    for (cin, cout, k, len, s) in [(2,48,8,4096,4), (48,96,8,1024,4), (96,192,8,256,4), (192,384,8,64,4)] {
        print("\(cin)->\(cout) k\(k) s\(s): rel=\(conv1dGpuVsCpu(cin: cin, cout: cout, k: k, len: len, stride: s))")
    }

    Happy to share the raw stems and the full per-band measurements.

  6. zcbenz commented on Jun 30, 2026

    @zcbenz
    Member

    With MLX_ENABLE_TF32=0 the 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.

  7. IARFLOW commented on Jul 1, 2026

    @IARFLOW
    Author

    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-swift package's custom Metal kernels: the fused GroupNorm kernels were missing a threadgroup_barrier between reading the broadcast mean from shared_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.

  8. zcbenz commented on Jul 3, 2026

    @zcbenz
    Member

    Glad you found out the root cause, I'm closing this since it is not a mlx bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions