Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions megatron/core/transformer/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,26 @@
rearrange = None

try:
from flashattn_hopper.flash_attn_interface import _flash_attn_forward
from flashattn_hopper.flash_attn_interface import (
from flash_attn_3.flash_attn_interface import _flash_attn_forward

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.

Can we get rid of the nested try/except clause loops?

It would look a bit nicer if we have smth like:

HAVE_FA3=False
try:
  # import old way
  HAVE_FA3=True
except:
  pass # ideally log
try:
  # import new way
  HAVE_FA3=True
except:
  pass # ideally log

Or maybe add a branch depending on the fa version or smth. TransformerEngine has FlashAttentionUtils that can give you the FA version, we could use that to unify the behaviour between the training and inference side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The second try block would have to be wrapped in a if not HAVE_FA3 clause though, so it seems like it would be equally messy? Open to either option though

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.

Agreed! Still looks more linear/simpler to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to not nest the imports

from flash_attn_3.flash_attn_interface import (
flash_attn_with_kvcache as flash_attn3_with_kvcache,
)

HAVE_FA3 = True
except:
except ImportError as e:
HAVE_FA3 = False

if not HAVE_FA3:
try:
from flashattn_hopper.flash_attn_interface import _flash_attn_forward
from flashattn_hopper.flash_attn_interface import (
flash_attn_with_kvcache as flash_attn3_with_kvcache,
)

HAVE_FA3 = True
except ImportError as e:
pass

try:
from flash_mla import flash_mla_with_kvcache, get_mla_metadata

Expand Down