Repository navigation
h3: MiniMax-H3-Image's prompt encoder, transformer and decoder resident across pictures - #3718
Conversation
7475720 to
d4392a0
Compare
|
Thanks @plotarmordev. Keeping the three stages loaded is a clear win to aim for, and the resident-against-fresh tests in
|
d4392a0 to
10f5136
Compare
|
For the earlier bind failure in point 3, I ran I added the following before the late-allocation check in var no_weights = f.folder.ck;
no_weights.shards = no_weights.shards[0..0];
var early = std.testing.FailingAllocator.init(testing.allocator, .{ .fail_index = std.math.maxInt(usize) });
try testing.expectError(error.MissingTensor, h3.Model.bind(early.allocator(), no_weights, f.cases[0].case.file, .{}));
try testing.expectEqual(@as(usize, 1), early.alloc_index);
try testing.expectEqual(early.allocated_bytes, early.freed_bytes);
try testing.expect(device.allocated() <= ceiling);With that check, Igor Lessio - AIFLOW LABS |
|
I checked the combined #4276 landing train at zig build tf-h3-image-server -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all
zig build test-h3-image-prompt test-h3-geometry test-h3-boundaries -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary allThe executable built and all 123 host tests passed. This run did not load the model. The train's check rollup currently shows only skipped jobs. I also ran The same train subsequently passed all five After #2766 landed, I trial-restacked On M5 Max with Zig 0.17.0, this scratch tree built both image executables and passed all 128 tests with: zig build test-h3-image-prompt test-h3-geometry test-h3-boundaries test-h3-image-server tf-h3-image-server tf-h3-image compile-h3-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary allThe standalone prompt, encoder and decoder tools also built. The checkpoint reference runs and the stack decision with #3733 remain open. Igor Lessio - AIFLOW LABS |
10f5136 to
a60e220
Compare
a60e220 to
5dceb4c
Compare
|
I verified the current landing train #4445 at exact tip zig build tf-h3-image-server test-h3-image-server test-h3-image-prompt test-h3-geometry test-h3-boundaries -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j4 --summary allAll 57 steps succeeded, with 128 tests passed and zero failures or skips. The H3 server built and all five resident-server tests ran, including the synthetic fresh-vs-resident Metal comparison and failed-bind unwind. The production checkpoint and a live image benchmark were outside this run. Igor Lessio - AIFLOW LABS |
|
The current published Metal head zig build tf-h3-image-server test-h3-image-server test-h3-image-prompt \
test-h3-geometry test-h3-boundaries -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary allThe current CUDA head #3733 at Igor Lessio - AIFLOW LABS |
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.
…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.
…es the canvas and the seed's starting rows tf-h3-image-prompt takes an optional size, seed and smallest area: it writes the canvas, the requested size and the starting rows (the seed's first torch CPU randn over the latent, patchified) beside the prompt's rows, a whole text-to-image case. tf-h3-image's free run exits 0 with nothing to gate. The recipe runs prompt, transformer and decoder in turn.
…at once and at a canvas's ratio
…nt across pictures tf-h3-image-server loads the three tools' weights once and serves one text-to-image request per stdin line. A request whose canvas, row count, reference count and tags match the last one's keeps the bound transformer and only replaces the prompt's text rows (Model.retext); another layout releases it and binds anew over the same mapped weights. The weights sit in one residency set across the three stages' queues, so macOS does not re-wire them on every request, and the idle keepalive keeps them wired between pictures while holding its ticks off during a request. A set that cannot be held or a ticker that cannot start stops startup, so the server never serves requests unwarmed. Model.load splits into open (the checkpoint mapped and read ahead) and bind (kernels and buffers for a case), and release leaves the checkpoint open; a failed bind frees every Metal object it made. The three tools load their weights once and run a picture as functions (Encoder, Folder, Vae), so the server and the one-shot tools share the same steps. The image tools' modules read safetensors and checkpoint fixtures through core, following main's core module split. metal_targets.zig was 613 lines once the H3-Image tools were registered, over the 600-line limit. FastH3's transformer and the H3-Image tools now register from h3_metal.zig, leaving metal_targets.zig at 589.
One process for every picture warm: the request line, binding reuse and retext, the weights held in one residency set, and the synthetic gates.
Follow main 4d244a0 (h3: use the current reader and share Metal tool registration). Keep its shared clip/picture tool constructors and register the incoming encoder, prompt, decoder and resident server beside them.
5dceb4c to
1e563e8
Compare
|
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. |
Builds on #2766, #2775, #2778, #2779, #2780 and #2781; until those merge, this diff includes their commits; I'll rebase as they land. Rebased on
mainate7ff97e13.What this changes
Before, every picture made by the H3-Image tools loaded the prompt encoder, the transformer's weights and the decoder again, and macOS unwired idle GPU memory between stages. Now
tf-h3-image-serverloads the three once, serves one text-to-image request per stdin line, and keeps the weights wired with the shared idle keepalive; a request whose canvas, rows, references and tags match the last one's replaces only the prompt's text rows (Model.retext), and another layout binds anew over the same mapped checkpoint. If the weights cannot be held resident or the idle ticker cannot start, startup fails instead of serving requests unwarmed.63df3ed32(server,Model.open/bind/release/retext, the tools'Encoder/Folder/Vae, startup refusal, and the tools' build registration inzig/build/h3_metal.zig),d4392a0c4(recipe). They also read safetensors and checkpoint fixtures throughcore, following main's module split.atc overlap --json(2026-11-18): incomplete (pullFilesUnavailable,timeLimit,workingTreeUnavailable); no overlaps reported.Checks on this head
d4392a0c415bd1d85c950c1a1b628df66489f8f7.python3 tools/pr_check.py --base refs/swarm/upstream/ashhart/TensorFold/main --pr 3718exits 0: lean 221 on this head against 221 on base, 0 new.zig/build/metal_targets.zigis 589 lines; the H3-Image tools and FastH3's transformer register fromzig/build/h3_metal.zig, so the registration no longer pushes the upstream file past 600.zig fmt --check zigandgit diff --checkpass;swarm scanis clean.zig build test-h3-image-serverpasses (the synthetic binding-reuse, failed-bind, idle-interval, over-long-line, and the two startup-refusal gates: weights past the residency limit and a ticker that cannot start).zig build test test-golden: 891/894 steps, 432/448 tests, 1 failed. The failure is inherited and untouched:net.test.stageexpectsAddressUnresolvedforno-such-host.invalid, and this machine's resolver answers it withAddressInUse; the diff againstmainunderzig/src/fabricis empty. Goldens: 55/55 and 322/335, 0 unexpected differences.Receipt
n/a:
tf-h3-image-serveris a developer tool beside the engine and changes no served path, tokens or arithmetic. No speed claim is made.Checklist