Skip to content

cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract - #5301

Merged
ashhart merged 3 commits into
ashhart:mainfrom
Bizuayeu:pr/ernie55-mhc-pre
Nov 28, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
Bizuayeu:pr/ernie55-mhc-pre

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

tf_ernie55_mhc_pre, in a new ernie5_5_mhc image: the mHC pre step ERNIE 5.5 runs before each attention and each FFN, as vLLM-ERNIE's mhc_pre_big_fuse_with_norm_tilelang defines it, one row a block of 256 threads. From a row's 4 bf16 residual streams (4 × 4,096) and fn's 24 float32 rows, it forms the 24 mixes and the streams' sum of squares (a thread's 64 terms in one fma chain, the warp's butterfly, then the 8 warps' partials in order), scales the mixes by ERNIE's mix RMS 1 / (sqrt(mean) + eps), and gives pre (sigmoid(m s0 + b) + hc_eps), post (2 sigmoid(m s1 + b)) and comb (each row's softmax plus hc_eps, then hc_sinkhorn_iters rounds of column and row sums with hc_eps in each divisor). It then collapses the streams, sum_s pre_s x_s from stream 0 in fma, takes the RMSNorm's sum of squares over the unrounded collapse, and writes bf16((bf16(collapse) · rsqrt) · w) with the layer's norm weight, rounded to bf16 once. Every step is an explicit round-to-nearest operation (__fmaf_rn, __fdiv_rn, __fsqrt_rn, expf). Nothing launches it yet, so served output is unchanged.

Why a family kernel and not common/mhc.cuh (#5164): the mix RMS is not the only difference from GLM's pre side. ERNIE's fn is float32 (the shared partial reads bf16), its norm squares the collapse before rounding it (the shared finish squares the rounded collapse), and it rounds the normed value once, after the weight (the shared finish also rounds rinv·c before it). I asked on #4926 (#4926 (comment)) whether you would rather have the shared bodies widened; this pull request leaves common/mhc.cuh and GLM untouched.

One choice differs from the vendor: the mixes are fp32 products of bf16 x and fp32 fn, where the vendor uses DeepGEMM's TF32 GEMM (tf32_hc_prenorm_gemm, split-k), which rounds fn and x to TF32 first. Nothing here is rounded below fp32 before the outputs.

Checks on this head

  • Head commit: 0336d7958d cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract, the restack of this commit onto cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract #5211's ba13f19e08. The GB10 run and pr_check below are on this head; the Windows and WSL runs are on the earlier heads named in them, the same source (mhc.cu took core's warp_sum from common/reduce.cuh on 01f035e6f4, with cubins the same as before at the five SMs in every section of every entry).
  • Lean checker (tools/pr_check.py --base ba13f19e08 --pr 5301, this commit against cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract #5211's head): 93 problems on both, 0 new; 0 errors. The 93 are inherited from main.
  • Tests run, with the command and pass, fail and skip counts (Zig 0.17.0, nvcc 13.0.88):
    • GB10 (sm_121): cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract #5211's head (ba13f19e08), then this head, each in its own worktree with an empty global cache, zig build compile-cuda-tests fatbins kernel-contract-fatbins -Dnvcc=... -Dsm=80,86,89,120,121: 0 errors. On the GPU, tf-cuda-test kernel-contract-limits zig-out/fatbin gives the five tf_ernie55_mhc_pre lines PASS ... (record current) (40 registers and 948 static shared bytes at every SM, bound 256), and its other lines are cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract #5211's head's, line for line (the same 201 stale records, 10 RED and 2 FAIL lines). tf-cuda-test ernie5_5-mhc passes the three outputs' gates 1-4: gate 1, the non-finite and finite outputs in their float64 classes; gate 2, five runs byte-identical; gate 3, 3 shapes within the FP64 bound, the worst at 0.912 of it for the normed rows and 0.001 for post and comb; gate 4, exact sums at k 16,384, where bf16 sums would miss by 6,152x (post) and 1,646x (comb), and for the normed rows fp16 by 128x and bf16 by 0.5x (registered as not provable there: pre is 1/2 under stress, so only the squares' sums reach the output). Both controls are red (below). ernie5_5-index passes cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract #5211's window merge with both its controls red, and kernel-contract ernie5_5 passes the five ERNIE kernels on this branch with all six controls red (the earlier heads' six and nine counted cuda ernie5_5: the DSA indexer's keys, k's LayerNorm then NeoX RoPE, under the kernel contract #5243's keys and controls). zig build test-kernel-contract 46 of 46 steps, 359 of 359 tests, test-ernie5_5-host 30 of 32 (2 skipped, main's), test-gradient-serving 1 of 1. mhc.cu's PTX (on d05feb3bcf, the same source): 33 compiles at each of compute_80, 86, 120 and 121 with address randomization on and off: one form at each.
    • Windows: pr_check on 01f035e6f4, 0 errors; before that head's rebase (the same files), zig fmt --check and zig ast-check on the 13 changed Zig files, zig build test-ernie5_5-host 25 of 27 passed, 2 skipped, compile-cuda-tests 12 of 12 steps and test-kernel-contract 46 of 46 steps, 343 of 343 tests.
    • WSL 2 (x86-64): main, cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract #5211's head, cuda ernie5_5: the DSA indexer's keys, k's LayerNorm then NeoX RoPE, under the kernel contract #5243's head, then this commit in one clone (so the Zig steps reuse the earlier trees' cache, and unchanged test modules are not run again; the GB10 run above has every count): git diff --check clean; zig build compile-cuda-tests 12 of 12 steps; test-gradient-serving 1 of 1; test-ernie5_5-host 30 of 32, 2 skipped; test-kernel-contract 46 of 46 steps (338 tests run); tools/zig/check_cuda_sources.py on mhc.cu with clang++ 6 of 6; host_tests_test OK; CI's pytest from the root (-q -rs) 856 passed, 116 skipped, 827 subtests passed on this tree and on main's, the same skips.
  • CI on this head (No GPU 46053219749, Zig format 46053219997): every job passed.
  • Not run, and why: the contract on GPUs other than the GB10 (sm_121); the other four SMs are covered by their limits records only. No served path, since nothing launches the kernel.

Receipt

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: tf_ernie55_mhc_pre (new; replaces none), its three outputs each registered alone, records for sm_80, 86, 89, 120 and 121 in zig/src/core/contract/ernie5_5_records.zig.
  • A planted control for each new kernel, and each one fails as planted: tf_ctl_ernie55_mhc_pre_plain_rms (the plain mix RMS), red at shapes on post ("at 6x4x16384 output 8 is 8.578284e-1 where float64 gives 1.334888e0: 2194.21 of its bound"); tf_ctl_ernie55_mhc_pre_column_softmax (comb's softmax over a column), red at shapes on comb (222.30 of its bound).
  • Each backend's SUMMARY line and exit status: SUMMARY ernie5_5 mhc: 3 kernels, 3 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed, exit 0; kernel-contract ernie5_5: (filtered) SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems, exit 0. CUDA only (no HIP, Metal or XPU image).
  • Chips I could not run: sm_80, 86, 89 and 120 GPUs (their limits records come from the fatbins).

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 host tests need only Zig; the contract's controls go red as planted; without a GPU or -Dnvcc the CUDA steps are not built, as for every image).
  • 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.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off (nothing launches the kernel yet).
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo (no served path changes).
  • No precision traded for speed. If the bits change, the description says which sums change and why (no served bits change; the mixes are fp32, not the vendor's TF32).
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled (no served path changes).

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. The Sinkhorn sequencing, collapse rounding and declared operation bounds are consistent, and the DSA key work underneath shows no static blocker.

Keeping the mHC mixes as fp32 products, rather than rounding inputs to TF32 the way the vendor's GEMM does, is the right choice here: we don't add rounding below what the checkpoint gives us, and the description states the difference plainly. No change is needed for that.

This one lands after #5211 and #5243 below it in the stack; #5211 has one malformed-count fix requested. No rebase is requested, and no device run is promised here.

  • Synthena

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu. The mHC pre-side arithmetic looks right: the FP32 mixes, the Sinkhorn sequence, the collapse rounding and the declared operation bounds are consistent, and no change is needed to the FP32-product choice.

This branch still carries an older copy of #5211. The interior-count case there (for example picks=[1,2,-1,-1], picked=4) is already fixed on #5211's current head ba13f19e08, where a negative pick inside the count ends the picks in both the kernel and the host reference. We'll restack this PR onto #5211's current head ourselves; nothing is needed from you for that.

After the restack, the remaining checks are on our side: ernie5_5-index, ernie5_5-mhc, the combined ERNIE contract and the five-SM limits records on a GB10. Synthetic inputs are enough, since nothing launches these kernels yet.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

After the restack, the remaining checks are on our side: ernie5_5-index, ernie5_5-mhc, the combined ERNIE contract and the five-SM limits records on a GB10.

(#5301 (comment))

Thanks for the restack. In case it saves you a run, I took those four on a GB10 at the restacked head 0336d7958d, with #5211's ba13f19e08 beside it, each built from an empty global cache at the five SMs with nvcc 13.0.88:

The description has the lines, with pr_check on this head (0 new problems).

…apse's RMSNorm, under the kernel contract

tf_ernie55_mhc_pre, in a new ernie5_5_mhc image, runs a row of ERNIE 5.5's 4 residual streams through vLLM-ERNIE's
mHC pre step: the 24 mixes against fn's float32 rows (a thread's 64 terms in one fma chain, the warp's butterfly,
the 8 warps' partials in order) times ERNIE's mix RMS, 1 / (sqrt(mean) + eps); pre, post (2 sigmoid) and comb (each
row's softmax plus hc_eps, then hc_sinkhorn_iters rounds of column and row sums); then the collapse sum_s pre_s x_s,
its RMSNorm from the unrounded sums' squares, and the norm's weight times the bf16 collapse, rounded to bf16 once.

GLM's finish in common/mhc.cuh takes the mix RMS from its caller, but it also reads fn in bf16, squares the rounded
collapse and rounds the normed value before the weight, so ERNIE's rounding points need their own body. The vendor's
mixes are a TF32 GEMM; these are fp32 products of bf16 x and fp32 fn.

Each output is checked alone against float64 (ernie5_5/mhc.zig), as GLM's float32-weight finish is, with two
controls: the plain mix RMS, 1 / sqrt(mean + eps), on rows below 1e-6, and comb's softmax over a column.
@ashhart
ashhart merged commit 6c22546 into ashhart:main Nov 28, 2026
1 check failed
@ashhart

ashhart commented Nov 28, 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