liveBook · Manning

Chapter 2: Generating text with a pretrained LLM

What you will learn in chapter 2 of Build a Reasoning Model (From Scratch)

The chapter establishes why it starts with a pretrained base LLM before adding reasoning techniques in later chapters.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter establishes why it starts with a pretrained base LLM before adding reasoning techniques in later chapters.

Key terms: conventional LLM, pretrained base model, nonreasoning LLM, reasoning methods

2.1 Introducing LLMs for text generation

This section introduces the pretrained base model used for text generation and explains how to read the chapter’s LLM diagrams.

Key terms: base model, pretraining, coherent text, basic instructions, inputs, outputs, flow of information

2.2 Setting up the coding environment

This section explains how to prepare a Python environment and run the book’s code using either pip or uv.

Key terms: Python 3.10, pip, requirements.txt, PyTorch, uv, dependency resolution, isolated environments, pyproject.toml, .venv/, GitHub repo

2.3 Understanding hardware needs and recommendations

The section explains why the book uses a smaller pretrained LLM and how readers can choose between CPU, local GPU, and cloud resources for its exercises.

Key terms: base-model LLM, pretrained LLM, reasoning techniques, CPU, GPU, PyTorch, PyTorch-supported GPU, cloud resources, Lightning AI Studio, Google Colab

2.4 Preparing input texts for LLMs

This section shows how a tokenizer converts text to and from token IDs, then explains subword tokenization and the tradeoff between vocabulary size and sequence length.

Key terms: tokenizer, token IDs, encode, decode, tokenization round trip, byte pair encoding (BPE), subword, token, word boundaries, vocabulary

2.5 Loading pretrained models

The section explains why the book uses Qwen3 0.6B and how to download its pretrained weights, load them into a local PyTorch model, and place the model on a device.

Key terms: Qwen3 0.6B, open-weight model, base model, reasoning variant, custom reimplementation, pure PyTorch, pretrained weights, locally, device, CPU

2.6 Understanding the sequential LLM text generation process

The section explains how an LLM generates text one token at a time by selecting and decoding a next token from its output scores.

Key terms: autoregressive text generation, token, next-token prediction, forward pass, input position, vocabulary size, last position, score matrix, argmax, max

2.7 Coding a minimal text generation function

The section implements basic sequential text generation with a pretrained LLM, adds end-of-sequence stopping and speed measurement, and clarifies what inference means in this context.

Key terms: generate_text_basic_stream, argmax, token IDs, max_new_tokens, <|endoftext|>, tokenizer.eos_token_id, end-of-sequence token, generate_stats, tokens per second, runtime

2.8 Faster inference via KV caching

This section explains how KV caching avoids repeated work during text generation, shows how to use it in the generation function, and compares its speed with the uncached version.

Key terms: KV caching, keys and values, attention mechanism, intermediate representations, generate_text_basic_stream_cache, KVCache, token_ids, next_token, O(n2), O(n)

2.9 Faster inference via PyTorch model compilation

This section explains how PyTorch compilation can speed up text generation, compares its measured effect with and without KV caching, and explains limits of the hardware benchmarks.

Key terms: torch.compile, computation graph, optimized kernels, Python overhead, initial compilation, tokens per second, KV cache, noncached, K and V tensors, preallocation

2.10 Summary

Summarizes the components of a pretrained-LLM text generation pipeline, its token-by-token operation, and two ways to improve generation speed.

Where this is used later

Concepts explained in this chapter

← Chapter 1: Understanding reasoning models Chapter 3: Evaluating reasoning models →

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.