Kayra-Stable: Fine-tuned with 21K Turkish QA dataset
Browse files- config.json +25 -0
- configuration_kayra.py +46 -0
- generation_config.json +9 -0
- modeling_kayra.py +136 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +30 -0
- tokenizer.json +0 -0
- tokenizer_config.json +44 -0
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"KayraForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.1,
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoConfig": "configuration_kayra.KayraConfig",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_kayra.KayraForCausalLM"
|
| 9 |
+
},
|
| 10 |
+
"bos_token_id": 2,
|
| 11 |
+
"dtype": "float32",
|
| 12 |
+
"eos_token_id": 3,
|
| 13 |
+
"hidden_dropout": 0.1,
|
| 14 |
+
"hidden_size": 640,
|
| 15 |
+
"initializer_range": 0.02,
|
| 16 |
+
"intermediate_size": 2560,
|
| 17 |
+
"max_position_embeddings": 512,
|
| 18 |
+
"model_type": "kayra",
|
| 19 |
+
"num_attention_heads": 10,
|
| 20 |
+
"num_hidden_layers": 10,
|
| 21 |
+
"pad_token_id": 3,
|
| 22 |
+
"transformers_version": "4.57.3",
|
| 23 |
+
"use_cache": true,
|
| 24 |
+
"vocab_size": 32000
|
| 25 |
+
}
|
configuration_kayra.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Kayra Configuration
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from transformers import PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class KayraConfig(PretrainedConfig):
|
| 9 |
+
model_type = "kayra"
|
| 10 |
+
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
vocab_size=32000,
|
| 14 |
+
hidden_size=640,
|
| 15 |
+
num_hidden_layers=10,
|
| 16 |
+
num_attention_heads=10,
|
| 17 |
+
intermediate_size=2560,
|
| 18 |
+
hidden_dropout=0.1,
|
| 19 |
+
attention_dropout=0.1,
|
| 20 |
+
max_position_embeddings=512,
|
| 21 |
+
initializer_range=0.02,
|
| 22 |
+
use_cache=True,
|
| 23 |
+
pad_token_id=0,
|
| 24 |
+
bos_token_id=2,
|
| 25 |
+
eos_token_id=3,
|
| 26 |
+
tie_word_embeddings=True,
|
| 27 |
+
**kwargs
|
| 28 |
+
):
|
| 29 |
+
self.vocab_size = vocab_size
|
| 30 |
+
self.hidden_size = hidden_size
|
| 31 |
+
self.num_hidden_layers = num_hidden_layers
|
| 32 |
+
self.num_attention_heads = num_attention_heads
|
| 33 |
+
self.intermediate_size = intermediate_size
|
| 34 |
+
self.hidden_dropout = hidden_dropout
|
| 35 |
+
self.attention_dropout = attention_dropout
|
| 36 |
+
self.max_position_embeddings = max_position_embeddings
|
| 37 |
+
self.initializer_range = initializer_range
|
| 38 |
+
self.use_cache = use_cache
|
| 39 |
+
|
| 40 |
+
super().__init__(
|
| 41 |
+
pad_token_id=pad_token_id,
|
| 42 |
+
bos_token_id=bos_token_id,
|
| 43 |
+
eos_token_id=eos_token_id,
|
| 44 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 45 |
+
**kwargs
|
| 46 |
+
)
|
generation_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 2,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
3
|
| 6 |
+
],
|
| 7 |
+
"pad_token_id": 3,
|
| 8 |
+
"transformers_version": "4.57.3"
|
| 9 |
+
}
|
modeling_kayra.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Kayra Turkish GPT Model
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
from transformers import PreTrainedModel
|
| 10 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 11 |
+
from .configuration_kayra import KayraConfig
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class RMSNorm(nn.Module):
|
| 15 |
+
def __init__(self, dim, eps=1e-6):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.eps = eps
|
| 18 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 19 |
+
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
rms = torch.sqrt(torch.mean(x ** 2, dim=-1, keepdim=True) + self.eps)
|
| 22 |
+
return x / rms * self.weight
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class Attention(nn.Module):
|
| 26 |
+
def __init__(self, config):
|
| 27 |
+
super().__init__()
|
| 28 |
+
self.n_heads = config.num_attention_heads
|
| 29 |
+
self.head_dim = config.hidden_size // config.num_attention_heads
|
| 30 |
+
|
| 31 |
+
self.qkv = nn.Linear(config.hidden_size, 3 * config.hidden_size, bias=False)
|
| 32 |
+
self.proj = nn.Linear(config.hidden_size, config.hidden_size, bias=False)
|
| 33 |
+
self.dropout = nn.Dropout(config.hidden_dropout)
|
| 34 |
+
|
| 35 |
+
mask = torch.triu(torch.ones(config.max_position_embeddings, config.max_position_embeddings), diagonal=1).bool()
|
| 36 |
+
self.register_buffer("mask", mask)
|
| 37 |
+
|
| 38 |
+
def forward(self, x):
|
| 39 |
+
B, T, C = x.shape
|
| 40 |
+
|
| 41 |
+
qkv = self.qkv(x).reshape(B, T, 3, self.n_heads, self.head_dim)
|
| 42 |
+
q, k, v = qkv.permute(2, 0, 3, 1, 4)
|
| 43 |
+
|
| 44 |
+
attn = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(self.head_dim))
|
| 45 |
+
attn = attn.masked_fill(self.mask[:T, :T], float('-inf'))
|
| 46 |
+
attn = F.softmax(attn, dim=-1)
|
| 47 |
+
attn = self.dropout(attn)
|
| 48 |
+
|
| 49 |
+
out = (attn @ v).transpose(1, 2).reshape(B, T, C)
|
| 50 |
+
return self.proj(out)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class FeedForward(nn.Module):
|
| 54 |
+
def __init__(self, config):
|
| 55 |
+
super().__init__()
|
| 56 |
+
self.w1 = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
|
| 57 |
+
self.w2 = nn.Linear(config.intermediate_size, config.hidden_size, bias=False)
|
| 58 |
+
self.w3 = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
|
| 59 |
+
self.dropout = nn.Dropout(config.hidden_dropout)
|
| 60 |
+
|
| 61 |
+
def forward(self, x):
|
| 62 |
+
return self.dropout(self.w2(F.silu(self.w1(x)) * self.w3(x)))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class Block(nn.Module):
|
| 66 |
+
def __init__(self, config):
|
| 67 |
+
super().__init__()
|
| 68 |
+
self.norm1 = RMSNorm(config.hidden_size)
|
| 69 |
+
self.attn = Attention(config)
|
| 70 |
+
self.norm2 = RMSNorm(config.hidden_size)
|
| 71 |
+
self.ff = FeedForward(config)
|
| 72 |
+
|
| 73 |
+
def forward(self, x):
|
| 74 |
+
x = x + self.attn(self.norm1(x))
|
| 75 |
+
x = x + self.ff(self.norm2(x))
|
| 76 |
+
return x
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
class KayraPreTrainedModel(PreTrainedModel):
|
| 80 |
+
config_class = KayraConfig
|
| 81 |
+
base_model_prefix = "model"
|
| 82 |
+
supports_gradient_checkpointing = True
|
| 83 |
+
|
| 84 |
+
def _init_weights(self, module):
|
| 85 |
+
if isinstance(module, nn.Linear):
|
| 86 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 87 |
+
elif isinstance(module, nn.Embedding):
|
| 88 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class KayraForCausalLM(KayraPreTrainedModel):
|
| 92 |
+
def __init__(self, config):
|
| 93 |
+
super().__init__(config)
|
| 94 |
+
self.config = config
|
| 95 |
+
|
| 96 |
+
self.tok_emb = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 97 |
+
self.pos_emb = nn.Embedding(config.max_position_embeddings, config.hidden_size)
|
| 98 |
+
self.drop = nn.Dropout(config.hidden_dropout)
|
| 99 |
+
|
| 100 |
+
self.blocks = nn.ModuleList([Block(config) for _ in range(config.num_hidden_layers)])
|
| 101 |
+
self.norm = RMSNorm(config.hidden_size)
|
| 102 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 103 |
+
|
| 104 |
+
if config.tie_word_embeddings:
|
| 105 |
+
self.lm_head.weight = self.tok_emb.weight
|
| 106 |
+
|
| 107 |
+
self.post_init()
|
| 108 |
+
|
| 109 |
+
def get_input_embeddings(self):
|
| 110 |
+
return self.tok_emb
|
| 111 |
+
|
| 112 |
+
def set_input_embeddings(self, value):
|
| 113 |
+
self.tok_emb = value
|
| 114 |
+
|
| 115 |
+
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
|
| 116 |
+
B, T = input_ids.shape
|
| 117 |
+
|
| 118 |
+
pos = torch.arange(T, device=input_ids.device)
|
| 119 |
+
x = self.drop(self.tok_emb(input_ids) + self.pos_emb(pos))
|
| 120 |
+
|
| 121 |
+
for block in self.blocks:
|
| 122 |
+
x = block(x)
|
| 123 |
+
|
| 124 |
+
x = self.norm(x)
|
| 125 |
+
logits = self.lm_head(x)
|
| 126 |
+
|
| 127 |
+
loss = None
|
| 128 |
+
if labels is not None:
|
| 129 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 130 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 131 |
+
loss = F.cross_entropy(shift_logits.view(-1, self.config.vocab_size), shift_labels.view(-1))
|
| 132 |
+
|
| 133 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits)
|
| 134 |
+
|
| 135 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 136 |
+
return {"input_ids": input_ids}
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c16a92b3b5f4483bf009f380f64d102490fb370ee19193f77bdfe1672de471c6
|
| 3 |
+
size 348075968
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<|startoftext|>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "<|endoftext|>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "<|endoftext|>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"unk_token": {
|
| 24 |
+
"content": "<|unk|>",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
}
|
| 30 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "<|pad|>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "<|unk|>",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "<|startoftext|>",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "<|endoftext|>",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
}
|
| 35 |
+
},
|
| 36 |
+
"bos_token": "<|startoftext|>",
|
| 37 |
+
"clean_up_tokenization_spaces": true,
|
| 38 |
+
"eos_token": "<|endoftext|>",
|
| 39 |
+
"extra_special_tokens": {},
|
| 40 |
+
"model_max_length": 512,
|
| 41 |
+
"pad_token": "<|endoftext|>",
|
| 42 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 43 |
+
"unk_token": "<|unk|>"
|
| 44 |
+
}
|