Kernel: geglu (Level 2, category: mlp_fusion)
Input shapes: [[512, 1024], [1024, 2048], [1024, 2048]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0.000122)
Pallas runtime: 2411.3861 ms; JAX XLA compiled runtime: 0.2083 ms; speedup vs compiled baseline: 0.0001

JAX reference implementation:
@jax.jit
def jax_geglu(x: jax.Array, w_gate: jax.Array, w_up: jax.Array) -> jax.Array:
    gate = jax.nn.gelu(x @ w_gate)
    up = x @ w_up
    return gate * up

Original Pallas kernel (TPU-oriented): """Level 2: Fused GeGLU activation via Pallas.

Provenance: variant of gated linear unit, used in PaLM/Gemma MLPs
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _geglu_kernel(x_ref, w_gate_ref, w_up_ref, o_ref):
    x = x_ref[...]
    gate = x @ w_gate_ref[...]
    gate = gate * 0.5 * (1.0 + jnp.tanh(
        jnp.sqrt(2.0 / jnp.pi) * (gate + 0.044715 * gate ** 3)
    ))
    up = x @ w_up_ref[...]
    o_ref[...] = gate * up


def pallas_geglu(x: jax.Array, w_gate: jax.Array, w_up: jax.Array) -> jax.Array:
    m, k = x.shape
    _, n = w_gate.shape
    bm = min(256, m)

    return pl.pallas_call(
        _geglu_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((k, n), lambda i: (0, 0)),
        ],
        out_specs=pl.BlockSpec((bm, n), lambda i: (i, 0)),
    )(x, w_gate, w_up)


pallas_kernel = pallas_geglu
task_name = "geglu"
input_shapes = [(512, 1024), (1024, 2048), (1024, 2048)]
category = "mlp_fusion"
level = 2

Fixed Pallas kernel (GPU-compatible): """Level 2: Fused GeGLU activation via Pallas.

Provenance: variant of gated linear unit, used in PaLM/Gemma MLPs
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _geglu_kernel(x_ref, w_gate_ref, w_up_ref, o_ref):
    x = x_ref[...]
    gate = x @ w_gate_ref[...]
    gate = gate * 0.5 * (1.0 + jnp.tanh(
        jnp.sqrt(2.0 / jnp.pi) * (gate + 0.044715 * gate ** 3)
    ))
    up = x @ w_up_ref[...]
    o_ref[...] = gate * up


def pallas_geglu(x: jax.Array, w_gate: jax.Array, w_up: jax.Array) -> jax.Array:
    m, k = x.shape
    _, n = w_gate.shape
    bm = min(16, m)
    bn = min(16, n)

    return pl.pallas_call(
        _geglu_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((k, bn), lambda i, j: (0, j)),
        ],
        out_specs=pl.BlockSpec((bm, bn), lambda i, j: (i, j)),
    )(x, w_gate, w_up)


pallas_kernel = pallas_geglu
task_name = "geglu"
input_shapes = [(512, 1024), (1024, 2048), (1024, 2048)]
category = "mlp_fusion"
level = 2

Diff of GPU fix: --- a/geglu.py
+++ b/geglu.py
@@ -26,18 +26,19 @@
 def pallas_geglu(x: jax.Array, w_gate: jax.Array, w_up: jax.Array) -> jax.Array:
     m, k = x.shape
     _, n = w_gate.shape
-    bm = min(256, m)
+    bm = min(16, m)
+    bn = min(16, n)
 
     return pl.pallas_call(
         _geglu_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((k, n), lambda i: (0, 0)),
+            pl.BlockSpec((bm, k), lambda i, j: (i, 0)),
+            pl.BlockSpec((k, bn), lambda i, j: (0, j)),
+            pl.BlockSpec((k, bn), lambda i, j: (0, j)),
         ],
-        out_specs=pl.BlockSpec((bm, n), lambda i: (i, 0)),
+        out_specs=pl.BlockSpec((bm, bn), lambda i, j: (i, j)),
     )(x, w_gate, w_up)
 
 

Jaxpr IR: { lambda ; a:f32[512,1024] b:f32[1024,2048] c:f32[1024,2048]. let
    d:f32[512,2048] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(32, 128), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=1024))), BlockMapping(block_shape=(Blocked(block_size=1024), Blocked(block_size=16))), BlockMapping(block_shape=(Blocked(block_size=1024), Blocked(block_size=16))), BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=16)))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; e:Ref<default>{f32[16,1024]} f:Ref<default>{f32[1024,16]}
          g:Ref<default>{f32[1024,16]} h:Ref<default>{f32[16,16]}. let
          i:f32[16,1024] <- e[...]
          j:f32[1024,16] <- f[...]
          k:f32[16,16] = dot_general[
            dimension_numbers=(([1], [0]), ([], []))
            preferred_element_type=float32
          ] i j
          l:f32[16,16] = mul k 0.5:f32[]
          m:f32[] = sqrt 0.6366197723675814:f32[]
          n:f32[16,16] = integer_pow[y=3] k
          o:f32[16,16] = mul 0.044714998453855515:f32[] n
          p:f32[16,16] = add k o
          q:f32[] = convert_element_type[new_dtype=float32 weak_type=False] m
          r:f32[16,16] = mul q p
          s:f32[16,16] = tanh r
          t:f32[16,16] = add 1.0:f32[] s
          u:f32[16,16] = mul l t
          v:f32[1024,16] <- g[...]
          w:f32[16,16] = dot_general[
            dimension_numbers=(([1], [0]), ([], []))
            preferred_element_type=float32
          ] i v
          x:f32[16,16] = mul u w
          y:f32[16,16], h[...] <- h[...], x
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[512,2048]),)
    ] a b c
  in (d,) }