Custom SML is a custom PyTorch implementation of a Small Language Model (SML) inspired by the Gemma architecture. It is designed to be trained from scratch and is configured here to learn from the TinyStories dataset.
- Gemma-Inspired Architecture: Implements advanced transformer features including Grouped Query Attention (GQA) and Rotary Positional Embeddings (RoPE).
- Sliding Window Attention: Combines full attention and sliding window attention layers for efficient long-context modeling.
- Efficient Normalization: Uses
RMSNormfor stable and efficient training. - Optimized Training Loop: Includes Mixed Precision Training (
bfloat16), Gradient Accumulation, and a Cosine Annealing Learning Rate Scheduler with Warmup. - Custom Tokenization: Uses standard Byte-Pair Encoding (BPE) via
tiktoken(GPT-2 encoding).
- Parameters: ~270M
- Vocabulary Size: 50,257
- Layers: 18 (Mix of
sliding_attentionandfull_attention) - Embedding Dimension: 640
- Heads: 4 (Query Heads), 1 (KV Group for GQA)
- Context Length: Up to 32,768 tokens (Sliding Window: 512)
Ensure you have Python 3.8+ installed. Install the required dependencies:
pip install torch numpy datasets tiktoken tqdmThe model uses the TinyStories dataset. The training script automatically downloads and tokenizes the dataset using tiktoken. It saves the tokenized data into .bin memory-mapped files (train.bin, validation.bin) for highly efficient batched loading.
The training loop is completely custom and tracks validation loss, periodically saving the best model state to best_model_params.pt.
Key training hyperparameters (configurable in the code):
- Batch Size: 32
- Block Size: 128
- Learning Rate: 1e-4 (Peak)
- Gradient Accumulation Steps: 32
To generate text using a trained model, utilize the generate function:
import torch
# Load tokenzier
import tiktoken
enc = tiktoken.get_encoding("gpt2")
# Load model
model = Gemma3Model(GEMMA3_CONFIG_270M)
model.load_state_dict(torch.load("best_model_params.pt"))
model.to("cuda")
model.eval()
# Generate
sentence = "There is a tree in the forest "
context = torch.tensor(enc.encode_ordinary(sentence)).unsqueeze(dim=0).to("cuda")
output_ids = model.generate(context, max_new_tokens=200)
print(enc.decode(output_ids.squeeze().tolist()))Gemma.ipynb: Main Jupyter Notebook containing the full implementation, dataset processing, model architecture, training loop, and inference code.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.