liveBook · Manning

Chapter 4: Improving reasoning with inference-time scaling

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

The chapter introduction establishes that reasoning accuracy can improve during text generation without retraining or modifying the model.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction establishes that reasoning accuracy can improve during text generation without retraining or modifying the model.

Key terms: inference-time scaling, inference

4.1 Introduction to inference-time scaling

The section defines inference-time scaling and introduces three ways to spend more computation after training to improve reasoning responses.

Key terms: training compute, inference-time scaling, test-time scaling, inference compute, chain-of-thought response, parallel sampling, self-consistency, iterative self-refinement

4.2 Loading a pretrained model

The section sets up a pretrained model and a flexible generation wrapper for comparing inference-time strategies, then shows that the baseline answer to a MATH-500 prompt is wrong.

Key terms: generate_text_stream_concat_flex, generate_text_stream_concat, generate_text_basic_stream_cache, MATH-500, pretrained base model, cpu, GPU

4.3 Generating better responses with chain-of-thought prompting

The section shows how a step-by-step prompt can improve a base model’s response, why it may help, and when its costs or limitations matter.

Key terms: chain-of-thought prompting, chain of thought, reasoning chain, intermediate steps, training examples, existing knowledge, inference-time-scaling tradeoff, tokens, forward pass, latency

4.4 Controlling output diversity with temperature scaling

This section contrasts sequential chain-of-thought prompting with independently generated answers and explains why self-consistency needs diverse responses to the same prompt.

Key terms: chain-of-thought prompting, sequential technique, parallel sampling, self-consistency sampling, text generation function, temperature scaling, top-p filtering, output diversity

4.4.1 Understanding the process of selecting the next token

This section traces how a model selects the next token from vocabulary-wide scores and illustrates those scores with an example.

Key terms: token IDs, logits, [:, -1], torch.argmax, vocabulary size, next_token_logits

4.4.2 Rescaling token scores (logits) via a temperature parameter

This subsection explains how dividing next-token logits by a positive temperature changes the differences between scores and, in turn, the likelihood of selecting different tokens.

Key terms: temperature parameter, logits, temperature scaling, sharper, flatter, probability gap, lower-ranked tokens

4.4.3 Sampling the next token from a probability distribution

The section shows how softmax turns temperature-scaled logits into token probabilities and how sampling from those probabilities produces varied next-token choices.

Key terms: softmax, logits, probability distribution, torch.softmax, multinomial sampling, greedy decoding, torch.multinomial, count_samples, temperature, sampling diversity

4.4.4 Adding temperature scaling to the text generation function

The section adds temperature scaling and sampling to a cached text-generation function and explains how to choose a temperature for different goals.

Key terms: generate_text_temp_stream_cache, temperature rescaling, sampling, token-generation loop, generate_text_stream_concat_flex, temperature, greedy decoding, diversity, reliability

4.5 Balancing diversity and coherence with top-p sampling

Introduces top-p sampling as a way to limit unlikely tokens while retaining sampling diversity, and outlines the filtering steps.

Key terms: top-p filter, nucleus sampling, temperature scaling, token probabilities, cumulative sum, top-p cutoff, renormalizing

4.5.1 Selecting a subset of top-p tokens

This section shows how top-p filtering selects a probability-based subset of next tokens, then prepares that subset for sampling.

Key terms: token probabilities, descending order, cumulative sum, torch.cumsum, top-p, top_p, keep_mask, cumulative probability mass, threshold, top-p filtering

4.5.2 Adding a top-p filter to the text generation function

This section adds top-p filtering to temperature-scaled text generation, shows how it changes sampled outputs, and contrasts it with top-k filtering.

Key terms: top_p_filter, prefix cumulative mass, top_p threshold, renormalizes, temperature scaling, top-p filter, generate_text_top_p_stream_cache, top-k filtering, top-p, cumulative mass

4.6 Improving response accuracy with self-consistency

This section implements self-consistency voting and examines how sampling choices affect its accuracy, compute cost, and applicability.

Key terms: self-consistency sampling, temperature scaling, top-p filtering, final boxed answer, majority voting, inference-time scaling, temperature, top-p, long answers, diversity

4.7 Summary

The chapter summary connects prompting and sampling techniques to the central inference-time scaling tradeoff: better answers can require more compute without retraining.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Evaluating reasoning models Chapter 5: Inference-time scaling via self-refinement →

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.