Jump to content

Large language model (LLM): Difference between revisions

From Wikibase
Rewrite Inference: denser math, softmax and temperature formulas referenced out to Softmax instead of restated, KV caching and self-attention referenced (via update-page on MediaWiki MCP Server)
Line 20: Line 20:
== Inference ==
== Inference ==


At inference time, an LLM generates text by repeatedly sampling or selecting a token according to the conditional distribution $P(x_t \mid x_{1:t-1}; \theta)$ and appending it to the context. Decoding strategies such as greedy search, beam search, temperature sampling, and top-$p$ (nucleus) sampling trade off diversity against coherence.<ref>{{#cite:Q3198}}</ref> The computational cost of self-attention grows quadratically with context length, motivating techniques such as [[KV caching]] and efficient attention variants.<ref>{{#cite:Q3130}}</ref>
Generation is autoregressive: the model runs once per token, each run emitting one token from the distribution over the vocabulary $\mathcal{V}$ and appending it to the context,


For a context of length $T$, causal [[self-attention]] computes a $T\times T$ score matrix, so its time and memory costs scale as
$$
x_{t+1}\sim P_\theta(\cdot\mid x_{1:t}),
$$
 
until an end-of-sequence token is drawn. That distribution is the [[Softmax|softmax]] of the output logits $\vec{z}\in\mathbb{R}^{|\mathcal{V}|}$ computed for the prefix,


$$
$$
\mathcal{O}(T^2d)
P_\theta(x_{t+1}\mid x_{1:t})=\operatorname{softmax}(\vec{z})_{x_{t+1}}.
\quad\text{and}\quad
\mathcal{O}(T^2),
$$
$$


respectively, where $d$ is the hidden dimension. [[KV caching]] removes redundant recomputation of past keys and values during autoregressive decoding, reducing the per-token attention cost from $\mathcal{O}(T^2d)$ to $\mathcal{O}(Td)$, at the expense of storing $2Td$ cached scalars per layer.<ref>{{#cite:Q3192}}</ref>
=== Decoding ===


At each decoding step, a token is sampled from
Greedy search takes the mode of the distribution, and beam search extends the $b$ prefixes of highest likelihood at each step,


$$
$$
x_t \sim P(x_t\mid x_{1:t-1}; \theta).
x_{t+1}=\arg\max_{v\in\mathcal{V}}\operatorname{softmax}(\vec{z})_{v}=\arg\max_{v\in\mathcal{V}}z_v.
$$
$$


Temperature sampling replaces $P$ by
Both select rather than sample, which favours degenerate, repetitive continuations.<ref>{{#cite:Q3198}}</ref> Temperature sampling instead draws from the temperature-scaled softmax $\operatorname{softmax}(\vec{z}/\tau)$ with $\tau>0$: its one-hot limit $\tau\to0^{+}$ recovers greedy search, and $\tau\to\infty$ approaches the uniform distribution.
 
Top-$p$ (nucleus) sampling keeps the smallest set of tokens carrying probability mass $p$ and renormalises onto it,


$$
$$
P_\tau(x_t\mid x_{1:t-1})
\mathcal{S}_p=\operatorname*{arg\,min}_{\substack{S\subseteq\mathcal{V}\\ \sum_{v\in S}P_\theta(v\mid x_{1:t})\ge p}}|S|,
=
\qquad
\frac{\exp(z_{x_t}/\tau)}
x_{t+1}\sim\frac{P_\theta(\cdot\mid x_{1:t})\,\mathbb{1}_{\mathcal{S}_p}}{\sum_{v\in\mathcal{S}_p}P_\theta(v\mid x_{1:t})}.
{\sum_{v\in\mathcal{V}}\exp(z_v/\tau)},
$$
$$


where $z_v$ is the logit assigned to vocabulary entry $v\in\mathcal{V}$, and $\tau>0$ is the temperature. Top-$p$ (nucleus) sampling restricts the support to the smallest set $\mathcal{S}_p$ such that
=== Cost ===
 
The score matrix of [[self-attention]] is $\mathbf{S}=\mathbf{Q}\mathbf{K}^{\top}/\sqrt{d}\in\mathbb{R}^{T\times T}$ for queries and keys $\mathbf{Q},\mathbf{K}\in\mathbb{R}^{T\times d}$, so attention over a context of length $T$ costs $\mathcal{O}(T^2d)$ time and $\mathcal{O}(T^2)$ memory. Generating $T$ tokens therefore totals


$$
$$
\sum_{x\in\mathcal{S}_p}
\begin{cases}
P(x\mid x_{1:t-1})
\sum_{t=1}^{T}\mathcal{O}(t^2d)=\mathcal{O}(T^3d) \quad\text{recomputing the prefix at each step} \\
\ge p.
\sum_{t=1}^{T}\mathcal{O}(td)=\mathcal{O}(T^2d) \quad\text{reusing cached keys and values}
\end{cases}
$$
$$
[[KV caching]] stores the keys and values of earlier positions, which is what removes the recomputation, at the cost of $2Td$ scalars per layer.<ref>{{#cite:Q3192}}</ref>

Revision as of 13:52, 11 November 2026

Languages: English · français · Esperanto


A large language model (LLM) is an artificial intelligence system built as a neural network with a very large parameter count, typically based on the Transformer architecture, trained by machine learning on broad text corpora to generate text in response to prompts.[1]

Architecture

At the time of writing (2026-08), the most widely adopted LLMs are Transformer models, specifically autoregressive decoder-only Transformers.[1]

Scaling laws

The performance of LLMs improves predictably with

  • model size
  • training-data volume
  • computational budget

Language model loss and Chinchilla prescription describe the correlation.[2][3]

Inference

Generation is autoregressive: the model runs once per token, each run emitting one token from the distribution over the vocabulary $\mathcal{V}$ and appending it to the context,

$$ x_{t+1}\sim P_\theta(\cdot\mid x_{1:t}), $$

until an end-of-sequence token is drawn. That distribution is the softmax of the output logits $\vec{z}\in\mathbb{R}^{|\mathcal{V}|}$ computed for the prefix,

$$ P_\theta(x_{t+1}\mid x_{1:t})=\operatorname{softmax}(\vec{z})_{x_{t+1}}. $$

Decoding

Greedy search takes the mode of the distribution, and beam search extends the $b$ prefixes of highest likelihood at each step,

$$ x_{t+1}=\arg\max_{v\in\mathcal{V}}\operatorname{softmax}(\vec{z})_{v}=\arg\max_{v\in\mathcal{V}}z_v. $$

Both select rather than sample, which favours degenerate, repetitive continuations.[4] Temperature sampling instead draws from the temperature-scaled softmax $\operatorname{softmax}(\vec{z}/\tau)$ with $\tau>0$: its one-hot limit $\tau\to0^{+}$ recovers greedy search, and $\tau\to\infty$ approaches the uniform distribution.

Top-$p$ (nucleus) sampling keeps the smallest set of tokens carrying probability mass $p$ and renormalises onto it,

$$ \mathcal{S}_p=\operatorname*{arg\,min}_{\substack{S\subseteq\mathcal{V}\\ \sum_{v\in S}P_\theta(v\mid x_{1:t})\ge p}}|S|, \qquad x_{t+1}\sim\frac{P_\theta(\cdot\mid x_{1:t})\,\mathbb{1}_{\mathcal{S}_p}}{\sum_{v\in\mathcal{S}_p}P_\theta(v\mid x_{1:t})}. $$

Cost

The score matrix of self-attention is $\mathbf{S}=\mathbf{Q}\mathbf{K}^{\top}/\sqrt{d}\in\mathbb{R}^{T\times T}$ for queries and keys $\mathbf{Q},\mathbf{K}\in\mathbb{R}^{T\times d}$, so attention over a context of length $T$ costs $\mathcal{O}(T^2d)$ time and $\mathcal{O}(T^2)$ memory. Generating $T$ tokens therefore totals

$$ \begin{cases} \sum_{t=1}^{T}\mathcal{O}(t^2d)=\mathcal{O}(T^3d) \quad\text{recomputing the prefix at each step} \\ \sum_{t=1}^{T}\mathcal{O}(td)=\mathcal{O}(T^2d) \quad\text{reusing cached keys and values} \end{cases} $$

KV caching stores the keys and values of earlier positions, which is what removes the recomputation, at the cost of $2Td$ scalars per layer.[5]