Shivam Sharma commited on
Commit
3d56b4c
·
verified ·
1 Parent(s): 6c6af14

Initial release: TinyWay 1.0.0 (52.94M params)

Browse files
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .modeling_tinyway import TinyWayConfig, TinyWayForCausalLM
config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "tinyway",
3
+ "architectures": [
4
+ "TinyWayForCausalLM"
5
+ ],
6
+ "auto_map": {
7
+ "AutoConfig": "modeling_tinyway.TinyWayConfig",
8
+ "AutoModelForCausalLM": "modeling_tinyway.TinyWayForCausalLM"
9
+ },
10
+ "vocab_size": 50257,
11
+ "n_positions": 256,
12
+ "n_embd": 384,
13
+ "n_layer": 8,
14
+ "n_head": 8,
15
+ "dropout": 0.1,
16
+ "bos_token_id": 50256,
17
+ "eos_token_id": 50256,
18
+ "pad_token_id": 50256
19
+ }
generation_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "max_new_tokens": 256,
3
+ "do_sample": true,
4
+ "temperature": 0.8,
5
+ "top_p": 0.95,
6
+ "top_k": 50,
7
+ "repetition_penalty": 1.1,
8
+ "eos_token_id": 50256,
9
+ "pad_token_id": 50256
10
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0bb66c2d2f46d420d5b0756c68282527ba39392258af7963e29a93becfce3da
3
+ size 213876668
modeling_tinyway.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import math
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+
7
+ from transformers import PreTrainedModel, PretrainedConfig
8
+ from transformers.generation.utils import GenerationMixin
9
+ from transformers.modeling_outputs import CausalLMOutput
10
+
11
+
12
+ # =========================
13
+ # Config
14
+ # =========================
15
+
16
+ class TinyWayConfig(PretrainedConfig):
17
+ model_type = "tinyway"
18
+
19
+ def __init__(
20
+ self,
21
+ vocab_size=50257,
22
+ n_positions=256,
23
+ n_embd=384,
24
+ n_layer=8,
25
+ n_head=8,
26
+ dropout=0.1,
27
+ **kwargs
28
+ ):
29
+ super().__init__(**kwargs)
30
+
31
+ # --- original fields ---
32
+ self.vocab_size = vocab_size
33
+ self.n_positions = n_positions
34
+ self.n_embd = n_embd
35
+ self.n_layer = n_layer
36
+ self.n_head = n_head
37
+ self.dropout = dropout
38
+
39
+ # --- HF standard aliases (CRITICAL) ---
40
+ self.hidden_size = n_embd
41
+ self.num_hidden_layers = n_layer
42
+ self.num_attention_heads = n_head
43
+ self.max_position_embeddings = n_positions
44
+
45
+
46
+
47
+ # =========================
48
+ # Attention
49
+ # =========================
50
+
51
+ class CausalSelfAttention(nn.Module):
52
+ def __init__(self, config):
53
+ super().__init__()
54
+ assert config.n_embd % config.n_head == 0
55
+
56
+ self.n_head = config.n_head
57
+ self.head_dim = config.n_embd // config.n_head
58
+
59
+ self.qkv = nn.Linear(config.n_embd, 3 * config.n_embd)
60
+ self.proj = nn.Linear(config.n_embd, config.n_embd)
61
+
62
+ self.register_buffer(
63
+ "mask",
64
+ torch.tril(torch.ones(config.n_positions, config.n_positions))
65
+ )
66
+
67
+ def forward(self, x):
68
+ B, T, C = x.shape
69
+
70
+ qkv = self.qkv(x)
71
+ q, k, v = qkv.chunk(3, dim=-1)
72
+
73
+ q = q.view(B, T, self.n_head, self.head_dim).transpose(1, 2)
74
+ k = k.view(B, T, self.n_head, self.head_dim).transpose(1, 2)
75
+ v = v.view(B, T, self.n_head, self.head_dim).transpose(1, 2)
76
+
77
+ att = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)
78
+ att = att.masked_fill(self.mask[:T, :T] == 0, float("-inf"))
79
+ att = F.softmax(att, dim=-1)
80
+
81
+ out = att @ v
82
+ out = out.transpose(1, 2).contiguous().view(B, T, C)
83
+
84
+ return self.proj(out)
85
+
86
+
87
+ # =========================
88
+ # Transformer Block
89
+ # =========================
90
+
91
+ class DecoderBlock(nn.Module):
92
+ def __init__(self, config):
93
+ super().__init__()
94
+ self.attn = CausalSelfAttention(config)
95
+ self.ffn = nn.Sequential(
96
+ nn.Linear(config.n_embd, 4 * config.n_embd),
97
+ nn.GELU(),
98
+ nn.Linear(4 * config.n_embd, config.n_embd)
99
+ )
100
+ self.ln1 = nn.LayerNorm(config.n_embd)
101
+ self.ln2 = nn.LayerNorm(config.n_embd)
102
+ self.dropout = nn.Dropout(config.dropout)
103
+
104
+ def forward(self, x):
105
+ x = x + self.dropout(self.attn(self.ln1(x)))
106
+ x = x + self.dropout(self.ffn(self.ln2(x)))
107
+ return x
108
+
109
+
110
+ # =========================
111
+ # Model
112
+ # =========================
113
+
114
+ class TinyWayForCausalLM(PreTrainedModel, GenerationMixin):
115
+ config_class = TinyWayConfig
116
+
117
+ def __init__(self, config):
118
+ super().__init__(config)
119
+
120
+ self.token_emb = nn.Embedding(config.vocab_size, config.n_embd)
121
+ self.pos_emb = nn.Embedding(config.n_positions, config.n_embd)
122
+
123
+ self.blocks = nn.ModuleList(
124
+ [DecoderBlock(config) for _ in range(config.n_layer)]
125
+ )
126
+
127
+ self.ln = nn.LayerNorm(config.n_embd)
128
+
129
+ # MUST match training
130
+ self.head = nn.Linear(config.n_embd, config.vocab_size)
131
+
132
+ self.post_init()
133
+
134
+ # ---- HF REQUIRED METHODS ----
135
+ def get_input_embeddings(self):
136
+ return self.token_emb
137
+
138
+ def set_input_embeddings(self, value):
139
+ self.token_emb = value
140
+
141
+ # ---- Forward ----
142
+ def forward(self, input_ids, **kwargs):
143
+ B, T = input_ids.shape
144
+ pos = torch.arange(T, device=input_ids.device)
145
+
146
+ x = self.token_emb(input_ids) + self.pos_emb(pos)
147
+
148
+ for block in self.blocks:
149
+ x = block(x)
150
+
151
+ x = self.ln(x)
152
+ logits = self.head(x)
153
+
154
+ return CausalLMOutput(logits=logits)
special_tokens_map.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<|endoftext|>",
3
+ "eos_token": "<|endoftext|>",
4
+ "pad_token": "<|endoftext|>",
5
+ "unk_token": "<|endoftext|>"
6
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "added_tokens_decoder": {
4
+ "50256": {
5
+ "content": "<|endoftext|>",
6
+ "lstrip": false,
7
+ "normalized": true,
8
+ "rstrip": false,
9
+ "single_word": false,
10
+ "special": true
11
+ }
12
+ },
13
+ "bos_token": "<|endoftext|>",
14
+ "clean_up_tokenization_spaces": false,
15
+ "eos_token": "<|endoftext|>",
16
+ "extra_special_tokens": {},
17
+ "model_max_length": 1024,
18
+ "pad_token": "<|endoftext|>",
19
+ "tokenizer_class": "GPT2Tokenizer",
20
+ "unk_token": "<|endoftext|>"
21
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff