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)
 
No edit summary
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Artificial neural network]]
[[Category:Artificial neural network| 1]]
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.
[[File:Deepseek chat.png|thumbnail|When one chats with an [[LLM]], the LLM repeatedly executes [[forward pass]] to generate a response, often token by token.]]
A '''forward pass''' of an [[artificial neural network (ANN)]], also called '''inference''', is the process of obtaining an output $f_\theta(X)$ based on input $X$.


== Definition ==
== Stages ==


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,
Common stages of a '''forward pass''' include:


$$
* '''[[tokenisation (machine learning)|Tokenisation]]''' and '''[[token embedding (machine learning)|Token embedding]]''', to convert raw input into real vectors.
\begin{cases}
* '''Computation'''
\vec z^{(l)}=\mathbf{W}^{(l)}\vec a^{(l-1)}+\vec b^{(l)}, \\
** in a simple [[ANN]]: via [[feedforward algorithm]];
\vec a^{(l)}=\sigma\left(\vec z^{(l)}\right),
** in a [[Transformer]]: via multiple layers of [[self-attention]], [[Feed-Forward Network (FFN) Sub-Layer (Transformer)|FFN sub-layer]], and [[residual connection (Transformer)|residual connection + layer normalisation]].
\end{cases}
* '''Output head''': converts the model output back into token(s), often via [[Softmax|softmax]].
\qquad l=1,\cdots,L,
$$
 
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)}$.
 
== Composition ==
 
Writing each layer as the map
 
$$
\varphi^{(l)}(\vec a)=\sigma\left(\mathbf{W}^{(l)}\vec a+\vec b^{(l)}\right),
$$
 
the forward pass is their composition,
 
$$
f_\theta=\varphi^{(L)}\circ\cdots\circ\varphi^{(1)},
$$
 
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.
 
== 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
 
$$
\mathcal{O}\left(\sum_{l=1}^{L}d_{l-1}d_l\right),
$$
 
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$.
 
== Relation to backpropagation ==
 
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.

Latest revision as of 10:43, 16 November 2026

Languages: English · français · Esperanto

When one chats with an LLM, the LLM repeatedly executes forward pass to generate a response, often token by token.
Image: Rong Martin-Siebler ZHOU · CC BY-SA 4.0
· Open in a new tab

A forward pass of an artificial neural network (ANN), also called inference, is the process of obtaining an output $f_\theta(X)$ based on input $X$.

Stages

Common stages of a forward pass include: