multi-head attention mechanism with causal masking for autoregressive generation
Implements scaled dot-product attention using Query/Key/Value linear projections (W_query, W_key, W_value) with causal masking to prevent attending to future tokens. The mechanism splits embeddings across multiple heads, computes attention scores via matrix multiplication (queries @ keys.transpose), applies a triangular mask buffer registered in __init__, and projects concatenated head outputs through out_proj. This enables parallel attention computation across sequence positions while maintaining autoregressive constraints required for token-by-token generation.
Unique: Provides pedagogically clear, step-by-step attention implementation with explicit mask buffer registration and head concatenation, making the mechanism's mechanics transparent rather than abstracted behind framework utilities. Includes visualization-friendly attention weight extraction for debugging.
vs alternatives: More interpretable than PyTorch's native scaled_dot_product_attention (which optimizes for speed) because it exposes each computation step, making it ideal for learning but ~15-20% slower for production inference.
gpt architecture scaling from 124m to 1558m parameters via configuration dictionary
Implements a modular GPTModel class that accepts a configuration dictionary specifying embedding dimension, number of layers, attention heads, and feed-forward width. The architecture stacks transformer blocks (each containing multi-head attention, layer normalization, and feed-forward networks) with token and positional embeddings, then projects to vocabulary logits. The configuration pattern allows instantiation of model variants (GPT-small, GPT-medium, GPT-large) by changing dict values rather than code, enabling systematic scaling studies and transfer learning experiments.
Unique: Uses explicit configuration dictionaries rather than dataclass configs or factory functions, making model variants immediately visible as data structures. Includes pre-defined configs for GPT2-small, GPT2-medium, GPT2-large that match OpenAI's published parameter counts, enabling direct weight loading from official checkpoints.
vs alternatives: More transparent than HuggingFace Transformers' AutoModel factory pattern because hyperparameters are visible as Python dicts rather than hidden in JSON configs, but requires manual weight conversion from HF format.
positional encoding via absolute position embeddings for sequence position awareness
Adds learnable or fixed positional embeddings to token embeddings to encode sequence positions, enabling the model to distinguish between tokens at different positions. The implementation creates a position embedding matrix (context_length, embedding_dim) and adds it element-wise to token embeddings before passing to transformer blocks. This allows attention mechanisms to incorporate position information, critical for understanding word order in language.
Unique: Implements positional embeddings as a learnable parameter matrix added to token embeddings, making the encoding mechanism transparent. Includes utilities to visualize position embedding patterns and to analyze how positions are represented in the embedding space.
vs alternatives: More interpretable than rotary embeddings (RoPE) because position information is explicit in embedding space; less effective for long sequences because absolute positions don't generalize beyond training context length.
batch data loading with sliding window context for efficient sequence packing
Creates training batches by sliding a fixed-size window over tokenized text, generating overlapping sequences that maximize data utilization. The implementation reads tokenized text, creates sliding windows of context_length, groups windows into batches, and yields (input, target) pairs where targets are inputs shifted by one position. This approach reduces memory overhead compared to padding variable-length sequences and ensures all tokens contribute to training.
Unique: Implements sliding window batching with explicit overlap handling and target sequence creation (shifted inputs), making data preparation transparent. Includes utilities to visualize batch composition and to analyze token distribution across batches.
vs alternatives: More efficient than padding variable-length sequences because it eliminates padding overhead; less flexible than HuggingFace datasets because it requires pre-tokenized data and doesn't support on-the-fly tokenization.
model evaluation via perplexity and loss metrics on validation sets
Evaluates model quality by computing perplexity (exp(loss)) and cross-entropy loss on held-out validation data. The implementation runs the model in evaluation mode (disabling dropout), computes loss without gradient computation, and aggregates metrics across batches. Perplexity measures how well the model predicts validation tokens — lower is better, with perplexity=1 indicating perfect predictions.
Unique: Implements evaluation with explicit loss computation and perplexity calculation, making model quality assessment transparent. Includes utilities to compute confidence intervals and to visualize loss curves across validation batches.
vs alternatives: More interpretable than black-box evaluation frameworks because metrics are computed explicitly; lacks task-specific metrics like BLEU or ROUGE, requiring external evaluation for generation quality.
byte-pair encoding (bpe) tokenization with vocabulary merging
Implements BPE tokenization by iteratively merging the most frequent adjacent token pairs in a corpus, building a vocabulary of subword units. The algorithm tracks pair frequencies, applies merges in order, and encodes text by greedily matching longest subword sequences. This approach reduces vocabulary size compared to character-level tokenization while maintaining semantic meaning, enabling efficient representation of rare words through composition.
Unique: Provides step-by-step BPE implementation with explicit pair frequency tracking and merge visualization, making the algorithm's behavior transparent. Includes utilities to inspect which subword boundaries are created at each merge step, useful for debugging tokenization issues.
vs alternatives: More educational than using tiktoken or SentencePiece directly because it exposes the merge algorithm; slower than optimized C++ implementations but sufficient for corpora <1GB and ideal for understanding tokenization mechanics.
causal language modeling pretraining with next-token prediction loss
Implements a training loop that predicts the next token given preceding context by computing cross-entropy loss between model logits and ground-truth next tokens. The loop iterates over batches, performs forward passes through the GPT model, computes loss on shifted token sequences (input tokens predict next tokens), backpropagates gradients, and updates weights via optimizer steps. This approach trains the model to learn conditional probability distributions P(token_t | tokens_0..t-1), the foundation of autoregressive generation.
Unique: Implements training with explicit loss computation on shifted sequences (input[:-1] predicts target[1:]), making the causal prediction objective transparent. Includes detailed logging of loss curves and validation metrics, enabling visual inspection of training dynamics.
vs alternatives: More interpretable than Hugging Face Trainer because loss computation is explicit and modifiable; slower due to lack of distributed training and gradient accumulation, but suitable for educational purposes and small-scale experiments.
instruction fine-tuning with supervised learning on task-specific examples
Adapts a pretrained language model to follow instructions by fine-tuning on curated instruction-response pairs. The approach computes loss only on response tokens (not instruction tokens), using a mask to zero out instruction loss. This trains the model to generate appropriate responses given task descriptions, shifting from next-token prediction to instruction-following behavior. The implementation supports both full-parameter fine-tuning and parameter-efficient variants.
Unique: Implements response-only loss masking by explicitly zeroing instruction token gradients, making the fine-tuning objective clear. Includes utilities to visualize which tokens contribute to loss, helping debug instruction-response boundary issues.
vs alternatives: More transparent than HuggingFace's trainer because loss masking is explicit and modifiable; requires manual implementation of evaluation metrics unlike AutoTrain, but enables fine-grained control over training dynamics.
+5 more capabilities