Skip to content

fix(CP, FA): the conditional logic in the FA version contains a vulnerability when processing the output of Flash Attn forward pass - #2825

Merged
ptrendx merged 2 commits into
NVIDIA:mainfrom
021ai:CP_FA_fix
May 12, 2026
Merged

ptrendx merged 2 commits into
NVIDIA:mainfrom
021ai:CP_FA_fix

Conversation

@zhujian19891203

Copy link
Copy Markdown
Contributor

Description

The conditional logic in the FA version contains a vulnerability when processing the output of Flash Attn forward pass, this maybe caused accidentally.

For example, when I only install FA3, and FA2 is totally not installed, something is wrong.
image

Fixes # (issue)

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

Please list the changes introduced in this PR:

  • Just context_parallel.py file

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

@greptile-apps

greptile-apps Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a conditional logic bug in context_parallel.py where Flash Attention 3 (FA3) would incorrectly fall into the FA2 < v2.7.0 output-indexing branch when fa_utils.v2_7_0_plus was False. The fix adds not use_flash_attn_3 as a guard to the outer if condition at three symmetric call sites, ensuring FA3 always routes to the else branch that uses the correct output indices (fa_outputs[0], fa_outputs[1]).

Confidence Score: 5/5

Safe to merge — the fix is minimal, correct, and applied symmetrically at all three affected call sites.

The change is a clean, targeted bug fix with no new logic introduced. All three hunks follow the same pattern: adding not use_flash_attn_3 to the outer guard so FA3 is never routed to the FA2 < v2.7.0 output-index path. No regressions are possible for pure FA2 users. No P0 or P1 issues remain.

No files require special attention.

Important Files Changed

Filename Overview
transformer_engine/pytorch/attention/dot_product_attention/context_parallel.py Three symmetric fixes to the FA version-dispatch logic: the outer condition now includes not use_flash_attn_3, preventing FA3 from incorrectly accessing FA2-style output indices when v2_7_0_plus is False.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[flash_attn_fwd called] --> B{use_flash_attn_3?}
    B -- Yes --> E[else branch\nout = fa_outputs 0\nlse = fa_outputs 1\nrng_state = None]
    B -- No --> C{fa_utils.v2_7_0_plus?}
    C -- Yes --> E
    C -- No --> D[if branch\nout = fa_outputs 4\nlse = fa_outputs 5\nrng_state = fa_outputs 7]
    style D fill:#d4edda
    style E fill:#d4edda
Loading

Reviews (3): Last reviewed commit: "Merge branch 'main' into CP_FA_fix" | Re-trigger Greptile

@zhujian19891203 zhujian19891203 changed the title Fix bug: the conditional logic in the FA version contains a vulnerability when processing the output of Flash Attn forward pass Fix(CP, FA): the conditional logic in the FA version contains a vulnerability when processing the output of Flash Attn forward pass Apr 3, 2026
@zhujian19891203 zhujian19891203 changed the title Fix(CP, FA): the conditional logic in the FA version contains a vulnerability when processing the output of Flash Attn forward pass fix(CP, FA): the conditional logic in the FA version contains a vulnerability when processing the output of Flash Attn forward pass Apr 3, 2026
@zhujian19891203

zhujian19891203 commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@cyanguwa Can you take a look at this PR, please?

@ptrendx ptrendx added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Apr 9, 2026
@ptrendx

ptrendx commented Apr 21, 2026

Copy link
Copy Markdown
Member

@zhujian19891203 Thank you for the contribution. Could you sign your commits? Please see this for the instructions on how to do it.

…he conditional logic in the FA version contains a vulnerability, fix it

Signed-off-by: zhujian <[email protected]>
@zhujian19891203

Copy link
Copy Markdown
Contributor Author

@zhujian19891203 Thank you for the contribution. Could you sign your commits? Please see this for the instructions on how to do it.

Thanks for your review. I have signed my commits by following your link.

@cyanguwa

Copy link
Copy Markdown
Collaborator

/te-ci torch

@ptrendx
ptrendx merged commit c3fd8f8 into NVIDIA:main May 12, 2026
1 check passed
faradawn pushed a commit to faradawn/TransformerEngine that referenced this pull request May 14, 2026
…rability when processing the output of Flash Attn forward pass (NVIDIA#2825)

fix(CP, FA): when processing the output of Flash Attn forward pass, the conditional logic in the FA version contains a vulnerability, fix it

Signed-off-by: zhujian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.16.0 community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants