liveBook · Manning

Chapter 5: Training large language models: How to generate the generator

What you will learn in chapter 5 of LLMs in Production

The chapter introduces training an LLM on a prepared dataset and previews the environment, training methods, and practical advice it will cover.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces training an LLM on a prepared dataset and previews the environment, training methods, and practical advice it will cover.

Key terms: training dataset, Slack messages, persona, training environment, resource-demanding, training from scratch, finetuning, advanced methodologies, tips and tricks

5.1 Multi-GPU environments

This section explains why training and working with large LLMs calls for multi-GPU environments and introduces cloud access and supporting tools as the next steps.

Key terms: multi-GPU environment, inference, training, parallelize, cloud, tools and libraries, playground

5.1.1 Setting up

This section walks through setting up, accessing, and deleting a paid multi-GPU Google Cloud VM for LLM work.

Key terms: multi-GPU, Google Cloud Project, billing, gcloud CLI, gpus_all_regions quota, g2-standard-24, NVIDIA L4 GPU, region, zone, deep-learning image

5.1.2 Libraries

The section compares four libraries for multi-GPU LLM work and explains why compatible software environments matter as much as correctly configured hardware.

Key terms: CUDA Toolkit, PyTorch, multi-GPU instances, DeepSpeed, Accelerate, distributed deep learning, parallelizing, BitsandBytes, xFormers, quantization

5.2 Basic training techniques

This section introduces the choices, resource needs, and staged life cycle behind basic LLM training techniques.

Key terms: Transformer architecture, diverse and representative text data, predict the next word, VRAM, batch sizes, context length, quantization, foundation model, transfer learning, finetuning

5.2.1 From scratch

This section walks through building and training a language model from scratch, then uses a small character-based example to show both the training loop and the limits of its tokenizer.

Key terms: from scratch, architecture, Transformer, dataset, tokenized, randomly initialized, autoregressive, batches, epochs, loss

5.2.2 Transfer learning (finetuning)

This section explains how finetuning adapts a pretrained LLM to a specific task, then illustrates the approach with GPT-2 and OpenAI’s finetuning API.

Key terms: transfer learning, source domain, target domain, pretraining, finetuning, pretrained model, preprocessed, tokenized, learning rate, separate dataset

5.2.3 Prompting

This section introduces prompting as a way to guide an LLM at run time and explains why crafting the prompt matters.

Key terms: prompt, prompting, run time, prompt engineering, model’s capabilities and limitations

5.3 Advanced training techniques

Introduces why production LLMs may need different advanced training techniques and reviews four classical learning paradigms used to understand them.

Key terms: advanced training techniques, production-ready model, supervised learning, unsupervised learning, adversarial learning, Critic model, Forger model, reinforcement learning

5.3.1 Prompt tuning

This section explains how prompt tuning supplies task context during training to improve performance while reducing the need to finetune a large language model.

Key terms: prompt tuning, pragmatic context, expected task, frozen language model, converge, finetuning, BLOOMZ, Parameter-Efficient Fine-Tuning, PEFT, prepending a prompt

5.3.2 Finetuning with knowledge distillation

This section explains how knowledge distillation trains a smaller LLM to imitate a larger one, trading some performance for substantial savings in size and computation.

Key terms: knowledge distillation, teacher model, student model, soft targets, loss function, compact student model, computational cost, finetuned, RLHF, human feedback

5.3.3 Reinforcement learning with human feedback

This section explains how human preferences can guide reinforcement learning for LLMs, then weighs RLHF’s benefits against its costs and production risks.

Key terms: reward system, intermediate rewards, Pylons, reinforcement learning with human feedback, RLHF, reward model, proximal policy optimization, PPO, human-acceptable answers, domain experts

5.3.4 Mixture of experts

This section explains how a mixture of experts uses sparse routing to retain specialization while reducing computation, then describes finetuning an existing Switch Transformer model.

Key terms: mixture of experts, MoE, sparsity, experts, routing algorithm, representative-enough dataset, tokenized input, computational requirements, specialization, Switch Transformer

5.3.5 LoRA and PEFT

This section explains how LoRA uses small, low-rank adapters to adapt a foundation model for new tasks or domains without storing a separately finetuned copy of the whole model.

Key terms: LoRA, ΔW, Wa, Wb, rank, R, c-value, LoraConfig, foundation model, base model

5.4 Training tips and tricks

This introduction explains why the section offers practical, hard-won tips for training and finetuning LLMs.

Key terms: finetuning, guardrails, training

5.4.1 Training data size notes

This section gives rough data-size checks for training a foundation model or finetuning an existing LLM, and warns against expecting strong results from too little data.

Key terms: foundation model, parameters, tokens, overfitting, finetune, 10K tokens, LoRA, RAG

5.4.2 Efficient training

This section explains how training-loop choices trade speed against memory use and why those choices must fit production constraints.

Key terms: speed, memory utilization, gradient accumulating, checkpointing, batch size, power of 2, production environment, training loop

5.4.3 Local minima traps

This section explains how to recognize suspicious early convergence during LLM training and use checkpoints and learning-rate changes to get past a local minimum.

Key terms: local minima, converging early, full dataset, checkpoint, learning rate, local minimum

5.4.4 Hyperparameter tuning tips

This section advises prioritizing better data and model optimizations over hyperparameter tuning, while noting when tuning may still be useful.

Key terms: hyperparameter tuning, accuracy, F1 score, oversampling, Optuna, quantizing the weights, F1

5.4.5 A note on operating systems

This section recommends learning Linux for professional LLM work despite its difficult setup, and identifies the tools that make that work practical.

Key terms: Windows Subsystem for Linux, macOS, Linux, Bash, command line, CUDA Toolkit, Nvidia drivers, virtual environments

5.4.6 Activation function advice

The section advises against casually changing a pretrained LLM’s activation function and points from model training toward production serving.

Key terms: activation functions, GPT-2, GELU, GeGLU, pretraining, finetuning, production-ready, LLM service, online inference

5.5 Summary

The chapter summary recaps the training workflow, the roles of data and adaptation techniques, and the resource tradeoffs involved in training LLMs.

Where this is used later

Concepts explained in this chapter

← Chapter 4: Data engineering for large language models: Setting up for success Chapter 6: Large language model services: A practical guide →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.