Skip to content

GLM Metal: masked sparse attention and the DFlash2 q4 head keep the reference's NaN and Inf - #4396

Merged
ashhart merged 6 commits into
mainfrom
lead/glm-metal-nonfinite
Nov 22, 2026
Merged

ashhart merged 6 commits into
mainfrom
lead/glm-metal-nonfinite

Conversation

@ashhart

@ashhart ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

What this changes

GLM's Metal kernels now give the same NaN and Inf results as the float64 reference in three places where main's GLM kernel contract recorded known failures:

  • Masked sparse attention (glm_sparse_nax.metal: the 64-row, 32-row and split forms). A visible NaN score failed the score > NO_SCORE test and got zero probability, so a NaN never reached the output. Visible scores are now tested for NaN first; for every finite score the predicate is the old one.
  • Padding entries in the same kernels. An invalid list entry loaded V from key 0, and the zero probabilities multiplied an Inf or NaN there into the output. Invalid entries now read zero V. Selected keys and the order of the three matrix multiplies are unchanged.
  • The DFlash2 head's q4 affine matmul (affine_mm.metal). Main forms q + 128 with an adjusted bias, which equals s*q*x + b*x for finite values but can turn opposing infinities into NaN. GLM opts into TF_AM_EXACT_NONFINITE (from affine_mm.zig): when a half-row's group sum is not finite, the kernel recomputes that half from the raw codes. Other families do not set the define, so their kernels are byte-for-byte the code they compile today.

The repaired failures leave the contract's known-findings list.

Checks on this head

  • Kernel contract, GLM on an M5 Ultra (tf-kernel-contract --only glm, ReleaseFast): 468 PASS and no real failure in gates 1-4. Every planted control is still red. On main the same run has nine known failures (the six masked-score cases, two padding cases and the DFlash2 q4 head).
  • Gate 5 (launch limits) then stops with error: UnknownLaunch, on main as well. That is a separate gate-5 mapping bug, with its own fix in flight.
  • Finite outputs, source level: for finite scores the masking predicate is the old one. For finite group sums the original q + 128 multiply and its bias run as before. 1,008 exact rational checks of the identity and the fallback selection pass.

Not run yet

  • A before/after comparison on a device: served GLM tokens and Metal decode speed with this head against main. This PR stays out of the landing queue until both match.

@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner Author

M3 Ultra result for this head, with #4397 applied (without it the GLM contract stops at startup on pre-M5 Macs):

tf-kernel-contract --only glm, ReleaseFast: 102 kernels, 101 pass gates 1-4, 68 of 68 planted controls red, 0 known findings. Main with #4397 alone has 5 known findings on this GPU: the q4 nonfinite metal affine_mm glm m3 cases at group sizes 32, 64 and 128, with fp32 output, and the gather form, NaN where float64 gives +Inf. All five pass here, so the exact non-finite path also covers the pre-M5 TF_GLM_M3_FAST matmul.

The one failure left, glm_index_scores_nax bytes on pre-M5, and its threadgroup limit are on main without this PR and are tracked separately.

Still to do before this lands: the device check that finite outputs are bit-identical to main's kernels.

  • Synthena

@ashhart
ashhart force-pushed the lead/glm-metal-nonfinite branch from 18d6e44 to e02062c Compare November 22, 2026 09:44
@ashhart
ashhart merged commit 718aac9 into main Nov 22, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/glm-metal-nonfinite branch November 22, 2026 09:44
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.

1 participant