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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- text-generation
|
| 7 |
+
- pytorch
|
| 8 |
+
- onnx
|
| 9 |
+
- tiny-lm
|
| 10 |
+
- custom-architecture
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
<p align="center">
|
| 14 |
+
<img src="assets/coolm.png" alt="CooLM" width="400"/>
|
| 15 |
+
</p>
|
| 16 |
+
|
| 17 |
+
<h1 align="center">CooLM</h1>
|
| 18 |
+
<p align="center"><em>A ~42M parameter LLM that talks like a rooftop pigeon.</em></p>
|
| 19 |
+
|
| 20 |
+
<p align="center">
|
| 21 |
+
<a href="#"><img src="https://img.shields.io/badge/π€_Dataset-coolm--500k-blue" alt="Dataset"/></a>
|
| 22 |
+
<a href="https://huggingface.co/MdHussain121/coolm-42M"><img src="https://img.shields.io/badge/π€_Model-coolm--42M-orange" alt="Model"/></a>
|
| 23 |
+
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green" alt="License"/></a>
|
| 24 |
+
</p>
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
> **This project exists to show that training your own language model is not magic.**
|
| 29 |
+
> No PhD required. No massive GPU cluster. One local machine, a few minutes, and you have a working LLM that you built from scratch β data generation, tokenizer, model architecture, training loop, and inference.
|
| 30 |
+
>
|
| 31 |
+
> It won't produce a billion-parameter model that writes essays. But it will show you exactly how every piece works β from raw text to trained weights to generated output β so the big models stop feeling like black boxes.
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+

|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## What is CooLM?
|
| 40 |
+
|
| 41 |
+
CooLM is a tiny language model that pretends to be a pigeon named Coo. It speaks in short, lowercase sentences about rooftops, food, weather, and surviving in the city. It doesn't understand human abstractions like money, phones, or politics β and it's not trying to.
|
| 42 |
+
|
| 43 |
+
It's trained from scratch on 500K synthetic conversations across 13 topics, can be trained locally on a single GPU in 22 minutes, and produces a model small enough to run in a browser.
|
| 44 |
+
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
## Architecture
|
| 48 |
+
|
| 49 |
+
| | |
|
| 50 |
+
|---|---|
|
| 51 |
+
| **Parameters** | 41,476,480 |
|
| 52 |
+
| **Layers** | 8 |
|
| 53 |
+
| **Hidden dim** | 640 |
|
| 54 |
+
| **Heads** | 10 |
|
| 55 |
+
| **FFN** | 2560 (GeLU) |
|
| 56 |
+
| **Vocab** | 3,269 (BPE) |
|
| 57 |
+
| **Max sequence** | 64 tokens |
|
| 58 |
+
| **Norm** | LayerNorm |
|
| 59 |
+
| **Position** | Learned embeddings |
|
| 60 |
+
| **LM head** | Weight-tied with embeddings |
|
| 61 |
+
|
| 62 |
+
Vanilla decoder-only transformer. No GQA, no RoPE, no SwiGLU. As simple as it gets.
|
| 63 |
+
|
| 64 |
+
---
|
| 65 |
+
|
| 66 |
+
## Training Performance
|
| 67 |
+
|
| 68 |
+
This was my first time ever building a language model from scratch! The entire training process ran locally on a standard gaming laptop (**NVIDIA RTX 4060 Laptop GPU**) and completed in 22 minutes.
|
| 69 |
+
|
| 70 |
+
```text
|
| 71 |
+
================================================================================
|
| 72 |
+
done. 10000 steps in 22.0m (1322s)
|
| 73 |
+
tokens processed: 8,968,042
|
| 74 |
+
avg throughput: 6784 tok/s
|
| 75 |
+
best val loss: 0.3262 (ppl 1.39)
|
| 76 |
+
================================================================================
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+

|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## Personality
|
| 84 |
+
|
| 85 |
+
Coo:
|
| 86 |
+
- Speaks in short, lowercase sentences with simple punctuation.
|
| 87 |
+
- Experiences the world through weather, rooftops, loud city sounds, and food.
|
| 88 |
+
- Doesn't understand human abstractions (alarms, jobs, money).
|
| 89 |
+
- Is observant, slightly anxious about threats, and always looking for a snack.
|
| 90 |
+
|
| 91 |
+
**13 topics:** birds, city_sounds, confused, emotions, flight, food, greetings, nesting, perch, rain, sleep, threats, weather.
|
| 92 |
+
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
## Quick Start
|
| 96 |
+
|
| 97 |
+
### Chat locally
|
| 98 |
+
|
| 99 |
+
You can run the entire pipeline or chat locally using the provided Windows batch files:
|
| 100 |
+
|
| 101 |
+
- **`train_coolm.bat`**: Installs requirements, generates the 500k dataset, prepares tokenizer/tensors, and trains the model.
|
| 102 |
+
- **`chat.bat`**: Spins up the optimized local Python web server and automatically opens your browser to the beautiful dark mode UI so you can chat with CooLM using the exported ONNX model.
|
| 103 |
+
|
| 104 |
+
#### Manual Step-by-Step Execution (No `.bat`)
|
| 105 |
+
|
| 106 |
+
If you prefer not to use the `.bat` files, you can execute the commands manually in your terminal:
|
| 107 |
+
|
| 108 |
+
1. **Install requirements**:
|
| 109 |
+
```bash
|
| 110 |
+
pip install -r requirements.txt
|
| 111 |
+
```
|
| 112 |
+
2. **Generate dataset** (500k samples):
|
| 113 |
+
```bash
|
| 114 |
+
python generate_data.py 500000
|
| 115 |
+
```
|
| 116 |
+
3. **Prepare and tokenize dataset**:
|
| 117 |
+
```bash
|
| 118 |
+
python -m coolm.prepare_data pigeon_data.jsonl
|
| 119 |
+
```
|
| 120 |
+
4. **Train the model**:
|
| 121 |
+
```bash
|
| 122 |
+
python -m coolm.train
|
| 123 |
+
```
|
| 124 |
+
5. **Export the trained model to ONNX**:
|
| 125 |
+
```bash
|
| 126 |
+
python -m tools.model_export
|
| 127 |
+
```
|
| 128 |
+
6. **Run the local chat server**:
|
| 129 |
+
```bash
|
| 130 |
+
python -m tools.server
|
| 131 |
+
```
|
| 132 |
+
*Then open `http://localhost:8000/index.html` in your browser to chat!*
|
| 133 |
+
|
| 134 |
+
---
|
| 135 |
+
|
| 136 |
+
## Dataset
|
| 137 |
+
|
| 138 |
+
| | |
|
| 139 |
+
|---|---|
|
| 140 |
+
| Samples | 500,000 (475K train / 25K val) |
|
| 141 |
+
| Format | `{"input": "...", "output": "...", "category": "..."}` |
|
| 142 |
+
| Categories | 13 |
|
| 143 |
+
| Generation | Synthetic template composition |
|
| 144 |
+
|
| 145 |
+
```python
|
| 146 |
+
import json
|
| 147 |
+
with open("pigeon_data.jsonl", "r") as f:
|
| 148 |
+
print(json.loads(f.readline()))
|
| 149 |
+
# {'input': 'can you hear the rickshaw car horn', 'output': 'the rickshaw horns are the loudest beep beep beep.', 'category': 'city_sounds'}
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
---
|
| 153 |
+
|
| 154 |
+
## Project Structure
|
| 155 |
+
|
| 156 |
+
```
|
| 157 |
+
coolm/
|
| 158 |
+
βββ config.py Hyperparameters (model + training)
|
| 159 |
+
βββ model.py Vanilla transformer
|
| 160 |
+
βββ prepare_data.py Data prep + tokenizer training
|
| 161 |
+
βββ train.py Training loop (cosine LR, AMP)
|
| 162 |
+
βββ inference.py Chat interface
|
| 163 |
+
|
| 164 |
+
generate_data.py Conversation data generator (13 topics)
|
| 165 |
+
|
| 166 |
+
tools/
|
| 167 |
+
βββ check_lengths.py Validates token sequence lengths
|
| 168 |
+
βββ model_export.py Export model to ONNX
|
| 169 |
+
βββ export_hf.py Push dataset and model to HuggingFace
|
| 170 |
+
|
| 171 |
+
docs/
|
| 172 |
+
βββ index.html Browser demo UI
|
| 173 |
+
|
| 174 |
+
assets/
|
| 175 |
+
βββ coolm.png Logo
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
---
|
| 179 |
+
|
| 180 |
+
## Design Decisions
|
| 181 |
+
|
| 182 |
+
**Why no system prompt?** Every training sample had the same one. A small model can't conditionally follow instructions β the personality is baked into the weights.
|
| 183 |
+
|
| 184 |
+
**Input Normalization:** Since the model is small and trained strictly on lowercase, unpunctuated input, `inference.py` strips punctuation and converts your prompt to lowercase automatically so it stays within the training distribution (OOD protection).
|
| 185 |
+
|
| 186 |
+
**Why synthetic data?** A pigeon character with consistent personality needs consistent training data. Template composition with randomized components generates 500K samples from basic templates, balanced evenly across 13 categories.
|
| 187 |
+
|
| 188 |
+
---
|
| 189 |
+
|
| 190 |
+
## Inspiration & Credits
|
| 191 |
+
|
| 192 |
+
CooLM was heavily inspired by **GuppyLM** (which demonstrated training a 9M parameter model on 60k samples). CooLM scales this up to a 42M parameter model trained on a 100% unique 500k sample dataset with validation tracking, early stopping, and an interactive dark mode web interface.
|
| 193 |
+
|
| 194 |
+
---
|
| 195 |
+
|
| 196 |
+
## License
|
| 197 |
+
|
| 198 |
+
MIT
|