[PyTorch] Compile-friendly attention API: pointer-free qkv layout detection - #16
Open
pggPL wants to merge 2 commits into
Open
[PyTorch] Compile-friendly attention API: pointer-free qkv layout detection#16pggPL wants to merge 2 commits into
pggPL wants to merge 2 commits into
Conversation
get_qkv_layout inspects storage (data_ptr/storage_offset/stride) to infer
the qkv memory layout, which graph-breaks under torch.compile (gb0156,
UntypedStorage.data_ptr). Add a JAX-style, pointer-free alternative that
derives the layout from how tensors are passed plus qkv_format.
DotProductAttention.forward now:
- accepts key_layer/value_layer=None to signal qkv-packed / kv-packed
inputs (unbound into q,k,v views, no data_ptr);
- accepts an explicit qkv_layout kwarg (escape hatch);
- uses the new get_qkv_layout_pointer_free when packing is signalled,
an explicit layout is given, or torch.compiler.is_compiling().
The existing pointer-detection path stays the default for the current
3-separate-tensor eager case, so eager behavior is unchanged. The return
contract of the layout step (layout string, q,k,v, q_format, kv_format) is
identical, so downstream selection/kernels are untouched.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…-free path The is_compiling() gate in DotProductAttention.forward does not fire when an earlier graph break makes Dynamo skip the forward frame: forward then runs eagerly (is_compiling() is False there), picks the pointer-inspection path, and Dynamo compiles get_qkv_layout as its own frame -- breaking on UntypedStorage.data_ptr. Gate inside get_qkv_layout itself instead: during tracing the check folds to a constant and delegates to get_qkv_layout_pointer_free, so a traced call never reaches pointer inspection. Eager behavior is unchanged. Verified on the workstation (FlashAttention forced, bf16, bshd): data_ptr graph breaks 3 -> 0 end-to-end through DotProductAttention.forward under torch.compile; compiled output bit-exact vs eager; packed (qkv/kv) inputs bit-exact vs separate tensors. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
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.
What
get_qkv_layoutdetects the qkv memory layout by inspecting storage —untyped_storage().data_ptr(),storage_offset(),stride(). Undertorch.compilethis graph-breaks hard (gb0156: Dynamo cannot traceUntypedStorage.data_ptr), and it was one of the main remaining breaks inDotProductAttention.forward.This introduces a JAX-style, pointer-free way to determine the layout, gated so default eager behavior is unchanged.
How
Commit 1 — pointer-free layout detection (JAX-style):
get_qkv_layout_pointer_free(...): builds the layout string fromqkv_format+ packing info only — nodata_ptr/storage_offset/stride. Same return contract asget_qkv_layout.DotProductAttention.forwardnow accepts packing signalled by argument presence (like JAX TE'sQKVLayout):key_layer=None, value_layer=None→query_layeris qkv-packed[..,3,h,d];value_layer=None→key_layeris kv-packed[..,2,h,d]; all three → separate. Packed tensors are unbound into views (traceable). An explicitqkv_layout: Optional[str]kwarg skips detection entirely.torch.compiler.is_compiling(). Otherwise (default eager, 3 separate tensors) the original pointer-inspection path runs unchanged.Commit 2 — gate inside
get_qkv_layoutitself:The forward-level gate does not fire when an earlier graph break makes Dynamo skip the forward frame (forward then runs eagerly,
is_compiling()is False there, and Dynamo compilesget_qkv_layoutas its own frame — breaking ondata_ptr). Checkingis_compiling()insideget_qkv_layoutfolds to a constant during tracing and delegates to the pointer-free path, so a traced call never reaches pointer inspection.Notes / limitations
h3d/h2dinterleaving cannot be signalled by argument presence (packing assumes3/2at dim -3, the JAX convention); pass an explicitqkv_layoutfor those.Verification (workstation, RTX Ada, FlashAttention forced, bf16, bshd)
data_ptrgraph breaks throughDotProductAttention.forwardundertorch.compile: 3 → 0.🤖 Generated with Claude Code