Text Generation
Keras
English
text-generation-inference
TinyStories
miniGPT
llama
educational
tensorflow
Instructions to use ARX1A07/miniGPT_Project with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use ARX1A07/miniGPT_Project with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://ARX1A07/miniGPT_Project") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -17,3 +17,259 @@ tags:
|
|
| 17 |
- keras
|
| 18 |
library_name: keras
|
| 19 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ow/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 |
+
---
|