Skip to content
McNopperPublic

About

Large Language Model implementation for educational purpose

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Athena

Athena, the Greek goddess of knowledge, wisdom, and intelligence.

A modern, well-documented implementation of a Large Language Model (LLM) for educational purposes. Built with PyTorch, featuring clean architecture and extensive documentation.

Overview

Athena is a transformer-based language model designed for learning and teaching. The codebase prioritizes:

  • Educational Value: Extensive comments explaining concepts, not just code
  • Clean Architecture: Modular, well-organized structure
  • Modern Design: LLaMA-style architecture with RoPE, RMSNorm, SwiGLU
  • Runnable: Works on consumer hardware, on any GPU vendor or CPU

Model Architecture

Key Components

  • Rotary Position Embeddings (RoPE): Modern position encoding
  • RMS Layer Normalization: Simplified normalization
  • Multi-Head Attention: Core transformer mechanism
  • SwiGLU Activation: Modern feed-forward network
  • Tied Embeddings: Efficient input/output projections

Model Sizes

Config Parameters Dim Heads Layers Seq Len Use Case Training Time
pico ~1M 64 4 4 256 Quick tests ~2 min/epoch
nano ~3M 128 8 6 256 CPU testing ~5 min/epoch
micro ~6M 192 12 8 512 Fast iteration ~10 min/epoch
tiny ~10M 256 8 6 512 Experimentation ~20 min/epoch
small ~25M 384 12 8 512 Recommended ~40 min/epoch
medium ~50M 512 16 12 512 Realistic demo ~70 min/epoch
large ~100M 640 20 16 512 Upper limit ~120 min/epoch

Testing Strategy:

  • pico (1M): Fastest - verify code works (~30 seconds on GPU)
  • nano (3M): CPU-friendly - test without GPU (~5 minutes)
  • micro (6M): Quick experiments - iterate fast (~10 minutes)
  • tiny (10M): Debugging - test architecture changes (~20 minutes)
  • small (25M): Recommended - balance of speed and quality
  • medium (50M): Realistic - demonstrate scaling (~70 minutes)
  • large (100M): Maximum - upper limit for 6GB VRAM (~120 minutes)

Project Structure

Athena/
β”œβ”€β”€ README.md              # This file
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ setup.py              # Package configuration
β”œβ”€β”€ data/                 # Training data and checkpoints
β”‚   β”œβ”€β”€ raw/              # Your training text files
β”‚   β”œβ”€β”€ processed/        # Tokenized data
β”‚   └── checkpoints/      # Model checkpoints
β”œβ”€β”€ src/                  # Source code
β”‚   β”œβ”€β”€ config/          # Model and training configurations
β”‚   β”œβ”€β”€ model/           # Model architecture components
β”‚   β”œβ”€β”€ tokenizer/       # Tokenization (BPE)
β”‚   β”œβ”€β”€ training/        # Training pipeline
β”‚   β”œβ”€β”€ inference/       # Text generation
β”‚   └── utils/           # Utilities
β”œβ”€β”€ scripts/             # Executable scripts
β”œβ”€β”€ tests/               # Unit tests
└── docs/                # Documentation

Installation

Requirements

  • Python 3.10+ (recommended: 3.11+ for better performance)
  • A supported compute backend (optional GPU, else CPU) β€” see Compute Backends
  • 6GB+ VRAM for the recommended configuration (less for smaller models)

Setup

  1. Clone the repository
  2. Install dependencies:
git clone https://github.com/McNopper/Athena.git
cd Athena
pip install -r requirements.txt

Usage

Quick Start

# Load configuration
from src.config.model_config import get_config
config = get_config("small")  # 25M parameters

# Create model
from src.model.llm import LLM
model = LLM(config)

# Move to GPU
from src.utils.device import get_device
device = get_device()
model = model.to(device)

Quick Start - Test Everything Works

Option 1: Run the complete test suite (Recommended - 2 minutes)

cd Athena
python scripts/quick_test.py

This will test:

  • βœ“ Model creation and forward pass
  • βœ“ Backward pass and gradient flow
  • βœ“ Tokenizer with small dataset
  • βœ“ Data preparation pipeline
  • βœ“ Single training step
  • βœ“ Text generation

Option 2: Test individual components

# Test model components
python -m src.model.layer_norm
python -m src.model.positional
python -m src.model.embeddings
python -m src.model.attention
python -m src.model.ffn
python -m src.model.transformer
python -m src.model.llm

# Test tokenizer
python -m src.tokenizer.vocabulary
python -m src.tokenizer.bpe_tokenizer

Preparing Training Data

Quick test with sample data:

# Create sample data
mkdir -p data/raw
echo "Hello world, this is a test." > data/raw/sample1.txt
echo "Machine learning is fascinating." > data/raw/sample2.txt

# Prepare data (small vocab for testing)
python scripts/prepare_data.py \
    --data-dir data/raw/ \
    --output-dir data/processed/ \
    --vocab-size 1000 \
    --test-run

Full training data:

# Place your training text files in data/raw/
# Then run:
python scripts/prepare_data.py \
    --data-dir data/raw/ \
    --output-dir data/processed/ \
    --vocab-size 32000 \
    --max-seq-length 512

Training

Quick test run:

python scripts/train.py --config tiny --steps 100 --test-run

Full training:

python scripts/train.py --config small --data-dir data/processed/

Inference

python scripts/generate.py \
    --checkpoint data/checkpoints/best.pt \
    --tokenizer data/processed/tokenizer.json \
    --prompt "Once upon a time" \
    --max-tokens 50

Testing with Small Datasets

The project supports testing with small datasets:

  1. Quick Test Script: python scripts/quick_test.py - Runs all tests in ~2 minutes
  2. Test Mode in Data Prep: --test-run flag uses subset of data
  3. Tiny Model Config: 10M parameters for fast iteration
  4. Configurable Model Sizes: Choose based on your hardware

Recommended testing workflow:

  1. Run quick_test.py to verify installation
  2. Test data preparation with --test-run
  3. Train on tiny model for 100 steps
  4. Scale up to larger models once everything works

πŸ§ͺ Testing Strategy

The project is designed to test incrementally:

  1. Unit Tests: Each component has built-in tests

    python -m src.model.llm  # Test specific component
  2. Integration Test: Quick test suite verifies everything works together

    python scripts/quick_test.py  # ~2 minutes, complete test
  3. Small Dataset Tests: Verify with minimal data before committing to full training

    python scripts/prepare_data.py --test-run  # Test data pipeline
    python scripts/train.py --steps 100  # Test training loop
  4. Scale Up: Once tests pass, use full datasets and larger models

This approach ensures everything works before investing time in long training runs.

Implementation Status

βœ… Phase 1: Foundation (Complete)

  • Project structure and requirements
  • Configuration system (model_config.py, training_config.py)
  • Device utilities (vendor-agnostic accelerator management)
  • RMS Layer Normalization (modern normalization)
  • Rotary Position Embeddings (RoPE)
  • Token Embeddings (with tied embeddings)

βœ… Phase 2: Core Model (Complete)

  • Multi-head Attention (with RoPE and causal masking)
  • Feed-Forward Networks (SwiGLU activation)
  • Transformer Blocks (pre-normalization)
  • Main LLM Model (complete assembly)

βœ… Phase 3: Tokenization (Complete)

  • Vocabulary Management (special tokens, encoding/decoding)
  • BPE Tokenizer (full training and inference)
  • Data Preparation Script (tokenize and save training data)

βœ… Phase 4: Training Infrastructure (Complete)

  • Training Dataset (PyTorch Dataset wrapper)
  • Training Loop (complete with gradient accumulation)
  • Optimizer & Scheduler (AdamW with cosine schedule)
  • Checkpoint Manager (save/load training state)
  • Logger (TensorBoard + CSV logging)
  • Metrics (loss, perplexity, throughput)
  • Training Script (train.py with CLI)

βœ… Phase 5: Inference Infrastructure (Complete)

  • KV Cache (efficient autoregressive generation)
  • Text Generator (sampling strategies)
  • Interactive CLI (command-line interface)
  • Generation Script (generate.py with CLI)

βœ… Phase 6: Documentation (Complete)

  • Architecture Documentation (with diagrams)
  • Training Guide (step-by-step procedures)
  • Inference Guide (generation usage and examples)

Total Implementation: 100% Complete

Educational Resources

Documentation

  • docs/learning_path.md - Start here β€” 9-paper reading guide mapped to the source files
  • docs/architecture.md - Detailed architecture explanation with diagrams
  • docs/training_guide.md - Training procedures
  • docs/inference_guide.md - Inference and generation

Code Comments

Every module includes:

  • Educational notes explaining concepts
  • Implementation details
  • Usage examples
  • Mathematical intuition where applicable

Contributing

This is an educational project. Feel free to:

  • Study the code
  • Run experiments
  • Modify and test variations
  • Use for learning and teaching

License

Educational use - feel free to learn and experiment!

Hardware Requirements

Minimum

  • CPU: Any modern multi-core processor
  • RAM: 8GB
  • GPU: Not required (but training will be slow)

Recommended

  • GPU: NVIDIA RTX 4050 or better (6GB+ VRAM)
  • RAM: 16GB
  • Storage: 10GB free space

Performance Estimates

Training times on RTX 4050 (6GB VRAM):

  • 25M model: ~40 minutes per epoch
  • 50M model: ~70 minutes per epoch
  • 100M model: ~120 minutes per epoch

For CPU training, multiply by ~10-20x.

Acknowledgments

Architecture inspired by:

  • LLaMA (Meta)
  • Transformer (Google)
  • RoFormer

Built with PyTorch and designed for education.

Devices / GPU vendors (vendor-agnostic)

The code is not tied to any single GPU vendor. src/utils/device.py auto-detects the fastest available accelerator and falls back to CPU; the train.py and generate.py scripts also accept --backend to force one.

CPU support ships with every PyTorch build. For a GPU, install one matching PyTorch wheel (use the official install picker):

Backend Hardware Install
cuda NVIDIA GPU (fastest; enables AMP mixed-precision) pip install torch --index-url https://download.pytorch.org/whl/cu124
cuda (ROCm) AMD GPU (reported as CUDA) pip install torch --index-url https://download.pytorch.org/whl/rocm6.1
xpu Intel GPU (oneAPI) pip install torch --index-url https://download.pytorch.org/whl/xpu
mps Apple Silicon (Metal) Included in the default macOS wheel
directml Any DirectX 12 GPU on Windows (AMD/Intel/NVIDIA) pip install torch-directml
cpu Any CPU (always-available fallback) pip install torch

CUDA/ROCm/XPU versions above are examples β€” pick the one matching your driver. Run python -m src.utils.device to print detected devices and the exact install command for any missing backend. There is no Vulkan backend (PyTorch's is experimental/inference-only; use DirectML on Windows instead).

# Auto-detect any available GPU (default)
python scripts/train.py --config small --data-dir data/processed/
python scripts/generate.py --checkpoint ... --tokenizer ... --prompt "Hi"

# Force a specific vendor/backend
python scripts/train.py --backend directml --config small --data-dir data/processed/
python scripts/generate.py --backend directml --checkpoint ... --tokenizer ... --prompt "Hi"

Further reading: PyTorch install picker Β· DirectML + PyTorch Β· MPS notes Β· XPU notes

References

The implementation follows established papers and standards. Each concept links to its source paper and a general reference (encyclopedia) entry.

Architecture

Tokenization

Training

Inference / decoding

Compute backends

About

Large Language Model implementation for educational purpose

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages