Skip to content

hy4_flash: W8A16 CUDA weight packing for qmmf projections and TP=1/2/4 FP8 experts (slice 2a of #4795) - #4895

Merged
ashhart merged 3 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-w8a16-pack
Nov 25, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-w8a16-pack

Conversation

@GrgaPalcic

@GrgaPalcic GrgaPalcic commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Adds host-side W8A16 CUDA weight packing for tencent/Hy4-flash-FP8 inside zig/src/families/hy4_flash/ (fp8_pack.zig and fp8_pack_test.zig), building directly on #4827 (fp8_weights.zig, fp8_partition.zig) and cuda.qmmf / cuda.fp8 / cuda.experts.Fp8Experts:

  • pack: stacks one or more BF16 or block-128 E4M3 projections by rows and slices 64-element input groups into cuda.qmmf layouts (qmmf.packBf16 or cuda.fp8.packCodes + cuda.fp8.packScalesAt with exact BF16→FP32 scale widening via core.quant.fp8.widen), refusing empty parts, mixed formats, unaligned slices, out-of-range slices, and non-finite scales without requantizing.
  • packHConcat: horizontally concatenates two BF16 column slices into qmmf BF16 layout with bounds and 64-element group checks.
  • hostExperts: slices stacked [E, 2w, L] gate_up_proj and [E, L, w] down_proj FP8 routed experts and their BF16 weight_scale_inv tensors on 128-row blocks across TP=1/2/4 into cuda.experts.Fp8Experts (packGateUp / packOne) + widened FP32 scale buffers, refusing unaligned shares, malformed shapes, and non-finite scales.
  • Wires cuda (host runtime, no GPU images) into zig/build/hy4_flash.zig and adds fp8_pack_test.zig to host_tests.zig (42/42 pass in test-hy4-flash-host).

No served path, model load, default, or GPU kernel is changed in this slice. Remaining work for #4795: CUDA kernels + kernel contract gates/controls/limits, served W8A16 TP=2/TP=4 + DFly2 CUDA Graphs with serial token-equality receipts, and SigLIP2 --vision + 3D M-RoPE on CUDA.

Checks on this head

  • Head commit: 30e31ad248b8eb3b76b1cc6c32b8c5c5e22491ca (base 9a38b1f7b33863507cdea47e88207d8b3bea3f22)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 165 on base (9a38b1f7b, inherited), 165 on head (30e31ad24), 0 new (python3 tools/pr_check.py --base origin/main: 0 error(s))
  • Tests run, with the command and pass, fail and skip counts:
    • zig build -Dfixed-cpu test-hy4-flash-host --summary all on 30e31ad24 (Zig 0.17.0, Linux x86_64): 42 pass, 0 fail, 0 skip (34 baseline + 8 new; exit 0)
    • zig fmt --check build.zig zig && git diff --check: exit 0
    • All 10/10 active GitHub Actions CI workflows are green on 30e31ad24 (fmt, zig, zig-b, zig-windows, zig-macos-dev-tools, pytest, release-paths (ubuntu-24.04), benchmark-clients (ubuntu-latest), benchmark-clients (windows-latest), comfyui-moss-installer).
  • Not run, and why: GPU kernel contract and real-model serving benchmarks (host-only packing slice; no GPU kernel or inference path is touched; device gates and serial token-equality receipts follow in the CUDA kernel and serving slices of Hy4-flash-FP8 (official W8A16) on 2x and 4x DGX Spark: native CUDA support in hy4_flash #4795). Windows/macOS hosts not run locally (covered by CI).

Receipt

n/a — host-only weight-packing module and unit tests; does not reach inference, model load, serving, or any GPU kernel.

  • Main against branch: n/a (host-only, no inference path changed)
  • Boxes it reaches: Linux x86_64 host tests (no GPU); macOS/Windows 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 9a38b1f7b, head 30e31ad24, Zig 0.17.0 (-Dfixed-cpu), Linux x86_64 host (no GPU); targets official tencent/Hy4-flash-FP8 @ 003107fa248f5892064ab1c43af0a7003dd68c9b
  • Paths and platforms the change reaches, which I ran and which I could not: ran Linux x86_64 host build and test suite (plus CI on Linux/macOS/Windows); did not run any CUDA/Metal/HIP/XPU GPU (host-only packing code)
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: n/a (does not reach inference; exactness against cuda.qmmf.packBf16, cuda.fp8.packCodes/packScalesAt, and cuda.experts.Fp8Experts.packGateUp/packOne plus refusal controls is verified in fp8_pack_test.zig)
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): n/a (host-only slice, no inference path changed)
  • Decode speed before and after, from tools/bench_openai.py: n/a (host-only slice)
  • Prompt speed before and after: n/a (host-only slice, cannot reach prompt processing)
  • 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.

@AIFlowML

Copy link
Copy Markdown
Contributor

The clean trial merge of 6bd6b9590b into 4c79cfe346 passed all 31 tests in zig build test-hy4-flash-host -Dfixed-cpu -j2 --summary all on M5 Max.

I added a host fixture for two stacked 64-row slices starting at rows 100 and 127, with 128 columns starting at 64. Those slices cross the checkpoint's 128×128 scale blocks in both dimensions. Hard-coded expectations check the four transitions in the packed scale array, and all 32 tests pass. In a scratch control that rounds only stacked source row offsets down to 128, the original 31 tests still pass and the new fixture fails. This would protect the nonaligned slice behavior if the helper is reused for another share layout.

Igor Lessio - AIFLOW LABS

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Thanks for testing the trial merge on M5 Max and for the suggestion, @AIFlowML!

I added that host fixture in zig/src/families/hy4_flash/fp8_pack_test.zig (stacked block-FP8 slices crossing 128x128 scale blocks in both dimensions hit every block transition): two stacked 64-row slices starting at rows 100 and 127 with columns [64, 192), asserting all four scale-block transitions (27 -> 28 on tile 0 and 0 -> 1 on tile 1 across both column groups), the full packed scale array, and the packed codes.

zig build test-hy4-flash-host -Dfixed-cpu --summary all passes 42/42 locally, with 0 new lean_check findings (pr_check.py) and zig fmt --check clean.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @GrgaPalcic. The new stacked-slice fixture covers the requested scale-block transitions. The packing preserves stored codes and BF16 words, widens scales exactly and keeps the slice/refusal checks.

Safe to land as the host-only packing slice. CUDA kernels, distributed serving and token-equality qualification remain for the later integrations.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the boundary fixture on the exact #5010 landing train, 865f5cce23. Its source matches this head merged into d2b43908db outside the changelog. On M5 Max, zig build test-hy4-flash-host check-native-cuda-host -Dfixed-cpu -j2 --summary all passed 32/32, and the ARM64 Linux native CUDA adapter compiled.

As a negative control in scratch, rounding the source row offset down to a 128-row boundary made the new test fail at its first transition (expected 3, found 1). The other 31 host tests passed. Restoring the exact row offset brought it back to 32/32. Thanks for adding that case.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart merged commit 777f3bb into ashhart:main Nov 25, 2026
12 checks passed
@ashhart

ashhart commented Nov 25, 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.

3 participants