Build a large language model from scratch, including pretraining, instruction fine-tuning, and RLHF reward modeling.
tokenizer.py— Simple tokenizer implementationmodels/gpt.py— GPT model, TransformerBase (shared base for LM and reward models)models/rewards.py— BradleyTerry reward modeldataset/gutenberg_dataset.py— Sliding window dataset for pretraining (no padding; every sample is exactly context_length tokens)dataset/instructionft_dataset.py— Instruction fine-tuning dataset with variable-length sequences, padded to batch max length using token 50256 (<|endoftext|>)inference.py— Training loops, inference, save/load utilities
TransformerBase: token embedding + positional embedding + dropout + transformer blocks. Shared base for all model heads.GPT/LanguageModel: TransformerBase + LayerNorm + Linear(dim, vocab_size). Generative, predicts next token.BradleyTerry: TransformerBase + Linear(dim, 1). Discriminative, outputs a scalar reward per sequence.
- Input:
x + y + <eos>(prompt + completion + end token), padded with 50256 to the longest sequence in the batch. - The reward is read from the hidden state at the last real token position (the
<eos>), identified using an attention mask. - No LayerNorm before the reward head (unlike the LM head) — standard practice.
- Reward models are discriminative: they score a sequence rather than generating tokens. Used in RLHF to elicit qualities like helpfulness and harmlessness.
- Trained on preference pairs (chosen, rejected).
- Loss:
-log(sigmoid(reward_chosen - reward_rejected)) - No target tensors or
-100masking needed — only the reward scalars are compared.
- Input tokens are padded with
50256(<|endoftext|>). - Target tokens in SFT use
-100as the ignore index forCrossEntropyLoss(pads beyond the first<eos>are masked out). -100is never used in input tokens.
- For preference reward model training, using https://huggingface.co/datasets/Anthropic/hh-rlhf
- Python 3.x
- Conda (Miniforge recommended for Apple Silicon)
~/miniforge3/bin/conda run -n ml-env pip install -r requirements.txt