perf: run one-frame Wan VAE convolutions as 2D convolutions - #2038
Merged
Merged
Conversation
Image VAEs built on the Wan VAE (Qwen Image 2.1 among them) keep Conv3d weights one frame deep, and an image is a single frame, so each such convolution is a 2D convolution. Metal has no IM2COL_3D, so these fall back to GGML_OP_CONV_3D (leejet#1731), whose Metal kernel accumulates every output element in a scalar loop: a 512x512 Qwen Image 2.1 decode takes over a minute on an M1 Pro. Run them through ggml_ext_conv_2d (im2col + mul_mat) instead. Backends that implement IM2COL_3D compute the same products through the 2D path.
Owner
|
Thanks. |
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.
Problem
On Metal, the VAE decode of Qwen Image 2.1 is slow: 74.5 s for a 512x512 image on an M1 Pro. The Wan VAE's convolutions go through
ggml_ext_conv_3d(); Metal has noIM2COL_3D, so since #1731 they fall back toGGML_OP_CONV_3D, whose Metal kernel accumulates each output element in a scalar loop.For images this fallback is avoidable. Image VAEs built on the Wan VAE keep their Conv3d weights one frame deep (every decoder convolution of Qwen Image 2.1's VAE is 1x3x3 or 1x1x1), and an image is a single frame, so each of these convolutions is a 2D convolution.
Change
WAN::CausalConv3d::forward()runs a single-frame input through a one-frame-deep kernel withggml_ext_conv_2d()(im2col + mul_mat) and everything else throughggml_ext_conv_3d()as before. VAEs with deeper temporal kernels (Wan 2.1/2.2, Qwen Image) are unchanged, as is video decoding. On backends that implementIM2COL_3Dthe 2D path computes the same products; the activations go through im2col in f16 either way.Verification
Apple M1 Pro (32 GB), macOS 26,
-DSD_METAL=ON -DGGML_METAL_EMBED_LIBRARY=ON, on master2dc7f54, Qwen Image 2.1 Q4_K_M, 512x512, 20 steps, seed 42:decode_first_stageLeft: master. Right: this PR.
Not tested on CUDA, Vulkan or CPU.