Skip to content

Implement FlashDecoding++ async softmax for split-K SDPA#18867

Open
Gasoonjia wants to merge 6 commits intocuda-graph-samplingfrom
gasoonjia/flashdecoding-pp-async-softmax
Open

Implement FlashDecoding++ async softmax for split-K SDPA#18867
Gasoonjia wants to merge 6 commits intocuda-graph-samplingfrom
gasoonjia/flashdecoding-pp-async-softmax

Conversation

@Gasoonjia
Copy link
Copy Markdown
Contributor

@Gasoonjia Gasoonjia commented Apr 14, 2026

Replace online softmax (per-tile max tracking + cross-split rescaling) with a unified maximum value (phi=5.0) approach from FlashDecoding++.

Key changes:

  • Split kernel: subtract fixed phi instead of tracking running max m_i, eliminating alpha rescaling between tiles
  • Reduce kernel: simple summation of partial outputs instead of max-aware weighted combination; removes M_partial buffer
  • ~12.9% average kernel-level speedup (6.8%-20.1% range) by saving HBM bandwidth (no M_partial reads/writes) and reducing ALU ops

The unified phi works because exp(qk - phi) is numerically stable for typical attention score ranges, and the fixed constant allows all splits to compute independently without synchronization.

Also used KernelAgent(https://github.com/meta-pytorch/KernelAgent) to further optimized the kernel.

cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 14, 2026

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 6 New Failures, 1 Pending, 4 Unrelated Failures

As of commit 1a79d9d with merge base c48ea12 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Apr 14, 2026
@digantdesai digantdesai force-pushed the digantdesai/sdpa-bench-and-perf-stats branch 4 times, most recently from 1af2029 to d5209fc Compare April 15, 2026 01:38
Base automatically changed from digantdesai/sdpa-bench-and-perf-stats to main April 15, 2026 02:16
@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.

@Gasoonjia Gasoonjia marked this pull request as ready for review April 15, 2026 23:46
@Gasoonjia Gasoonjia requested a review from lucylq as a code owner April 15, 2026 23:46
Copy link
Copy Markdown
Contributor

@digantdesai digantdesai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL FlashDecoding++, thanks.

Couple of things, seems like this is still online softmax (i.e you need to do reduce later). And can you add performance numbers with a sweep in the PR summary, thanks.

Also ask claude to do a quality comparisons using various prompts just to spotcheck quality.

@digantdesai
Copy link
Copy Markdown
Contributor

@claude review this PR.

@claude
Copy link
Copy Markdown

claude Bot commented Apr 21, 2026

Claude finished @digantdesai's task in 1m 17s —— View job


Review of PR #18867

  • Gather context and read changed files
  • Analyze the diff and implementation
  • Post review feedback

This PR makes several simplifying changes to the split-K decode kernel. Here's my analysis:

Summary of Changes

  1. Reverted exp2/log2-space optimization back to tl.exp — the previous version pre-scaled Q by sm_scale * log2(e) and used tl.math.exp2 (single PTX ex2 instruction). This PR reverts to tl.exp with per-tile qk * sm_scale multiply.

  2. Removed BATCH_ONE constexpr specialization — previously avoided a division for B=1 decode; now always computes b = pid_bh // H_kv.

  3. Removed _compute_num_splits heuristic — replaced the SM-aware occupancy heuristic (which queried get_device_properties) with a simpler min(max(cdiv(L_kv, 256), 1), 128).

  4. Added _splitk_buf_cache — caches O_partial and L_partial buffers to avoid repeated CUDA allocations. Previous version allocated fresh each call (and used torch.zeros for L_partial vs. torch.empty now).

  5. Removed num_stages=2 from reduce kernel loop — changed from tl.range(0, num_splits, num_stages=2) to tl.range(0, num_splits).

  6. Removed two autotune configs from the split kernel (256/8/3 and 128/8/3).

Issues & Questions

1. L_partial changed from torch.zeros to torch.empty — potential correctness issue

In the previous version, L_partial was initialized with torch.zeros. Now it uses torch.empty (via the buffer cache). This is safe only if every split fully writes its entry before the reduce kernel reads it. However, when L_kv is not evenly divisible by num_splits, the last split may process fewer than chunk_size tokens, but it still writes its l_i (initialized to zero in the kernel via tl.zeros). So this should be correct — but what about splits where start_n >= Lk? If num_splits * chunk_size > Lk, some splits will have start_n >= end_n (since end_n = tl.minimum(start_n + chunk_size, Lk)), meaning the loop body never executes. In that case, l_i stays at tl.zeros (0.0) and acc stays at tl.zeros, and both get stored — writing zeros. So this is fine. But the comment should acknowledge that correctness relies on the kernel writing zeros for empty splits.

2. tl.exp vs tl.math.exp2 regression

The previous version used tl.math.exp2 which maps to a single PTX ex2.approx.f32 instruction. Reverting to tl.exp (which uses ex2 internally but with a multiply by log2(e)) adds ALU work. The PR description says "KernelAgent" was used to further optimize — but this particular change goes in the opposite direction. Was this an intentional revert for readability/simplicity, or an oversight? The exp2 path with pre-scaled Q is strictly fewer FLOPs.

3. _compute_num_splits removal trades adaptivity for simplicity

The old heuristic targeted 2 full SM waves and adapted to the actual GPU (A100 vs RTX 4090 have different SM counts). The new cdiv(L_kv, 256) is simpler but can over-split on small L_kv or under-utilize on GPUs with many SMs. For example, with L_kv=512 you get 2 splits regardless of GPU, whereas the old heuristic would target more splits on a 128-SM GPU. Was there a measurable regression from the old heuristic that motivated this change?

4. BATCH_ONE removal

This was a minor optimization (avoiding an integer division in the kernel). Since B=1 is extremely common in decode, this may have had measurable benefit. Was it benchmarked before removal?

5. Reduce kernel num_stages change

Removing num_stages=2 from the reduce loop means no software pipelining. For large num_splits (up to 128), this could regress the reduce kernel. Was this benchmarked?

Minor Observations

  • The PR title says "Implement FlashDecoding++ async softmax" but the async-softmax (fixed phi) approach was already present in the previous version. This commit is more accurately described as "simplify/tune the FlashDecoding++ split-K kernel" — reverting some micro-optimizations and adding buffer caching.
  • The buffer cache _splitk_buf_cache is a module-level dict that grows unboundedly. In long-running inference servers with varying shapes, this could leak memory. Consider using an LRU cache or at least bounding the cache size.

Overall

The core FlashDecoding++ logic (fixed phi, simple sum in reduce) is correct and unchanged from the prior commit. The main changes are simplification/tuning tradeoffs. The buffer caching is a good optimization for decode latency. The exp2 -> exp revert and _compute_num_splits removal warrant explanation — if the benchmarks in the PR description already account for these changes, then the net speedup speaks for itself, but it would be good to confirm.

@Gasoonjia Gasoonjia force-pushed the gasoonjia/flashdecoding-pp-async-softmax branch from 9ced5cc to b2fbad0 Compare April 23, 2026 05:07
@github-actions github-actions Bot added ciflow/trunk module: arm Issues related to arm backend labels Apr 23, 2026
@Gasoonjia Gasoonjia changed the base branch from main to cuda-graph-sampling April 23, 2026 05:10
Gasoonjia and others added 4 commits April 22, 2026 22:19
Replace online softmax (per-tile max tracking + cross-split rescaling)
with a unified maximum value (phi=5.0) approach from FlashDecoding++.

Key changes:
- Split kernel: subtract fixed phi instead of tracking running max m_i,
  eliminating alpha rescaling between tiles
- Reduce kernel: simple summation of partial outputs instead of
  max-aware weighted combination; removes M_partial buffer
- ~12.9% average kernel-level speedup (6.8%-20.1% range) by saving
  HBM bandwidth (no M_partial reads/writes) and reducing ALU ops

The unified phi works because exp(qk - phi) is numerically stable
for typical attention score ranges, and the fixed constant allows
all splits to compute independently without synchronization.
Keep only sdpa.py changes on this branch; revert all other files
(aoti_delegate_handle.h, benchmark_sdpa.py, cuda_backend.cpp,
main.cpp, model.py) to their main branch state.
@Gasoonjia Gasoonjia force-pushed the gasoonjia/flashdecoding-pp-async-softmax branch from b2fbad0 to 39589ae Compare April 23, 2026 05:20
@Gasoonjia Gasoonjia force-pushed the cuda-graph-sampling branch from d3bca0d to 5245f64 Compare April 23, 2026 05:20
safe_diff = tl.where(
m_ij[:, None] > -float("inf"), qk - m_ij[:, None], -float("inf")
)
# FlashDecoding++ async softmax: subtract unified phi instead of local max
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@digantdesai here we replace the online softmax with async softmax by using a unified phi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/cuda ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: arm Issues related to arm backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants