[PyTorch] torch.compile support for FusedAttention - #3472
Draft
pggPL wants to merge 3 commits into
Draft
Conversation
…elpers Behavior-neutral restructuring of the cuDNN fused attention autograd function, mirroring what NVIDIA#2967 did for Linear: FusedAttnFwdArgs / FusedAttnBwdArgs dataclasses, module-level _fused_attn_forward_impl, _fused_attn_setup_ctx and _fused_attn_backward_impl, and a thin FusedAttnFunc wrapper taking the differentiable tensors plus one args object instead of 37 positional arguments. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
13 tasks
pggPL
force-pushed
the
fused_attention_torch_compile
branch
from
September 4, 2026 10:42
0fe1efd to
889032f
Compare
The bias / softmax_offset aux entries exist only when the tensor was passed, not merely when the bias / softmax type asks for one. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Register the cuDNN fused attention forward/backward as a torch custom op through dynamo/custom_op.py, reusing the single-argument helpers behind FusedAttnFunc, so DotProductAttention with the fused backend traces under torch.compile(fullgraph=True), including KV caching. F16/BF16 only; FP8, context parallelism, score_mod, FAv2 backward and CPU offloading keep falling back to eager via no_torch_dynamo(when=...). Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
pggPL
force-pushed
the
fused_attention_torch_compile
branch
from
September 4, 2026 11:05
889032f to
e16a1ab
Compare
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
Depends on #3480 (the
FusedAttnFuncrestructuring); the first commit here is that PR. Review the second commit only.Follow-up to #3053 (custom-op framework,
Linear) and #3286 (DotProductAttentionundertorch.compilefor the FlashAttention / unfused backends). This PR makes the FusedAttention (cuDNN) backend trace undertorch.compile(fullgraph=True)as well, soDotProductAttentionno longer graph-breaks atFusedAttention.forward-- on Hopper/Blackwell that is the default backend, so until now the whole attention block was an eager island there.The cuDNN fused attention forward/backward is registered as a torch custom op through
dynamo/custom_op.py, exactly likeLinear: theFusedAttnFwdArgs/FusedAttnBwdArgsdataclasses and the_fused_attn_forward_impl/_fused_attn_setup_ctx/_fused_attn_backward_implhelpers from #3480 are shared between the eagerFusedAttnFuncand the op, so this PR only adds the data-free fake forward/backward (TensorSpecs for the output, the softmax stats / rng_state aux tensors and the grads), the registration, the eager-fallback gate and the dispatch.Scope is F16/BF16 attention. Configurations the op does not cover keep working as before --
FusedAttention.forwardnow carries@no_torch_dynamo(when=...)and falls back to an eager island with a warning for: FP8 attention, context parallelism,score_mod,NVTE_FUSED_ATTN_USE_FAv2_BWDand CPU activation offloading. KV caching (inference_params, paged and non-paged) goes through the op.Notes for reviewers:
bias/softmax_offsetinputs as aux tensors, and the saved q/k/v/out are the inputs / output themselves; a custom op may not return its inputs, so the forward leaves those slotsNoneand names their source inctx_attrs["saved_from"](from [PyTorch] Split FusedAttnFunc into single-argument forward/backward helpers #3480), and only stats / rng_state cross the op boundary.bs3hd,bsh3d,bs2hd, ...) the backward returnsdq/dk/dvas views of onedQKVbuffer, whichtorch.libraryrejects as aliasing outputs; they are made contiguous on the op path only (a copy only in the packed case; eager keeps returning views).thddepends on the cuDNN version and SM ([tq, h, 1]ragged vs[b, h, sq, 1]); the fake mirrors the C++ condition using theassume_constant_resultversion/capability helpers. I could only test on an RTX Ada (sm89), where cuDNN attention withthdis not available, so neither is cuDNN attention with a KV cache -- so thethdbranch of the fake and the KV-cache cases (kv_cache_bshd-fused,test_dpa_torch_compile_kv_cache_decoding[*-fused]) are untested here and rely on the Hopper/Blackwell CI runners.Type of change
Changes
backends.py:_fused_attn_forward_fake/_fused_attn_backward_fake,_fused_attn_backward_op_impl,register_custom_op(op_name="fused_attn");FusedAttention.forwarddecorated with@no_torch_dynamo(when=_needs_eager_fused_attention)and, undertorch.compile, dispatching to the custom op instead ofFusedAttnFunc.apply.tests/pytorch/test_torch_compile.py: thefusedbackend now runs throughtest_dpa_torch_compile,test_dpa_torch_compile_cudagraphs,test_dpa_torch_compile_kv_cache_decodingandtest_dpa_torch_compile_eager_fallback;test_dpa_torch_compile_around_fusedkeeps covering the eager-island path by disabling the op.Checklist: