-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lightllm): rotary emb (#60)
move RotaryEmb wrapper for lightllm from cpp to python --------- Co-authored-by: root <[email protected]>
- Loading branch information
1 parent
c98cb5c
commit 778fc8c
Showing
5 changed files
with
64 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
from .rotary_embedding import apply_rotary, RotaryEmbedding | ||
|
||
__all__ = ["apply_rotary", "RotaryEmbedding"] |
This file contains 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,31 @@ | ||
import torch | ||
from typing import Optional, Union | ||
import deeplink_ext.cpp_extensions as ext | ||
|
||
|
||
def apply_rotary( | ||
x: torch.Tensor, | ||
cos: torch.Tensor, | ||
sin: torch.Tensor, | ||
seqlen_offsets: Union[int, torch.Tensor] = 0, | ||
cu_seqlens: Optional[torch.Tensor] = None, | ||
max_seqlen: Optional[int] = None, | ||
interleaved=False, | ||
inplace=False, | ||
conjugate=False, | ||
) -> torch.Tensor: | ||
output = torch.empty_like(x) | ||
ext.apply_rotary(output, x, cos, sin, conjugate, interleaved) | ||
return output | ||
|
||
|
||
class RotaryEmbedding(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, t, cos, sin): | ||
ctx.save_for_backward(cos, sin) | ||
return apply_rotary(t, cos, sin) | ||
|
||
@staticmethod | ||
def backward(ctx, t): | ||
cos, sin = ctx.saved_tensors | ||
return apply_rotary(t, cos, sin, conjugate=True), None, None |
This file contains 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 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