Source: original article
i always struggled remembering formulas for what would be the gradient of a particular layer in the transformer. recently while studying about distributed training I realized that I can not move forward without having a crystal clear understanding of how gradients flew in the network
so here is a blog which will teach you all you need to be a wizard of gradients
Rule 1
suppose we want to find the gradient where L is the loss and W is the gradient. the rule says that the shape of will be equal to the shape of W, memorize this.
clearly x is the input here, f(x) is the layer about which we care, wow it rhymes
y is the output of the layer, y = f(x) and L is the final loss.
what we are interested in is and , now F can be a weight matrice or some other function which has learnable parameters to it
Memorize
generally the f is of two types, weight matrices and element wise operations. we are going to look at element wise operations for now
an “element-wise” operation means the math happens to each number in the matrix independently. none of the numbers “talk” to each other.
Examples:
(Matrix Addition)
(Activation)
(Square every element)
Rule for Element Wise Operations (memorize this)
you might be wondering what does this circle and a dot between it means
that is the hadamard product (element-wise multiplication). it means: multiply the top-left of a with the top-left of b, top-right with top-right, etc. no fancy row-column dot products here. just simple multiplication.
example 1
(rule: if , keep it. if , set to 0)
so if or if
what does this mean philosophically? if pass my gradients (y speaking) as it is if not stop make all the gradients 0 ( do not change the weights they did not contribute to the loss)
example 2
Residual Connection
so the gradients flow as it is from Y to X and Z
note: we talked about relu above and only calculated and not because ReLU does not have any parameters to tune, if we were using GeGLU we would have also calculated dy/DW because we also want its parameters to learn.
now we have completed activation functions lets move on to the matrice multiplications
we want to learn two things and
so here are the formulas, please memorize
you are now all set for calculating any gradient in transformers
but lets cover one special hard case, that is gradient of loss wrt to the Logit layer
Softmax
(shape: b, t, v where v is vocab size).
(probabilities).
l = -log(p_correct_token).
deriving softmax is messy (lots of jacobian matrices), but the final result is elegantly simple
(one hot)
and from here it is all matrice multiplications and activation functions which we have already covered. so no worries.