Skip to content

h3: MiniMax-H3-Image's edit encoder on Metal in fp32 (tf-h3-image-encoder) - #2778

Merged
ashhart merged 6 commits into
mainfrom
lead/h3-image-encoder
Nov 21, 2026
Merged

ashhart merged 6 commits into
mainfrom
lead/h3-image-encoder

Conversation

@ashhart

@ashhart ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

What this changes

MiniMax-H3-Image's edit encoder runs on Metal. Before, an edit's reference rows had to come from the reference pipeline's case. After, tf-h3-image-encoder turns a reference picture into the clean latent rows (and anchor rows) the transformer conditions on. It is the VAE's CNN encoder in fp32, on any Apple GPU.

  • Host rules (picture_reference.zig), each with tests:

    • the reference's VAE-copy size: short edge into 1024..2048, sides snapped to 32 halves to even, Pillow LANCZOS;
    • the Qwen copy's bound;
    • the 16-pixel crop and the even latent grid;
    • input normalisation;
    • reflect-padding indices;
    • the encoder's layer list, checked against the checkpoint header;
    • the weight packing, taking each 3x3x3 causal kernel's last time slice;
    • clean rows and anchor rows.
  • image_encoder.zig and four Metal kernels:

    • a reflect-padded convolution (3x3, the stride-2 downsample, 1x1);
    • GroupNorm statistics in two passes;
    • GroupNorm with SiLU.

    It runs 256-pixel tiles and blends the 48 moment channels in latent units with picture_vae's rule. The posterior sample is kept as fp16.

  • moss_image/noise.zig: randn now calls a new draw on a generator, so a pipeline can chain draws on one stream (target, anchors, posterior). MOSS-Image's draws are unchanged; its torch digests still match.

  • Issue: n/a. The maintainer asked for MiniMax-H3-Image directly. This is adapted clean-room from a written spec of vLLM-Omni's and SGLang's support. copy_scan against hashes of every upstream file read finds no run of 12 or more tokens.

  • Stacked on: h3: MiniMax-H3-Image's VAE rules on the host, and Pillow's Lanczos-3 in core/picture #2775, which is stacked on h3: MiniMax-H3-Image's transformer on Metal from a picture case (tf-h3-image) #2766. This adds fc73371a6d, 0e39576870, 2c28443709 and dd4ec5e3c0.

  • atc overlap --json: no open pull request touches zig/src/families/h3/ or moss_image/noise.zig. The shared build files gain one line each.

Checks on this head

  • Head commit: dd4ec5e3c0.

  • 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 test-picture-host compile-h3-image-encoder --summary all: 11 of 11 steps. test-h3-geometry has 70 tests, 9 of them new.
    • zig test zig/src/families/moss_image/noise.zig: 3 passed, including the torch digests.
    • Each of the 17 host rules was broken in turn, and each failed a test.
    • zig fmt --check and git diff --check: clean.
  • tf-h3-image-encoder selftest on an M3 Ultra, synthetic weights against a float64 host reference, bounds fixed before the first run:

    Check Measured Bound
    Reflect-padded conv 7.6e-7 1e-5
    Stride-2 downsample 8.6e-7 1e-5
    1x1 with a residual 2.0e-7 1e-5
    GroupNorm + SiLU 3.0e-7 1e-5
    ResNet blocks 8.1e-7 and 6.3e-7 1e-5
    Tiled moments (uneven overlaps) 1.6e-6 1e-4
    Second run bit-identical bit-identical
    fp16 samples within one fp16 step at the largest value

    The fp16 sample gate was first one step per element. Values near zero moved by three of their own (much finer) steps, so it is now one step at the largest sample's magnitude. This was changed after the first run; please review it.

  • All-zero weights at the released widths, built from the published header: every name and shape passes the header check. A 1024x1024 reference encodes in 0.6 s (25 tiles).

  • CI: will run on this head.

  • Not run, and why: a comparison with the release's own moments. That needs the weights, and their download is on hold.

Receipt

  • Main against branch: n/a. This is a tool beside the engine; no token path changes, and MOSS-Image's noise is unchanged.
  • Boxes it reaches: Metal on any Apple GPU, through tf-h3-image-encoder.
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: dd4ec5e3c0, 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 four kernels are new and runtime-compiled for this tool; none replaces an engine kernel. They are gated by tf-h3-image-encoder 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 is an encoder for a denoiser beside the engine.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto today's main, on #2775's new head, so CI runs again (head c555f283d8). The earlier run on dd4ec5e3c0 was cancelled before it finished; no job failed.

On the new head these pass: test-h3-geometry (70 passed), test-picture-host (26 passed, 6 skipped: main's two new JPEG tests and the usual four), compile-h3-host, compile-h3-image-encoder and test-moss-generate-host (5 passed). That last one is main's new MOSS-Image generate driver, which uses the same noise module. The torch digest tests in moss_image/noise.zig passed in CI on #2779 and #2780, which carry this commit.

On the fp16 sample gate that changed after the first run (zig/tests/h3_image_encoder_selftest.zig:14): the new bound is right. Both samples come from the same posterior rule and the same noise, one fed the GPU's moments and one the host's, and those moments differ by about 1.6e-6 of the largest value. Where the mean and the noise term nearly cancel, a sample sits near zero, where fp16 steps are far finer, so that tiny difference can move it by a few of its own steps. A per-element step bound measures that cancellation, not the encoder. The new bound, 1/1024 of the largest sample's magnitude, is one to two fp16 steps at that value, and the gate still compares every sample against it.

What's left:

  1. h3: MiniMax-H3-Image's transformer on Metal from a picture case (tf-h3-image) #2766 and then h3: MiniMax-H3-Image's VAE rules on the host, and Pillow's Lanczos-3 in core/picture #2775 land first.
  2. With the weights: the encoder's moments on a real reference against the reference pipeline's. Their download is still on hold.
  • Synthena (Ash's Agent)

…, grids, encoder layers, clean and anchor rows
…rs and pictures through core, as main does

On main, H3's geometry.zig reads safetensors as core.safetensors. The encoder's module pulls geometry.zig in through
picture_reference.zig, so it now imports core instead of a safetensors module of its own, and image_encoder.zig,
picture_reference.zig and its test read st from core.safetensors. The tool reads PNGs through core.picture, since
core already holds picture.zig and a file may sit in one module of a build only.
@ashhart
ashhart force-pushed the lead/h3-image-encoder branch from 448710e to fed9ec9 Compare November 21, 2026 15:25
@ashhart
ashhart merged commit 760e822 into main Nov 21, 2026
21 of 23 checks passed
@ashhart
ashhart deleted the lead/h3-image-encoder branch November 21, 2026 15:25
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