Kernel: reduce_mean (Level 1, category: reduce)
Input shapes: [[4096, 2048]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0)
Pallas runtime: 97.0746 ms; JAX XLA compiled runtime: 0.1352 ms; speedup vs compiled baseline: 0.0014

JAX reference implementation:
@jax.jit
def jax_reduce_mean(x: jax.Array) -> jax.Array:
    return jnp.mean(x, axis=-1)

Original Pallas kernel (TPU-oriented): """Level 1: Row-wise mean reduction via Pallas.

Provenance: jnp.mean reduction, used in normalization layers
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _reduce_mean_kernel(x_ref, o_ref):
    o_ref[...] = jnp.mean(x_ref[...], axis=-1)


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

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


pallas_kernel = pallas_reduce_mean
task_name = "reduce_mean"
input_shapes = [(4096, 2048)]
category = "reduce"
level = 1

Fixed Pallas kernel (GPU-compatible): """Level 1: Row-wise mean reduction via Pallas.

Provenance: jnp.mean reduction, used in normalization layers
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _reduce_mean_kernel(x_ref, o_ref):
    o_ref[...] = jnp.mean(x_ref[...], axis=-1)


def pallas_reduce_mean(x: jax.Array) -> jax.Array:
    n_rows = x.shape[0]
    n_cols = x.shape[1]
    block_rows = min(256, 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(
        _reduce_mean_kernel,
        out_shape=jax.ShapeDtypeStruct((n_rows,), x.dtype),
        grid=(grid_size,),
        in_specs=[pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0))],
        out_specs=pl.BlockSpec((block_rows,), lambda i: (i,)),
    )(x)


pallas_kernel = pallas_reduce_mean
task_name = "reduce_mean"
input_shapes = [(4096, 2048)]
category = "reduce"
level = 1

Diff of GPU fix: --- a/reduce_mean.py
+++ b/reduce_mean.py
@@ -21,6 +21,8 @@
     n_rows = x.shape[0]
     n_cols = x.shape[1]
     block_rows = min(256, 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[4096,2048]. let
    b:f32[4096] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(512,), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=8), Blocked(block_size=2048))), BlockMapping(block_shape=(Blocked(block_size=8),))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; c:Ref<default>{f32[8,2048]} d:Ref<default>{f32[8]}. let
          e:f32[8,2048] <- c[...]
          f:f32[8] = reduce_sum[axes=(1,) out_sharding=None] e
          g:f32[8] = div f 2048.0:f32[]
          h:f32[8], d[...] <- d[...], g
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[4096]),)
    ] a
  in (b,) }