Skip to content

[MPS] migrate log softmax to metal - #195441

Closed
Isalia20 wants to merge 4 commits into
pytorch:mainfrom
Isalia20:logsoftmax-to-metal
Closed

Isalia20 wants to merge 4 commits into
pytorch:mainfrom
Isalia20:logsoftmax-to-metal

Conversation

@Isalia20

@Isalia20 Isalia20 commented Aug 31, 2026 •

Copy link
Copy Markdown
Collaborator

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
Latency
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
Memory usage before:
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
Memory usage after
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

@pytorch-bot

pytorch-bot Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit 58c05c8 with merge base 58a938a (image):

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

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 31, 2026
@Isalia20 Isalia20 added the topic: improvements topic category label Aug 31, 2026
@Isalia20
Isalia20 marked this pull request as ready for review August 31, 2026 14:56
@Isalia20
Isalia20 requested a review from malfet as a code owner August 31, 2026 14:56
@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 3, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 3, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

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

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
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) ciflow/trunk Trigger trunk jobs on your pull request 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.

6 participants