Skip to content

[ET-VK][sdpa] Use numerically-stable softmax in attention weights#18460

Merged
SS-JIA merged 2 commits into
mainfrom
gh/SS-JIA/500/orig
Mar 24, 2026
Merged

[ET-VK][sdpa] Use numerically-stable softmax in attention weights#18460
SS-JIA merged 2 commits into
mainfrom
gh/SS-JIA/500/orig

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #18407 by @SS-JIA
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/SS-JIA/500/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/500/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/main
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/500/orig
Differential Revision: D97757920
@diff-train-skip-merge

The SDPA attention weights softmax shader computed naive softmax:
exp(x) / sum(exp(x)). When attention weights are large (e.g., 151.29 for
Phi-4-mini with head_dim=128), exp(x) overflows float32 (threshold ~88.7),
producing Infinity and then NaN from inf/inf in the normalization step.

This replaces the naive softmax with the standard numerically-stable variant:
exp(x - max(x)) / sum(exp(x - max(x))). The implementation adds a cooperative
max-finding pass (same workgroup reduction pattern as the existing exp_sum pass)
before the exp_sum and normalization passes. The max subtraction ensures that the
largest exponent is 0, preventing overflow.

This fixes Phi-4-mini Vulkan inference which previously produced garbage output
due to NaN propagation from the first transformer layer's attention.

On-device A/B benchmarks on Samsung Galaxy S24 (Adreno 750) with Llama 3.2 1B
(8da4w g128 q4emb, 677 MB) confirm no performance regression:

  Llama 3.2 1B (short prompt, 4 tokens, --warmup):
    Prefill: 67.2 tok/s | Decode: 59.4 tok/s | TTFT: 60 ms

  Llama 3.2 1B (medium prompt, 197 tokens, --warmup):
    Prefill: 723.5 tok/s | Decode: 53.3 tok/s | TTFT: 273 ms

These numbers are within run-to-run variance of the baseline (no fix) measurements,
confirming the additional max-finding pass has negligible overhead.

Differential Revision: [D97757920](https://our.internmc.facebook.com/intern/diff/D97757920/)

ghstack-source-id: 356136427
Pull Request resolved: #18407
@pytorchbot pytorchbot requested a review from SS-JIA as a code owner March 24, 2026 19:59
@pytorch-bot

pytorch-bot Bot commented Mar 24, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 121 Pending

As of commit 186ad12 with merge base 60d57e5 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 24, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

…nt op (#18461)

This PR was created by the merge bot to help merge the original PR into
the main branch.
ghstack PR number: #18408 by
@SS-JIA
^ Please use this as the source of truth for the PR details, comments,
and reviews
ghstack PR base:
https://github.com/pytorch/executorch/tree/gh/SS-JIA/501/base
ghstack PR head:
https://github.com/pytorch/executorch/tree/gh/SS-JIA/501/head
Merge bot PR base:
https://github.com/pytorch/executorch/tree/gh/SS-JIA/500/orig
Merge bot PR head:
https://github.com/pytorch/executorch/tree/gh/SS-JIA/501/orig
Differential Revision:
[D97757921](https://our.internmc.facebook.com/intern/diff/D97757921/)
@diff-train-skip-merge

Co-authored-by: ssjia <ssjia@devvm26340.ftw0.facebook.com>
@SS-JIA SS-JIA merged commit ea9fd12 into main Mar 24, 2026
137 of 140 checks passed
@SS-JIA SS-JIA deleted the gh/SS-JIA/500/orig branch March 24, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants