-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATS][ ScaledSinusoidalEmbedding ] [ScaleNorm] [ReluSquared]
- Loading branch information
Kye
committed
Mar 2, 2024
1 parent
48991aa
commit 0d75ec6
Showing
12 changed files
with
129 additions
and
11 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
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,47 @@ | ||
import torch | ||
from torch import nn, Tensor, einsum | ||
|
||
from zeta.utils.main import divisible_by | ||
|
||
|
||
class ScaledSinusoidalEmbedding(nn.Module): | ||
def __init__(self, dim: int, theta: int = 10000): | ||
""" | ||
Initializes a ScaledSinusoidalEmbedding module. | ||
Args: | ||
dim (int): The dimension of the embedding. | ||
theta (int, optional): The scaling factor for the sinusoidal frequencies. Defaults to 10000. | ||
""" | ||
super().__init__() | ||
assert divisible_by(dim, 2) | ||
self.scale = nn.Parameter(torch.ones(1) * dim**-0.5) | ||
|
||
half_dim = dim // 2 | ||
freq_seq = torch.arange(half_dim).float() / half_dim | ||
inv_freq = theta**-freq_seq | ||
self.register_buffer("inv_freq", inv_freq, persistent=False) | ||
|
||
def forward(self, x: Tensor, pos=None, seq_start_pos=None): | ||
""" | ||
Forward pass of the ScaledSinusoidalEmbedding module. | ||
Args: | ||
x (Tensor): The input tensor. | ||
pos (Tensor, optional): The position tensor. Defaults to None. | ||
seq_start_pos (Tensor, optional): The starting position tensor for sequences. Defaults to None. | ||
Returns: | ||
Tensor: The embedded tensor. | ||
""" | ||
sq, device = x.shape[1], x.device | ||
|
||
if pos is not None: | ||
pos = torch.arange(sq, device=device) | ||
|
||
if seq_start_pos is not None: | ||
pos = pos - seq_start_pos[..., None] | ||
|
||
emb = einsum("i, j -> i j", pos, self.inv_freq) | ||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1) | ||
return emb * self.scale |
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,17 @@ | ||
from torch import nn | ||
import torch.nn.functional as F | ||
|
||
|
||
class ReluSquared(nn.Module): | ||
""" | ||
Applies the ReLU activation function and squares the output. | ||
Args: | ||
x (torch.Tensor): Input tensor. | ||
Returns: | ||
torch.Tensor: Output tensor after applying ReLU and squaring the result. | ||
""" | ||
|
||
def forward(self, x): | ||
return F.relu(x) ** 2 |
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,35 @@ | ||
import torch | ||
from torch import nn, Tensor | ||
|
||
|
||
class ScaleNorm(nn.Module): | ||
""" | ||
Applies scale normalization to the input tensor along the last dimension. | ||
Args: | ||
dim (int): The dimension of the input tensor. | ||
eps (float, optional): A small value added to the denominator for numerical stability. Default is 1e-5. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
dim: int, | ||
eps: float = 1e-5, | ||
): | ||
super().__init__() | ||
self.eps = eps | ||
|
||
self.g = nn.Parameter(torch.ones(1) * (dim**-0.5)) | ||
|
||
def forward(self, x: Tensor): | ||
""" | ||
Applies scale normalization to the input tensor. | ||
Args: | ||
x (Tensor): The input tensor. | ||
Returns: | ||
Tensor: The scale-normalized tensor. | ||
""" | ||
norm = torch.norm(x, dim=-1, keepdim=True) | ||
return x / norm.clamp(min=self.eps) + self.g |