Skip to content

Studio Mac: keep the Qwen-Image VAE causal conv zero-pad patch on MPS - #19616

Merged
danielhanchen merged 3 commits into
mainfrom
studio-mps-qwen-causal-pad
Nov 26, 2026
Merged

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

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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.

QwenImageCausalConv3d (Qwen-Image, Krea-2, Anima VAEs) has the same 6-tuple
causal padding and F.pad forward as WanCausalConv3d, so it takes the same
zero-buffer forward on MPS. The single-frame speed pass bound its own
forward over it and fell back to the class forward, which discarded the
zero-buffer patch; its fallback now calls the forward bound before it.

macos-15 (M1, 7.9 GiB cap), Qwen-Image VAE 512^2 decode x20, fresh process
per arm: stock ran out of MPS memory in 1 of 3 runs on torch 2.13 and 3 of
3 on 2.14.1, and its driver memory reached 7.7-8.1 GiB; the zero-buffer
forward stayed at 4.39 / 4.28 GiB, byte-identical output to stock, 8.2-8.5
vs 10.5-12.2 s per decode on 2.13.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 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-26T01:22:25.363780Z 9842b59 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. Breezy!

Reviewed commit: 5305f8495d

ℹ️ 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 and others added 2 commits November 25, 2026 16:54
[skip ci]
…only

Multi-frame and cached calls keep the stock forward: only the one-frame,
no-cache image path was measured on MPS. Adds CPU routing tests and an
MPS-only decode check against CPU.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 26, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 9842b59829

ℹ️ 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 ed42bee into main Nov 26, 2026
@danielhanchen
danielhanchen deleted the studio-mps-qwen-causal-pad branch November 26, 2026 02:38
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