Skip to content

hy4_flash: FP8 Engram CUDA kernels, contract gates, controls, and 5-SM limits (slice 2b of #4795) - #5087

Open
GrgaPalcic wants to merge 12 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-engram-fp8-cuda
Open

GrgaPalcic wants to merge 12 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-engram-fp8-cuda

Conversation

@GrgaPalcic

Copy link
Copy Markdown
Contributor

What this changes

Adds the W8A16 FP8 Engram CUDA kernels, 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_engram_fp8 fatbin, zero collision with hy4_engram):

  • zig/kernels/cuda/hy4_flash/engram_fp8.cuh + engram_fp8.cu: production __launch_bounds__(256) kernels (hy4_engram_fp8_embed for 100-byte E4M3FN+FP32 row decode + per-head RMSNorm into BF16, hy4_engram_fp8_bias for rank-0 BF16 bias into FP32 partials, and hy4_engram_fp8_gate for post-allreduce unified RMSNorm, 4-stream signed-sqrt sigmoid gate, scales, mask, and residual add). Zero planted control symbols in the production fatbin.
  • zig/kernels/cuda/contract_hy4_engram_fp8_controls.cuh (included from contract_controls_set.cu): 11 planted control kernels in fatbin_contract_controls covering non-finite drops, repeat drift, tail skips, and reduced-precision sums (fp16 on embed, bf16 on gate).
  • zig/src/families/hy4_flash/engram_fp8_cuda.zig: host-compilable CUDA launchers (Ops, GateWeights, launchEmbed, launchBias, launchGate), registered in zig/src/families/hy4_flash/host_tests.zig and zig/build/cuda.zig.
  • zig/tests/cuda/contract_hy4_engram_fp8.zig (registered in contract_family_sets.zig and contract_limits.zig): full 5-gate CUDA contract coverage (engram_embed, engram_bias, engram_gate engram_out, engram_gate streams_out, plus all 11 planted controls).
  • zig/src/core/contract/hy4_engram_fp8_records.zig (registered in registry_joins.zig): nvcc 13.0 5-SM (sm_80, 86, 89, 120, 121) register/shared-memory/bound records (0 spills on all 5 SMs).

No served path, model load, or default is changed in this slice. Remaining work for #4795: paged sparse indexer, 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: 8dcfa587a4f5628084d472ec380b230bd26201d4 (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 (8dcfa587a), 0 new (python3 tools/pr_check.py --base origin/main: 0 error(s))
  • Host tests (Zig 0.17.0, -Dfixed-cpu, Linux x86_64):
    • zig build -Dfixed-cpu test-hy4-flash-host --summary all: 49 pass, 0 fail, 0 skip (exit 0)
    • zig build -Dfixed-cpu test-kernel-contract --summary all: 328 pass, 0 fail, 0 skip (including Hy4 FP8 Engram CUDA: every launch is in the live registry and fits every SM; 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 kernel-contract-fatbins -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=80,86,89,120,121: exit 0 (0 bytes stack/spill on all 3 kernels across all 5 SMs)
    • ./zig-out/bin/tf-cuda-test kernel-contract-limits zig-out/fatbin: all 15 hy4_engram_fp8 5-SM records (embed, bias, gate × sm_80,86,89,120,121) reported PASS ... (record current), 0 hy4_engram_fp8 problems
    • ./zig-out/bin/tf-cuda-test kernel-contract hy4_engram_fp8 (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; gates 90 ms, limits 0 ms
    • Full unfiltered ./zig-out/bin/tf-cuda-test kernel-contract (exit 1 due to pre-existing unrelated muse/ricursive/exl3_experts nvcc 13.0 limit records and 2 unrelated failures; 0 failures, 0 missed controls, and 0 limit problems in hy4_engram_fp8):
      SUMMARY 908 kernels: 906 pass gates 1-4, 2 fail, 12 known findings; 597 controls: 597 red as planted, 0 missed; 292 limit problems; gates 5071123 ms, limits 772270 ms
  • 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 8dcfa587a, 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 11/11 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 finite E4M3FN widening and packed-row F32 scale step match the existing decoder. The legacy Engram sources and default serving reach are unchanged, the production and control entries are separate, and this head merges cleanly.

Ready for focused synthetic GB10 comparisons; no model checkpoint is needed for those. The reported four-kernel/eleven-control pass and fifteen launch records are retained. The FP64 bounds do not establish exact output bytes for the full RMS/gate pipeline, so those comparisons against the intended FP32 order and rounding seams remain before target qualification, including both gate outputs and masked rows. Whole-model qualification remains in the serving slice.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Pushed 270ad4dac to fix the No GPU/zig failure from tools/zig/cuda_common_check.py (engram_fp8.cuh previously defined a local block_sums helper).

  • Switched production warp reduction in zig/kernels/cuda/hy4_flash/engram_fp8.cuh to tf_common::warp_sum via ../common/reduce.cuh (keeping the planted BF16-rounding control path separate).
  • Re-verified locally: python3 tools/zig/cuda_common_check.py (0 violations), python3 tools/zig/cuda_fragment_check.py (0 misses), test-hy4-flash-host (49/49), compile-cuda-tests, zig fmt --check, and pr_check.py --base origin/main (lean 164 vs 164, 0 new).
  • Re-verified live on NVIDIA GB10 (sm_121, CUDA 13.0.88, -Dsm=80,86,89,120,121):
    • kernel-contract hy4_engram_fp8: 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems
    • kernel-contract-limits zig-out/fatbin: all 15 hy4_engram_fp8 5-SM records unchanged (PASS ... (record current)).

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @GrgaPalcic. The shared warp helper keeps XOR 16,8,4,2,1, the serial eight-warp combine, both barriers and the separate BF16 control. The decoder and other operator bodies remain unchanged, and the merge is clean. The new-head four/eleven pass and fifteen records are retained.

This remains suitable for focused synthetic GB10 comparisons. Source order is preserved, but no old/new output-byte comparison was supplied. The earlier exact RMS/gate checks still stand, including both outputs, masks and tails against the intended FP32 order and rounding seams. The bounded contract pass does not replace those comparisons; whole-model qualification stays with the later serving slice.

  • Synthena

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you for moving the production warp reduction to the shared helper and updating the serving pin. The helper keeps the same reduction order, barriers and separate BF16 control path. The branch merges cleanly, and the retained four-kernel/eleven-control and fifteen-record evidence is credited.

The remaining qualification is exact output comparison. The current FP64 bounds and planted controls do not prove the intended FP32 operation order and rounding seams for the complete pipeline. On the focused synthetic cell, please compare the production operators against the intended serial implementation byte for byte: embed decode, FP32 scale, RMSNorm and BF16 output; rank-0 FP32 bias; and both engram_out and streams_out through the gate. Include masked and unmasked rows, tail dimensions, four-stream residual updates, repeats and non-finite cases. Every compared output buffer should be initialized before launch so an unwritten region cannot inherit a passing value.

This is suitable for A/B rather than a request for another kernel rewrite. No model checkpoint applies to this kernel-only slice; whole-model token equality remains with the later serving work.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-engram-fp8-cuda to add exact byte-for-byte serial FP32 comparison on the focused synthetic cell:

  • Exact serial FP32 oracle (zig/tests/cuda/contract_hy4_engram_fp8_exact.zig, zig/tests/cuda/contract_hy4_engram_fp8.zig):
    • Added host serial FP32 reference implementations (serialEmbed, serialBias, serialGate) matching the exact FP32 operation order, reduction tree (32-lane XOR butterfly 16,8,4,2,1 + serial 8-warp combine), tf_exp sigmoid, and BF16 rounding seams of engram_fp8.cuh.
    • Compares every production output byte-for-byte against the serial FP32 oracle while keeping the existing FP64 bounds, Gate 4 stress sums, and all 11 planted controls:
      • hy4_engram_fp8_embed: e4m3 decode, FP32 scale, RMSNorm, and BF16 output across 14 shapes including tail dimension k = 37, repeats, and non-finite inputs.
      • hy4_engram_fp8_bias: rank-0 FP32 bias across 7 shapes including tail n = 259, repeats, and non-finite inputs.
      • hy4_engram_fp8_gate: both single-output (engram_out / streams_out) and combined dual-output (engram_out + streams_out in the same launch) across 8 shapes including tail k = 262, masked and unmasked rows, 4-stream residual updates, repeats, and non-finite inputs.
    • Initializes every compared output buffer with 0xa5 immediately before launch so unwritten regions cannot inherit passing values.

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_engram_fp8: 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems; gates 92 ms, limits 0 ms

@ashhart
ashhart force-pushed the pr/hy4-engram-fp8-cuda branch from 80a532e to 870f7df Compare November 26, 2026 12:58
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @GrgaPalcic. The new serial reference is a substantial improvement. The FP32 traversal, reduction tree, RMS sequence, gate arithmetic and two BF16 rounding points match the production formulas. Poisoning every compared buffer and checking both gate outputs in one launch are also confirmed. The shared reduction keeps its order and cleanup, and this slice still has no serving-model caller.

A few exactness and test-safety items remain before this can land:

  • The directed gate-4 stress inputs still receive only FP64 bounds. Please run the byte oracle on those cases and add a wrong-order control that proves this comparator can fail.
  • The earlier cell calls for the serial oracle to use the device's own FP32 intrinsics, tf_exp and BF16 conversion. The current host transcription should move into a CUDA test kernel. Bias NaNs also need exact byte comparison rather than the current any-NaN exemption.
  • Please repeat the combined dual-output launch from freshly poisoned buffers, and add the null-mask and directed alternating-mask cases. The directed 3070/3074, reduction, signed-square-root, BF16-halfway and E4M3 max-finite fixtures are still open.
  • The two dual-output allocations need an error-path stream drain before their deferred frees. A failed fill, launch or synchronization can otherwise leave queued writes targeting freed storage.
  • On an exact mismatch, please include the element indices and expected/actual hexadecimal values. The direct old-local-reduction versus new-shared-reduction byte receipt is also still absent; the new serial comparison is separate evidence.

Current main also has conflicts in zig/tests/cuda/contract_family_sets.zig, zig/tests/cuda/contract_limits.zig and zig/tests/fixtures/gradient-serving.tsv. Integration needs to keep main's sparse-attention/TFQ3 registrations and the new hy4_flash cleanup, then recompute the merged CUDA registry hash.

No production arithmetic rewrite is indicated by these findings. The retained GB10 contract results are credited, but the new exact checks above remain unrun here.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-engram-fp8-cuda (590d98573) to address every exactness, test-safety, and main-integration item:

  • GPU serial FP32 oracle (contract_hy4_engram_fp8_controls.cuh, contract_hy4_engram_fp8_exact.zig): moved the serial FP32 oracle for embed, bias, and gate into single-thread CUDA test kernels (tf_ref_hy4_engram_fp8_embed, tf_ref_hy4_engram_fp8_bias, tf_ref_hy4_engram_fp8_gate) in fatbin_contract_controls, using device FP32 intrinsics (__fadd_rn/__fmul_rn/__fdiv_rn/__fsqrt_rn), tf_glue::tf_exp, and __float2bfloat16_rn. hy4_engram_fp8_bias now compares exact 32-bit bytes on all gates including .nonfinite (no any-NaN exemption).
  • Gate-4 stress exactness & wrong-order control: enabled the GPU serial byte oracle on Gate 4 (.sums) stress inputs alongside Gates 1 and 2, and added tf_ctl_hy4_engram_fp8_gate_bad_order (reverses warp merge order so FP64 bounds pass while exact bytes differ), confirmed red with hex diagnostics (elem 5225 got=0xb86a want=0xb869).
  • Direct old-local vs shared-reduction byte receipt: added tf_ref_hy4_engram_fp8_gate_old_reduce (old local butterfly loop) in fatbin_contract_controls and verified 0-bit byte equality against production hy4_engram_fp8_gate (tf_common::warp_sum) on both engram_out and streams_out.
  • Dual-output repeat, masks & directed fixtures: repeated combined dual-output launches twice from freshly 0xa5-poisoned buffers; added null-mask (mask = 0) and directed alternating-mask ([1, 0, ...]) dual-output checks; added 3070/3074 shapes to gate_served; added directed E4M3 max-finite (0x7e/0xfe)/subnormal/BF16-halfway embed fixtures and directed signed-sqrt (g > 0, g < 0, g == 0, |g| < 1e-6)/reduction/BF16-halfway gate fixtures.
  • Error-path stream drain & hex mismatch diagnostics: every temporary DeviceBuffer in contract_hy4_engram_fp8_exact.zig drains the stream (defer self.rig.s.synchronize() catch {};) before deferred frees, and mismatches report the first differing element index and expected/actual hex values.
  • Current main integration: merged origin/main (eff4e5b75), preserving sparse_attn, tfq3/tfq3_moe, and cleanup order 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 590d98573:

  • tf-cuda-test kernel-contract hy4_engram_fp8:
    SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 12 controls: 12 red as planted, 0 missed; 0 limit problems; gates 172 ms, limits 0 ms

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. The device-side serial oracle, exact bias-NaN comparison, wrong-order control, freshly poisoned dual-output repeats, mask modes, directed fixtures, hexadecimal diagnostics, and old/local reduction equivalence are all present. The temporary-buffer drains also precede every relevant deferred free, and the final formatting commit brings the largest file under 600 lines without changing the arithmetic. The current registrations and registry pin are consistent.

One test-oracle issue remains. Gate 4 does not invoke k.vt.check, so the new .sums branches in checkEmbed and checkGate never run from kernel-contract; the stress launches still receive only FP64 bounds. At zig/src/core/contract/gates.zig:323, please invoke an available k.vt.check(k.ctx, .sums) after the existing bound check and turn a non-null result into a Gate 4 failure, or add the equivalent hy4_flash-local stress invocation. Merely accepting .sums in the helpers does not reach them.

The wrong-order control already demonstrates the comparator's sensitivity on shapes. No production kernel rewrite is indicated by this remaining qualification gap, and no serving checkpoint run applies.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-engram-fp8-cuda (390464b93) to invoke k.vt.check(k.ctx, .sums) on Gate 4 (zig/src/core/contract/gates.zig:324-326) after the existing bound check, turning any non-null result into a Gate 4 failure so checkEmbed and checkGate now run the GPU serial FP32 byte oracle (and old-local vs shared reduction check / dual-output repeats) on the Gate 4 stress launches too.

Verified locally (zig fmt --check, pr_check.py --base origin/main, test-kernel-contract, test-gradient-serving) and live on GB10 (sm_121, nvcc 13.0) on exact head 390464b93:

  • tf-cuda-test kernel-contract hy4_engram_fp8:
    SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 12 controls: 12 red as planted, 0 missed; 0 limit problems; gates 178 ms, limits 0 ms

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thank you, @GrgaPalcic. The device-side serial oracle, exact bias-NaN checks, wrong-order control, poisoned dual-output repeats, masks, directed fixtures, cleanup drains and hexadecimal diagnostics are present. The last change also reaches the new stress oracle.

The global Gate 4 call introduces a regression in existing kernels. CK and VIT hooks ignore the gate argument and unconditionally require a non-finite output. That assertion belongs to Gate 1's planted fixture. Gate 4 uses finite stress inputs, so otherwise-correct CK and VIT kernels now fail after valid numerical and replacement-byte checks.

Please make the sums callback an explicit per-kernel opt-in, use the permitted family-local stress invocation, or make every affected hook gate-aware before retaining the global call. Then rerun the focused contract suite and affected existing coverage. No production arithmetic rewrite is indicated, and no model-checkpoint run applies.

  • Synthena

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks @GrgaPalcic. Our re-check of 83533b370a finds the earlier source findings closed, and no new ones.

We don't have this model's checkpoint on our machines, so this lands on your receipts. For this exact head, please post:

  1. greedy and seeded-sampled output tokens equal to the serial reference, at a short and a long prompt;
  2. the FP8 Engram kernel-contract suite result on your GPU.

The branch conflicts with current main; we will rebase it for you, so there is nothing to do there.

  • Synthena

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Updated pr/hy4-engram-fp8-cuda (47bfbed00) to remove the global gates.zig change and run the stress exact-oracle checks locally inside hy4_flash, plus merged current main (91dbadce6):

  • zig/src/core/contract/gates.zig: reverted completely (0 diff against origin/main), eliminating any regression in existing CK/VIT hooks.
  • Family-local stress exact oracle (contract_hy4_engram_fp8_exact.zig): added checkStressEmbed (invoked on k=96) and checkStressGate (invoked on k=3074) running the exact Gate 4 .stress inputs into freshly 0xa5-poisoned buffers against the GPU serial FP32 oracle kernels, with error-path stream drains before deferred frees and hex mismatch diagnostics.
  • Current main integration: merged origin/main (91dbadce6), preserving ernie5_5_cuda, qmmf_many_records.zig, and refreshed the kernels.zig pin in gradient-serving.tsv.

Verified locally (pr_check.py --base origin/main, test-kernel-contract, test-gradient-serving) and live on GB10 (sm_121, nvcc 13.0) on exact head 47bfbed00:

  • tf-cuda-test kernel-contract hy4_engram_fp8:
    SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 12 controls: 12 red as planted, 0 missed; 0 limit problems; gates 182 ms, limits 0 ms
  • tf-cuda-test kernel-contract vit:
    SUMMARY 25 kernels: 25 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems; gates 594573 ms, limits 7 ms

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