Skip to content

hy4_flash: paged absorbed MLA + C1/C2 CUDA kernels, contracts, and 5-SM limits - #5109

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

GrgaPalcic wants to merge 8 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-mla-paged-cuda

Conversation

@GrgaPalcic

Copy link
Copy Markdown
Contributor

Refs #4795.

Summary

Adds the paged absorbed MLA + C1/C2 tensor-core CUDA kernel slice (Slice 2d-1 of #4795) for Hy4-flash (strictly bit-identical BF16 KV cache only; no lossy KV formats):

  • Three CUDA fatbin images (zig/kernels/cuda/hy4_flash/):
    • mla_paged.cu (hy4_mla_paged, 5 kernels): tf_hy4_mla_absorb, tf_hy4_mla_attn, tf_hy4_mla_attn_rows, tf_hy4_mla_value, tf_hy4_mla_post.
    • mla_attn_paged.cu (hy4_mla_attn_paged, 7 live 2-plane C1 kernels): hy4_mla_dec_x16, hy4_mla_dec_x16_rows, hy4_mla_dec_x16_qreg, hy4_mla_dec_x16_qreg_rows, hy4_mla_fold, hy4_mla_dec_x16_paged, hy4_mla_dec_x16_qreg_paged (all unused legacy symbols removed).
    • mla_c2.cu / mla_c2.cuh (hy4_mla_c2, 7 live C2 kernels reusing ../common/qmm_frag.cuh): hy4c2_absorb_d1_x, hy4c2_absorb_d2_x, hy4c2_absorb_p_x, hy4c2_expand_d1_f32lat, hy4c2_expand_d2_f32lat, hy4c2_expand_d4k64s2_f32lat, hy4c2_expand_p_f32lat (all unused symbols removed).
  • Zig launchers (zig/src/families/hy4_flash/):
    • mla_paged_cuda.zig, mla_c1_cuda.zig, mla_c2_cuda.zig (single root module hy4_mla_paged_cuda in zig/build/cuda.zig).
  • Live numerical contracts covering all 19/19 kernels + 21 controls (zig/tests/cuda/):
    • contract_hy4_mla_paged.zig + contract_hy4_mla_c1_c2.zig (wired via contract_family_sets.zig with Sets.initPair ZONOS3 errdefer cleanup and host failure regression).
    • hy4_mla_paged: all 5 kernels (10 cases across decode/prefill/per-row) + 20 planted controls (nonfinite, repeat, shapes, sums).
    • hy4_mla_attn_paged: all 7 C1 kernels (x16, x16_rows, x16_qreg, x16_qreg_rows, x16_paged, x16_qreg_paged, fold) with 2 streams, deterministic non-identity 256-row page tables + 2 unmapped NaN-poisoned physical pages, page-boundary tails (255, 256, 257, 513), exact byte equality against equivalent contiguous launches, plus a wrong-page negative control (hy4_mla_dec_x16_paged wrong page table, red at Gate 3).
    • hy4_mla_c2: all 7 C2 kernels (absorb_d1_x/d2_x/p_x exact 2-plane vs float64 W_UK_T, and expand_d1/d2/d4k64s2/p_f32lat + sigmoid gate vs float64 bound).
  • 5-SM resource records & CI pins:
    • zig/src/core/contract/hy4_mla_paged_records.zig: nvcc 13.0 records for all 19 live launches across sm_80, sm_86, sm_89, sm_120, sm_121 (95/95 records).
    • zig/tests/fixtures/gradient-serving.tsv: updated zig/src/cuda/kernels.zig hash pin.

Verification

  • Live GB10 (sm_121, nvcc 13.0):
    • tf-cuda-test kernel-contract hy4_mla_paged: 10/10 kernels pass Gates 1–4, 20/20 controls red as planted (0 missed), 0 limit problems.
    • tf-cuda-test kernel-contract hy4_mla_attn_paged: 6/6 C1 cases (all 7 symbols) pass Gates 1–4, 1/1 wrong-page control red as planted (0 missed), 0 limit problems.
    • tf-cuda-test kernel-contract hy4_mla_c2: 2/2 C2 cases (all 7 symbols, worst 0.913 of float64 bound) pass Gates 1–4, 0 limit problems.
    • tf-cuda-test kernel-contract-limits across sm_80, sm_86, sm_89, sm_120, sm_121: 95/95 (record current), 0 stale.
  • Host / CI checks:
    • python3 tools/zig/cuda_common_check.py: 0 violations.
    • python3 tools/zig/cuda_fragment_check.py: 0 misses.
    • zig build -Dfixed-cpu test-hy4-flash-host test-gradient-serving test-kernel-contract: all pass.
    • python3 tools/pr_check.py --base origin/main: 0 new lean findings, 0 errors.

Serving engine wiring (hy4_flash runtime + DFly2 CUDA graphs + 2×/4× Spark receipts) remains deferred to Slice 3; existing serving paths are unchanged.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. The 19 live kernels have launchers and registrations. The paged C1 tests use real nonidentity page tables, two streams, padded strides, page-boundary tails and a wrong-page negative, with contiguous-output byte comparisons. Existing serving paths are untouched, and the patch merges cleanly on current main.

Three fixes remain:

  • zig/src/families/hy4_flash/mla_c1_cuda.zig:133 always passes a null indexed-key list. A kind == 1 row at position 2048 or later dereferences it in the ordinary/paged and qreg kernels; the per-row form can skip that row. Please add the list pointer to Keys, forward it, and cover the long indexed-list path for ordinary/qreg, paged and per-row launches.
  • zig/tests/cuda/contract_hy4_mla_c1_c2.zig:440-448,565-567 checks C2 only against float64 tolerances. Please add same-engine byte comparisons for the absorb and expand variants, so changed rounding or BF16 bytes cannot pass. The existing bounds can remain as extra checks.
  • The added trailing comments at zig/kernels/cuda/hy4_flash/mla_attn_paged.cuh:89 and zig/kernels/cuda/hy4_flash/mla_c2.cuh:283 are 124 columns. Please shorten them or move them to their own preceding lines.

After those fixes, the outstanding checks are the GB10 hy4_mla_attn_paged contract with the new long-list cases and hy4_mla_c2 with byte comparisons. No serving checkpoint run is needed because this slice does not wire serving.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

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

  1. Forward list in mla_c1_cuda.zig and cover the long indexed-list path (kind == 1, pos >= 2048):

    • Added list: u64 = 0 to Keys in zig/src/families/hy4_flash/mla_c1_cuda.zig and forwarded k.list to hy4_mla_dec_x16* instead of passing 0.
    • Added a long-context shape (k = 2120, pos >= 2048, 512 non-monotonic group slots + -1 sentinels) in zig/tests/cuda/contract_hy4_mla_c1_c2.zig covering all 6 C1 launches (hy4_mla_dec_x16, hy4_mla_dec_x16_rows, hy4_mla_dec_x16_qreg, hy4_mla_dec_x16_qreg_rows, hy4_mla_dec_x16_paged, and hy4_mla_dec_x16_qreg_paged).
  2. Same-engine byte comparisons for C2 absorb and expand (zig/src/families/hy4_flash/mla_c2_cuda.zig, zig/tests/cuda/contract_hy4_mla_c2.zig):

    • Added absorbVariant / expandVariant helpers in mla_c2_cuda.zig and split the C2 contracts into contract_hy4_mla_c2.zig so every file stays well under 600 lines.
    • C2Absorb now checks exact byte-for-byte equality across all 3 absorb variants (d1_x == d2_x == p_x) plus multi-row vs R = 1 d1_x byte invariance, while keeping the existing float64 2-plane bound and non-finite checks.
    • C2Expand now checks exact byte-for-byte equality across all 4 expand variants (d1 == d2 == d4k64s2 == p_f32lat) plus multi-row vs R = 1 d1 byte invariance, while keeping the existing float64 reference, bounds, and non-finite checks.
  3. Trailing comments under 120 columns (zig/kernels/cuda/hy4_flash/mla_attn_paged.cuh, mla_c2.cuh):

    • Removed the unused kt32/h32/prefill_rows local declarations (and 124-col comment) in mla_attn_paged.cuh and wrapped the 124-col comment in mla_c2.cuh.

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_mla_attn_paged: 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 225 ms, limits 2 ms
    • tf-cuda-test kernel-contract hy4_mla_c2: 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 101 ms, limits 1 ms

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you for forwarding the C1 list, adding long indexed rows and same-engine byte comparisons, and shortening the two comments. The launcher and list forwarding changes are confirmed, and the branch merges cleanly on current main.

Two test-oracle issues remain:

  1. zig/tests/cuda/contract_hy4_mla_c1_c2.zig:295-323 adds the long-list float64 reference, but Gate 3 never evaluates it. Ordinary/qreg cases return through the custom hook, while paged/per-row cases stop after contiguous byte replacement. Please route all six production C1 cases through an explicit shapes check that calls the existing reference and bounds, while retaining the contiguous byte assertions and wrong-page control.
  2. zig/tests/cuda/contract_hy4_mla_c2.zig:277-281,290-294 reuses cmp and solo without clearing them between expand launches. Stale correct bytes can hide an unwritten output region. Please clear or poison each buffer immediately before every variant and R=1 launch, as the absorb checks already do.

After those changes, please rerun the two existing CUDA synthetic contract suites for paged C1 and C2 on the final head. No serving checkpoint run applies to this slice. No rebase is requested.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-mla-paged-cuda to address both test-oracle items:

  1. Explicit float64 reference() + bounds() evaluation on all 6 production C1 cases (zig/tests/cuda/contract_hy4_mla_c1_c2.zig):

    • Removed .replaces on C1Case.kernel so Gate 3 routes all 6 production C1 cases (x16, x16_rows, x16_qreg, x16_qreg_rows, x16_paged, x16_qreg_paged) through C1Case.check(.shapes).
    • C1Case.check retains the contiguous byte-equality comparison (with 0xff-poisoned base_dev before each baseline launch) for paged/per-row cases and the wrong-page negative control, and then evaluates self.reference(want, mag) + self.bounds(want, mag, allow) against every output across all shapes including the k = 2120 long indexed-list path.
  2. Poison cmp and solo before every C2 expand launch (zig/tests/cuda/contract_hy4_mla_c2.zig):

    • Added try cmp.fill8(0xff, self.rig.s.handle) before every variant launch (d1, d2, d4k64s2, p) and try solo.fill8(0xff, self.rig.s.handle) before every R = 1 row-invariance launch, matching C2Absorb.

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_mla_attn_paged: 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 3125 ms, limits 1 ms
    • tf-cuda-test kernel-contract hy4_mla_c2: 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 102 ms, limits 1 ms

@ashhart
ashhart force-pushed the pr/hy4-mla-paged-cuda branch from 08f4f66 to 532e484 Compare November 26, 2026 13:03
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks @GrgaPalcic for the careful updates. The indexed-list forwarding, all six long-list C1 reference-and-bounds checks, contiguous byte comparisons, wrong-page control, C2 variant comparisons, comment fixes, and immediate expand-buffer poisoning are all addressed in the final source.

One correctness issue remains in C2 absorb. All three live absorb entry points use the null-CSv path. That path writes the low roped plane but never writes the corresponding high roped columns; the main projection stores do not cover them. The contract currently zeroes the baseline, variant, and single-row buffers, so it accepts those unwritten bytes as zeros.

Please write a deterministic high roped plane for every valid row in this path—zeroing it is the smallest change consistent with the current contract—and poison the absorb baseline, comparison, and single-row buffers with 0xff instead of zero. That makes the revised test fail on the unwritten-output kernel and pass after the fix.

After that change, the remaining evidence is the two final CUDA synthetic contract runs for paged C1 and C2. No serving checkpoint run applies to this slice. I reviewed source only and did not build or run the PR.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-mla-paged-cuda (5cf8321d5) to address the C2 absorb roped-plane finding and merge current main:

  • Null-CSv hi roped plane (mla_c2.cuh): absorb_body now zeroes both the hi (QAv + o) and lo (QAv + qlo + o) roped planes on cb == 0 when CSv == nullptr && !OUT32, leaving no unwritten output bytes.
  • Buffer poisoning (contract_hy4_mla_c2.zig): C2Absorb now poisons the baseline (out_dev), variant comparison (cmp), and single-row (solo) output buffers with 0xff before every launch.
  • 5-SM resource records (hy4_mla_paged_records.zig): refreshed nvcc 13.0 5-SM counts from cuobjdump for hy4c2_absorb_d1_x (82, 82, 82, 78, 78) and hy4c2_absorb_d2_x (92, 92, 92, 88, 88); all other entries unchanged.
  • Current main integration: merged origin/main (eff4e5b75), keeping sparse_attn, tfq3/tfq3_moe, and reverse-order cleanup 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 5cf8321d5:

  • tf-cuda-test kernel-contract hy4_mla_attn_paged:
    SUMMARY 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 3124 ms, limits 1 ms
  • tf-cuda-test kernel-contract hy4_mla_c2:
    SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 103 ms, limits 1 ms

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. The final head writes deterministic zeroes to both null-CSv roped planes for every valid row, and all absorb baseline, variant, and single-row buffers are poisoned with 0xff before launch. The six C1 reference-and-bounds paths, contiguous byte comparisons, wrong-page control, and C2 expand poisoning and byte comparisons remain intact.

The refreshed absorb resource records, kernel hash pin, registrations, and current-main integration also look correct. I found no new source defect. Your exact-head GB10 runs complete the remaining paged-C1 and C2 synthetic qualification.

This is ready to land and goes into the next trains. No serving checkpoint run applies to this kernel-only slice. I reviewed source only and did not build or run the PR.

  • Synthena

@ashhart
ashhart force-pushed the pr/hy4-mla-paged-cuda branch 3 times, most recently from dec357f to 9ac1b55 Compare November 27, 2026 06:17
@ashhart
ashhart force-pushed the pr/hy4-mla-paged-cuda branch from 9ac1b55 to 4ec4b07 Compare November 27, 2026 09:32

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