Skip to content

Enable FA4 for context-parallel attention - #3149

Open
sudhakarsingh27 wants to merge 3 commits into
NVIDIA:mainfrom
sudhakarsingh27:fa4-cp-exploration
Open

Enable FA4 for context-parallel attention#3149
sudhakarsingh27 wants to merge 3 commits into
NVIDIA:mainfrom
sudhakarsingh27:fa4-cp-exploration

Conversation

@sudhakarsingh27

@sudhakarsingh27 sudhakarsingh27 commented Jun 26, 2026

Copy link
Copy Markdown
Member

Description

Enable FlashAttention 4 for context-parallel attention on the supported p2p, all_gather, and a2a paths; a2a+p2p remains disabled pending validation.

FA4 can represent padded THD layouts with physical padded offsets plus seqused_* visible lengths. This lets the CP path preserve the layout distinction used by the non-CP reference path.

This PR is limited to FA4 context-parallel enablement. FlashAttention package isolation, unsupported-configuration gating, and test-helper import fixes remain in #3341.

Type of change

  • Documentation change
  • Bug fix
  • New feature
  • Breaking change
  • Infra/Build change
  • Code refactoring

Changes

  • Route FA4 forward/backward calls through the CP p2p, all_gather, and a2a paths.
  • Pass padded THD cu_seqlens_* together with seqused_* for FA4.
  • Keep FA4 disabled for a2a+p2p.
  • Extend CP test eligibility so FA4-only environments reach the FA4-specific guards.

Validation

  • Existing H100- and B200-class padded THD CP canaries: 6 passed, 10 skipped, 48 deselected per platform.
  • Python compileall, Black, and git diff --check.
  • Updated GPU CI pending after restoring the PR to the FA4-only change set.

Checklist

  • I have read and followed the contributing guidelines.
  • I have commented non-obvious behavior.
  • My changes generate no new warnings.
  • I have added tests or CI coverage for the behavior.

@greptile-apps

greptile-apps Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Enables FlashAttention 4 for supported context-parallel attention paths.

  • Routes FA4 forward and backward kernels through the p2p, all_gather, and a2a implementations while leaving a2a+p2p disabled.
  • Preserves padded THD physical offsets using cu_seqlens_* and supplies visible token lengths through seqused_*.
  • Extends backend selection and context-parallel tests to recognize supported FA4 configurations.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
transformer_engine/pytorch/attention/dot_product_attention/context_parallel.py Adds FA4 kernel dispatch, argument adaptation, result handling, and autograd propagation across the supported context-parallel algorithms.
transformer_engine/pytorch/attention/dot_product_attention/backends.py Imports FA4 private forward/backward entry points and propagates FA4 selection and padded THD metadata into context-parallel execution.
transformer_engine/pytorch/attention/dot_product_attention/utils.py Enables FA4 backend selection for p2p, all-gather, and A2A context parallelism while retaining the combined A2A/P2P exclusion.
tests/pytorch/attention/test_attention_with_cp.py Extends context-parallel test eligibility and padded THD coverage conditions to FlashAttention 4.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[DotProductAttention] --> B{Context parallel?}
  B -->|No| C[Standard attention backend]
  B -->|Yes| D{CP communication type}
  D -->|p2p| E[FA4 P2P forward/backward]
  D -->|all_gather| F[FA4 all-gather forward/backward]
  D -->|a2a| G[FA4 A2A forward/backward]
  D -->|a2a+p2p| H[Disable FA4]
  E --> I{Padded THD?}
  F --> I
  G --> I
  I -->|Yes| J[Padded cu_seqlens plus seqused lengths]
  I -->|No| K[Regular FA4 arguments]
Loading

Reviews (7): Last reviewed commit: "Merge current main into FA4 CP support" | Re-trigger Greptile

sudhakarsingh27 and others added 3 commits July 29, 2026 17:04
Add minimal FA4 raw-call plumbing for p2p, all_gather, and a2a context-parallel attention. FA4 accepts padded THD cu_seqlens plus seqused values, so keep padded physical offsets separate from visible token lengths for CP and non-CP reference paths.

Keep a2a+p2p disabled because that hierarchical path has not been validated for FA4. Update CP tests so FA4-only environments do not skip the FlashAttention CP matrix before the FA4-specific guards run.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Bring PR 3149 onto the latest upstream code so the FA4 context-parallel paths can be validated against current main.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Comment on lines +303 to +306
@pytest.mark.skipif(
not (FlashAttentionUtils.v2_plus or FlashAttentionUtils.v4_is_installed),
reason="Flash-attn 2.0+ or Flash-attn 4 is required.",
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Shouldn't 2.0+ also apply to flash-attn 4?

**fa_forward_kwargs,
)
else:
fa_forward_args_thd = get_fa_args(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

is it possible to extend get_fa_args instead of creating a new get_fa4_thd_kwargs?

"Disabling FlashAttention 4 for context parallelism with cp_comm_type = %s",
cp_comm_type,
)
use_flash_attention_4 = False

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.

Please make sure the CI is running FA4 and for all the relevant tests.

dv=dv,
seqused_q=seqused_q,
seqused_k=seqused_k,
)

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.

Same here, as the comment above about fwd.

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