Repository navigation
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
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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 |
There was a problem hiding this comment.
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]
|
@codex review |
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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]
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # already streaming pageable (no GEMM on it): one more pageable streamer over the new split | ||
| old.remove() |
There was a problem hiding this comment.
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]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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.
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):
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
int8_gemm_route(placement)decides the GEMM per placement, and a streamer with nothing streamed counts as keeping its parameters. That plan takesh3_int8_gemm_streamed, the same GEMM and images as every other stream plan.activation_gb + 1.5, about 7.2 GB at 2048x2048, while about 3.45 GiB was actually reserved.Kill switch:
UNSLOTH_H3_IMAGE_MEASURED_WORKING_SET=0restores the old pricing and turns off the fit step and the backstop.UNSLOTH_H3_IMAGE_INT8_GEMM_STREAMED=0still turns off the streamed GEMM.Large canvases and edits (24 GiB cap)
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: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:
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