-
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 rms norm op, and rotary_embeding and mha. --------- Co-authored-by: root <[email protected]>
- Loading branch information
1 parent
778fc8c
commit 8261278
Showing
29 changed files
with
663 additions
and
659 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,4 @@ | ||
from .deeplink import rms_norm_out, rms_norm, rms_norm_backward_out, rms_norm_backward | ||
|
||
|
||
__all__ = ["rms_norm_out", "rms_norm", "rms_norm_backward_out", "rms_norm_backward"] |
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,78 @@ | ||
import torch | ||
import deeplink_ext.cpp_extensions as cpp_ext | ||
|
||
|
||
def rms_norm_out(output, inv_rms, input, normalized_shape, weight, bias, eps): | ||
if None == normalized_shape: | ||
cpp_ext.rms_norm(output, inv_rms, input, weight.shape, weight, bias, eps) | ||
else: | ||
cpp_ext.rms_norm(output, inv_rms, input, normalized_shape, weight, bias, eps) | ||
|
||
|
||
def rms_norm(input, normalized_shape, weight, bias, eps): | ||
output = torch.empty_like(input) | ||
inv_rms_shape = list(input.shape[:-1]) + [1] | ||
inv_rms = torch.empty(inv_rms_shape, dtype=input.dtype, device=input.device) | ||
rms_norm_out(output, inv_rms, input, normalized_shape, weight, bias, eps) | ||
|
||
return [output, inv_rms] | ||
|
||
|
||
def rms_norm_backward_out( | ||
grad_input, | ||
grad_weight, | ||
grad_bias, | ||
grad_output, | ||
input, | ||
weight, | ||
bias, | ||
inv_rms, | ||
normalized_shape, | ||
eps, | ||
): | ||
if None == normalized_shape: | ||
cpp_ext.rms_norm_backward( | ||
grad_input, | ||
grad_weight, | ||
grad_bias, | ||
grad_output, | ||
input, | ||
weight, | ||
bias, | ||
inv_rms, | ||
weight.shape, | ||
eps, | ||
) | ||
else: | ||
cpp_ext.rms_norm_backward( | ||
grad_input, | ||
grad_weight, | ||
grad_bias, | ||
grad_output, | ||
input, | ||
weight, | ||
bias, | ||
inv_rms, | ||
normalized_shape, | ||
eps, | ||
) | ||
|
||
|
||
def rms_norm_backward(input, grad_output, inv_rms, normalized_shape, weight, bias, eps): | ||
grad_input = torch.empty_like(input) | ||
grad_weight = torch.empty_like(weight) | ||
grad_bias = torch.empty_like(bias) | ||
rms_norm_backward_out( | ||
grad_input, | ||
grad_weight, | ||
grad_bias, | ||
grad_output, | ||
input, | ||
weight, | ||
bias, | ||
inv_rms, | ||
normalized_shape, | ||
eps, | ||
) | ||
|
||
return [grad_input, grad_weight, grad_bias] |
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,5 +1,40 @@ | ||
# Copyright (c) 2024, DeepLink. | ||
|
||
from . import mha, rms_norm, rotary | ||
from . import mha | ||
|
||
__all__ = ["mha", "rms_norm", "rotary"] | ||
|
||
_not_impl = "[deeplink_ext] {op_name} is not implemented in diopi. Falling back to the slower torch implementation." | ||
|
||
|
||
try: | ||
from .rms_norm import RMSNorm, RMSNormWithNormalizedShape | ||
except: | ||
print( | ||
_not_impl.format(op_name="RMSNorm or RMSNormWithNormalizedShape"), | ||
) | ||
from .rms_norm_fallback import ( | ||
RMSNorm, | ||
RMSNormWithNormalizedShape, | ||
) | ||
|
||
|
||
try: | ||
from .rotary_embedding import apply_rotary | ||
except: | ||
print(_not_impl.format(op_name="apply_rotary")) | ||
from .rotary_embeddinig_fallback import apply_rotary | ||
|
||
|
||
try: | ||
from .mha import SelfAttention, CrossAttention | ||
except Exception as e: | ||
print(_not_impl.format(op_name="mha")) | ||
from .mha_fallback import SelfAttention, CrossAttention | ||
|
||
__all__ = [ | ||
"SelfAttention", | ||
"CrossAttention", | ||
"RMSNorm", | ||
"RMSNormWithNormalizedShape", | ||
"apply_rotary", | ||
] |
Oops, something went wrong.