-
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.
- Loading branch information
1 parent
1820a5a
commit 0d9ddf2
Showing
10 changed files
with
191 additions
and
100 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
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,44 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
from typing import Optional, Union | ||
import deeplink_ext.cpp_extensions as ext | ||
|
||
__all__ = ["RotaryEmbedding"] | ||
|
||
|
||
def apply_rotary( | ||
x: torch.Tensor, | ||
cos: torch.Tensor, | ||
sin: torch.Tensor, | ||
interleaved=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): | ||
""" | ||
Apply rotary positional embedding to input tensor x. | ||
Args: | ||
x (Tensor): Input tensor x is of shape [seq_length, ... , dim] | ||
cos (Tensor): Input tensor cos is of shape [seq_length, ..., dim] | ||
sin (Tensor): Input tensor sin is of shape [seq_length, ..., dim] | ||
Returns: | ||
Tensor: The input tensor after applying RoPE | ||
""" | ||
|
||
@staticmethod | ||
def forward(ctx, x, cos, sin): | ||
cos, _ = torch.chunk(cos, 2, -1) | ||
sin, _ = torch.chunk(sin, 2, -1) | ||
ctx.save_for_backward(cos, sin) | ||
return apply_rotary(x, cos, sin) | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
cos, sin = ctx.saved_tensors | ||
return apply_rotary(grad_output, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
import torch_npu | ||
|
||
__all__ = ["RotaryEmbedding"] | ||
|
||
|
||
def _unsqueeze_to_4d(x: torch.Tensor): | ||
while x.dim() < 4: | ||
x = x.unsqueeze(0) | ||
return x | ||
|
||
|
||
def apply_rotary(x: torch.Tensor, cos, sin, confj=False, interleaved=False): | ||
assert interleaved == False, "interleaved not support by torch_npu" | ||
|
||
x_view = _unsqueeze_to_4d(x) | ||
cos_view = _unsqueeze_to_4d(cos) | ||
sin_view = _unsqueeze_to_4d(sin) | ||
|
||
cos_cat = torch.cat([cos_view, cos_view], -1) | ||
sin_cat = torch.cat([sin_view, sin_view], -1) | ||
|
||
if confj: | ||
sin_cat.neg_() | ||
|
||
x_view_chunks = x_view.chunk(2, -1) | ||
x_view_new = torch.cat([-x_view_chunks[1], x_view_chunks[0]], -1) | ||
|
||
print(cos_cat.shape) | ||
print(x_view.shape) | ||
|
||
cos_x = torch.mul(cos_cat, x_view) | ||
sin_x = torch.mul(sin_cat, x_view_new) | ||
out = cos_x + sin_x | ||
|
||
return out | ||
|
||
|
||
class RotaryEmbedding(torch.autograd.Function): | ||
""" | ||
Apply rotary positional embedding to input tensor x. | ||
Args: | ||
x (Tensor): Input tensor x is of shape [seq_length, ... , dim] | ||
cos (Tensor): Input tensor cos is of shape [seq_length, ..., dim] | ||
sin (Tensor): Input tensor sin is of shape [seq_length, ..., dim] | ||
Returns: | ||
Tensor: The input tensor after applying RoPE | ||
""" | ||
|
||
@staticmethod | ||
def forward(ctx, x, cos, sin): | ||
cos, _ = torch.chunk(cos, 2, -1) | ||
sin, _ = torch.chunk(sin, 2, -1) | ||
ctx.save_for_backward(cos, sin) | ||
return apply_rotary(x, cos, sin) | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
cos, sin = ctx.saved_tensors | ||
return apply_rotary(grad_output, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
import deeplink_ext.cpp_extensions as ext | ||
|
||
assert hasattr(ext, "scaled_masked_softmax_fwd") and hasattr(ext, "scaled_masked_softmax_bwd") | ||
|
||
__all__ = ["ScaledMaskedSoftmax"] | ||
|
||
|
||
class ScaledMaskedSoftmax(torch.autograd.Function): | ||
|
||
@staticmethod | ||
def forward(ctx, input, mask, scale, fixed_triu_mask): | ||
out = torch.empty_like(input) | ||
ext.scaled_masked_softmax_fwd(out, input, mask, scale, fixed_triu_mask) | ||
ctx.save_for_backward(out, mask) | ||
ctx.scale = scale | ||
ctx.fixed_triu_mask = fixed_triu_mask | ||
return out | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
out, mask = ctx.saved_tensors | ||
grad_input = torch.empty_like(grad_output) | ||
ext.scaled_masked_softmax_bwd(grad_input, grad_output, out, mask, ctx.scale, ctx.fixed_triu_mask) | ||
return grad_input, None, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
import torch_npu | ||
|
||
__all__ = ["ScaledMaskedSoftmax"] | ||
|
||
|
||
class ScaledMaskedSoftmax(torch.autograd.Function): | ||
|
||
@staticmethod | ||
def forward(ctx, input, mask, scale, fixed_triu_mask): | ||
out = torch_npu.npu_scaled_masked_softmax(input, mask, scale, fixed_triu_mask) | ||
|
||
ctx.save_for_backward(out, mask) | ||
ctx.scale = scale | ||
ctx.fixed_triu_mask = fixed_triu_mask | ||
return out | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
out, mask = ctx.saved_tensors | ||
grad_input = torch.empty_like(grad_output) | ||
|
||
grad_input = torch_npu.npu_scaled_masked_softmax_backward(grad_output, out, mask, ctx.scale, | ||
ctx.fixed_triu_mask) | ||
|
||
return grad_input, None, 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
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 |
---|---|---|
@@ -1,45 +1,13 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
from typing import Optional, Union | ||
import deeplink_ext.cpp_extensions as ext | ||
from deeplink_ext.utils import PlatformType, deeplink_ext_get_platform_type | ||
|
||
platform_type = deeplink_ext_get_platform_type() | ||
if platform_type == PlatformType.TORCH_NPU: | ||
from ._rotary_embedding_npu import RotaryEmbedding | ||
elif platform_type == PlatformType.TORCH_DIPU: | ||
from ._rotary_embedding_dipu import RotaryEmbedding | ||
else: | ||
raise ImportError | ||
|
||
__all__ = ["RotaryEmbedding"] | ||
|
||
|
||
def apply_rotary( | ||
x: torch.Tensor, | ||
cos: torch.Tensor, | ||
sin: torch.Tensor, | ||
interleaved=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): | ||
""" | ||
Apply rotary positional embedding to input tensor x. | ||
Args: | ||
x (Tensor): Input tensor x is of shape [seq_length, ... , dim] | ||
cos (Tensor): Input tensor cos is of shape [seq_length, ..., dim] | ||
sin (Tensor): Input tensor sin is of shape [seq_length, ..., dim] | ||
Returns: | ||
Tensor: The input tensor after applying RoPE | ||
""" | ||
|
||
@staticmethod | ||
def forward(ctx, x, cos, sin): | ||
cos, _ = torch.chunk(cos, 2, -1) | ||
sin, _ = torch.chunk(sin, 2, -1) | ||
ctx.save_for_backward(cos, sin) | ||
return apply_rotary(x, cos, sin) | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
cos, sin = ctx.saved_tensors | ||
return apply_rotary(grad_output, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,13 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
import torch | ||
import deeplink_ext.cpp_extensions as ext | ||
from deeplink_ext.utils import PlatformType, deeplink_ext_get_platform_type | ||
|
||
|
||
assert hasattr(ext, "scaled_masked_softmax_fwd") and hasattr( | ||
ext, "scaled_masked_softmax_bwd" | ||
) | ||
platform_type = deeplink_ext_get_platform_type() | ||
if platform_type == PlatformType.TORCH_NPU: | ||
from ._scaled_masked_softmax_npu import ScaledMaskedSoftmax | ||
elif platform_type == PlatformType.TORCH_DIPU: | ||
from ._scaled_masked_softmax_dipu import ScaledMaskedSoftmax | ||
else: | ||
raise ImportError | ||
|
||
__all__ = ["ScaledMaskedSoftmax"] | ||
|
||
|
||
class ScaledMaskedSoftmax(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, input, mask, scale, fixed_triu_mask): | ||
out = torch.empty_like(input) | ||
ext.scaled_masked_softmax_fwd(out, input, mask, scale, fixed_triu_mask) | ||
ctx.save_for_backward(out, mask) | ||
ctx.scale = scale | ||
ctx.fixed_triu_mask = fixed_triu_mask | ||
return out | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
out, mask = ctx.saved_tensors | ||
grad_input = torch.empty_like(grad_output) | ||
ext.scaled_masked_softmax_bwd( | ||
grad_input, grad_output, out, mask, ctx.scale, ctx.fixed_triu_mask | ||
) | ||
return grad_input, None, None, None |