Create embedding.py
Browse files- modules/embedding.py +97 -0
modules/embedding.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 (authors: Feiteng Li)
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import math
|
| 16 |
+
|
| 17 |
+
import torch
|
| 18 |
+
import torch.nn as nn
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class TokenEmbedding(nn.Module):
|
| 22 |
+
def __init__(
|
| 23 |
+
self,
|
| 24 |
+
dim_model: int,
|
| 25 |
+
vocab_size: int,
|
| 26 |
+
dropout: float = 0.0,
|
| 27 |
+
):
|
| 28 |
+
super().__init__()
|
| 29 |
+
|
| 30 |
+
self.vocab_size = vocab_size
|
| 31 |
+
self.dim_model = dim_model
|
| 32 |
+
|
| 33 |
+
self.dropout = torch.nn.Dropout(p=dropout)
|
| 34 |
+
self.word_embeddings = nn.Embedding(self.vocab_size, self.dim_model)
|
| 35 |
+
|
| 36 |
+
@property
|
| 37 |
+
def weight(self) -> torch.Tensor:
|
| 38 |
+
return self.word_embeddings.weight
|
| 39 |
+
|
| 40 |
+
def embedding(self, index: int) -> torch.Tensor:
|
| 41 |
+
return self.word_embeddings.weight[index : index + 1]
|
| 42 |
+
|
| 43 |
+
def forward(self, x: torch.Tensor):
|
| 44 |
+
X = self.word_embeddings(x)
|
| 45 |
+
X = self.dropout(X)
|
| 46 |
+
|
| 47 |
+
return X
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class SinePositionalEmbedding(nn.Module):
|
| 51 |
+
def __init__(
|
| 52 |
+
self,
|
| 53 |
+
dim_model: int,
|
| 54 |
+
dropout: float = 0.0,
|
| 55 |
+
scale: bool = False,
|
| 56 |
+
alpha: bool = False,
|
| 57 |
+
):
|
| 58 |
+
super().__init__()
|
| 59 |
+
self.dim_model = dim_model
|
| 60 |
+
self.x_scale = math.sqrt(dim_model) if scale else 1.0
|
| 61 |
+
self.alpha = nn.Parameter(torch.ones(1), requires_grad=alpha)
|
| 62 |
+
self.dropout = torch.nn.Dropout(p=dropout)
|
| 63 |
+
|
| 64 |
+
self.reverse = False
|
| 65 |
+
self.pe = None
|
| 66 |
+
self.extend_pe(torch.tensor(0.0).expand(1, 4000))
|
| 67 |
+
|
| 68 |
+
def extend_pe(self, x):
|
| 69 |
+
"""Reset the positional encodings."""
|
| 70 |
+
if self.pe is not None:
|
| 71 |
+
if self.pe.size(1) >= x.size(1):
|
| 72 |
+
if self.pe.dtype != x.dtype or self.pe.device != x.device:
|
| 73 |
+
self.pe = self.pe.to(dtype=x.dtype, device=x.device)
|
| 74 |
+
return
|
| 75 |
+
pe = torch.zeros(x.size(1), self.dim_model)
|
| 76 |
+
if self.reverse:
|
| 77 |
+
position = torch.arange(
|
| 78 |
+
x.size(1) - 1, -1, -1.0, dtype=torch.float32
|
| 79 |
+
).unsqueeze(1)
|
| 80 |
+
else:
|
| 81 |
+
position = torch.arange(
|
| 82 |
+
0, x.size(1), dtype=torch.float32
|
| 83 |
+
).unsqueeze(1)
|
| 84 |
+
div_term = torch.exp(
|
| 85 |
+
torch.arange(0, self.dim_model, 2, dtype=torch.float32)
|
| 86 |
+
* -(math.log(10000.0) / self.dim_model)
|
| 87 |
+
)
|
| 88 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 89 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 90 |
+
pe = pe.unsqueeze(0)
|
| 91 |
+
self.pe = pe.to(device=x.device, dtype=x.dtype).detach()
|
| 92 |
+
|
| 93 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 94 |
+
self.extend_pe(x)
|
| 95 |
+
output = x.unsqueeze(-1) if x.ndim == 2 else x
|
| 96 |
+
output = output * self.x_scale + self.alpha * self.pe[:, : x.size(1)]
|
| 97 |
+
return self.dropout(output)
|