ereniko commited on
Commit
719bac4
·
verified ·
1 Parent(s): de5e55a

Upload folder using huggingface_hub

Browse files
__pycache__/modeling_ivme.cpython-312.pyc ADDED
Binary file (18.6 kB). View file
 
config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "IvmeConversateV2HF"
4
+ ],
5
+ "model_type": "ivme",
6
+ "vocab_size": 16000,
7
+ "context_len": 1024,
8
+ "tie_word_embeddings": true,
9
+ "hidden_dim": 384,
10
+ "n_layers": 10,
11
+ "n_heads": 6,
12
+ "dropout": 0.0,
13
+ "ffn_mult": 1.0,
14
+ "norm_eps": 1e-05,
15
+ "rope_theta": 10000.0,
16
+ "head_dim": 64,
17
+ "auto_map": {
18
+ "AutoConfig": "modeling_ivme.IvmeConfig",
19
+ "AutoModelForCausalLM": "modeling_ivme.IvmeConversateV2HF"
20
+ },
21
+ "transformers_version": "4.41.0"
22
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5e5982f71cff3087f7581bdd94ada71e8f444dc0ab0499096e4e1b33733fa645
3
+ size 95396736
modeling_ivme.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from transformers import PretrainedConfig, PreTrainedModel
4
+ from transformers.modeling_outputs import CausalLMOutputWithPast
5
+
6
+ # ==========================================
7
+ # 1. RESMİ HUGGING FACE CONFIG SIFINFI
8
+ # ==========================================
9
+ class IvmeConfig(PretrainedConfig):
10
+ model_type = "ivme"
11
+
12
+ def __init__(
13
+ self,
14
+ vocab_size=16000,
15
+ context_len=1024,
16
+ tie_word_embeddings=True,
17
+ hidden_dim=384,
18
+ n_layers=10,
19
+ n_heads=6,
20
+ dropout=0.0,
21
+ ffn_mult=1.0,
22
+ norm_eps=1e-5,
23
+ rope_theta=10000.0,
24
+ head_dim=64,
25
+ **kwargs
26
+ ):
27
+ # Hugging Face API'sinin 'from_dict' motoru için kwargs paslanmalı ve tied özelliği üst sınıfa bildirilmeli
28
+ super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
29
+ self.vocab_size = vocab_size
30
+ self.context_len = context_len
31
+ self.hidden_dim = hidden_dim
32
+ self.n_layers = n_layers
33
+ self.n_heads = n_heads
34
+ self.dropout = dropout
35
+ self.ffn_mult = ffn_mult
36
+ self.norm_eps = norm_eps
37
+ self.rope_theta = rope_theta
38
+ self.head_dim = head_dim
39
+
40
+ # ==========================================
41
+ # 2. SİZİN MODELİNİZİN ORİJİNAL MATEMATİKSEL KATMANLARI
42
+ # ==========================================
43
+ class RMSNorm(nn.Module):
44
+ def __init__(self, dim: int, eps: float = 1e-5):
45
+ super().__init__()
46
+ self.eps = eps
47
+ self.weight = nn.Parameter(torch.ones(dim))
48
+ def forward(self, x):
49
+ pow_x = x.pow(2).mean(-1, keepdim=True)
50
+ return x * torch.rsqrt(pow_x + self.eps) * self.weight
51
+
52
+ def precompute_rope_freqs(dim: int, max_seq_len: int, theta: float = 10000.0):
53
+ inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2).float() / dim))
54
+ t = torch.arange(max_seq_len, dtype=torch.float32)
55
+ freqs = torch.outer(t, inv_freq)
56
+ return torch.polar(torch.ones_like(freqs), freqs)
57
+
58
+ class CausalSelfAttention(nn.Module):
59
+ def __init__(self, hidden_dim: int, n_heads: int, dropout: float = 0.0):
60
+ super().__init__()
61
+ self.n_heads = n_heads
62
+ self.head_dim = hidden_dim // n_heads
63
+ self.wq = nn.Linear(hidden_dim, hidden_dim, bias=False)
64
+ self.wk = nn.Linear(hidden_dim, hidden_dim, bias=False)
65
+ self.wv = nn.Linear(hidden_dim, hidden_dim, bias=False)
66
+ self.wo = nn.Linear(hidden_dim, hidden_dim, bias=False)
67
+ self.dropout = nn.Dropout(dropout) if dropout > 0 else nn.Identity()
68
+
69
+ def forward(self, x, rope_freqs):
70
+ B, T, C = x.shape
71
+ q, k, v = self.wq(x), self.wk(x), self.wv(x)
72
+ q = q.view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
73
+ k = k.view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
74
+ v = v.view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
75
+
76
+ # RoPE Uygulaması
77
+ q_complex = torch.view_as_complex(q.float().reshape(*q.shape[:-1], -1, 2))
78
+ k_complex = torch.view_as_complex(k.float().reshape(*k.shape[:-1], -1, 2))
79
+ freqs = rope_freqs[:T].view(1, 1, T, -1)
80
+ q = torch.view_as_real(q_complex * freqs).flatten(3).type_as(x)
81
+ k = torch.view_as_real(k_complex * freqs).flatten(3).type_as(x)
82
+
83
+ # Standart Attention
84
+ scores = torch.matmul(q, k.transpose(-2, -1)) / (self.head_dim ** 0.5)
85
+ mask = torch.full((T, T), float("-inf"), device=x.device).triu(1)
86
+ scores = scores + mask
87
+ probs = torch.softmax(scores, dim=-1)
88
+ probs = self.dropout(probs)
89
+
90
+ output = torch.matmul(probs, v)
91
+ output = output.transpose(1, 2).contiguous().view(B, T, C)
92
+ return self.wo(output)
93
+
94
+ class SwiGLU(nn.Module):
95
+ def __init__(self, hidden_dim: int, ffn_mult: float = 1.0):
96
+ super().__init__()
97
+ hidden_features = int(2 * hidden_dim * 4 / 3)
98
+ hidden_features = int(ffn_mult * hidden_features)
99
+ self.w1 = nn.Linear(hidden_dim, hidden_features, bias=False)
100
+ self.w2 = nn.Linear(hidden_features, hidden_dim, bias=False)
101
+ self.w3 = nn.Linear(hidden_dim, hidden_features, bias=False)
102
+ def forward(self, x):
103
+ return self.w2(F.silu(self.w1(x)) * self.w3(x))
104
+
105
+ import torch.nn.functional as F
106
+
107
+ class TransformerBlock(nn.Module):
108
+ def __init__(self, cfg):
109
+ super().__init__()
110
+ self.attn_norm = RMSNorm(cfg.hidden_dim, eps=cfg.norm_eps)
111
+ self.attn = CausalSelfAttention(cfg.hidden_dim, cfg.n_heads, cfg.dropout)
112
+ self.ffn_norm = RMSNorm(cfg.hidden_dim, eps=cfg.norm_eps)
113
+ self.ffn = SwiGLU(cfg.hidden_dim, cfg.ffn_mult)
114
+ def forward(self, x, rope_freqs):
115
+ x = x + self.attn(self.attn_norm(x), rope_freqs)
116
+ x = x + self.ffn(self.ffn_norm(x))
117
+ return x
118
+
119
+ # ==========================================
120
+ # 3. RESMİ HUGGING FACE CAUSAL LM MODEL SINIFI
121
+ # ==========================================
122
+ class IvmeConversateV2HF(PreTrainedModel):
123
+ config_class = IvmeConfig
124
+ base_model_prefix = "model"
125
+
126
+ def __init__(self, config):
127
+ super().__init__(config)
128
+ self.config = config
129
+
130
+ # Mimarinin Ayağa Kaldırılması
131
+ self.tok_embed = nn.Embedding(config.vocab_size, config.hidden_dim)
132
+ self.blocks = nn.ModuleList([TransformerBlock(config) for _ in range(config.n_layers)])
133
+ self.final_norm = RMSNorm(config.hidden_dim, eps=config.norm_eps)
134
+ self.lm_head = nn.Linear(config.hidden_dim, config.vocab_size, bias=False)
135
+
136
+ # Ağırlık bağlama kuralı
137
+ if config.tie_word_embeddings:
138
+ self.lm_head.weight = self.tok_embed.weight
139
+
140
+ # RoPE Hazırlığı
141
+ rope_freqs = precompute_rope_freqs(config.hidden_dim // config.n_heads, config.context_len, config.rope_theta)
142
+ self.register_buffer("rope_freqs", rope_freqs, persistent=False)
143
+
144
+ self.post_init() # Ağırlıkları otomatik başlatan resmi HF metodu
145
+
146
+ def forward(self, input_ids=None, labels=None, **kwargs):
147
+ B, T = input_ids.shape
148
+
149
+ x = self.tok_embed(input_ids)
150
+ for block in self.blocks:
151
+ x = block(x, self.rope_freqs)
152
+ x = self.final_norm(x)
153
+ logits = self.lm_head(x)
154
+
155
+ loss = None
156
+ if labels is not None:
157
+ loss = F.cross_entropy(
158
+ logits.view(-1, logits.size(-1)),
159
+ labels.view(-1),
160
+ ignore_index=-1,
161
+ )
162
+
163
+ # HF API standartlarına %100 uyum için resmi nesne çıktısı döndürüyoruz
164
+ return CausalLMOutputWithPast(loss=loss, logits=logits)
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_eos_token": false,
4
+ "model_max_length": 1024,
5
+ "tokenizer_class": "PreTrainedTokenizerFast",
6
+ "clean_up_tokenization_spaces": true
7
+ }