Kernel: linear_bias_relu (Level 2, category: matmul_activation)
Input shapes: [[1024, 1024], [1024, 2048], [2048]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0.000107)
Pallas runtime: 1272.4464 ms; JAX XLA compiled runtime: 0.1869 ms; speedup vs compiled baseline: 0.0001

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

Original Pallas kernel (TPU-oriented): """Level 2: Fused Linear + Bias + ReLU via Pallas.

Provenance: keras-team FusedDense pattern, standard MLP first layer
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _linear_bias_relu_kernel(x_ref, w_ref, b_ref, o_ref):
    z = x_ref[...] @ w_ref[...] + b_ref[...]
    o_ref[...] = jnp.maximum(z, 0)


def pallas_linear_bias_relu(x: jax.Array, w: jax.Array, b: jax.Array) -> jax.Array:
    m, k = x.shape
    _, n = w.shape
    bm = min(512, m)

    return pl.pallas_call(
        _linear_bias_relu_kernel,
        out_shape=jax.ShapeDtypeStruct((m, n), x.dtype),
        grid=(m // bm,),
        in_specs=[
            pl.BlockSpec((bm, k), lambda i: (i, 0)),
            pl.BlockSpec((k, n), lambda i: (0, 0)),
            pl.BlockSpec((n,), lambda i: (0,)),
        ],
        out_specs=pl.BlockSpec((bm, n), lambda i: (i, 0)),
    )(x, w, b)


pallas_kernel = pallas_linear_bias_relu
task_name = "linear_bias_relu"
input_shapes = [(1024, 1024), (1024, 2048), (2048,)]
category = "matmul_activation"
level = 2

Fixed Pallas kernel (GPU-compatible): """Level 2: Fused Linear + Bias + ReLU via Pallas.

Provenance: keras-team FusedDense pattern, standard MLP first layer
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _linear_bias_relu_kernel(x_ref, w_ref, b_ref, o_ref):
    z = x_ref[...] @ w_ref[...] + b_ref[...]
    o_ref[...] = jnp.maximum(z, 0)


def pallas_linear_bias_relu(x: jax.Array, w: jax.Array, b: jax.Array) -> jax.Array:
    m, k = x.shape
    _, n = w.shape
    bm = min(32, m)
    bn = min(32, n)

    return pl.pallas_call(
        _linear_bias_relu_kernel,
        out_shape=jax.ShapeDtypeStruct((m, n), x.dtype),
        grid=(m // bm, n // bn),
        in_specs=[
            pl.BlockSpec((bm, k), lambda i, j: (i, 0)),
            pl.BlockSpec((k, bn), lambda i, j: (0, j)),
            pl.BlockSpec((bn,), lambda i, j: (j,)),
        ],
        out_specs=pl.BlockSpec((bm, bn), lambda i, j: (i, j)),
    )(x, w, b)


pallas_kernel = pallas_linear_bias_relu
task_name = "linear_bias_relu"
input_shapes = [(1024, 1024), (1024, 2048), (2048,)]
category = "matmul_activation"
level = 2

Diff of GPU fix: --- a/linear_bias_relu.py
+++ b/linear_bias_relu.py
@@ -21,18 +21,19 @@
 def pallas_linear_bias_relu(x: jax.Array, w: jax.Array, b: jax.Array) -> jax.Array:
     m, k = x.shape
     _, n = w.shape
-    bm = min(512, m)
+    bm = min(32, m)
+    bn = min(32, n)
 
     return pl.pallas_call(
         _linear_bias_relu_kernel,
         out_shape=jax.ShapeDtypeStruct((m, n), x.dtype),
-        grid=(m // bm,),
+        grid=(m // bm, n // bn),
         in_specs=[
-            pl.BlockSpec((bm, k), lambda i: (i, 0)),
-            pl.BlockSpec((k, n), lambda i: (0, 0)),
-            pl.BlockSpec((n,), lambda i: (0,)),
+            pl.BlockSpec((bm, k), lambda i, j: (i, 0)),
+            pl.BlockSpec((k, bn), lambda i, j: (0, j)),
+            pl.BlockSpec((bn,), lambda i, j: (j,)),
         ],
-        out_specs=pl.BlockSpec((bm, n), lambda i: (i, 0)),
+        out_specs=pl.BlockSpec((bm, bn), lambda i, j: (i, j)),
     )(x, w, b)
 
 

Jaxpr IR: { lambda ; a:f32[1024,1024] b:f32[1024,2048] c:f32[2048]. let
    d:f32[1024,2048] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(32, 64), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=32), Blocked(block_size=1024))), BlockMapping(block_shape=(Blocked(block_size=1024), Blocked(block_size=32))), BlockMapping(block_shape=(Blocked(block_size=32),)), BlockMapping(block_shape=(Blocked(block_size=32), Blocked(block_size=32)))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; e:Ref<default>{f32[32,1024]} f:Ref<default>{f32[1024,32]}
          g:Ref<default>{f32[32]} h:Ref<default>{f32[32,32]}. let
          i:f32[32,1024] <- e[...]
          j:f32[1024,32] <- f[...]
          k:f32[32,32] = dot_general[
            dimension_numbers=(([1], [0]), ([], []))
            preferred_element_type=float32
          ] i j
          l:f32[32] <- g[...]
          m:f32[1,32] = broadcast_in_dim[broadcast_dimensions=(1,)] l
          n:f32[32,32] = add k m
          o:f32[32,32] = max n 0.0:f32[]
          p:f32[32,32], h[...] <- h[...], o
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[1024,2048]),)
    ] a b c
  in (d,) }