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 |
Part of the Liodon AI stack
- torchembed — fused kernels for embedding layers (RoPE, patch, categorical)
- torchnorm — fused kernels for normalisation layers
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchnorm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: torchnorm-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a17473dbc3a6eeecf116b3d6691be729b880818efdba01ffef100007119dbe6
|
|
| MD5 |
ce4d1e96b06fabfc99b1b39dad5cf3ec
|
|
| BLAKE2b-256 |
fe67638e7cb9d4981cc7375942a7bd162c2e3baedb2125df619b60952e9dfdb1
|