Skip to content

Studio MiniMax-H3-Image: keep more DiT blocks on streamed cards and keep the int8 GEMM when all fit (12-18% faster per step at 12-24 GB) - #18284

Merged
danielhanchen merged 8 commits into
mainfrom
studio-h3-image-resident-route
Nov 18, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
studio-h3-image-resident-route

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On cards that stream MiniMax-H3-Image's DiT, this PR keeps more blocks on the card and fixes a slow path that came from keeping all of them there. Warm step time on a capped A100-40 (2048x2048, 6 steps, link reading forced to 3 GiB/s to stand in for a PCIe 3.0 x4 card):

Cap Resident blocks, main to this PR main s/step this PR s/step Change Images vs main
24 GiB 43 to 50 0.7168 0.6188 -13.7% byte-identical, 12 of 12
16 GiB 22 to 29 1.1262 0.9868 -12.4% LPIPS at most 2e-5
12 GiB 5 to 18 1.4651 1.2058 -17.7% LPIPS at most 2e-5
8 GiB (one pair) 0 to 1 1.6164 1.5830 -2.1% LPIPS at most 2e-5
  • Each number is the median of 9 warm renders from 3 fresh processes per tree, run on the same VM in the order B H H B B H.
  • The A/A spread is under 0.3% at every cap.
  • The caps are real: GPU ballast plus a total-memory shim.

For a 24 GB RTX 3090 on PCIe 3.0 x4, the planner now keeps 50 blocks on the card instead of 44 (unit test). The step time on such a card is not measured.

Changes

  1. A stream plan with every block resident keeps its int8 GEMM.
    • On main, when the planner chose the stream tier and all 50 blocks fitted, the list streamer refused ("nothing streams") and neither int8 GEMM installed.
    • That plan ran at 1.307 s/step against 0.571 for the resident tier on A100.
    • Planner state reaches it at about 29.5 to 32.6 GB free (32 GB cards with a few GB in use), and the pricing change below would have made it reachable on every 24 GB card.
    • Fix: int8_gemm_route(placement) decides the GEMM per placement, and a streamer with nothing streamed counts as keeping its parameters. That plan takes h3_int8_gemm_streamed, the same GEMM and images as every other stream plan.
  2. Working set priced from measurement.
    • The stream tier budgeted activation_gb + 1.5, about 7.2 GB at 2048x2048, while about 3.45 GiB was actually reserved.
    • It now uses a per-layout working set measured on A100-40 records: today 1.9 GB, parked 1.2, parked + VAE 1.0.
    • On top of that: 0.30 GB per extra megapixel of DiT rows (canvas x (1 + references)), and a reserve of max(0.75 GB, 3% of the budget).
    • The plan records the layout, working set, slack, block size and canvas.
  3. Per-render fit.
    • The plan is made at load for 2048x2048 with no references.
    • A larger canvas or more references first gives resident blocks back to the stream. The pinned streamer is rebuilt, and blocks that already streamed keep their pinned copies.
    • It never falls back to the pageable streamer while an int8 GEMM is installed.
  4. Out-of-memory backstop. An OOM render on the stream tier frees the cache, streams at least 4 more blocks and renders once more. A second OOM raises as before.

Kill switch: UNSLOTH_H3_IMAGE_MEASURED_WORKING_SET=0 restores the old pricing and turns off the fit step and the backstop. UNSLOTH_H3_IMAGE_INT8_GEMM_STREAMED=0 still turns off the streamed GEMM.

Large canvases and edits (24 GiB cap)

Render main resident / s/step this PR resident / s/step Images
4096x4096 43 / 3.708 50, fit to 41 / 3.709 identical on r0, LPIPS 1e-5 on r1
2048x2048 edit, 3 references 43 / 4.826 50, fit to 41 / 4.829 identical on r0, LPIPS 1e-5 on r1
  • These renders are compute-bound, so 41 resident blocks run as fast as main's 43.
  • The first such render in a session pays the demotion once: 31.4 s vs 27.2 s for 4096x4096, and 47.7 s vs 42.4 s for the edit.
  • Calibration: the largest measured growth (3.74 GB for a 3-reference edit) is inside the model's 3.77 GB plus the reserve.

Why images differ at 16 and 12 GiB

The VAE picks its tile batch from live free memory, and this PR leaves less memory free. The decode batch goes from 16 tiles to 11, which changes cuDNN numerics by at most 1 pixel level. The first render is identical because free memory was still high then.

Testing

  • New test_h3_image_resident_route.py, 20 tests. They enumerate about 39k unforced plans and assert:

    • every placement gets an int8 GEMM route;
    • sequential never comes up;
    • the 50-of-50 stream case is reached.

    They also check that measured plans fit their budget, and cover the fit step, the OOM retry and the kill switch on a tiny CPU release with bit-identical images. The route test fails on main.

  • B200 correctness (no timing), a tiny release on the stream tier, all renders bit-identical:

    • 2 to 1 resident blocks: the nothing-streams streamer becomes the pinned prefetch streamer;
    • 1 to 0: it reuses the first group's pinned copy;
    • a render after an injected OOM.
  • 41 test files that import the changed modules: the same failing test IDs on main and on this branch (14 pre-existing failures).

  • Old-pricing tests: the existing tests that pin old-pricing numbers (the 3090 plan of 31 blocks, the L4 plan of 28) now set the kill switch. The floor test checks both pricings.

  • After merging current main: the route, stream layout and list streamer files give 64 passed, 7 skipped.

Not covered

  • Cards not measured: the physical PCIe 3.0 x4 3090, 32 GB cards, an Ada card on a fast link, L4, T4, Windows and AMD.
  • The 1.307 s/step defect is from an earlier probe on another VM. In this run main plans 43 blocks at the 24 GiB cap, so there was no same-run main arm at 50 resident.
  • The render-time OOM backstop was exercised only on CPU and on B200 with an injected OOM.
  • Free memory between about 4.5 and 7.2 GB: this PR now plans there, where main refused. No render was run in that range.
  • Resident-tier speed: at 50 resident the streamed route runs 0.619 s/step against 0.571 for the resident tier, because it lacks the fused QKV and SwiGLU. That is the next step.

A stream plan that keeps all 50 DiT blocks resident installed neither
int8 GEMM: the pinned streamer refuses when nothing streams, the
pageable one stays, and the streamed GEMM was gated on a streamer that
copies into the same parameters. A streamer with nothing to stream swaps
no parameter, so it now takes h3_int8_gemm_streamed. int8_gemm_route()
names the route per placement; a CPU test enumerates every plan the
planner returns and checks each has one.

The stream tier is now priced from the measured working set instead of
activation_gb() + 1.5 GB margin: per layout at 2048x2048 (today 1.9,
parked 1.2, parked + VAE 1.0 GB, measured on A100-40 over 0 to 50
resident blocks), 0.30 GB per extra megapixel of DiT rows (canvas or
reference pictures), plus a reserve of max(0.75 GB, 3% of the budget).
A 25 GB card on a 3 GiB/s link now keeps 50 blocks instead of 44.

Because the plan is made for the default canvas, a render whose canvas
or reference count needs more working set than planned first gives
resident blocks back to the stream (blocks already streaming keep their
pinned copies), and an out-of-memory render streams 4 more blocks and
runs once more. UNSLOTH_H3_IMAGE_MEASURED_WORKING_SET=0 restores the old
pricing and render path.
…built

A copy the prefetcher carried into the next forward now goes back with
the other groups when detach() hands them to a rebuilt streamer, instead
of waiting for the old prefetcher to be collected.
The measured working set refuses below about 4.5 GB of free GPU memory,
the old pricing (UNSLOTH_H3_IMAGE_MEASURED_WORKING_SET=0) below 7.2 GB.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T13:03:23.895774Z 6198583 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

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b9034f6c9d

ℹ️ 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".

why,
resident,
)
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore resident blocks when pinning fails during demotion

When an all-resident stream plan has the int8 GEMM installed, a pinning failure can leave the pipeline unusable. stream_module_list() moves the newly streamed blocks to CPU before constructing their pinned groups, and its exception handler leaves those blocks on CPU when it returns None. Because the original streamer is a ModuleListStreamer, reuse is None, so this branch returns without rebuilding the original placement. The original streamer has no onload hooks, and the next forward encounters CPU weights with CUDA inputs despite the plan still reporting every block resident. Restore the original resident blocks to the device before returning from this failure path.

Useful? React with 👍 / 👎.

stream_module_list() moved the newly streamed blocks to the host before
pinning them and left them there when pinning then failed, so a
demotion from an all-resident stream plan under the int8 GEMM returned
with blocks on the host, no streamer hooks over them and a plan still
saying every block was resident: the next render failed on a device
mismatch. A failure now puts each block back where it was, which is
what the function's "nothing changed" already promised.

demote_dit_blocks() also keeps the placement consistent on every
failure path: the GEMM guard puts the resident blocks back on the card,
and when even the earlier split cannot be pinned again it falls back to
the pageable streamer over that split instead of raising with no
streamer at all. The plan changes only when the residency did.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fa24fb6e5

ℹ️ 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".

"blocks, the rest stream from pageable memory",
resident,
)
new = ModuleListStreamer(blocks, device, resident)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore row padding before falling back to pageable streaming

If both pinned-streamer rebuild attempts fail after detaching an existing pinned streamer, this fallback installs ModuleListStreamer even when the int8 GEMM is installed. Its parameter swaps make diffusion_int8_gemm._linear_forward() take the stock path, but speed installation already removed the corresponding H3ImageRowPad wrappers. For packed sequence lengths unsupported by cuBLASLt's int8 GEMM, the stock torchao path consequently falls back to the much slower fp32 matmul—the regression those wrappers explicitly prevent. Restore the row-padding wrappers before enabling pageable streaming so this recovery path retains a usable int8 execution path.

Useful? React with 👍 / 👎.

When neither pinned DiT streamer rebuild succeeds, demotion falls back to
the pageable streamer. Its swapped parameters send the int8 GEMM to
torchao's stock path, but the GEMM install had unwrapped the row pads
for the Linears it served, so packed row counts cuBLASLt refuses fell
back to an fp32 matmul. restore_row_pads() wraps every per-row int8
Linear of the DiT blocks again (idempotent, skips absent names) before
that fallback.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6ec0162b8

ℹ️ 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".

Comment on lines +4518 to +4519
# already streaming pageable (no GEMM on it): one more pageable streamer over the new split
old.remove()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate host capacity before demoting resident blocks

When system RAM is tight enough that the encoder loads per prompt, demoting blocks for a larger canvas can invalidate the host-memory safety check in plan_placement(). That check budgets the host-side DiT plus the entire encoder, but this path moves additional blocks to CPU without checking the increased peak; host_need_gb is only incremented afterward. In particular, an existing pageable streamer takes this branch without any pin-budget check, and the subsequent encode_prompt() still loads the whole encoder. A configuration accepted at load time can therefore exceed system RAM and be OOM-killed after a larger render request. Check the prospective host peak before changing placement and reject the render if neither GPU nor host memory can accommodate it.

Useful? React with 👍 / 👎.

A render needing more working set than planned moved resident DiT blocks
to system memory without checking the host plan again: on a machine
tight enough to load the encoder per prompt, the larger render could
push the host-side DiT plus the whole encoder past what the load
accepted and get the process OOM-killed.

demotion_host_refusal() now checks, before anything moves, the load's
host budget with the extra blocks on the host (the encoder kept, or
loaded per prompt, as plan_placement() budgets it) and what is free
right now (the blocks, plus the encoder when it is not loaded). When it
refuses, placement and plan stay as they were and the render is
rejected with a clear error; a render the plan covers still runs.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 6198583ac8

ℹ️ 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 81803c7 into main Nov 18, 2026
@danielhanchen
danielhanchen deleted the studio-h3-image-resident-route branch November 18, 2026 19:17
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…he fused SwiGLU (#18798)

On the MiniMax-H3-Image streamed tier, every MLP ran the stock forward.
This is the tier 24 GB cards get (L4, 3090, a capped Ada). `_install` in
`diffusion_minimax_h3_image_speed.py` installed the fused SwiGLU only
when the levers held the exact string `"h3_int8_gemm"`, but the streamed
route records `"h3_int8_gemm_streamed"`. So on those cards `value *
silu(gate)` was written out in bf16, then rotated and quantised again
for the down projection, in every block and every step. The resident
tiers have fused this since #18090 / #18284.

## Change

- The fused SwiGLU now installs on both int8 GEMM routes.
- This is safe on the streamed route for three reasons:
- The pinned streamer keeps the swapped Linears and copies into their
parameters. That is the condition `int8_gemm_route` already requires
before it picks the streamed GEMM.
- `producer_linear` returns `None`, which falls back to the stock MLP,
for any call whose weight is not on the card.
- `_ready` falls back as well whenever the SwiGLU module or the down
Linear has a hook. The input projection still runs its own forward, so
its hooks are kept.
- The pageable streamer, which swaps parameters per call, still gets
neither the GEMM nor the fused MLP.
- `UNSLOTH_H3_FUSED_SWIGLU=0` keeps the stock MLP, as before.
- Q / K / V fusion stays resident-only. Merging the three weights into
one buffer would leave the streamer copying into parameters the GEMM no
longer reads.

## Tests

- `test_the_fused_swiglu_installs_on_every_int8_gemm_route` (CPU, 5
cases): pinned streamed installs it, resident installs it,
`UNSLOTH_H3_FUSED_SWIGLU=0`, `UNSLOTH_H3_IMAGE_INT8_GEMM_STREAMED=0` and
the pageable streamer do not.
- The streamed-pinned case fails on main and passes here
(`test_differential.py`: DISCRIMINATING, no head failures).
- `tests/test_diffusion_minimax_h3_image_speed.py`,
`tests/test_h3_fused_swiglu.py`,
`tests/test_h3_image_resident_route.py`: 62 passed.

## GPU (Colab L4, 22.5 GB: the streamed tier)

H3-Image at 1024x1024, 6 steps, `streamed` placement. Main and this
branch ran in fresh processes on one VM, sharing one Studio home, with 2
warm-up cells and then 3 interleaved reps per arm. The batch ran twice,
on two VMs, after an infrastructure retry.

| VM | main s/step | branch s/step | delta |
|---|---|---|---|
| 1 | 0.8628 | 0.8472 | -1.8% |
| 2 | 0.8652 | 0.8518 | -1.5% |

Medians of 9 steady renders per arm.

- **Fused-SwiGLU calls:** 1500 to 2100 per cell here, 0 on main.
- **Images:** identical to main, with LPIPS 0.000 and `torch.equal` on
all 4 prompts on both VMs. Every arm also repeats itself bit for bit.
- **Peak allocated memory:** 17.98 GiB on both arms.

## Not proven here

- sm86 (3090) is not a Colab tier.
- No larger-card resident-tier comparison ran. It is not needed for
accuracy, because the streamed images match main bit for bit.
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