-
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
28274a0
commit 27da2ec
Showing
4 changed files
with
86 additions
and
91 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
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,79 @@ | ||
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
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