Update modeling_pypilot.py
Browse files- modeling_pypilot.py +26 -9
modeling_pypilot.py
CHANGED
|
@@ -1,14 +1,31 @@
|
|
| 1 |
-
# PyPilot
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
|
|
|
| 4 |
|
| 5 |
-
class
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def forward(self, input_ids):
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# PyPilot Model Architecture
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 5 |
|
| 6 |
+
class PyPilotConfig(PretrainedConfig):
|
| 7 |
+
model_type = "pypilot"
|
| 8 |
+
|
| 9 |
+
def __init__(self, vocab_size=50000, hidden_size=768, num_layers=12, **kwargs):
|
| 10 |
+
self.vocab_size = vocab_size
|
| 11 |
+
self.hidden_size = hidden_size
|
| 12 |
+
self.num_layers = num_layers
|
| 13 |
+
super().__init__(**kwargs)
|
| 14 |
+
|
| 15 |
+
class PyPilotModel(PreTrainedModel):
|
| 16 |
+
config_class = PyPilotConfig
|
| 17 |
|
| 18 |
+
def __init__(self, config):
|
| 19 |
+
super().__init__(config)
|
| 20 |
+
self.embedding = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 21 |
+
self.transformer_blocks = nn.ModuleList([
|
| 22 |
+
nn.TransformerEncoderLayer(config.hidden_size, 8)
|
| 23 |
+
for _ in range(config.num_layers)
|
| 24 |
+
])
|
| 25 |
+
self.output_layer = nn.Linear(config.hidden_size, config.vocab_size)
|
| 26 |
+
|
| 27 |
def forward(self, input_ids):
|
| 28 |
+
x = self.embedding(input_ids)
|
| 29 |
+
for block in self.transformer_blocks:
|
| 30 |
+
x = block(x)
|
| 31 |
+
return self.output_layer(x)
|