Large language model (LLM): Difference between revisions
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 == | ||
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|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. | |||
$$ | $$ | ||
Temperature sampling | 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_\ | \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{\ | 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{ | |||
$$ | $$ | ||
=== 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_{ | \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.<ref>{{#cite:Q3192}}</ref> | |||
Revision as of 13:52, 11 November 2026
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]
- ↑ ↑ Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. 5998–6008. https://doi.org/10.48550/arXiv.1706.03762
- ↑ Kaplan, J., McCandlish, S., Henighan, T., Brown, T. B., Chess, B., Child, R., Gray, S., Radford, A., Wu, J., & Amodei, D. (2020). Scaling Laws for Neural Language Models. https://arxiv.org/abs/2001.08361
- ↑ Hoffmann, J., Borgeaud, S., Mensch, A., Buchatskaya, E., Cai, T., Rutherford, E., de Las Casas, D., Hendricks, L. A., Welbl, J., Clark, A., Hennigan, T., Noland, E., Millican, K., van den Driessche, G., Damoc, B., Guy, A., Osindero, S., Simonyan, K., Elsen, E., … Sifre, L. (2022). Training Compute-Optimal Large Language Models. https://doi.org/10.48550/arXiv.2203.15556
- ↑ Holtzman, A., Buys, J., Du, L., Forbes, M., & Choi, Y. (2020). The Curious Case of Neural Text Degeneration. https://doi.org/10.48550/arXiv.1904.09751
- ↑ Pope, R., Douglas, S., Chowdhery, A., Devlin, J., Bradbury, J., Levskaya, A., Heek, J., Xiao, K., Agrawal, S., & Dean, J. (2023). Efficiently Scaling Transformer Inference. https://doi.org/10.48550/arXiv.2211.05102