-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[diffusion] [NPU] support ring attention on NPU with FA #21383
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
105 changes: 105 additions & 0 deletions
105
python/sglang/multimodal_gen/runtime/layers/attention/backends/ascend_fa.py
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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.multimodal_gen.runtime.layers.attention.backends.attention_backend import ( | ||
| AttentionBackend, | ||
| AttentionImpl, | ||
| AttentionMetadata, | ||
| AttentionMetadataBuilder, | ||
| ) | ||
| from sglang.multimodal_gen.runtime.platforms import AttentionBackendEnum | ||
| from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| @dataclass | ||
| class AscendFAMetadata: | ||
| pass | ||
|
|
||
|
|
||
| class AscendFAMetadataBuilder(AttentionMetadataBuilder): | ||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
| def prepare(self) -> None: | ||
| pass | ||
|
|
||
| def build( | ||
| self, | ||
| **kwargs: dict[str, Any], | ||
| ) -> AttentionMetadata: | ||
| return AscendFAMetadata() | ||
|
|
||
|
|
||
| class AscendFABackend(AttentionBackend): | ||
|
|
||
| @staticmethod | ||
| def get_enum() -> AttentionBackendEnum: | ||
| return AttentionBackendEnum.FA | ||
|
|
||
| @staticmethod | ||
| def get_impl_cls() -> type["AscendFAImpl"]: | ||
| return AscendFAImpl | ||
|
|
||
| @staticmethod | ||
| def get_metadata_cls() -> type["AttentionMetadata"]: | ||
| raise NotImplementedError | ||
Makcum888e marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @staticmethod | ||
| def get_builder_cls() -> type["AttentionMetadataBuilder"]: | ||
| return AscendFAMetadataBuilder | ||
|
|
||
|
|
||
| class AscendFAImpl(AttentionImpl): | ||
|
|
||
| def __init__( | ||
| self, | ||
| num_heads: int, | ||
| head_size: int, | ||
| causal: bool, | ||
| softmax_scale: float, | ||
| num_kv_heads: int | None = None, | ||
| prefix: str = "", | ||
| **extra_impl_args, | ||
| ) -> None: | ||
Makcum888e marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.causal = causal | ||
| self.softmax_scale = softmax_scale | ||
| self.num_heads = num_heads | ||
| self.num_kv_heads = num_kv_heads or num_heads | ||
|
|
||
| def forward( | ||
| self, | ||
| query: torch.Tensor, | ||
| key: torch.Tensor, | ||
| value: torch.Tensor, | ||
| attn_metadata: AttentionMetadata, | ||
Makcum888e marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return_softmax_lse: bool = False, | ||
| ) -> torch.Tensor: | ||
| mask = None | ||
| if self.causal: | ||
| seq_len = query.shape[1] | ||
| mask = torch.triu( | ||
| torch.ones(seq_len, seq_len, device=query.device), diagonal=1 | ||
| ).bool() | ||
| # transpose to bs, heads, seq_len, head_dim | ||
| query = query.transpose(1, 2) | ||
| key = key.transpose(1, 2) | ||
| value = value.transpose(1, 2) | ||
| output, lse = torch.ops.npu.npu_fused_infer_attention_score( | ||
| query, | ||
| key, | ||
| value, | ||
| num_heads=self.num_heads, | ||
| num_key_value_heads=self.num_kv_heads, | ||
| scale=self.softmax_scale, | ||
| input_layout="BNSD", | ||
| softmax_lse_flag=return_softmax_lse, | ||
| atten_mask=mask, | ||
| ) | ||
| output = output.transpose(1, 2) | ||
| if return_softmax_lse: | ||
| return output, lse | ||
| return output | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.