Create modeling_thai_llm.py
Browse files- modeling_thai_llm.py +77 -0
modeling_thai_llm.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel
|
| 4 |
+
from transformers.modeling_outputs import CausalLMOutput
|
| 5 |
+
from .configuration_thai_llm import ThaiLLMConfig
|
| 6 |
+
|
| 7 |
+
class ThaiLLMBlock(nn.Module):
|
| 8 |
+
def __init__(self, config):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.attn = nn.MultiheadAttention(
|
| 11 |
+
config.hidden_size,
|
| 12 |
+
config.num_attention_heads,
|
| 13 |
+
dropout=config.attention_probs_dropout_prob,
|
| 14 |
+
batch_first=True
|
| 15 |
+
)
|
| 16 |
+
self.norm1 = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 17 |
+
self.mlp = nn.Sequential(
|
| 18 |
+
nn.Linear(config.hidden_size, config.intermediate_size),
|
| 19 |
+
nn.GELU(),
|
| 20 |
+
nn.Linear(config.intermediate_size, config.hidden_size),
|
| 21 |
+
nn.Dropout(config.hidden_dropout_prob)
|
| 22 |
+
)
|
| 23 |
+
self.norm2 = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 24 |
+
|
| 25 |
+
def forward(self, x, key_padding_mask=None):
|
| 26 |
+
residual = x
|
| 27 |
+
x, _ = self.attn(x, x, x, key_padding_mask=key_padding_mask)
|
| 28 |
+
x = self.norm1(x + residual)
|
| 29 |
+
|
| 30 |
+
residual = x
|
| 31 |
+
x = self.mlp(x)
|
| 32 |
+
x = self.norm2(x + residual)
|
| 33 |
+
|
| 34 |
+
return x
|
| 35 |
+
|
| 36 |
+
class ThaiLLMForCausalLM(PreTrainedModel):
|
| 37 |
+
config_class = ThaiLLMConfig
|
| 38 |
+
|
| 39 |
+
def __init__(self, config):
|
| 40 |
+
super().__init__(config)
|
| 41 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 42 |
+
self.embed_positions = nn.Embedding(config.max_position_embeddings, config.hidden_size)
|
| 43 |
+
self.layers = nn.ModuleList([ThaiLLMBlock(config) for _ in range(config.num_hidden_layers)])
|
| 44 |
+
self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 45 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 46 |
+
|
| 47 |
+
self._init_weights()
|
| 48 |
+
|
| 49 |
+
def _init_weights(self):
|
| 50 |
+
for name, module in self.named_modules():
|
| 51 |
+
if isinstance(module, (nn.Linear, nn.Embedding)):
|
| 52 |
+
nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
|
| 53 |
+
if isinstance(module, nn.Linear) and module.bias is not None:
|
| 54 |
+
nn.init.zeros_(module.bias)
|
| 55 |
+
|
| 56 |
+
def forward(self, input_ids, attention_mask=None, labels=None):
|
| 57 |
+
bsz, seq_len = input_ids.shape
|
| 58 |
+
position_ids = torch.arange(seq_len, dtype=torch.long, device=input_ids.device).unsqueeze(0).expand(bsz, -1)
|
| 59 |
+
|
| 60 |
+
hidden_states = self.embed_tokens(input_ids) + self.embed_positions(position_ids)
|
| 61 |
+
|
| 62 |
+
key_padding_mask = attention_mask == 0 if attention_mask is not None else None
|
| 63 |
+
|
| 64 |
+
for layer in self.layers:
|
| 65 |
+
hidden_states = layer(hidden_states, key_padding_mask=key_padding_mask)
|
| 66 |
+
|
| 67 |
+
hidden_states = self.norm(hidden_states)
|
| 68 |
+
logits = self.lm_head(hidden_states)
|
| 69 |
+
|
| 70 |
+
loss = None
|
| 71 |
+
if labels is not None:
|
| 72 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 73 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 74 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 75 |
+
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
|
| 76 |
+
|
| 77 |
+
return CausalLMOutput(logits=logits, loss=loss)
|