jax-ml/jax · error · ValueError
x.shape={x.shape} != mask.shape={mask.shape}
Error message
x.shape={x.shape} != mask.shape={mask.shape} What it means
scan_count requires mask.shape == x.shape exactly; broadcasting is not applied.
Source
Thrown at jax/_src/pallas/mosaic/sc_primitives.py:531
The barrier must be used with
:class:`jax.experimental.pallas.tpu_sc.VectorSubcoreMesh`.
"""
barrier_p.bind()
scan_count_p = jax_core.Primitive("scan_count")
scan_count_p.multiple_results = True
@scan_count_p.def_abstract_eval
def _scan_count_abstract_eval(x, mask):
if x.dtype not in (jnp.uint32, jnp.int32, jnp.float32):
raise NotImplementedError(
f"x.dtype={x.dtype} must be uint32, int32 or float32")
if not jnp.issubdtype(mask.dtype, jnp.bool):
raise TypeError(f"mask.dtype={mask.dtype} is not a boolean dtype")
if x.shape != mask.shape:
raise ValueError(f"x.shape={x.shape} != mask.shape={mask.shape}")
return jax_core.ShapedArray(x.shape, jnp.int32), mask
@sc_lowering.register_lowering_rule(scan_count_p)
def _scan_count_lowering_rule(ctx: sc_lowering.LoweringRuleContext, x, mask):
del ctx # Unused.
# Reverse, because the MLIR op returns the mask first.
return tpu.scan_count(mask, x)[::-1]
def scan_count(
x: jax.Array, mask: jax.Array | None = None
) -> tuple[jax.Array, jax.Array]:
"""Computes the running duplicate occurrence count of the array.
Args:
x: An array of integers or floats.
mask: An optional array of booleans, which specifies which elements ``x``View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Broadcast the mask to x.shape explicitly: jnp.broadcast_to(mask, x.shape)
- Recompute the mask at matching shape inside the kernel
Example fix
// before scan_count(x, mask) # mask (N,), x (N,M) // after scan_count(x, jnp.broadcast_to(mask, x.shape))
Defensive patterns
Strategy: validation
Validate before calling
if mask.shape != x.shape:
mask = jnp.broadcast_to(mask, x.shape) Type guard
def shapes_match(x, mask) -> bool:
return tuple(x.shape) == tuple(mask.shape) Prevention
- Broadcast masks explicitly; never rely on implicit rules
- Derive masks from the tensor they mask
- Test with non-trivial shapes
When it happens
Trigger: Passing mask of shape (N,) with x of shape (N, M), or a scalar mask with any x.
Common situations: Assuming mask broadcasts over the vector dimension; reusing a batch-level mask for token-level data.
Related errors
- {x.shape=} does not match expected shape {expected_shape}
- x.dtype={x.dtype} must be uint32, int32 or float32
- mask.dtype={mask.dtype} is not a boolean dtype
- Block shape for {origin} (= {block_shape}) must have the sam
- Stacking only supported when the block size along the stack
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/5359c6a77f1086aa.
Report an issue: GitHub.