Skip to content

[Pytorch][Attention][CP][CI] Make 3221 changes robust - #3438

Open
sudhakarsingh27 wants to merge 7 commits into
NVIDIA:mainfrom
sudhakarsingh27:sudhakars/make_3221_robust
Open

[Pytorch][Attention][CP][CI] Make 3221 changes robust#3438
sudhakarsingh27 wants to merge 7 commits into
NVIDIA:mainfrom
sudhakarsingh27:sudhakars/make_3221_robust

Conversation

@sudhakarsingh27

@sudhakarsingh27 sudhakarsingh27 commented Aug 28, 2026

Copy link
Copy Markdown
Member

Description

Follow-up to #3221 that makes no-load-balance THD all-gather context parallelism robust for unpadded FlashAttention backends and fixes a bug in CP

  • Request zeroed FlashAttention 2 backward outputs for deterministic grouped-query attention.
  • Match the autograd backward return arity to the forward inputs.
  • Allow unpadded FlashAttention backends while retaining the FusedAttention requirement when sequences have inter-sequence padding.

Validation

  • git diff --check
  • Python 3.12 syntax compilation
  • H100, FlashAttention 2, deterministic GQA, THD all-gather CP without load balancing: output, dQ, dK, and dV matched on both ranks
  • qa/L1_pytorch_distributed_unittest/test.sh: all tests passed

FlashAttention 2 backward needs zeroed gradient outputs for deterministic grouped-query attention in this path. Permit unpadded FlashAttention backends because inter-sequence padding requires no backend-specific handling, and align the autograd return arity with the forward inputs.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
@cyanguwa cyanguwa added the 2.19 label Aug 28, 2026
@greptile-apps

greptile-apps Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This follow-up broadens unpadded no-load-balance THD context parallelism to supported FlashAttention backends, initializes FA2 backward accumulation buffers, and corrects the custom autograd return arity.

  • Relaxes the backend assertion while retaining the FusedAttention requirement for inter-sequence padding.
  • Requests zeroed FA2 backward buffers for no-load-balance THD grouped-query attention.
  • Reworks the distributed FlashAttention test to use capability-based availability checks.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains.

No blocking failure remains.

Important Files Changed

Filename Overview
transformer_engine/pytorch/attention/dot_product_attention/context_parallel.py Extends unpadded no-load-balance THD support across FlashAttention backends, initializes relevant backward buffers, and aligns backward return arity.
tests/pytorch/attention/test_attention_with_cp.py Replaces the architecture-specific FlashAttention test gate with runtime backend capability detection.

Reviews (6): Last reviewed commit: "Rely on existing L1 attention runs" | Re-trigger Greptile

Comment on lines 5549 to +5551
assert (
use_fused_attention or use_flash_attn_3
), "No-load-balance THD partitioning requires FusedAttention or FlashAttention 3."
assert not (
use_flash_attn_3 and pad_between_seqs
), "No-load-balance THD partitioning with FlashAttention 3 does not support padding yet."
use_fused_attention or not pad_between_seqs
), "No-load-balance THD partitioning only supports padding with FusedAttention."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 FA2 lacks regression coverage

This relaxed assertion enables FlashAttention 2 for unpadded no-load-balance THD all-gather context parallelism, but the existing test covers only FlashAttention 3. Add FA2 forward and backward coverage so regressions in output, dQ/dK/dV initialization, backend argument compatibility, and autograd behavior are detected.

Knowledge Base Used: PyTorch attention execution

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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.

Addressed. The no-load-balance CP test now queries production backend selection for the BF16 THD all-gather GQA configuration and validates output, dQ, dK, and dV. In L1, FA4 is disabled and FA3 is not installed, so the existing deterministic and non-deterministic suite runs exercise FA2 without a separate forced invocation. The focused path was also validated with FA2 on SM90 and SM100 and with FA3/FA4 on supported hardware.

use_flash_attn_3 and pad_between_seqs
), "No-load-balance THD partitioning with FlashAttention 3 does not support padding yet."
use_fused_attention or not pad_between_seqs
), "No-load-balance THD partitioning only supports padding with FusedAttention."

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.

Is this change because FA3 and pad_between_seqs are incompatible? I think it's better to keep them separate right?

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.

This is not a general incompatibility between FA3 and pad_between_seqs. The limitation is specific to the no-load-balance partitioning strategy. Backend selection remains separate: any supported FlashAttention version can run the unpadded path, while inter-sequence padding in this path remains FusedAttention-only. The assertion and comments now reflect that separation.

Document why FA2 initializes backward gradient buffers and why inter-sequence padding remains limited to FusedAttention. This makes the supported backend matrix explicit without changing behavior.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
cyanguwa
cyanguwa previously approved these changes Aug 28, 2026
Reuse the focused CP test across supported FlashAttention generations and force FA2 from L1 in a fresh process. Backend controls are read at import time, so a separate invocation covers deterministic FA2 GQA backward without adding runner plumbing.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Query the existing backend selector for the exact THD context-parallel configuration instead of duplicating architecture and version gates in pytest. This keeps capability decisions in one place while allowing version-forced test invocations to exercise production selection logic.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Document why the focused deterministic case runs in a fresh process and how the version flags force FA2, while the full runs retain normal backend selection.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
The general context-parallel test is already collected by both L1 attention invocations. Since this suite selects FA2, a separate forced deterministic invocation duplicates coverage without exercising a distinct path.

Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
@sudhakarsingh27 sudhakarsingh27 changed the title make_3221_robust [Pytorch][Attention][CP][CI]Make 3221 changes robust Aug 28, 2026
@sudhakarsingh27 sudhakarsingh27 changed the title [Pytorch][Attention][CP][CI]Make 3221 changes robust [Pytorch][Attention][CP][CI] Make 3221 changes robust Aug 28, 2026

@KshitijLakhani KshitijLakhani left a comment

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.

LGTM for merge after CI passes successfully !

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.

3 participants