Skip to content

Studio images: MiniMax-H3-Image sequential lockstep batching, opt-in batched forward - #19880

Merged
danielhanchen merged 2 commits into
mainfrom
studio-h3-image-lockstep-sequential
Nov 27, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-h3-image-lockstep-sequential

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

MiniMax-H3-Image has never run a batched forward. Its lockstep mode runs each image as its own batch-1 call through every block, and only the streamed weight upload is shared. This PR adds a real batched forward behind UNSLOTH_H3_IMAGE_BATCHED=1. It is off by default, because it wins only on some device and size combinations.

  • Rows that share a packed layout run every block as ONE call over all rows. The layout is the canvas, text token count and tags, reference grids and steps. Rows with other layouts still share the forward, as separate block calls, so the streamed upload stays one per step.
  • Each row keeps its own state: noise, reference noise, refined text and scheduler. Per-row reference images come in through row_images.
  • The fp32 patch projection and the output head run per row. Without that, a 1e-7 batch-shape rounding in the head grew through the 6 steps to up to 0.24 LPIPS per image. With it, a batched row is bit-identical to its own solo render.
  • The row-mod kernel runs over the flattened rows, with the index repeated per image. Batch 1 calls the original forward.
  • A block call takes at most UNSLOTH_H3_IMAGE_BATCH_TOKENS (4608) packed rows. At 2048 px one image already fills that budget, so each image runs in its own call there.

It also lets the sequential tier denoise a request's images together. On that tier (one big component on the card at a time), every image used to bring the DiT onto the card and back on its own. Now the DiT comes in once for the group, and each image runs each block as its own batch-1 call (lockstep), so its pixels are those of its own render. This is on by default. UNSLOTH_H3_IMAGE_LOCKSTEP=0 keeps one render after another.

row_layout_key(pipe, prompt, ...) in diffusion_minimax_h3_image_batch.py is the match key for a cross-request batcher. It uses only the tokenizer.

Based on main; it needs nothing from the image batching infrastructure (#19837). The sequential lockstep is on by default; the batched forward stays behind its env flag.

Results

int8, Colab RTX PRO 6000 (G4) for the final build, and A100-80 for an earlier build. Bit-identical means equal latent and PNG hashes.

check result
single image, base vs this PR identical hashes (A100-80 resident and streamed, G4 streamed)
target at positions 0 / 1 / 2. Neighbour changes: prompt (same length), seed, input x1e4, per-row reference bit-identical. Identical re-run floor 0 (A100-80 resident, G4 resident, G4 streamed)
member vs own solo / vs closest other member's solo, LPIPS (12 rows) 0.0 / 0.474 or more
batched minus solo LPIPS vs a bf16 DiT + bf16 encoder reference (earlier build, n=12) mean -0.0048, 95% upper bound +0.0022, worst row +0.0064. With the final build, rows equal their solo renders
block calls for a batch of 4 300 (6 steps x 50 blocks), each over 4 rows

Batch of 4, median of 3 runs (spread under 0.5%):

card, placement size batched lockstep serial vs the incumbent
RTX PRO 6000, resident 1024 2.110 s 2.336 s 2.352 s 1.11x
RTX PRO 6000, streamed (16 GB budget) 1024 2.166 s 2.401 s 7.05 s 1.11x
RTX PRO 6000, resident 2048 9.73 s 8.55 s 8.54 s 0.88x
A100-80, resident (earlier build) 1024 3.69 s 3.66 s 3.68 s 1.00x
A100-80, resident (earlier build) 2048 16.38 s 15.01 s 15.01 s 0.92x

Sequential tier, forced with UNSLOTH_H3_IMAGE_PLACEMENT=sequential on an RTX PRO 6000 (Auto: int8, model offload), 1024 px, 6 steps:

  • a batch of 4 takes 11.35 s, against 29.48 s on base (2.6x);
  • every image is bit-identical to its own solo render, and to the base batch;
  • neighbour swaps leave the target row bit-identical;
  • a single image is bit-identical to base.

Changes

  • diffusion_minimax_h3_image_batch.py (new):
    • layout grouping;
    • the batched block calls;
    • the row-mod wrapper;
    • the token budget;
    • row_layout_key.
  • diffusion_minimax_h3_image.py: the row_images argument, batching on resident DiTs, the per-row head, and lockstep on the sequential tier (_sequential_refusal, with the room priced without the waiting DiT).
  • diffusion_minimax_h3_image_speed.py: installs the row-mod wrapper.

Tests

  • tests/test_h3_image_batched_forward.py (new, 13 tests): neighbour invariance, per-row references, call counts and the row-mod wrapper. All 13 fail on base; there, a batch of 4 makes 48 calls of 1 row instead of 12 of 4.
  • tests/test_diffusion_h3_image_lockstep.py is pinned to the lockstep path. It adds a sequential-tier test: the DiT enters once for 3 images, not 3 times, and the latents are bit-identical to separate renders.
  • tests/test_diffusion_minimax_h3_image_parity.py also reads batched rows.

Not covered

  • The final build was not run on an A100. Its A100 numbers come from before the per-row head.
  • A streamed tier on an A100-40.
  • Prompts of different token lengths fall back to lockstep. The fused q/k-norm-RoPE kernel handles batch 1 only, so batched calls use the stock math for that op. That math is bit-exact, but it may explain the 2048 slowdown.
  • This exact head on GPU: the GPU numbers come from the same H3-Image code on an earlier base. On this branch only the CPU tests ran.

H3-Image never ran a batched forward: lockstep ran each image as its own
batch-1 call through every block, sharing only the streamed upload. With
UNSLOTH_H3_IMAGE_BATCHED=1, rows that share a packed layout (canvas, text
token count and tags, reference grids, steps) run every block as ONE call
over all rows; other layouts still share the forward as separate block calls.
Each row keeps its own noise, reference noise, refined text and scheduler, and
per-row references come in through row_images. The fp32 patch projection and
the output head run per row, so a batched row is bit-identical to its solo
render. The row-mod kernel runs over the flattened rows with the index
repeated per image; batch 1 calls the original forward. A block call takes at
most UNSLOTH_H3_IMAGE_BATCH_TOKENS (4608) packed rows.

It is off by default: a batch of 4 is 1.11x the incumbent at 1024 on an RTX
PRO 6000 (resident and streamed) but 1.00x on an A100-80 at 1024 and 0.88x at
2048, so the family keeps lockstep until a measured race picks it.
row_layout_key is the coalescer's match key.

On the sequential tier (one big component on the card at a time) every image
of a request brought the DiT onto the card and back on its own. The images
now denoise together there: the DiT comes in once for the group and each
image runs each block as its own batch-1 call (lockstep), so its pixels are
its own render's; the group's room is priced with the DiT's size deducted
while it waits on the host. RTX PRO 6000 forced sequential, batch of 4:
11.35 s against 29.48 s, every image bit-identical to its own render.
UNSLOTH_H3_IMAGE_LOCKSTEP=0 keeps one render after another.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T04:41:48.794069Z b99c949 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: b99c949aa5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit b29b00a into main Nov 27, 2026
1 check passed
@danielhanchen
danielhanchen deleted the studio-h3-image-lockstep-sequential branch November 27, 2026 05:29
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