Repository navigation
Add missing threadgroup barrier in fused GroupNorm kernels - #4
Merged
kylehowells merged 1 commit intoJul 4, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The fused GroupNorm+GELU and GroupNorm+GLU Metal kernels have a write-after-read hazard on threadgroup memory: there is no
threadgroup_barrierbetween reading the broadcast mean fromshared_sums[0](end of pass 1) and overwritingshared_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
conv1d/conv2d, andrfft/irfftare all numerically clean on A19 (~1e-6) — so the corruption was not a standard MLX op.hybridThreshold = 0) removed the artifact entirely.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 reusesshared_sums, in both kernels. One line each.