[Add] Add FlashAttention 4 context parallel support.#3259
Conversation
Signed-off-by: yangfan.bai <yangfan.bai@shopee.com>
for more information, see https://pre-commit.ci
Greptile SummaryAdds FlashAttention 4 support to the P2P context-parallel attention path.
Confidence Score: 2/5This PR should not merge until FA4 CP preserves configured attention dropout and accepts supported hybrid group layouts. The new FA4 raw path omits dropout state for an admitted training configuration, causing incorrect attention behavior, and its mode guard rejects a reachable a2a+p2p subgroup layout before execution. Files Needing Attention: transformer_engine/pytorch/attention/dot_product_attention/context_parallel.py and transformer_engine/pytorch/attention/dot_product_attention/utils.py Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[DotProductAttention] --> B[Backend selection]
B -->|FA4 + context parallel| C[attn_forward_func_with_cp]
C -->|p2p or a2a+p2p| D[AttnFuncWithCPAndKVP2P]
D --> E[FA4 raw forward]
E --> F[Ring communication and output merge]
F --> G[FA4 raw backward]
G --> H[Q/K/V gradients]
Reviews (2): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile |
| flash_attn_fwd = ( | ||
| _flash_attn_fwd_v4 # pylint: disable=possibly-used-before-assignment | ||
| ) | ||
| fa_forward_kwargs["return_lse"] = True | ||
| fa_forward_kwargs["softcap"] = 0.0 |
There was a problem hiding this comment.
When FA4 context-parallel training uses nonzero attention_dropout, this branch omits dropout_p from the raw forward call, while the backward path also omits the dropout probability and corresponding RNG state, causing the configured dropout to be ignored or gradients to use an inconsistent dropout mask.
Knowledge Base Used: PyTorch Attention Stack
| cp_group, dist_group_type | ||
| ), f"cp_group must be {dist_group_type} type for {cp_comm_type=}!" | ||
|
|
||
| if use_flash_attn_4 and cp_comm_type not in ["p2p", "a2a+p2p"]: | ||
| raise ValueError( | ||
| "FlashAttention v4 context parallelism is supported for " | ||
| "cp_comm_type='p2p' and 'a2a+p2p' only." |
There was a problem hiding this comment.
Hybrid mode normalization rejects FA4
When cp_comm_type="a2a+p2p" uses a P2P subgroup of size one, the preceding normalization rewrites the mode to "a2a", so this guard rejects an advertised supported hybrid configuration and raises ValueError before attention executes.
Knowledge Base Used: PyTorch Attention Stack
Summary
This PR adds FlashAttention 4 support for TransformerEngine context parallel attention.
The change enables FA4 in the CP backend selection path and wires the FA4 raw CuTe forward/backward APIs into the existing P2P context-parallel implementation. It also adds a lightweight distributed comparison script that validates FA4 CP outputs and gradients against non-CP full-sequence attention.
Changes
_flash_attn_fwdand_flash_attn_bwdAPIs.use_flash_attn_4through the FlashAttention CP path.run_fa4_cp_vs_non_cp.pyto compare FA4 CP with non-CP attention.Validation
python3 -m py_compilepassed for modified Python files.git diff --checkpassed.