Skip to content

add flash-attn deterministic option to flash-attn>=2.4.1#31961

Merged
ArthurZucker merged 4 commits into
huggingface:mainfrom
junrae6454:full-determinism-flash-attention
Jul 16, 2024
Merged

add flash-attn deterministic option to flash-attn>=2.4.1#31961
ArthurZucker merged 4 commits into
huggingface:mainfrom
junrae6454:full-determinism-flash-attention

Conversation

@junrae6454

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR introduces support for the deterministic mode available in Flash Attention version 2.4.1 and above.
The deterministic mode in Flash Attention 2.4.1 ensures reproducible results, which is crucial for debugging and scientific research. By integrating this feature, users can benefit from deterministic behavior in their models.
The changes include enabling deterministic behavior through an environment variable and integrating this feature into the _flash_attention_forward function.

  • Flash Attention Forward Function Update:

    • Added deterministic parameter to _flash_attention_forward to control deterministic behavior.
    • Modified the function to check for Flash Attention version 2.4.1 or higher and set the deterministic argument accordingly.
  • Trainer Utilities Update:

    • Set the FLASH_ATTENTION_DETERMINISTIC environment variable in enable_full_determinism.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@ArthurZucker

@ArthurZucker ArthurZucker 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.

sounds good! are there other args we should be reading from env ?

import torch.nn.functional as F

from .utils import is_flash_attn_2_available
from .utils import is_flash_attn_2_available, is_flash_attn_greater_or_equal_2_41

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.

Suggested change
from .utils import is_flash_attn_2_available, is_flash_attn_greater_or_equal_2_41
from .utils import is_flash_attn_2_available, is_flash_attn_greater

)
flash_kwargs = {"window_size": (sliding_window, sliding_window)} if use_sliding_windows else {}

if is_flash_attn_greater_or_equal_2_41():

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.

Suggested change
if is_flash_attn_greater_or_equal_2_41():
if is_flash_attn_greater_or_equal("2.41"):

Comment thread src/transformers/utils/import_utils.py Outdated
Comment on lines +817 to +821
def is_flash_attn_greater_or_equal_2_41():
if not _is_package_available("flash_attn"):
return False

return version.parse(importlib.metadata.version("flash_attn")) >= version.parse("2.4.1")

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.

Suggested change
def is_flash_attn_greater_or_equal_2_41():
if not _is_package_available("flash_attn"):
return False
return version.parse(importlib.metadata.version("flash_attn")) >= version.parse("2.4.1")

unnecessary, see def is_flash_attn_greater_or_equal(library_version: str):

@junrae6454

Copy link
Copy Markdown
Contributor Author

sounds good! are there other args we should be reading from env ?

@ArthurZucker
Other changes are related to the model structure and the model's return value, so there doesn't seem to be anything else that needs to be read from the environment.

@ArthurZucker ArthurZucker mentioned this pull request Jul 16, 2024
4 tasks

@ArthurZucker ArthurZucker 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.

thanks for iterating

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.

2 participants