Write Triton LayerNorm Kernel

Write a Triton LayerNorm kernel with RMSNorm support and masking.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Implements a numerically stable, fused LayerNorm kernel in Triton with optional RMSNorm support for fast, single-pass normalization across rows, including stable mean/variance computation and optional affine gamma/beta application.

Core Features & Use Cases

  • Fused LayerNorm (mean subtraction + std normalization) across rows with vectorized tile processing.
  • RMSNorm variant (no mean subtraction) with learnable gamma/beta and optional training path.
  • Backward pass readiness: outputs mean and rstd for gradient computation and supports masking for non-divisible hidden dimensions in inputs.

Quick Start

Load the Triton kernel and run it on a (N, H) input with gamma and beta, verifying numeric stability against a reference LayerNorm.

Frequently Asked Questions about Write Triton LayerNorm Kernel

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

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

To implement numerically stable LayerNorm in Triton, use per-row program instances for single-pass mean and variance computation, enforcing PyTorch-compatible epsilon placement and dividing the denominator by H.

Can I use a single Triton kernel for both LayerNorm and RMSNorm?

Yes, a single Triton kernel can support both LayerNorm and RMSNorm by toggling mean subtraction, allowing optional learnable gamma and beta affine parameters for flexible normalization across rows.

Does this Triton LayerNorm kernel support backward pass gradient computation?

Yes, the Triton LayerNorm kernel is backward pass ready by outputting mean and rstd intermediate values during the forward training pathway, providing necessary data for gradient computation.

How does the Triton normalization kernel handle non-divisible hidden dimensions?

The Triton normalization kernel handles non-divisible hidden dimensions by masking out-of-bounds elements during vectorized tile processing, ensuring correct computation while keeping memory traffic efficient.

What is the best way to apply affine parameters in a fused GPU normalization kernel?

The best way to apply affine parameters in a fused GPU normalization kernel is integrating learnable gamma and beta directly into the single-pass normalization execution path.

Why does my custom Triton LayerNorm output mismatch the PyTorch reference implementation?

Custom Triton LayerNorm outputs mismatch PyTorch references when epsilon placement is incorrect or the variance denominator fails to divide by H, requiring strict numerical alignment for stability.