-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [transformer] add rms-norm * fix assert
- Loading branch information
Showing
7 changed files
with
82 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import torch | ||
|
||
|
||
class RMSNorm(torch.nn.Module): | ||
""" https://arxiv.org/pdf/1910.07467.pdf | ||
""" | ||
|
||
def __init__( | ||
self, | ||
dim: int, | ||
eps: float = 1e-6, | ||
): | ||
super().__init__() | ||
self.eps = eps | ||
self.weight = torch.nn.Parameter(torch.ones(dim)) | ||
|
||
def _norm(self, x): | ||
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) | ||
|
||
def forward(self, x): | ||
x = self._norm(x.float()).type_as(x) | ||
return x * self.weight |
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 |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
# -*- coding: utf-8 -*- | ||
# Copyright [2023-11-28] <[email protected], Xingchen Song> | ||
import torch | ||
from torch.nn import BatchNorm1d, LayerNorm | ||
from wenet.paraformer.embedding import ParaformerPositinoalEncoding | ||
from wenet.transformer.norm import RMSNorm | ||
from wenet.transformer.positionwise_feed_forward import ( | ||
GatedVariantsMLP, MoEFFNLayer, PositionwiseFeedForward) | ||
|
||
|
@@ -77,3 +79,9 @@ | |
'moe': MoEFFNLayer, | ||
'gated': GatedVariantsMLP | ||
} | ||
|
||
WENET_NORM_CLASSES = { | ||
'layer_norm': LayerNorm, | ||
'batch_norm': BatchNorm1d, | ||
'rms_norm': RMSNorm | ||
} |