ARX1A07 commited on
Commit
23e1293
Β·
verified Β·
1 Parent(s): 2b8afa9

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +256 -0
README.md CHANGED
@@ -17,3 +17,259 @@ tags:
17
  - keras
18
  library_name: keras
19
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  - keras
18
  library_name: keras
19
  ---
20
+
21
+ # 🧠 MiniGPT β€” TinyStories
22
+
23
+ A compact **LLaMA-inspired decoder-only Transformer** trained from scratch in TensorFlow/Keras on the TinyStories dataset.
24
+
25
+ The model is designed as a small-scale experiment in implementing and training a modern decoder-only language model with components such as **RoPE, RMSNorm, SwiGLU, causal self-attention, frozen pretrained embeddings, and weight tying**.
26
+
27
+ ---
28
+
29
+ ## πŸ“‹ Model Configuration
30
+
31
+ | Parameter | Value |
32
+ | ---------------------------- | -----------------------: |
33
+ | Architecture | Decoder-only Transformer |
34
+ | Architecture style | LLaMA-inspired |
35
+ | Parameters | **85,791,744 (~85.8M)** |
36
+ | Transformer layers | **5** |
37
+ | Hidden dimension (`d_model`) | **768** |
38
+ | Attention heads | **12** |
39
+ | Head dimension | **64** |
40
+ | FFN hidden dimension | **3072** |
41
+ | Vocabulary size | **50,257** |
42
+ | Context length | **256 tokens** |
43
+ | Tokenizer | GPT-2 BPE |
44
+ | Framework | TensorFlow / Keras |
45
+ | Precision | Mixed `bfloat16` |
46
+
47
+ ---
48
+
49
+ ## πŸ—οΈ Model Architecture
50
+
51
+ The model follows a compact LLaMA-style decoder architecture:
52
+
53
+ ```text
54
+ GPT-2 BPE Tokenizer
55
+ β”‚
56
+ β–Ό
57
+ Token IDs
58
+ β”‚
59
+ β–Ό
60
+ Frozen GPT-2 Embedding Matrix
61
+ β”‚
62
+ β–Ό
63
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
64
+ β”‚ Decoder Block Γ— 5 β”‚
65
+ β”‚ β”‚
66
+ β”‚ RMSNorm β”‚
67
+ β”‚ β”‚ β”‚
68
+ β”‚ β–Ό β”‚
69
+ β”‚ Causal Multi-Head Self-Attention β”‚
70
+ β”‚ β”‚ β”‚
71
+ β”‚ β”œβ”€β”€ Q β”‚
72
+ β”‚ β”œβ”€β”€ K ──► RoPE β”‚
73
+ β”‚ β”œβ”€β”€ V β”‚
74
+ β”‚ β”‚ β”‚
75
+ β”‚ └── Attention + Residual β”‚
76
+ β”‚ β”‚
77
+ β”‚ RMSNorm β”‚
78
+ β”‚ β”‚ β”‚
79
+ β”‚ β–Ό β”‚
80
+ β”‚ SwiGLU Feed-Forward Network β”‚
81
+ β”‚ β”‚ β”‚
82
+ β”‚ └── Residual β”‚
83
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
84
+ β”‚
85
+ β–Ό
86
+ Final RMSNorm
87
+ β”‚
88
+ β–Ό
89
+ Weight-Tied Language Modeling Head
90
+ β”‚
91
+ β–Ό
92
+ Logits [sequence_length Γ— 50,257]
93
+ ```
94
+
95
+ ### πŸ”© Decoder Block
96
+
97
+ Each of the **5 decoder layers** operates on a hidden representation of size **768**.
98
+
99
+ #### 1. RMSNorm
100
+
101
+ The input representation is normalized before each major sub-layer using **RMSNorm** rather than LayerNorm.
102
+
103
+ ```text
104
+ x β†’ RMSNorm β†’ Attention
105
+ ```
106
+
107
+ and subsequently:
108
+
109
+ ```text
110
+ x β†’ RMSNorm β†’ SwiGLU
111
+ ```
112
+
113
+ This gives the block the pre-normalized structure commonly used by modern decoder-only architectures.
114
+
115
+ #### 2. Causal Multi-Head Self-Attention
116
+
117
+ The model uses:
118
+
119
+ * **12 attention heads**
120
+ * **64 dimensions per head**
121
+ * `12 Γ— 64 = 768` total attention dimension
122
+ * causal masking to prevent access to future tokens
123
+
124
+ The hidden representation is projected into:
125
+
126
+ ```text
127
+ Q = XWq
128
+ K = XWk
129
+ V = XWv
130
+ ```
131
+
132
+ with attention operating independently across the 12 heads before the heads are merged back into the original `768`-dimensional representation.
133
+
134
+ #### 3. Rotary Positional Embeddings
135
+
136
+ **RoPE** is applied to the query and key representations inside the attention mechanism.
137
+
138
+ This provides positional information without requiring a learned positional embedding table.
139
+
140
+ #### 4. SwiGLU Feed-Forward Network
141
+
142
+ The attention output is followed by a **SwiGLU** feed-forward block with an intermediate dimension of **3072**.
143
+
144
+ Conceptually:
145
+
146
+ ```text
147
+ x
148
+ β”‚
149
+ β”œβ”€β”€ Linear ──► SiLU ──┐
150
+ β”‚ Γ— ──► Linear ──► output
151
+ └── Linear β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
152
+ ```
153
+
154
+ The resulting representation is returned to the residual stream.
155
+
156
+ #### 5. Residual Connections
157
+
158
+ Each major sub-layer uses a residual connection:
159
+
160
+ ```text
161
+ x β†’ RMSNorm β†’ Attention β†’ + x
162
+ β”‚
163
+ β–Ό
164
+ x'
165
+ β”‚
166
+ RMSNorm β†’ SwiGLU β†’ + x'
167
+ ```
168
+
169
+ This structure is repeated **5 times**.
170
+
171
+ ---
172
+
173
+ ## 🎯 Output Projection & Weight Tying
174
+
175
+ After the final decoder block:
176
+
177
+ ```text
178
+ Hidden States
179
+ β”‚
180
+ β–Ό
181
+ Final RMSNorm
182
+ β”‚
183
+ β–Ό
184
+ Language Modeling Head
185
+ β”‚
186
+ β–Ό
187
+ 50,257 vocabulary logits
188
+ ```
189
+
190
+ The language-modeling projection is **weight-tied with the input token embedding representation**, reducing redundant parameters and following the common tied-embedding approach.
191
+
192
+ The resulting tensor has the conceptual shape:
193
+
194
+ ```text
195
+ (batch, sequence_length, 50,257)
196
+ ```
197
+
198
+ ---
199
+
200
+ ## πŸ“Š Parameter Distribution
201
+
202
+ The final Keras model contains:
203
+
204
+ | Category | Parameters |
205
+ | ------------- | -------------: |
206
+ | Total | **85,791,744** |
207
+ | Trainable | **47,194,368** |
208
+ | Non-trainable | **38,597,376** |
209
+
210
+ The large non-trainable component corresponds primarily to the **frozen pretrained GPT-2 embedding matrix**.
211
+
212
+ ---
213
+
214
+ ## πŸ“š Training Configuration
215
+
216
+ | Parameter | Configuration |
217
+ | ----------------------- | --------------------- |
218
+ | Dataset | TinyStories |
219
+ | Training token budget | 100M tokens |
220
+ | Validation token budget | 5M tokens |
221
+ | Context length | 256 |
222
+ | Batch size | 16 |
223
+ | Epochs configured | 3 |
224
+ | Optimizer | AdamW |
225
+ | Weight decay | 0.01 |
226
+ | Gradient clipping | `clipnorm=1.0` |
227
+ | Maximum LR | `1e-4` |
228
+ | Minimum LR | `1e-5` |
229
+ | Warmup | 2,000 steps |
230
+ | LR schedule | Warmup + cosine decay |
231
+ | Precision | Mixed `bfloat16` |
232
+
233
+ ---
234
+
235
+ ## πŸ“ˆ Evaluation
236
+
237
+ | Metric | Result |
238
+ | ---------------------------------------------------- | ------------------------------------- |
239
+ | Validation loss / Perplexity / Generation evaluation | **Yet to be collected and evaluated** |
240
+
241
+ ---
242
+
243
+ ## πŸ› οΈ Implementation & Deployment
244
+
245
+ The model itself is implemented and trained in **TensorFlow/Keras**.
246
+
247
+ The notebook contains the experimental training implementation and model construction.
248
+
249
+ * πŸ““ **Training / model-construction notebook:** `[<COLAB LINK>](https://colab.research.google.com/drive/1DUSM24y3hcrc06BduC47kyWnciu8OGZg?usp=sharing)`
250
+ * 🐳 **Docker-based inference server:** `[<GITHUB LINK>](https://github.com/abhayramasamy/MiniGPT_Project)`
251
+ * πŸ€— **Hugging Face model / artifacts:** `[<HF LINK>](https://huggingface.co/ARX1A07/miniGPT_Project/)`
252
+
253
+ > The Hugging Face integration and generation pipeline are intentionally documented separately from the core architecture. The Docker repository contains the deployment-oriented inference implementation.
254
+
255
+ ---
256
+
257
+ ## ⚠️ Limitations
258
+
259
+ This is a relatively small language model trained specifically on **TinyStories**. Its capabilities should therefore be interpreted within the scope of the dataset and parameter scale.
260
+
261
+ It should **not** be considered a general-purpose language model or compared directly with substantially larger pretrained LLMs.
262
+
263
+ Evaluation and generation-quality benchmarking are still pending.
264
+
265
+ ---
266
+
267
+ ## πŸ“š References
268
+
269
+ * Eldan, R. & Li, Y. β€” *TinyStories: How Small Can Language Models Be and Still Speak Coherent English?* (2023).
270
+ * Hugging Face β€” TinyStories dataset.
271
+ * Vaswani et al. β€” *Attention Is All You Need*.
272
+ * Su et al. β€” *RoFormer: Enhanced Transformer with Rotary Position Embedding*.
273
+ * Shazeer β€” *GLU Variants Improve Transformer*.
274
+
275
+ ---