Loading repository data…
Loading repository data…
ChaitanyaK77 / repository
This Repository provides a Jupyter Notebook for building a small language model from scratch using 'TinyStories' dataset. Covers data preprocessing, BPE tokenization, binary storage, GPU memory management, and training a Transformer in PyTorch. Generate sample stories to test your model. Ideal for learning NLP and PyTorch.
A step-by-step Jupyter Notebook demonstrating how to build and train a compact small language model (“SLM”) from scratch using the TinyStories dataset. Covers data preparation, BPE tokenization, efficient binary storage, GPU memory locking, Transformer architecture, training configuration, and sample text generation.
tiktoken (BPE) for subword encodings..bin files for fast reloads.Building Large Language Models (LLMs) from scratch can be resource-intensive. This notebook shows how to create a Small Language Model (SLM) using a lightweight dataset, minimalist code, and standard hardware (e.g., a single GPU).
datasetstiktokennumpy, matplotlib, tqdm# Create a virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate
# Install core dependencies
pip install torch torchvision \
datasets \
tiktoken \
numpy \
matplotlib \
tqdm
from datasets import load_dataset
ds = load_dataset("roneneldan/TinyStories")
Implements Byte Pair Encoding via tiktoken.
Converts text → token IDs → binary .bin files.
Saves training/validation tokens on disk for fast reload.
Reserves GPU memory (torch.cuda.set_per_process_memory_fraction or similar) to prevent fragmentation.
Lightweight Transformer with configurable layers, heads, and embedding size.
@dataclass
class GPTConfig:
block_size: int
vocab_size: int
n_layer: int
n_head: int
n_embd: int
dropout: float = 0.0
bias: bool = True
Example hyperparameters:
| Parameter | Value |
|---|---|
| block_size | 128 |
| vocab_size | 50,000 |
| n_layer | 4 |
| n_head | 8 |
| n_embd | 256 |
| dropout | 0.1 |
After training, run:
prompt = "Once upon a time"
generated = model.generate(prompt, max_new_tokens=100)
print(generated)
Expect TinyStories-style outputs (short, coherent sentences).
git checkout -b feature/xyz)git commit -m 'Add xyz feature')git push origin feature/xyz)All contributions—bug reports, documentation fixes, new features—are welcome!
I have taken inspiration from the following resources
Released under the MIT License.