Skip to content

cuda ernie5_5: the mHC post side, a layer's output into its 4 streams, under the kernel contract - #5311

Closed
Bizuayeu wants to merge 3 commits into
ashhart:mainfrom
Bizuayeu:pr/ernie55-mhc-post
Closed

Bizuayeu wants to merge 3 commits into
ashhart:mainfrom
Bizuayeu:pr/ernie55-mhc-post

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

tf_ernie55_mhc_post, in the ernie5_5_mhc image (#5301): the mHC post side ERNIE 5.5 runs after each attention and each FFN, as vLLM's mhc_post forms it (mhc_post_tilelang), one row a block of 256 threads. A layer's bf16 output x goes into its row's 4 residual streams: stream o = post[o] x + sum_i comb[i][o] res[i], with post [4] and comb [16] from the pre side (#5301), from the product, then the input streams in order in fma, rounded to bf16 once. GLM's post side sits in glm_glue_rows.cuh with GLM's other glue, so ERNIE's is a family kernel beside the pre side. Nothing launches it yet, so served output is unchanged.

Checks on this head

  • On your rebase (99270b8f6e, on cuda ernie5_5: the DSA indexer's keys, k's LayerNorm then NeoX RoPE, under the kernel contract #5243's a106f24ea2), on a GB10 (sm_121): the tree cleaned (git clean -xdf) with an empty global cache, zig build compile-cuda-tests fatbins kernel-contract-fatbins -Dnvcc=... -Dsm=80,86,89,120,121 (nvcc 13.0.88): 0 errors. On the GPU, tf-cuda-test ernie5_5-mhc: SUMMARY ernie5_5 mhc: 4 kernels, 4 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 red as planted, 0 missed; tf-cuda-test kernel-contract ernie5_5: SUMMARY 7 kernels: 7 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems. Against cuda ernie5_5: the DSA indexer's keys, k's LayerNorm then NeoX RoPE, under the kernel contract #5243's head a106f24ea2, built and run the same way: kernel-contract-limits gives both the same 205 stale records and the same RED and FAIL lines, apart from this tree's 5 tf_ernie55_mhc_post lines (PASS ... (record current)); the only fatbin that differs is ernie5_5_mhc, whose SASS, compared function by function, adds 3 functions at each of the 5 SMs (tf_ctl_ernie55_mhc_post_comb_transposed, tf_ctl_ernie55_mhc_post_no_post, tf_ernie55_mhc_post) and changes none of the parent's; no image is added or removed. Host (zig build -Dfixed-cpu): test-ernie5_5-host 34/36 tests passed (2 skipped), test-gradient-serving 1/1 tests passed, test-kernel-contract 368/368 tests passed. The items below are from before your rebase.
  • Head commit: ff7009442a cuda ernie5_5: the mHC post side, a layer's output into its 4 streams, under the kernel contract. The GB10 runs below are on ea9393e263 and the WSL runs on 7023af0806, each this commit on a copy of cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract #5301's on main 77eae824e4, before the stack was rebased onto 469f1f7fd8 (which re-pinned only kernels.zig's line in gradient-serving.tsv, in cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract #5301's commit) and before its CHANGELOG line took this pull request's number; this commit's added and removed lines are the same there; on this head, the Windows checks. Since, mhc.cu takes core's warp_sum (common/reduce.cuh) in place of its own copy, which CI's cuda_common_check.py refuses; at the five SMs its cubins (nvcc 13.0.88 on x86-64) are the same as before in every section of every entry.
  • Lean checker (tools/pr_check.py --base 01f035e6f4 --pr 5311, this commit against cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract #5301's): 123 problems on both, 0 new; 0 errors. The 123 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): this commit (ea9393e263) checked out with an empty global cache and the tree's own cache and zig-out removed, 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_post lines PASS ... (record current) (48 registers at sm_80, 120 and 121, 47 at sm_86 and 89, no static shared memory, bound 256, no stack or spills), and its other lines are cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract #5301's (the same 186 stale records and RED and FAIL lines). tf-cuda-test ernie5_5-mhc passes post's gates 1-4: gate 1, 12 non-finite and 81,908 finite outputs in their float64 classes; gate 2, five runs byte-identical; gate 3, 3 shapes within the FP64 bound, the worst at 0.996 of it; gate 4, exact sums at k 5, where fp16 sums would miss by 16,384x and bf16 by 65,536x. Both controls are red (below), and the pre side's three outputs pass as on cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract #5301. kernel-contract ernie5_5 passes the seven ERNIE kernels with all 11 controls red. In the stack's earlier GB10 chain (the same kernel source, before gate 4's fill was fixed), the fatbins equal cuda ernie5_5: the mHC pre side, a row's mixes, Sinkhorn and the collapse's RMSNorm, under the kernel contract #5301's but for ernie5_5_mhc, whose existing functions' SASS is unchanged (post's 15 added), and mhc.cu's PTX on the next commit's tree (65a8c8d632, which adds hc_head to the file): 33 compiles at each of compute_80, 86, 120 and 121 with address randomization on and off, one form at each.
    • Windows, on this head: zig fmt --check and zig ast-check on the 6 changed Zig files, and pr_check: 0 errors.
    • WSL 2 (x86-64): this commit (7023af0806) in a clone of the stack: git diff --check clean; zig build compile-cuda-tests 12 of 12 steps; test-gradient-serving 1 of 1; test-ernie5_5-host 32 of 34, 2 skipped; test-kernel-contract 46 of 46 steps, 350 of 350 tests; 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, the same skips as main's.
  • CI: (after the push)
  • 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_post (new; replaces none), 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_post_comb_transposed (comb read as comb[o][i]), red at shapes ("at 4x16384x5 output 0 is -2.500000e0 where float64 gives -3.900770e-1: 1384.32 of its bound"); tf_ctl_ernie55_mhc_post_no_post (the layer output added without post), red at shapes (50.00 of its bound).
  • Each backend's SUMMARY line and exit status: SUMMARY ernie5_5 mhc: 4 kernels, 4 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 red as planted, 0 missed, exit 0; kernel-contract ernie5_5: (filtered) SUMMARY 7 kernels: 7 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 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 post side's sums are fp32 fmas, rounded to bf16 once).
  • 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 28, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. The Sinkhorn sequencing, collapse rounding and declared mHC operation bounds are internally consistent.

Two blockers remain in this cumulative stage. First, an oversized picked count can still copy -1 padding into the live sparse-MLA list. With picks [5, -1], k=2, picked=7, seq=10, and window 3, both host and CUDA produce [5, -1, 7, 8, 9] with count 5, and sparse MLA treats -1 as a cache row. Please stop both merges at the valid nonnegative prefix before appending the window. Add fail-before host and CUDA cases with an oversized count and short valid prefix, and run structural assertions on that malformed row.

Second, the mHC mix still substitutes unrounded FP32 coefficients and a custom reduction for the pinned TF32/DeepGEMM arithmetic. A coefficient such as 1 + 2^-11 demonstrates the difference, and the host oracle currently repeats the same substitute. Please reproduce the pinned coefficient rounding and reduction order exactly, or provide byte-exact same-engine evidence for the alternative without tolerance. Update the oracle and add a planted non-TF32 coefficient control.

The inherited hardening fixes also remain: guard window-shape multiplication with division, clear the contract's extra slice before its fallible reallocation, and add errdefer cleanup for tok before allocating cnt.

No rebase is requested, and no device run is promised here.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Please reproduce the pinned coefficient rounding and reduction order exactly, or provide byte-exact same-engine evidence for the alternative without tolerance.

(#5311 (comment))

Thanks. The mixes stay fp32 products on purpose: your reviews of #5301 found that the right choice, "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" (#5301 (comment)) and "no change is needed to the FP32-product choice" (#5301 (comment)), and the kernel and the oracle here are that choice. Should I keep it, or move the kernel and the oracle to the vendor's TF32 inputs and DeepGEMM's reduction order, with the planted 1 + 2^-11 coefficient?

The -1 padding and the hardening fixes are as on #5243 (#5243 (comment)): the merge's fix is #5211's, and this branch's copy of #5211's commit predates it; the two test fixes are ready as commits for #5211 and #5243.

@ashhart

ashhart commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. Keep the FP32 products and the stated reduction order. Our TF32 rewrite request was not justified by this full-precision contract, and we’re withdrawing it. No TF32 rewrite or control against retaining full FP32 coefficients is needed.

The post-side orientation and scaling agree with the host form. The remaining changes are the two prepared allocation-cleanup fixes described on #5243. We’ll carry those and the list fixes already on main through the restack; you do not need to duplicate them here or rebase.

Current-tree CUDA qualification remains pending.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Keep the FP32 products and the stated reduction order.

(#5311 (comment))

Thanks, kept: the kernel and the oracle stay as they are, with no change on this branch. The two cleanup fixes are now on #5243 (544ed10f0d) and in #5535.

@ashhart

ashhart commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. Keeping the agreed FP32 coefficients, products and reduction order meets the arithmetic request. The TF32 rewrite request remains withdrawn; no TF32 rewrite or control against retaining FP32 coefficients is needed. The post-side orientation and scaling remain consistent with the host form.

The remaining work is the planned stack integration: preserve main's corrected window merge and overflow guard, and carry the allocation cleanup from #5243 and #5535. Those fixes need not be duplicated in this PR, and no rebase is requested from you here.

Current-tree CUDA qualification remains pending after that integration.

  • Synthena

@ashhart
ashhart force-pushed the pr/ernie55-mhc-post branch from 3238d95 to 99270b8 Compare November 28, 2026 23:33
…under the kernel contract

ERNIE 5.5's indexer scores each query against keys made from wk's k: LayerNorm(128, eps=1e-6) with k_norm's weight
and bias, then RoPE on the first 64 dims in NeoX's halves, pairs i and i + 32, at the config's rope_theta (the DSA
layers' plain RoPE; YaRN is the HCA layers'). tf_ernie55_index_key (in the ernie5_5_index image) makes a key a warp:
the LayerNorm in fp32, the mean then the squares about it, y = fma(xc / sqrt(var + eps), w, b), then the turn at the
fp32 angle inv[i] times the position with sincosf, and one rounding to bf16 (vLLM rounds to bf16 after the LayerNorm
and again after the rope). The vendor's Hadamard on q and k before its FP8 quantization is left out: with bf16 keys it
changes no score in exact arithmetic. key.zig is the host's float64 key; its tests hold it to vLLM's F.layer_norm and
NeoX halves.

The contract (ernie5_5-index, also in kernel-contract) asks each key within its float64 bound, from prompt chunks and
a decode row at the first position to decode rows past sincosf's fast range and at the 1M context's end; gate 4's rows
of 254 to 260 in steps of 2 keep fp32 sums exact while fp16 and bf16 sums lose the steps. The k rows sit 192 apart
with NaN past 128, so a read past the key shows. Three controls go red: GPT-J's pairs, the LayerNorm without its
bias, and the next position's angle. Nothing launches it yet.
…eing and reallocating it

reference freed extra and allocated it again; had that allocation failed, extra would still have held the freed
slice, freed a second time by the next reference or by deinit. It is now empty between the two.
…, under the kernel contract

tf_ernie55_mhc_post, in the ernie5_5_mhc image, adds a layer's bf16 output to its row's 4 residual streams as
vLLM's mhc_post does for ERNIE 5.5: stream o = post[o] x + sum_i comb[i][o] res[i], with post and comb from the pre
side, from the product, then the input streams in order in fma, rounded to bf16 once. GLM's post body sits in its
own glue, so ERNIE's is a family kernel.

The streams are checked against float64 (ernie5_5/mhc.zig), with gate 4 on integer terms that fp32 holds in any
order, cancelling to +-1/2, whose fp16 and bf16 roundings do not; two controls: comb read transposed, and the layer
output added without post.
@ashhart
ashhart force-pushed the pr/ernie55-mhc-post branch from 99270b8 to 8aeabdc Compare November 29, 2026 03:45
@Bizuayeu

Bizuayeu commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

land #5538 and close #5508 and #5536 as included in it

(#4926 (comment))

#5538's head 6b16a9441a carries this pull request's work too (the mHC post side, ee129091bc), so it can close as included in the same way. CI passes on that head, and the GB10 run of the whole ERNIE contract on it, 17 kernels through gates 1-4 with their controls red as planted and 0 limit problems, is the first item of #5538's Checks.

@ashhart

ashhart commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu.

Confirmed: the mHC post side keeps the agreed FP32 products and reduction order, and its orientation and scaling match the host form. In #5538 the post kernel is byte-for-byte the one here, and #5538 also adds the furthest-offset bounds on the mHC checks.

So we'll land #5538 and close this one as included. Nothing more is needed from you here.

  • Synthena

@ashhart

ashhart commented Nov 30, 2026

Copy link
Copy Markdown
Owner

This landed as part of #5538, so I am closing it as included. Thank you, @Bizuayeu.

  • Synthena

@ashhart ashhart closed this Nov 30, 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