Skip to content

cuda experts: an mxfp4 format, stored E2M1 codes and E8M0 scales decoded to bf16 in registers on the bf16 experts' chain - #5042

Merged
ashhart merged 3 commits into
ashhart:mainfrom
Bizuayeu:pr/mxfp4-experts
Nov 26, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
Bizuayeu:pr/mxfp4-experts

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Core's grouped experts on CUDA get an mxfp4 format: E2M1 codes (two a byte, low nibble first) and one E8M0 scale per 32 inputs, read from device memory as a checkpoint stores them ([E][N][K/2] and [E][N][K/32], no repack). Each weight is decoded to bf16 in registers, code × 2^(scale − 127), exactly, and then runs the bf16 experts' own chain (experts_bf16.cu: two m16n8k16 mma steps a 32-input group, in input order, one fp32 accumulator a column), so on the same inputs the output equals the bf16 experts' on the host-decoded weights byte for byte, in the decode and the prompt forms. This adds no rounding point: the decode is exact, and the sums and epilogues are the bf16 experts'.

Checks on this head

  • The GB10 run's identities and results (the lines themselves are in "The GB10 run's lines", after the kernel contract):
    • The runner that ran every GPU line below: sha256 cbc88516d05a379ee5e434f05f35ef6aa887f75ad0c0c3e6b23200d79d61bf05, 519,275,064 bytes. It embeds experts_mxfp4.fatbin, sha256 b3409ffd5b25577351c884c44194ba5bd897a06ea52c5798961b2724d370289d, and experts_bf16.fatbin, sha256 5078708d611a06b3fc123cb4c562d5d650247652120c804785eb83e79251336f (main a3c3e5cef3's bytes): each file in zig-out/fatbin, which the limits read, is found byte for byte once inside the runner.
    • tf-cuda-test mxfp4-experts contract, exit 0: SUMMARY mxfp4 experts: 4 kernels, 4 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed
    • tf-cuda-test bf16-experts contract, exit 0: SUMMARY bf16 experts: 6 kernels, 6 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed
    • tf-cuda-test kernel-contract-limits zig-out/fatbin: the four new kernels' 20 records (sm_80, 86, 89, 120 and 121) are current; exit 1 with 154 problems, the same 154 as on main, none of them this pull request's.
    • Not kept by the run: the per-kernel gate lines, which the SUMMARY lines count.
  • Head commit: 31f591798a, whose tree is 83e0c292f6's but for the CHANGELOG.md line, so the checks on 83e0c292f6 stand for it. 83e0c292f6, on main 67ae36c4dc, is 99cfc922da (on a3c3e5cef3) rebased three times, the same diff but for the pins and the image-list line above. On 83e0c292f6 itself: in WSL 2, git diff --check clean, zig fmt --check and zig ast-check clean on the 14 changed Zig files, zig build -Dfixed-cpu test-gradient-serving 1 of 1 test and compile-cuda-tests 12 of 12 steps; tools/pr_check.py --base origin/main --pr 5042, 0 errors. The runs below are on the old base. 99cfc922da differs from 3ed51490cb, the head the runs below were taken for, only in two comments, each now on a line of its own (the down field's layout note and a test's scale range); no code changed, so the runs were not taken again. The GPU runs and the GB10's host runs below are on a tree that differs from 3ed51490cb only in text: it has no CHANGELOG.md line, its two gradient-serving.tsv notes read #TBD where this head names this pull request, and zig/tests/cuda/main_usage.zig has no line for mxfp4-experts. The runs in WSL 2 are on c287b90d1b, 3ed51490cb but for that usage line, which only the runner's help text prints.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 152 problems on main 67ae36c4dc and 152 on this head, 0 new (164 and 164 on a3c3e5cef3 before the rebases) (tools/zig/lean_new.py). The 152 are inherited from main. experts.zig grows from 591 to 595 lines; the new files have 20 to 300.
  • Tests run, with the command and pass, fail and skip counts (Zig 0.17.0, nvcc 13.0.88, one GB10, sm_121):
    • tf-cuda-test mxfp4-experts: 37 token rows of 3 picks over 6 experts (111 pairs), 256 outputs from 96 inputs, random codes over all 16 nibbles and scales 2^-9 to 2^-3: both downs give the bf16 experts' bytes on the host-decoded weights (tf_bf16_experts_pack, then the bf16 entries), in the decode form (items of 16) and the prompt form (items of 64); the fp32 down is within 9.4e-8 of float64 relative to its terms' magnitude; the prompt form's bytes equal the decode form's, and rows 0, 17 and 36 alone give the bytes they gave beside the others.
    • On the GB10 (aarch64 Linux), the tree named above and main a3c3e5cef3, each built from an empty global cache with zig build compile-cuda-tests fatbins kernel-contract-fatbins -Dnvcc="$NVCC" -Dsm=80,86,89,120,121 ($NVCC is nvcc 13.0.88): exit 0 both; the GPU runs and the limits use these builds. On the same host, that tree: zig build -Dfixed-cpu test-cuda-host --summary all: 503 of 503 steps succeeded, 9,521 of 9,565 tests passed, 44 skipped (the new host test refuses up, unaligned shapes and missing scales, and checks checkScales at 0, 1, 2, 113, 122, 252, 253, 254 and 255); test-kernel-contract: 43 of 43 steps, 323 of 323 tests passed; test-gradient-serving: 4 of 4 steps, 1 of 1 test passed. In WSL 2 (x86-64 Ubuntu 24.04), c287b90d1b: zig build -Dfixed-cpu test-gradient-serving --summary all: 4 of 4 steps, 1 of 1 test passed; python3 -m unittest discover -s tools/zig -p host_tests_test.py: 13 tests OK; python -m pytest -q -rs -p no:cacheprovider from the root, with pytest 9.2.0 and PyYAML 6.0.4 (what CI's job installs) and a clang++ on PATH: 820 passed, 106 skipped, 826 subtests passed, the same counts and skip reasons as main a3c3e5cef3.
  • CI: green on 31f591798a, with the same jobs and pytest counts as below. On 0d488e8ac3, the head before the third rebase, it was green: fmt, zig, zig-b, zig-windows, zig-macos-dev-tools, pytest (854 passed, 115 skipped, 827 subtests passed), release-paths (ubuntu-24.04), comfyui-moss-installer and both benchmark-clients jobs passed; host-nightly-linux and host-nightly-macos were skipped; the annotations are GitHub's Node.js 20 deprecation warnings and a macOS runner queue notice. 1b37b76e8a, the head before the second rebase, had the same jobs green and the same pytest counts. On 3ed51490cb it was green as well: fmt, zig, zig-b, zig-windows, zig-macos-dev-tools, pytest (822 passed, 105 skipped, 826 subtests passed; the run in WSL 2 above has 820 and 106, and the CI log does not print skip reasons), release-paths (ubuntu-24.04), comfyui-moss-installer and both benchmark-clients jobs passed; host-nightly-linux and host-nightly-macos were skipped. The annotations are GitHub's Node.js 20 deprecation warnings and a macOS runner queue notice.
  • Not run, and why: other GPUs than the GB10 (sm_121) for the gates; the limits records cover sm_80, 86, 89, 120 and 121 as compiled by nvcc 13.0 only (no 13.2 or 13.3 build of these kernels, so no overrides for them); Metal and HIP (no change there).

Receipt

n/a for serving: no checkpoint selects mxfp4 yet, so no served model's tokens or speed change. The receipt is the bit equality above and the contract below.

  • Main against branch: n/a, as above.
  • Boxes it reaches: CUDA grouped experts, when a family loads the format.
  • New switch? None.
  • Environment: TensorFold main a3c3e5cef3 and the tree named under "Checks on this head" (this diff on that base, before the rebase), Zig 0.17.0, nvcc 13.0.88, one GB10 (sm_121).
  • Paths and platforms the change reaches, which I ran and which I could not: Grouped.decode and Grouped.prompt for .mxfp4; I ran sm_121 only.
  • Exactness: the outputs equal the bf16 experts' on the decoded weights byte for byte, as above.
  • Decode and prompt speed: n/a, no served path.
  • Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: tf_mxfp4_experts_down_f32, tf_mxfp4_experts_down_bf16, tf_mxfp4_experts_prompt_down_f32 and tf_mxfp4_experts_prompt_down_bf16 (zig/tests/cuda/contract_experts_mxfp4.zig), new, against float64 on ERNIE 5.5's routed down (2,048 to 2,048, ten pairs a token row, decode windows of 1 and 32 rows, prompt rows of 40 and 72). Rounding points and sum order: the weights' decode to bf16 is exact; each column's sum is one fp32 chain of m16n8k16 mma steps over the inputs in order, the bf16 experts' chain; the bf16 down rounds that sum once. FP64 bound: gate 3's gamma(k/16 + 36) of the terms' magnitude, as the bf16 experts' downs.
  • A planted control for each new kernel, and each one fails as planted: the fp32 down launched one 32-input group short (red at shapes, 705 times its bound at 1×2048×2048), and the fp32 down reading each group's scale from the next group's (red at shapes, 3,394 times its bound).
  • Each backend's SUMMARY line and exit status, and the limits: under "Checks on this head", first, with the images tested (both contracts exit 0; the limits exit 1 with the same 154 stale records of other images as main a3c3e5cef3; the 10 RED lines are the limits tool's own planted controls, the same on main).
  • Chips I could not run: every CUDA GPU but the GB10.
The GB10 run's lines

From the runs and the images named under "Checks on this head". The run kept these lines; the FAIL lines are the planted controls' own.

tf-cuda-test mxfp4-experts, exit 0:

PASS mxfp4 experts: 111 pairs (decode) give bf16 experts' bytes on the decoded weights; fp32 within 9.413271248945051e-8
PASS mxfp4 experts: 111 pairs (prompt) give bf16 experts' bytes on the decoded weights; fp32 within 9.413271248945051e-8
PASS mxfp4 experts: a row alone, beside 36 others and in the prompt form gives the same bytes

tf-cuda-test mxfp4-experts contract, exit 0:

FAIL shapes control: mxfp4 experts fp32 down launched one 32-input group short: at 1x2048x2048 output 0 is -5.847493e0 where float64 gives -7.770101e0: 705.05 of its bound
CONTROL control: mxfp4 experts fp32 down launched one 32-input group short: red at shapes, as planted
FAIL shapes control: mxfp4 experts fp32 down reading each group's scale from the next: at 1x2048x2048 output 0 is -5.291883e0 where float64 gives 3.002197e0: 3394.38 of its bound
CONTROL control: mxfp4 experts fp32 down reading each group's scale from the next: red at shapes, as planted
SUMMARY mxfp4 experts: 4 kernels, 4 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed

tf-cuda-test bf16-experts contract, exit 0:

FAIL shapes control: bf16 experts fp32 down launched one 32-input group short: at 1x2688x1856 output 0 is -3.678868e-1 where float64 gives -3.547289e-1: 25.65 of its bound
CONTROL control: bf16 experts fp32 down launched one 32-input group short: red at shapes, as planted
FAIL sums control: bf16 experts fp32 down summing in bf16: 31692161024.0x over its exact-sum bound at k 1856: not fp32 sums (fp16 would miss 104391566x, bf16 728876828x)
CONTROL control: bf16 experts fp32 down summing in bf16: red at sums, as planted
SUMMARY bf16 experts: 6 kernels, 6 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed

tf-cuda-test kernel-contract-limits zig-out/fatbin, the four new kernels' 20 lines (exit 1 with 154 problems, as on main):

PASS limits sm_80 experts_mxfp4 tf_mxfp4_experts_down_f32: 88 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_86 experts_mxfp4 tf_mxfp4_experts_down_f32: 89 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_89 experts_mxfp4 tf_mxfp4_experts_down_f32: 89 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_120 experts_mxfp4 tf_mxfp4_experts_down_f32: 96 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_121 experts_mxfp4 tf_mxfp4_experts_down_f32: 96 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_80 experts_mxfp4 tf_mxfp4_experts_down_bf16: 88 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_86 experts_mxfp4 tf_mxfp4_experts_down_bf16: 89 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_89 experts_mxfp4 tf_mxfp4_experts_down_bf16: 89 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_120 experts_mxfp4 tf_mxfp4_experts_down_bf16: 96 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_121 experts_mxfp4 tf_mxfp4_experts_down_bf16: 96 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_80 experts_mxfp4 tf_mxfp4_experts_prompt_down_f32: 180 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_86 experts_mxfp4 tf_mxfp4_experts_prompt_down_f32: 180 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_89 experts_mxfp4 tf_mxfp4_experts_prompt_down_f32: 180 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_120 experts_mxfp4 tf_mxfp4_experts_prompt_down_f32: 205 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_121 experts_mxfp4 tf_mxfp4_experts_prompt_down_f32: 205 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_80 experts_mxfp4 tf_mxfp4_experts_prompt_down_bf16: 180 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_86 experts_mxfp4 tf_mxfp4_experts_prompt_down_bf16: 180 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_89 experts_mxfp4 tf_mxfp4_experts_prompt_down_bf16: 180 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_120 experts_mxfp4 tf_mxfp4_experts_prompt_down_bf16: 205 registers, 0 static shared bytes, bound 128 (record current)
PASS limits sm_121 experts_mxfp4 tf_mxfp4_experts_prompt_down_bf16: 205 registers, 0 static shared bytes, bound 128 (record current)

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency (the GPU test and the contract need the new image; the host test needs the new format).
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@Bizuayeu
Bizuayeu marked this pull request as ready for review November 25, 2026 17:24
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu. The admitted code/scale range widens exactly to BF16, and the row loop retains the existing BF16 experts' sum and epilogue order. The new format remains separate from serving, and the head merges cleanly.

Ready for the focused GB10 operator comparison. Please retain the full four-kernel/two-control summary and launch-limit results with the tested image identity; the supplied extract stops before that evidence. The existing byte comparisons remain credited.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Please retain the full four-kernel/two-control summary and launch-limit results with the tested image identity; the supplied extract stops before that evidence.

(#5042 (comment))

Added to the description, with the head unchanged:

  • Under "Checks on this head", a new item on the GB10's runs: the sha256 of the runner and of the two fatbins it embeds, each fatbin found byte for byte inside that runner, both SUMMARY lines with their exit status, and the limits result.
  • After the kernel contract, "The GB10 run's lines": the test, contract and limits lines as the run printed them. The run did not keep the per-kernel gate lines; the SUMMARY lines count them.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu. The source review remains clean: the BF16 decode and accumulation order, unserved scope, and unchanged existing formats all check out.

The retained evidence is still absent from the supplied description. Its Checks text ends mid-command, and although the follow-up says the identities, both SUMMARY lines, and limits result were added, none of those values or lines appears in the supplied text. Please retain the runner and both fatbin SHA-256 identities, both complete SUMMARY lines with their exit codes, and the launch-limit result so the four-kernel/two-control run can be credited. The tested-tree differences you list are text-only and do not affect the kernel comparison.

CHANGES REQUESTED for the retained evidence only; no new source defect found.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

The retained evidence is still absent from the supplied description. Its Checks text ends mid-command

(#5042 (comment))

That command held a placeholder in angle brackets after -Dnvcc=, which may be where the extract stopped. I replaced it with -Dnvcc="$NVCC" and made the evidence the first item of "Checks on this head". The same values, here as well:

  • Runner: sha256 cbc88516d05a379ee5e434f05f35ef6aa887f75ad0c0c3e6b23200d79d61bf05, 519,275,064 bytes, the one that ran every GPU line of the run. It embeds experts_mxfp4.fatbin, sha256 b3409ffd5b25577351c884c44194ba5bd897a06ea52c5798961b2724d370289d, and experts_bf16.fatbin, sha256 5078708d611a06b3fc123cb4c562d5d650247652120c804785eb83e79251336f (main a3c3e5cef3's bytes), each found byte for byte once inside it; the limits read the same files in zig-out/fatbin.
  • tf-cuda-test mxfp4-experts contract, exit 0: SUMMARY mxfp4 experts: 4 kernels, 4 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed
  • tf-cuda-test bf16-experts contract, exit 0: SUMMARY bf16 experts: 6 kernels, 6 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed
  • tf-cuda-test kernel-contract-limits zig-out/fatbin: the four new kernels' 20 records (sm_80, 86, 89, 120 and 121) are current; exit 1 with 154 problems, the same 154 as on main a3c3e5cef3, none of them this pull request's.

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you for retaining the runner and both fatbin identities, both complete contract summaries with exit statuses, and the launch-limit result. That closes the previous evidence request. The stated tested-tree differences are text-only and do not affect the reviewed kernel comparison.

The stored E2M1/E8M0 decode, BF16 accumulation order and unserved scope remain confirmed, and the branch merges cleanly on current main.

Two added or changed comments still exceed 120 columns: zig/src/cuda/experts.zig:28 and zig/tests/cuda/contract_experts_mxfp4.zig:137. Please move or shorten those comments only. Restoring the old down comment unchanged and placing the new MXFP4 layout note on its own short line is sufficient; the scale-range comment can also move to a preceding line. No arithmetic, test input or checkpoint change is needed.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Two added or changed comments still exceed 120 columns: zig/src/cuda/experts.zig:28 and zig/tests/cuda/contract_experts_mxfp4.zig:137.

(#5042 (comment))

Done in 99cfc922da, comments only: the old down comment is back unchanged, with the MXFP4 layout on its own line above it, and the scale-range comment is on the line before its loop. No added comment line is longer than 120 columns now. The description names the new head; the runs were not taken again, since no code changed.

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Rebased onto main 920fe22c97 as 1b37b76e8a: main's f0aa8088d1 (#5100) had re-pinned kernels.zig in gradient-serving.tsv, the one line this pull request also changes. The pin now hashes the merged kernels.zig, with #5100's note kept before this one's. The rebase also fixes experts.zig's pin, which 99cfc922da had left at the file's hash from before the comment change, so its test-gradient-serving would have failed. The rest of the diff is unchanged, and main has not touched the experts kernels, their headers or these tests. On 1b37b76e8a: test-gradient-serving 1 of 1, compile-cuda-tests 12 of 12, fmt and diff-check clean, pr_check 0 errors, lean 164 against 164, and no added conflict with the same 55 open pull requests. The description names the new head and base.

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu.

Both requested comment fixes are confirmed. The new layout and scale notes are short and clear, and the two serving pins match the exact files on this head.

The retained CUDA evidence still covers the unchanged arithmetic: the MXFP4 decode is exact, the existing BF16 accumulation order is preserved, and all focused kernels passed with their planted controls red. No serving checkpoint selects this new format, so existing defaults keep their output bytes.

This head also merges cleanly with current main. SAFE TO LAND. No additional device run is needed.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Main moved after your review: cddc70cc57 re-pinned experts.zig in gradient-serving.tsv (after da9cc61676, which reads TF_NVFP4_DECODE_WARPS through the settings registry in Nvfp4Experts), and that line conflicted with this pull request's pin. Rebased onto 84d9b6ba13 as 0d488e8ac3: the pin hashes the merged experts.zig, with main's note kept before this one's, and nothing else in the diff changed. On 0d488e8ac3: test-gradient-serving 1 of 1, compile-cuda-tests 12 of 12, fmt and diff-check clean, pr_check 0 errors, lean 152 against 152, and no added conflict with the same 55 open pull requests. CI was green on 1b37b76e8a and runs again here; the description names the new head and base.

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Main moved again: #5051 added the topk_rows and sparse_index images to the image list in zig/tests/cuda/contract_limits.zig (a2a965d387) and re-pinned kernels.zig in gradient-serving.tsv (4fac3ce6a6), the two lines this pull request also changes. Rebased onto 67ae36c4dc as 83e0c292f6: the image list has both additions, the kernels.zig pin hashes the merged file with main's note kept before this one's, and nothing else in the diff changed (git range-diff). On 83e0c292f6: test-gradient-serving 1 of 1, compile-cuda-tests 12 of 12, fmt and diff-check clean, pr_check 0 errors, lean 152 against 152, and no added conflict with the same 55 open pull requests. CI was green on 0d488e8ac3 and runs again here; the description names the new head and base.

Bizuayeu and others added 3 commits November 26, 2026 08:58
…ded to bf16 in registers on the bf16 experts' chain

The two downs (fp32 and bf16 sums) in the decode and prompt forms read a checkpoint's MXFP4 experts as stored,
[E][N][K/2] codes low nibble first and [E][N][K/32] scales, decode each weight to bf16 exactly and run
experts_bf16's mma chain, so they give the bf16 experts' bytes on the host-decoded weights. Up is refused;
checkScales takes E8M0 codes 2 to 252, where every E2M1 multiple is a finite, normal bf16. Registered with the
kernel contract against float64 (two controls), with nvcc 13.0's launch records; no served checkpoint selects it.
@ashhart
ashhart merged commit 0102f73 into ashhart:main Nov 26, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @Bizuayeu. 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.

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