BlockSpec index_map returns the wrong rank
index_map runs once per grid step and it has to hand back one coordinate for every axis of the block shape you declared. A (2, 128) block is two-dimensional, so index_map needs to return a two-element tuple, but this one returns just `(i,)`. The error spells out the mismatch directly: it wants 2 values and gets 1. Whenever you see that pattern, count the axes in your block_shape and check the length of the tuple your index_map returns against it, they need to match even when one axis never moves.
def broken_index_map_rank():
def kernel(x_ref, o_ref):
o_ref[...] = x_ref[...] * 2.0
def index_map(i):
return (i,) # bug: a (2, 128) block over a 2D array needs two entries
x = jnp.ones((8, 128), dtype=jnp.float32)
return pl.pallas_call(
kernel,
grid=(4,),
in_specs=[pl.BlockSpec((2, 128), index_map)],
out_specs=pl.BlockSpec((2, 128), index_map),
out_shape=jax.ShapeDtypeStruct((8, 128), jnp.float32),
interpret=True,
)(x)def fixed_index_map_rank():
def kernel(x_ref, o_ref):
o_ref[...] = x_ref[...] * 2.0
def index_map(i):
return (i, 0) # one entry per block dimension
x = jnp.ones((8, 128), dtype=jnp.float32)
return pl.pallas_call(
kernel,
grid=(4,),
in_specs=[pl.BlockSpec((2, 128), index_map)],
out_specs=pl.BlockSpec((2, 128), index_map),
out_shape=jax.ShapeDtypeStruct((8, 128), jnp.float32),
interpret=True,
)(x)