At each iteration of deep learning training, we run the model to obtain a prediction (which is called the forward pass). Then we compare the prediction with the ground truth to compute a loss . To update the model parameters (weights), we calculate the gradient of the loss with respect to each parameter as , which is called the backward pass. The gradients are then used to update the parameters via an optimization algorithm (e.g., SGD or Adam).
The input to the backward pass of an operator is the gradient of the loss with respect to its output, and the output of the backward pass is the gradient with respect to its input.
Gradients of Common Primitives
Scalar Operations
Reduction Operations
- Input: a vector .
- Forward Pass: ,
where is a binary operation on scalars, and is the initial value of the reduction.
Since the final form of the gradient depends on the specific binary operation , we discuss two common reduction operations.
Summation: , where and .
where is the all-ones vector.Product: , where and .
Element-wise Binary Operations
- Inputs: two vectors .
- Forward Pass: ,
where is a binary operation on scalars applied element-wise. Gradients: let and ,
where is the element-wise product.
Inner Product
Outer Product
- Inputs: two vectors .
- Forward Pass: , where .
Gradients:
Matrix Multiplication
- Inputs: two matrices .
- Forward Pass: , where .
Gradients:
Gradients of Composite Operators
Softmax
- Input: a vector .
Forward Pass:
Gradients: let ,
where is the all-ones vector, and is the element-wise product.
RMSNorm
- Input: a vector .
- Learnable parameters: .
Forward Pass:
Gradients: let ,
Gradients of Common Building Blocks
Cross-Entropy Loss
Scaled Dot-Product Attention
- Inputs: three matrices .
- Constants: a scalar , and a mask matrix .
Forward Pass:
where is applied row-wise.Gradients: let , and ,
where is the all-ones vector.
Gated Linear Units
Let be the gating function, whose derivative is denoted as .
- Input: a matrix .
- Learnable parameters: two matrices .
Forward Pass:
Gradients:
Feed-Forward Network
We focus on the SwiGLU FFN, which is widely used in transformer models. Let be the Swish activation function, whose derivative is denoted as , thus
- Input: a matrix .
- Learnable parameters: three matrices .
Forward Pass:
Gradients:
Mixture of Experts
We focus on the post-router part of an MoE layer.
- Inputs: a matrix , a routing table , and a gating score matrix .
- Learnable parameters: SwiGLU FFN experts with , where for .
Forward Pass: let be the -th rows of ,
Gradients:
where and come from the SwiGLU FFN backward pass of , and is non-differentiable.
In practice, tokens routed to the same expert are grouped into a contiguous batch of size (with ) so all experts execute as a Grouped GEMM. Let be the binary gather matrix selecting the tokens routed to expert , and be their corresponding gating scores from .
Forward Pass:
Gradients: let ,
where is obtained from the SwiGLU FFN backward pass of on via Grouped GEMM (which also yields directly), and is scattered back to .