Skip to content

hy4_flash: paged sparse indexer CUDA kernels, contracts, controls, and 5-SM limits - #5095

Open
GrgaPalcic wants to merge 9 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-index-paged-cuda
Open

GrgaPalcic wants to merge 9 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-index-paged-cuda

Conversation

@GrgaPalcic

Copy link
Copy Markdown
Contributor

What this changes

Adds the paged sparse-indexer CUDA kernels, host-compilable launchers, contract gates, planted controls, and 5-SM launch limits for tencent/Hy4-flash-FP8 inside zig/src/families/hy4_flash/ and zig/kernels/cuda/hy4_flash/ (distinct hy4_index_paged fatbin, zero collision with hy4_indexpool or hy4_index_recall):

  • zig/kernels/cuda/hy4_flash/index_paged_common.cuh, index_paged_pool.cuh, index_paged_score.cuh, index_paged_select.cuh, and index_paged.cu: production kernels covering RoPE table fill, paged K mean-pooling (1cta, small, persist), index query M-RoPE, tensor-core paged index scoring (decode, decode_rope, prefill via ../common/qmm_frag.cuh), and deterministic Top-K selection (1cta, scan, merge). Zero planted control symbols in the production fatbin.
  • zig/kernels/cuda/contract_hy4_index_paged_controls.cuh (included from contract_controls_set.cu): 41 planted control kernels in fatbin_contract_controls covering non-finite drops, repeat mutation/drift, tail skips, tie-breaking regressions, and reduced-precision (fp16) accumulation.
  • zig/src/families/hy4_flash/index_paged_cuda.zig: host-compilable CUDA launchers (Ops, paged pool/score/select/RoPE entrypoints + geometry unit tests), registered in zig/src/families/hy4_flash/host_tests.zig and zig/build/cuda.zig.
  • zig/tests/cuda/contract_hy4_index_paged.zig, contract_hy4_index_paged_pool.zig, contract_hy4_index_paged_score.zig, contract_hy4_index_paged_select.zig (registered in contract_family_sets.zig and contract_limits.zig): full 5-gate CUDA contract coverage across all 10 production kernels and all 41 planted controls.
  • zig/src/core/contract/hy4_index_paged_records.zig (registered in registry_joins.zig): nvcc 13.0 5-SM (sm_80, 86, 89, 120, 121) register/shared-memory/bound records (73 active SM records; pool_small_paged and pool_persist_paged marked min_sm = 120).

No served path, model load, or default is changed in this slice. Remaining work for #4795: paged absorbed MLA + fused iHC/HC, router/rows/DFly2 glue CUDA kernel slices, served W8A16 TP=2/TP=4 + DFly2 CUDA Graphs with serial token-equality receipts, and CUDA SigLIP2 --vision + 3D M-RoPE.

Checks on this head

  • Head commit: 852ab8119aad84cb8d629667e10607d7b60eebe9 (base 12535c348897e1fef942310bbf930ccbb3b4d864)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 164 on base (12535c348, inherited), 164 on head (852ab8119), 0 new (python3 tools/pr_check.py --base origin/main: 0 error(s))
  • CUDA static guards:
    • python3 tools/zig/cuda_common_check.py: 55 local copies, 0 violations
    • python3 tools/zig/cuda_fragment_check.py: 2 fragments, 17 includes, 0 misses
  • Host tests (Zig 0.17.0, -Dfixed-cpu, Linux x86_64):
    • zig build -Dfixed-cpu test-hy4-flash-host --summary all: 51 pass, 0 fail, 0 skip (exit 0)
    • zig build -Dfixed-cpu test-contract-images-host --summary all: 6 pass, 0 fail, 0 skip (exit 0)
    • zig build -Dfixed-cpu test-kernel-contract --summary all: 315 pass, 0 fail, 0 skip (exit 0)
    • zig fmt --check build.zig zig && git diff --check: exit 0
  • Device qualification on NVIDIA GB10 (sm_121, CUDA 13.0.88, nvcc 13.0, Zig 0.17.0, -Dsm=80,86,89,120,121):
    • zig build fatbins -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=80,86,89,120,121 and zig build -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=80,86,89,120,121: exit 0
    • ./zig-out/bin/tf-cuda-test kernel-contract hy4_index_paged (exit 0):
      (filtered) SUMMARY 10 kernels: 10 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems; gates 439 ms, limits 7 ms
    • ./zig-out/bin/tf-cuda-test kernel-contract-limits zig-out/fatbin: all 73 active hy4_index_paged 5-SM records reported PASS ... (record current), 0 hy4_index_paged limit problems
  • Not run, and why: real-model serving benchmarks (kernel-only slice; no served model path or default is changed; serial token-equality and throughput receipts follow in the serving slice of Hy4-flash-FP8 (official W8A16) on 2x and 4x DGX Spark: native CUDA support in hy4_flash #4795). Windows/macOS hosts covered by CI.

Receipt

n/a — kernel-only CUDA slice with full 5-gate device contract and 5-SM launch-limit qualification; does not reach serving or change any model load/default.

  • Main against branch: n/a (kernel-only slice; no served path changed)
  • Boxes it reaches: Linux x86_64 host tests, Linux aarch64 + NVIDIA GB10 (sm_121, CUDA 13.0.88) device contract & 5-SM fatbin limits (sm_80,86,89,120,121); macOS/Windows host builds covered by CI
  • New switch? Default, accepted values, profile row: n/a (no switches or environment variables added)
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base 12535c348, head 852ab8119, Zig 0.17.0, CUDA 13.0.88 (nvcc 13.0), NVIDIA GB10 (sm_121, Linux aarch64); targets official tencent/Hy4-flash-FP8 @ 003107fa248f5892064ab1c43af0a7003dd68c9b
  • Paths and platforms the change reaches, which I ran and which I could not: ran Linux host build/tests and live NVIDIA GB10 (sm_121) CUDA contract + 5-SM (80,86,89,120,121) fatbin limit checks; did not run physical SM 80/86/89/120 GPUs (compiled and checked against nvcc 13.0 limits)
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: n/a (kernel-only slice; float64 contract exactness, non-finite class preservation, 5-run byte-identical repeatability, shape bounds, FP32 sum discrimination, and 41/41 red planted controls verified live on sm_121)
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): n/a (kernel-only slice, no served path changed)
  • Decode speed before and after, from tools/bench_openai.py: n/a (kernel-only slice)
  • Prompt speed before and after: n/a (kernel-only slice)
  • Licence of any new weights or drafter, or "n/a": n/a (no weights or drafter bundled)

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.
  • 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.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @GrgaPalcic. The separate image keeps the current serving paths unchanged, and this head merges cleanly. The reported ten cases, 41 controls and 73 resource checks are useful; the existing pool/score byte comparisons and selection oracle are retained.

One coverage change is needed at zig/tests/cuda/contract_hy4_index_paged.zig:120. The numerical set chooses unpaged pool/score handles, so it never launches the six _paged entries or supplies a page table. Please add those cases with nonidentity pages, stream/group offsets and page-boundary tails, compare gathered keys/scores exactly to equivalent contiguous inputs, and include a wrong-page negative. Resource records alone cannot catch a page-address error. Synthetic inputs are enough; no checkpoint is needed for those tests.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-index-paged-cuda to address all items:

  1. Live numerical contracts for all 6 _paged entries (zig/tests/cuda/contract_hy4_index_paged_paging.zig):
    • Exercises tf_hy4_idx_pool_1cta_paged, tf_hy4_idx_pool_small_paged, tf_hy4_idx_pool_persist_paged, tf_hy4_idx_score_decode_paged, tf_hy4_idx_score_decode_rope_paged, and tf_hy4_idx_score_prefill_paged.
    • Uses 2 streams, non-zero group offsets (g_base = 59 + JG), deterministic non-identity physical page tables with 2 unmapped 0xffff-poisoned physical pages, and page-boundary tails (63, 64, 65, 129, 257), checking exact byte-for-byte equality against the equivalent contiguous hy4_index_paged launches (including untouched unmapped pages and mixed finite/non-finite propagation).
    • Adds 2 wrong-page negative controls (tf_hy4_idx_pool_1cta_paged wrong page table and tf_hy4_idx_score_decode_paged wrong page table), both verified red at Gate 3 (shapes).
  2. ZONOS3 error cleanup + failure regression (zig/tests/cuda/contract_family_sets.zig):
    • Keeps errdefer zonos3.deinit() via Sets.initPair before hy4_index_paged initialization with a host unit test verifying zonos3.deinit() runs if the next set fails.
  3. test-gradient-serving pin (zig/tests/fixtures/gradient-serving.tsv):
    • Updated the zig/src/cuda/kernels.zig hash pin for registering hy4_index_paged.

Live GB10 (sm_121) verification:

  • tf-cuda-test kernel-contract hy4_index_paged: 16/16 kernels pass Gates 1–4, 43/43 controls red as planted (0 missed), 0 limit problems (gates 484 ms, limits 7 ms).
  • Host CI checks (compile-cuda-tests, test-kernel-contract, test-hy4-flash-host, test-gradient-serving, pr_check.py --base origin/main): all pass (0 new lean findings, 0 errors).

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @GrgaPalcic. The new paged numerical set now exercises all six _paged entries with nonidentity physical pages, stream/group offsets, page tails, contiguous byte comparisons, and wrong-page controls. The initPair cleanup and failure regression also close the ZONOS3 initialization ask.

One selection issue remains. In zig/kernels/cuda/hy4_flash/index_paged_select.cuh:234-244, emit_selected expands every selected group to all four token IDs, including the row's own group at ROW_POS[r] / 4. At position 2048, for example, the current-group output is 2048, 2049, 2050, 2051; only 2048 is visible to that query. The established CUDA recall path limits this suffix with position % 4 + 1 (hy4_flash/index_recall.cu:51-55), and direct consumption would reject the future IDs in attention_slots.zig:28-39.

The new oracle repeats the same expansion at zig/tests/cuda/contract_hy4_index_paged_select.zig:77-80, so its existing four position residues currently certify the noncausal output rather than catch it. Please pass the row position into expansion, retain all four tokens for closed groups, and emit only offsets through ROW_POS[r] % 4 for the current group, with -1 in the remaining fixed-capacity slots. Update the host oracle to the same rule; its existing residue coverage should then fail before the kernel fix and pass after it without weakening any other comparison.

After that correction, the focused qualification is tf-cuda-test kernel-contract hy4_index_paged on GB10/sm_121: all 16 kernels must pass gates 1-4, all 43 controls must remain red as planted, and no launch-limit problem may appear. This is still a kernel-only slice with no serving checkpoint A/B.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-index-paged-cuda to fix the causal current-group token expansion:

  1. Causal current-group expansion (zig/kernels/cuda/hy4_flash/index_paged_select.cuh):

    • Passed pos = ROW_POS[r] into emit_selected across both select_1cta_body and merge_body (fast and main paths).
    • Closed groups (g < pos / 4) retain all 4 token IDs; the current group (g == pos / 4) emits only offsets 0 .. pos % 4 (4 * g + (e & 3) <= pos), writing -1 to remaining fixed-capacity slots (matching hy4_flash/index_recall.cu:51-55).
  2. Host oracle & 5-SM records (zig/tests/cuda/contract_hy4_index_paged_select.zig, zig/src/core/contract/hy4_index_paged_records.zig):

    • Updated verifyOutputs to take pos: []const i32 and apply the exact same want >= 0 and tok_id <= pos[r] causal rule across all four position residues (r % 4).
    • Updated nvcc 13.0 5-SM register records from rebuilt hy4_index_paged.fatbin (cuobjdump --dump-resource-usage) for tf_hy4_idx_select_1cta (45, 46, 46, 60, 60) and tf_hy4_idx_select_merge (57, 62, 62, 62, 62).

Verification receipts

  • Local:
    • python3 tools/zig/cuda_common_check.py: 55 local copies, 0 violations
    • python3 tools/zig/cuda_fragment_check.py: 2 fragments, 17 includes, 0 misses
    • python3 tools/pr_check.py --base origin/main: lean 164 vs 164, 0 new; 0 error(s)
    • zig build -Dfixed-cpu test-kernel-contract test-hy4-flash-host test-gradient-serving: PASS
  • Live GB10 (sm_121, nvcc 13.0):
    • tf-cuda-test kernel-contract hy4_index_paged: 16 kernels: 16 pass gates 1-4, 0 fail, 0 known findings; 43 controls: 43 red as planted, 0 missed; 0 limit problems; gates 484 ms, limits 7 ms

@ashhart
ashhart force-pushed the pr/hy4-index-paged-cuda branch from 8f5f1d8 to b99895c Compare November 26, 2026 13:00
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks, @GrgaPalcic. The updated head addresses the earlier review asks. All six paged entries have nonidentity-page numerical contracts and wrong-page controls; ZONOS3 cleanup has its failure regression; and both selection paths now clamp the current group at the row position. The updated causal oracle would catch the old future-token expansion in both one-CTA and scan/merge paths.

My recheck verdict is CHANGES because I found two new correctness issues:

  • zig/kernels/cuda/hy4_flash/index_paged_select.cuh:21-29 does not canonicalize negative NaNs. For example, 0xffc00000 becomes key 0x003fffff, below +Inf, although the contract and host oracle require every NaN above infinity. This affects direct, scan, and merge selection. After the intentional kBadNan branch, please return 0xFFFFFFFFu for any remaining NaN, leaving all finite and infinite keys unchanged.
  • Paged pooling writes logical group g at unified slot g_base + g (index_paged_pool.cuh:49-56), but all three paged score variants gather from slot p without that base (index_paged_score.cuh:172-188). ScoreCall and the paged wrappers have no g_base, so the current zero-base score fixtures miss the producer/consumer mismatch. Please thread the base through scoring and add a pool-to-score case with g_base=59, nonidentity pages crossing 63/64, and exact contiguous-output equality for decode, decode-RoPE, and prefill.

Two integration/test changes also remain:

  • Static merge analysis against current main conflicts in zig/tests/cuda/contract_family_sets.zig, zig/tests/cuda/contract_limits.zig, and zig/tests/fixtures/gradient-serving.tsv. Please add one fresh main adaptation preserving both registration lists and all cleanup guards, including main's sparse_attn set, then regenerate the kernels.zig pin from the merged tree.
  • The special page-table fallback for phys_pages % 5 == 0 at zig/tests/cuda/contract_hy4_index_paged_paging.zig:31-33 has no numerical cell. Existing shapes produce 6, 8, or 12 physical pages. Please add an n=193 score case: four pages per stream produce 10 physical pages and exercise the fallback in all three paged score entries.

After those corrections, the focused GB10/sm_121 qualification is tf-cuda-test kernel-contract hy4_index_paged: all 16 numerical kernels through gates 1–4, all 43 controls red as planted, and zero launch-limit problems. Retain the negative-NaN selection case, run the new nonzero-base composition and n=193 cells, and regenerate five-SM resource records if the NaN fix changes measured usage. This remains a kernel-only slice, so no serving checkpoint A/B applies.

This was a read-only static recheck; I did not run builds, tests, shaders, or devices.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-index-paged-cuda (9135cc6d0) to address both correctness findings, the n=193 page-table fallback cell, and current main integration:

  • Negative NaN canonicalization (index_paged_select.cuh): after the kBadNan control branch in order_key, any remaining NaN (s != s) now returns 0xFFFFFFFFu directly so negative NaNs (e.g. 0xffc00000) sort above +Inf across direct, scan, and merge selection while leaving finite and infinite keys unchanged.
  • Unified g_base threading (index_paged_score.cuh, index_paged.cu, index_paged_cuda.zig): added g_base to score_rows, all three paged score entry points (score_decode_paged, score_decode_rope_paged, score_prefill_paged), and ScoreCall, gathering from g_base + p to match paged pooling's unified slot layout.
  • Pool-to-score composition & n=193 fallback (contract_hy4_index_paged_paging.zig, contract_hy4_index_paged.zig):
    • Added checkPoolToScore running tf_hy4_idx_pool_1cta_paged (g_base=59, non-identity page table crossing 63/64) into all three paged score variants (decode, decode_rope, prefill) and verifying exact byte-for-byte equality against contiguous pool + score.
    • Added .{ .rows = 8, .n = 193, .k = 128 } to score_paged_served (4 pages/stream -> 10 physical pages, exercising the phys_pages % 5 == 0 bijection fallback across all three paged score entries).
  • 5-SM resource records (hy4_index_paged_records.zig): refreshed nvcc 13.0 5-SM counts from cuobjdump for tf_hy4_idx_select_1cta (32, 50, 50, 54, 56), tf_hy4_idx_select_scan (47, 47, 47, 48, 48), and tf_hy4_idx_select_merge (56, 62, 62, 62, 61); all other entries unchanged.
  • Current main integration: merged origin/main (eff4e5b75), keeping sparse_attn, tfq3/tfq3_moe, and cleanup guards in contract_family_sets.zig, and refreshed the kernels.zig pin in gradient-serving.tsv.

Live GB10 (sm_121, nvcc 13.0) qualification on exact head 9135cc6d0:

  • tf-cuda-test kernel-contract hy4_index_paged:
    SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 0 known findings; 43 controls: 43 red as planted, 0 missed; 0 limit problems; gates 497 ms, limits 7 ms

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. This head closes every remaining ask: negative NaNs canonicalize above infinity across every selection path, g_base reaches all three paged scores, and the exact nonzero-base pool-to-score composition covers all three variants with the corrected row count. The n=193 case exercises the ten-page fallback everywhere required, and causal expansion remains correct in direct and scan/merge paths.

Current-main registrations and cleanup are preserved, the registry pin matches, and the select resource records are refreshed. I found no new source defect. Your exact-head GB10 receipt completes the focused qualification.

This is ready to land and goes into the next trains. No serving checkpoint run applies to this kernel-only slice.

  • Synthena

@ashhart
ashhart force-pushed the pr/hy4-index-paged-cuda branch 2 times, most recently from d5146c4 to aaee2bb Compare November 27, 2026 02:41
@ashhart
ashhart force-pushed the pr/hy4-index-paged-cuda branch from aaee2bb to 01c412b Compare November 27, 2026 06:11

This branch has not been deployed

No deployments
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