Lecture 20: Inference

Course theme: AI — Module: GPT & Language Models — Target audience: interested adults

Focus points: What happens during an answer? Why doesn't a model "think" like a human? Which factors influence the answer? Why does inference take varying amounts of time?

Introduction and Learning Objectives

This session explains what technically and conceptually happens during inference — i.e., when a language model generates an answer at runtime. We ask to what extent model behavior resembles or differs from human thinking, which internal and external factors influence the formulation of the answer, and why response times vary. The presentation is based on established scholarly publications and technical reports; uncertainties and open points are marked.

Part 1 (20 minutes): Basic Understanding — What Happens During an Answer?

Think of the inference of a large language model simplified as a very advanced "autocomplete": during training the model learned to predict a probability distribution over possible next tokens for a given input (prompt plus tokens already generated). At inference time this prediction is used to select a next token; that token is appended and the process repeats until an endpoint is reached. This basic mode of operation is described in the major language model publications as an autoregressive prediction principle (Brown et al., 2020).

Technically, for each token generation the current input passes through a chain of the model's layers that rely on weighted transformation steps. In transformers this occurs in the form of self-attention and feedforward blocks (Vaswani et al., 2017). Although the layer structure can be applied in parallel to components of the input, token generation itself remains fundamentally sequential: each next token builds on the sequence generated so far (Brown et al., 2020).

What image helps to understand this? A useful analogy is an orchestra with a score-generator: the model has observed patterns during training (similar to a composer studying many scores). During inference it proposes a distribution for the next sound (token); the actual choice then corresponds either to a deterministic decision (e.g., the loudest instrument) or a sample from that distribution (e.g., different plausible interpretations). If sampling is used, variability in answers arises even for the same prompt (Holtzman et al., 2019).

Important: The model does not store explicit, symbolic facts or "opinions" in the human sense; its internal weights encode statistical regularities of the training data. The generated answers are the result of this weighted pattern recognition and probability prediction (Bommasani et al., 2021).

Part 2 (20 minutes): Deeper Insight and Key Terms

Autoregressive prediction model: The model is trained to predict the next token conditioned on all previous tokens. This training objective distinguishes most large language models from other architectures and explains why generation happens step by step (Brown et al., 2020).

Decoding methods: The method by which a concrete token is chosen from the predicted probability distribution has a major impact on style, diversity, and correctness of the output. Common methods are greedy decoding (always choose the most probable token), beam search (track multiple candidate paths), temperature-controlled sampling and top-k/top-p (nucleus) sampling. In particular, top-p sampling has been empirically investigated as a practical method for producing diverse, less degenerate text (Holtzman et al., 2019).

Context and cache mechanisms: Implementations often store intermediate results (key/value cache) from earlier layer computations during generation to avoid repeated work and thus make inference more efficient. This optimization is documented in practical implementations and affects latency and memory usage (Hugging Face — Performance & Inference documentation).

Model weights versus control layers: The actual answers follow from the learned weights (pretraining and possibly fine-tuning), but additional control layers — such as instruction adaptations, reward models or safety filters — can moderate the final behavior. Techniques like Reinforcement Learning from Human Feedback (RLHF) are specifically used to steer model responses in desired directions (Ouyang et al., 2022; OpenAI, GPT-4 Technical Report, 2023).

Computational complexity and context length: The self-attention within a transformer has computational complexity that grows with the square of the context length; this means that longer inputs cause significantly more work per inference step (Vaswani et al., 2017). Research and applications have led to optimizations (e.g., FlashAttention) that exploit implementation variations to reduce memory and runtime costs (Dao et al., 2022).

Why models do not "think like humans": The scientific consensus is that language models are powerful statistical prediction machines that learn patterns in large text corpora. Models can imitate complex, sequential structures and even present chains of reasoning, especially when special prompting techniques like chain-of-thought are used. However, this does not mean they undergo the same cognitive processes as humans; models reproduce probabilities and learned patterns, not conscious, semantically grounded introspection (Bommasani et al., 2021; Wei et al., 2022). Chain-of-thought prompting can elicit reasoning-like utterances, but it does not demonstrate that the model has an internal mental model like a human (Wei et al., 2022).

Scaling and capabilities: As model size and the amount of data increase, many capabilities improve, but scaling alone does not produce human-like thinking. Scaling laws describe systematic performance gains; however, the fundamental objective and the probabilistic nature remain unchanged (Kaplan et al., 2020).

Part 3 (10 minutes): Applications, Limits, and Thought Exercises

Applications: Inference is at the core of every practical application of language models — chatbots, translation, text generation, code understanding, research assistance. In each case prompt formulation, model size, decoding strategy and possibly regulatory or safety-related post-processing determine the concrete result (Brown et al., 2020; Ouyang et al., 2022).

Limits and risks: Models produce coherent and sometimes very convincing text that can nevertheless be factually wrong or misleading. This is inherent to statistical predictions: probabilities are not guarantees of truth. In addition, dangerous or unwanted outputs can arise from training-data bias or inappropriate prompting rules; therefore moderation and downstream control mechanisms are common (Bommasani et al., 2021; OpenAI, GPT-4 Technical Report, 2023).

Thought exercises for deeper exploration (briefly discussable):

1) Prompt experiment: Test how different decoding parameters (temperature, top-p) change answer variability. Observe whether correctness systematically decreases or whether only style and creativity vary (Holtzman et al., 2019).

2) Latency analysis: Compare how response time changes when making the same request with a short versus a long context history. Consider which parts of the pipeline (attention, feedforward, I/O, filters) might contribute most (Vaswani et al., 2017; Dao et al., 2022).

3) Causal analysis: Formulate a prompt that requires a logical chain and check whether an explicit chain-of-thought prompt improves answer quality. Discuss whether improved output formulation is equivalent to internal understanding (Wei et al., 2022).

Final notes on the evidence base: The descriptions here are based on technical papers and reports that represent the currently accepted mechanisms and optimizations. Some implementation details (e.g., proprietary inference pipelines of large providers) are not fully disclosed; this leads to information gaps and uncertainties about exact runtime optimizations or additional filters (the OpenAI GPT-4 Technical Report points to implementation details but does not disclose all operational parameters).