aethertp commited on
Commit
4db2d6b
·
verified ·
1 Parent(s): 96af0f8

Initial verified release: safetensors + modeling + tokenizer (4K YaRN)

Browse files
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "picolm_v3",
3
+ "auto_map": {
4
+ "AutoConfig": "configuration_picolm_v3.PicoLMV3Config",
5
+ "AutoModelForCausalLM": "modeling_picolm_v3.PicoLMV3ForCausalLM"
6
+ },
7
+ "vocab_size": 24576,
8
+ "emb_dim": 128,
9
+ "dim": 576,
10
+ "n_layers": 21,
11
+ "layer_repeat": 2,
12
+ "n_heads": 9,
13
+ "n_kv_heads": 3,
14
+ "head_dim": 64,
15
+ "intermediate_dim": 1664,
16
+ "max_seq_len": 2048,
17
+ "orig_seq_len": 2048,
18
+ "norm_eps": 1e-05,
19
+ "rope_theta": 10000.0,
20
+ "bos_token_id": 0,
21
+ "eos_token_id": 2,
22
+ "pad_token_id": 3,
23
+ "tie_word_embeddings": true,
24
+ "architectures": [
25
+ "PicoLMV3ForCausalLM"
26
+ ]
27
+ }
configuration_picolm_v3.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+ class PicoLMV3Config(PretrainedConfig):
4
+ model_type = "picolm_v3"
5
+
6
+ def __init__(
7
+ self,
8
+ vocab_size: int = 24576,
9
+ emb_dim: int = 128,
10
+ dim: int = 576,
11
+ n_layers: int = 21,
12
+ layer_repeat: int = 2,
13
+ n_heads: int = 9,
14
+ n_kv_heads: int = 3,
15
+ head_dim: int = 64,
16
+ intermediate_dim: int = 1664,
17
+ max_seq_len: int = 2048,
18
+ orig_seq_len: int = 2048,
19
+ norm_eps: float = 1e-5,
20
+ rope_theta: float = 10000.0,
21
+ bos_token_id: int = 0,
22
+ eos_token_id: int = 2,
23
+ pad_token_id: int = 3,
24
+ tie_word_embeddings: bool = True,
25
+ **kwargs,
26
+ ):
27
+ self.vocab_size = vocab_size
28
+ self.emb_dim = emb_dim
29
+ self.dim = dim
30
+ self.n_layers = n_layers
31
+ self.layer_repeat = layer_repeat
32
+ self.n_heads = n_heads
33
+ self.n_kv_heads = n_kv_heads
34
+ self.head_dim = head_dim
35
+ self.intermediate_dim = intermediate_dim
36
+ self.max_seq_len = max_seq_len
37
+ self.orig_seq_len = orig_seq_len
38
+ self.norm_eps = norm_eps
39
+ self.rope_theta = rope_theta
40
+ super().__init__(
41
+ bos_token_id=bos_token_id,
42
+ eos_token_id=eos_token_id,
43
+ pad_token_id=pad_token_id,
44
+ tie_word_embeddings=tie_word_embeddings,
45
+ **kwargs,
46
+ )
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b64eafc3dbb91f28a139f86bdf153890055207f5cce80dd7023a0950510f9bbf
3
+ size 328962560
modeling_picolm_v3.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ from transformers import PreTrainedModel, GenerationMixin
6
+ from transformers.modeling_outputs import CausalLMOutputWithPast
7
+ from .configuration_picolm_v3 import PicoLMV3Config
8
+
9
+ class RMSNorm(nn.Module):
10
+ def __init__(self, dim: int, eps: float = 1e-5):
11
+ super().__init__()
12
+ self.eps = eps
13
+ self.weight = nn.Parameter(torch.ones(dim))
14
+ def forward(self, x):
15
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.weight
16
+
17
+ def precompute_rope_cis_yarn(head_dim=64, max_seq_len=4096, orig_len=2048, theta=10000.0):
18
+ half = head_dim // 2
19
+ scale = max_seq_len / orig_len
20
+ idx = torch.arange(half, dtype=torch.float64)
21
+ inv_freq = theta ** (-2.0 * idx / head_dim)
22
+
23
+ if scale > 1.0:
24
+ def corr_dim(n_rot):
25
+ return head_dim * math.log(orig_len / (n_rot * 2 * math.pi)) / (2 * math.log(theta))
26
+ low = max(math.floor(corr_dim(32.0)), 0)
27
+ high = min(math.ceil(corr_dim(1.0)), half - 1)
28
+ gamma = ((idx - low) / (high - low)).clamp(0.0, 1.0)
29
+ inv_freq = inv_freq * (1.0 - gamma) + (inv_freq / scale) * gamma
30
+ attn_scale = 0.1 * math.log(scale) + 1.0
31
+ else:
32
+ attn_scale = 1.0
33
+
34
+ t = torch.arange(max_seq_len, dtype=torch.float64)
35
+ angles = torch.outer(t, inv_freq)
36
+ cos = (torch.cos(angles) * attn_scale).to(torch.float32)
37
+ sin = (torch.sin(angles) * attn_scale).to(torch.float32)
38
+ return cos, sin
39
+
40
+ def apply_rope(x, cos, sin):
41
+ B, H, S, D = x.shape
42
+ cos = cos[:S, :].to(x.device).unsqueeze(0).unsqueeze(0)
43
+ sin = sin[:S, :].to(x.device).unsqueeze(0).unsqueeze(0)
44
+ x1, x2 = x[..., : D // 2], x[..., D // 2 :]
45
+ return torch.cat([x1 * cos - x2 * sin, x1 * sin + x2 * cos], dim=-1)
46
+
47
+ class Attention(nn.Module):
48
+ def __init__(self, config):
49
+ super().__init__()
50
+ self.head_dim = config.head_dim
51
+ self.n_heads = config.n_heads
52
+ self.n_kv_heads = config.n_kv_heads
53
+ self.num_reps = config.n_heads // config.n_kv_heads
54
+ self.q_proj = nn.Linear(config.dim, config.n_heads * config.head_dim, bias=False)
55
+ self.k_proj = nn.Linear(config.dim, config.n_kv_heads * config.head_dim, bias=False)
56
+ self.v_proj = nn.Linear(config.dim, config.n_kv_heads * config.head_dim, bias=False)
57
+ self.out_proj = nn.Linear(config.n_heads * config.head_dim, config.dim, bias=False)
58
+ self.q_norm = RMSNorm(config.head_dim, eps=config.norm_eps)
59
+ self.k_norm = RMSNorm(config.head_dim, eps=config.norm_eps)
60
+
61
+ def forward(self, x, cos, sin):
62
+ B, S, C = x.shape
63
+ q = apply_rope(self.q_norm(self.q_proj(x).view(B, S, self.n_heads, self.head_dim).transpose(1, 2)), cos, sin)
64
+ k = apply_rope(self.k_norm(self.k_proj(x).view(B, S, self.n_kv_heads, self.head_dim).transpose(1, 2)), cos, sin)
65
+ v = self.v_proj(x).view(B, S, self.n_kv_heads, self.head_dim).transpose(1, 2)
66
+ if self.num_reps > 1:
67
+ k = k[:, :, None, :, :].expand(B, self.n_kv_heads, self.num_reps, S, self.head_dim).reshape(B, self.n_heads, S, self.head_dim)
68
+ v = v[:, :, None, :, :].expand(B, self.n_kv_heads, self.num_reps, S, self.head_dim).reshape(B, self.n_heads, S, self.head_dim)
69
+ attn_out = F.scaled_dot_product_attention(q, k, v, is_causal=True)
70
+ return self.out_proj(attn_out.transpose(1, 2).contiguous().view(B, S, C))
71
+
72
+ class SwiGLUMLP(nn.Module):
73
+ def __init__(self, config):
74
+ super().__init__()
75
+ self.gate_proj = nn.Linear(config.dim, config.intermediate_dim, bias=False)
76
+ self.up_proj = nn.Linear(config.dim, config.intermediate_dim, bias=False)
77
+ self.down_proj = nn.Linear(config.intermediate_dim, config.dim, bias=False)
78
+ def forward(self, x):
79
+ return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
80
+
81
+ class TransformerBlockV3(nn.Module):
82
+ def __init__(self, config):
83
+ super().__init__()
84
+ self.attn = Attention(config)
85
+ self.mlp = SwiGLUMLP(config)
86
+ self.attn_norm_1 = RMSNorm(config.dim, eps=config.norm_eps)
87
+ self.mlp_norm_1 = RMSNorm(config.dim, eps=config.norm_eps)
88
+ self.attn_norm_2 = RMSNorm(config.dim, eps=config.norm_eps)
89
+ self.mlp_norm_2 = RMSNorm(config.dim, eps=config.norm_eps)
90
+
91
+ def forward(self, x, cos, sin, pass_idx=0):
92
+ norm_attn = self.attn_norm_1 if pass_idx == 0 else self.attn_norm_2
93
+ norm_mlp = self.mlp_norm_1 if pass_idx == 0 else self.mlp_norm_2
94
+ # V2 HATASI DÜZELTİLDİ: Doğru Kalıntı Yolu
95
+ x = x + self.attn(norm_attn(x), cos, sin)
96
+ x = x + self.mlp(norm_mlp(x))
97
+ return x
98
+
99
+ class PicoLMV3ForCausalLM(PreTrainedModel, GenerationMixin):
100
+ config_class = PicoLMV3Config
101
+ base_model_prefix = "model"
102
+
103
+ def __init__(self, config):
104
+ super().__init__(config)
105
+ self.config = config
106
+ self.tok_embeddings = nn.Embedding(config.vocab_size, config.emb_dim)
107
+ self.emb_proj = nn.Linear(config.emb_dim, config.dim, bias=False)
108
+ self.blocks = nn.ModuleList([TransformerBlockV3(config) for _ in range(config.n_layers)])
109
+ self.final_norm = RMSNorm(config.dim, eps=config.norm_eps)
110
+ cos, sin = precompute_rope_cis_yarn(config.head_dim, config.max_seq_len, config.orig_seq_len, config.rope_theta)
111
+ self.register_buffer("rope_cos", cos, persistent=False)
112
+ self.register_buffer("rope_sin", sin, persistent=False)
113
+ self.post_init()
114
+
115
+ def get_input_embeddings(self):
116
+ return self.tok_embeddings
117
+
118
+ def set_input_embeddings(self, value):
119
+ self.tok_embeddings = value
120
+
121
+ def forward(self, input_ids=None, labels=None, **kwargs):
122
+ B, S = input_ids.shape
123
+ x_low = self.tok_embeddings(input_ids)
124
+ x = self.emb_proj(x_low)
125
+ cos, sin = self.rope_cos[:S], self.rope_sin[:S]
126
+
127
+ for block in self.blocks:
128
+ for p_idx in range(self.config.layer_repeat):
129
+ x = block(x, cos, sin, pass_idx=p_idx)
130
+
131
+ h = self.final_norm(x)
132
+ # Factorized Tied Embedding Çıkışı
133
+ h_low = F.linear(h, self.emb_proj.weight.t())
134
+ logits = F.linear(h_low, self.tok_embeddings.weight)
135
+
136
+ loss = None
137
+ if labels is not None:
138
+ loss = F.cross_entropy(logits.reshape(-1, self.config.vocab_size), labels.reshape(-1), ignore_index=-100)
139
+
140
+ return CausalLMOutputWithPast(loss=loss, logits=logits)
141
+
142
+ def prepare_inputs_for_generation(self, input_ids, **kwargs):
143
+ return {"input_ids": input_ids}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "bos_token": "<|endoftext|>",
4
+ "clean_up_tokenization_spaces": false,
5
+ "eos_token": "<|im_end|>",
6
+ "model_max_length": 1000000000000000019884624838656,
7
+ "pad_token": "<|pad|>",
8
+ "tokenizer_class": "TokenizersBackend",
9
+ "chat_template": "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
10
+ }