Skip to content

[MPS] Migrate constant pad to metal - #195368

Closed
Isalia20 wants to merge 6 commits into
pytorch:mainfrom
Isalia20:constant-pad-to-metal
Closed

Isalia20 wants to merge 6 commits into
pytorch:mainfrom
Isalia20:constant-pad-to-metal

Conversation

@Isalia20

@Isalia20 Isalia20 commented Aug 30, 2026 •

Copy link
Copy Markdown
Collaborator

Following this PR #195153 I tried to pin down other ops which might make memory usage high as the nll_loss backward did.
To make findings meaningful, I tried doing inference of various LLM/image/video generation models and found that for Wan-AI/Wan2.2-TI2V-5B-Diffusers memory usage explodes because of the pad function that is used during VAE decode phase of the model.

PR migrates MPS Graph constant pad to metal kernels. Perf:

Metric MPSGraph Fused Metal Change
MPS current-memory peak 4.422 GiB 4.095 GiB -0.327 GiB
MPS allocator-reserved peak 8.070 GiB 8.070 GiB unchanged
Driver memory outside allocator 262.065 GiB 0.038 GiB -262.027 GiB (-99.986%)
Total driver-memory peak 270.135 GiB 8.108 GiB -262.027 GiB (-97.0%)

The reason for such huge memory is because of Wan model calling F.pad before every 3D convolution where one spatial tile makes 300+ calls to this function, the complete 28-tile decode makes 8,764 calls.

The way kernels work is simple, there are dense and strided kernels:

  • Dense (constant_pad_nd_dense): input and output contiguous, at most 3 padded dims. The output is treated as [outer, D, H, W] (outer = all leading dims flattened) and dispatched as the matching 3D grid (W / ILP_PER_THREAD, H, outer * D), each thread writing 4 elements along W. One of the reason of this is kernel speed, with this, we avoid the stride calculations which makes the kernel faster.
  • Strided (constant_pad_nd): everything else. Dims are sorted by output stride, tid.x walks the innermost dim, tid.y is decomposed over the remaining dims and per-dim input/output strides are applied directly. Offsets use uint32 when every element offset of both tensors fits or uint64 otherwise.
Latency changes
Case Input shape Padding Dtype Layout Before After Speedup
vector_2d (32, 4096) (8, 8) float32 contiguous 0.0178 ms 0.0118 ms 1.52x
vector_2d (32, 4096) (8, 8) float32 transposed 0.0192 ms 0.0121 ms 1.58x
vector_2d (32, 4096) (8, 8) float32 sliced 0.0189 ms 0.0090 ms 2.10x
vector_2d (32, 4096) (8, 8) float16 contiguous 0.0159 ms 0.0077 ms 2.07x
vector_2d (32, 4096) (8, 8) float16 transposed 0.0187 ms 0.0116 ms 1.61x
vector_2d (32, 4096) (8, 8) float16 sliced 0.0186 ms 0.0080 ms 2.33x
vector_2d (32, 4096) (8, 8) bfloat16 contiguous 0.0152 ms 0.0081 ms 1.88x
vector_2d (32, 4096) (8, 8) bfloat16 transposed 0.0187 ms 0.0120 ms 1.56x
vector_2d (32, 4096) (8, 8) bfloat16 sliced 0.0185 ms 0.0084 ms 2.20x
vector_2d (32, 4096) (8, 8) int32 contiguous 0.0152 ms 0.0079 ms 1.94x
vector_2d (32, 4096) (8, 8) int32 transposed 0.0189 ms 0.0120 ms 1.58x
vector_2d (32, 4096) (8, 8) int32 sliced 0.0187 ms 0.0088 ms 2.12x
vector_2d (32, 4096) (8, 8) bool contiguous 0.0164 ms 0.0072 ms 2.29x
vector_2d (32, 4096) (8, 8) bool transposed 0.0188 ms 0.0098 ms 1.92x
vector_2d (32, 4096) (8, 8) bool sliced 0.0189 ms 0.0083 ms 2.27x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) float32 contiguous 0.0190 ms 0.0143 ms 1.33x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) float32 transposed 0.0531 ms 0.0190 ms 2.80x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) float32 sliced 0.0317 ms 0.0176 ms 1.80x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) float16 contiguous 0.0176 ms 0.0123 ms 1.42x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) float16 transposed 0.0369 ms 0.0167 ms 2.22x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) float16 sliced 0.0256 ms 0.0159 ms 1.61x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) bfloat16 contiguous 0.0166 ms 0.0124 ms 1.33x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) bfloat16 transposed 0.0369 ms 0.0168 ms 2.20x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) bfloat16 sliced 0.0256 ms 0.0159 ms 1.61x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) int32 contiguous 0.0168 ms 0.0143 ms 1.17x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) int32 transposed 0.0411 ms 0.0191 ms 2.16x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) int32 sliced 0.0302 ms 0.0177 ms 1.71x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) bool contiguous 0.0185 ms 0.0108 ms 1.72x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) bool transposed 0.0350 ms 0.0150 ms 2.33x
image_4d (1, 64, 128, 128) (1, 1, 1, 1) bool sliced 0.0234 ms 0.0145 ms 1.61x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) float32 contiguous 0.0186 ms 0.0127 ms 1.46x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) float32 transposed 0.0217 ms 0.0130 ms 1.67x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) float32 sliced 0.0213 ms 0.0129 ms 1.66x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) float16 contiguous 0.0191 ms 0.0115 ms 1.66x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) float16 transposed 0.0215 ms 0.0120 ms 1.79x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) float16 sliced 0.0212 ms 0.0120 ms 1.77x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) bfloat16 contiguous 0.0183 ms 0.0116 ms 1.57x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) bfloat16 transposed 0.0214 ms 0.0119 ms 1.80x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) bfloat16 sliced 0.0213 ms 0.0118 ms 1.80x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) int32 contiguous 0.0182 ms 0.0130 ms 1.40x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) int32 transposed 0.0217 ms 0.0132 ms 1.65x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) int32 sliced 0.0212 ms 0.0128 ms 1.66x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) bool contiguous 0.0187 ms 0.0109 ms 1.71x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) bool transposed 0.0214 ms 0.0115 ms 1.86x
mixed_crop_4d (2, 32, 64, 96) (-2, 3, 1, -1) bool sliced 0.0209 ms 0.0115 ms 1.81x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) float32 contiguous 0.0286 ms 0.0074 ms 3.86x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) float32 transposed 0.0310 ms 0.0107 ms 2.90x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) float32 sliced 0.0311 ms 0.0101 ms 3.09x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) float16 contiguous 0.0284 ms 0.0074 ms 3.82x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) float16 transposed 0.0313 ms 0.0104 ms 3.00x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) float16 sliced 0.0310 ms 0.0099 ms 3.14x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) bfloat16 contiguous 0.0287 ms 0.0075 ms 3.85x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) bfloat16 transposed 0.0312 ms 0.0105 ms 2.98x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) bfloat16 sliced 0.0308 ms 0.0095 ms 3.26x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) int32 contiguous 0.0288 ms 0.0073 ms 3.95x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) int32 transposed 0.0309 ms 0.0103 ms 3.01x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) int32 sliced 0.0312 ms 0.0099 ms 3.15x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) bool contiguous 0.0285 ms 0.0073 ms 3.92x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) bool transposed 0.0307 ms 0.0101 ms 3.04x
wan_small_5d (1, 48, 1, 16, 16) (1, 1, 1, 1, 2, 0) bool sliced 0.0309 ms 0.0096 ms 3.21x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) float32 contiguous 0.1519 ms 0.0430 ms 3.54x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) float32 transposed 0.2057 ms 0.0644 ms 3.20x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) float32 sliced 0.1902 ms 0.0693 ms 2.74x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) float16 contiguous 0.1059 ms 0.0396 ms 2.68x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) float16 transposed 0.1534 ms 0.0607 ms 2.53x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) float16 sliced 0.1308 ms 0.0609 ms 2.15x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) bfloat16 contiguous 0.1057 ms 0.0393 ms 2.69x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) bfloat16 transposed 0.1533 ms 0.0607 ms 2.52x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) bfloat16 sliced 0.1303 ms 0.0609 ms 2.14x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) int32 contiguous 0.1522 ms 0.0430 ms 3.54x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) int32 transposed 0.2045 ms 0.0670 ms 3.05x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) int32 sliced 0.1906 ms 0.0684 ms 2.79x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) bool contiguous 0.0973 ms 0.0303 ms 3.21x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) bool transposed 0.1432 ms 0.0541 ms 2.64x
wan_mid_5d (1, 512, 1, 64, 64) (1, 1, 1, 1, 2, 0) bool sliced 0.1194 ms 0.0545 ms 2.19x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) float32 contiguous 0.0461 ms 0.0526 ms 0.88x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) float32 transposed 0.2299 ms 0.0767 ms 3.00x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) float32 sliced 0.1857 ms 0.0960 ms 1.94x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) float16 contiguous 0.0386 ms 0.0279 ms 1.38x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) float16 transposed 0.2154 ms 0.0699 ms 3.08x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) float16 sliced 0.1240 ms 0.0721 ms 1.72x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) bfloat16 contiguous 0.0384 ms 0.0285 ms 1.35x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) bfloat16 transposed 0.2150 ms 0.0710 ms 3.03x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) bfloat16 sliced 0.1256 ms 0.0733 ms 1.71x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) int32 contiguous 0.0514 ms 0.0520 ms 0.99x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) int32 transposed 0.2317 ms 0.0753 ms 3.08x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) int32 sliced 0.1884 ms 0.1065 ms 1.77x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) bool contiguous 0.0359 ms 0.0174 ms 2.07x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) bool transposed 0.2035 ms 0.0635 ms 3.21x
zero_pad_5d (1, 512, 4, 64, 64) (0, 0, 0, 0, 0, 0) bool sliced 0.1162 ms 0.0637 ms 1.82x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) float32 contiguous 0.6938 ms 0.2953 ms 2.35x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) float32 transposed 1.2018 ms 0.3505 ms 3.43x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) float32 sliced 1.1201 ms 0.4372 ms 2.56x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) float16 contiguous 0.4209 ms 0.1144 ms 3.68x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) float16 transposed 0.9549 ms 0.1926 ms 4.96x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) float16 sliced 0.7193 ms 0.2155 ms 3.34x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) bfloat16 contiguous 0.4179 ms 0.1097 ms 3.81x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) bfloat16 transposed 0.9514 ms 0.1937 ms 4.91x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) bfloat16 sliced 0.7133 ms 0.2131 ms 3.35x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) int32 contiguous 0.6962 ms 0.2960 ms 2.35x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) int32 transposed 1.2045 ms 0.3451 ms 3.49x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) int32 sliced 1.1212 ms 0.4381 ms 2.56x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) bool contiguous 0.3444 ms 0.0948 ms 3.63x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) bool transposed 0.8468 ms 0.1806 ms 4.69x
wan_large_5d (1, 256, 6, 128, 128) (1, 1, 1, 1, 0, 0) bool sliced 0.5935 ms 0.1726 ms 3.44x

…ytorch into constant-pad-to-metal

# Conflicts:
#	aten/src/ATen/native/mps/kernels/ReplicationPad.metal
#	aten/src/ATen/native/mps/operations/Pad.mm
@pytorch-bot

pytorch-bot Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195368

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit bcf0fc1 with merge base 58a938a (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Aug 30, 2026
@Isalia20 Isalia20 added the topic: improvements topic category label Aug 30, 2026
@Isalia20
Isalia20 marked this pull request as ready for review August 30, 2026 22:21
@Isalia20
Isalia20 requested a review from malfet as a code owner August 30, 2026 22:21
@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 31, 2026
@malfet

malfet commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "Lint + MPS are green"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Sep 3, 2026
Following #195368 of PRs for reducing memory usage for the ops which are mainly on modern model inference/training side and use MPS Graph, next one being log_softmax.

To briefly explain the kernels, there are 4:
- `log_softmax_row` - used when dim we are reducing on is relatively small (<=1024) or when reduction dim is between 1025 and 2048 but there are enough "rows" (>=128) to keep gpu saturated (reduction dim should be contiguous)
- `log_softmax_partial` and `log_softmax_finalize` - Regular 2 pass kernel, used when reduction dim is >2048 and there are < 256 rows to reduce (reduction dim should be contiguous)
- `log_softmax` - generic kernel, which works on strided tensors and tensors which do not dispatch to the above more optimized ones

<details>
<summary> Latency </summary>

| Shape | Dtype | Before (us) | After (us) | Speedup | Model |
|---|---|---:|---:|---:|---|
| `4x499x46` | fp32 | 34.9 | 5.1 | **6.819x** | wav2vec2-xlsr-53-pt |
| `1x201088` | fp32 | 45.1 | 9.3 | **4.851x** | gpt-oss-20b |
| `5x51866` | fp32 | 46.9 | 11.4 | **4.126x** | whisper-large-v3-turbo |
| `1x151936` | fp32 | 45.5 | 11.9 | **3.838x** | Qwen3 |
| `64x1000` | fp32 | 33.3 | 9.0 | **3.694x** | ViT-base |
| `4x129280` | fp32 | 51.2 | 16.6 | **3.075x** | DeepSeek-V4-Flash |
| `1x151936` | fp32 | 49.8 | 20.8 | **2.401x** | Qwen3 |
| `4096x30522` | fp32 | 8927.1 | 5079.3 | **1.758x** | bert-base-uncased |
| `2x150x512x512` | fp32 | 4881.9 | 2873.2 | **1.699x** | SegFormer-b0 |
| `8x154880` | fp32 | 80.8 | 49.6 | **1.629x** | GLM-5.3 |
| `4x499x2341` | fp32 | 242.9 | 167.1 | **1.454x** | wav2vec2-xlsr-53-ja |
| `1024x50257` | fp32 | 3641.2 | 2960.5 | **1.230x** | gpt2 |
| `2048x128256` | fp32 | 15348.7 | 15238.8 | **1.007x** | Llama-3.2 |
| `2048x129280` | fp32 | 15357.3 | 15380.9 | **0.998x** | DeepSeek-V4-Flash |
| `2048x151936` | fp32 | 17924.8 | 18055.6 | **0.993x** | Qwen3 |
| `8192x151936` | fp32 | 72202.2 | 73142.5 | **0.987x** | Qwen3 |
| `2048x154880` | fp32 | 18289.3 | 18554.2 | **0.986x** | GLM-5.3 |
| `1024x201088` | fp32 | 11830.8 | 12034.2 | **0.983x** | gpt-oss-20b |
| `4x1023x151936` | bf16 | 17874.3 | 19306.0 | **0.926x** | Qwen3 |
| `4x151936x128` | fp32 | 4860.5 | 5352.9 | **0.908x** | Qwen3 |

</details>

<details>
<summary> Memory usage before: </summary>

```
baseline driver memory: 32.7 MiB
tokens= 1024 tensor=   32.0 MiB driver=    32.7 MiB (+    0.0)
tokens= 1124 tensor=   35.1 MiB driver=    68.7 MiB (+   36.0)
tokens= 1224 tensor=   38.2 MiB driver=   108.7 MiB (+   40.0)
tokens= 1324 tensor=   41.4 MiB driver=   150.7 MiB (+   42.0)
tokens= 1424 tensor=   44.5 MiB driver=   196.7 MiB (+   46.0)
tokens= 1524 tensor=   47.6 MiB driver=   244.7 MiB (+   48.0)
tokens= 1624 tensor=   50.8 MiB driver=   296.7 MiB (+   52.0)
tokens= 1724 tensor=   53.9 MiB driver=   350.7 MiB (+   54.0)
tokens= 1824 tensor=   57.0 MiB driver=   408.7 MiB (+   58.0)
tokens= 1924 tensor=   60.1 MiB driver=   470.7 MiB (+   62.0)
tokens= 2024 tensor=   63.2 MiB driver=   534.7 MiB (+   64.0)
tokens= 2124 tensor=   66.4 MiB driver=   602.7 MiB (+   68.0)
tokens= 2224 tensor=   69.5 MiB driver=   672.7 MiB (+   70.0)
tokens= 2324 tensor=   72.6 MiB driver=   746.7 MiB (+   74.0)
tokens= 2424 tensor=   75.8 MiB driver=   822.7 MiB (+   76.0)
tokens= 2524 tensor=   78.9 MiB driver=   902.7 MiB (+   80.0)
tokens= 2624 tensor=   82.0 MiB driver=   984.7 MiB (+   82.0)
tokens= 2724 tensor=   85.1 MiB driver=  1070.7 MiB (+   86.0)
tokens= 2824 tensor=   88.2 MiB driver=  1160.7 MiB (+   90.0)
tokens= 2924 tensor=   91.4 MiB driver=  1252.7 MiB (+   92.0)
tokens= 3024 tensor=   94.5 MiB driver=  1348.7 MiB (+   96.0)
tokens= 3124 tensor=   97.6 MiB driver=  1446.7 MiB (+   98.0)
tokens= 3224 tensor=  100.8 MiB driver=  1548.7 MiB (+  102.0)
tokens= 3324 tensor=  103.9 MiB driver=  1652.7 MiB (+  104.0)
growth over 24 new shapes: 1620.0 MiB
growth over 24 repeated shapes: -1548.0 MiB
[3812, 32000] fp32 retained: 466.0 MiB
final driver memory: 570.7 MiB
```

</details>

<details>
<summary> Memory usage after </summary>

```
baseline driver memory: 0.5 MiB
tokens= 1024 tensor=   32.0 MiB driver=     0.5 MiB (+    0.0)
tokens= 1124 tensor=   35.1 MiB driver=     0.5 MiB (+    0.0)
tokens= 1224 tensor=   38.2 MiB driver=     0.5 MiB (+    0.0)
tokens= 1324 tensor=   41.4 MiB driver=     0.5 MiB (+    0.0)
tokens= 1424 tensor=   44.5 MiB driver=     0.5 MiB (+    0.0)
tokens= 1524 tensor=   47.6 MiB driver=     0.5 MiB (+    0.0)
tokens= 1624 tensor=   50.8 MiB driver=     0.5 MiB (+    0.0)
tokens= 1724 tensor=   53.9 MiB driver=     0.5 MiB (+    0.0)
tokens= 1824 tensor=   57.0 MiB driver=     0.5 MiB (+    0.0)
tokens= 1924 tensor=   60.1 MiB driver=     0.5 MiB (+    0.0)
tokens= 2024 tensor=   63.2 MiB driver=     0.5 MiB (+    0.0)
tokens= 2124 tensor=   66.4 MiB driver=     0.5 MiB (+    0.0)
tokens= 2224 tensor=   69.5 MiB driver=     0.4 MiB (+   -0.0)
tokens= 2324 tensor=   72.6 MiB driver=     0.4 MiB (+    0.0)
tokens= 2424 tensor=   75.8 MiB driver=     0.4 MiB (+    0.0)
tokens= 2524 tensor=   78.9 MiB driver=     0.4 MiB (+    0.0)
tokens= 2624 tensor=   82.0 MiB driver=     0.4 MiB (+    0.0)
tokens= 2724 tensor=   85.1 MiB driver=     0.4 MiB (+    0.0)
tokens= 2824 tensor=   88.2 MiB driver=     0.4 MiB (+    0.0)
tokens= 2924 tensor=   91.4 MiB driver=     0.4 MiB (+    0.0)
tokens= 3024 tensor=   94.5 MiB driver=     0.4 MiB (+    0.0)
tokens= 3124 tensor=   97.6 MiB driver=     0.4 MiB (+    0.0)
tokens= 3224 tensor=  100.8 MiB driver=     0.4 MiB (+    0.0)
tokens= 3324 tensor=  103.9 MiB driver=     0.4 MiB (+    0.0)
growth over 24 new shapes: -0.0 MiB
growth over 24 repeated shapes: 0.0 MiB
[3812, 32000] fp32 retained: 0.0 MiB
final driver memory: 0.4 MiB
```

</details>
Pull Request resolved: #195441
Approved by: https://github.com/kurtamohler, https://github.com/malfet
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
Following this PR pytorch#195153 I tried to pin down other ops which might make memory usage high as the nll_loss backward did.
To make findings meaningful, I tried doing inference of various LLM/image/video generation models and found that for `Wan-AI/Wan2.2-TI2V-5B-Diffusers` memory usage explodes because of the pad function that is used during VAE decode phase of the model.

PR migrates MPS Graph constant pad to metal kernels. Perf:

| Metric | MPSGraph | Fused Metal | Change |
|---|---:|---:|---:|
| MPS current-memory peak | 4.422 GiB | 4.095 GiB | -0.327 GiB |
| MPS allocator-reserved peak | 8.070 GiB | 8.070 GiB | unchanged |
| Driver memory outside allocator | 262.065 GiB | 0.038 GiB | -262.027 GiB (-99.986%) |
| Total driver-memory peak | 270.135 GiB | 8.108 GiB | -262.027 GiB (-97.0%) |

The reason for such huge memory is because of Wan model calling `F.pad` before every 3D convolution where one spatial tile makes 300+ calls to this function, the complete 28-tile decode makes 8,764 calls.

The way kernels work is simple, there are dense and strided kernels:

- **Dense** (`constant_pad_nd_dense`): input and output contiguous, at most 3 padded dims. The output is treated as `[outer, D, H, W]` (`outer` = all leading dims flattened) and dispatched as the matching 3D grid `(W / ILP_PER_THREAD, H, outer * D)`, each thread writing 4 elements along `W`. One of the reason of this is kernel speed, with this, we avoid the stride calculations which makes the kernel faster.
- **Strided** (`constant_pad_nd`): everything else. Dims are sorted by output stride, `tid.x` walks the innermost dim, `tid.y` is decomposed over the remaining dims and per-dim input/output strides are applied directly. Offsets use uint32 when every element offset of both tensors fits or uint64 otherwise.

<details>
<summary> Latency changes </summary>

| Case | Input shape | Padding | Dtype | Layout | Before | After | Speedup |
|---|---|---|---|---|---:|---:|---:|
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `float32` | `contiguous` | 0.0178 ms | 0.0118 ms | 1.52x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `float32` | `transposed` | 0.0192 ms | 0.0121 ms | 1.58x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `float32` | `sliced` | 0.0189 ms | 0.0090 ms | 2.10x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `float16` | `contiguous` | 0.0159 ms | 0.0077 ms | 2.07x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `float16` | `transposed` | 0.0187 ms | 0.0116 ms | 1.61x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `float16` | `sliced` | 0.0186 ms | 0.0080 ms | 2.33x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `bfloat16` | `contiguous` | 0.0152 ms | 0.0081 ms | 1.88x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `bfloat16` | `transposed` | 0.0187 ms | 0.0120 ms | 1.56x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `bfloat16` | `sliced` | 0.0185 ms | 0.0084 ms | 2.20x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `int32` | `contiguous` | 0.0152 ms | 0.0079 ms | 1.94x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `int32` | `transposed` | 0.0189 ms | 0.0120 ms | 1.58x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `int32` | `sliced` | 0.0187 ms | 0.0088 ms | 2.12x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `bool` | `contiguous` | 0.0164 ms | 0.0072 ms | 2.29x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `bool` | `transposed` | 0.0188 ms | 0.0098 ms | 1.92x |
| `vector_2d` | `(32, 4096)` | `(8, 8)` | `bool` | `sliced` | 0.0189 ms | 0.0083 ms | 2.27x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `float32` | `contiguous` | 0.0190 ms | 0.0143 ms | 1.33x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `float32` | `transposed` | 0.0531 ms | 0.0190 ms | 2.80x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `float32` | `sliced` | 0.0317 ms | 0.0176 ms | 1.80x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `float16` | `contiguous` | 0.0176 ms | 0.0123 ms | 1.42x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `float16` | `transposed` | 0.0369 ms | 0.0167 ms | 2.22x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `float16` | `sliced` | 0.0256 ms | 0.0159 ms | 1.61x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `bfloat16` | `contiguous` | 0.0166 ms | 0.0124 ms | 1.33x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `bfloat16` | `transposed` | 0.0369 ms | 0.0168 ms | 2.20x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `bfloat16` | `sliced` | 0.0256 ms | 0.0159 ms | 1.61x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `int32` | `contiguous` | 0.0168 ms | 0.0143 ms | 1.17x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `int32` | `transposed` | 0.0411 ms | 0.0191 ms | 2.16x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `int32` | `sliced` | 0.0302 ms | 0.0177 ms | 1.71x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `bool` | `contiguous` | 0.0185 ms | 0.0108 ms | 1.72x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `bool` | `transposed` | 0.0350 ms | 0.0150 ms | 2.33x |
| `image_4d` | `(1, 64, 128, 128)` | `(1, 1, 1, 1)` | `bool` | `sliced` | 0.0234 ms | 0.0145 ms | 1.61x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `float32` | `contiguous` | 0.0186 ms | 0.0127 ms | 1.46x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `float32` | `transposed` | 0.0217 ms | 0.0130 ms | 1.67x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `float32` | `sliced` | 0.0213 ms | 0.0129 ms | 1.66x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `float16` | `contiguous` | 0.0191 ms | 0.0115 ms | 1.66x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `float16` | `transposed` | 0.0215 ms | 0.0120 ms | 1.79x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `float16` | `sliced` | 0.0212 ms | 0.0120 ms | 1.77x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `bfloat16` | `contiguous` | 0.0183 ms | 0.0116 ms | 1.57x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `bfloat16` | `transposed` | 0.0214 ms | 0.0119 ms | 1.80x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `bfloat16` | `sliced` | 0.0213 ms | 0.0118 ms | 1.80x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `int32` | `contiguous` | 0.0182 ms | 0.0130 ms | 1.40x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `int32` | `transposed` | 0.0217 ms | 0.0132 ms | 1.65x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `int32` | `sliced` | 0.0212 ms | 0.0128 ms | 1.66x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `bool` | `contiguous` | 0.0187 ms | 0.0109 ms | 1.71x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `bool` | `transposed` | 0.0214 ms | 0.0115 ms | 1.86x |
| `mixed_crop_4d` | `(2, 32, 64, 96)` | `(-2, 3, 1, -1)` | `bool` | `sliced` | 0.0209 ms | 0.0115 ms | 1.81x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `float32` | `contiguous` | 0.0286 ms | 0.0074 ms | 3.86x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `float32` | `transposed` | 0.0310 ms | 0.0107 ms | 2.90x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `float32` | `sliced` | 0.0311 ms | 0.0101 ms | 3.09x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `float16` | `contiguous` | 0.0284 ms | 0.0074 ms | 3.82x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `float16` | `transposed` | 0.0313 ms | 0.0104 ms | 3.00x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `float16` | `sliced` | 0.0310 ms | 0.0099 ms | 3.14x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `bfloat16` | `contiguous` | 0.0287 ms | 0.0075 ms | 3.85x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `bfloat16` | `transposed` | 0.0312 ms | 0.0105 ms | 2.98x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `bfloat16` | `sliced` | 0.0308 ms | 0.0095 ms | 3.26x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `int32` | `contiguous` | 0.0288 ms | 0.0073 ms | 3.95x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `int32` | `transposed` | 0.0309 ms | 0.0103 ms | 3.01x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `int32` | `sliced` | 0.0312 ms | 0.0099 ms | 3.15x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `bool` | `contiguous` | 0.0285 ms | 0.0073 ms | 3.92x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `bool` | `transposed` | 0.0307 ms | 0.0101 ms | 3.04x |
| `wan_small_5d` | `(1, 48, 1, 16, 16)` | `(1, 1, 1, 1, 2, 0)` | `bool` | `sliced` | 0.0309 ms | 0.0096 ms | 3.21x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `float32` | `contiguous` | 0.1519 ms | 0.0430 ms | 3.54x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `float32` | `transposed` | 0.2057 ms | 0.0644 ms | 3.20x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `float32` | `sliced` | 0.1902 ms | 0.0693 ms | 2.74x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `float16` | `contiguous` | 0.1059 ms | 0.0396 ms | 2.68x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `float16` | `transposed` | 0.1534 ms | 0.0607 ms | 2.53x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `float16` | `sliced` | 0.1308 ms | 0.0609 ms | 2.15x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `bfloat16` | `contiguous` | 0.1057 ms | 0.0393 ms | 2.69x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `bfloat16` | `transposed` | 0.1533 ms | 0.0607 ms | 2.52x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `bfloat16` | `sliced` | 0.1303 ms | 0.0609 ms | 2.14x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `int32` | `contiguous` | 0.1522 ms | 0.0430 ms | 3.54x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `int32` | `transposed` | 0.2045 ms | 0.0670 ms | 3.05x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `int32` | `sliced` | 0.1906 ms | 0.0684 ms | 2.79x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `bool` | `contiguous` | 0.0973 ms | 0.0303 ms | 3.21x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `bool` | `transposed` | 0.1432 ms | 0.0541 ms | 2.64x |
| `wan_mid_5d` | `(1, 512, 1, 64, 64)` | `(1, 1, 1, 1, 2, 0)` | `bool` | `sliced` | 0.1194 ms | 0.0545 ms | 2.19x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `float32` | `contiguous` | 0.0461 ms | 0.0526 ms | 0.88x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `float32` | `transposed` | 0.2299 ms | 0.0767 ms | 3.00x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `float32` | `sliced` | 0.1857 ms | 0.0960 ms | 1.94x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `float16` | `contiguous` | 0.0386 ms | 0.0279 ms | 1.38x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `float16` | `transposed` | 0.2154 ms | 0.0699 ms | 3.08x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `float16` | `sliced` | 0.1240 ms | 0.0721 ms | 1.72x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `bfloat16` | `contiguous` | 0.0384 ms | 0.0285 ms | 1.35x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `bfloat16` | `transposed` | 0.2150 ms | 0.0710 ms | 3.03x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `bfloat16` | `sliced` | 0.1256 ms | 0.0733 ms | 1.71x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `int32` | `contiguous` | 0.0514 ms | 0.0520 ms | 0.99x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `int32` | `transposed` | 0.2317 ms | 0.0753 ms | 3.08x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `int32` | `sliced` | 0.1884 ms | 0.1065 ms | 1.77x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `bool` | `contiguous` | 0.0359 ms | 0.0174 ms | 2.07x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `bool` | `transposed` | 0.2035 ms | 0.0635 ms | 3.21x |
| `zero_pad_5d` | `(1, 512, 4, 64, 64)` | `(0, 0, 0, 0, 0, 0)` | `bool` | `sliced` | 0.1162 ms | 0.0637 ms | 1.82x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `float32` | `contiguous` | 0.6938 ms | 0.2953 ms | 2.35x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `float32` | `transposed` | 1.2018 ms | 0.3505 ms | 3.43x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `float32` | `sliced` | 1.1201 ms | 0.4372 ms | 2.56x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `float16` | `contiguous` | 0.4209 ms | 0.1144 ms | 3.68x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `float16` | `transposed` | 0.9549 ms | 0.1926 ms | 4.96x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `float16` | `sliced` | 0.7193 ms | 0.2155 ms | 3.34x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `bfloat16` | `contiguous` | 0.4179 ms | 0.1097 ms | 3.81x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `bfloat16` | `transposed` | 0.9514 ms | 0.1937 ms | 4.91x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `bfloat16` | `sliced` | 0.7133 ms | 0.2131 ms | 3.35x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `int32` | `contiguous` | 0.6962 ms | 0.2960 ms | 2.35x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `int32` | `transposed` | 1.2045 ms | 0.3451 ms | 3.49x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `int32` | `sliced` | 1.1212 ms | 0.4381 ms | 2.56x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `bool` | `contiguous` | 0.3444 ms | 0.0948 ms | 3.63x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `bool` | `transposed` | 0.8468 ms | 0.1806 ms | 4.69x |
| `wan_large_5d` | `(1, 256, 6, 128, 128)` | `(1, 1, 1, 1, 0, 0)` | `bool` | `sliced` | 0.5935 ms | 0.1726 ms | 3.44x |

</details>
Pull Request resolved: pytorch#195368
Approved by: https://github.com/malfet
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
Following pytorch#195368 of PRs for reducing memory usage for the ops which are mainly on modern model inference/training side and use MPS Graph, next one being log_softmax.

To briefly explain the kernels, there are 4:
- `log_softmax_row` - used when dim we are reducing on is relatively small (<=1024) or when reduction dim is between 1025 and 2048 but there are enough "rows" (>=128) to keep gpu saturated (reduction dim should be contiguous)
- `log_softmax_partial` and `log_softmax_finalize` - Regular 2 pass kernel, used when reduction dim is >2048 and there are < 256 rows to reduce (reduction dim should be contiguous)
- `log_softmax` - generic kernel, which works on strided tensors and tensors which do not dispatch to the above more optimized ones

<details>
<summary> Latency </summary>

| Shape | Dtype | Before (us) | After (us) | Speedup | Model |
|---|---|---:|---:|---:|---|
| `4x499x46` | fp32 | 34.9 | 5.1 | **6.819x** | wav2vec2-xlsr-53-pt |
| `1x201088` | fp32 | 45.1 | 9.3 | **4.851x** | gpt-oss-20b |
| `5x51866` | fp32 | 46.9 | 11.4 | **4.126x** | whisper-large-v3-turbo |
| `1x151936` | fp32 | 45.5 | 11.9 | **3.838x** | Qwen3 |
| `64x1000` | fp32 | 33.3 | 9.0 | **3.694x** | ViT-base |
| `4x129280` | fp32 | 51.2 | 16.6 | **3.075x** | DeepSeek-V4-Flash |
| `1x151936` | fp32 | 49.8 | 20.8 | **2.401x** | Qwen3 |
| `4096x30522` | fp32 | 8927.1 | 5079.3 | **1.758x** | bert-base-uncased |
| `2x150x512x512` | fp32 | 4881.9 | 2873.2 | **1.699x** | SegFormer-b0 |
| `8x154880` | fp32 | 80.8 | 49.6 | **1.629x** | GLM-5.3 |
| `4x499x2341` | fp32 | 242.9 | 167.1 | **1.454x** | wav2vec2-xlsr-53-ja |
| `1024x50257` | fp32 | 3641.2 | 2960.5 | **1.230x** | gpt2 |
| `2048x128256` | fp32 | 15348.7 | 15238.8 | **1.007x** | Llama-3.2 |
| `2048x129280` | fp32 | 15357.3 | 15380.9 | **0.998x** | DeepSeek-V4-Flash |
| `2048x151936` | fp32 | 17924.8 | 18055.6 | **0.993x** | Qwen3 |
| `8192x151936` | fp32 | 72202.2 | 73142.5 | **0.987x** | Qwen3 |
| `2048x154880` | fp32 | 18289.3 | 18554.2 | **0.986x** | GLM-5.3 |
| `1024x201088` | fp32 | 11830.8 | 12034.2 | **0.983x** | gpt-oss-20b |
| `4x1023x151936` | bf16 | 17874.3 | 19306.0 | **0.926x** | Qwen3 |
| `4x151936x128` | fp32 | 4860.5 | 5352.9 | **0.908x** | Qwen3 |

</details>

<details>
<summary> Memory usage before: </summary>

```
baseline driver memory: 32.7 MiB
tokens= 1024 tensor=   32.0 MiB driver=    32.7 MiB (+    0.0)
tokens= 1124 tensor=   35.1 MiB driver=    68.7 MiB (+   36.0)
tokens= 1224 tensor=   38.2 MiB driver=   108.7 MiB (+   40.0)
tokens= 1324 tensor=   41.4 MiB driver=   150.7 MiB (+   42.0)
tokens= 1424 tensor=   44.5 MiB driver=   196.7 MiB (+   46.0)
tokens= 1524 tensor=   47.6 MiB driver=   244.7 MiB (+   48.0)
tokens= 1624 tensor=   50.8 MiB driver=   296.7 MiB (+   52.0)
tokens= 1724 tensor=   53.9 MiB driver=   350.7 MiB (+   54.0)
tokens= 1824 tensor=   57.0 MiB driver=   408.7 MiB (+   58.0)
tokens= 1924 tensor=   60.1 MiB driver=   470.7 MiB (+   62.0)
tokens= 2024 tensor=   63.2 MiB driver=   534.7 MiB (+   64.0)
tokens= 2124 tensor=   66.4 MiB driver=   602.7 MiB (+   68.0)
tokens= 2224 tensor=   69.5 MiB driver=   672.7 MiB (+   70.0)
tokens= 2324 tensor=   72.6 MiB driver=   746.7 MiB (+   74.0)
tokens= 2424 tensor=   75.8 MiB driver=   822.7 MiB (+   76.0)
tokens= 2524 tensor=   78.9 MiB driver=   902.7 MiB (+   80.0)
tokens= 2624 tensor=   82.0 MiB driver=   984.7 MiB (+   82.0)
tokens= 2724 tensor=   85.1 MiB driver=  1070.7 MiB (+   86.0)
tokens= 2824 tensor=   88.2 MiB driver=  1160.7 MiB (+   90.0)
tokens= 2924 tensor=   91.4 MiB driver=  1252.7 MiB (+   92.0)
tokens= 3024 tensor=   94.5 MiB driver=  1348.7 MiB (+   96.0)
tokens= 3124 tensor=   97.6 MiB driver=  1446.7 MiB (+   98.0)
tokens= 3224 tensor=  100.8 MiB driver=  1548.7 MiB (+  102.0)
tokens= 3324 tensor=  103.9 MiB driver=  1652.7 MiB (+  104.0)
growth over 24 new shapes: 1620.0 MiB
growth over 24 repeated shapes: -1548.0 MiB
[3812, 32000] fp32 retained: 466.0 MiB
final driver memory: 570.7 MiB
```

</details>

<details>
<summary> Memory usage after </summary>

```
baseline driver memory: 0.5 MiB
tokens= 1024 tensor=   32.0 MiB driver=     0.5 MiB (+    0.0)
tokens= 1124 tensor=   35.1 MiB driver=     0.5 MiB (+    0.0)
tokens= 1224 tensor=   38.2 MiB driver=     0.5 MiB (+    0.0)
tokens= 1324 tensor=   41.4 MiB driver=     0.5 MiB (+    0.0)
tokens= 1424 tensor=   44.5 MiB driver=     0.5 MiB (+    0.0)
tokens= 1524 tensor=   47.6 MiB driver=     0.5 MiB (+    0.0)
tokens= 1624 tensor=   50.8 MiB driver=     0.5 MiB (+    0.0)
tokens= 1724 tensor=   53.9 MiB driver=     0.5 MiB (+    0.0)
tokens= 1824 tensor=   57.0 MiB driver=     0.5 MiB (+    0.0)
tokens= 1924 tensor=   60.1 MiB driver=     0.5 MiB (+    0.0)
tokens= 2024 tensor=   63.2 MiB driver=     0.5 MiB (+    0.0)
tokens= 2124 tensor=   66.4 MiB driver=     0.5 MiB (+    0.0)
tokens= 2224 tensor=   69.5 MiB driver=     0.4 MiB (+   -0.0)
tokens= 2324 tensor=   72.6 MiB driver=     0.4 MiB (+    0.0)
tokens= 2424 tensor=   75.8 MiB driver=     0.4 MiB (+    0.0)
tokens= 2524 tensor=   78.9 MiB driver=     0.4 MiB (+    0.0)
tokens= 2624 tensor=   82.0 MiB driver=     0.4 MiB (+    0.0)
tokens= 2724 tensor=   85.1 MiB driver=     0.4 MiB (+    0.0)
tokens= 2824 tensor=   88.2 MiB driver=     0.4 MiB (+    0.0)
tokens= 2924 tensor=   91.4 MiB driver=     0.4 MiB (+    0.0)
tokens= 3024 tensor=   94.5 MiB driver=     0.4 MiB (+    0.0)
tokens= 3124 tensor=   97.6 MiB driver=     0.4 MiB (+    0.0)
tokens= 3224 tensor=  100.8 MiB driver=     0.4 MiB (+    0.0)
tokens= 3324 tensor=  103.9 MiB driver=     0.4 MiB (+    0.0)
growth over 24 new shapes: -0.0 MiB
growth over 24 repeated shapes: 0.0 MiB
[3812, 32000] fp32 retained: 0.0 MiB
final driver memory: 0.4 MiB
```

</details>
Pull Request resolved: pytorch#195441
Approved by: https://github.com/kurtamohler, https://github.com/malfet
danielhanchen added a commit to unslothai/unsloth that referenced this pull request Nov 22, 2026
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/mps Run MPS tests (subset of trunk) Merged open source release notes: mps Release notes category topic: improvements topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants