Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00
..

2D Layer Normalization Backward

This example demonstrates the backward pass of 2D Layer Normalization. This operation computes the gradients of the loss with respect to the input, gamma, and beta parameters of a layer normalization layer, which is essential for training neural networks that use layer normalization, particularly Transformers.

Mathematical Formulation

The backward pass of layer normalization involves computing gradients for three components: input X, scale parameter gamma, and shift parameter beta.

Given:

  • Input tensor X with shape [M, N]
  • Scale parameter gamma with shape [N]
  • Shift parameter beta with shape [N]
  • Output gradients dL/dY with shape [M, N]

From the forward pass, we have:

  • Mean: \mu_i = \frac{1}{N} \sum_{j=0}^{N-1} X_{ij} for each row i
  • Variance: \sigma_i^2 = \frac{1}{N} \sum_{j=0}^{N-1} (X_{ij} - \mu_i)^2
  • Normalized: \hat{X}_{ij} = \frac{X_{ij} - \mu_i}{\sqrt{\sigma_i^2 + \epsilon}}
  • Output: Y_{ij} = \gamma_j \cdot \hat{X}_{ij} + \beta_j

Gradient Computations

Gradient w.r.t. beta: \frac{\partial L}{\partial \beta_j} = \sum_{i=0}^{M-1} \frac{\partial L}{\partial Y_{ij}}

Gradient w.r.t. gamma: \frac{\partial L}{\partial \gamma_j} = \sum_{i=0}^{M-1} \frac{\partial L}{\partial Y_{ij}} \cdot \hat{X}_{ij}

Gradient w.r.t. input (most complex): \frac{\partial L}{\partial X_{ij}} = \frac{\gamma_j}{\sqrt{\sigma_i^2 + \epsilon}} \left[ \frac{\partial L}{\partial Y_{ij}} - \frac{1}{N}\left(\frac{\partial L}{\partial \beta_j} + \hat{X}_{ij} \frac{\partial L}{\partial \gamma_j}\right) \right]

Where the gradient w.r.t. input involves the normalized input values and requires careful handling of the mean and variance computations.

Algorithmic Strategy: Multi-Pass Gradient Computation

The backward pass requires multiple reduction operations and careful coordination between gradient computations.

  1. Pass 1: Compute Gamma and Beta Gradients

    • Grid Scheduling: Parallelize over features (N dimension).
    • Reduction per Feature: For each feature j, reduce across the batch dimension (M) to compute:
      • grad_beta[j] = sum(grad_output[:, j])
      • grad_gamma[j] = sum(grad_output[:, j] * x_normalized[:, j])
  2. Pass 2: Compute Input Gradients

    • Grid Scheduling: Parallelize over rows (M dimension).
    • Per-Row Computation: For each row i:
      • Read the previously computed grad_beta and grad_gamma
      • Compute intermediate values needed for the input gradient formula
      • Apply the complex gradient formula for each element in the row
  3. Memory Management:

    • Store intermediate statistics (mean, variance, normalized values) from forward pass or recompute them
    • Use shared memory for efficient intra-block reductions
    • Optimize memory access patterns for coalescing

Source Code Organization

Build and Run

Prerequisites

Ensure the Composable Kernel library is built and installed.

cd /path/to/composable_kernel/build
make -j install

Build the Example

cd /path/to/composable_kernel/example/53_layernorm2d_bwd
mkdir build && cd build

cmake \
  -DCMAKE_CXX_COMPILER=/opt/rocm/bin/hipcc \
  -DCMAKE_PREFIX_PATH="/opt/rocm;${CK_INSTALL_PATH}" \
  ..

make -j

Run the Example

# Run the example with default settings
./layernorm2d_bwd_xdl

# Run with verification, data initialization, and timing
./layernorm2d_bwd_xdl 1 2 1

Computational Complexity

The backward pass of layer normalization has similar computational complexity to the forward pass but requires additional memory for storing gradients:

  • Time Complexity: O(M × N) for each gradient computation
  • Memory Complexity: O(M × N) for input gradients plus O(N) for parameter gradients
  • Numerical Stability: Requires careful handling of the variance computation and division operations

Role in Transformer Training

Layer normalization backward is crucial for training Transformer models:

  • Gradient Flow: Provides stable gradient propagation through normalization layers
  • Parameter Updates: Enables learning of the scale (gamma) and shift (beta) parameters
  • Training Stability: The normalization helps maintain stable gradients throughout the network
  • Convergence: Proper implementation is essential for achieving good convergence rates in Transformer training

The efficient implementation of this operation is critical for the overall training performance of large language models and other Transformer-based architectures.