Write CUDA LayerNorm Kernel

Write CUDA LayerNorm kernels with Welford reduction and FP32 accumulation.

54|7|Updated Apr 10, 2026
One-click install
npx skills add https://github.com/tensormux/kernel-skills --skill write-cuda-layernorm-kernel
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Write CUDA LayerNorm Kernel
Source: https://github.com/tensormux/kernel-skills/tree/main/skills/cuda/write-cuda-layernorm-kernel
Command: npx skills add https://github.com/tensormux/kernel-skills --skill write-cuda-layernorm-kernel

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a correct, high-performance CUDA implementation of LayerNorm (and RMSNorm) kernels with numerically stable mean/variance computation, supporting forward and backward passes, and non-standard normalization axes.

Core Features & Use Cases

  • Welford online accumulation: single-pass mean/variance computation for numerical stability.
  • RMSNorm variant: optional variant that normalizes by root mean square without mean subtraction.
  • Backward support: optional backward pass with saved intermediates for gradient computation.
  • Non-standard shapes: handles arbitrary hidden dimensions including non-power-of-two sizes and 3D inputs (N x S x D) with proper per-row normalization.
  • FP32 accumulation: all variance and accumulation in FP32 even if inputs are FP16/BF16.

Quick Start

Run the CUDA LayerNorm kernel on a tensor of shape [N, D] or [N, S, D] and verify the output is properly normalized with optional gamma/beta or RMSNorm variant.

Frequently Asked Questions about Write CUDA LayerNorm Kernel

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a numerically stable CUDA LayerNorm kernel?

A numerically stable CUDA LayerNorm kernel uses a single-pass Welford reduction for mean and variance, combined with FP32 accumulation to prevent overflow when processing FP16 or BF16 inputs.

Does this CUDA LayerNorm implementation support non-power-of-two hidden dimensions?

Yes, the CUDA LayerNorm implementation handles arbitrary hidden dimensions, including non-power-of-two sizes, by performing proper per-row normalization across the normalization dimension for N x S x D tensor shapes.

Can I compute RMSNorm and backward gradients with this CUDA kernel?

Yes, the kernel includes an optional RMSNorm variant that normalizes by root mean square without mean subtraction, and supports a backward pass with saved intermediates for gradient computation.

Why should I use Welford online accumulation for GPU LayerNorm?

Welford online accumulation computes mean and variance in a single pass, ensuring numerical stability and reducing memory bandwidth usage compared to two-pass variance methods in GPU LayerNorm kernels.

How do I handle epsilon placement in a custom CUDA LayerNorm kernel?

Epsilon should be placed inside the rsqrtf function as rsqrtf(var + epsilon), ensuring the CUDA LayerNorm kernel applies normalization correctly and prevents division by zero during variance scaling.

What is the best way to optimize LayerNorm performance for FP16 inputs on GPU?

The best way to optimize LayerNorm for FP16 inputs is to use FP32 accumulation for all variance and mean computations, ensuring numerical stability while maintaining high throughput in your CUDA kernel.