Kernel: rmsnorm_residual (Level 2, category: norm_residual)
Input shapes: [[2048, 1024], [2048, 1024]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 1e-06)
Pallas runtime: 215.7769 ms; JAX XLA compiled runtime: 0.1216 ms; speedup vs compiled baseline: 0.0006

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

Original Pallas kernel (TPU-oriented): """Level 2: Fused RMSNorm + Residual Add via Pallas.

Demonstrates: two-input fusion, norm + elementwise add in one kernel.
Inspired by pallas-forge's 3.44x speedup over XLA for this pattern.
"""


from pallasbench.provenance import describe_task as _describe_task

__doc__ = _describe_task("L2/rmsnorm_residual", __doc__)

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


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


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

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


pallas_kernel = pallas_rmsnorm_residual
task_name = "rmsnorm_residual"
input_shapes = [(2048, 1024), (2048, 1024)]
category = "norm_residual"
level = 2

Fixed Pallas kernel (GPU-compatible): """Level 2: Fused RMSNorm + Residual Add via Pallas.

Demonstrates: two-input fusion, norm + elementwise add in one kernel.
Inspired by pallas-forge's 3.44x speedup over XLA for this pattern.
"""


from pallasbench.provenance import describe_task as _describe_task

__doc__ = _describe_task("L2/rmsnorm_residual", __doc__)

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


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


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

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


pallas_kernel = pallas_rmsnorm_residual
task_name = "rmsnorm_residual"
input_shapes = [(2048, 1024), (2048, 1024)]
category = "norm_residual"
level = 2

Diff of GPU fix: --- a/rmsnorm_residual.py
+++ b/rmsnorm_residual.py
@@ -28,6 +28,8 @@
     n_rows = x.shape[0]
     n_cols = x.shape[1]
     block_rows = min(128, n_rows)
+    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] b:f32[2048,1024]. let
    c: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))), BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=1024)))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; d:Ref<default>{f32[16,1024]} e:Ref<default>{f32[16,1024]}
          f:Ref<default>{f32[16,1024]}. let
          g:f32[16,1024] <- d[...]
          h:f32[16,1024] <- e[...]
          i:f32[16,1024] = integer_pow[y=2] g
          j:f32[16] = reduce_sum[axes=(1,) out_sharding=None] i
          k:f32[16,1] = broadcast_in_dim[broadcast_dimensions=(0,)] j
          l:f32[16,1] = div k 1024.0:f32[]
          m:f32[16,1] = add l 9.999999747378752e-06:f32[]
          n:f32[16,1] = rsqrt m
          o:f32[16,1024] = mul g n
          p:f32[16,1024] = add o h
          q:f32[16,1024], f[...] <- f[...], p
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[2048,1024]),)
    ] a b
  in (c,) }