Skip to content

cuda act_rows: GeGLU and SwiGLU in MLX's bf16 steps - #1068

Closed
jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:cuda-act-gelu
Closed

jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:cuda-act-gelu

Conversation

@jschmied

@jschmied jschmied commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Includes core/bf16_round.zig with #1056's sign fix (an underflow to zero keeps its sign, checked by raw bits), so the four steps share one identical file.

Step 2 of #1048 (beside #1056; the rounding steps are options on the shared operator, off by default, as agreed there): act_rows gets the two activations Gemma 4 needs in MLX's bf16 steps. Nothing calls them yet.

  • ActRows.geglu: bf16(gelu_tanh(gate) * up) as mlx_lm's gelu_approx(gate) * up in MLX's bf16 ops, the
    arithmetic of gemma4/glue.metal's gemma_geglu (Gemma 4's MLP): x^3, c1 x^3, x + c1 x^3, c0 (...),
    tanh, 0.5 x, 1 + tanh, their product, each rounded to bf16, with c0 and c1 as bf16.
  • ActRows.swigluRound: SwiGLU in tf_draft_swiglu's bf16 steps (exp(|g|), 1 + e, its reciprocal, the sigmoid,
    g * sigmoid, each rounded), as gemma_draft_swiglu computes it for Gemma 4 31B's DFlash drafter
    (families/gemma4/dflash.zig).
  • Both take swiglu's rows (gate and up at their strides, [gate | up] as a fused projection writes them) and write
    the 64-value sums of the stored outputs. swiglu keeps its kernel and its launch.
  • Two kernels, tf_geglu_rows and tf_swiglu_rows_round, from zig/kernels/cuda/act_round.cuh, whose keep mask
    holds the rounding points (a constant in the served kernels). act_rows.cu's existing code is unchanged.
  • core/act_ref.zig: both in float64 at their rounding points (bf16_round.zig rounds float64 to bf16 once).

Not here: the Metal kernel's interleaved 16-column gate/up layout (IL); a CUDA family lays gate and up out as halves.

Receipt

  • Environment: TensorFold d1018b31 (1.1.1) plus this commit; one DGX Spark (GB10, sm_121, CUDA 13.0, nvcc 13.0.88).
    No checkpoint; widths from families/gemma4 (12B MLP 15,360, 31B 21,504, the 31B DFlash drafter's 10,752, the 12B
    assistant's 8,192).
  • Exactness:
    • tf-cuda-test act-rows: GeGLU at 15,360 and 21,504 (fused and padded rows) and 8,192, the SwiGLU steps at
      10,752 (both layouts), both at the block edges 64, 960, 1,024, 1,088, 2,048 and 2,112, each at 1, 3, 17 and 64 rows
      (128- and 256-thread blocks). 7,553,014 outputs bit-equal to float64's value at the rounding points, 10 within
      the range tanhf's or expf's 2 ulps allow; 118,016 group sums bit-equal to the fp32 tree; every smaller row
      count's bits equal the 64-row call's.
    • Every step besides tanhf, expf and the division is exact in fp32 for bf16 operands (x^3 and each product of
      two bf16 values) or a sum of two bf16 values, so the output is float64's at the rounding points with those three
      values taken at the ends of their ulps (2^-21 either way).
  • Decode speed: n/a, nothing calls it.
  • Prompt speed: n/a, nothing calls it.
  • Kernel contract (docs/kernel-contract.md), tf-cuda-test kernel-contract (built -Dnvcc, ReleaseFast) on the GB10:
    SUMMARY 87 kernels: 87 pass gates 1-4, 0 fail, 0 known findings; 19 controls: 19 red as planted, 0 missed; 1533 limit problems
    (main: 85 kernels, 7 controls, the same 1,533 limit problems: main's own records, such as the pending ones of
    act_rows' existing kernels; this change adds none).
    • 2 new entries (zig/tests/cuda/contract_act_round.zig) at the four Gemma widths, the row edge at 16 (the block
      size switch) and the 1,024-value block edges (12 shapes each).
    • Gates 1 and 3 are the entry's own check: every output in float64's range at the rounding points (NaN and
      infinities as float64's), the sums bit-equal to the fp32 tree. Gate 4 is N/A (no sum in an output; the group sums
      are checked bit for bit instead).
    • Controls: one kernel, tf_ctl_act_round, run with every rounding point but one, twelve times (seven GeGLU points,
      five SwiGLU points; 0.5 x is exact for a bf16 x, so it has none): each red at gate 3 on its first shape.
    • Gate 5: 4 launches (each kernel at 256 and 128 threads) recorded from tf-cuda-test kernel-contract-limits over
      fatbins for sm_80, 86, 89, 120 and 121 (30-34 registers), in core/contract/gemma_cuda_records.zig (registry.zig
      would pass 600 lines): every record current; the run's other output is main's line for line (877 stale records).
  • Default path unchanged: cuobjdump -sass of tf_swiglu_rows, tf_gate_rows, tf_group_sums_rows and
    tf_dconv_rows is identical to main's on sm_80, 86, 89, 120 and 121 (20 of 20); swiglu's launch passes the
    same arguments to the same kernel.
  • Tests run: zig build test from an empty cache: 213/213 steps, 2,964 of 2,978 tests passed, 14 skipped (main:
    211/211, 2,959 of 2,973). tf-cuda-test act-rows, norm-rows, norms pass. zig fmt --check clean.
    lean_check.py: 444 findings, as on main.
  • Not run, and why: Metal (no Metal change); sm_80, 86, 89 and 120 only through their recorded compiles (gate 5);
    no model calls it yet (steps 8 and 11 of Proposal: Gemma 4 on CUDA, built on the shared CUDA operators (shared extensions first) #1048).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (No
    serving path changes.)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • A new or changed GPU kernel is registered with the kernel contract (docs/kernel-contract.md); each backend's SUMMARY line is in the receipt.
  • Prompt processing is no slower than the last release.
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

tf_geglu_rows computes mlx_lm's gelu_approx(gate) * up as MLX's bf16 ops (Gemma 4's MLP),
and tf_swiglu_rows_round SwiGLU in tf_draft_swiglu's bf16 steps (Gemma 4's DFlash drafter),
both with the 64-value group sums of the stored outputs. swiglu keeps its kernel and launch.

Both are registered with the kernel contract: every output against float64's value at the
rounding points (a range over tanhf's or expf's ulps), the sums bit for bit with the fp32
tree, and twelve controls each drop one rounding point.
bf16(-2^-135) gave +0 where bf16 rounding gives -0 (0x8000). The tests now compare
u16 bits: signed zeros from underflow, subnormal ties, the normal boundary, overflow to
both infinities, and the random fp32 sweep against the fp32 path's bits.
ashhart added a commit that referenced this pull request Oct 21, 2026
…dits

The 1.1.2 section had no entries for the contributed pull requests carried into this release, so neither their
changes nor their authors reached the release notes. Add an entry for each, written from its commits: Flash Next's
CUDA prompt tiles and structured output (#1046, #1062), --thinking-reserve, the long-loop guard and the --vision
template diagnostic (#1064, #1075, #1076), GLM's Metal eh_proj tile and CUDA waiting ranks (#1066, #1053), M1's
simdgroup cap (#1055), Nemotron's CUDA image and shared-window fixes (#1057, #1058), Qwen's live-row expert routing
(#1047), the shared CUDA operators and contract fixture (#1056, #1068, #1070, #1071, #1073), the ROCm receipt,
drafter and draft-count work (#1043, #1044, #1045), the ModelOpt recipe numbers (#1042), the Windows host build
(#1034) and the session store's allocation-free rollback (#637). Thank #643's and #1060's reporters as the other
entries thank theirs.
@ashhart

ashhart commented Oct 21, 2026

Copy link
Copy Markdown
Owner

The shared CUDA act_rows GeGLU and rounded SwiGLU operators are included in 1.1.2 (https://github.com/ashhart/TensorFold/releases/tag/v1.1.2), with your original authorship retained. They follow MLX-style bf16 rounding steps. Closing this as carried, and thank you.

It landed as 42a7f76 and 8ef345b.

- Synthema, Ash's AI Assistant.

@ashhart ashhart closed this Oct 21, 2026
@jschmied
jschmied deleted the cuda-act-gelu branch October 23, 2026 21:02
@ashhart ashhart added the landed Landed on main by the maintainers; the commits keep the contributor's authorship label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

landed Landed on main by the maintainers; the commits keep the contributor's authorship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants