Tools, Notes and Ideas for Building a LLM

Tools, notes and ideas I collect while learning to build a large language model.

Foundations

Reference implementations

  • LLMs-from-scratch (Sebastian Raschka): the official code for the book, with extra bonus chapters.
  • nanoGPT and nanochat (Andrej Karpathy): leaner training pipelines, good for seeing a full pretraining run end to end.
  • LitGPT (Lightning AI): from-scratch implementations of common open models, with finetuning scripts.

Data

  • Hugging Face datasets and tokenizers: load corpora and train a custom tokenizer.
  • SentencePiece (Google): an unsupervised text tokenizer, another way to train your own vocabulary.
  • FineWeb: a large web-text corpus for pretraining, and a paper on how it was filtered and deduplicated.
  • datatrove (Hugging Face): a library for filtering, deduplicating and tokenizing text at scale.

Training method

Training efficiently

  • Mixed precision. bf16 needs a GPU with compute capability 8.0 or higher (Ampere and later). Turing cards such as the T1000 (7.5) use fp16 with gradient scaling, see the PyTorch AMP docs.
  • torch.compile. It can speed up training. It relies on Triton, which has no official Windows build, so on Windows it needs community-built wheels.
  • Gradient accumulation. Simulates a large batch when GPU memory is too small to hold one. The linked post walks through finetuning an LLM on a single GPU with it.

Distributed training

  • FSDP (PyTorch): shards parameters, gradients and optimizer states across GPUs.
  • DeepSpeed: a library for distributed training and inference. It matters once the model no longer fits on one GPU.

Watching and debugging

Post-training

  • LoRA and QLoRA: low-rank adapters, and the same idea on a quantized base model, for finetuning with little memory.
  • PEFT: the Hugging Face library for parameter-efficient finetuning.
  • TRL: Hugging Face library to train language models with reinforcement learning, including supervised finetuning and preference optimization.

Evaluation

  • lm-evaluation-harness: the standard benchmark runner.
  • Ollama: in chapter 7 of the book it runs the judge model that scores instruction-following answers. On a small GPU, a smaller judge model is the practical choice.

Inference and deployment

  • llama.cpp: LLM inference in C/C++, runs quantized models on modest hardware.
  • vLLM: a high-throughput, memory-efficient inference and serving engine.
  • GPTQ: post-training quantization for generative pretrained transformers.

Learning resources