Skip to content

[PyTorch] Add torch.compile support for MultiheadAttention - #3974

Draft
pggPL wants to merge 2 commits into
NVIDIA:mainfrom
pggPL:mha_torch_compile
Draft

pggPL wants to merge 2 commits into
NVIDIA:mainfrom
pggPL:mha_torch_compile

Conversation

@pggPL

@pggPL pggPL commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Enable torch.compile(fullgraph=True) for single-GPU FP16/BF16 MultiheadAttention, including self/cross-attention, GQA, input LayerNorm/RMSNorm, and rotary embeddings in SBHD/BSHD layouts across the FlashAttention 2, fused cuDNN, and unfused backends.

Includes and builds on split-parameter torch.compile support (#3623), extended to LayerNormLinear, so separate Q/K/V parameters sharing adjacent storage compile without a concatenation copy while disjoint parts fall back to torch.cat. Unpacked GQA/RoPE paths ensure contiguous Q/K/V inputs. Q/K normalization, attention checkpointing, FP8 attention, and context parallelism remain unsupported by the underlying compile paths.

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

  • Include [PyTorch] Support split Linear parameters with torch.compile #3623 split-parameter torch.compile support (dynamo/concat.py, TENSOR_LIST custom-op support, prepare_parameter, Linear) and extend it to LayerNormLinear, with unit and numerics tests.
  • Avoid reading or constructing the default FP8 recipe in MultiheadAttention.forward when FP8 is disabled.
  • Use traceable torch.split for Q/K/V splitting under torch.compile, ensuring contiguous unpacked Q/K/V tensors before DotProductAttention.
  • Wrap fused RoPE forward/backward kernels (tex.fused_rope_forward / tex.fused_rope_backward) as torch.library.custom_ops (te_attention::rope_fwd and te_attention::rope_bwd) with fake kernels while preserving FusedRoPEFunc.
  • Document torch.compile(fullgraph=True) support and limitations on MultiheadAttention.
  • Add torch.compile tests in tests/pytorch/test_torch_compile.py covering split-parameter Linear/LayerNormLinear, MultiheadAttention (self/cross, input LayerNorm/RMSNorm, fused/separate QKV params, GQA, RoPE, extra outputs, FP16/BF16, SBHD/BSHD, all three backends, CUDA Graphs train/eval), and standalone fused RoPE.

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

pggPL added 2 commits December 1, 2026 09:52
…rch.compile

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL
pggPL force-pushed the mha_torch_compile branch from 4eef1d6 to 332b0e4 Compare December 1, 2026 08:58

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant