Optionally pad QK/V for better kernel selection - #3339
Open
janEbert wants to merge 6 commits into
Open
Conversation
While support for native QKV shapes has been added, some MLA kernels still benefit from having QKV padded to the same head dimension. Since it's not easy to automate this decision, we probe `get_attention_backend` twice with and without padding to figure out which version selects the better kernel. If padding results in a better kernel than not padding, we pad; otherwise, Q, K, and V stay native. Signed-off-by: janEbert <janpabloe@nvidia.com>
Contributor
Greptile SummaryThe PR adds backend probing that optionally pads unequal MLA QK/V head dimensions, trims backend outputs to the original V width, and adds L0 tests for selection and numerical behavior.
Confidence Score: 4/5The PR is not yet safe to merge because packed QKV/KV inputs can still dispatch padded fused attention using unchanged unpadded packed storage. The attempted packed-input fix only excludes Float8TensorStorage values; ordinary packed tensors still produce padded views while the original packed buffers are consumed by fused-attention quantization. Files Needing Attention: transformer_engine/pytorch/attention/dot_product_attention/dot_product_attention.py Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Build native AttentionParams] --> B[Probe native backend]
B --> C{Native uses unfused attention?}
C -- No --> D[Keep native QK/V dimensions]
C -- Yes --> E[Probe equal padded dimensions]
E --> F{Fused or Flash available?}
F -- No --> D
F -- Yes --> G[Pad Q, K, and V views]
G --> H[Dispatch selected backend]
D --> H
H --> I{Padding applied?}
I -- Yes --> J[Trim output to original V width]
I -- No --> K[Return native-width output]
J --> K
Reviews (2): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile |
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
Signed-off-by: janEbert <janpabloe@nvidia.com>
janEbert
force-pushed
the
optional-mla-pad
branch
from
August 10, 2026 23:30
8d821bf to
bdc487e
Compare
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
While support for native QKV shapes has been added, some MLA kernels
still benefit from having QKV padded to the same head dimension. Since
it's not easy to automate this decision, we probe
get_attention_backendtwice with and without padding to figure outwhich version selects the better kernel.
If padding results in a better kernel than not padding, we pad;
otherwise, Q, K, and V stay native.
Ref NVIDIA/Megatron-LM#6240, ref NVIDIA/Megatron-LM#6241.
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: