Aadit-032 commited on
Commit
4c3876f
Β·
verified Β·
1 Parent(s): cb9c8fd

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +209 -0
  2. config.json +33 -0
  3. model.safetensors +3 -0
  4. training_results.json +28 -0
README.md ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Model Architecture
2
+
3
+ The goal of LiteGPT-16M is not to achieve state-of-the-art performance, but to provide a clean and understandable implementation of a GPT-style language model that can be trained from scratch and extended with modern techniques in future experiments.
4
+
5
+ ## Overview
6
+ - Model type: Decoder-only Transformer
7
+ - Parameters: ~16M
8
+ - Context length: 128
9
+ - Vocabulary size: 50,257
10
+ - Attention: Causal Self-Attention
11
+ - Positional Encoding: Learned Position Embeddings
12
+
13
+ ## Architecture Diagram
14
+
15
+ ```text
16
+ Input Tokens [B, T]
17
+ β”‚
18
+ β–Ό
19
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
20
+ β”‚ Token Embeddings β”‚
21
+ β”‚ [vocab, d_model] β”‚
22
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
23
+ β”‚
24
+ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
25
+ β–Ό β”‚
26
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
27
+ β”‚ Position Embeddings β”‚β”‚
28
+ β”‚ [seq_len, d_model] β”‚β”‚
29
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
30
+ β”‚ β”‚
31
+ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
32
+ β–Ό
33
+ x = tok + pos
34
+ β”‚
35
+ β–Ό
36
+ ╔══════════════════════════════╗
37
+ β•‘ Transformer Block Γ— 4 β•‘
38
+ β•‘ β•‘
39
+ β•‘ LayerNorm β•‘
40
+ β•‘ β”‚ β•‘
41
+ β•‘ β–Ό β•‘
42
+ β•‘ Multi-Head Attention β•‘
43
+ β•‘ β”‚ β•‘
44
+ β•‘ β–Ό β•‘
45
+ β•‘ Residual Add β•‘
46
+ β•‘ β”‚ β•‘
47
+ β•‘ β–Ό β•‘
48
+ β•‘ LayerNorm β•‘
49
+ β•‘ β”‚ β•‘
50
+ β•‘ β–Ό β•‘
51
+ β•‘ FFN β•‘
52
+ β•‘ β”‚ β•‘
53
+ β•‘ β–Ό β•‘
54
+ β•‘ Residual Add β•‘
55
+ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
56
+ β”‚
57
+ β–Ό
58
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
59
+ β”‚ Final LayerNorm β”‚
60
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
61
+ β”‚
62
+ β–Ό
63
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
64
+ β”‚ LM Head β”‚
65
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
66
+ β”‚
67
+ β–Ό
68
+ Logits [B,T,V]
69
+ ```
70
+
71
+ ## Configuration
72
+
73
+ | Parameter | Value |
74
+ |------------|---------|
75
+ | batch_size | 16 |
76
+ | grad_accum_steps | 4 |
77
+ | n_layers | 4 |
78
+ | d_model | 256 |
79
+ | n_heads | 4 |
80
+ | head_dim | 64 |
81
+ | ffn_dim | 1024 |
82
+ | context_length | 128 |
83
+ | vocab_size | 50257 |
84
+
85
+ ## Transformer Block
86
+
87
+ ### Attention
88
+ - Multi-Head Self Attention
89
+ - Causal Masking
90
+
91
+ ### Feed Forward Network
92
+
93
+ FFN(x) = W2(GELU(W1(x)))
94
+
95
+ Expansion ratio: 4Γ—
96
+
97
+ ### Residual Connections
98
+
99
+ x = x + Attention(x)
100
+
101
+ x = x + FFN(x)
102
+
103
+ ### Normalization
104
+ - LayerNorm
105
+
106
+ ## Parameter Count
107
+
108
+ | Component | Params |
109
+ |------------|---------|
110
+ | Token Embeddings | (n_vocab x d_model) 50257 x 256 = 12,865,792 |
111
+ | Position Embeddings | (seq_len x d_model) 128 x 256 = 32,768 |
112
+ | Attention | ([QKV + O] x n_layers) [256 x (3 x 256) + 256 x 256] x 4 = 1,048,576 |
113
+ | FFN | [(d_model x (4 x d_model) + (4 x d_model) + d_model)] x n_layers = [256 x 1024 + 1024 x 256] x 4 = 2,097,152 |
114
+ | Norm | [(2 x d_model) x n_layers] (2 x 256 x 4) = 2048 |
115
+ | Final Norm | 256 |
116
+ | LM Head | weight tying with token embeddings |
117
+ | Total | ~16M |
118
+
119
+ ## Design Decisions
120
+
121
+ This model is intentionally kept as close to GPT-2 as possible to build a strong understanding of decoder-only transformers before introducing modern architectural improvements.
122
+
123
+ ### GPT-2 Baseline
124
+ The model uses:
125
+ - Learned token embeddings
126
+ - Learned positional embeddings
127
+ - Multi-Head Self Attention (MHSA)
128
+ - GELU activations
129
+ - LayerNorm
130
+ - Causal masking
131
+
132
+ ### Simplicity Over Performance
133
+ Features such as RoPE, GQA, FlashAttention, SwiGLU, RMSNorm, and Mixture-of-Experts are intentionally omitted. While these improve efficiency or performance, they add implementation complexity and make it harder to study the core transformer architecture.
134
+
135
+ ### Small Scale Training
136
+ The model is designed to train on a single NVIDIA T4 GPU using Google Colab. Model size, context length, and batch size are chosen to fit within limited compute resources.
137
+
138
+
139
+ ## Dataset
140
+
141
+ | Metric | Value |
142
+ |----------|---------|
143
+ | Characters | 1,115,394 |
144
+ | GPT-2 Tokens | ~338,000 |
145
+ | Vocabulary Used | ~11,706 |
146
+ | Full GPT-2 Vocabulary | 50,257 |
147
+
148
+
149
+ ```text
150
+ shakespeare.txt
151
+ β”‚
152
+ β–Ό
153
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
154
+ β”‚ GPT-2 Tokenizer β”‚
155
+ β”‚ (tiktoken) β”‚
156
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
157
+ β”‚
158
+ β–Ό
159
+ Token IDs
160
+ β”‚
161
+ β–Ό
162
+ ���──────────────────┐
163
+ β”‚ 90/10 Split β”‚
164
+ β”‚ Train / Val β”‚
165
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
166
+ β”‚
167
+ β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
168
+ β–Ό β–Ό
169
+ train.bin val.bin
170
+ (uint16) (uint16)
171
+ ```
172
+
173
+ ### Tokenizer
174
+
175
+ - Type: tiktoken
176
+ - Encoding: gpt2
177
+ - Vocabulary size: 50,257
178
+
179
+ ### Storage Format
180
+
181
+ train.bin
182
+
183
+ val.bin
184
+
185
+ dtype = uint16
186
+
187
+
188
+ ## Training
189
+
190
+ ### Hyperparameters
191
+
192
+ | Parameter | Value |
193
+ |------------|---------|
194
+ | Batch Size | 16 |
195
+ | Sequence Length | 256 |
196
+ | Learning Rate | 3e-4 |
197
+ | Weight Decay | 0.1 |
198
+ | Warmup Steps | 2000 |
199
+ | Max Steps | 40000 |
200
+
201
+ ## Results
202
+
203
+ ### Final Results
204
+
205
+ | Metric | Value |
206
+ |---------|---------|
207
+ | Train Loss | 2.878491520881653 |
208
+ | Val Loss | 5.985600624084473 |
209
+ | Perplexity | 397.6612944866264 |
config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "batch_size": 16,
3
+ "grad_accum_steps": 4,
4
+ "max_iters": 40000,
5
+ "warmup_iters": 2000,
6
+
7
+ "max_lr": 3e-4,
8
+ "weight_decay": 0.1,
9
+
10
+ "beta1": 0.9,
11
+ "beta2": 0.95,
12
+ "eps": 1e-8,
13
+
14
+ "eval_interval": 500,
15
+ "eval_iters": 100,
16
+
17
+ "grad_clip": 1.0,
18
+
19
+ "device": "cuda",
20
+
21
+ "seed": 42,
22
+
23
+ "n_vocab": 50257,
24
+
25
+ "seq_len": 128,
26
+
27
+ "n_layers": 4,
28
+ "n_head": 4,
29
+ "d_model": 256,
30
+
31
+ "dropout": 0.1,
32
+ "bias": false
33
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:58efdd6619cb98f0dd54522c9ba800d19f7aad1a94d8733450cf7f60922c0600
3
+ size 64199104
training_results.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "final_metrics": {
3
+ "loss": 2.878491520881653,
4
+ "learning_rate": 0.0002715339687685131,
5
+ "grad_norm": 1.165515422821045,
6
+ "tokens_per_sec": 18409.28742120674,
7
+ "perplexity": 397.6612944866264,
8
+ "val_loss": 5.985600624084473
9
+ },
10
+ "best_val_loss": 5.135919117927552,
11
+ "total_optimizer_steps": 10000,
12
+ "config": {
13
+ "batch_size": 16,
14
+ "grad_accum_steps": 4,
15
+ "max_iters": 40000,
16
+ "warmup_iters": 2000,
17
+ "max_lr": 0.0003,
18
+ "weight_decay": 0.1,
19
+ "beta1": 0.9,
20
+ "beta2": 0.95,
21
+ "eps": 1e-08,
22
+ "eval_interval": 500,
23
+ "eval_iters": 100,
24
+ "grad_clip": 1.0,
25
+ "device": "cuda",
26
+ "seed": 42
27
+ }
28
+ }