Skip to content

Fix bug in torch compile and seqdim is integer - #1217

Merged
ksivaman merged 4 commits into
NVIDIA:mainfrom
wplf:patch-2
Oct 11, 2024
Merged

ksivaman merged 4 commits into
NVIDIA:mainfrom
wplf:patch-2

Conversation

@wplf

@wplf wplf commented Sep 29, 2024 •

Copy link
Copy Markdown
Contributor

Description

When NVTE_TORCH_COMPILE is 0, jit_fuser will be torch.jit.script.
And the compiled function don't accept interger. Then, bug will occur in the function below due to the seq_dim argument.

@jit_fuser
def flash_attn_fwd_out_correction(out, out_per_step, seq_dim, softmax_lse, softmax_lse_per_step):
    """Merge partial outputs of each step in Attention with context parallelism"""
    softmax_lse_corrected_exp = torch.exp(softmax_lse_per_step - softmax_lse).movedim(2, seq_dim)
    softmax_lse_corrected_exp = softmax_lse_corrected_exp.unsqueeze(-1)
    out_corrected = out_per_step * softmax_lse_corrected_exp
    out.add_(out_corrected)

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 refractor

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

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

@ksivaman ksivaman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@wplf Can we not annotate flash_attn_fwd_out_correction instead? IIUC, the fusers assume every argument is a tensor unless explicitly specified.

@wplf

wplf commented Oct 1, 2024 •

Copy link
Copy Markdown
Contributor Author

@wplf Can we not annotate flash_attn_fwd_out_correction instead? IIUC, the fusers assume every argument is a tensor unless explicitly specified.
Of course, But I am afraid removing the annotation will degrade TE performance in the case of torch.complie.

Which solution do you perfer to?

@ksivaman

ksivaman commented Oct 1, 2024

Copy link
Copy Markdown
Member

Let's try and annotate the function and see if it fixes the error!

@wplf

wplf commented Oct 2, 2024

Copy link
Copy Markdown
Contributor Author

Let's try and annotate the function and see if it fixes the error!

I have checked that removing the annotation will fix the problem. And the pull request is changed, too.

@ksivaman

ksivaman commented Oct 2, 2024

Copy link
Copy Markdown
Member

I don't see the changes yet, could you update the PR?

change the jit_fuser to torch.compile on flash_attn_fwd_out_correction

Signed-off-by: 李金梁 <[email protected]>
@wplf

wplf commented Oct 2, 2024 •

Copy link
Copy Markdown
Contributor Author

I don't see the changes yet, could you update the PR?

Sorry, I don't expect you so quickly like that. You can check it now.
For code style, I change jit_fuser to torch.compile instead of removing it.

Comment thread transformer_engine/pytorch/attention.py Resolved
@ksivaman

ksivaman commented Oct 2, 2024

Copy link
Copy Markdown
Member

We still want the jit_fuser decorator instead of forcing it to be torch.compile. What I meant by annotating is to add explicit types to the arguments. See given suggestion (just an example)

@ksivaman

Copy link
Copy Markdown
Member

@wplf Could you see if the new changes resolve the error?

@ksivaman

Copy link
Copy Markdown
Member

/te-ci pytorch

@xrennvidia

Copy link
Copy Markdown
Collaborator

LGTM.

@ksivaman
ksivaman merged commit 9ee2dbd into NVIDIA:main Oct 11, 2024
timmoon10 pushed a commit to timmoon10/TransformerEngine that referenced this pull request Nov 7, 2024
* Fix bug in torch compile and seqdim is integer

Signed-off-by: 李金梁 <[email protected]>

* Update attention.py

change the jit_fuser to torch.compile on flash_attn_fwd_out_correction

Signed-off-by: 李金梁 <[email protected]>

* Annotate fused functions

Signed-off-by: Kirthi Shankar Sivamani <[email protected]>

---------

Signed-off-by: 李金梁 <[email protected]>
Signed-off-by: Kirthi Shankar Sivamani <[email protected]>
Co-authored-by: Kirthi Shankar Sivamani <[email protected]>
xiaoteng-cui-mt pushed a commit to MooreThreads/MT-TransformerEngine that referenced this pull request Oct 9, 2026
* Fix bug in torch compile and seqdim is integer

Signed-off-by: 李金梁 <[email protected]>

* Update attention.py

change the jit_fuser to torch.compile on flash_attn_fwd_out_correction

Signed-off-by: 李金梁 <[email protected]>

* Annotate fused functions

Signed-off-by: Kirthi Shankar Sivamani <[email protected]>

---------

Signed-off-by: 李金梁 <[email protected]>
Signed-off-by: Kirthi Shankar Sivamani <[email protected]>
Co-authored-by: Kirthi Shankar Sivamani <[email protected]>
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.

3 participants