forked from facebookresearch/fairseq
-
Notifications
You must be signed in to change notification settings - Fork 3
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
50e56c9
commit 61a2110
Showing
7 changed files
with
87 additions
and
35 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
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,20 +1,35 @@ | ||
import torch | ||
from torch import nn | ||
|
||
|
||
class RMSNorm(nn.Module): | ||
def __init__(self, normalized_shape, eps=1e-6): | ||
super().__init__() | ||
self.eps = eps | ||
self.normalized_shape = normalized_shape | ||
self.scale = nn.Parameter(torch.ones(normalized_shape)) | ||
|
||
def forward(self, x): | ||
x_fp32 = x.float() | ||
x_normed = ( | ||
x_fp32 * torch.rsqrt(x_fp32.pow(2).mean(-1, keepdim=True) + self.eps) | ||
).type_as(x) | ||
return x_normed * self.scale | ||
|
||
def extra_repr(self): | ||
return f"normalized_shape={self.normalized_shape}, eps={self.eps}" | ||
import torch | ||
import torch.nn as nn | ||
|
||
try: | ||
from apex.normalization import FusedRMSNorm as _FusedRMSNorm | ||
|
||
has_fused_rmsnorm = True | ||
|
||
class FusedRMSNorm(_FusedRMSNorm): | ||
@torch.jit.unused | ||
def forward(self, x): | ||
if not x.is_cuda: | ||
return super().forward(x) | ||
else: | ||
with torch.cuda.device(x.device): | ||
return super().forward(x) | ||
|
||
except ImportError: | ||
has_fused_rmsnorm = False | ||
|
||
|
||
def RMSNorm(normalized_shape, eps=1e-5, elementwise_affine=True, export=False): | ||
if torch.jit.is_scripting() or torch.jit.is_tracing(): | ||
export = True | ||
if not export and torch.cuda.is_available() and has_fused_rmsnorm: | ||
return FusedRMSNorm( | ||
normalized_shape=normalized_shape, | ||
eps=eps, | ||
elementwise_affine=elementwise_affine, | ||
) | ||
return nn.RMSNorm( | ||
normalized_shape=normalized_shape, | ||
eps=eps, | ||
elementwise_affine=elementwise_affine, | ||
) |
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