Kernel: mse_loss (Level 1, category: loss)
Input shapes: [[2048, 1024], [2048, 1024]]
Target hardware: NVIDIA A100 80GB PCIe
Status: pass (correct: yes, max diff vs baseline: 0)
Pallas runtime: 157.6705 ms; JAX XLA compiled runtime: 0.1155 ms; speedup vs compiled baseline: 0.0007

JAX reference implementation:
@jax.jit
def jax_mse_loss(pred: jax.Array, target: jax.Array) -> jax.Array:
    diff = pred - target
    return jnp.mean(diff * diff, axis=-1)

Original Pallas kernel (TPU-oriented): """Level 1: Mean squared error loss via Pallas.

Provenance: standard regression loss, (pred - target)^2 reduced per row
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _mse_kernel(pred_ref, target_ref, o_ref):
    diff = pred_ref[...] - target_ref[...]
    o_ref[...] = jnp.mean(diff * diff, axis=-1)


def pallas_mse_loss(pred: jax.Array, target: jax.Array) -> jax.Array:
    n_rows = pred.shape[0]
    n_cols = pred.shape[1]
    block_rows = min(256, n_rows)
    grid_size = n_rows // block_rows

    return pl.pallas_call(
        _mse_kernel,
        out_shape=jax.ShapeDtypeStruct((n_rows,), pred.dtype),
        grid=(grid_size,),
        in_specs=[
            pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
            pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
        ],
        out_specs=pl.BlockSpec((block_rows,), lambda i: (i,)),
    )(pred, target)


pallas_kernel = pallas_mse_loss
task_name = "mse_loss"
input_shapes = [(2048, 1024), (2048, 1024)]
category = "loss"
level = 1

Fixed Pallas kernel (GPU-compatible): """Level 1: Mean squared error loss via Pallas.

Provenance: standard regression loss, (pred - target)^2 reduced per row
"""


from pallasbench.provenance import describe_task as _describe_task

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

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


def _mse_kernel(pred_ref, target_ref, o_ref):
    diff = pred_ref[...] - target_ref[...]
    o_ref[...] = jnp.mean(diff * diff, axis=-1)


def pallas_mse_loss(pred: jax.Array, target: jax.Array) -> jax.Array:
    n_rows = pred.shape[0]
    n_cols = pred.shape[1]
    block_rows = min(256, n_rows)
    while block_rows * n_cols > 16384 and block_rows > 1:
        block_rows //= 2
    grid_size = n_rows // block_rows

    return pl.pallas_call(
        _mse_kernel,
        out_shape=jax.ShapeDtypeStruct((n_rows,), pred.dtype),
        grid=(grid_size,),
        in_specs=[
            pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
            pl.BlockSpec((block_rows, n_cols), lambda i: (i, 0)),
        ],
        out_specs=pl.BlockSpec((block_rows,), lambda i: (i,)),
    )(pred, target)


pallas_kernel = pallas_mse_loss
task_name = "mse_loss"
input_shapes = [(2048, 1024), (2048, 1024)]
category = "loss"
level = 1

Diff of GPU fix: --- a/mse_loss.py
+++ b/mse_loss.py
@@ -22,6 +22,8 @@
     n_rows = pred.shape[0]
     n_cols = pred.shape[1]
     block_rows = min(256, n_rows)
+    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,1024] b:f32[2048,1024]. let
    c:f32[2048] = pallas_call[
      compiler_params=None
      cost_estimate=None
      debug=False
      grid_mapping=GridMapping(grid=(128,), block_mappings=(BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=1024))), BlockMapping(block_shape=(Blocked(block_size=16), Blocked(block_size=1024))), BlockMapping(block_shape=(Blocked(block_size=16),))))
      input_output_aliases=()
      interpret=False
      jaxpr={ lambda ; d:Ref<default>{f32[16,1024]} e:Ref<default>{f32[16,1024]}
          f:Ref<default>{f32[16]}. let
          g:f32[16,1024] <- d[...]
          h:f32[16,1024] <- e[...]
          i:f32[16,1024] = sub g h
          j:f32[16,1024] = mul i i
          k:f32[16] = reduce_sum[axes=(1,) out_sharding=None] j
          l:f32[16] = div k 1024.0:f32[]
          m:f32[16], f[...] <- f[...], l
        in () }
      mesh=None
      metadata=None
      name=None
      out_avals=(ShapedArray(float32[2048]),)
    ] a b
  in (c,) }