Kernel: layernorm (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: 348.5668 ms; JAX XLA compiled runtime: 0.144 ms; speedup vs compiled baseline: 0.0004

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

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

Demonstrates: mean/variance reduction, epsilon stability, row-parallel tiling.
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _layernorm_kernel(x_ref, o_ref):
    x = x_ref[...]
    mean = jnp.mean(x, axis=-1, keepdims=True)
    var = jnp.var(x, axis=-1, keepdims=True)
    o_ref[...] = (x - mean) / jnp.sqrt(var + 1e-5)


def pallas_layernorm(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(
        _layernorm_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_layernorm
task_name = "layernorm"
input_shapes = [(2048, 1024)]
category = "normalization"
level = 1

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

Demonstrates: mean/variance reduction, epsilon stability, row-parallel tiling.
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _layernorm_kernel(x_ref, o_ref):
    x = x_ref[...]
    mean = jnp.mean(x, axis=-1, keepdims=True)
    var = jnp.var(x, axis=-1, keepdims=True)
    o_ref[...] = (x - mean) / jnp.sqrt(var + 1e-5)


def pallas_layernorm(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(
        _layernorm_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_layernorm
task_name = "layernorm"
input_shapes = [(2048, 1024)]
category = "normalization"
level = 1

Diff of GPU fix: --- a/layernorm.py
+++ b/layernorm.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] = reduce_sum[axes=(1,) out_sharding=None] e
          g:f32[16,1] = broadcast_in_dim[broadcast_dimensions=(0,)] f
          h:f32[16,1] = div g 1024.0:f32[]
          i:f32[16,1] = jit[
            name=_var
            jaxpr={ lambda ; e:f32[16,1024] j:i32[]. let
                k:f32[16] = reduce_sum[axes=(1,) out_sharding=None] e
                l:f32[16,1] = broadcast_in_dim[broadcast_dimensions=(0,)] k
                m:f32[16,1] = div l 1024.0:f32[]
                n:f32[16,1024] = sub e m
                o:f32[16,1024] = square n
                p:f32[] = convert_element_type[
                  new_dtype=float32
                  weak_type=False
                ] j
                q:f32[] = sub 1024.0:f32[] p
                r:f32[16] = reduce_sum[axes=(1,) out_sharding=None] o
                s:f32[16,1] = broadcast_in_dim[broadcast_dimensions=(0,)] r
                t:f32[16,1] = div s q
                u:bool[] = gt q 0.0:f32[]
                i:f32[16,1] = jit[
                  name=_where
                  jaxpr={ lambda ; u:bool[] t:f32[16,1] v:f32[]. let
                      w:f32[] = convert_element_type[
                        new_dtype=float32
                        weak_type=False
                      ] v
                      x:f32[16,1] = broadcast_in_dim w
                      i:f32[16,1] = select_n u x t
                    in (i,) }
                ] u t nan:f32[]
              in (i,) }
          ] e 0:i32[]
          y:f32[16,1024] = sub e h
          z:f32[16,1] = add i 9.999999747378752e-06:f32[]
          ba:f32[16,1] = sqrt z
          bb:f32[16,1024] = div y ba
          bc:f32[16,1024], d[...] <- d[...], bb
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[2048,1024]),)
    ] a
  in (b,) }