-
Notifications
You must be signed in to change notification settings - Fork 647
[ops]support triton mrope #4668
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
shiyuan680
wants to merge
1
commit into
vllm-project:main
Choose a base branch
from
shiyuan680:trition_mrope
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.
+94
−0
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -24,7 +24,12 @@ | |
| from vllm.model_executor.layers.rotary_embedding import ( | ||
| DeepseekScalingRotaryEmbedding, MRotaryEmbedding, RotaryEmbedding, | ||
| YaRNScalingRotaryEmbedding) | ||
| from vllm.model_executor.layers.rotary_embedding.mrope import triton_mrope | ||
| from vllm.platforms import CpuArchEnum | ||
| from vllm.triton_utils import HAS_TRITON | ||
|
|
||
| if HAS_TRITON: | ||
| import torch_npu._inductor | ||
|
Collaborator
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. why import torch_npu._inductor here? |
||
|
|
||
| from vllm_ascend.platform import NPUPlatform | ||
| from vllm_ascend.utils import (AscendDeviceType, enable_custom_op, | ||
|
|
@@ -402,12 +407,78 @@ def forward(self, | |
|
|
||
| class AscendMRotaryEmbedding(MRotaryEmbedding): | ||
|
|
||
| def __init__( | ||
| self, | ||
| head_size: int, | ||
| rotary_dim: int, | ||
| max_position_embeddings: int, | ||
| base: float, | ||
| is_neox_style: bool, | ||
| dtype: torch.dtype, | ||
| mrope_section: list[int] | None = None, | ||
| mrope_interleaved: bool = False, | ||
| *, | ||
| scaling_factor: float | None = None, | ||
| extrapolation_factor: float = 1, | ||
| attn_factor: float = 1, | ||
| beta_fast: int = 32, | ||
| beta_slow: int = 1, | ||
| ) -> None: | ||
| self.cos = None | ||
| self.sin = None | ||
| extra_kwargs = { | ||
| "scaling_factor": scaling_factor, | ||
| "extrapolation_factor": extrapolation_factor, | ||
| "attn_factor": attn_factor, | ||
| "beta_fast": beta_fast, | ||
| "beta_slow": beta_slow | ||
| } | ||
| super().__init__(head_size, rotary_dim, max_position_embeddings, base, | ||
| is_neox_style, dtype, mrope_section, | ||
| mrope_interleaved, **extra_kwargs) | ||
|
|
||
| def forward_triton(self, | ||
| positions: torch.Tensor, | ||
| query: torch.Tensor, | ||
| key: torch.Tensor | None = None, | ||
| offsets: torch.Tensor | None = None): | ||
| assert positions.ndim == 2 | ||
| assert key is not None | ||
|
|
||
| self._match_cos_sin_cache_dtype(query) | ||
|
|
||
| if self.cos is None and self.sin is None: | ||
| cos_sin = self.cos_sin_cache[positions] # type: ignore | ||
| cos, sin = cos_sin.chunk(2, dim=-1) | ||
| self.cos = cos.contiguous() | ||
| self.sin = sin.contiguous() | ||
| query_shape = query.shape | ||
| key_shape = key.shape | ||
|
|
||
| assert self.mrope_section | ||
|
|
||
| q, k = triton_mrope( | ||
| query, | ||
| key, | ||
| self.cos, | ||
| self.sin, | ||
| self.mrope_section, | ||
| self.head_size, | ||
| self.rotary_dim, | ||
| self.mrope_interleaved, | ||
| ) | ||
|
|
||
| return q.reshape(query_shape), k.reshape(key_shape) | ||
|
|
||
| def forward_oot( | ||
| self, | ||
| positions: torch.Tensor, | ||
| query: torch.Tensor, | ||
| key: torch.Tensor, | ||
| ): | ||
| if HAS_TRITON and positions.ndim == 2: | ||
| # todo: need cann update | ||
| return self.forward_triton(positions, query, key) | ||
|
Collaborator
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. this rely on CANN 8.5, please make it backward capability with CANN8.3RC2 |
||
| # TODO: This judgment will be removed once the mrope precision issue is fixed | ||
| if self.mrope_section != [ | ||
| 16, 24, 24 | ||
|
|
||
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.
move this import to L31 under
if HAS_TRITON