Overview

Appendix B. References and Further Reading

This appendix curates a broad map of the LLM landscape, from the foundational ideas behind transformers to practical resources for building, training, and adapting models. It highlights the rise of decoder-style GPT models, the continued relevance of encoder-style transformers for classification, and the extension of transformer ideas beyond text to vision. It also showcases how domain-specific pretraining and task-focused finetuning can surpass general-purpose models, while noting emerging non-transformer architectures that challenge the status quo. Overall, it serves as a guide to both the conceptual pillars and the hands-on tools needed to construct and refine large language models from scratch.

On the architectural side, the references trace the lineage from scaled dot-product attention to modern GPT systems, alongside BERT and the vision transformer, and include alternatives such as RWKV, Hyena, and state-space models like Mamba. Efficiency and stability are recurring themes, with entries on FlashAttention, PyTorch attention primitives, dropout, and normalization strategies (LayerNorm, Pre-LN vs. Post-LN, RMSNorm), as well as GELU activations. The appendix also points to tokenizer design and tooling (BPE, SentencePiece, WordPiece), scaling insights from GPT-2 to GPT-3, and instruction tuning with human feedback. Collectively, these works ground the reader in the core components and trade-offs that underpin modern LLMs.

On the practical front, the appendix aggregates resources for datasets, training curricula, and evaluation. It spans large open corpora and suites for analyzing scaling (Dolma, The Pile, RefinedWeb, RedPajama, FineWeb, Pythia, OLMo), data preparation pipelines (for example, converting public-domain books for pretraining), and training strategies such as warmup, cosine annealing, continued pretraining, and memory-efficient optimization via low-rank gradient projection (GaLore). Decoding and inference references cover methods like top-k sampling and beam search. Finetuning materials include tutorials and experiments on classification with decoder and encoder models, handling class imbalance, layer-freezing choices, and modifications that improve classification (e.g., removing causal masks and deriving powerful text embeddings). Case studies in finance and medicine illustrate the gains from domain adaptation and task-specific tuning, rounding out a practical toolkit for building and customizing LLMs.

Chapter 7

The Alpaca dataset for instruction finetuning contains 52 thousand instruction-response pairs and is one of the first and most popular publicly available datasets for instruction finetuning:

Listed below are additional publicly accessible datasets suitable for instruction finetuning:

Phi-3 is a 3.8B parameter model with an instruction-finetuned variant that is reported to be comparable to much larger proprietary models, such as GPT-3.5:

Researchers propose a synthetic instruction data generation method that generates 300,000 high-quality instruction-response pairs from an instruction-finetuned Llama-3 model. A pretrained Llama 3 base model finetuned on these instruction examples performs comparably to the original instruction-finetuned Llama-3 model:

Research has shown that not masking the instructions and inputs in instruction finetuning effectively improves performance on various NLP tasks and open-ended generation benchmarks, particularly when trained on datasets with lengthy instructions and brief outputs or when using a small number of training examples:

Prometheus and PHUDGE are openly available LLMs that match GPT-4 in evaluating long-form responses with customizable criteria. We haven't used these in chapter 7 because they are not yet supported by Ollama, and thus cannot be executed efficiently on a laptop.

The results in the following report support the view that large language models primarily acquire factual knowledge during pretraining and that finetuning mainly enhances their efficiency in using this knowledge. Furthermore, this study explores how finetuning large language models with new factual information affects their ability to use preexisting knowledge, revealing that models learn new facts more slowly and their introduction during finetuning increases the model's tendency to generate incorrect information:

Preference finetuning is an optional step after instruction finetuning to align the LLM more closely with human preferences. The following articles by the author provide more information about this process:

FAQ

What are the foundational papers I should read to understand transformers and LLMs?Start with the core milestones: Attention Is All You Need (Vaswani et al., 2017) introducing the Transformer (https://arxiv.org/abs/1706.03762); BERT (Devlin et al., 2018) for encoder-style transformers (https://arxiv.org/abs/1810.04805); GPT-3: Language Models are Few-Shot Learners (Brown et al.) for decoder-style LLMs (https://arxiv.org/abs/2005.14165); and the Vision Transformer showing transformers beyond text (https://arxiv.org/abs/2010.11929).
Can custom or fine-tuned LLMs outperform general-purpose models?Yes. BloombergGPT, trained from scratch on finance data, outperformed general models on finance tasks while staying strong on generic benchmarks (https://arxiv.org/abs/2303.17564). In the medical domain, fine-tuned models by Google Research/DeepMind achieved expert-level medical QA (https://arxiv.org/abs/2305.09617).
Are there viable alternatives to transformer-based LLMs?Several experimental architectures show promise: RWKV (RNN-style, https://arxiv.org/abs/2305.13048); Hyena Hierarchy (convolution-based, https://arxiv.org/abs/2302.10866); and Mamba (state-space models, https://arxiv.org/abs/2312.00752). These illustrate that not all high-performing sequence models need to be transformers.
How do LLM tokenizers work, and where can I learn or try them?For BPE, see Sennrich et al. (https://arxiv.org/abs/1508.07909); OpenAI’s GPT-2 tokenizer code (https://github.com/openai/gpt-2/blob/master/src/encoder.py); interactive GPT tokenizer UI (https://platform.openai.com/tokenizer); and Karpathy’s minimal BPE implementation (https://github.com/karpathy/minbpe). Alternatives: SentencePiece (https://aclanthology.org/D18-2012/) and Fast WordPiece (https://arxiv.org/abs/2012.15524).
What should I read to understand attention and efficient variants like FlashAttention?For sequence-to-sequence attention, see Bahdanau et al. (https://arxiv.org/abs/1409.0473). Scaled dot-product self-attention originates in Attention Is All You Need (https://arxiv.org/abs/1706.03762). Efficient implementations: FlashAttention v1 (https://arxiv.org/abs/2205.14135) and v2 (https://arxiv.org/abs/2307.08691), plus PyTorch’s scaled_dot_product_attention (https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) and MultiheadAttention (https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html).
Which normalization and activation choices are recommended for modern LLMs?LayerNorm is foundational (https://arxiv.org/abs/1607.06450). Pre-LN variants tend to stabilize training (https://arxiv.org/abs/2002.04745; https://arxiv.org/abs/2304.14802). RMSNorm offers efficiency (https://arxiv.org/abs/1910.07467). GELU is a common activation in transformers (https://arxiv.org/abs/1606.08415). See also block simplifications (https://arxiv.org/abs/2311.01906).
What are common decoding strategies like top-k sampling and beam search?Top-k sampling was popularized for open-ended generation by Fan et al. (https://arxiv.org/abs/1805.04833). Beam search and its diversity-enhanced variants are covered by Vijayakumar et al. (https://arxiv.org/abs/1610.02424). These methods trade off diversity, quality, and computation at inference time.
Where can I find large-scale pretraining datasets and guidance for preparing my own?Open corpora include Dolma (https://arxiv.org/abs/2402.00159), The Pile (https://arxiv.org/abs/2101.00027), RefinedWeb (https://arxiv.org/abs/2306.01116), RedPajama (https://github.com/togethercomputer/RedPajama-Data), and FineWeb (https://huggingface.co/datasets/HuggingFaceFW/fineweb). For a worked example of preparing Project Gutenberg books, see the book’s supplemental repo (https://github.com/rasbt/LLMs-from-scratch/tree/main/ch05/03_bonus_pretraining_on_gutenberg).
Which papers cover training recipes, scaling studies, and efficiency for (continual) pretraining?Pythia analyzes LLMs across training and scaling (https://arxiv.org/abs/2304.01373). OLMo documents open training practices (https://arxiv.org/abs/2402.00838). For continual pretraining, see Simple and Scalable Strategies (https://arxiv.org/abs/2403.08763). For memory-efficient training via low-rank gradient projection, see GaLore (paper: https://arxiv.org/abs/2403.03507; code: https://github.com/jiaweizzhao/GaLore).
How can I finetune LLMs for downstream tasks like classification?Guides and experiments: Using and Finetuning Pretrained Transformers (https://magazine.sebastianraschka.com/p/using-and-finetuning-pretrained-transformers), Finetuning Large Language Models (https://magazine.sebastianraschka.com/p/finetuning-large-language-models), additional spam experiments (https://github.com/rasbt/LLMs-from-scratch/tree/main/ch06/02_bonus_additional-experiments), and a loss-function primer (https://sebastianraschka.com/blog/2022/losses-learned-part1.html). For data and class imbalance: imbalanced-learn (https://imbalanced-learn.org/stable/user_guide.html) and an email spam dataset (https://huggingface.co/datasets/TrainingDataPro/email-spam-classification). Encoder models like BERT (https://arxiv.org/abs/1810.04805) and RoBERTa (https://arxiv.org/abs/1907.11692) are strong for classification. Recent improvements include removing the causal mask or using LLM embeddings: Label Supervised LLaMA Finetuning (https://arxiv.org/abs/2310.01208) and LLM2Vec (https://arxiv.org/abs/2404.05961).
What open models and minimal codebases can I study to learn the GPT-style architecture?Meta’s Llama 2 is a widely used open GPT-like model (https://arxiv.org/abs/2307.09288). For a compact, educational GPT codebase, see Karpathy’s nanoGPT (https://github.com/karpathy/nanoGPT), which complements the implementations and explanations in the book.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Large Language Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Large Language Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Large Language Model (From Scratch) ebook for free