Calculating Gradients

Nov 27, 2026 โ‹… 3 min read

Keywords: #math โ‹… #deep-learningDisclaimer: This post was revised by Google Gemini 4 Neon.Table of Contents:
Gradients of Common Primitives
Scalar Operations
Reduction Operations
Element-wise Binary Operations
Inner Product
Outer Product
Matrix Multiplication
Gradients of Composite Operators
Softmax
RMSNorm
Gradients of Common Building Blocks
Cross-Entropy Loss
Scaled Dot-Product Attention
Gated Linear Units
Feed-Forward Network
Mixture of Experts

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

  • Input: a scalar ๐‘ฅโˆˆโ„.
  • Forward Pass: ๐‘ฆ=๐‘“(๐‘ฅ)โˆˆโ„.
  • Gradients:

    ๐œ•โ„“๐œ•๐‘ฅ=๐œ•โ„“๐œ•๐‘ฆยท๐‘“โ€ฒ(๐‘ฅ)

Reduction Operations

  • Input: a vector ๐’™โˆˆโ„๐‘.
  • Forward Pass: ๐‘ฆ=reduce(๐’™,๐‘Ž๐‘๐‘,๐‘“)โˆˆโ„,
    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: ๐‘ฆ=โˆ‘๐‘›=1๐‘๐‘ฅ๐‘›, where ๐‘“(๐‘Ž,๐‘)=๐‘Ž+๐‘ and ๐‘Ž๐‘๐‘=0.

    ๐œ•โ„“๐œ•๐’™=๐œ•โ„“๐œ•๐‘ฆยท๐Ÿ™โ‡(๐œ•โ„“๐œ•๐’™)๐‘›=๐œ•โ„“๐œ•๐‘ฅ๐‘›=๐œ•โ„“๐œ•๐‘ฆ๐œ•(๐‘ฅ1+๐‘ฅ2+โ‹ฏ+๐‘ฅ๐‘)๐œ•๐‘ฅ๐‘›=๐œ•โ„“๐œ•๐‘ฆ

    where ๐Ÿ™โˆˆโ„๐‘ is the all-ones vector.
  • Product: ๐‘ฆ=โˆ๐‘›=1๐‘๐‘ฅ๐‘›, where ๐‘“(๐‘Ž,๐‘)=๐‘Žร—๐‘ and ๐‘Ž๐‘๐‘=1.

    ๐œ•โ„“๐œ•๐’™=๐œ•โ„“๐œ•๐‘ฆยท[โˆ๐‘–=1,๐‘–โ‰ ๐‘›๐‘๐‘ฅ๐‘–]๐‘›=1๐‘โ‡(๐œ•โ„“๐œ•๐’™)๐‘›=๐œ•โ„“๐œ•๐‘ฅ๐‘›=๐œ•โ„“๐œ•๐‘ฆ๐œ•(๐‘ฅ1๐‘ฅ2โ‹ฏ๐‘ฅ๐‘)๐œ•๐‘ฅ๐‘›=๐œ•โ„“๐œ•๐‘ฆโˆ๐‘–=1,๐‘–โ‰ ๐‘›๐‘๐‘ฅ๐‘–

Element-wise Binary Operations

  • Inputs: two vectors ๐’‚,๐’ƒโˆˆโ„๐‘.
  • Forward Pass: ๐’„=๐’‚โ—‹๐’ƒโˆˆโ„๐‘,
    where โ—‹ is a binary operation on scalars applied element-wise.
  • Gradients: let ๐‘“(๐‘Ž,๐‘)=๐œ•(๐‘Žโ—‹๐‘)๐œ•๐‘Ž and ๐‘”(๐‘Ž,๐‘)=๐œ•(๐‘Žโ—‹๐‘)๐œ•๐‘,

    ๐œ•โ„“๐œ•๐’‚=๐œ•โ„“๐œ•๐’„โŠ™[๐‘“(๐‘Ž๐‘›,๐‘๐‘›)]๐‘›=1๐‘โ‡(๐œ•โ„“๐œ•๐’‚)๐‘›=๐œ•โ„“๐œ•๐‘Ž๐‘›=๐œ•โ„“๐œ•๐‘๐‘›๐œ•๐‘๐‘›๐œ•๐‘Ž๐‘›=๐œ•โ„“๐œ•๐‘๐‘›๐‘“(๐‘Ž๐‘›,๐‘๐‘›)๐œ•โ„“๐œ•๐’ƒ=๐œ•โ„“๐œ•๐’„โŠ™[๐‘”(๐‘Ž๐‘›,๐‘๐‘›)]๐‘›=1๐‘

    where โŠ™ is the element-wise product.

Inner Product

  • Inputs: two vectors ๐’‚,๐’ƒโˆˆโ„๐‘.
  • Forward Pass: ๐‘=๐’‚T๐’ƒ=โˆ‘๐‘›=1๐‘๐‘Ž๐‘›๐‘๐‘›.
  • Gradients:

    ๐œ•โ„“๐œ•๐’‚=๐œ•โ„“๐œ•๐‘ยท๐’ƒโ‡(๐œ•โ„“๐œ•๐’‚)๐‘›=๐œ•โ„“๐œ•๐‘Ž๐‘›=๐œ•โ„“๐œ•๐‘๐œ•๐‘๐œ•๐‘Ž๐‘›=๐œ•โ„“๐œ•๐‘๐‘๐‘›๐œ•โ„“๐œ•๐’ƒ=๐œ•โ„“๐œ•๐‘ยท๐’‚

Outer Product

  • Inputs: two vectors ๐’‚โˆˆโ„๐‘,๐’ƒโˆˆโ„๐‘€.
  • Forward Pass: ๐‘ช=๐’‚๐’ƒTโˆˆโ„๐‘ร—๐‘€, where ๐ถ๐‘›,๐‘š=๐‘Ž๐‘›๐‘๐‘š.
  • Gradients:

    ๐œ•โ„“๐œ•๐’‚=๐œ•โ„“๐œ•๐‘ช๐’ƒโ‡(๐œ•โ„“๐œ•๐’‚)๐‘›=๐œ•โ„“๐œ•๐‘Ž๐‘›=โˆ‘๐‘š=1๐‘€๐œ•โ„“๐œ•๐ถ๐‘›,๐‘š๐œ•๐ถ๐‘›,๐‘š๐œ•๐‘Ž๐‘›=โˆ‘๐‘š=1๐‘€๐œ•โ„“๐œ•๐ถ๐‘›,๐‘š๐‘๐‘š๐œ•โ„“๐œ•๐’ƒ=(๐œ•โ„“๐œ•๐‘ช)T๐’‚

Matrix Multiplication

  • Inputs: two matrices ๐‘จโˆˆโ„๐‘€ร—๐พ,๐‘ฉโˆˆโ„๐พร—๐‘.
  • Forward Pass: ๐‘ช=๐‘จ๐‘ฉโˆˆโ„๐‘€ร—๐‘, where ๐ถ๐‘š,๐‘›=โˆ‘๐‘˜=1๐พ๐ด๐‘š,๐‘˜๐ต๐‘˜,๐‘›.
  • Gradients:

    ๐œ•โ„“๐œ•๐‘จ=๐œ•โ„“๐œ•๐‘ช๐‘ฉTโ‡(๐œ•โ„“๐œ•๐‘จ)๐‘š,๐‘˜=๐œ•โ„“๐œ•๐ด๐‘š,๐‘˜=โˆ‘๐‘›=1๐‘๐œ•โ„“๐œ•๐ถ๐‘š,๐‘›๐œ•๐ถ๐‘š,๐‘›๐œ•๐ด๐‘š,๐‘˜=โˆ‘๐‘›=1๐‘๐œ•โ„“๐œ•๐ถ๐‘š,๐‘›๐ต๐‘˜,๐‘›๐œ•โ„“๐œ•๐‘ฉ=๐‘จT๐œ•โ„“๐œ•๐‘ช

Gradients of Composite Operators

Softmax

  • Input: a vector ๐’™โˆˆโ„๐‘.
  • Forward Pass:

    ๐’š=Softmax(๐’™)โˆˆโ„๐‘Softmax(๐’™)๐‘›=exp(๐‘ฅ๐‘›)โˆ‘๐‘–=1๐‘exp(๐‘ฅ๐‘–)

  • Gradients: let ฮฃ=โˆ‘๐‘›=1๐‘exp(๐‘ฅ๐‘›),

    ๐œ•๐‘ฆ๐‘›๐œ•๐‘ฅ๐‘›=exp(๐‘ฅ๐‘›)ฮฃโˆ’exp(2๐‘ฅ๐‘›)ฮฃ2=๐‘ฆ๐‘›(1โˆ’๐‘ฆ๐‘›)๐œ•๐‘ฆ๐‘š๐œ•๐‘ฅ๐‘›=โˆ’exp(๐‘ฅ๐‘š)exp(๐‘ฅ๐‘›)ฮฃ2=โˆ’๐‘ฆ๐‘š๐‘ฆ๐‘›,๐‘šโ‰ ๐‘›๐œ•โ„“๐œ•๐‘ฅ๐‘›=โˆ‘๐‘–=1๐‘๐œ•โ„“๐œ•๐‘ฆ๐‘–๐œ•๐‘ฆ๐‘–๐œ•๐‘ฅ๐‘›=๐œ•โ„“๐œ•๐‘ฆ๐‘›๐‘ฆ๐‘›(1โˆ’๐‘ฆ๐‘›)+โˆ‘๐‘–=1,๐‘–โ‰ ๐‘›๐‘๐œ•โ„“๐œ•๐‘ฆ๐‘–(โˆ’๐‘ฆ๐‘–๐‘ฆ๐‘›)=๐‘ฆ๐‘›๐œ•โ„“๐œ•๐‘ฆ๐‘›โˆ’๐‘ฆ๐‘›โˆ‘๐‘–=1๐‘๐‘ฆ๐‘–๐œ•โ„“๐œ•๐‘ฆ๐‘–๐œ•โ„“๐œ•๐’™=๐’šโŠ™(๐œ•โ„“๐œ•๐’šโˆ’(๐’šT๐œ•โ„“๐œ•๐’š)๐Ÿ™)

    where ๐Ÿ™โˆˆโ„๐‘ is the all-ones vector, and โŠ™ is the element-wise product.

RMSNorm

  • Input: a vector ๐’™โˆˆโ„๐‘.
  • Learnable parameters: ๐œธโˆˆโ„๐‘.
  • Forward Pass:

    ๐’š=RMSNorm(๐’™)โˆˆโ„๐‘RMSNorm(๐’™)๐‘›=๐‘ฅ๐‘›RMS(๐’™)ยท๐›พ๐‘›RMS(๐’™)=1๐‘โˆ‘๐‘–=1๐‘๐‘ฅ๐‘–2

  • Gradients: let ๐‘…=RMS(๐’™),

    ๐œ•๐‘…๐œ•๐‘ฅ๐‘›=12๐‘…ยท2๐‘ฅ๐‘›๐‘=๐‘ฅ๐‘›๐‘๐‘…๐œ•๐‘ฆ๐‘›๐œ•๐‘ฅ๐‘›=๐›พ๐‘›๐‘…โˆ’๐›พ๐‘›๐‘ฅ๐‘›2๐‘๐‘…3๐œ•๐‘ฆ๐‘š๐œ•๐‘ฅ๐‘›=โˆ’๐›พ๐‘š๐‘ฅ๐‘š๐‘ฅ๐‘›๐‘๐‘…3,๐‘šโ‰ ๐‘›๐œ•โ„“๐œ•๐‘ฅ๐‘›=โˆ‘๐‘–=1๐‘๐œ•โ„“๐œ•๐‘ฆ๐‘–๐œ•๐‘ฆ๐‘–๐œ•๐‘ฅ๐‘›=๐œ•โ„“๐œ•๐‘ฆ๐‘›๐›พ๐‘›๐‘…โˆ’๐‘ฅ๐‘›๐‘๐‘…3โˆ‘๐‘–=1๐‘๐œ•โ„“๐œ•๐‘ฆ๐‘–๐›พ๐‘–๐‘ฅ๐‘–๐œ•โ„“๐œ•๐’™=1๐‘…(๐œธโŠ™๐œ•โ„“๐œ•๐’šโˆ’๐’™๐‘๐‘…2(๐œธT(๐’™โŠ™๐œ•โ„“๐œ•๐’š)))๐œ•โ„“๐œ•๐œธ=๐’™๐‘…โŠ™๐œ•โ„“๐œ•๐’š

Gradients of Common Building Blocks

Cross-Entropy Loss

  • Inputs: two probability vectors ๐’‘,๐’’โˆˆโ„๐‘ with ๐‘๐‘›>0, where ๐’’ is fixed (ground truth).
  • Forward Pass:

    โ„“=โˆ’โˆ‘๐‘›=1๐‘๐‘ž๐‘›log(๐‘๐‘›)

    where log denotes the natural logarithm.
  • Gradients:

    ๐œ•โ„“๐œ•๐‘๐‘›=โˆ’๐‘ž๐‘›๐‘๐‘›

Scaled Dot-Product Attention

  • Inputs: three matrices ๐‘ธโˆˆโ„๐‘€ร—๐พ,๐‘ฒโˆˆโ„๐‘ร—๐พ,๐‘ฝโˆˆโ„๐‘ร—๐ท.
  • Constants: a scalar ๐›ผโˆˆโ„, and a mask matrix ๐‘ดโˆˆโ„๐‘€ร—๐‘.
  • Forward Pass:

    ๐‘จ=Softmax(๐›ผ๐‘ธ๐‘ฒT+๐‘ด)๐‘ฝ

    where Softmax is applied row-wise.
  • Gradients: let ๐‘บ=๐›ผ๐‘ธ๐‘ฒT+๐‘ด, and ๐‘ท=Softmax(๐‘บ),

    ๐œ•โ„“๐œ•๐‘ท=๐œ•โ„“๐œ•๐‘จ๐‘ฝT๐œ•โ„“๐œ•๐‘บ=๐‘ทโŠ™(๐œ•โ„“๐œ•๐‘ทโˆ’((๐‘ทโŠ™๐œ•โ„“๐œ•๐‘ท)๐Ÿ™๐‘)๐Ÿ™๐‘T)๐œ•โ„“๐œ•๐‘ธ=๐›ผ๐œ•โ„“๐œ•๐‘บ๐‘ฒ๐œ•โ„“๐œ•๐‘ฒ=๐›ผ(๐œ•โ„“๐œ•๐‘บ)T๐‘ธ๐œ•โ„“๐œ•๐‘ฝ=๐‘ทT๐œ•โ„“๐œ•๐‘จ

    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 ๐‘พ1,๐‘พ2โˆˆโ„๐ทร—๐ทโ€ฒ.
  • Forward Pass:

    ๐‘จ=๐‘ฟ๐‘พ1โˆˆโ„๐‘ร—๐ทโ€ฒ๐‘ฉ=๐‘ฟ๐‘พ2โˆˆโ„๐‘ร—๐ทโ€ฒ๐’€=๐‘จโŠ™๐œŽ(๐‘ฉ)

  • Gradients:

    ๐œ•โ„“๐œ•๐‘จ=๐œ•โ„“๐œ•๐’€โŠ™๐œŽ(๐‘ฉ)๐œ•โ„“๐œ•๐‘ฉ=๐œ•โ„“๐œ•๐’€โŠ™๐‘จโŠ™๐œŽโ€ฒ(๐‘ฉ)๐œ•โ„“๐œ•๐‘ฟ=๐œ•โ„“๐œ•๐‘จ๐‘พ1T+๐œ•โ„“๐œ•๐‘ฉ๐‘พ2T๐œ•โ„“๐œ•๐‘พ1=๐‘ฟT๐œ•โ„“๐œ•๐‘จ๐œ•โ„“๐œ•๐‘พ2=๐‘ฟT๐œ•โ„“๐œ•๐‘ฉ

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

๐œŽ(๐‘ฅ)=๐‘ฅ1+exp(โˆ’๐‘ฅ),๐œŽโ€ฒ(๐‘ฅ)=1+(1+๐‘ฅ)exp(โˆ’๐‘ฅ)(1+exp(โˆ’๐‘ฅ))2

  • Input: a matrix ๐‘ฟโˆˆโ„๐‘ร—๐ท.
  • Learnable parameters: three matrices ๐‘พ1,๐‘พ2โˆˆโ„๐ทร—๐ทโ€ฒ,๐‘พ3โˆˆโ„๐ทโ€ฒร—๐ท.
  • Forward Pass:

    ๐‘จ=๐‘ฟ๐‘พ1โˆˆโ„๐‘ร—๐ทโ€ฒ๐‘ฉ=๐‘ฟ๐‘พ2โˆˆโ„๐‘ร—๐ทโ€ฒ๐‘ช=๐œŽ(๐‘ฉ)โˆˆโ„๐‘ร—๐ทโ€ฒ๐’€=๐‘จโŠ™๐‘ชโˆˆโ„๐‘ร—๐ทโ€ฒ๐’=๐’€๐‘พ3

  • Gradients:

    ๐œ•โ„“๐œ•๐’€=๐œ•โ„“๐œ•๐’๐‘พ3T๐œ•โ„“๐œ•๐‘จ=๐œ•โ„“๐œ•๐’€โŠ™๐‘ช๐œ•โ„“๐œ•๐‘ช=๐œ•โ„“๐œ•๐’€โŠ™๐‘จ๐œ•โ„“๐œ•๐‘ฉ=๐œ•โ„“๐œ•๐‘ชโŠ™๐œŽโ€ฒ(๐‘ฉ)๐œ•โ„“๐œ•๐‘ฟ=๐œ•โ„“๐œ•๐‘จ๐‘พ1T+๐œ•โ„“๐œ•๐‘ฉ๐‘พ2T๐œ•โ„“๐œ•๐‘พ3=๐’€T๐œ•โ„“๐œ•๐’๐œ•โ„“๐œ•๐‘พ1=๐‘ฟT๐œ•โ„“๐œ•๐‘จ๐œ•โ„“๐œ•๐‘พ2=๐‘ฟT๐œ•โ„“๐œ•๐‘ฉ

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 ๐œƒ๐‘’=(๐‘พ1๐‘’,๐‘พ2๐‘’,๐‘พ3๐‘’), where ๐‘พ1๐‘’,๐‘พ2๐‘’โˆˆโ„๐ทร—๐ทโ€ฒ,๐‘พ3๐‘’โˆˆโ„๐ทโ€ฒร—๐ท for ๐‘’โˆˆ[๐ธ].
  • Forward Pass: let ๐’™๐‘›,๐’š๐‘›โˆˆโ„1ร—๐ท be the ๐‘›-th rows of ๐‘ฟ,๐’€,

    ๐’™๐‘›,๐‘˜=๐’™๐‘›๐’›๐‘›,๐‘˜=๐‘“๐‘…๐‘›,๐‘˜(๐’™๐‘›,๐‘˜)โˆˆโ„1ร—๐ท๐’š๐‘›=โˆ‘๐‘˜=1๐พ๐บ๐‘›,๐‘˜๐’›๐‘›,๐‘˜

  • Gradients:

    ๐œ•โ„“๐œ•๐บ๐‘›,๐‘˜=๐œ•โ„“๐œ•๐’š๐‘›๐’›๐‘›,๐‘˜T๐œ•โ„“๐œ•๐’›๐‘›,๐‘˜=๐บ๐‘›,๐‘˜๐œ•โ„“๐œ•๐’š๐‘›๐œ•โ„“๐œ•๐’™๐‘›=โˆ‘๐‘˜=1๐พ๐œ•โ„“๐œ•๐’™๐‘›,๐‘˜๐œ•โ„“๐œ•๐œƒ๐‘’=โˆ‘(๐‘›,๐‘˜):๐‘…๐‘›,๐‘˜=๐‘’ฮ”๐œƒ๐‘›,๐‘˜

    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 โˆ‘๐‘’=1๐ธ๐‘๐‘’=๐‘๐พ) so all experts execute as a Grouped GEMM. Let ๐‘ท๐‘’โˆˆ{0,1}๐‘๐‘’ร—๐‘ be the binary gather matrix selecting the tokens routed to expert ๐‘’, and ๐’ˆ๐‘’โˆˆโ„๐‘๐‘’ be their corresponding gating scores from ๐‘ฎ.

  • Forward Pass:

    ๐‘ฟ๐‘’=๐‘ท๐‘’๐‘ฟโˆˆโ„๐‘๐‘’ร—๐ท๐’๐‘’=๐‘“๐‘’(๐‘ฟ๐‘’)โˆˆโ„๐‘๐‘’ร—๐ท๐’€=โˆ‘๐‘’=1๐ธ๐‘ท๐‘’T(diag(๐’ˆ๐‘’)๐’๐‘’)

  • Gradients: let ๐‘ผ๐‘’=๐‘ท๐‘’๐œ•โ„“๐œ•๐’€โˆˆโ„๐‘๐‘’ร—๐ท,

    ๐œ•โ„“๐œ•๐’ˆ๐‘’=(๐‘ผ๐‘’โŠ™๐’๐‘’)๐Ÿ™๐ท๐œ•โ„“๐œ•๐’๐‘’=diag(๐’ˆ๐‘’)๐‘ผ๐‘’๐œ•โ„“๐œ•๐‘ฟ=โˆ‘๐‘’=1๐ธ๐‘ท๐‘’T๐œ•โ„“๐œ•๐‘ฟ๐‘’

    where ๐œ•โ„“๐œ•๐‘ฟ๐‘’ is obtained from the SwiGLU FFN backward pass of ๐‘“๐‘’ on (๐‘ฟ๐‘’,๐œ•โ„“๐œ•๐’๐‘’) via Grouped GEMM (which also yields ๐œ•โ„“๐œ•๐œƒ๐‘’ directly), and ๐œ•โ„“๐œ•๐’ˆ๐‘’ is scattered back to ๐œ•โ„“๐œ•๐‘ฎ.

Fr4nk1in ยฉ 2025 โ‹… Built with Tola & Tufted