Kernel: reduce_sum (Level 1, category: reduce)
Input shapes: [[4096, 2048]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 1.9e-05)
Pallas runtime: 94.2345 ms; JAX XLA compiled runtime: 0.1039 ms; speedup vs compiled baseline: 0.0011

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

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

Demonstrates: reduction along an axis, row-parallel BlockSpec.
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _reduce_sum_kernel(x_ref, o_ref):
    o_ref[...] = jnp.sum(x_ref[...], axis=-1)


def pallas_reduce_sum(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_sum_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_sum
task_name = "reduce_sum"
input_shapes = [(4096, 2048)]
category = "reduce"
level = 1

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

Demonstrates: reduction along an axis, row-parallel BlockSpec.
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _reduce_sum_kernel(x_ref, o_ref):
    o_ref[...] = jnp.sum(x_ref[...], axis=-1)


def pallas_reduce_sum(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_sum_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_sum
task_name = "reduce_sum"
input_shapes = [(4096, 2048)]
category = "reduce"
level = 1

Diff of GPU fix: --- a/reduce_sum.py
+++ b/reduce_sum.py
@@ -20,6 +20,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], d[...] <- d[...], f
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[4096]),)
    ] a
  in (b,) }