-
Notifications
You must be signed in to change notification settings - Fork 1k
[Perf] torch compile for dit and rope kernel #317
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
15 commits
Select commit
Hold shift + click to select a range
ba0265d
init
ZJY0516 eeb5dec
update
ZJY0516 5f82d72
update
ZJY0516 2b570c4
update
ZJY0516 7d2ad68
update
ZJY0516 dcfaf36
remove comments
ZJY0516 0f51e5c
Merge branch 'main' into torch-compile
ZJY0516 72e2437
update
ZJY0516 90f3c17
Merge branch 'main' into torch-compile
ZJY0516 4e8c8b0
Merge branch 'main' into torch-compile
ZJY0516 6a41f77
update
ZJY0516 24c3394
update
ZJY0516 f7c1409
update
ZJY0516 18ed830
update
ZJY0516 e30ee51
update
ZJY0516 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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| import torch.nn as nn | ||
|
|
||
| from vllm_omni.utils.platform_utils import detect_device_type | ||
|
|
||
|
|
||
| class CustomOp(nn.Module): | ||
| """ | ||
| Base class for custom ops. | ||
| Dispatches the forward method to the appropriate backend. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.is_cuda = detect_device_type() == "cuda" | ||
| self._forward_method = self.dispatch_forward() | ||
|
|
||
| def dispatch_forward(self) -> Callable: | ||
| if self.is_cuda: | ||
| return self.forward_cuda | ||
| else: | ||
| return self.forward_native | ||
|
|
||
| def forward(self, *args, **kwargs) -> Any: | ||
| return self._forward_method(*args, **kwargs) | ||
|
|
||
| def forward_native(self, *args, **kwargs): | ||
| """PyTorch-native implementation of the forward method. | ||
| This method is optional. If implemented, it can be used with compilers | ||
| such as torch.compile or PyTorch XLA. Also, it can be used for testing | ||
| purposes. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def forward_cuda(self, *args, **kwargs): | ||
| raise NotImplementedError |
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,80 @@ | ||
| import torch | ||
| from einops import rearrange, repeat | ||
|
|
||
| from vllm_omni.diffusion.layers.custom_op import CustomOp | ||
|
|
||
|
|
||
| def rotate_half(x, interleaved=False): | ||
| if not interleaved: | ||
| x1, x2 = x.chunk(2, dim=-1) | ||
| return torch.cat((-x2, x1), dim=-1) | ||
| else: | ||
| x1, x2 = x[..., ::2], x[..., 1::2] | ||
| return rearrange(torch.stack((-x2, x1), dim=-1), "... d two -> ... (d two)", two=2) | ||
|
|
||
|
|
||
| def apply_rotary_emb_torch(x, cos, sin, interleaved=False): | ||
| """ | ||
| x: (batch_size, seqlen, nheads, headdim) | ||
| cos, sin: (seqlen, rotary_dim / 2) or (batch_size, seqlen, rotary_dim / 2) | ||
| """ | ||
| ro_dim = cos.shape[-1] * 2 | ||
| assert ro_dim <= x.shape[-1] | ||
| cos = repeat(cos, "... d -> ... 1 (2 d)" if not interleaved else "... d -> ... 1 (d 2)") | ||
| sin = repeat(sin, "... d -> ... 1 (2 d)" if not interleaved else "... d -> ... 1 (d 2)") | ||
| return torch.cat( | ||
| [ | ||
| x[..., :ro_dim] * cos + rotate_half(x[..., :ro_dim], interleaved) * sin, | ||
| x[..., ro_dim:], | ||
| ], | ||
| dim=-1, | ||
| ) | ||
|
|
||
|
|
||
| class RotaryEmbedding(CustomOp): | ||
| """ | ||
| rotary positional embedding. | ||
| interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead | ||
| of 1st half and 2nd half (GPT-NeoX style). | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| is_neox_style: bool = False, | ||
| ) -> None: | ||
| super().__init__() | ||
| self.is_neox_style = is_neox_style | ||
| self.interleaved = not is_neox_style | ||
|
|
||
| def forward_cuda( | ||
| self, | ||
| x: torch.Tensor, | ||
| cos: torch.Tensor, | ||
| sin: torch.Tensor, | ||
| ) -> torch.Tensor: | ||
| from vllm.vllm_flash_attn.layers.rotary import apply_rotary_emb | ||
|
|
||
| if cos.dim() == 3: | ||
| # (B, S, D/2) -> (S, D/2) | ||
| cos = cos[0] | ||
| sin = sin[0] | ||
|
|
||
| return apply_rotary_emb( | ||
| x, | ||
| cos, | ||
| sin, | ||
| interleaved=self.interleaved, | ||
| ) | ||
|
|
||
| def forward_native( | ||
| self, | ||
| x: torch.Tensor, | ||
| cos: torch.Tensor, | ||
| sin: torch.Tensor, | ||
| ) -> torch.Tensor: | ||
| return apply_rotary_emb_torch( | ||
| x, | ||
| cos, | ||
| sin, | ||
| interleaved=self.interleaved, | ||
| ) |
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
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.