Skip to content

Studio video: decode Wan VAE tiles on both cards when the decode is tiled and the second card has room - #18829

Merged
danielhanchen merged 9 commits into
mainfrom
diffusion-mgpu-vae-decode
Nov 22, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
diffusion-mgpu-vae-decode

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

On a multi-GPU selection, Studio's tiled Wan VAE decode now shares its spatial tiles with the second card: the load card decodes every other tile while a short-lived copy of the VAE decodes the rest on the partner card, and the tiles are blended on the load card exactly as diffusers does. The output is bitwise identical to the one-card tiled decode.

Each spatial tile of AutoencoderKLWan.tiled_decode is an independent causal decode over every frame (its own feature cache), so the split changes where a tile runs, not what it computes. Temporal chunks are not split (the causal cache makes them dependent).

When it engages

  • Only for tiled decodes (tiled_decode is only reached when the decode is tiled: speed tier off, or a default-tier decode whose untiled peak does not fit).
  • Only under a multi-GPU selection (Automatic or two or more picked cards), only for AutoencoderKLWan, and only when the installed tiled_decode body matches the mirrored diffusers body (checked by an ast.unparse fingerprint; diffusers 0.42.1 and 0.43.0). Anything else keeps the stock decode.
  • Only when the partner card has the copy plus the tile working set free, counting reusable allocator blocks (after a CFG replica, if one is placed there). Otherwise the decode stays on one card.
  • UNSLOTH_VIDEO_VAE_MULTIGPU=0 turns it off.

Stream discipline

The copy is built and its tiles decoded on a dedicated partner stream that first waits for the caller's stream. Copies from the load card run with the caller's stream current for that card, and the decoded tiles come back on the caller's stream after it waits for the partner stream. Nothing on the partner card is freed before that stream finishes. The copy is collected (it sits in reference cycles) and the partner cache emptied after each decode, so the next render finds the memory free again.

An earlier version that built the copy on the caller thread and relied on implicit cross-card copies on the default streams aborted inside Studio's render with CUDA error: unspecified launch failure (it ran clean under CUDA_LAUNCH_BLOCKING=1). This version has not faulted in any run below.

Results (Kaggle 2x T4, Wan 2.2 TI2V 5B, fp32 VAE)

Standalone VAE decode, random latents, median of 3, bitwise vs the stock tiled decode in every case:

size tiles one card tiled split untiled
480x320x17 6 21.2 s 13.1 s 14.6 s
832x480x33 15 128.9 s 65.3 s 74.8 s
1280x704x49 28 424.9 s 215.3 s OOM

Inside Studio, Automatic placement (CFG replica on the second card), speed tier off, 480x320x17, 6 steps, 2 renders per load, fresh Studio home per load, three kernels, main as base in the same kernel (main's Automatic decode stays on one card: 20.5 - 25.0 s):

VAE decode (render 1 / 2) render (1 / 2) frames vs one card
one card 20.5 - 24.9 s 44.4 - 48.1 s / 30.2 - 34.3 s reference
this PR, Automatic 11.6 - 13.9 s 33.5 - 36.3 s / 18.8 - 22.0 s bitwise

Default tier at 1280x704x17: the decode uses wide tiles that need about 9 GB on the partner card, which the CFG replica leaves only about 5 GB of, so the split correctly stays on one card (no change in output or speed).

Tests

  • tests/test_video_vae_multigpu.py (CPU): split equals the stock tiled decode bitwise with and without patch_size, with a supplied copy and with the copy the split builds itself; a changed tiled_decode body or another VAE class is refused; install / uninstall routing and the one-card fallback; forward hooks stay on the source; the partner free-memory read counts reusable blocks and the working set scales with the tile size; a copy holding tensors on another card is detected.
  • The partner thread decodes under the caller's CUDA autocast state (autocast is thread-local), and a load-card failure still synchronizes the partner stream and releases the copy. test_split_tiles_keep_the_callers_autocast_on_two_cards (two CUDA cards, fp16 autocast, bitwise against the stock tiled decode under the same autocast) and test_a_failing_load_card_still_releases_the_copy both fail with the previous module and pass with this one. Kaggle 2x T4, same kernel: previous module 2 failed / 8 passed, this branch 10 passed with no skips.
  • Existing VAE OOM, untiled-decode, stream-encode and multi-GPU suites pass.

Speed paths and Automatic placement

  • The default speed tier installs fused Wan forwards that close over the load card's modules. The copy's forwards are now rebuilt against its own modules, and a copy that still reaches the source is refused (one-card decode). Kaggle 2x T4: the previous head crashed cross-device with the fused forwards; this branch passes 18 / 18 there, the fused path runs on both cards, and the output matches the one-card fused decode within an ulp (4.5e-7 in fp32).
  • Under Automatic GPU placement the split only borrows an idle card of the same model (the CFG copy's rule); an explicit multi-card pick keeps it, and UNSLOTH_VIDEO_VAE_MULTIGPU=force restores it under Automatic.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 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-22T10:22:32.674769Z a4219df 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 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: 153a7dd2dc

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

…ase the copy when the load card fails

[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: 25dcc545c1

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

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

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

…e same model

Automatic (no gpu_ids) selections hold every visible card, and the split took the
first secondary whenever torch reported room, opening a CUDA context on it and
decoding beside whatever another process runs there. Pick the partner the way
the automatic CFG copy does (automatic_partner over nvidia-smi probes: idle,
same model, room for the copy); explicit multi-card picks are unchanged and
UNSLOTH_VIDEO_VAE_MULTIGPU=force keeps the old behaviour under Automatic.
…skip a non-mirrored diffusers body

[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: f5a7050c83

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

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

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: a4219df02b

ℹ️ 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 ee02c63 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the diffusion-mgpu-vae-decode branch November 22, 2026 19:24
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…r the two-card Wan decode with the VAE seam guard (main reds) (#18945)

## Summary

Two checks are red on `main` and fail on every PR that runs them, for
example the staging run for #18941:

| Failing check | Introduced by | Which side was wrong | Fix |
|---|---|---|---|
|
`tests/test_python39_compatibility.py::test_studio_evaluated_unions_do_not_grow`
(`Core`, all three combos): "36 studio files now evaluate PEP 604 unions
on the floor, against 35 recorded" | #18917 added
`core/inference/diffusion_lanczos_triton.py` | Code. Its public entry
point declares `crop`, `mean` and `std` as `tuple[...] \| None`, which
is evaluated when the function is defined and raises `TypeError` on
Python 3.9. | Those three parameters become `Optional[tuple[...]]`. No
`from __future__ import annotations`: the module's Triton kernels read
their `tl.constexpr` annotations, which that import would turn into
strings. |
|
`studio/backend/tests/test_vae_tile_geometry.py::test_every_tiled_decode_patch_is_probed`
(`seam-guard`): "['video_vae_multigpu'] rebind a VAE's tiled_decode but
expose no install(vae) this guard applies" | #18829 added
`core/inference/video_vae_multigpu.py` | Test registry. The module
mirrors the stock `AutoencoderKLWan.tiled_decode` body (checked by AST
fingerprint) and only shares its tiles between two cards. It keeps the
stock tile grid, and
`test_video_vae_multigpu.py::test_split_tiles_match_the_stock_tiled_decode_bitwise`
proves it. Its `install(pipe, partner)` takes a pipeline, not a VAE, so
the guard cannot apply it. | Listed in `GEOMETRY_PRESERVING` with that
reason, which is the remedy the guard's message names for a module that
keeps the stock tiles. |

Neither fix changes behaviour.

## Testing

- `tests/test_python39_compatibility.py`: fails on `main` with the
message above, 8 passed with this change.
- `tests/test_vae_tile_geometry.py` and
`tests/test_video_vae_multigpu.py`:
`test_every_tiled_decode_patch_is_probed` fails on `main`. With this
change, 55 passed, 2 skipped, 2 xfailed (the recorded known seams).
- `tests/test_diffusion_ref_preprocess.py` and
`tests/test_diffusion_ref_preprocess_gate.py`, the suites that exercise
the Lanczos module: 33 passed.
- `ruff check --select F` on the Lanczos module: clean.
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