Skip to content

Hy4-flash on Metal: native family, serving with DFly2, compressed indexer - #3941

Merged
ashhart merged 12 commits into
mainfrom
hy4-flash-metal
Nov 20, 2026
Merged

ashhart merged 12 commits into
mainfrom
hy4-flash-metal

Conversation

@ashhart

@ashhart ashhart commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Adds Tencent's Hy4-flash (hy_v4_flash) as a native family and serves it from tensorfold-native on Metal. Before: no engine read the checkpoint. After: tensorfold-native serve takes the 4- and 8-bit MLX checkpoints with DFly2 drafts and contexts to 256K tokens (128K by default). Past 2,048 tokens, each sparse layer attends the 2,048 keys its compressed indexer recalls. A prompt that starts with the previous prompt's tokens prefills only the rest.

Every kernel computes a row the same way whatever pass the row lands in, so:

Checks on this head

  • Head commit: 5931932
  • Lean checker, on the base and on this head: 191 and 191, 0 new.
  • The light landing gate passes on this tree: tools/light_gate.sh ALL PASS (skill, gitleaks, whitespace, lean, cuda_common, fmt, pytests, buildopts, contract-host, hip, devtools, linux, paths, pins) on the tree before the last rebase onto main; it is rerunning on this head
  • Tests run:
    • zig build test-hy4-flash-host passed;
    • zig build test-kernel-contract test-nemotron-prefill-metal-sources passed (Metal runtime sources: 1,061 attempted, 0 failed);
    • zig build -Dfixed-cpu test-gradient-serving test-qwen-serving-host passed;
    • zig test zig/src/server/hf_hy4.zig passed;
    • zig fmt --check build.zig zig and git diff --check are clean.
  • gradient-serving.tsv: the pins for zig/src/cuda/kernels.zig (the Hy4-flash fatbin images registered) and zig/src/cuda/root.zig (cuda.affine_experts exported) take their new hashes, as earlier families' registrations were recorded.
  • Source pins: zig/gemma4_shared_affine_test.zig takes the new bytes of core affine_mm.metal (Hy4-flash's mapped experts and rounded SiLU, behind defines no other caller sets) and lane_projection.metal (inline sums, the 8-bit reg layout). environment_test.zig covers TF_HY4_ENGRAM and TF_HY4_DRAFT_BITS. CI on 95c7f47 failed on exactly these two tests (zig, zig-b, zig-windows); zig build test-gemma4-affine-host test-environment-host passes on this head.
  • Row windows before the M5: Rows.load builds rows.metal on the wide row core on every chip. Without that, chips before the M5 failed to compile the row library when a Hy4-flash model loaded; the build and other models were unaffected. On the M5 the compiled text is unchanged. contract: Hy4-flash's Metal kernels under gates 1 to 5 #4022's kernel contract found it: the library now compiles for M1 and M2 offline, but no M1-M4 GPU was run. zig build native test-hy4-flash-host check-release-paths passes on this head.
  • CI: pending on this head.
  • Not run, and why: CUDA and Windows device runs (no CUDA serving path yet; the CUDA kernels only build and pass the host checks).

Receipt

  • Main against branch: main has no Hy4-flash engine. Every number below is this branch on one machine.

  • Boxes it reaches: Metal with tensor units (M5). Other Apple chips: not run.

  • New switches:

    • TF_HY4_ENGRAM=nvme|ram|gpu: where the Engram tables live; default nvme. All three give the same tokens.
    • TF_HY4_DRAFT_BITS=4|8|16: the DFly2 linears quantized at load; default 16 (BF16). It changes drafts only.

    Both read through platform/environment.zig.

  • Environment: Zig 0.17.0, Apple M5 Ultra with 512 GB, macOS 27. Checkpoints TensorFold/Hy4-flash-MLX-4bit-MTP and TensorFold/Hy4-flash-MLX-8bit-MTP.

  • Paths and platforms: the Metal serve path on M5 Ultra. No other chip was run.

  • Exactness, tools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0 --levels 1,4 --alone --serial, 8-bit, on this head's server:

    • alone: 3/3 at level 1 and 12/12 at level 4, for both the code and chat prompts;
    • serial (against "draft": false): 1/1 at each level for both prompts.

    The server runs one stream at a time, so concurrent requests queue and each equals its run alone.

  • Exactness, resumed against fresh, through the server, 8-bit, greedy, 256 tokens:

    • a short prompt and a 2,1xx-token prompt (the resumed run reused 2,117 cached tokens) give the same token_sha with drafts on and off, fresh and resumed;
    • the hashes are unchanged across the last three commits.
  • Exactness by row count, greedy, measured with a local probe build (not in the tree):

    • plain decode, DFly2 at 4 rows, and prompt chunks of 1, 37 and 512 rows give the same ids;
    • at 4- and 8-bit, at short context, at 3,000 tokens, and with decode crossing the 2,048-key boundary.
  • Decode, the same bench_concurrent run (8-bit, one stream, greedy, drafts on):

    • code prompt: 61.4 tok/s, 2.98 tokens a round;
    • chat prompt: 51.8 tok/s, 2.39 tokens a round.

    The same server gave 40.3 tok/s with "draft": false, so the drafter is a net gain.

  • Prompt speed:

    • a 512-row chunk takes 305 ms at 8-bit (about 1,680 tok/s);
    • an 8-bit 29.6K-token prompt took 30 s cold (986 tok/s) before the row-invariance commit;
    • tools/bench_cache.py --require-cold: not run.
  • Long context:

    • the indexer's recall at 6K tokens matches an fp64 ranking of the same inputs: 0 of 511 groups off;
    • 6K-token NLL equals full attention: 3.9825 vs 3.9827 on one text, 3.592 vs 3.585 on another.
  • Licence of any new weights or drafter: the Tencent Hy License (the checkpoint and its DFly2 drafter).

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract: the new Hy4-flash kernels are not registered yet. The changed core kernel, tf_lane_reg, gains an 8-bit path and an in-kernel sums option. Both give the same words as the 8-bit tensor layout and the sums pass, shown by the unchanged token hashes above.
  • Planted controls: none yet.
  • SUMMARY: Metal runtime sources 1,061 attempted, 0 failed; Metal launch map 896 registered, 2 controls, 0 missing.
  • Chips I could not run: everything except M5 Ultra.

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. (The one new test, in hf_hy4.zig, covers new code.)
  • 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.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled. (Main has no Hy4-flash; drafted is faster than plain greedy, and sampled was not measured.)

@ashhart ashhart changed the title Hy4-flash on Metal: native family, serving, compressed indexer and decode speed Hy4-flash on Metal: native family, serving with DFly2, compressed indexer Nov 19, 2026
@ashhart
ashhart marked this pull request as ready for review November 19, 2026 13:33
atakann added a commit to atakann/TensorFold that referenced this pull request Nov 20, 2026
ashhart#3941 registers test-hy4-flash-host too; with both, zig build --help panics (found by @AIFlowML in review).
…, MLA, iHC, Engram, indexer kernels, DFly2 drafter, host tests)
…eed work; compressed indexer past 2,048 keys

- Serving: the lane host with one stream, 512-row prompt chunks, DFly2 verify windows of up to six rows drawn on the host with the stream's sampler, the last stream's caches reused for a prompt that starts with their tokens, and every buffer in one residency set. Hy4's :opensource marker tokens read as the GLM spellings the server already parses.
- Long context: past 2,048 keys each sparse layer recalls its 511 best closed four-token groups plus its own (the reference's compressed indexer); token keys cached each pass, touched groups re-pooled in fp32. An fp64 check of the recall matches at 6K, and 6K-token NLL equals full attention.
- Decode: IHC collapse and seams over threadgroups, row and tensor projections, decode GEMVs for 1-2 rows, expert pair GEMVs, split-key decode attention, rank-based router select, 512-row prompt chunks.
- Probe: tf-hy4-speed (greedy ids and their hash, ms a token, per-stage GPU profile, NLL over a text).
…e and a resumed prompt equals a fresh one

Every pass takes the kernels a prompt chunk takes, so a row's bits never depend on how many rows share its pass:
- dense projections on the tensor units' lane projection at every row count (no 1-2 row GEMVs);
- routed experts on the tensor gathers at every row count (no pair GEMVs);
- MLA absorb and expand on the BF16 copies at every row count (no 1-row GEMVs over kv_b's codes);
- flash attention unsplit (a split count from the pass's widest row changed the other rows' sums);
- a pass across 2,048 keys attends its earlier rows over their whole range, as any other pass would, and only
  its later rows over the recalled lists.

Greedy tokens on the probe's prompts: plain, DFly2 (4 rows) and prompt chunks of 1, 37 and 512 rows give the same
ids at 4- and 8-bit, at short context, at 3,000 tokens and across the 2,048-key boundary.
…asses of 8 rows or fewer, and 8-bit codes reach the tensor units through registers

- Small passes skip the sums dispatch and its barrier: each split adds its (group, row) sums up front with the sums pass's own loop, one chain a lane, so the words are the same.
- 8-bit layouts take the reg path: the codes in the 4-bit reg order a byte each, unpacked as fp16 integers into the tensor op's right input; the same words as the 8-bit tensor layout.

Same greedy tokens as before at 4- and 8-bit (plain, DFly2, 512-row chunks). 4-bit one row 17.19 -> 16.54 ms, four rows 22.97 -> 22.45; 8-bit one row 24.13 -> 22.36 ms, four rows 34.75 -> 33.15, 512 rows 305 ms.
…vironment.zig, no dead decode paths, files under 600 lines

- TF_HY4_ENGRAM (nvme, ram or gpu) and TF_HY4_DRAFT_BITS (4, 8 or 16) are read through the environment module.
- The 1-2 row dense GEMVs, the expert pair and group GEMVs, the 1-row MLA GEMVs over kv_b's codes and their kernels go, with the diagnostic switches that chose them; every pass already took the prompt chunks' kernels.
- The speed probe leaves the tree; Linears, Rows and Scratch move out of metal_dense.zig, residency and drafting out of serve_metal.zig, and the Hy4 marker spellings out of hf_text.zig into hf_hy4.zig (with a test).
…UDA serving pins take the Hy4-flash images

- The family files import core/row_projection.zig, lane_projection.zig and moe_route.zig by path; the re-export modules made tensorfold.zig the root of a second module in the Metal runtime source tests.
- gradient-serving.tsv: kernels.zig with the Hy4-flash fatbin images registered and root.zig with cuda.affine_experts exported, as earlier families' registrations were recorded.
rows.metal calls the wide core's tf_rp_rows; before the M5, Rows.load paired it with the legacy core, whose tf_rp_rows
takes one template argument fewer, so the library failed to compile there. The wide core is plain Metal (the M5 only
prefers it for speed), and on the M5 the compiled text is unchanged.
…istrations

The rebase onto main joins main's newer kernels.zig rows with this branch's
Hy4-flash images; the serving pin takes the joined file's bytes.
@ashhart
ashhart merged commit 767d3d7 into main Nov 20, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the hy4-flash-metal branch November 20, 2026 15:28
atakann added a commit to atakann/TensorFold that referenced this pull request Nov 20, 2026
ashhart added a commit that referenced this pull request Nov 22, 2026
Unreleased: the five (#N) placeholders get their PR numbers, the two (#PR)
duplicates go, #3192's line goes (#4427 replaced that behaviour on main and
its line refs #3192), #3960 is named as the PR that landed #2799, and the
penalty under --instruct-sampling card; ?token= Web addresses and pull's
digest and size checks).

Sixteen PRs merged 20-21 Nov with no line anywhere: a blank lines file
replaced each PR's own line at landing. #4321 and #4322 go under
Unreleased, the twelve that shipped in 1.3.3 under 1.3.3, and the two
1.3.1 lines that carried no PR number get #3941 and #4022.

docs/environment.md: Flash Next checks 37 settings, and TF_GLM_INDEX_SPLIT
(registered by #4510) gets its row.
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.

1 participant