Skip to content

gemma4: the E4B tensor plan, with BF16 and 8-bit per-module overrides - #1401

Closed
Mirrdhyn wants to merge 2 commits into
ashhart:mainfrom
hygurlabs:e4b-family-loader
Closed

Mirrdhyn wants to merge 2 commits into
ashhart:mainfrom
hygurlabs:e4b-family-loader

Conversation

@Mirrdhyn

@Mirrdhyn Mirrdhyn commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Part of #1222, stacked on #1400 (only the last commit is new).

  • plan.zig (twin cuda_plan.zig): the names, shapes and widths a config's geometry stores, and the ones it leaves unread. For E4B: the per-layer token table, model projection and norm, each layer's per-layer gate, projection and post norm, v_proj on global layers, and no k_proj, v_proj or k_norm read on the 18 shared layers (transformers skips them too: modeling_gemma4.py, the shared layers take the owner's cache). check() refuses a missing tensor or a wrong shape by name. No loader calls it yet: each backend does when it lifts its refusal.
  • widths.zig (twin cuda_widths.zig) reads mlx-lm's per-module overrides from quantization, as agreed in Gemma 4 E4B (NVIDIA Nemotron 3.5 Bolt E4B): per-layer inputs and shared KV, Intel Arc first #1222: a per-layer input module set to false stays BF16, a module may carry its own bits; the global layers' v_proj takes its own entry. Only the per-layer input modules may be BF16, and one module cannot be named twice under the two spellings.
  • Geometry.cacheLayers(): how many layers own a cache, so a backend sizes and snapshots only those.

The twin test in cuda.zig now also covers plan.zig / cuda_plan.zig.

Receipt

Same stack receipt as #1400 (exactness, speeds, kernel contract and suites, CUDA and Metal). Specific to this PR: the E4B checkpoint written by the converter (#1403) with BF16 per-layer linears passes check() on all 1,185 tensors; the 8-bit attention table and the dense 12B and 31B geometries (no per-layer input, no global v_proj) are covered by the tests above.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Landed on main as 91dbe98, in 1.1.7, with #1400. Gemma 4 31B on an M5 Ultra and a GB10 and Gemma 4 12B on an M3 Ultra give the same tokens as main, with prefill and decode unchanged (within 0.2%). Thank you!

@ashhart ashhart closed this Oct 27, 2026
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