Skip to content

hy4_flash: router, BF16 row helpers, and DFly2 glue CUDA kernels, contracts, and 5-SM limits - #5099

Open
GrgaPalcic wants to merge 5 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-route-rows-dfly2-cuda
Open

GrgaPalcic wants to merge 5 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-route-rows-dfly2-cuda

Conversation

@GrgaPalcic

Copy link
Copy Markdown
Contributor

What this changes

Adds the Hy4-flash bitonic Top-16 router (hy4_route), BF16 TP row helpers (hy4_rows), and DFly2 drafter glue (hy4_dfly2_glue) CUDA kernels, host-compilable launchers, contract gates, 14 planted controls, and 5-SM launch limits inside zig/src/families/hy4_flash/ and zig/kernels/cuda/hy4_flash/:

  • zig/kernels/cuda/hy4_flash/route.cuh + route.cu (hy4_route): multi-warp bitonic Top-16 sigmoid router (tf_hy4_route_m4, tf_hy4_route_m4_plan1) reusing ../common/glue_math.cuh, bit-exact against tf_moe_topk_sigmoid_score_warp on all served shapes and tie-breaking cases.
  • zig/kernels/cuda/hy4_flash/rows.cuh + rows.cu (hy4_rows): 18 production kernels for wide/narrow BF16 row matmuls (hy4_rows_wide, hy4_rows_narrow), lowest-id argmax (hy4_rows_argmax), TP slice/interleave/SwiGLU/splicing helpers, exact BF16 KV cache writers (contiguous and paged — strictly BF16 KV only), Q RoPE / 3D M-RoPE, and ViT patch helpers, reusing ../common/cp_async.cuh and ../common/vec.cuh.
  • zig/kernels/cuda/hy4_flash/dfly2_glue.cuh + dfly2_glue.cu (hy4_dfly2_glue): 6 production s1 + helper kernels (hy4_dfly_dual_rmsnorm_cat_s1, hy4_dfly_fusion_gated_add_s1, hy4_dfly_sublayer_boundary_s1, hy4_dfly_extend_topk_k, hy4_dfly_pick_argmax, hy4_dfly_gather_hidden) reusing ../common/vec.cuh.
  • zig/kernels/cuda/contract_hy4_route_rows_controls.cuh (included from contract_controls_set.cu): 14 planted control kernels in fatbin_contract_controls (zero control symbols in production fatbins).
  • zig/src/families/hy4_flash/route_cuda.zig, rows_cuda.zig, and dfly2_glue_cuda.zig: host-compilable CUDA launchers, registered in zig/src/families/hy4_flash/host_tests.zig and zig/build/cuda.zig.
  • zig/tests/cuda/contract_hy4_route.zig, contract_hy4_rows.zig, contract_hy4_argmax.zig, contract_hy4_sample_rows.zig, and contract_hy4_route_rows.zig (registered in contract_family_sets.zig and contract_limits.zig): full 5-gate CUDA contract coverage and all 14 planted controls.
  • zig/src/core/contract/hy4_route_rows_records.zig (registered in registry_joins.zig): nvcc 13.0 5-SM (sm_80, 86, 89, 120, 121) register/shared-memory/bound records (130 active SM records across 26 kernels).

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

Checks on this head

  • Head commit: e0d71ce1d1c34af097699d486566b2a24155bc49 (base f36acccdd9bd0200080a1d143be01ea83eaa2438)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 164 on base (f36acccdd, inherited), 164 on head (e0d71ce1d), 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: 328 pass, 0 fail, 0 skip (exit 0)
    • zig build -Dfixed-cpu compile-cuda-tests: 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_route (exit 0):
      (filtered) SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems
    • ./zig-out/bin/tf-cuda-test kernel-contract hy4_rows (exit 0):
      (filtered) SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 12 controls: 12 red as planted, 0 missed; 0 limit problems
    • ./zig-out/bin/tf-cuda-test kernel-contract hy4_dfly2 (exit 0): all 6 hy4_dfly2_glue live sm_121 launch limits pass (0 limit problems)
    • ./zig-out/bin/tf-cuda-test kernel-contract-limits zig-out/fatbin: all 130 (26 × 5) hy4_route/hy4_rows/hy4_dfly2_glue 5-SM records reported PASS ... (record current), 0 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 f36acccdd, head e0d71ce1d, 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; bit-exact router/row comparisons, float64 contract exactness, non-finite class preservation, 5-run byte-identical repeatability, shape bounds, FP32 sum discrimination, and 14/14 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 router and row-dot replacement checks, conversion/interleave checks and reported passes are useful. The existing serving paths are unchanged, and the head merges cleanly.

Three fixes remain. zig/tests/cuda/contract_hy4_route_rows.zig:50 exercises seven entries; please cover the remaining row/DFly entries and all plan1 outputs. zig/kernels/cuda/hy4_flash/dfly2_glue.cu:276 also emits twelve split variants, plus the overlay writer elsewhere in that file, beyond the declared resource list; please remove unused exports or qualify every retained one. At zig/tests/cuda/contract_family_sets.zig:88, add ZONOS3 error cleanup before the new fallible setup and a focused failure regression.

Synthetic inputs on GB10 are enough for these operator checks; no checkpoint is needed. Keep the existing passes scoped to their tested entries, and compare exact replacement bytes and every written output before target qualification.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-route-rows-dfly2-cuda to address all three items:

  1. Full live contract coverage for all 26 entries + all plan1 outputs (zig/tests/cuda/contract_hy4_route.zig, contract_hy4_rows_copy.zig, contract_hy4_rows_ops.zig, contract_hy4_dfly2.zig, contract_hy4_route_rows.zig):
    • tf_hy4_route_m4_plan1: now verifies all five written outputs (IDS, W, MEMBERS, ITEMS, COUNTS) byte-for-byte against tf_moe_topk_sigmoid_score_warp + exact host grouped plan (including duplicate-expert-0 runs on -inf rows).
    • hy4_rows (all 18/18 entries): added live contracts for the remaining 13 entries (slice_bf16, interleave2_bf16, slice_f32, comb_swiglu_bf16, kv_write, kv_write_ex, kv_write_paged, q_rope, q_rope_mrope, splice_streams, vit_qkv checking q/k/v, vit_merge_gather, vit_append_newline), including non-identity 256-row page tables + unmapped 0xffff-poisoned pages + 3D M-RoPE + byte-exact comparison vs contiguous kv_write_ex and a wrong-page negative control for hy4_rows_kv_write_paged.
    • hy4_dfly2_glue (all 6/6 retained entries): added live contracts checking every written output (dual_rmsnorm_cat_s1 with direct + indexed/OOB zero-fill rows, fusion_gated_add_s1 checking both res_out and norm_out, sublayer_boundary_s1 checking both ro and no with and without sandwich norm, extend_topk_k checking out, slot_out, and row_pos_out across TOK/LIST and paged/contiguous modes, pick_argmax checking prev_ids and draft_out, and gather_hidden). Also fixed a signed-division compile bug in dfly2_glue_cuda.zig:runExtendTopk.
  2. Removed all 13 unused dfly2_glue.cu exports (zig/kernels/cuda/hy4_flash/dfly2_glue.cu, dfly2_glue.cuh):
    • Removed the 12 unused HY4D_ROW_KERNELS(2,3,4,6) split variants and hy4_dfly_kv_overlay_write_k; hy4_dfly2_glue.fatbin now exports strictly the 6 declared live symbols (cuobjdump --dump-resource-usage verified exact 1-to-1 match across sm_80, sm_86, sm_89, sm_120, sm_121).
  3. ZONOS3 error cleanup + failure regression (zig/tests/cuda/contract_family_sets.zig, zig/build/kernel_contract.zig):
    • Added Sets.initPair (errdefer first.deinit()) protecting zonos3 before hy4_route_rows initialization, plus a host failure regression unit test verifying zonos3.deinit() runs when the subsequent set fails.

Live GB10 (sm_121, nvcc 13.0) verification:

  • tf-cuda-test kernel-contract hy4_route: 2/2 kernels (including all 5 plan1 outputs), 2/2 controls red as planted, 0 limit problems.
  • tf-cuda-test kernel-contract hy4_rows: 18/18 kernels, 13/13 controls red as planted (including kv_write_paged wrong-page control), 0 limit problems.
  • tf-cuda-test kernel-contract hy4_dfly2_glue: 6/6 kernels, 0 limit problems (cuobjdump verified exact 6-symbol export set and all 5-SM register/shared counts match hy4_route_rows_records.zig).
  • Host CI checks (cuda_common_check.py, cuda_fragment_check.py, compile-cuda-tests, test-kernel-contract, test-hy4-flash-host, test-gradient-serving, pr_check.py --base origin/main): all pass (0 violations, 0 misses, 0 new lean findings, 0 errors).

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @GrgaPalcic. All 26 retained entries now have live contracts, plan1 checks all five outputs, the DFly2 export list is reduced to six, and initPair closes the ZONOS3 cleanup ask. Four correctness cases remain.

KV/RoPE is not yet byte-equivalent to the established BF16 path. rows.cu:162-183,264-289 normalizes and applies gain in FP32 before one BF16 conversion and uses unrounded FP32 sincosf results. The established writer rounds normalized values to BF16 before gain and uses BF16 rotary coefficients (mla_project.cu:25-39,53-72). The reduction association and 1/sqrt versus rsqrtf operations also differ. Finite rounding-boundary inputs can therefore produce different cache/Q bytes. The FP64/tolerance contracts do not establish the requested exact replacement bytes; please compare against the established kernels and align this arithmetic before target qualification.

Negative cache sentinels are dereferenced. rows.cu:155-160,300-304,318-322 converts slot_pos[r] into cache addresses without the established slot < 0 return. With -1, contiguous/extended writes use a negative position converted to size_t; the paged writer first indexes page-table entry 0x00ff_ffff. Please restore the guard in all three entries and add a sentinel case that leaves the entire cache and guard pages unchanged.

DFly2 rejects valid physical block zero. dfly2_glue.cu:186-190 requires id > 0, leaving slot_out at pad_slot for an active request mapped to block 0. No ABI reservation makes zero a sentinel, and the sibling paged writer accepts it. The host oracle repeats id > 0 at contract_hy4_dfly2.zig:399-406, while its active fixtures use nonzero blocks. Accept block zero and add an active paged row that addresses it.

Several vector launchers floor unsupported dimensions. rows_cuda.zig:70-90,118-130,251-260,271-294 divides widths by eight or four without remainder checks. For example, sliceBf16 with dst_cols = 9 writes only eight elements; splice, merge and newline paths have analogous truncation, and odd ViT grid dimensions are floored. Reject unsupported dimensions as interleaveBf16 already does, or handle tails, with a remainder case for each public launcher.

After these changes, rerun tf-cuda-test kernel-contract hy4_route, tf-cuda-test kernel-contract hy4_rows, and tf-cuda-test kernel-contract hy4_dfly2_glue on GB10. Require all 26 kernels through gates 1-4, every planted control red, the new exact/sentinel/block-zero/remainder checks passing, and zero limit problems. This head still changes no serving loader or target path, so current main's serving tokens are unaffected; serial token equality remains a later target-qualification requirement.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-route-rows-dfly2-cuda to address all four items:

  1. KV/RoPE byte-equivalence to established hy4_mla_project (zig/kernels/cuda/hy4_flash/rows.cu, zig/tests/cuda/contract_hy4_rows_exact.zig, zig/tests/cuda/contract_hy4_rows_ops.zig):

    • Aligned hy4_rows_kv_write, hy4_rows_kv_write_ex, and hy4_rows_kv_write_paged to match tf_hy4_cache_keys + tf_hy4_rotary_table op-for-op: 128-thread 4-strided FMA accumulation with tf_common::warp_sum + 4-warp tree reduction, rsqrtf, BF16-rounded normalized values before gain, and BF16-rounded sincosf rotary coefficients.
    • Aligned hy4_rows_q_rope and hy4_rows_q_rope_mrope to use BF16-rounded sincosf rotary coefficients matching tf_hy4_rotary_table + tf_hy4_rotate.
    • Loaded cuda.kernels.hy4_mla_project in contract_hy4_rows_exact.zig and asserted exact byte-for-byte equality against tf_hy4_cache_keys, tf_hy4_rotary_table, and tf_hy4_rotate across all shapes.
    • Updated the 3 shifted nvcc 13.0 5-SM records in zig/src/core/contract/hy4_route_rows_records.zig (hy4_rows_kv_write, hy4_rows_kv_write_ex, hy4_rows_kv_write_paged, static shared 16 bytes) from cuobjdump --dump-resource-usage on rebuilt 5-SM hy4_rows.fatbin.
  2. Negative cache sentinel guards (zig/kernels/cuda/hy4_flash/rows.cu, contract_hy4_rows_exact.zig, contract_hy4_rows_ops.zig):

    • Restored if (p < 0) return; / if (sp < 0) return; before any cache or page-table indexing in hy4_rows_kv_write, hy4_rows_kv_write_ex, and hy4_rows_kv_write_paged.
    • Added negative sentinel (-1) tests across all three KV writers into 0xa5-poisoned caches, asserting every cache byte and guard region remains untouched.
  3. DFly2 physical block 0 (zig/kernels/cuda/hy4_flash/dfly2_glue.cu, zig/tests/cuda/contract_hy4_dfly2.zig):

    • Changed if (id > 0) to if (id >= 0) in hy4_dfly_extend_topk_k and its host oracle, and updated the active paged fixture so request 0 addresses physical block 0.
  4. Vector launcher remainder checks (zig/src/families/hy4_flash/rows_cuda.zig):

    • Added error.BadStepShape remainder validation to sliceBf16, interleave2Bf16, sliceF32, spliceStreams, vitMergeGather (including odd gh/gw), and vitAppendNewline, with host unit tests verifying error.BadStepShape for every public vector launcher in test-hy4-flash-host.

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 compile-cuda-tests test-kernel-contract test-hy4-flash-host test-gradient-serving: PASS
  • Live GB10 (sm_121, nvcc 13.0):
    • tf-cuda-test kernel-contract hy4_route: 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 44 ms, limits 0 ms
    • tf-cuda-test kernel-contract hy4_rows: 18 kernels: 18 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems; gates 4575 ms, limits 5 ms
    • tf-cuda-test kernel-contract hy4_dfly2_glue: 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 51 ms, limits 1 ms

@ashhart
ashhart force-pushed the pr/hy4-route-rows-dfly2-cuda branch from d7fe3a4 to a549594 Compare November 26, 2026 16:11
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks @GrgaPalcic. The exact KV/RoPE comparisons, negative-slot guards, physical block-zero case, vector remainder checks, full retained-kernel coverage, six-entry DFly2 scope, and ZONOS3 cleanup are all present in the final source. I found no new source defect.

The remaining step is final CUDA contract qualification on the exact final head. Please run the route, rows, and DFly2 contract filters with all 26 kernels through gates 1-4, every planted control red, the exact/sentinel/block-zero checks passing, and zero limit problems. No serving checkpoint run applies to this kernel-only slice.

We will also run our own device cells for it in its train once the 1.3.6 release is out.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Completed final CUDA contract qualification on the exact rebased head a549594c6 (sm_121 / GB10, nvcc 13.0):

  • tf-cuda-test kernel-contract hy4_route:
    SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 46 ms, limits 0 ms
  • tf-cuda-test kernel-contract hy4_rows:
    SUMMARY 18 kernels: 18 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems; gates 4556 ms, limits 5 ms
  • tf-cuda-test kernel-contract hy4_dfly2_glue:
    SUMMARY 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 52 ms, limits 1 ms

All 26 kernels pass Gates 1–4 (including exact KV/RoPE byte-equality against hy4_mla_project, -1 sentinel guards, and physical block 0), all 15 planted controls are red, and Gate 5 live limits report 0 problems.

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. The exact KV/RoPE comparisons against the established MLA kernels, all three negative-slot guards and their untouched-cache checks, physical block zero, launcher remainder rejection, full retained-kernel coverage, and ZONOS3 cleanup are present on a549594c6. The final helper cleanup also leaves the single guarded initPair in place.

I found no new source defect. Your exact-head GB10 results complete the focused qualification for route, rows, and DFly2: all 26 kernels through Gates 1–4, all 15 planted controls red, and zero limit problems.

This is ready to land and goes into the next trains. Our train cells will retain the three focused contract filters. No serving checkpoint run applies to this kernel-only slice because it adds no serving-model caller or default path.

  • Synthena

@ashhart
ashhart force-pushed the pr/hy4-route-rows-dfly2-cuda branch from a549594 to 8742f99 Compare November 27, 2026 02:36

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