Kernel: rmsnorm (Level 1, category: normalization)
Input shapes: [[2048, 1024]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 1e-06)
Pallas runtime: 170.3765 ms; JAX XLA compiled runtime: 0.0798 ms; speedup vs compiled baseline: 0.0005

JAX reference implementation:
@jax.jit
def jax_rmsnorm(x: jax.Array) -> jax.Array:
    ms = jnp.mean(x ** 2, axis=-1, keepdims=True)
    return x / jnp.sqrt(ms + 1e-5)

Original Pallas kernel (TPU-oriented): """Level 1: RMS normalization via Pallas.

Demonstrates: squared-mean reduction, rsqrt pattern.
Inspired by pallas-forge's RMSNorm kernel (3.44x over XLA).
"""

from pallasbench.provenance import describe_task as _describe_task

__doc__ = _describe_task("L1/rmsnorm", __doc__)

import jax
import jax.numpy as jnp
from jax.experimental import pallas as pl


def _rmsnorm_kernel(x_ref, o_ref):
    x = x_ref[...]
    ms = jnp.mean(x ** 2, axis=-1, keepdims=True)
    o_ref[...] = x * jax.lax.rsqrt(ms + 1e-5)


def pallas_rmsnorm(x: jax.Array) -> jax.Array:
    n_rows = x.shape[0]
    block_rows = min(128, n_rows)
    n_cols = x.shape[1]
    grid_size = n_rows // block_rows

    return pl.pallas_call(
        _rmsnorm_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=(grid_size,),
        in_specs=[pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0))],
        out_specs=pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
    )(x)


pallas_kernel = pallas_rmsnorm
task_name = "rmsnorm"
input_shapes = [(2048, 1024)]
category = "normalization"
level = 1

Fixed Pallas kernel (GPU-compatible): """Level 1: RMS normalization via Pallas.

Demonstrates: squared-mean reduction, rsqrt pattern.
Inspired by pallas-forge's RMSNorm kernel (3.44x over XLA).
"""

from pallasbench.provenance import describe_task as _describe_task

__doc__ = _describe_task("L1/rmsnorm", __doc__)

import jax
import jax.numpy as jnp
from jax.experimental import pallas as pl


def _rmsnorm_kernel(x_ref, o_ref):
    x = x_ref[...]
    ms = jnp.mean(x ** 2, axis=-1, keepdims=True)
    o_ref[...] = x * jax.lax.rsqrt(ms + 1e-5)


def pallas_rmsnorm(x: jax.Array) -> jax.Array:
    n_rows = x.shape[0]
    block_rows = min(128, n_rows)
    n_cols = x.shape[1]
    while block_rows * n_cols > 16384 and block_rows > 1:
        block_rows //= 2
    grid_size = n_rows // block_rows

    return pl.pallas_call(
        _rmsnorm_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=(grid_size,),
        in_specs=[pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0))],
        out_specs=pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
    )(x)


pallas_kernel = pallas_rmsnorm
task_name = "rmsnorm"
input_shapes = [(2048, 1024)]
category = "normalization"
level = 1

Diff of GPU fix: --- a/rmsnorm.py
+++ b/rmsnorm.py
@@ -23,6 +23,8 @@
     n_rows = x.shape[0]
     block_rows = min(128, n_rows)
     n_cols = x.shape[1]
+    while block_rows * n_cols > 16384 and block_rows > 1:
+        block_rows //= 2
     grid_size = n_rows // block_rows
 
     return pl.pallas_call(

Jaxpr IR: { lambda ; a:f32[2048,1024]. let
    b:f32[2048,1024] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(128,), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=1024))), BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=1024)))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; c:Ref<default>{f32[16,1024]} d:Ref<default>{f32[16,1024]}. let
          e:f32[16,1024] <- c[...]
          f:f32[16,1024] = integer_pow[y=2] e
          g:f32[16] = reduce_sum[axes=(1,) out_sharding=None] f
          h:f32[16,1] = broadcast_in_dim[broadcast_dimensions=(0,)] g
          i:f32[16,1] = div h 1024.0:f32[]
          j:f32[16,1] = add i 9.999999747378752e-06:f32[]
          k:f32[16,1] = rsqrt j
          l:f32[16,1024] = mul e k
          m:f32[16,1024], d[...] <- d[...], l
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[2048,1024]),)
    ] a
  in (b,) }