
The 6NP rule is a shortcut for estimating how many floating-point operations are needed to train a dense neural network. It can be summarized like so:
In this blog post we are going to walk our way through understanding where does this simple rule come from.
Let’s define as the number of trainable parameters and the number of tokens processed during training.
1) What is a FLOP?
A FLOP is a floating-point operation. For instance is one multiplication followed by one addition. This counts as 2 FLOPs.
2) Forward pass
Consider our network such that
So for every weight , we do:
Therefore we have 2 FLOPs per weight. Now let’s try to find back this result but using matrix multiplications instead.
Let’s consider a linear layer:
The number of parameters in is
Let’s consider one token from . We have . In this operation we have outputs and each one of these outputs require a dot product involving values.
So the cost for one token is
Hence the forward FLOPs per token is .
However we have to remember that training involves two stages : forward loss backward
The forward pass computes the prediction. The backward pass helps us understand how should parameters change to improve our prediction.
3) Backward pass
During the forward prediction we had . Now during backprop we want to compute
This is what is going to tell us how our loss change with respect to the inputs and the parameters.
The gradient with respect to the inputs is
while the gradient with respect to the weights is
So for the backpropagation step we have two more matrix multiplications. So we have
Each matrix costs FLOPs like we saw before.
So for one token
And now process tokens instead of one and you get . Next time you will hear about that rule, you will know the reason why :)