Skip to content

cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract - #5211

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

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

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

A new ernie5_5_index image with tf_ernie55_window_merge: the list of keys each of ERNIE 5.5's DSA rows attends over, its indexer's top-k picks and then its 128-position sliding window, as vLLM-ERNIE's merge_swa_topk lays them out for its sparse MLA ([topk[:n] | seq-w .. seq-1 | -1 ...], with w = min(seq, 128)). It reads topk_rows' out and picked (#5051) and writes the tokens [rows, w] and counts [rows] that mla.zig's sparse On takes, row r at position pos[0] + r as the MLA has it. One choice differs from the vendor's kernel: n is the row's picked, held to [0, k] and then to the picks before the row's first -1, not min(max(seq - 128, 0), k) from the position, because the MLA reads every entry before a row's count as a cache row (common/mla.cuh), so no -1 may fall inside the list. Where picked is that count (the indexer told the row's positions older than the window), the lists are the vendor's. Nothing launches it yet, so served output is unchanged.

Checks on this head

  • Head commit: c07fcb5e32 cuda ernie5_5: the DSA key lists, top-k picks then the window, under the kernel contract. It is 55a5d64aa5 with window.check's size test made a division and its fail-before host case (2 files, window.zig and window_test.zig). The GB10 and WSL runs below are on 55a5d64aa5, whose kernel, contract and records this head keeps (the GB10 beside 24e735876a, built the same way), and the last Windows line is on this head; the earlier runs on 0e0286db5c and 7103160ea0, before the fix, are in the last lines.
  • Lean checker (tools/pr_check.py --base 8b1eaff2c7 --pr 5211, this commit against main, Windows): 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), 24e735876a and 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 each. On the GPU, tf-cuda-test kernel-contract-limits zig-out/fatbin gives this head's five tf_ernie55_window_merge lines PASS ... (record current) (24 registers at sm_80, 86 and 89, 40 at sm_120 and 121, 4 static shared bytes, bound 256), and its other lines are 24e735876a's, line for line (191 problems each); tf-cuda-test ernie5_5-index passes gates 1-3 (gate 3 at 12 shapes, each exact by its own check; gate 4 n/a, no sums) with both controls red as planted, and kernel-contract ernie5_5 passes both ERNIE kernels with all four controls red (below). On the same host, zig build test-ernie5_5-host 26 of 28 passed, 2 skipped; test-kernel-contract 46 of 46 steps, 356 of 356 tests; test-gradient-serving 1 of 1.
    • Fail before, on the GPU: this head's contract with 24e735876a's index.cu (the kernel before the fix, rebuilt in this tree): tf-cuda-test ernie5_5-index fails, as it must, at gates 1 and 3: "at 16x120x2048: list entry 10880 is -1, window.merge gives 0", the first entry of row 5 (seq 125, no valid picks, told k + 7); SUMMARY ernie5_5 index: 1 kernels, 0 pass gates 1-4, 1 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed, exit 1.
    • Fail before, on the host (Windows): the new window_test.zig case with 24e735876a's window.zig fails, the merge giving the review's row, [5, -1, 7, 8, 9], and [-1, 4, 8, 9, 10] for the second (25 of 28 passed, 2 skipped, 1 failed); on 55a5d64aa5 test-ernie5_5-host passed 26 of 28, 2 skipped. The new check case (2^63 + 1 rows of 2, k 0, window 1) with 55a5d64aa5's window.zig panics with integer overflow (26 of 29 passed, 2 skipped, 1 crashed); on this head test-ernie5_5-host passes 27 of 29, 2 skipped.
    • Windows, on 55a5d64aa5: the shared-copy and fragment checks (61 local copies, 0 violations; 2 fragments, 18 includes, 0 misses); zig build compile-cuda-tests 12 of 12 steps; test-kernel-contract 46 of 46 steps, 343 of 343 tests; test-gradient-serving 4 of 4 steps; pr_check 0 errors. On this head: zig fmt --check and zig ast-check on the two changed files; zig build test-ernie5_5-host 27 of 29, 2 skipped; pr_check --base 8b1eaff2c7 --pr 5211 0 errors (123 problems on both, 0 new).
    • WSL 2 (x86-64), on 55a5d64aa5: git diff --check clean; zig build compile-cuda-tests 12 of 12 steps; test-ernie5_5-host 26 of 28, 2 skipped; test-kernel-contract 46 of 46 steps, 356 of 356 tests; test-gradient-serving 1 of 1; tools/zig/check_cuda_sources.py on index.cu with clang++ 6 of 6; host_tests_test OK; CI's pytest from the root (-q -rs) 861 passed, 116 skipped, 827 subtests passed, the same skips as main 8b1eaff2c7's (861 passed, 116 skipped, 827 subtests passed).
  • CI on this head: (after the push).
  • CI on 24e735876a: fmt, pytest, zig-macos-dev-tools, release-paths (ubuntu-24.04) and both benchmark-clients jobs passed; zig failed at tools/zig/env_names.py on files this pull request does not touch (docs/environment-names.md: TF_INKLING2_DRAFT_HEAD_Q4 has no row; nine names in zig/src/server/confidential.zig).
  • Earlier, before the fix: on 0e0286db5c (main 77eae824e4), the GB10 contract and limits (22 registers at sm_80, 86 and 89, 40 at sm_120 and 121, 0 static shared bytes) and WSL 2's steps and CI's pytest, the same skips as main's; CI on 7103160ea0 (main 469f1f7fd8) green.
  • 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_window_merge, so no served token or byte can change. On the GB10, against 24e735876a built the same way, the fatbins that differ are ernie5_5_index (its 15 functions: the merge and its two controls at five SMs), contract_controls (one function, tf_ctl_moe_router_tiles_row at sm_89: the router-tiles entries whose compiles give more than one form, cuda: the shared router tiles read their shared-memory rows as 32-bit words #5360), and glm_glue and glm_index, whose functions are all equal.
  • Boxes it reaches: none served yet; it is written for ERNIE 5.5's DSA 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: 55a5d64aa5 on main 8b1eaff2c7 (this head adds the host's check division); Zig 0.17.0, nvcc 13.0.88; one GB10 (sm_121); no checkpoint (synthetic picks).
  • 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_window_merge (new; replaces none), records for sm_80, 86, 89, 120 and 121 in zig/src/core/contract/ernie5_5_records.zig: 24 registers at sm_80, 86 and 89 and 40 at sm_120 and 121, 4 static shared bytes (the block's count of valid picks), bound 256.
  • A planted control for each new kernel, and each one fails as planted: tf_ctl_ernie55_window_merge_all_picks (every row takes all k picks), red at shapes ("at 16x120x2048: list entry 0 is -1, window.merge gives 0"), and tf_ctl_ernie55_window_merge_short_window (the window's oldest position dropped), red at shapes ("list entry 0 is 1, window.merge gives 0").
  • Each backend's SUMMARY line and exit status: SUMMARY ernie5_5 index: 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 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 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 test fails on the merge before the fix and the contract on the kernel before it; 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 (integer lists; no sums).
  • 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 valid-input list construction is clear, and the CUDA byte checks and planted controls cover the normal window and top-k boundaries well.

One malformed-count case still breaks the list's safety invariant. If picked is clamped to k but the valid pick prefix is shorter and already ends in -1, the kernel copies that padding into the live list. For example, picks [5, -1], k=2, picked=7, seq=10, and window 3 produce [5, -1, 7, 8, 9] with count 5. Sparse MLA then treats -1 as a live cache row.

Please make both the CUDA and host merge stop at the valid nonnegative prefix before copying and counting picks. Add fail-before host and CUDA cases where an oversized count exceeds a short valid prefix, and assert that every entry before the count is structurally valid. The current oversized fixture uses a full pick row, and structural checks skip malformed-count rows, so neither catches this case.

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

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

If picked is clamped to k but the valid pick prefix is shorter and already ends in -1, the kernel copies that padding into the live list.

(#5211 (comment))

Thanks, fixed in 55a5d64aa5. It is your 24e735876a with the fix amended, on the same main 8b1eaff2c7; against 24e735876a the diff is the fix alone (6 files, 47 insertions, 21 deletions).

  • Both merges now stop at the valid nonnegative prefix before copying and counting picks. A row's n is the number of picks before its first -1 among the first clamp(picked, 0, k). In index.cu each block finds that index with an atomicMin into one shared int over its held picks; window.zig does the same on the host. The shared count adds 4 static shared bytes and 2 registers at sm_80, 86 and 89 (24; 40 at sm_120 and 121 as before), and the records follow.
  • Host, fail before: your example is the new case in window_test.zig, picks [5, -1], k 2, picked 7, seq 10, window 3, now [5, 7, 8, 9, -1] with count 4, beside a row whose first pick is -1; it asserts that every entry before each count is nonnegative. With 24e735876a's window.zig it fails with your list, [5, -1, 7, 8, 9].
  • CUDA, fail before: the contract plants a second oversized count, k + 7 on a row whose valid picks end in -1 before k, and its structural checks now cover every row, malformed counts included: as many entries as the row's valid picks and window, each a row in [0, seq), ascending to seq - 1, -1 after. With 24e735876a's index.cu rebuilt in this tree, ernie5_5-index fails on the GB10 at gates 1 and 3: "at 16x120x2048: list entry 10880 is -1, window.merge gives 0", the first entry of row 5 (seq 125, no valid picks, told k + 7). On this head ernie5_5-index passes with both controls red as planted, and the five window-merge limits lines are record current.

The receipts are in the description. I left the stack above as it is, for your rebase.

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks, @Bizuayeu. The nonnegative-prefix fix and the new malformed-count host and CUDA cases address the last review in full. One separate host validation bug remains: window.check evaluates rows * w before comparing it with the i32 limit. On a 64-bit host, rows = 2^63 + 1, k = 0, w = 2, and window = 1 overflow that multiplication. Safety-checked builds panic instead of returning WindowShape; wrapping arithmetic can also let the oversized shape pass. Please use rows > maxInt(i32) / w instead. The earlier checks already guarantee w > 0, and please add this trigger as a fail-before host case. No rebase or broader DSA rewrite is needed; the kernel-contract run stays on our side.

  • Synthena

@Bizuayeu

Copy link
Copy Markdown
Contributor Author

window.check evaluates rows * w before comparing it with the i32 limit.

(#5211 (comment))

Thanks, fixed in c07fcb5e32, amended on the same main; against 55a5d64aa5 the diff is these two files, 7 insertions and 1 deletion.

  • window.check now tests rows > maxInt(i32) / w, after the checks that hold w >= k + window >= 1, so nothing is multiplied before the limit.
  • The fail-before host case is your trigger, in window_test.zig: 2^63 + 1 rows on a 64-bit host (written as the top bit of usize plus one, so a 32-bit host wraps too), k = 0, w = 2, window = 1, expecting WindowShape. With 55a5d64aa5's window.zig the test binary panics with integer overflow (26 of 29 passed, 2 skipped, 1 crashed, Windows); with the fix, test-ernie5_5-host passes 27 of 29, 2 skipped. pr_check against main is clean.

The kernel, its contract and records are unchanged from 55a5d64aa5, as you'll run the contract on your side.

Bizuayeu and others added 3 commits November 28, 2026 15:58
…the kernel contract

ERNIE 5.5's DSA layers attend over each row's indexer picks and its 128-position sliding window, laid out by
vLLM-ERNIE's merge_swa_topk as one list a row. tf_ernie55_window_merge (a new ernie5_5_index image) writes that list
for the sparse MLA from topk_rows' out and picked: the row's picks up to picked, held to [0, k] and ending at the
first -1, then positions seq - min(seq, 128) .. seq - 1, then -1, with the row's count. The count comes from picked
rather than from the position, and the picks stop at their first -1, so an entry the MLA reads is never a -1, even
where picked counts past the picks a row holds. window.zig is the host's list; its tests hold it to the vendor's rule
where picked is the indexer's count, and to the stop where picked is past a row's valid picks.

The contract (ernie5_5-index, also in kernel-contract) asks for the host's lists to the byte, and every row's entries
to be rows below seq, ascending to seq - 1 with its picks below the window, from decode rows at the first position
and deep in context, across seq = 128 and seq = k + 128, to prompt chunks and the 1M context's last rows. A negative
picked and two oversized ones, past a row of k picks and past a shorter row, are planted. The two controls, every row
taking all k picks and the window's oldest position dropped, go red. Nothing launches it yet. gradient-serving.tsv
re-pins zig/src/cuda/kernels.zig.
@ashhart
ashhart merged commit 633eb56 into ashhart:main Nov 28, 2026
2 of 12 checks passed
@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