Skip to content

fix: make MPS conv VAE decode 4x faster with per-tap conv2d - #189

Closed
Sabbagh Fares (Sabmit) wants to merge 6 commits into
Lightricks:mainfrom
Sabmit:mps-conv3d-as-conv2d
Closed

Sabbagh Fares (Sabmit) wants to merge 6 commits into
Lightricks:mainfrom
Sabmit:mps-conv3d-as-conv2d

Conversation

@Sabmit

@Sabmit Sabbagh Fares (Sabmit) commented Oct 31, 2026 •

Copy link
Copy Markdown

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 3x3x3 CausalConv3d layers. On MPS, torch conv3d is slow. On an M3 Max with torch 2.11:

  • conv3d gives approximately 3 TFLOPS.
  • conv2d gives 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:

  1. Copy the weight of each temporal tap into a dense tensor.
  2. Move the frames of each tap into the batch dimension.
  3. Run one conv2d for each tap.
  4. Add the results.

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 conv2d gives incorrect values when the weight or the input is a strided view. It does not show an error. There are two cases:

The patch copies the weight and the input into a dense layout before each conv2d call. 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 conv3d rounds 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

  • MacBook Pro M3 Max, 40-core GPU, 48 GB, AC power, macOS 27.0, torch 2.11
  • Headless dev backend at 1.3.1. Each run starts a new backend process.
  • Text-to-video, 540p (1024x576), 5 s, 24 fps, seed 42, Fast decode on
  • 3 rounds. Each round runs the four configurations one after the other. Odd rounds use the opposite order.
  • The stage times come from the progress endpoint. The total is the wall time of the /api/generate request. The total includes the model load.
  • The table shows the median. The range is in parentheses.

Time

Step Stock This PR This PR and #190
Stage 1 (8 steps) 3:09 (3:08-3:11) 3:11 (3:08-3:12) 1:11 (1:11-1:15)
Stage 2 (3 steps) 1:48 (1:47-2:01) 1:49 (1:49-1:50) 1:40 (1:39-1:42)
VAE decode 1:36 (1:32-1:45) 0:24 (0:24-0:25) 0:25 (0:24-0:25)
Total 6:50 (6:49-7:12) 5:42 (5:39-5:44) 3:35 (3:33-3:36)

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/mps and footprint -p each second during the runs above. Stock and this PR gave the same values in each of their runs.

Phase Stock: torch / MPS driver This PR: torch / MPS driver
Model load (peak torch allocation of the run) 9.83 / 10.35 GiB 9.83 / 10.35 GiB
Stage 2 (peak driver memory of the run) 4.58 / 11.16 GiB 3.79 / 11.16 GiB
VAE decode 2.43 / 8.94 GiB 2.34 / 6.81 GiB
Idle after the run 0 / 4.94 GiB 0 / 2.81 GiB

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 the IOAccelerator category of footprint).

Why not upgrade torch?

Torch 2.14 replaces the MPSGraph conv3d with 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:

Conv3d path Contiguous tensors channels_last_3d tensors
torch 2.11 conv3d ~3.2 TFLOPS ~3.0 TFLOPS
torch 2.14.1 and 2.15.0 conv3d ~11.5 TFLOPS ~5.0 TFLOPS
This patch ~16 TFLOPS ~16 TFLOPS

The memory-efficient decoder uses channels_last_3d tensors. 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.15 conv3d. 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:

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.py

To also decode one 1024x576, 121-frame latent with the real conv VAE, use this command. It needs the ltx-2.5-video-vae-conv checkpoint. It takes approximately 3 minutes.

uv run python performance_runner/bench_mps_conv3d.py --vae

This is my result on the M3 Max with torch 2.11:

layout                C   T    H    W |  stock ms TFLOPS |  patch ms TFLOPS | speedup  max|d|
channels_last_3d   1024  16   18   32 |     217.0   2.41 |      34.1  15.31 |   6.37x  0.0156
channels_last_3d    512  32   36   64 |     338.8   3.08 |      65.5  15.92 |   5.17x  0.0312
channels_last_3d    256  64   72  128 |     682.9   3.06 |     128.2  16.28 |   5.33x  0.0312
channels_last_3d    128  32  144  256 |     360.3   2.90 |      68.1  15.32 |   5.29x  0.0312
contiguous         1024  16   18   32 |     187.7   2.78 |      36.1  14.47 |   5.20x  0.0156
contiguous          512  32   36   64 |     339.3   3.08 |      73.9  14.12 |   4.59x  0.0312
contiguous          256  64   72  128 |     666.2   3.13 |     179.4  11.63 |   3.71x  0.0312
contiguous          128  32  144  256 |     342.5   3.05 |     119.7   8.72 |   2.86x  0.0312

stock   decode   97.6s
patched decode   22.9s  (4.27x)
patched vs stock: PSNR 54.3 dB, max|d| 0.0273

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 errors
  • pnpm 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:

  • 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.

To run the tests on a Mac, use this command:

pnpm backend:test -- tests/test_mps_conv3d_as_conv2d.py

Removal

Remove this patch when torch MPS conv3d is as fast as conv2d for channels_last_3d tensors. To find this, run bench_mps_conv3d.py after each torch update. When the channels_last_3d speedup is approximately 1x, remove the patch.

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.
@Sabmit Sabbagh Fares (Sabmit) changed the title Use conv2d for MPS Conv3d in the conv VAE decoder fix: make MPS conv VAE decode 4x faster with per-tap conv2d Oct 31, 2026
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
@Sabmit
Sabbagh Fares (Sabmit) marked this pull request as ready for review November 1, 2026 22:53
@Sabmit

Copy link
Copy Markdown
Author

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.
cc Bar Galili (@bureyburey) James Greatorex (@jamesgltx)
Sorry for the ping, but I noticed that no PR got a review so I'm not sure what's the expected process.

…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
@bureyburey

Copy link
Copy Markdown
Member

Hi Sabbagh Fares (@Sabmit), thank you for this PR. The per-tap conv2d idea, the MPS strided-view findings, and the benchmark script are very good work. We also apologize that we did not answer sooner.

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 conv2d behavior can differ by chip, macOS version, and torch version. We credited you in the patch, the test, and the benchmark docstrings.

We did not use your other PR, #190. LTX-2 v1.6.0 now reads disk-streamed weights with os.preadv, so we did not need that patch. We wrote the details in #190.

Thank you for the careful tests and the clear removal conditions. They made the review much easier. We hope you continue to contribute.

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.

2 participants