Repository navigation
fix: make MPS conv VAE decode 4x faster with per-tap conv2d - #189
Sabbagh Fares (Sabmit) wants to merge 6 commits into
Conversation
On Apple Silicon, torch MPS conv3d is approximately 5 times slower than conv2d. The conv VAE decoder uses 3x3x3 Conv3d layers. Thus, the VAE decode is slow. This patch changes each applicable Conv3d into one conv2d for each temporal tap. Then it adds the results. The math does not change. For all other Conv3d layers, the patch uses the original forward. MPS conv2d gives incorrect values when the weight or the input is a strided view. The patch copies the weight and the input into a dense layout before each conv2d call. On an M3 Max with 48 GB, the VAE decode time decreases from 1:30 to 0:21. The full 540p/5 s generation time decreases from 6:20 to 5:12.
The tests compare the patched forward with the stock Conv3d forward. They run only on a Mac with an MPS GPU. On other machines, pytest skips them. Each test finds one possible defect: - The patch does not copy a strided input into a dense layout. - The patch changes a Conv3d with stride or temporal padding. - The patch loses frames in the last partial chunk.
16e5c29 to
f8bf642
Compare
The script compares the stock Conv3d with the patch on Apple Silicon. It does not need model weights. It uses one Conv3d for each stage of the conv VAE decoder. It shows the time, the TFLOPS, and the speedup. With --vae, the script also decodes one latent with the real conv VAE. It shows the stock and the patched decode times and the PSNR. Use the script to check the patch on a Mac and after a torch update.
The old note gave a 7 GiB tile size. That value was not correct. The memory-efficient decoder does not keep a full tile in memory. The new note gives the measured cost. For a 540p/5 s decode, the peak MPS driver memory increases from 9.7 GiB to 10.7 GiB.
The per-tap loop built its accumulator from None and asserted it after the loop. Running the first tap, which carries the bias, before the loop removes the Optional and the assert, and names the frame batching in one helper. Claude-Session: https://claude.ai/code/session_01M8Y4f9pRwDZfXY2VAqZNvz
|
tl;dr : On Apple Silicon, torch's MPS conv3d runs at about 1/5 of conv2d throughput, so this PR conv2d calls, cutting Fast decode from 1:36 to 0:24 on an M3 Max with no visible change (48.7 dB PSNR) and no new dependencies. |
…ench The bench script names the FLOP factors (FLOPS_PER_MAC, KERNEL_TAPS), derives the latent shape from the clip and VAE constants, uses one SEED constant (42), and returns results from bench_ops and bench_vae so that report prints once at the end. The patch and the bench use full variable names instead of two-letter ones. No change in behaviour: the tests pass and the --vae run gives the same 4.3x speedup and 54.3 dB PSNR. Claude-Session: https://claude.ai/code/session_01M8Y4f9pRwDZfXY2VAqZNvz
|
Hi Sabbagh Fares (@Sabmit), thank you for this PR. The per-tap We implemented your suggestion in LTX Desktop. The patch is an experimental setting for Apple Silicon, under Fast decode. It is off by default, because the speed and the MPS We did not use your other PR, #190. LTX-2 v1.6.0 now reads disk-streamed weights with Thank you for the careful tests and the clear removal conditions. They made the review much easier. We hope you continue to contribute. |
Summary
This PR makes the conv VAE decode on Apple Silicon approximately 4 times faster. On an M3 Max, a 540p 5 s video takes 5:42 instead of 6:50. The decoded frames agree with stock at 48.7 dB PSNR and 0.991 SSIM. The PR adds no dependencies. The PR has no effect on other platforms.
Problem
The conv VAE decoder (
ltx-2.5-video-vae-conv, Fast decode) uses 3x3x3CausalConv3dlayers. On MPS, torchconv3dis slow. On an M3 Max with torch 2.11:conv3dgives approximately 3 TFLOPS.conv2dgives approximately 16 TFLOPS on the same GPU.In torch 2.11, the MPS 3D path always uses MPSGraph
convolution3D. No flag or environment variable changes this path. pytorch/pytorch#192213 reports the same ratio.Solution
A Conv3d with stride 1 and no temporal padding is equal to a sum of Conv2d operations. Each temporal tap gives one Conv2d on time-shifted frames. For each applicable Conv3d on MPS, the patch does these steps:
conv2dfor each tap.The patch does these steps on small chunks of frames. This keeps the temporary tensors small. All other Conv3d layers use the original
forward: layers that are not on MPS, and layers with stride, dilation, groups, temporal padding, or a padding mode that is not zeros.Incorrect conv2d values on MPS
In torch 2.11, MPS
conv2dgives incorrect values when the weight or the input is a strided view. It does not show an error. There are two cases:weight[:, :, i]. See [MPS] F.conv2d returns wrong results with a non-contiguous weight pytorch/pytorch#192212. Torch 2.14.0 corrects this with [MPS] Fix conv2d/backward with non-contiguous weights pytorch/pytorch#192303.channels_last_3dworkspace of the memory-efficient decoder. I did not find a PyTorch issue for this case.The patch copies the weight and the input into a dense layout before each
conv2dcall. Without these copies, 69% of the decoded values were NaN. One test covers each copy.Output difference
The per-tap sum rounds the partial sums to bf16 three times. Stock
conv3drounds one time. The decoded frames differ from stock at the bf16 noise level: 48.7 dB PSNR average, 47.8 dB PSNR minimum, 0.991 SSIM over all 121 frames. The difference is not visible. The output is deterministic: each patched run gives the same file.Results
Conditions
/api/generaterequest. The total includes the model load.Time
All three stock runs gave the same file. All six runs with this PR gave one other file, with and without #190.
Memory
The dense copies use temporary memory. The chunking limits this memory to approximately 512 MiB of input plus the partial sums. I sampled
/api/gpu-info/mpsandfootprint -peach second during the runs above. Stock and this PR gave the same values in each of their runs.The peak of the run does not change. The peak occurs at model load and in stage 2, before the decoder runs. During the decode, the conv2d path holds 2 GiB less driver memory than the stock MPSGraph
conv3d. After the run, the MPS allocator keeps 2.2 GiB less memory in its cache (770 MB instead of 2949 MB in theIOAcceleratorcategory offootprint).Why not upgrade torch?
Torch 2.14 replaces the MPSGraph
conv3dwith Metal kernels (pytorch/pytorch#188802). I measured torch 2.14.1 and 2.15.0 on the same M3 Max, bf16, with the decoder shapes:channels_last_3dtensorsconv3dconv3dThe memory-efficient decoder uses
channels_last_3dtensors. These tensors do not use the fast Metal path (pytorch/pytorch#188802 describes the fallback). In the decoder, this patch is 3.2 to 3.4 times faster than torch 2.14 and 2.15conv3d. On M5 GPUs, the torch 2.14 kernels are much faster. After a torch update, measure this patch on M5 before you keep it.A torch upgrade is also a larger change than this PR:
F.paderror on large 5D tensors (F.pad(mode="constant") with temporal-axis padding silently corrupts ~80% of elements for large 5D tensors on MPS pytorch/pytorch#194922). Torch 2.15 corrects it.conv3dgave incorrect values on macOS 26.2 ([MPS] Conv3d returns incorrect results in contiguous (NCDHW) memory format pytorch/pytorch#197114). One report in that issue is an LTX VAE decode.I did not find another implementation of this decomposition for MPS in LTX-2, ComfyUI, or diffusers.
How to check this PR
Use
backend/performance_runner/bench_mps_conv3d.py. It needs an Apple Silicon Mac. It adds no dependencies. Stop the backend before you start the script.To compare one Conv3d for each decoder stage, use this command. It does not need model weights. It takes approximately 1 minute.
cd backend uv run python performance_runner/bench_mps_conv3d.pyTo also decode one 1024x576, 121-frame latent with the real conv VAE, use this command. It needs the
ltx-2.5-video-vae-convcheckpoint. It takes approximately 3 minutes.This is my result on the M3 Max with torch 2.11:
I measured only on an M3 Max. Results from other Apple Silicon chips are welcome.
Checks
I ran these checks on the M3 Max, in the environment from
pnpm setup:dev:pnpm typecheck: 0 errorspnpm backend:test: all tests pass. The one skipped test is for Windows only.backend/tests/test_mps_conv3d_as_conv2d.py: 4 tests. The tests need MPS. On other machines, pytest skips them.Each test finds one error in the patch. I put each error into the patch, one at a time. Each time, the related test failed. These are the errors:
To run the tests on a Mac, use this command:
Removal
Remove this patch when torch MPS
conv3dis as fast asconv2dforchannels_last_3dtensors. To find this, runbench_mps_conv3d.pyafter each torch update. When thechannels_last_3dspeedup is approximately 1x, remove the patch.