Repository navigation
Hy4-flash on Metal: native family, serving with DFly2, compressed indexer - #3941
Merged
Merged
Conversation
ashhart
force-pushed
the
hy4-flash-metal
branch
from
November 19, 2026 10:39
080e5df to
3937347
Compare
ashhart
force-pushed
the
hy4-flash-metal
branch
from
November 19, 2026 13:33
e59a787 to
bd2f17e
Compare
ashhart
marked this pull request as ready for review
November 19, 2026 13:33
ashhart
force-pushed
the
hy4-flash-metal
branch
from
November 19, 2026 13:37
bd2f17e to
95c7f47
Compare
This was referenced Nov 19, 2026
ashhart
force-pushed
the
hy4-flash-metal
branch
from
November 20, 2026 13:42
5931932 to
523acfc
Compare
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.
…ver TF_HY4_ENGRAM and TF_HY4_DRAFT_BITS
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
force-pushed
the
hy4-flash-metal
branch
from
November 20, 2026 15:28
523acfc to
c19a6af
Compare
ashhart
added a commit
that referenced
this pull request
Nov 20, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Adds Tencent's Hy4-flash (
hy_v4_flash) as a native family and serves it fromtensorfold-nativeon Metal. Before: no engine read the checkpoint. After:tensorfold-native servetakes 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:
drafted output equals
"draft": false;a resumed prompt equals a fresh one;
prompt chunks of any size give the same tokens.
Issue: Refs Add native Hy4-flash support on Metal and CUDA #3797 (this covers Metal; CUDA serving and image input remain).
Stacked on: main. The chat template needs
str.format()from template: str.format() with positional fields and brace escapes #3816 (atakann's), so serving fails at template compile until that lands. Everything else here stands alone.atc overlap --json: incomplete (2 limitations), with no conflicts listed.atc check 3797: free.Checks on this head
tools/light_gate.shALL 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 headzig build test-hy4-flash-hostpassed;zig build test-kernel-contract test-nemotron-prefill-metal-sourcespassed (Metal runtime sources: 1,061 attempted, 0 failed);zig build -Dfixed-cpu test-gradient-serving test-qwen-serving-hostpassed;zig test zig/src/server/hf_hy4.zigpassed;zig fmt --check build.zig zigandgit diff --checkare clean.gradient-serving.tsv: the pins forzig/src/cuda/kernels.zig(the Hy4-flash fatbin images registered) andzig/src/cuda/root.zig(cuda.affine_expertsexported) take their new hashes, as earlier families' registrations were recorded.zig/gemma4_shared_affine_test.zigtakes the new bytes of coreaffine_mm.metal(Hy4-flash's mapped experts and rounded SiLU, behind defines no other caller sets) andlane_projection.metal(inline sums, the 8-bit reg layout).environment_test.zigcoversTF_HY4_ENGRAMandTF_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-hostpasses on this head.Rows.loadbuildsrows.metalon 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-pathspasses on this head.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; defaultnvme. 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-MTPandTensorFold/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:"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:
token_shawith drafts on and off, fresh and resumed;Exactness by row count, greedy, measured with a local probe build (not in the tree):
Decode, the same
bench_concurrentrun (8-bit, one stream, greedy, drafts on):The same server gave 40.3 tok/s with
"draft": false, so the drafter is a net gain.Prompt speed:
tools/bench_cache.py --require-cold: not run.Long context:
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
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.SUMMARY: Metal runtime sources 1,061 attempted, 0 failed; Metal launch map 896 registered, 2 controls, 0 missing.Checklist
hf_hy4.zig, covers new code.)"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.--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.)