File size: 8,542 Bytes
c914f4e
 
 
 
aa065af
 
 
 
 
 
 
 
 
dd66b05
 
 
 
2b8afa9
c914f4e
23e1293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
license: mit
language:
- en
datasets:
- roneneldan/TinyStories
base_model:
- openai-community/gpt2
pipeline_tag: text-generation
tags:
- text-generation-inference
- TinyStories
- miniGPT
- llama
- educational
- tensorflow
- keras
library_name: keras
---

# 🧠 MiniGPT β€” TinyStories

A compact **LLaMA-inspired decoder-only Transformer** trained from scratch in TensorFlow/Keras on the TinyStories dataset.

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**.

---

## πŸ“‹ Model Configuration

| Parameter                    |                    Value |
| ---------------------------- | -----------------------: |
| Architecture                 | Decoder-only Transformer |
| Architecture style           |           LLaMA-inspired |
| Parameters                   |  **85,791,744 (~85.8M)** |
| Transformer layers           |                    **5** |
| Hidden dimension (`d_model`) |                  **768** |
| Attention heads              |                   **12** |
| Head dimension               |                   **64** |
| FFN hidden dimension         |                 **3072** |
| Vocabulary size              |               **50,257** |
| Context length               |           **256 tokens** |
| Tokenizer                    |                GPT-2 BPE |
| Framework                    |       TensorFlow / Keras |
| Precision                    |         Mixed `bfloat16` |

---

## πŸ—οΈ Model Architecture

The model follows a compact LLaMA-style decoder architecture:

```text
GPT-2 BPE Tokenizer
        β”‚
        β–Ό
Token IDs
        β”‚
        β–Ό
Frozen GPT-2 Embedding Matrix
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Decoder Block Γ— 5              β”‚
β”‚                                     β”‚
β”‚  RMSNorm                            β”‚
β”‚     β”‚                               β”‚
β”‚     β–Ό                               β”‚
β”‚  Causal Multi-Head Self-Attention   β”‚
β”‚     β”‚                               β”‚
β”‚     β”œβ”€β”€ Q                           β”‚
β”‚     β”œβ”€β”€ K ──► RoPE                  β”‚
β”‚     β”œβ”€β”€ V                           β”‚
β”‚     β”‚                               β”‚
β”‚     └── Attention + Residual        β”‚
β”‚                                     β”‚
β”‚  RMSNorm                            β”‚
β”‚     β”‚                               β”‚
β”‚     β–Ό                               β”‚
β”‚  SwiGLU Feed-Forward Network        β”‚
β”‚     β”‚                               β”‚
β”‚     └── Residual                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
Final RMSNorm
        β”‚
        β–Ό
Weight-Tied Language Modeling Head
        β”‚
        β–Ό
Logits [sequence_length Γ— 50,257]
```

### πŸ”© Decoder Block

Each of the **5 decoder layers** operates on a hidden representation of size **768**.

#### 1. RMSNorm

The input representation is normalized before each major sub-layer using **RMSNorm** rather than LayerNorm.

```text
x β†’ RMSNorm β†’ Attention
```

and subsequently:

```text
x β†’ RMSNorm β†’ SwiGLU
```

This gives the block the pre-normalized structure commonly used by modern decoder-only architectures.

#### 2. Causal Multi-Head Self-Attention

The model uses:

* **12 attention heads**
* **64 dimensions per head**
* `12 Γ— 64 = 768` total attention dimension
* causal masking to prevent access to future tokens

The hidden representation is projected into:

```text
Q = XWq
K = XWk
V = XWv
```

with attention operating independently across the 12 heads before the heads are merged back into the original `768`-dimensional representation.

#### 3. Rotary Positional Embeddings

**RoPE** is applied to the query and key representations inside the attention mechanism.

This provides positional information without requiring a learned positional embedding table.

#### 4. SwiGLU Feed-Forward Network

The attention output is followed by a **SwiGLU** feed-forward block with an intermediate dimension of **3072**.

Conceptually:

```text
x
β”‚
β”œβ”€β”€ Linear ──► SiLU ──┐
β”‚                     Γ— ──► Linear ──► output
└── Linear β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

The resulting representation is returned to the residual stream.

#### 5. Residual Connections

Each major sub-layer uses a residual connection:

```text
x β†’ RMSNorm β†’ Attention β†’ + x
                         β”‚
                         β–Ό
                       x'
                         β”‚
             RMSNorm β†’ SwiGLU β†’ + x'
```

This structure is repeated **5 times**.

---

## 🎯 Output Projection & Weight Tying

After the final decoder block:

```text
Hidden States
     β”‚
     β–Ό
 Final RMSNorm
     β”‚
     β–Ό
 Language Modeling Head
     β”‚
     β–Ό
50,257 vocabulary logits
```

The language-modeling projection is **weight-tied with the input token embedding representation**, reducing redundant parameters and following the common tied-embedding approach.

The resulting tensor has the conceptual shape:

```text
(batch, sequence_length, 50,257)
```

---

## πŸ“Š Parameter Distribution

The final Keras model contains:

| Category      |     Parameters |
| ------------- | -------------: |
| Total         | **85,791,744** |
| Trainable     | **47,194,368** |
| Non-trainable | **38,597,376** |

The large non-trainable component corresponds primarily to the **frozen pretrained GPT-2 embedding matrix**.

---

## πŸ“š Training Configuration

| Parameter               | Configuration         |
| ----------------------- | --------------------- |
| Dataset                 | TinyStories           |
| Training token budget   | 100M tokens           |
| Validation token budget | 5M tokens             |
| Context length          | 256                   |
| Batch size              | 16                    |
| Epochs configured       | 3                     |
| Optimizer               | AdamW                 |
| Weight decay            | 0.01                  |
| Gradient clipping       | `clipnorm=1.0`        |
| Maximum LR              | `1e-4`                |
| Minimum LR              | `1e-5`                |
| Warmup                  | 2,000 steps           |
| LR schedule             | Warmup + cosine decay |
| Precision               | Mixed `bfloat16`      |

---

## πŸ“ˆ Evaluation

| Metric                                               | Result                                |
| ---------------------------------------------------- | ------------------------------------- |
| Validation loss / Perplexity / Generation evaluation | **Yet to be collected and evaluated** |

---

## πŸ› οΈ Implementation & Deployment

The model itself is implemented and trained in **TensorFlow/Keras**.

The notebook contains the experimental training implementation and model construction.

* πŸ““ **Training / model-construction notebook:** `[<COLAB LINK>](https://colab.research.google.com/drive/1DUSM24y3hcrc06BduC47kyWnciu8OGZg?usp=sharing)`
* 🐳 **Docker-based inference server:** `[<GITHUB LINK>](https://github.com/abhayramasamy/MiniGPT_Project)`
* πŸ€— **Hugging Face model / artifacts:** `[<HF LINK>](https://huggingface.co/ARX1A07/miniGPT_Project/)`

> The Hugging Face integration and generation pipeline are intentionally documented separately from the core architecture. The Docker repository contains the deployment-oriented inference implementation.

---

## ⚠️ Limitations

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.

It should **not** be considered a general-purpose language model or compared directly with substantially larger pretrained LLMs.

Evaluation and generation-quality benchmarking are still pending.

---

## πŸ“š References

* Eldan, R. & Li, Y. β€” *TinyStories: How Small Can Language Models Be and Still Speak Coherent English?* (2023). 
* Hugging Face β€” TinyStories dataset.
* Vaswani et al. β€” *Attention Is All You Need*.
* Su et al. β€” *RoFormer: Enhanced Transformer with Rotary Position Embedding*.
* Shazeer β€” *GLU Variants Improve Transformer*.

---