Kernel: sigmoid (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: 283.8244 ms; JAX XLA compiled runtime: 0.2116 ms; speedup vs compiled baseline: 0.0007

JAX reference implementation:
@jax.jit
def jax_sigmoid(x: jax.Array) -> jax.Array:
    return jax.nn.sigmoid(x)

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

Provenance: jax.nn.sigmoid, used in loss functions and gating
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _sigmoid_kernel(x_ref, o_ref):
    x = x_ref[...]
    o_ref[...] = 1.0 / (1.0 + jnp.exp(-x))


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

    return pl.pallas_call(
        _sigmoid_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_sigmoid
task_name = "sigmoid"
input_shapes = [(4096, 4096)]
category = "activation"
level = 1

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

Provenance: jax.nn.sigmoid, used in loss functions and gating
"""

from pallasbench.provenance import describe_task as _describe_task

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

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


def _sigmoid_kernel(x_ref, o_ref):
    x = x_ref[...]
    o_ref[...] = 1.0 / (1.0 + jnp.exp(-x))


def pallas_sigmoid(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(
        _sigmoid_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=grid,
        in_specs=in_sp,
        out_specs=out_sp,
    )(x)


pallas_kernel = pallas_sigmoid
task_name = "sigmoid"
input_shapes = [(4096, 4096)]
category = "activation"
level = 1

Diff of GPU fix: --- a/sigmoid.py
+++ b/sigmoid.py
@@ -18,16 +18,21 @@
 
 
 def pallas_sigmoid(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(
         _sigmoid_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] = neg e
          g:f32[128,128] = exp f
          h:f32[128,128] = add 1.0:f32[] g
          i:f32[128,128] = div 1.0:f32[] h
          j:f32[128,128], d[...] <- d[...], i
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[4096,4096]),)
    ] a
  in (b,) }