NormalizationBenchmarks pages
Benchmarks
Normalization
LayerNorm and RMSNorm forward bandwidth on a 4096 × 4096 matrix, using the kernels from the examples.
What it measures
bench_norms runs two example kernels on a 4096 × 4096 matrix in float32 and float16, one row per program, with 8 SIMD groups:
- LayerNorm from
examples/03_layernorm.py, withBLOCK=1024andBLOCK=4096. In the style of the Triton tutorial, the kernel reads each row three times: once for the mean, once for the variance, and once to normalize. It also writes the per-row mean and reciprocal standard deviation. - RMSNorm from
examples/06_rmsnorm.py, withBLOCK=4096, which holds the whole row in one block.
Times are the minimum of 20 GPU-timed runs. GB/s counts the minimum traffic, one read and one write of the matrix, so the LayerNorm rereads don't count. The script has no MLX or PyTorch comparison.
Results
The following table lists the minimum time and the bandwidth for each kernel:
| Kernel | dtype | BLOCK | Min (ms) | GB/s |
|---|---|---|---|---|
| LayerNorm | float32 | 1024 | 0.565 | 237.4 |
| LayerNorm | float32 | 4096 | 0.573 | 234.4 |
| RMSNorm | float32 | 4096 | 0.560 | 239.5 |
| LayerNorm | float16 | 1024 | 0.278 | 241.8 |
| LayerNorm | float16 | 4096 | 0.277 | 241.9 |
| RMSNorm | float16 | 4096 | 0.271 | 247.4 |
Reading the results
- RMSNorm runs at copy bandwidth. At 239.5 GB/s in
float32and 247.4 GB/s infloat16, it's close to the row softmax on the same matrix. - LayerNorm's rereads mostly hit the cache. With
BLOCK=1024, LayerNorm reaches 237.4 and 241.8 GB/s, within 1-3% of RMSNorm, even though it reads each row three times. - Block size matters little. In
float32,BLOCK=4096costs about 1% againstBLOCK=1024: 234.4 against 237.4 GB/s. Infloat16, the two block sizes run at the same speed, 241.9 and 241.8 GB/s.
For the kernels themselves, see the LayerNorm and RMSNorm pages in Examples.
Raw output
The following output is the bench_norms section of the results file:
Device: Apple M4 Pro; 4096 x 4096
LayerNorm float32 BLOCK=1024 0.565 ms 237.4 GB/s
LayerNorm float32 BLOCK=4096 0.573 ms 234.4 GB/s
RMSNorm float32 BLOCK=4096 0.560 ms 239.5 GB/s
LayerNorm float16 BLOCK=1024 0.278 ms 241.8 GB/s
LayerNorm float16 BLOCK=4096 0.277 ms 241.9 GB/s
RMSNorm float16 BLOCK=4096 0.271 ms 247.4 GB/sSource
"""LayerNorm and RMSNorm forward at 4096 x 4096, using the kernels in `examples/`.
GB/s counts the minimum traffic: one read and one write of the matrix. The LayerNorm
example reads each row three times (Triton-tutorial style); the rereads mostly hit the
cache.
Run with `uv run python benchmarks/bench_norms.py`.
"""
from __future__ import annotations
import importlib.util
from pathlib import Path
import enceladus
from enceladus.testing import do_bench
ROOT = Path(__file__).resolve().parents[1]
M = N = 4096
def load(stem: str):
spec = importlib.util.spec_from_file_location(stem, ROOT / "examples" / f"{stem}.py")
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def main() -> None:
ln, rms = load("03_layernorm"), load("06_rmsnorm")
print(f"Device: {enceladus.get_device().caps.name}; {M} x {N}")
for dtype in ("float32", "float16"):
x = enceladus.randn(M, N, dtype=dtype)
y = enceladus.empty_like(x)
w, b = enceladus.randn(N, dtype=dtype), enceladus.randn(N, dtype=dtype)
mean, rstd = enceladus.empty(M), enceladus.empty(M)
nbytes = 2 * M * N * x.itemsize
for block in (1024, 4096):
def run_ln(block=block):
ln.layernorm_kernel[(M,)](x, y, w, b, mean, rstd, N, N, N, 1e-5, BLOCK=block,
num_warps=8) # fmt: skip
ms = do_bench(run_ln, rep=20, return_mode="min")
print(f"LayerNorm {dtype} BLOCK={block:<5} {ms:7.3f} ms {nbytes / ms / 1e6:7.1f} GB/s")
def run_rms():
rms.rmsnorm_kernel[(M,)](x, w, y, N, N, N, 1e-6, BLOCK=N, num_warps=8)
ms = do_bench(run_rms, rep=20, return_mode="min")
print(f"RMSNorm {dtype} BLOCK={N:<5} {ms:7.3f} ms {nbytes / ms / 1e6:7.1f} GB/s")
if __name__ == "__main__":
main()