Skip to content

cuda qk_rows: head dims to 512, a weightless V norm and MLX's rounded rows - #1071

Closed
jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:cuda-qk-gemma
Closed

jschmied wants to merge 2 commits into
ashhart:mainfrom
jschmied:cuda-qk-gemma

Conversation

@jschmied

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 4 of #1048 (agreed there: the rounding points are options on the shared operator, off by default): qk_rows gets what Gemma 4's attention inputs need, as shared options. Nothing calls them yet.

  • Head dims to 512 (today at most 256), for Gemma 4's global layers.
  • Shape.v_norm: v by a unit (weightless) RMS norm, bf16(x * inv), without RoPE, into the v output and the v cache.
    Without it v is copied as today.
  • Shape.round: MLX's rounding points, as gemma4/glue.metal's gemma_norm_rope computes them:
    q and k = bf16(w * bf16(x * inv)), then rotate-half RoPE in fp32 on those bf16 values, stored as bf16. Without it,
    (x * inv) * w stays in fp32 until the store, as tf_qk_rows does.
  • V read from k's raw columns (Gemma 4's attention_k_eq_v global layers, q | k rows) needs nothing new: the source
    strides already point v at k. Proportional RoPE (64 of 256 pairs rotated) is an inv table with zeros past pair 64.
  • Two new kernels, tf_qk_rows_once (tf_qk_rows' arithmetic at dims to 512, 512 threads) and tf_qk_rows_round
    (MLX's rows), from one device function in zig/kernels/cuda/qk_round.cuh whose bits are its options. A head dim
    past 256, round or v_norm selects them; every other call keeps its kernel and its launch.
  • qk_rows.cu's shared structs moved verbatim to qk_rows.cuh, so the controls can include them; tf_qk_rows and
    tf_qk_rows_w compile to the same SASS.
  • core/qk_ref.zig: a head in float64 at its rounding points, as ranges over an fp32 run's spread
    (bf16_round.zig rounds float64 to bf16 once; the same file as in cuda norms: bf16 residual tails with a branch norm, a layer scalar and MLX's rounded rows #1056 and cuda act_rows: GeGLU and SwiGLU in MLX's bf16 steps #1068).

Gemma 4 uses round with v_norm on its sliding (head dim 256) and global (512, v from k) layers, and round alone
for the 12B assistant's query-only rows.

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 needed; shapes from families/gemma4/config.zig (12B: 16 query heads, 8 x 256 sliding and 1 x 512
    global key heads; 31B: 32 query heads, 16 x 256 and 4 x 512). The norm weights' ranges in the tests follow
    mlx-community/gemma-4-12B-it-4bit (q_norm 0.95 to 1.05, k_norm 0.06 to 0.13).
  • Exactness:
    • tf-cuda-test qk-rows: MLX's rows with the unit v norm at 12B's and 31B's sliding (256, RoPE from inv_freq)
      and global layers (512, v from k's columns, 64 of 256 pairs rotated), the assistant's query-only rows at 256 and
      512, one rounding at 512 (RoPE from a table), MLX's rows unrotated, and the unit v norm alone at 256 and 128, each
      at 1, 3, 17 and 300 rows. 36,836,736 outputs bit-equal to float64's value at the rounding points; 27,264 (0.07 %)
      where an fp32 inverse within its proven spread, or cosf/sinf within 2 ulps, can round a point either way,
      each within that range; the k and v caches hold the outputs' bytes at their slots; every smaller row count's bits
      equal the 300-row call's.
    • Where both kernels run (head dims 128 and 256, v_norm alone), q and k are bit-equal to tf_qk_rows' and
      tf_qk_rows_w's.
    • The range: the fp32 inverse is within gamma(5 + dim / 32) / 2 + 4 * 2^-24 of float64's (the warp tree and the
      warps in order, then four fp32 steps); x * inv, the RoPE products and their sum each add 2^-24; the products by
      a bf16 weight of a bf16 value are exact, so each output is float64's at the rounding points evaluated at the ends.
  • 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 88 kernels: 88 pass gates 1-4, 0 fail, 0 known findings; 10 controls: 10 red as planted, 0 missed; 1533 limit problems
    (main: SUMMARY 85 kernels: 85 pass gates 1-4, 0 fail, 0 known findings; 7 controls: 7 red as planted, 0 missed; 1533 limit problems).
    • 3 new entries (zig/tests/cuda/contract_qk_round.zig): tf_qk_rows_round at Gemma 4 31B's global form (512,
      v from k's columns by a unit norm, 64 of 256 pairs rotated) and sliding form (256, unit v norm), tf_qk_rows_once
      at 512 with RoPE from a table; each at 1 and 300 rows and the 16-row tile's edges (5 shapes).
    • 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).
    • Gate 4: integer inputs whose squares are exact in fp32; fp16 sums overflow (51x the bound that carries the
      rounding points), bf16 sums stay within it, so the spec states both (sums.f16 = 40, sums.bf16 = 0).
    • Controls: the global form without the normalized row's rounding, without the weighted row's rounding before RoPE,
      and without v's unit norm, each red at gate 3.
    • Gate 5: 2 launches recorded from tf-cuda-test kernel-contract-limits over fatbins for sm_80, 86, 89, 120 and 121
      (30-36 registers, 64 static shared bytes, bound 512): both records current on all five; the run over every image reports 877 problems, as main's does.
    • The limit problems in the SUMMARY line are main's own (records 1.1.1 lists without a compiled usage yet);
      this commit adds none.
  • Default path unchanged: cuobjdump -sass of tf_qk_rows and tf_qk_rows_w is identical to main's on sm_80, 86,
    89, 120 and 121 (10 of 10), and their launches pass the same arguments.
  • Tests run: zig build test from an empty cache: 213/213 steps, 2,964 of 2,978 tests passed, 14 skipped (main: 211/211 steps, 2,959 of 2,973 tests passed,
    14 skipped). tf-cuda-test qk-rows, norm-rows 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 (step 8 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_qk_rows_once carries tf_qk_rows' arithmetic to head dim 512 and can norm v by a unit
RMS norm; tf_qk_rows_round adds MLX's rounding points, q and k = bf16(w * bf16(x * inv))
before RoPE, as Metal's gemma_norm_rope computes them. Shape.round and Shape.v_norm select
them, as does a head dim past 256; every other call keeps its kernel and its launch, and
tf_qk_rows and tf_qk_rows_w keep their code.

Both are registered with the kernel contract: their own check compares every output with
float64's value at the rounding points (a range over the fp32 inverse's spread and cosf's
ulps), and three controls each drop one option.
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 CUDA qk_rows extension is included in 1.1.2 (https://github.com/ashhart/TensorFold/releases/tag/v1.1.2), with your original authorship retained. It supports head dimensions in multiples of 32 up to 512, a weightless V norm and optional MLX-style rounded rows. Closing this as carried, and thank you.

It landed as 93a3607 and 9c4af08.

- Synthema, Ash's AI Assistant.

@ashhart ashhart closed this Oct 21, 2026
@jschmied
jschmied deleted the cuda-qk-gemma 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