Jump to content

Forward pass: Difference between revisions

From Wikibase
Do not abbreviate to "neural network": a neural network can be biological. Link the ANN instead (via update-page on MediaWiki MCP Server)
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Line 10: Line 10:
# '''[[tokenisation (machine learning)|Tokenisation]]''' cuts the raw input into tokens from a [[tokenisation (machine learning)#Definition|token vocabulary]] $\mathcal{V}$, yielding a sequence of $T$ indices.
# '''[[tokenisation (machine learning)|Tokenisation]]''' cuts the raw input into tokens from a [[tokenisation (machine learning)#Definition|token vocabulary]] $\mathcal{V}$, yielding a sequence of $T$ indices.
# '''[[token embedding (machine learning)|Token embedding]]''' maps those indices to vectors in $\mathbb{R}^d$ through the embedding matrix $\mathbf{W}_E$, stacked as the rows of $\mathbf{X}\in\mathbb{R}^{T\times d}$, after which a [[positional encoding]] supplies the order.
# '''[[token embedding (machine learning)|Token embedding]]''' maps those indices to vectors in $\mathbb{R}^d$ through the embedding matrix $\mathbf{W}_E$, stacked as the rows of $\mathbf{X}\in\mathbb{R}^{T\times d}$, after which a [[positional encoding]] supplies the order.
# A stack of $N$ '''blocks''', each applying [[self-attention]] across positions and then the [[feedforward algorithm]] to each position separately.
# A stack of $N$ '''blocks''', each applying [[self-attention]] across positions and then the [[Feed-Forward Network (FFN) Sub-Layer (Transformer)|FFN sub-layer]] to each position separately, both inside a [[residual connection (Transformer)|residual connection and a layer normalisation]].
# An '''output head''' with [[Softmax|softmax]] maps the final representation to logits over $\mathcal{V}$.
# An '''output head''' with [[Softmax|softmax]] maps the final representation to logits over $\mathcal{V}$.



Revision as of 10:12, 12 November 2026

Languages: English · français · Esperanto

A forward pass is 1 evaluation of an artificial neural network (ANN) on an input: the input is carried through the model's stages to produce the output, with no gradient computed.[1] For a network trained by backpropagation it is the 1st of the 2 passes, and it is the only one that runs at inference.

The pass is an assembly of stages, each defined on its own page, rather than a single formula.

Stages

For a Transformer language model, the stages are:

  1. Tokenisation cuts the raw input into tokens from a token vocabulary $\mathcal{V}$, yielding a sequence of $T$ indices.
  2. Token embedding maps those indices to vectors in $\mathbb{R}^d$ through the embedding matrix $\mathbf{W}_E$, stacked as the rows of $\mathbf{X}\in\mathbb{R}^{T\times d}$, after which a positional encoding supplies the order.
  3. A stack of $N$ blocks, each applying self-attention across positions and then the FFN sub-layer to each position separately, both inside a residual connection and a layer normalisation.
  4. An output head with softmax maps the final representation to logits over $\mathcal{V}$.

The 2 inner stages are the ones that give the model its power and its cost, and are treated at self-attention and feedforward algorithm respectively. A plain artificial neural network (ANN) is the degenerate case: it has no attention stage and no tokenisation, and its pass is repeated application of the feedforward step.

Composition

Writing the block at layer $l$ as a map $\varphi^{(l)}$ from its input representation to its output representation, the model as a whole is the composition

$$ f_\theta=\varphi^{(N)}\circ\cdots\circ\varphi^{(1)}, $$

so that the output of the pass is $f_\theta(\vec x)$, and it is this $f_\theta$ whose parameters $\theta$ are defined at model parameters.

Cost

Because the stages compose, so do their costs. For a context of length $T$ and hidden dimension $d$, the 2 dominant terms per block are

$$ \mathcal{O}(T^2d) \qquad\text{and}\qquad \mathcal{O}(Td^2), $$

the 1st coming from self-attention and growing quadratically with context, the 2nd coming from the feedforward algorithm and growing with the square of the width. Which of the 2 dominates therefore depends on whether the context or the width is larger, and over $N$ blocks both scale linearly in $N$.

Training and inference

The pass retains the model parameters and, during training, the intermediate activations, because backpropagation needs them to evaluate the local gradients on the way back. That retention is part of the cost of training but not of inference.

At inference the pass is the unit of generation: each output token costs 1 pass, and the stages of the previous steps need not be recomputed, which is the purpose of KV caching.