Skip to main content

Fused Triton kernels for RMSNorm, LayerNorm, FusedAddRMSNorm, GroupNorm, InstanceNorm, ScaleNorm

Project description

torchnorm

Fused Triton kernels for normalisation layers. Drop-in replacements for PyTorch's norm modules with automatic dispatch to optimised CUDA kernels when triton is available.

Installation

pip install torchnorm            # PyTorch fallback only
pip install "torchnorm[triton]"  # with Triton fused kernels

From source:

git clone https://github.com/liodon-ai/torchnorm
cd torchnorm
pip install -e ".[triton]"

Modules

RMSNorm

Root Mean Square Layer Normalisation (Zhang & Sennrich, 2019). Used in LLaMA, Mistral, Gemma, Qwen, Falcon, and most modern LLMs.

from torchnorm import RMSNorm

norm = RMSNorm(dim=4096).cuda()
x = torch.randn(4, 512, 4096, device="cuda", dtype=torch.float16)
out = norm(x)  # fused Triton kernel on CUDA, PyTorch fallback elsewhere

The fused kernel reads each element once, computes the RMS, and applies the weight in a single pass — no intermediate allocation.

FusedAddRMSNorm

Fuses the residual add and RMSNorm into one kernel. Canonical pattern in transformer pre-norm blocks:

from torchnorm import FusedAddRMSNorm

norm = FusedAddRMSNorm(dim=4096).cuda()

# Instead of:
#   residual = x + residual
#   hidden   = rms_norm(residual)

# Write:
hidden, residual = norm(x, residual)

Returns both the normalised output and the updated residual so the next block can use it directly.

LayerNorm

Layer Normalisation (Ba et al., 2016). Identical interface to torch.nn.LayerNorm.

from torchnorm import LayerNorm

norm = LayerNorm(768, bias=False).cuda()   # BERT-style, no bias
out  = norm(x)

GroupNorm

Group Normalisation (Wu & He, 2018). Standard for diffusion UNets and CNNs.

from torchnorm import GroupNorm

norm = GroupNorm(num_groups=32, num_channels=512).cuda()
out  = norm(x)   # x: (N, C, H, W)

InstanceNorm

Instance Normalisation (Ulyanov et al., 2016). Normalises each sample–channel pair over spatial dims. Standard in style transfer.

from torchnorm import InstanceNorm

norm = InstanceNorm(num_features=512, affine=True).cuda()
out  = norm(x)   # x: (N, C, H, W) or (N, C)

ScaleNorm

ScaleNorm (Nguyen & Salazar, 2019). L2-normalises and scales by a single learnable scalar. Cheaper than LayerNorm — no mean subtraction, no per-element parameters.

from torchnorm import ScaleNorm

norm = ScaleNorm(dim=512).cuda()
out  = norm(x)

Dispatch logic

Every module auto-selects the fastest available path:

Condition Path
CUDA tensor + triton installed Fused Triton kernel
CPU tensor or triton not installed PyTorch (F.rms_norm, F.layer_norm, …)

No flags to set. Installing triton is the only opt-in required.

Compatibility

Python ≥ 3.9
PyTorch ≥ 2.0
Triton ≥ 2.1 (optional)
CUDA any version supported by your PyTorch build

Related projects

Project Focus Install
Liger Kernel Full training suite: RMSNorm + RoPE + SwiGLU + CrossEntropy + model patching pip install liger-kernel
flash-attn Flash Attention + fused ops including LayerNorm build from source
torchembed Companion library: RoPE (rotate-half & adjacent-pairs), ALiBi, patch, categorical embeddings pip install torchembed

torchnorm vs Liger Kernel: Liger patches entire model classes at import time — ideal when you want the full optimization suite with use_liger_kernel=True. torchnorm provides individual nn.Module drop-ins without model patching — useful when building custom architectures or when you only need the norm layers.

Part of the Liodon AI stack

  • torchembed — fused kernels for embedding layers (RoPE, patch, categorical)
  • torchnorm — fused kernels for normalisation layers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchnorm-0.1.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file torchnorm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: torchnorm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for torchnorm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9bca26fec4d73eb2e86688ead1040d8d25a25b3744638248216799acbf3bed3c
MD5 dafa3cb5fd2c2b33428accf3fffb67f0
BLAKE2b-256 1a5dffc108d36a8ebd8d7a51ba325aad01cd0ba4b3cf79db4c9bb91ece62acf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page