Inference mode disables gradient tracking and other training-time behavior during model execution, so generation can run with lower memory use and usually better speed. section 2.6
It appears in the text-generation workflow when the model is used only to produce the next token, not to support a backward pass. The same setting is later contrasted with gradient-enabled scoring code, where inference-only execution becomes a limitation. section 2.6 section 6.5
Typical uses shown here:
- Wrap forward passes for next-token generation. section 2.6
- Generate rollouts efficiently before any training step. section 6.5
- Avoid storing gradient information that generation does not need. section 2.6
Generation call with inference mode
A prompt is tokenized, reshaped with unsqueeze(0), passed through the model inside torch.inference_mode(), then squeezed back to a 2D output tensor for token selection. section 2.6
Figure 2.11 The LLM processes the full input sequence and produces one next-token prediction per input position. These predictions are conceptually shifted one step ahead because each position predicts the following token. The illustration is position aligned for intuition, but the outputs are not literally the input prompt shifted by one position. During generation, we keep only the final prediction and use it to continue the input prompt one token at a time.
Use this to see why only the final position matters during generation and how inference mode fits that step. section 2.6
