Skip to content

cuda ernie5_5: the inverse rope on the attention output, GPT-J's pairs turned back, under the kernel contract - #5536

Closed
Bizuayeu wants to merge 16 commits into
ashhart:mainfrom
Bizuayeu:pr/ernie55-inv-rope
Closed

Bizuayeu wants to merge 16 commits into
ashhart:mainfrom
Bizuayeu:pr/ernie55-inv-rope

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

tf_ernie55_inv_rope, in #5326's ernie5_5_hca image (hca.cu, so no new image and no change to the kernels.zig pin): ERNIE 5.5's inverse rope on the HCA attention output. vLLM's fused_inv_rope_fp8_quant (with quantize=False when wo_a is bf16) turns each head's rope dims 448..511 back after VHA, by the query's position: GPT-J's pairs, x_e cos + x_o sin and x_o cos - x_e sin, from the HCA rope's fp32 table, computed in fp32 from the bf16 input and stored as bf16; the nope dims pass. A launch takes rows tokens of 64 heads, bf16 [rows][64][512], row r at pos[0] + r, and writes a separate output. Nothing launches it yet, so served output is unchanged.

One 256-thread block a token; thread t holds dims 2t and 2t + 1 of each of the 64 heads, so threads 224..255 hold the 32 rope pairs. Pair l = t - 224 turns by the angle freq[l] * pos, the fp32 product of the HCA rope's YaRN frequency (theta 160,000, factor 16 over 65,536 positions, core.rope_yarn.yarn) and the position, its sine and cosine sincosf's, once a thread for its 64 heads (the compressor's angle and its out-of-line sincosf, #5326). Each product and the sum round in fp32 (__fmul_rn, then __fadd_rn or __fsub_rn), then bf16 once. vLLM's Triton kernel leaves fusion on, so it may contract x cos ± x' sin into an fma; the bound below covers either.

  • zig/src/families/ernie5_5/hca.zig holds the host's float64 inverse rope (turnBack, at the same angle as the compressor's rope) and invCheck (more rows than the grid's i32 refused). hca_test.zig holds turnBack to vLLM's form written apart from it at positions 0, 127, 4,000, 199,999 and 1M - 1; to the identity at position 0 and to the rope undone at the same position (turn then turnBack); and invCheck's refusal.
  • cuda_hca.zig's Hca loads tf_ernie55_inv_rope with the image's other entries and launches it (invRope, invRopeWith): a token a 256-thread block.
  • The contract (zig/tests/cuda/contract_ernie5_5_inv_rope.zig, tf-cuda-test ernie5_5-inv-rope, also in kernel-contract) checks every output (bf16) against float64. With no sums, .accum = .none and gate 4 does not apply. The bound adds, beyond the output's bf16 rounding, sincosf's 2 ulps and the steps' roundings as 8 units (2^-24) of each output's terms, |x_e| |cos| + |x_o| |sin|. The shapes: 4 rows from position 126 (the window filling), 1 at position 0, 64 from 4,000 (a prompt chunk), 1 at 199,999 (past sincosf's fast range) and 2 ending at the 1M context's last position, with x drawn from [-2, 2) as bf16; gate 1 plants NaN in a nope dim, inf in a rope dim and -inf in the last. Two planted controls, in the kernel's body: the pairs turned forward (the q's rope again), and each row at the next position's angle.
  • The contract file's own tests join cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's root (contract_ernie5_5_test.zig), so test-cuda-host runs them.
  • The record (ernie5_5_records.zig), the runner's module, docs/kernel-contract.md and the usage line.

Searched first: no open pull request or issue covers this (searched "inverse rope" and "inv_rope"). On main, DeepSeek V4.1 folds its inverse rope into its attention merge, before the fp32 output is rounded; ERNIE's VHA rounds to bf16 in between (#5508), so that form does not carry over.

Checks on this head

  • Head commit: 55fa125d32 cuda ernie5_5: VHA's z as its two 32-head halves added once, vLLM's form at two ranks, on your restack 68a53268b1 (15 commits on main 0546beeee8), as agreed on ERNIE-5.5-Flash-Preview on two DGX Sparks: a native CUDA family #4926 (ERNIE-5.5-Flash-Preview on two DGX Sparks: a native CUDA family #4926 (comment)): cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's 5b63cc0ee1 picked, its hca.cu and docs/kernel-contract.md hunks the same patch, the records line giving tf_ernie55_vha the same registers, and no CHANGELOG line, since the restack carries none for VHA; cuda ernie5_5: the output gate, z times bf16(sigmoid(g)) at torch's roundings, under the kernel contract #5538 (fb59c9db2c) has the same change. On the GB10 (sm_121), 55fa125d32 and 68a53268b1 each checked out with an empty global cache, zig build compile-cuda-tests fatbins kernel-contract-fatbins -Dnvcc=... -Dsm=80,86,89,120,121: 0 errors on both. tf-cuda-test kernel-contract-limits zig-out/fatbin gives the five tf_ernie55_vha lines PASS ... (record current), 74 registers at sm_80, 86 and 89 and 64 at sm_120 and 121 (8,192 static shared bytes, bound 256; 56, 59, 59, 40 and 52 on 68a53268b1), and its other lines are 68a53268b1's. tf-cuda-test ernie5_5-vha passes gates 1-4 with 68a53268b1's numbers, run beside it: gate 1, 192 non-finite and 163,648 finite outputs in their float64 classes; gate 2, five runs byte-identical; gate 3, 8 shapes within the float64 bound, the worst at 0.990 of it; gate 4, exact sums at k 64; its two controls red as planted at 940.80 and 33.46. tf-cuda-test kernel-contract ernie5_5: 16 kernels pass gates 1-4, 24 controls red as planted, 0 limit problems. Against 68a53268b1's fatbins, 208 of 212 are byte-identical; in ernie5_5_hca only tf_ernie55_vha and its two controls change, at each SM (cuobjdump -sass, addresses dropped), and ernie5_5_index, glm_glue and glm_index differ in bytes with no SASS function changed. hca.cu's PTX: 33 compiles at each of compute_80, 86, 120 and 121 with address randomization on and off, one form at each. zig build -Dfixed-cpu test-ernie5_5-host 53 of 55, 2 skipped; test-kernel-contract 49 of 49 steps, 381 of 381 tests; test-gradient-serving 1 of 1; test-ernie5_5-contract-host 23 of 23; test-cuda-host 576 of 576 steps, 11,008 of 11,058 tests, 50 skipped. Windows: tools/pr_check.py --pr 5536 against 68a53268b1, 0 errors, lean 92 against 92, 0 new, import cycle edges 25 against 25, cross-family edges 11 against 11 (1 allowed); zig fmt --check and zig ast-check on the records file; git diff --check clean; compile-cuda-tests 12 of 12 steps, test-ernie5_5-host 53 of 55, 2 skipped, test-ernie5_5-contract-host 23 of 23, test-kernel-contract 49 of 49 steps, 381 of 381 tests. WSL 2 (x86-64), a fresh clone: git diff --check clean, the same host steps with the same counts and compile-cuda-tests 12 of 12, test-cuda-host 576 of 576 steps, 10,723 of 10,771 tests, 48 skipped, tools/zig/check_cuda_sources.py on hca.cu with clang++ 6 of 6, host_tests_test OK, CI's pytest from the root (-q -rs) 966 passed, 121 skipped, 869 subtests passed, the same skips as on 68a53268b1. conflict_delta against 68a53268b1 over the 363 open pull requests on 2026-11-30 at 02:45 UTC: it changes the conflicting hunks of two, cuda ernie5_5: the output gate, z times bf16(sigmoid(g)) at torch's roundings, under the kernel contract #5538 (21 to 19; it has the same change) and cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508 (41 to 39; it has the change), with no new files.
  • The inverse rope's commit before your restacks: 42b3204f22 cuda ernie5_5: the inverse rope on the attention output, GPT-J's pairs turned back, under the kernel contract: deb0fa3a51 restacked on cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's 7bd2c5bd9d (git range-diff equal), its tree the same as 20e08a2e4b's, where the heads bound sat on this branch before it moved to cuda ernie5_5: the HCA attention, a row's window and compressed keys with fp32 sinks, under the kernel contract #5443 and cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508. No shape the receipts below run reaches that bound (the HCA attention's contract runs 64 and 32 heads). deb0fa3a51 differs from 9cc1e164cf, where the GB10 steps below ran, in its base (cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's head now, with the contract-test root), its line in that root, the contract's extra emptied between freeing and reallocating it, and this pull request's number in its CHANGELOG line; hca.cu and the kernel's launch are the same. 1f3bd0507f (this commit on cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's first commit, without the root line and the number), built on the GB10 the same way, against 9cc1e164cf's fatbins: 196 of 200 byte-identical, and the four whose bytes differ (ernie5_5_hca, ernie5_5_index, glm_glue, glm_index) have no SASS function changed; so the receipts below stood for it; your restack 68a53268b1 and this head have their GB10, Windows and WSL steps in the line above.
  • Lean checker (tools/pr_check.py --base 7bd2c5bd9d --pr 5536, 42b3204f22 against cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's): 123 problems on both, 0 new; import cycle edges 24 against 24, 0 new; cross-family edges 11 against 11 (1 allowed); 0 errors.
  • Tests run, with the command and pass, fail and skip counts (Zig 0.17.0, nvcc 13.0.88):
    • GB10 (sm_121): 9cc1e164cf checked out with an empty global cache beside its parent 7f02aa063b built the same way, zig build compile-cuda-tests fatbins kernel-contract-fatbins -Dnvcc=... -Dsm=80,86,89,120,121: 0 errors on both. On the GPU, tf-cuda-test kernel-contract-limits zig-out/fatbin gives the five tf_ernie55_inv_rope lines PASS ... (record current) (32 registers at sm_80, 34 at the others, no static shared memory, bound 256), and its other lines are the parent's; tf-cuda-test ernie5_5-inv-rope passes gates 1-3: gate 1, 5 non-finite and 163,835 finite outputs in their float64 classes; gate 2, five runs byte-identical; gate 3, 5 shapes within the float64 bound, the worst at 0.996 of it; gate 4 n/a (no sums); its two controls red as planted (below). tf-cuda-test kernel-contract ernie5_5: 16 kernels pass gates 1-4, 24 controls red as planted. hca.cu's PTX: 33 compiles at each of compute_80, 86, 120 and 121 with address randomization on and off, one form at each. zig build -Dfixed-cpu test-ernie5_5-host 51 of 53, 2 skipped; test-kernel-contract 46 of 46 steps, 353 of 353 tests; test-gradient-serving 1 of 1; test-cuda-host 512 of 512 steps, 9,862 of 9,907 tests, 45 skipped; the contract file's tests, from a temporary root over the runner's modules, 3 of 3.
    • On 20e08a2e4b, the tree before your restacks: zig build -Dfixed-cpu test-ernie5_5-host --summary all 3 of 3 steps, 51 of 53 tests passed, 0 failed, 2 skipped, on Windows and in a fresh WSL 2 clone; compile-cuda-tests 12 of 12 steps on both.
    • Windows, on d26a9ef17f (deb0fa3a51 but for this pull request's number in its CHANGELOG line): zig build -Dfixed-cpu test-ernie5_5-contract-host --summary all 7 of 7 steps, 23 of 23 tests passed, 0 failed, 0 skipped (cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's 21 and this file's 2); zig fmt --check and zig ast-check on the changed Zig files; on 9cc1e164cf, compile-cuda-tests 12 of 12 steps and test-kernel-contract 46 of 46 steps, 353 of 353 tests.
    • WSL 2 (x86-64), on d26a9ef17f, in a fresh clone of the stack: git diff --check clean; zig build -Dfixed-cpu compile-cuda-tests 12 of 12 steps; test-ernie5_5-host 51 of 53, 2 skipped; test-cuda-host, which runs the contract-test root, 518 of 518 steps, 9,885 of 9,930 tests passed, 45 skipped (2 more than cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's head, this contract file's 2); tools/zig/check_cuda_sources.py on hca.cu with clang++ 6 of 6; host_tests_test OK. CI's pytest from the root (-q -rs), in cuda ernie5_5: the output gate, z times bf16(sigmoid(g)) at torch's roundings, under the kernel contract #5538's clone (244bedcd96, the output gate on d26a9ef17f): 861 passed, 116 skipped, 827 subtests passed, the same skips as main 469f1f7fd8's.
  • CI: passed on this head: the No GPU and Zig format workflows (runs 46350737120 and 46350737116), 2026-11-30 04:02 to 06:53 UTC, 11 jobs succeeded and the two host-nightly jobs were skipped.
  • 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

  • Main against branch: n/a. Nothing launches tf_ernie55_inv_rope, so no served token or byte can change. On the GB10, against its parent built the same way, ernie5_5_hca gains tf_ernie55_inv_rope and its two controls at each SM with no existing function changed (cuobjdump -sass, addresses dropped), and of the other 199 fatbins 196 are byte-identical and three differ in bytes with no SASS function changed (ernie5_5_index, glm_glue and glm_index, which differ the same way between two builds of the same sources).
  • Boxes it reaches: none served yet; it is written for ERNIE 5.5's HCA layers (refs ERNIE-5.5-Flash-Preview on two DGX Sparks: a native CUDA family #4926).
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: 55fa125d32 (this head, its line above, against your restack 68a53268b1 on main 0546beeee8), 42b3204f22 (the head before your restacks), 20e08a2e4b (its tree: the host tests above), deb0fa3a51 (this commit on cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508's 9466965593), d26a9ef17f (deb0fa3a51 but for its CHANGELOG number: Windows, WSL), 1f3bd0507f (the GB10's fatbins against the gate's) and 9cc1e164cf (the GB10's gates and host steps, Windows' build), on cuda ernie5_5: VHA, the attention output's head mix o + V (U^T o), under the kernel contract #5508 on main 469f1f7fd8; Zig 0.17.0, nvcc 13.0.88; one GB10 (sm_121); no checkpoint (synthetic rows; the rope dims and frequencies from the released config).
  • Paths and platforms the change reaches, which I ran and which I could not: the kernel on sm_121 through the contract; sm_80, 86, 89 and 120 by their limits records only.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: n/a (no served path).
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): n/a (no served path).
  • Decode speed before and after, from tools/bench_openai.py: n/a (no served path).
  • Prompt speed before and after: n/a (no served path).
  • Licence of any new weights or drafter, or "n/a": 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_ernie55_inv_rope (new; replaces none), records for sm_80, 86, 89, 120 and 121 in zig/src/core/contract/ernie5_5_records.zig (32 registers at sm_80, 34 at sm_86, 89, 120 and 121, no static shared memory, bound 256, all current).
  • A planted control for each new kernel, and each one fails as planted: the pairs turned forward, red at 4x126x64 at 49.14 of the bound; each row at the next position's angle, red there at 236.05.
  • Each backend's SUMMARY line and exit status: SUMMARY ernie5_5 inv_rope: 1 kernels, 1 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 16 kernels: 16 pass gates 1-4, 0 fail, 0 known findings; 24 controls: 24 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; each output is one pair's two fp32 products and their sum, rounded to bf16 once, as written above).
  • 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 for the detailed kernel contracts. Two cases need correction before the remaining stack moves forward.

In window.zig, merge clamps picked[0] and copies that many entries, but does not stop at a negative pick. With k = 2, picked[0] = 7, position 9, a three-token window, and picks {5, -1}, it produces the active list {5, -1, 7, 8, 9} with count 5. The CUDA merge produces the same active list. The -1 sentinel is included as though it were a key. Current main already corrects this; please retain that correction and its regression when rebasing.

Separately, attnCheck accepts rows = 1, heads = 65536, w = 1, nch = 1. cuda_hca.zig launches a valid chunk grid, then calls mergeSink, whose grid Y is heads. That is 65,536, above CUDA's 65,535 limit, so the merge fails and no final attention output is produced. Please reject head counts above 65,535 or change the merge geometry, with a regression at this boundary.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

Please reject head counts above 65,535 or change the merge geometry, with a regression at this boundary.

(#5536 (comment))

Thanks. Pushed as a second commit here, 20e08a2e4b on deb0fa3a51. attnCheck now refuses more than 65,535 heads, the sink merge's grid y. That also bounds the chunk tiles' (heads + 15) / 16, which was the only heads bound before. Its test accepts 65,535 heads and refuses 65,536 (rows = 1, w = 1, nch = 1); the 65,536 case failed on the old check. Its partials case moves from 16 x 65,535 heads to 65,535, so that it still meets only the partials' bound. test-ernie5_5-host: 51 of 53, 2 skipped, on Windows and in a fresh WSL clone. #5538 is restacked on it, and #5508's head is unchanged.

On the negative pick: this stack still carries #5211's commit from before its malformed-prefix fix, so its window.zig and index.cu are the old ones. When I rebase this onto main after #5508 lands, I will keep main's window.zig, index.cu and their regressions.

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

The heads bound has moved to where the check lives: #5443 (a515365bc9) and #5508 (7bd2c5bd9d), as I wrote on #5508 (#5508 (comment)). This branch is restacked on #5508's new head as 42b3204f22. Its own commit is again only the inverse rope, and its tree is the same as 20e08a2e4b's, so the checks in my reply above stand.

@ashhart

ashhart commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. The HCA check now rejects more than 65,535 heads before the chunk launch and sink merge, and the regression accepts 65,535 while refusing 65,536 with the reported one-row shape. That request is met. The inverse RoPE increment has no additional source-proven defect in this re-check.

When the dependency stack is restacked, retain main's nonnegative-prefix window merge and its regressions. Also preserve the prepared allocation fixes from #5243, #5326 and #5535; their older versions remain in this cumulative branch. No duplicate window patch is needed here.

Current-tree CUDA qualification remains pending after the integrated stack is ready.

  • Synthena

@ashhart
ashhart force-pushed the pr/ernie55-inv-rope branch 3 times, most recently from 915c225 to 39ecca7 Compare November 29, 2026 13:11
Bizuayeu and others added 15 commits November 29, 2026 20:25
…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.
…r the kernel contract

tf_ernie55_hc_head, in the ernie5_5_mhc image, folds ERNIE 5.5's 4 residual streams into the one row the final
RMSNorm takes, as vLLM's hc_head does: its 4 mixes against fn's float32 rows and the streams' squares, summed as the
pre side sums them, the plain RMS 1 / sqrt(mean + eps) (vLLM's hc_head keeps it, where ERNIE's pre side takes
1 / (sqrt(mean) + eps)), pre = sigmoid(m r scale + base) + hc_eps, then sum_s pre_s x_s rounded to bf16 once.

The row is checked against float64 (ernie5_5/mhc.zig), from the pre side's rows; gate 4 runs pre at 1/2 on one
stream. Two controls: ERNIE's pre-side mix RMS, and the mixes without hc_head's scale.
… under the kernel contract

tf_ernie55_mhc_pre_broadcast, in the ernie5_5_mhc image, is ERNIE 5.5's first-layer mHC pre as vLLM-ERNIE's
broadcast forms it: the embedding row e as each of the 4 streams, its 24 mixes against fnb (fn's 4 column blocks
summed at load, float32), the mix RMS 1 / (sqrt(mean) + eps) over e's own 4096 squares, then the pre side's pre,
post, comb (Sinkhorn) and normed collapse, and e written into the 4 residual streams.

The pre side's sums, its mixes' outputs and its norm become three inline functions the two kernels share; the pre
side's usage is unchanged at the five SMs.

Each output is checked alone against float64 (ernie5_5/mhc.zig, whose host test holds the broadcast to the pre side
of e's 4 copies, bit for bit); gate 4 runs pre at 1/2 on half-integer rows. Two controls: the mix RMS's mean over
pre's 4 x 4096 values, and e written into stream 0 alone.
…d RoPE, under the kernel contract

tf_ernie55_hca_compress, in a new ernie5_5_hca image, forms each compressed row of ERNIE 5.5's HCA layers from its
block of 128 tokens as vLLM's C128 compressor does: for each of the 512 channels, the softmax over the tokens of
score + ape pooling the fp32 kv (the block's max, then expf of each difference summed and kv e accumulated in fma, the
tokens in order, one division); the row's RMSNorm 1 / sqrt(mean + eps) times its weight; and GPT-J's pairs on the last
64 dims turned at the block's first position with YaRN's frequencies at compress_rope_theta; rounded to bf16 once.

The rows are checked against float64 (ernie5_5/hca.zig, whose host test holds it to the vendor's two-stage pool); gate
4 sets every exponent to 0, with odd kv whose second half cancels the first. Two controls: the pool without ape, and
the rope at the block's last position.

hca.check also refuses strides whose furthest offsets, (128 rows - 1) ts + 1023 and (rows - 1) os + 511 floats, would
pass the kernel's i64, so the launch's casts to its i64 arguments cannot trap.
…eeing 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.
…with fp32 sinks, under the kernel contract

tf_ernie55_hca_chunks, in a new ernie5_5_hca_attn image, is core's chunk tiles (common/mla.cuh's chunks_wide, the body
glm_glue's tf_glm_chunks_16 runs) over 512-wide q and keys, a key row its own value: 16 heads a block, a row's keys
512 a chunk as its list gives them (the window's rows max(0, p - 127) .. p, then the compressed rows 0 .. (p + 1) / 128
- 1). The mla image's tf_mla_merge_sink then merges the chunks with each head's fp32 sink as a zero-value key: the
vendor's attn_sink, a natural-log logit not multiplied by the softmax scale 512^-0.5. cuda_hca.zig's Attn launches the
two.

The outputs are checked against float64 (ernie5_5/hca.zig, whose host test holds it to vLLM's eager softmax over the
scores and the sink, the sink's column dropped); gate 4 sets q to 0, so every weight is 1 and each output 1/16 with its
sums exact. Three controls in the launch's arguments: the sinks left out, the sinks times the scale, and the compressed
rows not counted.
…der the kernel contract

tf_ernie55_vha, in hca.cu's ernie5_5_hca image, mixes a token's 64 attention heads at each of its dims as vLLM's
VHAPostMix does at tensor parallel above 1: z = U^T o (16 sums of 64 heads), then each head's V z with its own o added
last, in fp32 fmaf in that order and rounded to bf16 once. U and V are the layer's bf16 [64][16]; a launch takes rows
tokens of d dims, 512 for HCA's layers and 256 for DSA's. One launch serves a rank; split across ranks, z is the sum
to reduce.

The outputs are checked against float64 (ernie5_5/hca.zig's vha, whose host tests hold it to the matrix form and, in
exact arithmetic, to the fold I + V U^T); gate 4's column puts 2^20, 24 and -2^20 on heads 0-2 and 1/4 on the rest
with U = 1 and V = 1/16, so z is 39.25 and exact in fp32 in any order, while bf16's sums lose the 24 and fp16's
overflow. Two controls: the mix without o, and U and V read as [16][64].
…ilt over the runner's modules

No build step compiled the test blocks of zig/tests/cuda/contract_ernie5_5_*.zig: the runner, zig/tests/cuda/main.zig,
is built only as an executable, so a test there could be wrong unseen. contract_ernie5_5_test.zig imports the six that
hold tests (20), and test-ernie5_5-contract-host builds it with the runner's imports, as asked in ashhart#4926;
test-cuda-host depends on that step, beside contract_arch_test.zig.

Building them found the HCA compressor contract's YaRN test dividing the comptime float -40.0 by hh.rope, a comptime
int, which Zig 0.17 refuses. It now divides by hh.rope as f64: the same value, compared against the same bounds.
…, the sink merge's grid y

attnCheck bounded the chunk tiles' grid y, (heads + 15) / 16, but mergeSink then launches with grid y = heads, so
heads = 65,536 passed the check and the merge failed to launch, leaving no output. The check now bounds heads itself,
which also bounds the tiles' grid; the test accepts 65,535 and refuses 65,536, and its partials case moves to 65,535
heads so that it still meets only the partials' bound.
…s turned back, under the kernel contract

tf_ernie55_inv_rope, in hca.cu's ernie5_5_hca image, turns each of a token's 64 heads' rope dims 448..511 back as
vLLM's inverse rope does after VHA: GPT-J's pairs at the row's position (row r at pos[0] + r) with the HCA rope's YaRN
frequencies, x_e c + x_o s and x_o c - x_e s, each step rounded in fp32 and the output to bf16 once; the nope dims are
copied. The angle is the fp32 product of the frequency and the position, as the compressor's, its sine and cosine
sincosf's.

The outputs are checked against float64 (ernie5_5/hca.zig's turnBack, whose host tests hold it to vLLM's form written
apart and to the rope undone at the same position); with no sums, gate 4 does not apply. Two controls: the pairs
turned forward, and each row at the next position's angle.
…orm at two ranks

tf_ernie55_vha built z = U^T o as one fp32 chain over the 64 heads, so a run split over two ranks would match it only
if rank 1 went on from rank 0's z. z is now the chain over heads 0 to 31 and the chain over 32 to 63, each from 0,
added once in fp32: vLLM's tensor-parallel form at world 2 (z = all_reduce(bmm(U^T, o, out_dtype=fp32))), where each
rank's half goes through one all-reduce of two values, whose sum has no order. V z + o is as before. The kernel holds
the two partial sums, so its records go from 56, 59, 59, 40 and 52 registers to 74, 74, 74, 64 and 64 at sm_80, 86,
89, 120 and 121, and sm_120's 8-byte spill is gone. docs/kernel-contract.md says the sum's form.
@Bizuayeu

Copy link
Copy Markdown
Contributor Author

We'll review [...] #5536 and #5538 after their pushes.

(#4926 (comment))

Thanks. Pushed: this branch's head is now 55fa125d32, #5508's two-chain VHA change (5b63cc0ee1) picked onto your restack 68a53268b1. It changes hca.cu, the records line (VHA at 74, 74, 74, 64 and 64 registers) and docs/kernel-contract.md; your restack carries no VHA CHANGELOG line, so it adds none.

On the GB10, beside 68a53268b1:

  • ernie5_5-vha passes gates 1-4 with the same numbers, and both planted controls are red.
  • In kernel-contract-limits, only the five VHA lines change, each record current.
  • kernel-contract ernie5_5 gives 16 kernels passing and 24 controls red.
  • Only the three VHA entries' SASS changes, at each SM.

The host steps pass on the GB10, Windows and WSL. The numbers are in the body's first Checks item. #5538 has carried the same change since fb59c9db2c.

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

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

(#4926 (comment))

Agreed. #5538's head 6b16a9441a carries this pull request's work as 6973755b6a (the inverse rope) and fb59c9db2c (the two-chain VHA change, 55fa125d32 here), and CI passes on it. Nothing here needs to land on its own.

@ashhart

ashhart commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu.

We compared this branch with #5538 file by file. Everything here is in #5538 unchanged or extended, and #5538 also has the three host fixes and #5443's bounds that this branch doesn't. So, as you suggested, the plan is to land #5538 and close this one as included. Nothing more is needed from you here, and thank you for keeping the two in step.

  • 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