Skip to content

h3: MiniMax-H3-Image's image decoder on Metal in bf16 (tf-h3-image-decoder) - #2780

Closed
ashhart wants to merge 9 commits into
mainfrom
lead/h3-image-decoder
Closed

ashhart wants to merge 9 commits into
mainfrom
lead/h3-image-decoder

Conversation

@ashhart

@ashhart ashhart commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

What this changes

MiniMax-H3-Image's image decoder runs on Metal. Before, tf-h3-image's latent could only be decoded by the reference pipeline. After, tf-h3-image-decoder turns the latent into a PNG on any Apple GPU.

It is the VAE's 2D ViT decoder: 24 blocks of 1,024 width, 16 heads of 64, and 4 register tokens. It runs in 256-pixel tiles, blended in float32, with ImageNet pixels and truncating bytes. When the requested size differs from the canvas, it resizes back with Pillow's LANCZOS.

Checks on this head

  • Head commit: 9eb514c504. On the whole stack: zig build test-h3-geometry test-h3-image-prompt test-picture-host compile-h3-host compile-h3-image-encoder compile-h3-image-prompt compile-h3-image-decoder-host --summary all gives 18 of 18 steps, 116 passed and 4 skipped (the usual four), and all three tools build.

  • Lean checker: 386 on the base and 386 on this head, 0 new. tools/pr_check.py: 0 errors (the one advisory warning is the Unreleased line, which the lead adds).

  • Tests run, with the command and pass, fail and skip counts:

    • zig build test-h3-geometry compile-h3-host compile-h3-image-decoder-host test-picture-host --summary all: 11 of 11 steps. test-h3-geometry has 68 tests, 7 of them new.
    • Each host rule was broken in turn, and each failed a test.
    • compile-h3-image-decoder-host is added to host CI's macOS list.
    • zig fmt --check and git diff --check: clean.
  • tf-h3-image-decoder selftest on an M3 Ultra, with synthetic weights against the float64 reference. The bounds were set from the host reference's own float32-against-float64 spread before the GPU ran.

    Check Bound Measured
    Embedding ≤ 1 ULP 0-1 ULP
    Each block (teacher-forced) ≤ 8 ULP, ≥ 90% identical, rel ≤ 2e-3 worst 4 ULP, 93.5-95.6%, rel ≤ 8.1e-4
    norm_out + proj_out ≤ 1 ULP 1 ULP
    Output after 2 blocks ≤ 8 ULP, rel ≤ 6e-3 3 ULP, 2.84e-3
    Output after 24 blocks rel ≤ 3e-2 1.49e-2
    Decodes in batches of 3 and 4 bit-identical to tiles one at a time bit-identical
    Picture bytes ≥ 80% identical, ≤ 2 apart 91.8%, at most 1

    24 blocks take 13.4 ms a 16 x 16 tile, about 1.6 s of GPU time for a 2048 x 2048 canvas.

  • A run of the whole tool on all-zero weights at the released shapes (built from the published header) writes a uniform picture of the ImageNet mean, as it should.

  • CI: will run on this head.

  • Not run, and why: the comparison with the release's decoder (PSNR ≥ 45 dB, ≥ 99% of bytes within ±1). It needs the weights, and their download is on hold.

  • Open point for that comparison: attention keeps its probabilities in float32 for the value product. The reference's fused attention may round them to bf16 first.

Receipt

  • Main against branch: n/a. This is a tool beside the engine; no token path changes.
  • Boxes it reaches: Metal on any Apple GPU, through tf-h3-image-decoder.
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: 0c0be0c438, Zig 0.17.0, an M3 Ultra under macOS. Shapes from MiniMaxAI/MiniMax-H3-Image at 492fd8c4ec.
  • Paths and platforms the change reaches, which I ran and which I could not: Metal (ran on an M3 Ultra). No CUDA.
  • Exactness, decode and prompt speed: n/a.
  • Licence of any new weights or drafter: n/a, no weights.
Kernel contract
  • The 8 kernels are new and runtime-compiled for this tool; none replaces an engine kernel. They are gated by tf-h3-image-decoder selftest against a float64 reference, not yet registered with tf-kernel-contract.

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.

The inference checklist does not apply: this decodes a denoiser's latent, beside the engine.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto today's main, on #2779's new head, so the stack's CI runs on one base (head 325bd04876). CI on the old head 9eb514c504 was green.

On the new head these pass: test-h3-geometry (77 passed), test-h3-image-prompt (13 passed), test-picture-host (26 passed, 6 skipped: main's two new JPEG tests and the usual four), test-h3-modulation-bf16, compile-h3-host, compile-h3-image-encoder, compile-h3-image-prompt and compile-h3-image-decoder-host. That is 21 of 21 steps, 117 passed and 6 skipped. zig fmt --check and the lean checker are clean on every changed file.

What's left:

  1. h3: MiniMax-H3-Image's transformer on Metal from a picture case (tf-h3-image) #2766, h3: MiniMax-H3-Image's VAE rules on the host, and Pillow's Lanczos-3 in core/picture #2775, h3: MiniMax-H3-Image's edit encoder on Metal in fp32 (tf-h3-image-encoder) #2778 and h3: MiniMax-H3-Image's prompt path on Metal: Qwen3-VL to layer 50 and the token refiner (tf-h3-image-prompt) #2779 land first, in that order.
  2. With the weights: the comparison with the release's decoder (PSNR at least 45 dB, at least 99% of bytes within one). It also settles the open point on whether attention's probabilities round to bf16 before the value product. The weights' download is still on hold.
  • Synthena (Ash's Agent)

Each text segment is tokenised on its own and cut to 4,096 tokens; references enter as a label and a vision
block whose rows are tagged 0. Positions follow Qwen-VL's M-RoPE, and the rotary table rounds to bf16. The Qwen
copy of a reference keeps or scales its short edge to 1,024 with Pillow's LANCZOS. The text encoder's and the
refiner's configs are checked at the released geometry.
tf-h3-image-prompt turns a prompt into the text rows tf-h3-image reads: Qwen3-VL's text model to the hidden
state leaving layer index 49, then condition_proj, the two token-refiner blocks over every row and the final
norm. Weights are bf16 read in place; products run in simdgroup matrices with fp32 sums, so chips without
tensor units run it too. The encoder also takes a reference's vision rows and DeepStack features at their image
pads, ready for the edit's tower.

Its selftest writes a tiny synthetic checkpoint, loads it as a real run does, and gates each kernel alone and
each whole run against a float64 host reference with the same bf16 rounding points.
… its fixture through core, as main does

prompt.zig pulls in tensors.zig, which on main reads core.safetensors, so the tool's prompt module imports core. A file
may sit in one module of a build only, and core already holds picture.zig, qwen_vl/rope.zig and checkpoint_fixture.zig,
so picture_prompt.zig, its test and the tool read them through core, and the build's own picture, rope and fixture
modules go. test-h3-image-prompt's root imports core in their place.
…d rotary table, unpatchify

image_decoder.zig: the 297 decoder tensors MiniMax-H3-Image's VAE file holds
(F32, checked by name and shape, decoder.mask_token left unread), their
places in one bf16 buffer, a tile's sequence (its cells, 4 registers, one
zero token), the 2D rotary ids in bf16 arithmetic and their angles, the
proj_out rows unpatchified to pixels, and the tile geometry the attention
holds (at most 16 cells a side). Host tests run in test-h3-geometry.
… on the host

image_decoder_metal.zig runs the VAE's 2D ViT decoder on a batch of tiles
a command buffer, on any Apple GPU (its kernels compile from source at
load, no tensor units): post_quant_conv and x_embedder, the 24 blocks
(RMSNorm, grouped q | k | v, q and k normed and turned half-style over
their first 32 dims, softmax attention at 1/8, LayerScale residuals, the
gated SiLU FFN), norm_out in fp32 and proj_out. Products are bf16 with
fp32 sums, rounded where the reference rounds. decode() blends the tiles
in fp32 with picture_vae's Assembler. image_decoder_reference.zig is the
same forward on the host with float64 sums, for the self-test.
…self-test

tf-h3-image-decoder MODEL_DIR latent.f32 ROWS COLUMNS out.png [WIDTHxHEIGHT]
reads vae/config.json and the decoder's tensors, denormalises the latent,
decodes it in tiles of up to 16 a pass, blends them, truncates to bytes,
resizes with Pillow's LANCZOS when asked and writes a PNG.

`selftest` runs the kernels on synthetic weights against the float64 host
reference: the embedding and proj_out within 1 bf16 ULP, each block
teacher-forced, two and all 24 blocks chained, batched decodes bit-identical
to the staged tiles, and the picture's bytes. It exits 1 past a bound.

zig build tf-h3-image-decoder builds it (shaders compile at load);
compile-h3-image-decoder-host compiles it without the Metal compiler and
joins the macOS nightly host checks. picture_writer.zig roots MOSS-Image's
PNG writer and core's resampler in one module for tools beside the engine.
… main does

image_decoder.zig pulls in geometry.zig, which on main reads core.safetensors, and image_decoder_test.zig runs in
test-h3-geometry, whose module imports core alone, so the decoder's files read st from core.safetensors.

The tool also writes PNGs with MOSS-Image's writer, whose test reads core/picture/png.zig by path, and a file may sit
in one module of a build only. So the tool's core is h3_decoder_core.zig, one module holding safetensors, core's
pictures and that writer, in place of picture_writer.zig; it resamples through core.picture.
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner Author

Closing this one: all of its work is on main, some of it moved or renamed since this branch was cut, so nothing is left to merge.

  • Synthena

@ashhart ashhart closed this Nov 23, 2026
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