Commit
·
de1fe87
1
Parent(s):
19b8ed8
modeling
Browse files- modeling_gpt_custom.py +33 -0
modeling_gpt_custom.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedModel, PretrainedConfig, AutoModel, AutoConfig
|
| 2 |
+
import torch
|
| 3 |
+
from Naive_gpt.model import GPTLanguageModel
|
| 4 |
+
|
| 5 |
+
class GPTConfig(PretrainedConfig):
|
| 6 |
+
model_type = "gpt_custom"
|
| 7 |
+
|
| 8 |
+
def __init__(self, vocab_size=20000, **kwargs):
|
| 9 |
+
super().__init__(**kwargs)
|
| 10 |
+
self.vocab_size = vocab_size
|
| 11 |
+
self.block_size = 1024
|
| 12 |
+
self.n_embd = 768
|
| 13 |
+
self.n_head = 12
|
| 14 |
+
self.n_layer = 12
|
| 15 |
+
self.dropout = 0.2
|
| 16 |
+
|
| 17 |
+
class GPTModelHF(PreTrainedModel):
|
| 18 |
+
config_class = GPTConfig
|
| 19 |
+
|
| 20 |
+
def __init__(self, config):
|
| 21 |
+
super().__init__(config)
|
| 22 |
+
self.model = GPTLanguageModel(config.vocab_size, config)
|
| 23 |
+
self.config = config
|
| 24 |
+
|
| 25 |
+
def forward(self, x):
|
| 26 |
+
return self.model(x)
|
| 27 |
+
|
| 28 |
+
def generate(self, idx, max_new_tokens):
|
| 29 |
+
return self.model.generate(idx, max_new_tokens)
|
| 30 |
+
|
| 31 |
+
# Register the model (this will run when the file is imported)
|
| 32 |
+
AutoConfig.register("gpt_custom", GPTConfig)
|
| 33 |
+
AutoModel.register(GPTConfig, GPTModelHF)
|