Skip to content

gemma4 CUDA: Gemma 4 E4B (NVIDIA Nemotron 3.5 Bolt E4B), per-layer inputs and shared KV - #2230

Merged
ashhart merged 9 commits into
ashhart:mainfrom
jschmied:e4b-cuda-main
Nov 5, 2026
Merged

ashhart merged 9 commits into
ashhart:mainfrom
jschmied:e4b-cuda-main

Conversation

@jschmied

@jschmied jschmied commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The CUDA half of #1222, on the Gemma 4 CUDA family:

  • Per-layer inputs. Once per forward after the embedding: per_layer_model_projection through the existing
    projections, the token table gathered by embed_rows, and one launch in Gemma's own image, tf_gemma4_ple_rows in gemma4_ple.cu, for the scale,
    the 256-wide norm, the sum with the table row and the mix. Per layer after the MLP: the gate on the residual, GeGLU
    with the layer's slice, the projection, then one addRmsTail with the post norm and the layer scalar. The steps are
    layer_input.zig's.

  • Shared KV. Layers 24-41 alias the caches of 22 and 23, compute q only and write nothing. Global layers load their
    own v_proj; the window comes from the geometry (512). The pool, openBytes and cuda_snap.zig count and copy the
    24 owning layers only. TF_KV_SHARE still refuses shared layers.

  • Admission. load admits the E-series with affine per-layer linears and refuses BF16 ones by name.

  • Issue: Refs Gemma 4 E4B (NVIDIA Nemotron 3.5 Bolt E4B): per-layer inputs and shared KV, Intel Arc first #1222 (the CUDA half; the Metal half stays with @Mirrdhyn).

  • Stacked on: main e166f3f79, eight commits (393f33423 to e413a3804).

  • atc overlap --json: no conflicts against open pull requests.

The kernel contract gates tf_gemma4_ple_rows (tests/cuda/contract_ple.zig): float64 within a derived bound and the host steps' bits, non-finite, repeats, fp32 sums, two planted controls. kernel-contract takes words to run one family's kernels.

Checks on this head

  • Head commit: e413a3804.
  • Lean checker on the base and on this head: 0 new.
  • Tests run: zig build test RC 0, 0 errors; the no-GPU CI set RC 0; git diff --check clean. On a GB10: tf-cuda-test gemma4-ple, gemma4-e4b-force and gemma4-pages (12B and 31B) pass. Filtered contract: (filtered) SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems. 12B and 31B, main against branch (fresh server an arm, page cache dropped): served greedy token_sha equal on 3 prompts each, forced logits byte-equal on every row; decode 12B main 31.87 tok/s median (spread 0.63 over 6 runs), branch 31.92 (0.64); 31B main 12.71 (0.14), branch 12.71 (0.13). Per round (AB, then BA): 12B branch both (+0.05, +0.04), 31B main then branch (-0.01, +0.03): no difference.
  • Not run, and why: other CUDA chips (only a GB10 here); BF16 per-layer linears (refused by name; a follow-up).

Receipt

  • Environment: this head; Zig 0.17.0, nvcc 13.0; GB10 (sm_121). Checkpoint nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-BF16 at 49843eed8db1, every file checked against the publisher's hashes, converted with mlx_lm.convert -q --q-bits 4 --q-group-size 64 (mlx 0.32.4, mlx-lm 0.32.2).
  • tf_gemma4_ple_rows: 1 to 512 rows of 42 parts bit-equal to the host steps (tf-cuda-test gemma4-ple).
  • Forced logits (tf-cuda-test gemma4-e4b-force, gemma4_e4b_compare.py), 5 prompts, 239 positions:
    • against transformers on the same 4/64 weights: 236 top-1, KL mean 0.0013, max 0.046; the 3 misses have margins of at most 0.25 on both sides (two of them ties);
    • against BF16: 218 top-1, KL mean 0.202, the same as transformers on the 4/64 weights (0.199).
  • Served: greedy replies repeat; 2 and 4 streams equal their replies alone; a 2,943-token prompt resumed from its kept
    prefix (2,940 cached) equals the fresh reply.
  • One stream decodes at 67 tok/s; 2 and 4 streams reach 90 and 157 tok/s together. TTFT is 33 ms for a short chat,
    604 ms for 2,943 tokens cold and 39 ms resumed.
  • The 12B and 31B keep their paged-vs-linear replies and logits (gemma4-pages).
  • Licence of new weights: n/a (no weights shipped; the checkpoint is NVIDIA's release).

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. On your two questions: act_rows holds row operations any family can call (SwiGLU, GeGLU, gates, softcap, group sums). If tf_ple_rows is that kind of row op, with no Gemma geometry baked in, put it there with its contract record. If it knows Gemma's shapes, give it Gemma's own image, as Qwen4's per-layer inputs have qwen4_ple.cu. And please leave the BF16 per-layer linears for a follow-up, so this PR keeps one gate.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

A few things before this leaves draft, @jschmied. Shared layers that only project q and read their owner's cache through one alias, used by both the paged and unpaged builds, is a tidy way to do it.

  1. tf_ple_rows has Gemma's shape in it: the fixed 256-wide row and the scale, norm, table and mix steps. So, per my earlier answer, please move it out of act_rows into Gemma's own image, as qwen4_ple.cu is for Qwen4, with its contract record there.
  2. The contract gate and planted control you mention, with a float64 reference beside the host-steps check.
  3. The three top-1 misses: please list each one's position, our top two and the reference's, with their margins.
  4. Main against branch for the 12B and the 31B on the GB10: tokens equal and decode tok/s. This touches their layer loop (the v source, the window, the cache loops), so the paged-against-linear check on the branch isn't enough on its own.
  5. A CHANGELOG line. One small thing besides: the comment in cuda_checkpoint_cache_host.zig names a loadESeries that doesn't exist.

@jschmied

jschmied commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All five points are in e413a3804; numbers below.

  1. Own image. tf_ple_rows left act_rows. It is now tf_gemma4_ple_rows in zig/kernels/cuda/gemma4_ple.cu, its own image as qwen4_ple.cu is Qwen4's, with the steps in gemma4_ple.cuh (shared with the planted control). The family loads it through gemma4/cuda_ple.zig; the record names image gemma4_ple, 64 threads, 26 registers and 8 shared bytes on sm_121 (24 on sm_80), re-measured from the new image. act_rows.cu and act_rows.zig are main's again.

  2. Contract gate. tests/cuda/contract_ple.zig, gates 1 to 4:

    • non-finite: NaN and both infinities in the projected and the table rows, classes equal to float64's;
    • repeat: five runs byte-identical, guards and inputs untouched;
    • shapes: every output within a derived float64 bound (a bf16 point decided the other way moves y or t·16 by an ulp, which can be most of an output when the sum cancels), and beside it, the bits equal layer_input.zig's host steps;
    • sums: integer squares, exact in fp32; fp16 would miss 26 times the bound, so f16 is 20 with the reason in the spec.

    Two planted controls, the norm skipped and the token scale dropped, both go red at shapes (17.6 and 23.3 times the bound). The filtered run on the GB10 (kernel-contract gemma4 Gemma act_rows "cuda act ", words added so a family can run its kernels alone):
    (filtered) SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems.

  3. The three top-1 misses against transformers on the same 4/64 weights (logits after the soft cap):

    prompt, step (position) ours: top two reference: top two margins (ours, ref)
    chat_short, 6 (25) a 21.125, Rayleigh 21.0 Rayleigh 21.25, a 21.0 0.125, 0.25
    french, 35 (75) . 24.75, , 24.625 , 24.75, . 24.5 0.125, 0.25
    french, 58 (98) ( 21.875, actif 21.875 différent 21.875, ( 21.875 0, 0

    Every margin is at most two bf16 steps at that size (0.125). In the third, both sides have a tie at the top.

  4. 12B and 31B, main against branch. Fresh server per arm, page cache dropped before each start:

    • served greedy token_sha (128 tokens max, 3 prompts) equal on both: 12B 3671d17d9e555410, bee4fef01cda9ba5, 8cd311483fa0ad9f; 31B bba0880ed1576885, 95948f8a0dfc2a37, 2b643acc6240791a;
    • forced logits (gemma4-e4b-force, prefill in 512-row chunks, then each forced token decoded): every row byte-equal, 3 prompts x 2 models, 183 rows each;
    • decode, ABBA: 12B main 31.87 tok/s median (spread 0.63 over 6 runs), branch 31.92 (0.64); 31B main 12.71 (0.14), branch 12.71 (0.13). Per round (AB, then BA): 12B branch both (+0.05, +0.04), 31B main then branch (-0.01, +0.03): no difference.
  5. Changelog and comment. There's a line under Unreleased. The load-cache check's comment now says load, and loadESeries no longer exists.

BF16 per-layer linears are left for a follow-up, as you said.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, all five are in, and the margins table makes the three misses easy to judge. Main has moved under it, so it needs a rebase. Then it can come out of draft and land after the cut.

@jschmied
jschmied marked this pull request as ready for review November 5, 2026 15:42
@jschmied

jschmied commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main cde4fe7d9; head 102d43146, same eight commits. The only conflict was the CHANGELOG's Unreleased list (main had moved the LFM3 line), kept as main's plus the Gemma line.

Re-run on the rebased head, GB10:

  • Lean 0 new, zig build test, the no-GPU CI set, git diff --check and zig fmt --check pass.
  • tf-cuda-test kernel-contract ple: 3 kernels pass gates 1-4, both planted controls red, 0 limit problems.
  • E4B served greedy (--parallel 4 --context 8192), token hashes equal to the pre-rebase ones: chat_short 7bc8b9d3863239a2, code a4880b8bbf9097ee, french 11b146b971d782d8; concurrent equals alone at 1, 2 and 4; resumed equals fresh (f76855d65b658684).

Out of draft now.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The rebase is clean, the E4B hashes match the pre-rebase ones, and the 12B and 31B are level against main with byte-equal forced logits, so all five points hold. Ready to land after the 1.2.3 cut.

jschmied and others added 9 commits November 5, 2026 18:22
…nputs and blocks, shared layers on their owner's cache (ashhart#1222)

Layers 24-41 alias the caches of 22 and 23, compute q only and write nothing; the pool, openBytes and the
snapshots count and copy the 24 owning layers. Global layers load their own v_proj, the sliding window comes
from the geometry. load still refuses the E-series; loadESeries admits it with affine per-layer linears.
…layer linears (ashhart#1222)

Receipts on NVIDIA-Nemotron-3.5-Bolt-E4B (mlx_lm.convert 4/64): forced logits against transformers on the
same weights, 236 of 239 top-1 (3 near ties), KL mean 0.0013; greedy replies repeat, 2 and 4 streams equal
alone, a resumed prompt equals the fresh one. Pages alias shared layers; TF_KV_SHARE still refuses them.
…ted by the contract (ashhart#1222)

tf_gemma4_ple_rows leaves act_rows. Its contract test checks a float64 reference within a derived bound and
the host steps' bits, plants two controls (no norm, no token scale), and kernel-contract takes words that
filter the kernels and their live limits.
…ract_after.zig, gemma4_ple's registration in its own file
@ashhart
ashhart merged commit 78c7a83 into ashhart:main Nov 5, 2026
0 of 7 checks passed
@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit that referenced this pull request Nov 5, 2026
ashhart added a commit that referenced this pull request Nov 5, 2026
ashhart added a commit that referenced this pull request Nov 7, 2026
Step: test-gradient-serving. Old pins: e166f3f. New base: 063d881 (origin/main).
Sets the hash of the 46 changed entries in gradient-serving.tsv to 063d881's bytes; paths,
modes (all raw) and note columns are unchanged. pocket-serving-approved.tsv needs no edit: its
entries for these files have prefix 0, so they never read the manifest hash. No code changes;
1/1 test passes.

Recorded, as merged and reviewed:
- #1784 (cf63ff6): Flash Next at --tp 2 splits dense output columns with exact FP32 gathers.
- #1784 (cde4fe7): the BF16 prompt tile folds a pinned (cut) weight's whole K-slice order.
- #1942 (15f118a): a shared round's DeltaNet steps run as one launch over its lanes.
- #1792 (52b9f90): the whole shared expert's down writes straight into its output slot.
- #2160 (a54748a): beside another open stream a greedy chain stops at 0.6 a draft; wide
  rounds draft 6 deep.
- #2246 (4294752): at --parallel 1 on one GPU a prompt pass takes 8,192 rows.
- #2214 (e4d7f13): kept prompt states with drafts across ranks.
- #2230 (1f2c993): Gemma 4's PLE kernel in its own image; controls and blob constant.
- #2103 (a3962b4, 194115d): synchronous downloads stage through a pinned bounce, on
  integrated devices only, and the bounce counts in memory use.
- #2289 (e9ee738): the remaining synchronous readbacks use that staging; close() releases it.
- #2127 (b2c277a): the lane host's KV ledger counts the paged pool's promises.
- 4fb2f65, 60d80da, 8cc6ac4, 36a87df, 41ff25d, efdf28f, fcb1529, f3bcb78,
  ecef17d, 2ebc3c2, 42d3461, a86c31c, 1913a46, 80218cd: comments, verbatim moves,
  test-block imports, exports, and opt-in or off-by-default paths only.
@ashhart

ashhart commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied! This is in TensorFold 1.2.4, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.4

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.

2 participants