Kernel: softmax (Level 1, category: softmax)
Input shapes: [[2048, 2048]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0)
Pallas runtime: 390.0773 ms; JAX XLA compiled runtime: 0.1184 ms; speedup vs compiled baseline: 0.0003

JAX reference implementation:
@jax.jit
def jax_softmax(x: jax.Array) -> jax.Array:
    return jax.nn.softmax(x, axis=-1)

Original Pallas kernel (TPU-oriented): """Level 1: Row-wise softmax via Pallas.

Demonstrates: reductions within a block, numerical stability (max subtraction),
multi-pass pattern (max -> subtract -> exp -> sum -> divide).
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _softmax_kernel(x_ref, o_ref):
    x = x_ref[...]
    row_max = jnp.max(x, axis=-1, keepdims=True)
    x_safe = x - row_max
    exp_x = jnp.exp(x_safe)
    sum_exp = jnp.sum(exp_x, axis=-1, keepdims=True)
    o_ref[...] = exp_x / sum_exp


def pallas_softmax(x: jax.Array) -> jax.Array:
    n_rows = x.shape[0]
    block_rows = min(128, n_rows)
    n_cols = x.shape[1]
    grid_size = n_rows // block_rows

    return pl.pallas_call(
        _softmax_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=(grid_size,),
        in_specs=[pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0))],
        out_specs=pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
    )(x)


pallas_kernel = pallas_softmax
task_name = "softmax"
input_shapes = [(2048, 2048)]
category = "softmax"
level = 1

Fixed Pallas kernel (GPU-compatible): """Level 1: Row-wise softmax via Pallas.

Demonstrates: reductions within a block, numerical stability (max subtraction),
multi-pass pattern (max -> subtract -> exp -> sum -> divide).
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _softmax_kernel(x_ref, o_ref):
    x = x_ref[...]
    row_max = jnp.max(x, axis=-1, keepdims=True)
    x_safe = x - row_max
    exp_x = jnp.exp(x_safe)
    sum_exp = jnp.sum(exp_x, axis=-1, keepdims=True)
    o_ref[...] = exp_x / sum_exp


def pallas_softmax(x: jax.Array) -> jax.Array:
    n_rows = x.shape[0]
    block_rows = min(128, n_rows)
    n_cols = x.shape[1]
    while block_rows * n_cols > 16384 and block_rows > 1:
        block_rows //= 2
    grid_size = n_rows // block_rows

    return pl.pallas_call(
        _softmax_kernel,
        out_shape=jax.ShapeDtypeStruct(x.shape, x.dtype),
        grid=(grid_size,),
        in_specs=[pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0))],
        out_specs=pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
    )(x)


pallas_kernel = pallas_softmax
task_name = "softmax"
input_shapes = [(2048, 2048)]
category = "softmax"
level = 1

Diff of GPU fix: --- a/softmax.py
+++ b/softmax.py
@@ -27,6 +27,8 @@
     n_rows = x.shape[0]
     block_rows = min(128, n_rows)
     n_cols = x.shape[1]
+    while block_rows * n_cols > 16384 and block_rows > 1:
+        block_rows //= 2
     grid_size = n_rows // block_rows
 
     return pl.pallas_call(

Jaxpr IR: { lambda ; a:f32[2048,2048]. let
    b:f32[2048,2048] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(256,), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=8), Blocked(block_size=2048))), BlockMapping(block_shape=(Blocked(block_size=8), Blocked(block_size=2048)))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; c:Ref<default>{f32[8,2048]} d:Ref<default>{f32[8,2048]}. let
          e:f32[8,2048] <- c[...]
          f:f32[8] = reduce_max[axes=(1,)] e
          g:f32[8,1] = broadcast_in_dim[broadcast_dimensions=(0,)] f
          h:f32[8,2048] = sub e g
          i:f32[8,2048] = exp h
          j:f32[8] = reduce_sum[axes=(1,) out_sharding=None] i
          k:f32[8,1] = broadcast_in_dim[broadcast_dimensions=(0,)] j
          l:f32[8,2048] = div i k
          m:f32[8,2048], d[...] <- d[...], l
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[2048,2048]),)
    ] a
  in (b,) }