Skip to content

contract: Hy4-flash's Metal kernels under gates 1 to 5 - #4022

Merged
ashhart merged 1 commit into
mainfrom
hy4-contract
Nov 20, 2026
Merged

ashhart merged 1 commit into
mainfrom
hy4-contract

Conversation

@ashhart

@ashhart ashhart commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Registers Hy4-flash's Metal kernels with the kernel contract. Before: #3941 adds the family with none of its kernels registered. After: every Metal pipeline Hy4-flash loads runs through gates 1 to 5, each group with planted controls, and so do the lane projection variants #3941 adds to core (8-bit reg, inline group sums).

Building the records found one real bug. On Apple chips before the M5, Hy4-flash's row-window library failed to compile when a model loaded: Rows.load paired rows.metal with the legacy row core, whose tf_rp_rows takes one template argument fewer. The fix (the wide row core on every chip, plain Metal, the M5's text unchanged) moved into #3941 as 5931932, so main never carries the broken load. This pull request changes only tests, contract records and docs.

Checks on this head

  • Head commit: f91e082 (the same tree as the runs below, less one changelog sentence that moved with the fix)
  • Lean checker on the base (07845eb) and on this head: 189 and 189, 0 new.
  • Tests run, all on an M5 Ultra with tensor units:
    • tf-kernel-contract --only "hy4": SUMMARY 76 kernels: 76 pass gates 1-4, 0 fail, 12 known findings; 51 controls: 51 red as planted, 0 missed; 0 limit problems
    • tf-kernel-contract --only "Hy4-flash" --no-limits (the lane variants): SUMMARY 12 kernels: 12 pass gates 1-4, 0 fail, 0 known findings
    • tf-kernel-contract --only "lane projection" --no-limits (every lane entry, after the probe's changes): SUMMARY 22 kernels: 22 pass gates 1-4, 0 fail, 0 known findings
    • tf-kernel-contract --no-gates --offline DIR --only "hy4": PASS limits: 185 selected registered Metal launches against M1-M5, 0 problems, and the M1/M2 compiles have 0 limit problems.
    • zig build test-kernel-contract: the launch map has 1,081 registered launches, 0 missing.
    • The light gate's build steps: test-kernel-contract test-nemotron-prefill-metal-sources test-environment-host test-gemma4-affine-host, -Dhip=true hip-host-test check-native-hip-host, dev-tools, check-native-cuda-host compile-cuda-tests -Dtarget=x86_64-linux-gnu, check-release-paths, -Dfixed-cpu test-gradient-serving test-qwen-serving-host and test-hy4-flash-host all exit 0. The skill gate says SAFE TO PUSH.
    • zig fmt --check build.zig zig, git diff --check and gitleaks are clean.
  • CI: pending on this head.
  • Not run, and why:
    • M1 to M4 GPUs: no such box is free. Their gate 5 limits come from the offline compiles.
    • CUDA: no CUDA host serves Hy4-flash yet (only native/metal.zig lists hy_v4_flash), so its CUDA kernels have no served shapes to register.

Receipt

  • Main against branch: n/a. This pull request changes no engine source; the one engine change it found is in Hy4-flash on Metal: native family, serving with DFly2, compressed indexer #3941.
  • Boxes it reaches: Metal. M5 Ultra run; M1 to M4 by offline compile only.
  • New switch: none.
  • Environment: Zig 0.17.0, Apple M5 Ultra, macOS 27, the Metal toolchain's offline compiler for M1/M2.
  • Paths and platforms: the kernel contract on Metal.
  • Exactness: no engine source changes here. The contract itself checks replacement kernels bit for bit: inline sums equal the reg kernel with its sums pass; 4-bit reg equals the cooperative kernel; the two-step MLA attention equals the one-step pass; the fused seam and the split collapse equal their unfused steps; the gated expand equals expand then gate.
  • Decode and prompt speed: n/a, no engine source changes.
  • Licence: n/a.
Kernel contract
  • Kernels registered, by group (zig/tests/contract/metal_hy4_*.zig, records in zig/src/core/contract/hy4_*_records.zig):
    • indexer: tf_hy4_index_keys, tf_hy4_index_groups, tf_hy4_group_scores, tf_hy4_recall_groups;
    • MLA projections: the rotary table, tf_hy4_rotate, tf_hy4_cache_keys, tf_hy4_gate_values, tf_hy4_absorb_q, tf_hy4_expand_v, and tf_hy4_expand_v_gated (replaces expand then gate);
    • MLA attention: chunks and merge, and scores and scored and merge (replaces chunks and merge) for sparse and draft strides;
    • tensor-unit MLA: tf_hy4_absorb_nax, tf_hy4_expand_gated_nax, tf_hy4_attend_nax, and the split pair with its combine;
    • hyper-connections and DFly2 glue: the collapse at 4 and 8 gates, partials and finish (replace the one launch), tail and head, the fused seam (replaces tail then head), the add, the draft fuse and residual;
    • routing: core moe_route's logits and select at Hy4's width, tf_hy4_route_select_rows and _rank, tf_hy4_routed_sum, tf_add_bf16;
    • experts: tf_affine_experts_up/down_4/8, the tensor-unit gathers with Hy4's mapped experts and rounded SiLU, and tf_hy4_qkv_split, tf_hy4_active_experts, tf_hy4_group_pairs;
    • rows, Engram and embedding: tf_hy4_rows_r2_q4/q8, tf_hy4_argmax, Engram's lookup, gather and apply, and tf_embed_b4/b8_g64 at hidden 3,072;
    • lane projections: q4 and q8 reg at K splits 2, 4 and 8, with and without inline sums, plus 66 gate 5 records at Hy4's eleven served widths.
  • Planted controls: 51, at least one for each group, each red at its gate.
  • Known findings: 12, all at gate 1 and all on non-finite inputs. Each has an entry of its own, so the main entries keep a clean gate 1; docs/kernel-contract.md lists them under "Known findings".
    • tf_hy4_attend_chunks and tf_hy4_attend_scored: a -Inf score at a chunk's first key gives NaN.
    • tf_hy4_attend_nax: a non-finite value at a masked key its tile reads gives NaN; a row whose every score is NaN gives 0.
    • tf_hy4_route_select_rank: a NaN key ranks 0 and races the best expert for the first pick.
    • tf_hy4_group_scores: a NaN dot scores 0.
    • tf_hy4_engram_apply: a NaN dot is dropped by max.
    • The SwiGLU clip (tf_affine_experts_up_4/8, affine_mm's am_swiglu at q4 and q8) drops a NaN.
    • The 4-bit tensor scatter carries affine_mm's existing +128 code offset finding.
  • Not registered, and why:
    • attention_sink.metal and indexpool.metal: nothing on Metal loads them.
    • The other eight rows.metal templates: no loader builds them.
  • Loaded but not served today, registered anyway at the shapes their encoders use: tf_hy4_expand_v, tf_hy4_gate_values, the split attention pair, the one-launch collapse, tf_hy4_ihc_add, tf_hy4_route_select_rows, tf_add_bf16.
  • Chips not run: M1 to M4 GPUs. CUDA has no Hy4-flash host.

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: each control goes red, and tensor-only entries print UNSUPPORTED without tensor units.
  • 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.
  • No precision traded for speed; no engine source changes.

@ashhart
ashhart force-pushed the hy4-contract branch 2 times, most recently from f91e082 to eed268c Compare November 20, 2026 13:11
@ashhart
ashhart marked this pull request as ready for review November 20, 2026 19:58
Every Metal pipeline Hy4-flash loads registers with the kernel contract, in eight groups (zig/tests/contract/metal_hy4_*.zig,
records in zig/src/core/contract/hy4_*_records.zig), each with planted controls. The lane projection's 8-bit reg and
inline-sums variants join the affine probe, and their launches record gate 5 at Hy4-flash's served widths. Twelve gate-1
findings on non-finite inputs are recorded as known, with entries of their own and lines in docs/kernel-contract.md.
@ashhart
ashhart merged commit 9f904af into main Nov 20, 2026
10 of 11 checks passed
@ashhart
ashhart deleted the hy4-contract branch November 20, 2026 20:49
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