Repository navigation
Studio: pad Wan causal convs into a zero buffer on Metal - #18932
Merged
Merged
Conversation
Before torch 2.15, MPS constant pad is an MPSGraph op that keeps driver memory outside the allocator on every call (pytorch/pytorch#195368), and the Wan VAE pads before every causal Conv3d. Apple Silicon installs torch 2.13, so install_frame_pad_fix now also gives WanCausalConv3d a forward that writes the cache frames and the input into one zero buffer (the same values, no F.pad), and the video loader installs it next to the VAE sync.
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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
[skip ci]
danielhanchen
added a commit
that referenced
this pull request
Nov 26, 2026
…#19616) ## Summary On MPS before torch 2.15, a 5-D `F.pad` costs far more driver memory than the conv it feeds. #18932 fixed this for Wan's `WanCausalConv3d` by padding into a zero buffer. `QwenImageCausalConv3d`, used by the Qwen-Image VAE (Qwen-Image, Krea-2, Anima), has the same 6-tuple causal `_padding` and the same `F.pad` forward, but was not covered. This change: - Adds `QwenImageCausalConv3d` to `_CAUSAL_PAD_CLASSES`, so it takes the same zero-buffer forward on MPS, but only on one-frame calls without a cache. That is the call shape every image encode and decode makes, and the one measured below. Multi-frame and cached calls keep the stock forward, since their MPS numerics through the zero buffer are not established (see Not covered). - Fixes the single-frame speed pass (`diffusion_vae_single_frame`). It runs after the MPS pad fix and binds its own forward on every causal conv. On every call it did not take itself (tiled decodes, more than one frame, a cache), it fell back to the class `forward`, the stock `F.pad`. That silently discarded the zero-buffer patch, so adding the class alone would have done nothing on speed-on loads. The fallback now calls the forward that was bound before it, if there is one. Other devices are untouched: `install_frame_pad_fix` returns early off MPS. ## Measurements GitHub macos-15 runner (M1, MPS cap 7.93 GiB): - Qwen-Image VAE (`Qwen/Qwen-Image` `vae/`, the Krea-2 VAE), fp32. - Untiled 512x512 decode (64x64 latent) repeated 20 times. - One fresh process per arm, interleaved stock / patched x3. | torch | arm | runs that finished | driver memory after decode 1 / 20 | s per decode (median) | output | |---|---|---|---|---|---| | 2.13.0 | stock | 2 of 3 (1 MPS out-of-memory) | 6.86 / 7.73 and 6.86 / 8.09 GiB | 12.2, 10.5 | corr 0.9999999999995 vs CPU fp32 | | 2.13.0 | zero buffer | 3 of 3 | 3.36 / 4.39 GiB (flat after decode 2) | 8.5, 8.2, 8.5 | byte-identical to stock (same sha) | | 2.14.1 | stock | 0 of 3 (MPS out-of-memory in all 3) | - | - | - | | 2.14.1 | zero buffer | 3 of 3 | 4.28 / 4.28 GiB | 24.0, 24.3, 23.8 | corr 0.9999999999993 vs CPU fp32 | Every patched run patched all 61 causal convs. ## Tests `tests/test_diffusion_mps_qwen_causal_pad.py` (CPU) checks the following: - the Qwen causal convs patch and decode bit-identically to stock; - a one-frame decode with the pad fix takes the zero buffer on every causal conv call and never reaches the stock forward; - a three-frame decode, with and without the single-frame pass, sends every multi-frame or cached call to the stock forward and only the first chunk's one-frame calls to the zero buffer, bit-identical to stock (fails without the one-frame gate); - with Studio's order (pad fix, then the single-frame pass), a one-frame decode still takes the 2D path, and a three-frame decode still reaches the bound pad-fix forward; - the single-frame pass alone still falls back to the class forward; - other devices are untouched; - on MPS only (skipped elsewhere), a tiny one- and three-frame decode with the pad fix matches CPU. `test_diffusion_device.py` and `test_diffusion_vae_single_frame.py` pass (113 passed, 1 skipped). ## Not covered - Multi-frame decodes on MPS have no valid comparison. A 3-frame 384x384 decode ran out of memory on stock under both torch versions. The patched arm finished, but near the memory cap (6.7-7.3 GiB driver, 25-147 s per decode) and with wrong values (correlation 0.18 / NaN against CPU). These image families decode one frame, so this is outside their path. Because the cause (zero-buffer path or memory pressure) is unknown, multi-frame and cached calls are left on the stock forward here. - `QwenImage21CausalConv3d` (a Conv2d with 4-D padding) is not covered. Only 5-D pads were measured. - The single-frame pass's own 4-D pad was not measured on MPS. --------- Co-authored-by: danielhanchen <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Apple Silicon, Studio's Wan VAE decode now writes each causal conv's padding into a zero buffer instead of calling
F.pad. The output is bit-identical, and the decode needs less Metal driver memory.Before torch 2.15, MPS constant pad is an MPSGraph op. Each call keeps driver memory outside the caching allocator (pytorch/pytorch#195368, which ships in 2.15). The Wan VAE pads before every
WanCausalConv3d, a few hundred times per decode tile. Upstream measured 270 GiB of total driver memory for one Wan 2.2 TI2V-5B decode, against 8.1 GiB with the fix. Studio's Mac installs pin torch 2.13 (install.sh), so that fix cannot reach them.Changes:
install_frame_pad_fix(diffusion_device.py) also gives everyWanCausalConv3dan instanceforward. It allocates the padded shape once withnew_zeros, then copies the cache frames and the input into it. The values are the same asF.pad(cat([cache_x, x])), with no pad op.forwardis already wrapped, such as by an offload hook.install_frame_pad_fixnext toinstall_vae_sync. Until now only the image loader called it. Like before, it is a no-op off Metal.Evidence (macos-15 staging, Apple M1 (Virtual), torch 2.13.0, diffusers 0.42.1)
The real Wan VAE weights decode a fixed-seed latent, with each arm in its own process. The peak is
torch.mps.driver_allocated_memory(), sampled after every decoder causal conv.install_frame_pad_fix+install_vae_sync, which is what the video loader runs today.What the table shows:
Tests
test_diffusion_device.pychecks the following:torch.equalto stock across kernel and padding shapes, with 0, 1 and 2 cache frames, and with noF.padcall left.Results: Linux 26 passed in the new and existing frame pad tests; macos-15 with real MPS torch 2.13, head 102 passed and base 87 passed.
Not tested