Create modeling_arabic-gpt.py
Browse files- modeling_arabic-gpt.py +130 -0
modeling_arabic-gpt.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
import regex as re
|
| 6 |
+
import collections
|
| 7 |
+
import os
|
| 8 |
+
import random
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
from transformers import PreTrainedModel
|
| 11 |
+
from transformers import PretrainedConfig
|
| 12 |
+
|
| 13 |
+
from transformers import PretrainedConfig
|
| 14 |
+
|
| 15 |
+
class ArabicGPTConfig(PretrainedConfig):
|
| 16 |
+
model_type = "arabic-gpt"
|
| 17 |
+
|
| 18 |
+
def __init__(self,
|
| 19 |
+
vocab_size=32000,
|
| 20 |
+
max_seq_len=1024,
|
| 21 |
+
embed_dim=768,
|
| 22 |
+
num_heads=12,
|
| 23 |
+
num_layers=12,
|
| 24 |
+
ff_dim=3072,
|
| 25 |
+
dropout=0.1,
|
| 26 |
+
**kwargs):
|
| 27 |
+
super().__init__(**kwargs)
|
| 28 |
+
self.vocab_size = vocab_size
|
| 29 |
+
self.max_seq_len = max_seq_len
|
| 30 |
+
self.embed_dim = embed_dim
|
| 31 |
+
self.num_heads = num_heads
|
| 32 |
+
self.num_layers = num_layers
|
| 33 |
+
self.ff_dim = ff_dim
|
| 34 |
+
self.dropout = dropout
|
| 35 |
+
self.tie_word_embeddings = True
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
import torch
|
| 39 |
+
import torch.nn as nn
|
| 40 |
+
from transformers import PreTrainedModel
|
| 41 |
+
|
| 42 |
+
class ArabicGPTModel(PreTrainedModel):
|
| 43 |
+
config_class = ArabicGPTConfig
|
| 44 |
+
|
| 45 |
+
def __init__(self, config: ArabicGPTConfig):
|
| 46 |
+
super().__init__(config)
|
| 47 |
+
self.model = ArabicGPT(
|
| 48 |
+
vocab_size=config.vocab_size,
|
| 49 |
+
max_seq_len=config.max_seq_len,
|
| 50 |
+
embed_dim=config.embed_dim,
|
| 51 |
+
num_heads=config.num_heads,
|
| 52 |
+
num_layers=config.num_layers,
|
| 53 |
+
ff_dim=config.ff_dim,
|
| 54 |
+
dropout=config.dropout,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
def forward(self, x):
|
| 58 |
+
return self.model(x)
|
| 59 |
+
|
| 60 |
+
def generate(self, prompt_ids, max_new_tokens, temperature=1.0, top_k=50, top_p=0.9):
|
| 61 |
+
return self.model.generate(prompt_ids, max_new_tokens, temperature=1.0, top_k=50, top_p=0.9)
|
| 62 |
+
|
| 63 |
+
def get_input_embeddings(self):
|
| 64 |
+
return self.model.token_embedding
|
| 65 |
+
|
| 66 |
+
def set_input_embeddings(self, new_embeddings):
|
| 67 |
+
self.model.token_embedding = new_embeddings
|
| 68 |
+
|
| 69 |
+
def get_output_embeddings(self):
|
| 70 |
+
return self.model.lm_head
|
| 71 |
+
|
| 72 |
+
def tie_weights(self):
|
| 73 |
+
self.model.lm_head.weight = self.model.token_embedding.weight
|
| 74 |
+
|
| 75 |
+
class ArabicGPTConfig(PretrainedConfig):
|
| 76 |
+
model_type = "arabic-gpt"
|
| 77 |
+
|
| 78 |
+
def __init__(self,
|
| 79 |
+
vocab_size=32000,
|
| 80 |
+
max_seq_len=1024,
|
| 81 |
+
embed_dim=768,
|
| 82 |
+
num_heads=12,
|
| 83 |
+
num_layers=12,
|
| 84 |
+
ff_dim=3072,
|
| 85 |
+
dropout=0.1,
|
| 86 |
+
**kwargs):
|
| 87 |
+
super().__init__(**kwargs)
|
| 88 |
+
self.vocab_size = vocab_size
|
| 89 |
+
self.max_seq_len = max_seq_len
|
| 90 |
+
self.embed_dim = embed_dim
|
| 91 |
+
self.num_heads = num_heads
|
| 92 |
+
self.num_layers = num_layers
|
| 93 |
+
self.ff_dim = ff_dim
|
| 94 |
+
self.dropout = dropout
|
| 95 |
+
self.tie_word_embeddings = True
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class ArabicGPTModel(PreTrainedModel):
|
| 99 |
+
config_class = ArabicGPTConfig
|
| 100 |
+
|
| 101 |
+
def __init__(self, config: ArabicGPTConfig):
|
| 102 |
+
super().__init__(config)
|
| 103 |
+
self.model = ArabicGPT(
|
| 104 |
+
vocab_size=config.vocab_size,
|
| 105 |
+
max_seq_len=config.max_seq_len,
|
| 106 |
+
embed_dim=config.embed_dim,
|
| 107 |
+
num_heads=config.num_heads,
|
| 108 |
+
num_layers=config.num_layers,
|
| 109 |
+
ff_dim=config.ff_dim,
|
| 110 |
+
dropout=config.dropout,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
def forward(self, x):
|
| 114 |
+
return self.model(x)
|
| 115 |
+
|
| 116 |
+
def generate(self, prompt_ids, max_new_tokens, temperature=1.0, top_k=50, top_p=0.9):
|
| 117 |
+
return self.model.generate(prompt_ids, max_new_tokens, temperature=1.0, top_k=50, top_p=0.9)
|
| 118 |
+
|
| 119 |
+
def get_input_embeddings(self):
|
| 120 |
+
return self.model.token_embedding
|
| 121 |
+
|
| 122 |
+
def set_input_embeddings(self, new_embeddings):
|
| 123 |
+
self.model.token_embedding = new_embeddings
|
| 124 |
+
|
| 125 |
+
def get_output_embeddings(self):
|
| 126 |
+
return self.model.lm_head
|
| 127 |
+
|
| 128 |
+
def tie_weights(self):
|
| 129 |
+
self.model.lm_head.weight = self.model.token_embedding.weight
|
| 130 |
+
|