Kernel: clamp (Level 1, category: elementwise)
Input shapes: [[4096, 4096]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0)
Pallas runtime: 114.9357 ms; JAX XLA compiled runtime: 0.2023 ms; speedup vs compiled baseline: 0.0018

JAX reference implementation:
@jax.jit
def jax_clamp(x: jax.Array) -> jax.Array:
    return jnp.clip(x, -1.0, 1.0)

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

Provenance: jnp.clip, used in gradient clipping and activation clamping
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _clamp_kernel(x_ref, o_ref):
    o_ref[...] = jnp.clip(x_ref[...], -1.0, 1.0)


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

    return pl.pallas_call(
        _clamp_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_clamp
task_name = "clamp"
input_shapes = [(4096, 4096)]
category = "elementwise"
level = 1

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

Provenance: jnp.clip, used in gradient clipping and activation clamping
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _clamp_kernel(x_ref, o_ref):
    o_ref[...] = jnp.clip(x_ref[...], -1.0, 1.0)


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


pallas_kernel = pallas_clamp
task_name = "clamp"
input_shapes = [(4096, 4096)]
category = "elementwise"
level = 1

Diff of GPU fix: --- a/clamp.py
+++ b/clamp.py
@@ -18,16 +18,21 @@
 
 
 def pallas_clamp(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(
         _clamp_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] = jit[
            name=clip
            jaxpr={ lambda ; e:f32[128,128] g:f32[] h:f32[]. let
                i:f32[] = convert_element_type[
                  new_dtype=float32
                  weak_type=False
                ] g
                j:f32[128,128] = max i e
                k:f32[] = convert_element_type[
                  new_dtype=float32
                  weak_type=False
                ] h
                f:f32[128,128] = min k j
              in (f,) }
          ] e -1.0:f32[] 1.0:f32[]
          l:f32[128,128], d[...] <- d[...], f
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[4096,4096]),)
    ] a
  in (b,) }