Repository navigation
Conversation
EmbedRows.affine reads MLX's affine tables at 2, 3, 4, 5, 6 or 8 bits (groups of 32, 64 or 128) and, given a bf16 scale, multiplies each dequantized bf16 value by it and rounds again: bf16(bf16(q * s + b) * scale), Gemma 4's embedding times bf16(sqrt(hidden)). With 4 bits, no scale and groups of 32 or 64 it is affine4Groups itself, and the existing kernels' code is unchanged. The 4-bit rows and the new kernel at each width are registered with the kernel contract: every output bit-equal to float64's value at the rounding points, and two controls, one without the bf16 rounding before the scale and one folding the scale into the group's scale and bias.
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 embed_rows extension is included in 1.1.2 (https://github.com/ashhart/TensorFold/releases/tag/v1.1.2), with your original authorship retained. It reads MLX affine tables at 2, 3, 4, 5, 6 or 8 bits with an optional bf16 scale. Closing this as carried, and thank you. It landed as 36caa42 and 42cb101. - 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 3 of #1048 (beside #1056 and #1068; the rounding points are options on the shared operator, off by default, as agreed there):
embed_rowsgets what Gemma 4's embedding needs, as one shared option of the MLX affine rows. Nothing calls it yet.EmbedRows.affine(ids, w, scales, biases, out, rows, d, .{ .bits, .group, .scale }):bits: MLX's affine codes at 2, 3, 4, 5, 6 or 8 bits, in MLX's little-endian words (a code may cross a word);group: 32, 64 or 128 values a scale and bias;bf16(q * s + b)(the product is exact in fp32, so fma or not is the same);scale: a bf16 value each row is multiplied by in fp32 and rounded to bf16 again,bf16(bf16(q * s + b) * scale),as
gemma4/glue.metal'sgemma_embedcomputes the embedding times MLX's weak-typedbf16(sqrt(hidden)). A scalethat is not a bf16 value (or not finite) is refused.
affineisaffine4Groups: the same kernel and the same launch.tf_embed_rows_affine, fromzig/kernels/cuda/embed_affine.cuh(it reads codes withaffine_codes.cuh'scode_at, asaffine8.cudoes), whosekeepbit is the rounding point (a constant in theserved kernel).
embed_rows.cu's existing code is unchanged.core/embed_ref.zig: MLX's code unpacking and the row value at its rounding points (the fp32 sum, its bf16, thefp32 product, its bf16);
bf16_round.zigas 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
.{ .bits = 4, .scale = 62 }on the 12B 4-bit checkpoint (bf16(sqrt(3840))), 73.5 on the 31B(
bf16(sqrt(5376))), and.bits = 8on the oQ8e checkpoints.Receipt
d1018b31(1.1.1) plus this commit; one DGX Spark (GB10, sm_121, CUDA 13.0, nvcc 13.0.88).No checkpoint needed; widths from
families/gemma4/config.zig(12B hidden 3,840, 31B 5,376). The scales' andbiases' ranges in the tests follow
mlx-community/gemma-4-12B-it-4bit'sembed_tokens(scales -0.18 to 0.32,biases -4.1 to 2.7).
tf-cuda-test embed-rows: every width (2, 3, 4, 5, 6, 8 bits) at 3,840 and 5,376 with their scales, at 3,840without one, at the blocks' edges 64, 1,984, 2,048 and 2,112, and with groups of 32 (scaled) and 128, plus 4-bit
groups of 32 at 2,560; each at 1, 3, 17 and 64 rows. 8,347,648 outputs bit-equal to float64's value at the
rounding points; every smaller row count's bits equal the 64-row call's; scale 1 gives
tf_embed_rows_affine4'sbytes; the refusals (7 bits, 1 bit, a group of 48, an unrounded or infinite scale, an unaligned output) hold.
is rounded once to fp32; a bf16 value times a bf16 scale is exact in fp32 too, so every output is float64's value
at those points, bit for bit.
tf-cuda-test norm-rows(the existing 4-bit rows and copies) passes.docs/kernel-contract.md),tf-cuda-test kernel-contract(built-Dnvcc, ReleaseFast) on the GB10:SUMMARY 92 kernels: 92 pass gates 1-4, 0 fail, 0 known findings; 9 controls: 9 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).main's: 1.1.1's records that are zero or stale (tf_q35_*,tree_roundand others).This change adds none; it leaves that count as it is.
zig/tests/cuda/contract_embed.zig):tf_embed_rows_affine4(4 bits, as served today) andtf_embed_rows_affineat each width with its scale, each at Gemma 4 12B's and 31B's widths and the 2,048-valueblock edges (7 shapes each).
where float64's is NaN; infinities as float64's), with one group in 16 far past the checkpoint's ranges. Gate 4 is
N/A (no sums).
tf_ctl_embed_affinerun without the dequantized value's bf16 rounding, and with the scale folded intothe group's scale and bias (
q (s scale) + b scale, rounded once): each red at gate 3 on its first shape.tf_embed_rows_affine(256 threads) recorded fromtf-cuda-test kernel-contract-limitsover fatbins forsm_80, 86, 89, 120 and 121 (38-40 registers, no shared memory): its record current.
cuobjdump -sassoftf_embed_rows_affine4,tf_embed_rows_bf16andtf_embed_rows_bf16_copiesis identical tomain's on sm_80, 86, 89, 120 and 121 (15 of 15), and theirlaunchers are untouched.
zig build testfrom an empty cache: 213/213 steps, 2,965 of 2,979 tests passed, 14 skipped (main: 211/211, 2,959 of 2,973).tf-cuda-test embed-rowsandnorm-rowspass.zig fmt --checkclean.lean_check.py: 444 findings, as onmain.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.