Skip to content

Upstream PR: H3 Metal products on h3_gemm_32x64 in 64-row bands, 64-tile decoder pass - #4641

Merged
ashhart merged 13 commits into
ashhart:mainfrom
plotarmordev:feat/108-upstream-pr-h3-metal-products-on-h3-gemm
Nov 24, 2026
Merged

ashhart merged 13 commits into
ashhart:mainfrom
plotarmordev:feat/108-upstream-pr-h3-metal-products-on-h3-gemm

Conversation

@plotarmordev

@plotarmordev plotarmordev commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

H3's transformer products move from MLX's gemm_nax (swizzle 2) to H3's own h3_gemm_32x64 kernel, whose threadgroups walk 64 row tiles under each weight tile, so a 2048x2048 picture reads each weight tile once while it is cached. The decoder decodes 64 tiles a pass instead of 16. Sums are unchanged: the pictures are byte-identical, and a warm 2048 picture goes from 13.03 s to 12.12 s on my M5 Ultra.

How to check

  • Base commit: 11b815e376ce122026f3c4011029ada2a56da53c (Ash's main). Head commit: 7f199148ea47c1fb71b1ee6e8eac5895cf88a4d2.
  • Machine: Mac Studio M5 Ultra, macOS 27.0, Zig 0.17.0.
  • zig build test-h3-gemm: exit 0, 1 passed, 0 failed, 0 skipped. It runs h3_gemm_32x64 (whole and ragged) and MLX's gemm_nax band of 4 on the same inputs and requires every output bit equal, across a band edge, a ragged tile and an untouched guard row tile.
  • zig build tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast && zig-out/bin/tf-kernel-contract --only "h3 product": exit 0, SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems. Each product carries a planted control, its 32-deep K steps reversed, and both go red at the shapes gate.
  • zig build dev-tools -Doptimize=Debug: exit 0; it builds tf-h3-image-server, whose decoder tool reads most_batch through image.zig.
  • zig build test test-golden: 1910 passed, 4 failed, 30 skipped; 979/986 steps; the fifth failed step is the tensorfold-native memcpy link check. The failures are the same set as on base main 11b815e37 and none is in h3: the two deepseek_v41 vision_config tests, the net DNS test (.invalid resolves on my Mac), and persistence_identity. On base, serve_steps_test:278 failed once and passed on rerun; here it passed.
  • Picture: tf-h3-image-server, card 34's prompt.txt (sha256 cc969bca…), seed 42, six steps, 2048x2048, one cold request and then warm requests in the same resident server. Timings are from my fork receipts, not a new run on this base: before is 13.03 s (my recorded baseline in the fork's docs/recipes/minimax-h3-image-metal.md, fork dfa6d9da2), after is 12.12 and 12.13 s (fork 8d6f94ca4, the same kernel as this head). Every PNG before and after is sha1 904814d66e7d…, byte for byte.

Checks on this head

  • Head commit: 7f199148ea47c1fb71b1ee6e8eac5895cf88a4d2
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 165 on base 11b815e37, 165 on this head, 0 new (lean_new.py). The 165 are inherited from main.
  • Tests run, with the command and pass, fail and skip counts: see "How to check".
  • CI: at 5bd056431, zig-macos-dev-tools failed compiling tf-h3-image-server: zig/tests/h3_image_decoder.zig:22: root source file struct 'image' has no member named 'most_batch' (job). The server maps h3_image_decoder to image.zig, and I had dropped its most_batch re-export as unused; 3378466a8 restores it. CI on 7f199148e is running.
  • Not run, and why: no new GPU timing on this base. The receipts above come from my fork at the same kernel, and this Mac's GPU is shared.

Receipt

  • Main against branch: warm 2048 picture, 13.03 s before and 12.12 s after; 6 steps 11.39 s before, 10.49 s after; decoder 1.40 s after.
  • Boxes it reaches: Mac (Metal), MiniMax-H3-Image only.
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: fork 8d6f94ca4 for the after timing, Zig 0.17.0, macOS 27.0 Metal, M5 Ultra 256 GB, MiniMax-H3-Image bf16 weights (117.6 GiB resident).
  • Paths and platforms the change reaches, which I ran and which I could not: H3 image transformer products and decoder on Metal; I ran them on M5 Ultra only.
  • Exactness: latents and PNGs byte-identical before and after (sha1 f3563c56… latent, 904814d66e7d… PNG); test-h3-gemm bit for bit against MLX.
  • Main against this branch, same machine and checkpoint: from my fork receipts at the same kernel, as above.
  • Decode speed before and after, from tools/bench_openai.py: n/a, an image model with no token decode.
  • Prompt speed before and after: n/a, the prompt encoder is unchanged (0.22 s warm both sides).
  • Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: h3_gemm_32x64 and h3_gemm_32x64_ragged in zig/src/core/contract/registry.zig, replacing MLX's gemm_nax for H3's products.
  • A planted control for each new kernel, in zig/tests/contract/metal_h3.zig: each product's 32-deep K steps run in reverse, so its sums round differently from gemm_nax's. Both are red at the shapes gate, as planted:
FAIL shapes control: metal h3 product h3_gemm_32x64, whole tiles' K steps reversed: at 4160x7168x5376 its bytes differ from MLX's gemm_nax (32 x 32 a warp, swizzle 2)'s from byte 1050
CONTROL control: metal h3 product h3_gemm_32x64, whole tiles' K steps reversed: red at shapes, as planted
FAIL shapes control: metal h3 product ragged h3_gemm_32x64_ragged, edge tile's K steps reversed: at 4184x7168x5376 its bytes differ from MLX's gemm_nax (32 x 32 a warp, swizzle 2)'s from byte 59644006
CONTROL control: metal h3 product ragged h3_gemm_32x64_ragged, edge tile's K steps reversed: red at shapes, as planted

test-h3-gemm also checks both against gemm_nax bit for bit and that the guard row tile past the product stays untouched.

  • Each backend's SUMMARY line and exit status: Metal only. tf-kernel-contract --only "h3 product" on the M5 Ultra at 5bd056431 (the kernels and the contract are unchanged since) exits 0:
SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 14170 ms, limits 503 ms

Both h3_gemm_32x64 and h3_gemm_32x64_ragged pass gates 1-4 and launch 128 threads with 0 threadgroup bytes. test-h3-gemm exits 0.

  • Chips I could not run: Metal chips other than 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.
  • 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. If the bits change, the description says which sums change and why.
Before (picture-2048-warm) After (picture-2048-warm)
Before After

How to check

  • Base commit (main): 7bd4b6815acc107a486b88ec0cb98afd8f0e4c68
  • Head commit: 6334933bab5671ea2b4e53db3879941c3cd08cb7
  • Machine: a Mac Studio (Apple M5 Ultra) · macOS 27.0 · Zig 0.17.0

Test results:

  • zig build test-h3-gemm at 5bd056431b17 (older head): 1 passed, 0 failed, 0 skipped
  • zig build test test-golden at 7f199148ea47 (older head): 1910 passed, 4 failed, 30 skipped
  • ./.zig-cache/o/df7000222636bb896dd27c6a9f9914fe/test (the test-h3-gemm binary, run directly after zig build test-h3-gemm --summary all was a cache hit) at 8a6c776cb9c0 (older head): 1 passed, 0 failed, 0 skipped
  • nice -n 10 zig build test test-golden --summary failures at d2af92294558 (older head): 1912 passed, 4 failed, 30 skipped
  • ./.zig-cache/o/ab48407ae88445c88db672e9ad084642/test (the test-h3-gemm binary, run directly after zig build test-h3-gemm --summary all was a cache hit) at d2af92294558 (older head): 1 passed, 0 failed, 0 skipped
  • zig build test test-golden --summary all at eaf1613e92ee (older head): 1905 passed, 4 failed, 30 skipped
  • zig build test-h3-gemm test-metal-runtime-source-modes-host test-h3-geometry compile-h3-host test-kernel-contract --summary all at bbb8e7b91665 (older head): 293 passed, 0 failed, 0 skipped
  • zig build tf-kernel-contract && zig-out/bin/tf-kernel-contract --only "h3 " at bbb8e7b91665 (older head): 15 passed, 0 failed, 0 skipped
  • zig build test-h3-gemm --summary all at 5bd056431b17 (older head): 1 passed, 0 failed, 0 skipped
  • zig build tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast && zig-out/bin/tf-kernel-contract --only "h3 product" at 5bd056431b17 (older head): 2 passed, 0 failed, 0 skipped
  • zig-out/bin/tf-kernel-contract --only h3 at eaf1613e92ee (older head): 19 passed, 0 failed, 0 skipped
  • zig-out/bin/tf-kernel-contract --only "h3 product" at 886ff8fd0b4f (older head): 2 passed, 0 failed, 0 skipped
  • ./zig-out/bin/tf-kernel-contract --only "h3 product" at 886ff8fd0b4f (older head): 2 passed, 0 failed, 0 skipped
  • zig build test test-golden --summary failures at 886ff8fd0b4f (older head): 1905 passed, 4 failed, 30 skipped
  • zig build test test-golden --summary line at 5bd056431b17 (older head): 1918 passed, 4 failed, 30 skipped
  • zig build tf-kernel-contract && ./zig-out/bin/tf-kernel-contract --only h3_gemm_32x64 at 5bd056431b17 (older head): 2 passed, 0 failed, 0 skipped
  • zig build dev-tools -Doptimize=Debug at 3378466a8809 (older head): 1 passed, 0 failed, 0 skipped
  • zig build compile-h3-host test-h3-geometry test-h3-modulation-bf16 -Dcpu=apple_m1 --summary all at 6334933bab56: 79 passed, 0 failed, 0 skipped
  • zig build test-h3-gemm -Dcpu=apple_m1 --summary all at 6334933bab56: 1 passed, 0 failed, 0 skipped
  • zig build dev-tools -Dcpu=apple_m1 --summary all at 6334933bab56: 307 passed, 0 failed, 0 skipped

@ashhart
ashhart force-pushed the feat/108-upstream-pr-h3-metal-products-on-h3-gemm branch from 7f19914 to 21fd564 Compare November 24, 2026 00:03
@ashhart
ashhart force-pushed the feat/108-upstream-pr-h3-metal-products-on-h3-gemm branch from 6334933 to 89d0050 Compare November 24, 2026 06:43
plotarmordev and others added 13 commits November 24, 2026 13:42
…d edge and a ragged tile

(cherry picked from commit 0470bc0)
Each threadgroup is 64 x 128 with four warps of 32 x 64, so a loaded fragment feeds two MMAs where MLX's 32 x 32
warp feeds one, and the K chain is MLX's own: 32-deep steps in order. Every output's sums, the latent and the PNG are
unchanged; test-h3-gemm holds the new kernel against MLX's bit for bit across a band edge, a whole band and a ragged
tail. At 4,120 rows the shapes run at 116, 116, 108 and 103 TFLOP/s against MLX's 118, 118, 113 and 89, so the
products sit at the tensor units' rate and only the 14,336-deep fc2 gains (0.3 s of the six steps); warm 2048
pictures took 12.12 and 12.13 s against 12.30 s.

(cherry picked from commit 8d6f94c)
The image decoder decodes up to its scratch's 64 tiles a pass instead of 16, two passes at 2048x2048 instead of
eight; every tile's pixels are independent, so the latent and PNG are byte-identical.

(cherry picked from commit b6b209f, decoder part only)
@ashhart
ashhart force-pushed the feat/108-upstream-pr-h3-metal-products-on-h3-gemm branch from 89d0050 to cbd2211 Compare November 24, 2026 13:43
@ashhart
ashhart merged commit 3890372 into ashhart:main Nov 24, 2026
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @plotarmordev. 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.

2 participants