Skip to content

Studio: pad Wan causal convs into a zero buffer on Metal - #18932

Merged
danielhanchen merged 3 commits into
mainfrom
studio-mps-wan-causal-pad
Nov 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-mps-wan-causal-pad

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 every WanCausalConv3d an instance forward. It allocates the padded shape once with new_zeros, then copies the cache frames and the input into it. The values are the same as F.pad(cat([cache_x, x])), with no pad op.
    • It falls back to the stock forward for anything else: negative or back padding, a non-5-D input, or a cache whose dtype differs from the input.
    • It skips a conv whose forward is already wrapped, such as by an offload hook.
  • The video loader now calls install_frame_pad_fix next to install_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.

  • base = the merge base's install_frame_pad_fix + install_vae_sync, which is what the video loader runs today.
  • head = this branch.
  • raw = no Studio hooks.
  • cat = base, plus padding by concatenating zero blocks.
decode arm causal conv calls peak driver GiB peak allocator GiB driver outside allocator GiB s (relative) md5
Wan 2.2 TI2V-5B, 9 frames 256x256, 1 tile raw 100 7.823 3.910 3.913 75 5b6abdec0ec9
base 100 6.694 3.795 2.899 95 5b6abdec0ec9
base (repeat) 100 6.692 3.795 2.897 70 5b6abdec0ec9
cat 100 6.242 3.760 2.482 91 5b6abdec0ec9
head 100 5.242 3.772 1.470 49 5b6abdec0ec9
Wan 2.2, 9 frames 384x384 tiled, 4 tiles base 400 6.692 3.781 2.911 358 105d3929dfc7
head 400 5.242 3.765 1.477 228 105d3929dfc7
Wan 2.2, 9 frames 512x512 tiled, 9 tiles raw 192 7.889 3.903 3.986 n/a MPS backend out of memory
base 900 7.624 3.828 3.796 696 938c48b9c1d4
head 900 5.243 3.773 1.470 323 938c48b9c1d4
Wan 2.1, 9 frames 256x256, 1 tile base 97 5.032 1.604 3.428 26 305e2511dd68
head 97 3.248 1.590 1.658 16 305e2511dd68

What the table shows:

  • The defect reproduces on torch 2.13. Stock diffusers (raw) runs out of MPS memory 192 calls into the 9-tile decode.
  • With Studio's current hooks (base), the driver memory outside the allocator still grows with the tile count: 2.90, then 2.91, then 3.80 GiB over 1, 4 and 9 tiles.
  • With this change (head), it stays flat at 1.47 GiB. The peak driver total drops from 7.62 to 5.24 GiB at 9 tiles, and the allocator peak is unchanged.
  • Within each config, every arm that completed has the same md5.
  • The decode was also 1.6x to 2.2x faster on this runner. That is relative only, since the runner is a virtual M1.
  • Padding by concatenation (cat) recovers only a third of the gap, so the zero buffer is the one used.

Tests

test_diffusion_device.py checks the following:

  • The zero-buffer forward is torch.equal to stock across kernel and padding shapes, with 0, 1 and 2 cache frames, and with no F.pad call left.
  • It is Metal-only, it skips a conv that is already wrapped, and it falls back to stock on mismatched dtypes.
  • The video loader installs it.

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

  • torch 2.15 on the runner: its MPS backend reports no supported Metal GPU inside the virtual machine, so the upstream fix could not be measured side by side.
  • Real Apple GPUs other than the virtual M1, and full-length Wan clips. The decode sizes here are small.

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.
@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-22T17:56:19.045466Z 5177588 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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 517758863e

ℹ️ 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 a6dd0f9 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-mps-wan-causal-pad branch November 22, 2026 20:51
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]>
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