Skip to content

Studio: composited-canvas tiled VAE decode for MiniMax-H3 / H3-Image (batched tiles, tile CUDA graphs, fp16 decoder) - #17595

Merged
danielhanchen merged 11 commits into
mainfrom
vae-canvas-tiles
Nov 13, 2026
Merged

danielhanchen merged 11 commits into
mainfrom
vae-canvas-tiles

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Tiled VAE decode for MiniMax-H3-Image (and the MiniMax-H3 video VAE) now composites tiles into one preallocated canvas, decodes many tiles per call, can replay the tile decoder as CUDA graphs, and runs the H3-Image decoder Linears in fp16. At 2048 px the H3-Image decode goes from 2.82 s to 0.280 s on an A100 80GB, and the corner seams of the stock stitch are fixed.

New module studio/backend/core/inference/diffusion_vae_canvas.py. It engages on any VAE whose class carries the stock MiniMax tile loop (matched by a hash of the _blend / _stitch_tiles source); an edited or overridden stitch keeps the stock path.

The levers, each with its own switch (0 turns it off)

  • Canvas (UNSLOTH_DIFFUSION_VAE_CANVAS): each tile is added once into one output buffer, using the stock ramps in the same dtype, with no final torch.cat. The stock stitch blends each tile against its neighbours' raw edges, so where four tiles meet one tile drops out of the blend: with four constant tiles 0 / 1 / 2 / 3 and a 2 pixel overlap an interior corner pixel comes out 2.0 where a normalized overlap-add gives 0. The canvas blends against the already composited strips. Outside the corners the result is bit-identical to stock.
  • Batched tiles (UNSLOTH_DIFFUSION_VAE_TILE_BATCH): batch size from free VRAM, capped at 16, kept steady per tile shape.
  • Tile CUDA graphs (UNSLOTH_DIFFUSION_VAE_TILE_GRAPHS): one graph per tile-batch shape with 1, 2 or 4 replicas, each on its own stream and memory pool (one replica on ROCm). Each replica must match eager bit for bit before use, the arms are raced in place against eager on the real tiles, and eager is kept unless a graph arm is more than 2% faster. Pools are capped at 1 GiB or 4% of the card, only one tile shape keeps graphs at a time, and graphs turn off if the decoder weights move.
  • fp16 decoder (UNSLOTH_DIFFUSION_VAE_HALF, H3-Image only): only the block Linears are cast; norms, layer scales and the residual stream stay fp32. A probe after every decode re-decodes in fp32 on non-finite output and keeps fp32 for the rest of the load.

Results (decode only, real weights and latents, base and head interleaved in fresh processes)

card 2048 px base 2048 px head 1024 px base -> head race picked
A100 80GB 2.73 to 2.86 s 0.280 s (10.1x) 0.58 -> 0.062 s 2 graph replicas
L4 4.55 to 4.80 s 1.005 to 1.028 s (4.6x) 0.97 -> 0.20 s eager (graphs lost the race)
RTX PRO 6000 1.345 s 0.154 s (8.8x) 0.28 -> 0.038 s 1 replica

Where the A100 time goes at 2048 px: canvas alone 2.75 s, plus batch 16 in fp32 1.67 s, plus fp16 0.295 s, plus graphs 0.280 s.

Exactness:

  • Graphs: bit-identical to eager at the same batch and dtype, for 1, 2 and 4 replicas, on A100, L4 and RTX PRO 6000.
  • fp16 vs the fp32 canvas: 77.5 to 81.7 dB PSNR, LPIPS at most 1e-6, worst pixel 0.0040. The fallback never fired on real latents.
  • Batch 16 vs one tile per call: 131 to 138 dB (not bit-exact).

Seam fix on real images. An untiled 2048 px decode is not a usable reference for this decoder (it is shipped with 256 px tiling, and untiled output breaks visibly), so four real 2048 px H3-Image renders were encoded with the VAE's own encoder and decoded back, scored against the original render:

image stock PSNR, all / corners canvas PSNR, all / corners
1 27.304 / 26.607 27.361 / 26.967
2 29.750 / 29.852 29.804 / 30.266
3 30.387 / 31.166 30.449 / 31.721
4 32.061 / 30.854 32.135 / 31.266

Memory on the A100: the decode working set is 0.11 GiB larger, the fp16 decoder weights are 0.75 GiB smaller, and up to 1.0 GiB is reserved for graph pools.

Tests

  • New tests/test_diffusion_vae_canvas.py (26 tests): the corner oracle, canvas equal to stock outside corners at five layouts, kill switches and fallbacks, batching, the fp16 non-finite and flat-image checks, race decisions, the video clip loop.
  • CUDA checks on a T4: graphs equal eager, the race, moved weights, fp16, fallback, graphs with fp16 (6 / 6).
  • CPU: H3 video VAE, canvas, tile geometry, VAE tiling and H3-Image suites, 3265 passed, 34 skipped, 2 xfailed.

Not covered

  • MiniMax-H3 video with real weights (CPU tests on a tiny random VAE only), and a full render rather than decode alone.
  • The L4 decode ran on an otherwise empty card; the streamed planner was not re-checked against the race's transient reserve.
  • ROCm, T4 timing, Windows and macOS.
  • At 1024 px the graph race takes about 14 decodes to decide.

…(corner-correct overlap-add, batched tiles, tile CUDA graphs race, probed fp16 decoder)
…pped replicas undercounted per call), cap kept graph pools at 1 GiB / 4% of the card
…ecided arm whose re-record passes the pool cap goes eager
…cap skip, refused captures retry, OOM drops cut race runs, graphs only for equal tiles, sync before dropping graphs, steady batch per tile shape, per-decode stats, flat images re-checked in fp32 before leaving fp16
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-13T12:41:31.867266Z 47fdd9e 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.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-12T14:38:04.569454Z
We couldn’t complete this review. Comment @codex review to try again.
7b55ead PR opened
ℹ️ 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.

@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: 7b55eadf57

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

for j, (xs, xl) in enumerate(zip(x[0], x[1]))
]
equal = len({t[2].shape for t in tiles}) == 1
batch = tile_batch_size(vae, tiles[0][2], len(tiles)) if equal else 1

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 Honor the speed switch when selecting tile batches

With UNSLOTH_H3_IMAGE_SPEED=0, install_h3_image_vae_canvas() passes speed=False to install(), but this decode path still calls tile_batch_size() unconditionally for equal-sized tiles. On CUDA, that automatically batches up to 16 tiles despite the disabled speed levers, increasing peak decoder memory and changing numerical results relative to one-tile decoding. Persist the batching permission from install() and use a batch size of one when speed optimizations are disabled, while retaining the canvas seam correction.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 4419057. install() now records whether batching is allowed (speed on and CUDA), and tile_batch_size returns 1 when it is not, so UNSLOTH_H3_IMAGE_SPEED=0 decodes one tile per call while keeping the canvas seam fix. An explicit UNSLOTH_DIFFUSION_VAE_TILE_BATCH still wins. Added test_speed_off_keeps_one_tile_per_call.

@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: 4419057f92

ℹ️ 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 +531 to +533
forced = tile_graphs_setting()
if forced == 0 or device.type != "cuda" or not vae.__dict__.get("_unsloth_canvas_graphs"):
return ()

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 Honor the global CUDA graph kill switches for tile decoding

On CUDA, setting UNSLOTH_DIFFUSION_CUDA_GRAPHS=0 or UNSLOTH_DISABLE_CUDA_GRAPH=1 still enables the tile-graph race because this guard checks only the tile-specific switch. diffusion_cuda_graph.cuda_graph_disabled() implements these global kill switches, but neither this path nor TileGraphs.grow() calls it. Consequently, H3-Image decoding still captures and replays CUDA graphs when users explicitly disable them to avoid capture failures or graph-pool memory overhead. Check the shared helper before selecting graph arms so the global switches also disable this new capture path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 75e27b3. Tile graph arms now check diffusion_cuda_graph.cuda_graph_disabled(), so UNSLOTH_DIFFUSION_CUDA_GRAPHS=0 or UNSLOTH_DISABLE_CUDA_GRAPH=1 turns the race and every capture off (TileGraphs.grow is only reached through those arms), and the lever is not reported. The global switch also wins over a forced UNSLOTH_DIFFUSION_VAE_TILE_GRAPHS. Added test_global_cuda_graph_kill_switches_turn_tile_graphs_off.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 75e27b3bd0

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

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. More of your lovely PRs please.

Reviewed commit: 00ab077c3d

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

MiniMaxH3ImagePipeline.release() empties the VAE weights in place so an unload frees the card even while something
outside still references the VAE. The tile graphs' private pools lived in the VAE's __dict__ and stayed reserved in
that case (912 MiB on a B200 with two replicas at 2048 px). Drop them in release().
@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. Swish!

Reviewed commit: 47fdd9e0d0

ℹ️ 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 596238b into main Nov 13, 2026
@danielhanchen
danielhanchen deleted the vae-canvas-tiles branch November 13, 2026 13:12
danielhanchen added a commit that referenced this pull request Nov 13, 2026
…d VAE canvas gates (#17681)

## Summary

Two checks fail on main (`7bf13f2`) and on every PR based on it. The
`(Python 3.13, a-f)` and `(Python 3.13, rest)` shards show them on
staging runs:

```
studio/backend/tests/test_diffusion_rocm_tag_detect.py::test_no_diffusion_or_video_gate_reads_version_hip_directly
E   AssertionError: use core.inference.rocm_bf16.is_rocm_torch(torch): AMD SDK / Radeon wheels leave torch.version.hip unset.
    Raw reads: {'diffusion_torchao_patches.py': [66], 'diffusion_vae_canvas.py': [296]}

studio/backend/tests/test_small_host_dense_denoiser.py::test_qkv_fusion_skips_a_mapped_denoiser[True]
core/inference/diffusion_small_host.py:863: in prepare_streamed_encoder_
E   ModuleNotFoundError: No module named 'diffusers'
```

Both reproduce locally on main (the second with `diffusers` blocked).

### ROCm gates (#17613, #17595)

The lint is right, and both reads are real bugs on AMD SDK / Radeon
wheels, which leave `torch.version.hip` unset and carry the ROCm tag
only in `torch.__version__`:

- #17613 gates the int8 GEMM's transposed-weight path with
`getattr(torch.version, "hip", None) is None`. On a tag-only ROCm wheel
that is true, so the cuBLASLt-only TN layout would be sent to hipBLASLt.
The comment above it says ROCm should keep the row-major copy.
- #17595's `diffusion_vae_canvas._is_rocm()` calls `is_rocm_torch()`
with no argument. That raises `TypeError`, which the `except` swallows.
The fallback then reads `torch.version.hip`, so every call took the raw
read, and a tag-only ROCm wheel got the NVIDIA tile-graph replica count.

### Mapped denoiser test (#17667)

`test_qkv_fusion_skips_a_mapped_denoiser[True]` maps the denoiser
through `prepare_streamed_encoder_`, which uses
`diffusers.hooks.apply_layerwise_casting`. The backend test shards do
not install `diffusers`. The bit-for-bit test in the same file already
guards on it with `pytest.importorskip("diffusers.hooks")`.

## Change

- `diffusion_torchao_patches.py`: `keep_transposed` now uses
`_is_rocm_torch(torch)`, a small helper that calls
`rocm_bf16.is_rocm_torch`.
- `unsloth/import_fixes.py`: `_make_safe_int_mm` must stay AST-identical
to the Studio copy (`test_studio_copy_matches_unsloth_import_fixes`). It
gets the same call, plus its own `_is_rocm_torch` with the same
tag-aware check, so whichever copy patches torchao first behaves the
same.
- `diffusion_vae_canvas._is_rocm()`: calls `is_rocm_torch(torch)`, with
no raw fallback.
- `test_small_host_dense_denoiser.py`: the mapped case calls
`pytest.importorskip("diffusers.hooks")`, like its sibling. The unmapped
case still runs everywhere.

## Guards

- `test_vae_canvas_graph_replicas_see_a_tag_only_rocm_wheel` and
`test_vae_canvas_rocm_check_does_not_fall_back_on_a_cuda_build` in
`test_diffusion_rocm_tag_detect.py`.
- `test_a_tag_only_rocm_wheel_keeps_the_row_major_copy` in
`test_torchao_int_mm_transposed.py`, next to the existing
`torch.version.hip` case.

## Testing

- `test_diffusion_rocm_tag_detect.py`,
`test_torchao_int_mm_transposed.py`,
`test_torchao_intmm_patch_wiring.py`,
`test_small_host_dense_denoiser.py`: 33 passed, 4 skipped (CUDA-only
cases).
- Sabotage: with main's three source files and these tests, 3 fail (the
lint, the VAE canvas tag-only case, the int8 tag-only case).
- With `diffusers` blocked: main's
`test_qkv_fusion_skips_a_mapped_denoiser[True]` fails with the CI error;
here it skips and `[False]` passes.
danielhanchen added a commit that referenced this pull request Nov 13, 2026
…iffusers is missing (#17685)

## Summary

`(Python 3.13, a-f)` fails on main and on every PR based on it since
#17595 (seen on the staging runs for #15275 and #16707): 25 tests in
`studio/backend/tests/test_diffusion_vae_canvas.py`, for example

```
tests/test_diffusion_vae_canvas.py:126: in test_constant_grid_matches_the_overlap_add_and_stock_does_not
tests/test_diffusion_vae_canvas.py:25: in _image_vae_cls
core/inference/diffusion_minimax_h3_image.py:211: in vae_class
core/inference/diffusion_minimax_h3_image.py:178: in _load_vendored
E   ModuleNotFoundError: No module named 'diffusers'
```

`vae_class()` returns diffusers' `AutoencoderKLMiniMaxH3Image` or,
failing that, the vendored copy in
`studio/backend/vendor/diffusers_minimax_h3_image/`. The vendored file
builds on diffusers' own base classes (`from
diffusers.configuration_utils import ConfigMixin`,
`diffusers.models.attention`, ...), so without diffusers both paths
fail. The Studio backend CI shards do not install diffusers.

The product code is fine: Studio only reaches this VAE when diffusers is
installed. Only the test assumed diffusers was there. One test in the
file already guards on it (`pytest.importorskip("diffusers")` at line
439).

Reproduced on main with `diffusers` blocked: 25 failed, 3 passed, 2
skipped.

## Change

`_image_vae_cls()`, the helper every VAE-building test goes through,
calls `pytest.importorskip("diffusers")` first. The tests that do not
build the VAE (the bad-output probe and the graph race bookkeeping)
still run without diffusers. Test-only change.

## Testing

- `diffusers` blocked: 3 passed, 27 skipped. main: 25 failed.
- With diffusers installed: 30 passed, same as main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant