Skip to content

gemma4: the E-series per-layer inputs on the host at MLX's bf16 steps, checked against a float64 reference - #1402

Closed
Mirrdhyn wants to merge 3 commits into
ashhart:mainfrom
hygurlabs:e4b-family-ref
Closed

Mirrdhyn wants to merge 3 commits into
ashhart:mainfrom
hygurlabs:e4b-family-ref

Conversation

@Mirrdhyn

@Mirrdhyn Mirrdhyn commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Part of #1222, on current main (#1400 and #1401 have landed).

layer_input.zig: the per-layer inputs (model projection, scale, norm, sum with the token table row, mix) and each layer's per-layer block (gate with GELU, projection, post norm, residual and layer scalar), on the host, following MLX's arithmetic at one row: the bf16 gemv's tree, rms_norm's two simd_sums with a correctly rounded rsqrt, GELU flushing subnormals as the GPU does, and the weak scalars rounded to bf16 before the product. cuda_layer_input.zig is its CUDA-build twin (the twin test covers it), so a CUDA test can import it.

The oracle

tools/zig/gemma4_e4b_layer_input_oracle.py, pinned to MLX 0.32.4 and mlx-lm 0.32.2 (Metal), runs MLX at the E4B's shapes (2560 hidden, 256 inputs) on seeded inputs and weights, so no checkpoint weights are stored. The fixture tools/zig/fixtures/e4b_layer_input_mlx.json (14 KB) holds the SHA-256 of MLX's bf16 output at every step, for 3 layers by 4 tokens, plus the GELU table over all finite bf16 inputs. layer_input_mlx_test.zig chains the host's steps and compares each step's print with MLX's: all 13 steps equal, inputs() and apply() end to end too. On a mismatch it names the first token, layer and step that differ.

The float64 reference in layer_input.zig's tests is an internal consistency check only; the MLX prints are the oracle.

Bound: with more than one row MLX takes another kernel (gemv_wide or the GEMM), whose bf16 outputs differ from the one-row call on at most 0.05% of the values (M = 2 to 64 measured). The host does not reproduce that path. Equality is measured on an M4 Max with MLX 0.32.4.

Receipt

  • On this branch (main 7e43ebe plus the three commits): test 4770/4796 (26 skipped), test-gemma4-config 108/109 (1 skipped: the optional raw-output file is not in the tree), test-golden passes; zig fmt clean; lean_check.py 428, as on main.

@ashhart

ashhart commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Review against main e9b69ed. #1400 and #1401 are on our devices now; this one needs its oracle settled first.

The host dot accumulates in FP32 left to right, and the test reference repeats the same proposed BF16 steps in FP64 (layer_input.zig:19, :66), with tolerances on a 32-hidden/16-input synthetic shape. That shows internal consistency, not equality with MLX. Your own six-token receipt has 38/42 input layers and 22/42 blocks bit-identical, so the rest differ.

What would settle it: reproducible inputs, weights and MLX outputs pinned to the MLX version and operation, at the real E4B dimensions (256 inputs, 2560 hidden), checked step by step: the RMS intermediate rounding, the projection accumulation, GELU and the scalar steps (layer_input.zig:25, :33, :43, :57). Then either match MLX's actual arithmetic bit for bit, or state the bound explicitly and label it that way. The test reference's scratch (64 and 128 values) also can't hold the real shape (layer_input.zig:110).

E4B stays refused and no served GPU operator is added here, so nothing regresses meanwhile. Thank you for the candid receipt; it made this easy to pin down.

@Mirrdhyn

Copy link
Copy Markdown
Contributor Author

Thanks, agreed on all points. Two commits on top:

  • 1d6843aae adds tools/zig/gemma4_e4b_layer_input_oracle.py, pinned to MLX 0.32.4 and mlx-lm 0.32.2 (Metal), at the E4B's shapes (2560 hidden, 256 inputs). Inputs and weights come from a seeded generator (splitmix64), so no checkpoint weights are stored. The fixture tools/zig/fixtures/e4b_layer_input_mlx.json (14 KB) holds the SHA-256 of MLX's bf16 output at every step, for 3 layers by 4 tokens, plus the GELU table over all finite bf16 inputs, with the versions, device and operations.
  • b7d0bbf72 makes the host steps follow MLX's actual arithmetic at one row, from its sources: the bf16 matmul is the gemv's tree (32 lanes of 4-value fp32 chains per 128, then shuffle-down, gemv.h:175,206), rms_norm is its two simd_sums with a correctly rounded rsqrt (rms_norm.metal:36-61), and GELU flushes subnormals as the GPU does, keeping the sign of zero. The weak scalars are rounded to bf16 before the product, as MLX does.

The new test (layer_input_mlx_test.zig) chains the host's own steps and compares each step's print with MLX's: all 13 steps equal, inputs() and apply() end to end too. On a mismatch it names the first token, layer and step that differ, and it skips cleanly without the fixture.

--full also writes MLX's raw outputs (432 KB), which the test then uses to feed each step MLX's previous output and report the exact difference. I left that file out of the PR for its size; I can add it if you'd rather have it in the tree.

The bound, labelled in the file header: with more than one row MLX takes another kernel (gemv_wide, or the GEMM: matmul.cpp:1458,1623), and its bf16 outputs differ from the one-row call on at most 0.05% of the values (M = 2, 4, 5, 8, 16 and 64 measured). The host does not reproduce that path. Equality is measured on an M4 Max with MLX 0.32.4 only, since the kernel choice depends on shape and GPU generation. The float64 reference now holds the real shape (2560 and 256), and is only an internal consistency check; the MLX test is the oracle.

Tests: test, test-golden, test-gemma4-config on a GB10 host and an M4 Max (61 passed, 1 skipped without the raw file, on the GB10), plus test-cuda-host and test-gemma4-cuda-host on the GB10; zig fmt clean; lean_check.py unchanged (433).

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thank you, this settles the oracle question. It is what we asked for:

  • MLX 0.32.4 pinned at the E4B's real shapes;
  • every step hashed;
  • the host following MLX's one-row gemv and rms_norm trees;
  • the bound for wider calls stated plainly.

The host tests pass here on the branch rebased onto main.

Three small things:

  1. Please rebase onto current main. gemma4: admit the E4B geometry in the family config; each backend refuses it until it computes it #1400 and gemma4: the E4B tensor plan, with BF16 and 8-bit per-module overrides #1401 have landed, and the branch still carries their earlier commits.
    That's why GitHub shows conflicts in gemma4/cuda.zig and gemma4_config_test.zig. Your three new commits apply
    cleanly on top.
  2. Please update the description. It still says this stacks on gemma4: the E4B tensor plan, with BF16 and 8-bit per-module overrides #1401, and it calls the float64 reference the oracle;
    the MLX prints are the oracle now.
  3. There's no need to add the 432 KB .bin; the prints are enough.

We'll run the oracle on our M3 and M5 Ultras to see whether MLX's one-row prints change by chip. If they do, we'll ask
you to name the chips in the header's bound. Then this is ready.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @Mirrdhyn, all three are done. The branch sits on current main with only your three commits. The description names the MLX prints as the oracle and the float64 reference as a consistency check. The .bin stays out of the tree.

We merged it onto main again. fmt and lean are clean, and test-gemma4-config, test-gemma4-cuda-host and the CUDA host cross-compile pass. The prints test is a real oracle. When we swapped dot's shuffle-down tree for a plain sum, it failed and named token 0, layer 1, "back projection".

One small thing: tools/zig/fixtures/README.md:32 says the prints test is in layer_input.zig. It's in layer_input_mlx_test.zig.

Next we'll run the oracle on an M5 Ultra with MLX 0.32.4 and compare its prints with the committed fixture. If they differ from your M4 Max's, we'll ask you to name the chips in the header's bound. Otherwise this is ready.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Landed on main with your authorship (785b865, 42a4348, 57722ac), plus a small commit of ours fixing the README's test file name. Our M5 Ultra gives exactly your M4 Max's prints with MLX 0.32.4 (0 of the digests differ), so the README now says so. Thanks @Mirrdhyn.

@ashhart ashhart closed this Oct 27, 2026
@ashhart ashhart added the landed Landed on main by the maintainers; the commits keep the contributor's authorship label Oct 28, 2026
@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

This is in TensorFold 1.1.8, released today. Thank you, @Mirrdhyn!

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