Skip to content

[PyTorch] Split FusedAttnFunc into single-argument forward/backward helpers - #3480

Merged
pggPL merged 5 commits into
NVIDIA:mainfrom
pggPL:fused_attn_func_refactor
Sep 14, 2026
Merged

pggPL merged 5 commits into
NVIDIA:mainfrom
pggPL:fused_attn_func_refactor

Conversation

@pggPL

@pggPL pggPL commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Description

Behavior-neutral restructuring of FusedAttnFunc, the cuDNN fused attention autograd.Function, mirroring what #2967 did for Linear ahead of #3053. Groundwork for #3472 (torch.compile support for the fused attention backend), which registers these helpers as a torch custom op through dynamo/custom_op.py.

  • FusedAttnFwdArgs / FusedAttnBwdArgs dataclasses replace the 37 positional arguments of FusedAttnFunc.apply and the loose ctx.* attributes.
  • Module-level _fused_attn_forward_impl, _fused_attn_setup_ctx and _fused_attn_backward_impl hold the actual logic; FusedAttnFunc is 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 36 Nones.
  • The cuDNN aux pack is stored in fixed slots (softmax_stats, rng_state, aux_bias, aux_softmax_offset) instead of a variable-length list.
  • A saved tensor identical to a forward input or the output is not saved twice: the forward hands back None in that slot and names its source in ctx_attrs["saved_from"]; _fused_attn_setup_ctx re-attaches it. This is what a custom op needs (it may not return its own inputs) and keeps the FP8 paths -- where a None f16 slot means "not needed" -- exact.
  • Dropped ctx attributes 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.py and test_torch_compile.py all pass. FP8 attention and context parallelism could not be exercised on that GPU and rely on CI.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • attention/dot_product_attention/backends.py: FusedAttnFwdArgs, FusedAttnBwdArgs, _fused_attn_forward_impl, _fused_attn_setup_ctx, _fused_attn_backward_impl, _reload_qkv_layout; FusedAttnFunc reduced to a wrapper; FusedAttention.forward builds the args object.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

…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>
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
pggPL marked this pull request as ready for review September 4, 2026 13:28
@pggPL
pggPL requested a review from cyanguwa as a code owner September 4, 2026 13:28
@pggPL

pggPL commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@greptile-apps

greptile-apps Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge because no blocking failure remains in the eligible follow-up review scope.

Summary

  • Introduces slotted argument dataclasses for forward and backward state.
  • Extracts forward, context setup, backward, and QKV-layout restoration helpers.
  • Reduces FusedAttnFunc to a thin autograd wrapper and updates FusedAttention.forward to construct the new argument object.

Diagram

sequenceDiagram
    participant FA as FusedAttention
    participant AF as FusedAttnFunc
    participant FW as _fused_attn_forward_impl
    participant CTX as _fused_attn_setup_ctx
    participant BW as _fused_attn_backward_impl
    FA->>AF: apply(q, k, v, bias, offset, fwd_args)
    AF->>FW: forward(fwd_args)
    FW-->>AF: output, max_logit, saved tensors, attributes
    AF->>CTX: populate backward arguments
    CTX-->>AF: tensors to save
    AF-->>FA: output
    Note over AF: Autograd invokes backward
    AF->>BW: backward(bwd_args)
    BW-->>AF: dq, dk, dv, d_bias, d_offset
Loading

Reviews (3) · Last reviewed commit: "[PyTorch] Preserve separate attention pa..."

@cyanguwa cyanguwa added the 2.20 label Sep 4, 2026
Comment thread transformer_engine/pytorch/attention/dot_product_attention/backends.py Outdated
Comment thread transformer_engine/pytorch/attention/dot_product_attention/backends.py Outdated
fwd_args.attn_bias = attn_bias
fwd_args.softmax_offset = softmax_offset
out, max_logit, tensors_to_save_from_forward, ctx_attrs = _fused_attn_forward_impl(fwd_args)
bwd_args = FusedAttnBwdArgs()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we add if ctx is not None: here like in Linear:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

FusedAttnFunc.apply is the only way this backend is invoked, so this if will always be true

Comment thread transformer_engine/pytorch/attention/dot_product_attention/backends.py Outdated
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…defaults

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL
pggPL merged commit e26066b into NVIDIA:main Sep 14, 2026
12 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants