Skip to content

Add missing threadgroup barrier in fused GroupNorm kernels - #4

Merged
kylehowells merged 1 commit into
kylehowells:masterfrom
IARFLOW:fix/groupnorm-threadgroup-barrier
Jul 4, 2026
Merged

kylehowells merged 1 commit into
kylehowells:masterfrom
IARFLOW:fix/groupnorm-threadgroup-barrier

Conversation

@IARFLOW

@IARFLOW IARFLOW commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

The fused GroupNorm+GELU and GroupNorm+GLU Metal kernels have a write-after-read hazard on threadgroup memory: there is no threadgroup_barrier between reading the broadcast mean from shared_sums[0] (end of pass 1) and overwriting shared_sums[wid] with the per-simdgroup variance (pass 2).

When the GPU scheduler lets a fast simdgroup reach the pass-2 write before a lagging simdgroup has finished reading the mean, shared_sums[0] is clobbered early and the variance for that group is computed against a corrupted mean. The result is sporadic, broadband numerical noise.

Impact

Hardware-dependent. On Apple A19 (iPhone 17 Pro) it surfaces as audible broadband high-frequency hash across all separated stems. On Apple-silicon Macs (M-series) the same kernel source happens to order the shared-memory accesses safely and stays clean — so the bug is invisible there.

How it was isolated

  • Isolated GPU-vs-CPU reproducers showed matmul (with TF32 off), conv1d/conv2d, and rfft/irfft are all numerically clean on A19 (~1e-6) — so the corruption was not a standard MLX op.
  • Forcing both GroupNorm kernels to their pure-MLX fallbacks (hybridThreshold = 0) removed the artifact entirely.
  • Re-enabling the kernels with the added barrier keeps the artifact gone and preserves the fused-kernel performance.

Thanks to @zcbenz / @davidkoski on ml-explore/mlx#3702 for the "replace the custom kernel with composited ops" suggestion that pinpointed this.

Fix

Add threadgroup_barrier(mem_flags::mem_threadgroup) after the mean is read and before pass 2 reuses shared_sums, in both kernels. One line each.

The fused GroupNorm+GELU and GroupNorm+GLU Metal kernels read the
broadcast mean from shared_sums[0] in pass 1 and then overwrite
shared_sums[wid] with the per-simdgroup variance in pass 2, with no
threadgroup_barrier between the read and the write.

On GPUs whose scheduler lets a fast simdgroup reach the pass-2 write
before a lagging simdgroup has finished reading the mean, shared_sums[0]
is clobbered early, corrupting the variance for that group. The result
is sporadic broadband numerical noise in the output.

This is hardware-dependent: it surfaces as audible broadband
high-frequency hash in the separated stems on Apple A19 (iPhone 17 Pro),
while Apple-silicon Macs happen to order the shared-memory accesses
safely and stay clean with the exact same kernel source.

Add a threadgroup_barrier(mem_flags::mem_threadgroup) after the mean is
read and before pass 2 reuses shared_sums, in both kernels.
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