[PyTorch] Split FusedAttnFunc into single-argument forward/backward helpers - #3480
Open
pggPL wants to merge 2 commits into
Open
[PyTorch] Split FusedAttnFunc into single-argument forward/backward helpers#3480pggPL wants to merge 2 commits into
pggPL wants to merge 2 commits into
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>
This was referenced Sep 4, 2026
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>
pggPL
marked this pull request as ready for review
September 4, 2026 13:28
Collaborator
Author
|
/te-ci pytorch L1 |
Contributor
Greptile SummaryThe PR restructures the cuDNN fused-attention autograd implementation into typed forward/backward argument containers and module-level helpers without intending to change behavior.
Confidence Score: 5/5The PR appears safe to merge with no changed-code defects identified. The refactored paths preserve saved-tensor handling, native auxiliary-pack ordering, the supported shared page-table model, backward gradient arity, and module importability. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[FusedAttention.forward] --> B[Build FusedAttnFwdArgs]
B --> C[FusedAttnFunc.apply]
C --> D[_fused_attn_forward_impl]
D --> E[_fused_attn_setup_ctx]
E --> F[Save backward tensors and metadata]
F --> G[FusedAttnFunc.backward]
G --> H[Restore FusedAttnBwdArgs]
H --> I[_fused_attn_backward_impl]
I --> J[dQ, dK, dV, bias and offset gradients]
Reviews (1): Last reviewed commit: "[PyTorch] Match the fused attention aux ..." | Re-trigger Greptile |
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
Behavior-neutral restructuring of
FusedAttnFunc, the cuDNN fused attentionautograd.Function, mirroring what #2967 did forLinearahead of #3053. Groundwork for #3472 (torch.compile support for the fused attention backend), which registers these helpers as a torch custom op throughdynamo/custom_op.py.FusedAttnFwdArgs/FusedAttnBwdArgsdataclasses replace the 37 positional arguments ofFusedAttnFunc.applyand the loosectx.*attributes._fused_attn_forward_impl,_fused_attn_setup_ctxand_fused_attn_backward_implhold the actual logic;FusedAttnFuncis a thin wrapper taking the differentiable tensors (q,k,v,attn_bias,softmax_offset) plus one args object, so its backward returns 6 grads instead of 36Nones.softmax_stats,rng_state,aux_bias,aux_softmax_offset) instead of a variable-length list.Nonein that slot and names its source inctx_attrs["saved_from"];_fused_attn_setup_ctxre-attaches it. This is what a custom op needs (it may not return its own inputs) and keeps the FP8 paths -- where aNonef16 slot means "not needed" -- exact.ctxattributes the backward never read (fp8_recipe,fp8_meta,is_output_fp8).No functional change intended. Tested on an RTX Ada with the fused backend forced:
tests/pytorch/attention/test_attention.py(test_dpa*,test_dot_product_attention,test_transformer_layer),test_kv_cache.py,test_cpu_offloading_v1.pyandtest_torch_compile.pyall pass. FP8 attention and context parallelism could not be exercised on that GPU and rely on CI.Type of change
Changes
attention/dot_product_attention/backends.py:FusedAttnFwdArgs,FusedAttnBwdArgs,_fused_attn_forward_impl,_fused_attn_setup_ctx,_fused_attn_backward_impl,_reload_qkv_layout;FusedAttnFuncreduced to a wrapper;FusedAttention.forwardbuilds the args object.Checklist: