Study the paper
Scaling Laws for Neural Language Models
Lessons, visuals, quizzes, flashcards, and resources—organized in teaching order.
Transformer Parameter and Compute Scaling
Transformer Forward Pass FLOPs Estimate
Transformer Forward Pass FLOPs Estimate
Source equation
This equation estimates the computational cost (in FLOPs per token) of a forward pass through a standard Transformer model. It accounts for both the parameter-dependent operations (such as feed-forward networks and projections) and the context-dependent attention operations.
Sources
equation
Cforward≈2N+2nlayernctxdmodelsubscript𝐶forward2𝑁2subscript𝑛layersubscript𝑛ctxsubscript𝑑modelC_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}} (2.2)
C_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}}Sources
equation
Cforward≈2N+2nlayernctxdmodelsubscript𝐶forward2𝑁2subscript𝑛layersubscript𝑛ctxsubscript𝑑modelC_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}} (2.2)
C_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}}The term represents the FLOPs required for the matrix multiplications associated with the non-embedding parameters (since each parameter is used in a multiply-accumulate operation, which counts as 2 FLOPs per token). The term represents the FLOPs required for the attention matrix multiplications (query-key dot products and attention-value multiplication) which scale quadratically with the context length .
Sources
equation
Cforward≈2N+2nlayernctxdmodelsubscript𝐶forward2𝑁2subscript𝑛layersubscript𝑛ctxsubscript𝑑modelC_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}} (2.2)
C_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}}Implementation detail
Illustrative Calculation
Let's calculate the forward pass FLOPs per token for a small Transformer model with the following parameters:
- Non-embedding parameters (100M)
- Number of layers
- Context length
- Model dimension
Using the formula:
- Calculate the parameter-dependent term:
- Calculate the attention-dependent term:
- Sum the terms:
Sources
equation
Cforward≈2N+2nlayernctxdmodelsubscript𝐶forward2𝑁2subscript𝑛layersubscript𝑛ctxsubscript𝑑modelC_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}} (2.2)
C_{\rm forward}\approx 2N+2n_{{\rm layer}}n_{{\rm ctx}}d_{{\rm model}}- Computational cost of a forward pass per token (in FLOPs) · scalar
- Number of non-embedding parameters in the model · scalar
- Number of layers in the Transformer · scalar
- Context window length (number of tokens) · scalar
- Hidden dimension size of the model · scalar