miniGPT_Project / README.md
ARX1A07's picture
Update README.md
23e1293 verified
|
Raw History Blame Contribute Delete
8.54 kB
---
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*.
---