Repository navigation
Conversation
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.
9 tasks done
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.
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. |
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.
What this changes
Includes
core/bf16_round.zigwith #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_rowsgets what Gemma 4's attention inputs need, as shared options. Nothing calls them yet.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, asgemma4/glue.metal'sgemma_norm_ropecomputes 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) * wstays in fp32 until the store, astf_qk_rowsdoes.attention_k_eq_vglobal layers,q | krows) needs nothing new: the sourcestrides already point v at k. Proportional RoPE (64 of 256 pairs rotated) is an
invtable with zeros past pair 64.tf_qk_rows_once(tf_qk_rows' arithmetic at dims to 512, 512 threads) andtf_qk_rows_round(MLX's rows), from one device function in
zig/kernels/cuda/qk_round.cuhwhose bits are its options. A head dimpast 256,
roundorv_normselects them; every other call keeps its kernel and its launch.qk_rows.cu's shared structs moved verbatim toqk_rows.cuh, so the controls can include them;tf_qk_rowsandtf_qk_rows_wcompile 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.zigrounds 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
roundwithv_normon its sliding (head dim 256) and global (512, v from k) layers, androundalonefor the 12B assistant's query-only rows.
Receipt
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 512global 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).tf-cuda-test qk-rows: MLX's rows with the unit v norm at 12B's and 31B's sliding (256, RoPE frominv_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/sinfwithin 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.
v_normalone), q and k are bit-equal totf_qk_rows' andtf_qk_rows_w's.gamma(5 + dim / 32) / 2 + 4 * 2^-24of float64's (the warp tree and thewarps in order, then four fp32 steps);
x * inv, the RoPE products and their sum each add 2^-24; the products bya bf16 weight of a bf16 value are exact, so each output is float64's at the rounding points evaluated at the ends.
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).zig/tests/cuda/contract_qk_round.zig):tf_qk_rows_roundat 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_onceat 512 with RoPE from a table; each at 1 and 300 rows and the 16-row tile's edges (5 shapes).
infinities as float64's).
rounding points), bf16 sums stay within it, so the spec states both (
sums.f16 = 40,sums.bf16 = 0).and without v's unit norm, each red at gate 3.
tf-cuda-test kernel-contract-limitsover 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.SUMMARYline aremain's own (records 1.1.1 lists without a compiled usage yet);this commit adds none.
cuobjdump -sassoftf_qk_rowsandtf_qk_rows_wis identical tomain's on sm_80, 86,89, 120 and 121 (10 of 10), and their launches pass the same arguments.
zig build testfrom 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-rowspass.zig fmt --checkclean.lean_check.py: 444 findings, ason
main.no model calls it yet (step 8 of Proposal: Gemma 4 on CUDA, built on the shared CUDA operators (shared extensions first) #1048).
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Noserving path changes.)
SUMMARYline is in the receipt.