Skip to content

hy4_flash: fused iHC and HC CUDA kernels, contracts, controls, and 5-SM limits - #5100

Merged
ashhart merged 4 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-ihc-hc-cuda
Nov 26, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-ihc-hc-cuda

Conversation

@GrgaPalcic

Copy link
Copy Markdown
Contributor

What this changes

Adds the Hy4-flash fused iHC (hy4_ihc_fused) and HC (hy4_hc) CUDA kernels, host-compilable launchers, contract gates, 13 planted controls, and 5-SM launch limits inside zig/src/families/hy4_flash/ and zig/kernels/cuda/hy4_flash/ (distinct hy4_hc and hy4_ihc_fused fatbins, zero collision with hy4_ihc):

  • zig/kernels/cuda/hy4_flash/hc.cuh + hc.cu (hy4_hc): 3 production kernels (hy4_hc_pre, hy4_hc_head, hy4_hc_merge) in IEEE FP32 (--fmad=false --ftz=false --prec-div=true --prec-sqrt=true) reusing ../common/reduce.cuh and ../common/glue_math.cuh. Zero planted control symbols in the production fatbin.
  • zig/kernels/cuda/hy4_flash/ihc_fused.cuh, ihc_fused_coop_prefill.cuh, and ihc_fused.cu (hy4_ihc_fused): 15 production kernels covering 1-launch ticket, cooperative, tiled, and batched prefill iHC boundaries (boundary, entry, epilogue_head, head_only, post_only, post_vec, pre_tiled_rb2/rb4, head_tiled, prefill_m8/m4), reusing ../common/cp_async.cuh, ../common/reduce.cuh, and ../common/vec.cuh.
  • zig/kernels/cuda/contract_hy4_hc_controls.cuh (included from contract_controls_set.cu): 13 planted control kernels in fatbin_contract_controls covering non-finite drops, repeat drift, tail skips, and reduced-precision (bf16) sums.
  • zig/src/families/hy4_flash/hc_cuda.zig and ihc_fused_cuda.zig: host-compilable CUDA launchers with shared-memory budget unit tests, registered in zig/src/families/hy4_flash/host_tests.zig and zig/build/cuda.zig.
  • zig/tests/cuda/contract_hy4_hc.zig and contract_hy4_ihc_fused.zig (registered in contract_family_sets.zig and contract_limits.zig): full 5-gate CUDA contract coverage across all 4 hy4_hc outputs, all 5 hy4_ihc_fused boundary modes (0-bit row invariance across launch arms), and all 13 planted controls.
  • zig/src/core/contract/hy4_ihc_hc_records.zig (registered in registry_joins.zig): nvcc 13.0 5-SM (sm_80, 86, 89, 120, 121) register/shared-memory/bound records (90 active SM records across 18 kernels).

No served path, model load, or default is changed in this slice. Remaining work for #4795: paged absorbed MLA CUDA kernel slice, 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: 625a0e4ace487af8610e8028242a55022517fffd (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 (625a0e4ac), 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: 50 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_hc (exit 0):
      (filtered) SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems
    • ./zig-out/bin/tf-cuda-test kernel-contract hy4_ihc_fused (exit 0):
      (filtered) SUMMARY 5 kernels: 5 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-limits zig-out/fatbin: all 90 (18 × 5) hy4_hc/hy4_ihc_fused 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 625a0e4ac, 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, 0-bit row invariance across launch arms, non-finite class preservation, 5-run byte-identical repeatability, shape bounds, FP32 sum discrimination, and 13/13 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. All 18 production entries are reachable and recorded, and the four HC outputs have separate cases. The fixture-pin update matches the loader source; kernel/test code is unchanged from the reported run. The head merges cleanly and serving still uses the existing path.

Four fixes remain:

  • At zig/kernels/cuda/hy4_flash/hc.cuh:66, separate multiply/add changes the existing FMA dot's bytes. Please preserve the required arithmetic and compare every output to the existing serial operations.
  • At zig/tests/cuda/contract_hy4_ihc_fused.zig:190, the fused checks omit updated X and post_next. Please compare those too, including post aliasing and both input-rounding modes.
  • At zig/src/core/contract/hy4_ihc_hc_records.zig:22, both head ticket records need 34,624 dynamic bytes for rows 17–64, rather than 30,288.
  • At zig/tests/cuda/contract_family_sets.zig:88, protect ZONOS3 before the new fallible setup and add a focused failure regression.

Keep the reported passes within their tested scope. Synthetic GB10 inputs are enough for these operator repairs; no checkpoint is needed.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-ihc-hc-cuda to address all four items:

  1. hy4_hc exact FMA arithmetic and serial byte-equality (zig/kernels/cuda/hy4_hc.cuh, zig/tests/cuda/contract_hy4_hc.zig):

    • Restored exact FMA dot accumulation (fmaf) plus rsqrtf and 1.0f / (1.0f + expf(-...)) in hy4_hc.cuh to match tf_hy4_ihc_collapse and tf_hy4_ihc_add in ihc.cu.
    • Updated contract_hy4_hc.zig to load cuda.kernels.hy4_ihc (tf_hy4_ihc_collapse and tf_hy4_ihc_add) and assert exact byte-for-byte equality on every written output (hy4_hc_pre collapsed output + post gates, hy4_hc_head, and hy4_hc_merge) in addition to the float64 bounds, non-finite propagation, and all 11 planted controls.
    • Updated the nvcc 13.0 5-SM register records in zig/src/core/contract/hy4_ihc_hc_records.zig for hy4_hc_pre (sm_120/sm_121: 80 -> 78) and hy4_hc_head (sm_80: 48 -> 47) from the rebuilt 5-SM fatbins.
  2. hy4_ihc_fused full output, aliasing, and rounding-mode coverage (zig/tests/cuda/contract_hy4_ihc_fused.zig):

    • Verified updated X' and post_next alongside primary output a across all 5 modes (boundary, entry, epilogue_head, post_only, head_only), including R=1 solo row-invariance byte equality on a, X', and post_next.
    • Added explicit post_next == post_prev aliasing byte-equality coverage in boundary mode (verifying a, X', and post_next match the non-aliased launch byte-for-byte).
    • Added round_in = false coverage alongside round_in = true across all post-branch modes (boundary, epilogue_head, post_only).
  3. Dynamic shared memory records for head ticket launches (zig/src/core/contract/hy4_ihc_hc_records.zig, zig/src/families/hy4_flash/ihc_fused_cuda.zig):

    • Updated hy4_ihc_epilogue_head and hy4_ihc_head_only dynamic shared memory records from 30288 to 34624 bytes to cover the rows 17..64 ticketSmem(2, 4, 4) launch path, and added a host unit test asserting ticketSmem(2, 4, 4) == 34624.
  4. Contract set teardown safety (zig/tests/cuda/contract_family_sets.zig, zig/build/kernel_contract.zig):

    • Wrapped zonos3 and hy4_hc initialization in Sets.initPair (errdefer first.deinit()) so zonos3 is cleaned up if hy4_hc initialization fails, with a focused host failure regression test wired into test-kernel-contract.

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_hc: 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems; gates 131 ms, limits 0 ms
    • tf-cuda-test kernel-contract hy4_ihc_fused: 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 374 ms, limits 5 ms
    • 5-SM nvcc 13.0 (sm_80,86,89,120,121) resource usage checked via cuobjdump --dump-resource-usage on rebuilt hy4_hc.fatbin and live sm_121 limits across all 18 entries: 0 limit problems.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you for the four focused repairs.

The HC kernel now matches the serial IHC arithmetic, including explicit FMA operations, and the contracts compare every collapsed, post-gate, merge, primary-output, updated-X and solo-row output byte. The fused-mode, aliasing and round_in=false coverage is present. Both ticket/cooperative records now account for 34,624 dynamic bytes, and the failure path after the ZONOS3 setup releases the first initialized owner exactly once.

Your current-head synthetic contract and resource results cover the requested cells. I found no remaining correctness issue. This merges cleanly on current main and goes into our next landing train. No additional device or checkpoint run is needed for this kernel-only slice.

  • Synthena

@ashhart
ashhart merged commit 920fe22 into ashhart:main Nov 26, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. 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