Jump to content

Forward pass: Difference between revisions

From Wikibase
Evaluation of a network on an input: layer recurrence, composition, cost, and its relation to backpropagation — AI-assisted (RonzzWikiCowriter) (via create-page on MediaWiki MCP Server)
 
Rewrite as an overview that composes the component pages, instead of restating the feedforward algorithm (via update-page on MediaWiki MCP Server)
Line 1: Line 1:
[[Category:Artificial neural network]]
[[Category:Artificial neural network]]
A '''forward pass''' is the evaluation of a [[neural network]] on an input: the input is propagated through the layers to produce the output, with no gradient computed.<ref>{{#cite:Q3074}}</ref> For a network trained by [[backpropagation]] it is the 1st of the 2 passes, and it is the only one that runs at inference.
A '''forward pass''' is 1 evaluation of a [[neural network]] on an input: the input is carried through the model's stages to produce the output, with no gradient computed.<ref>{{#cite:Q3074}}</ref> For a network trained by [[backpropagation]] it is the 1st of the 2 passes, and it is the only one that runs at inference.


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


Let $L$ be the number of layers, $\vec a^{(0)}=\vec x$ the input, $\mathbf{W}^{(l)},\vec b^{(l)}$ the weights and biases of layer $l$, which together form the [[model parameters]] $\theta$, and $\sigma$ the [[activation function]]. Each layer applies an affine map and then the activation,
== Stages ==


$$
For a [[Transformer]] language model, the stages are:
\begin{cases}
 
\vec z^{(l)}=\mathbf{W}^{(l)}\vec a^{(l-1)}+\vec b^{(l)}, \\
# '''[[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.
\vec a^{(l)}=\sigma\left(\vec z^{(l)}\right),
# '''Token embedding''' maps those indices to vectors in $\mathbb{R}^d$, stacked as the rows of $\mathbf{X}\in\mathbb{R}^{T\times d}$.
\end{cases}
# A stack of $L$ '''blocks''', each applying [[self-attention]] across positions and then the [[feedforward algorithm]] to each position separately.
\qquad l=1,\cdots,L,
# An '''output head''' with [[Softmax|softmax]] maps the final representation to logits over $\mathcal{V}$.
$$


where $\vec z^{(l)}$ is the pre-activation and $\vec a^{(l)}$ the activation of layer $l$. The output of the network is $\vec a^{(L)}$.
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 ==
== Composition ==


Writing each layer as the map
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
 
$$
\varphi^{(l)}(\vec a)=\sigma\left(\mathbf{W}^{(l)}\vec a+\vec b^{(l)}\right),
$$
 
the forward pass is their composition,


$$
$$
Line 30: Line 23:
$$
$$


so that $\vec a^{(L)}=f_\theta(\vec x)$. A [[large language model (LLM)|language model]] is 1 such $f_\theta$, whose output is a distribution over the vocabulary rather than a single value.
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 ==
== Cost ==


The cost is dominated by the affine maps. A matrix-vector product $\mathbf{W}^{(l)}\vec a^{(l-1)}$ costs $\mathcal{O}(d_{l-1}d_l)$ for layer widths $d_{l-1}$ and $d_l$, so the pass costs
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}\left(\sum_{l=1}^{L}d_{l-1}d_l\right),
\mathcal{O}(T^2d)\ \text{from [[self-attention]],}
\qquad
\mathcal{O}(Td^2)\ \text{from the [[feedforward algorithm]],}
$$
$$


linear in the number of layers and quadratic in the width. Layers that are not plain affine maps add their own terms, such as the $\mathcal{O}(T^2d)$ of [[self-attention]] for a context of length $T$.
the 1st growing quadratically with context and the 2nd with the square of the width. Which of the 2 dominates therefore depends on whether the context or the width is larger, and over $L$ blocks both scale linearly in $L$.
 
== Training and inference ==


== Relation to backpropagation ==
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.


The forward pass must retain $\vec z^{(l)}$ and $\vec a^{(l)}$ for every layer, because [[backpropagation]] needs them to evaluate the local gradients on the way back. Storing the activations is therefore part of the cost of training, though not of inference, where only the intermediate values of the current step are needed.
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]].

Revision as of 15:28, 11 November 2026

Languages: English · français · Esperanto

A forward pass is 1 evaluation of a neural network 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$, stacked as the rows of $\mathbf{X}\in\mathbb{R}^{T\times d}$.
  3. A stack of $L$ blocks, each applying self-attention across positions and then the feedforward algorithm to each position separately.
  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^{(L)}\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)\ \text{from self-attention,} \qquad \mathcal{O}(Td^2)\ \text{from the feedforward algorithm,} $$

the 1st growing quadratically with context and the 2nd with the square of the width. Which of the 2 dominates therefore depends on whether the context or the width is larger, and over $L$ blocks both scale linearly in $L$.

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.