Kernel: relu (Level 1, category: activation)
Input shapes: [[4096, 4096]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0)
Pallas runtime: 104.7967 ms; JAX XLA compiled runtime: 0.2017 ms; speedup vs compiled baseline: 0.0019

JAX reference implementation:
@jax.jit
def jax_relu(x: jax.Array) -> jax.Array:
    return jnp.maximum(x, 0)

Original Pallas kernel (TPU-oriented): """Level 1: Elementwise ReLU via Pallas.

Demonstrates: basic pallas_call, grid, BlockSpec, program_id.
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _relu_kernel(x_ref, o_ref):
    x = x_ref[...]
    o_ref[...] = jnp.maximum(x, 0)


def pallas_relu(x: jax.Array) -> jax.Array:
    n = x.shape[0]
    block_size = min(1024, n)
    grid_size = n // block_size

    return pl.pallas_call(
        _relu_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=(grid_size,),
        in_specs=[pl.BlockSpec((block_size, *x.shape[1:]), lambda i: (i, *([0] * (x.ndim - 1))))],
        out_specs=pl.BlockSpec((block_size, *x.shape[1:]), lambda i: (i, *([0] * (x.ndim - 1)))),
    )(x)


pallas_kernel = pallas_relu
task_name = "relu"
input_shapes = [(4096, 4096)]
category = "activation"
level = 1

Fixed Pallas kernel (GPU-compatible): """Level 1: Elementwise ReLU via Pallas.

Demonstrates: basic pallas_call, grid, BlockSpec, program_id.
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _relu_kernel(x_ref, o_ref):
    x = x_ref[...]
    o_ref[...] = jnp.maximum(x, 0)


def pallas_relu(x: jax.Array) -> jax.Array:
    bm = min(128, x.shape[0])
    bn = min(128, x.shape[1]) if x.ndim > 1 else x.shape[0]
    grid = (x.shape[0] // bm, x.shape[1] // bn) if x.ndim > 1 else (x.shape[0] // bm,)
    if x.ndim > 1:
        in_sp = [pl.BlockSpec((bm, bn), lambda i, j: (i, j))]
        out_sp = pl.BlockSpec((bm, bn), lambda i, j: (i, j))
    else:
        in_sp = [pl.BlockSpec((bm,), lambda i: (i,))]
        out_sp = pl.BlockSpec((bm,), lambda i: (i,))
    return pl.pallas_call(
        _relu_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=grid,
        in_specs=in_sp,
        out_specs=out_sp,
    )(x)


pallas_kernel = pallas_relu
task_name = "relu"
input_shapes = [(4096, 4096)]
category = "activation"
level = 1

Diff of GPU fix: --- a/relu.py
+++ b/relu.py
@@ -18,16 +18,21 @@
 
 
 def pallas_relu(x: jax.Array) -> jax.Array:
-    n = x.shape[0]
-    block_size = min(1024, n)
-    grid_size = n // block_size
-
+    bm = min(128, x.shape[0])
+    bn = min(128, x.shape[1]) if x.ndim > 1 else x.shape[0]
+    grid = (x.shape[0] // bm, x.shape[1] // bn) if x.ndim > 1 else (x.shape[0] // bm,)
+    if x.ndim > 1:
+        in_sp = [pl.BlockSpec((bm, bn), lambda i, j: (i, j))]
+        out_sp = pl.BlockSpec((bm, bn), lambda i, j: (i, j))
+    else:
+        in_sp = [pl.BlockSpec((bm,), lambda i: (i,))]
+        out_sp = pl.BlockSpec((bm,), lambda i: (i,))
     return pl.pallas_call(
         _relu_kernel,
         out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
-        grid=(grid_size,),
-        in_specs=[pl.BlockSpec((block_size, *x.shape[1:]), lambda i: (i, *([0] * (x.ndim - 1))))],
-        out_specs=pl.BlockSpec((block_size, *x.shape[1:]), lambda i: (i, *([0] * (x.ndim - 1)))),
+        grid=grid,
+        in_specs=in_sp,
+        out_specs=out_sp,
     )(x)
 
 

Jaxpr IR: { lambda ; a:f32[4096,4096]. let
    b:f32[4096,4096] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(32, 32), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=128), Blocked(block_size=128))), BlockMapping(block_shape=(Blocked(block_size=128), Blocked(block_size=128)))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; c:Ref<default>{f32[128,128]} d:Ref<default>{f32[128,128]}. let
          e:f32[128,128] <- c[...]
          f:f32[128,128] = max e 0.0:f32[]
          g:f32[128,128], d[...] <- d[...], f
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[4096,4096]),)
    ] a
  in (b,) }