-
Notifications
You must be signed in to change notification settings - Fork 6.4k
[core] support sage attention through kernels
#12439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sayakpaul
wants to merge
9
commits into
main
Choose a base branch
from
sage-kernels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e9ea1c5
up
sayakpaul f630dab
Merge branch 'main' into sage-kernels
sayakpaul 18c3e8e
Merge branch 'main' into sage-kernels
sayakpaul d344134
support automatic dispatch.
sayakpaul 3688c9d
Merge branch 'main' into sage-kernels
sayakpaul 23c173e
Merge branch 'main' into sage-kernels
sayakpaul 1b1a497
disable compile support for now./
sayakpaul 177f5b3
up
sayakpaul 4e62a44
Merge branch 'main' into sage-kernels
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| import inspect | ||
| import math | ||
| from enum import Enum | ||
| from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Tuple, Union | ||
| from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union | ||
|
|
||
| import torch | ||
|
|
||
|
|
@@ -83,12 +83,17 @@ | |
| raise ImportError( | ||
| "To use FA3 kernel for your hardware from the Hub, the `kernels` library must be installed. Install with `pip install kernels`." | ||
| ) | ||
| from ..utils.kernels_utils import _get_fa3_from_hub | ||
| from ..utils.kernels_utils import _DEFAULT_HUB_ID_FA3, _DEFAULT_HUB_ID_SAGE, _get_kernel_from_hub | ||
|
|
||
| flash_attn_interface_hub = _get_fa3_from_hub() | ||
| flash_attn_interface_hub = _get_kernel_from_hub(_DEFAULT_HUB_ID_FA3) | ||
| flash_attn_3_func_hub = flash_attn_interface_hub.flash_attn_func | ||
|
|
||
| sage_interface_hub = _get_kernel_from_hub(_DEFAULT_HUB_ID_SAGE) | ||
| sage_attn_func_hub = sage_interface_hub.sageattn | ||
|
|
||
| else: | ||
| flash_attn_3_func_hub = None | ||
| sage_attn_func_hub = None | ||
|
|
||
| if _CAN_USE_SAGE_ATTN: | ||
| from sageattention import ( | ||
|
|
@@ -162,10 +167,6 @@ def wrap(func): | |
| # - CP with sage attention, flex, xformers, other missing backends | ||
| # - Add support for normal and CP training with backends that don't support it yet | ||
|
|
||
| _SAGE_ATTENTION_PV_ACCUM_DTYPE = Literal["fp32", "fp32+fp32"] | ||
| _SAGE_ATTENTION_QK_QUANT_GRAN = Literal["per_thread", "per_warp"] | ||
| _SAGE_ATTENTION_QUANTIZATION_BACKEND = Literal["cuda", "triton"] | ||
|
Comment on lines
-165
to
-167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see their usage, hence removed. |
||
|
|
||
|
|
||
| class AttentionBackendName(str, Enum): | ||
| # EAGER = "eager" | ||
|
|
@@ -190,6 +191,7 @@ class AttentionBackendName(str, Enum): | |
|
|
||
| # `sageattention` | ||
| SAGE = "sage" | ||
| SAGE_HUB = "sage_hub" | ||
| SAGE_VARLEN = "sage_varlen" | ||
| _SAGE_QK_INT8_PV_FP8_CUDA = "_sage_qk_int8_pv_fp8_cuda" | ||
| _SAGE_QK_INT8_PV_FP8_CUDA_SM90 = "_sage_qk_int8_pv_fp8_cuda_sm90" | ||
|
|
@@ -404,14 +406,14 @@ def _check_attention_backend_requirements(backend: AttentionBackendName) -> None | |
| ) | ||
|
|
||
| # TODO: add support Hub variant of FA3 varlen later | ||
| elif backend in [AttentionBackendName._FLASH_3_HUB]: | ||
| elif backend in [AttentionBackendName._FLASH_3_HUB, AttentionBackendName.SAGE_HUB]: | ||
| if not DIFFUSERS_ENABLE_HUB_KERNELS: | ||
| raise RuntimeError( | ||
| f"Flash Attention 3 Hub backend '{backend.value}' is not usable because the `DIFFUSERS_ENABLE_HUB_KERNELS` env var isn't set. Please set it like `export DIFFUSERS_ENABLE_HUB_KERNELS=yes`." | ||
| f"Attention backend '{backend.value}' is not usable because the `DIFFUSERS_ENABLE_HUB_KERNELS` env var isn't set. Please set it like `export DIFFUSERS_ENABLE_HUB_KERNELS=yes`." | ||
| ) | ||
| if not is_kernels_available(): | ||
| raise RuntimeError( | ||
| f"Flash Attention 3 Hub backend '{backend.value}' is not usable because the `kernels` package isn't available. Please install it with `pip install kernels`." | ||
| f"Attention backend '{backend.value}' is not usable because the `kernels` package isn't available. Please install it with `pip install kernels`." | ||
| ) | ||
|
|
||
| elif backend in [ | ||
|
|
@@ -1756,6 +1758,31 @@ def _sage_attention( | |
| return (out, lse) if return_lse else out | ||
|
|
||
|
|
||
| @_AttentionBackendRegistry.register( | ||
| AttentionBackendName.SAGE_HUB, | ||
| constraints=[_check_device_cuda, _check_qkv_dtype_bf16_or_fp16, _check_shape], | ||
| supports_context_parallel=False, | ||
| ) | ||
| def _sage_attention_hub( | ||
| query: torch.Tensor, | ||
| key: torch.Tensor, | ||
| value: torch.Tensor, | ||
| is_causal: bool = False, | ||
| scale: Optional[float] = None, | ||
| return_lse: bool = False, | ||
| _parallel_config: Optional["ParallelConfig"] = None, | ||
| ) -> torch.Tensor: | ||
| lse = None | ||
| if _parallel_config is None: | ||
| out = sage_attn_func_hub(q=query, k=key, v=value) | ||
| if return_lse: | ||
| out, lse, *_ = out | ||
| else: | ||
| raise NotImplementedError("SAGE attention doesn't yet support parallelism.") | ||
|
|
||
| return (out, lse) if return_lse else out | ||
|
|
||
|
|
||
| @_AttentionBackendRegistry.register( | ||
| AttentionBackendName.SAGE_VARLEN, | ||
| constraints=[_check_device_cuda, _check_qkv_dtype_bf16_or_fp16, _check_shape], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge fan of downloading all kernels if the env variable is set, since it's downloading stuff without explicit user consent. I think we need to rethink this part a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. This is why #12475. Let's get that reviewed and merged first as it will unblock this PR and also #12387