keras-team/keras · error · ValueError
Shapes are incompatible for associative_scan interleaving…
Error message
Shapes are incompatible for associative_scan interleaving. a.shape[{axis}]={a.shape[axis]}, b.shape[{axis}]={b.shape[axis]} What it means
Internal shape check inside the NumPy `associative_scan`'s `_interleave` helper. While combining partial scan results, it interleaves two tensors a and b along the scan axis; for odd lengths the two operands may differ by exactly one element along that axis, otherwise the interleave is geometrically impossible. If a.shape[axis] and b.shape[axis] differ by more than one (or disagree when they must match), this error reports both sizes and the axis.
Solutions
- Ensure scan inputs have lengths that are compatible with the halving scheme (differences of at most one element per level) — typically satisfied by any valid associative_scan input, so treat this as a sign of malformed custom elems
- Check that the combine function `f` returns tensors with the same shapes as its inputs, preserving the scan-axis length
- Report the issue upstream if it occurs with well-formed inputs, as it indicates an internal inconsistency in the divide-and-conquer path
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/numpy/ops/core.py:240 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4078d94c82a73312.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/numpy/ops/core.py:240
c = f(a, b)
c_flat = tree.flatten(c)
return c_flat
num_elems = int(elems_flat[0].shape[axis])
if not all(int(elem.shape[axis]) == num_elems for elem in elems_flat[1:]):
raise ValueError(
"Array inputs to associative_scan must have the same "
"first dimension. (saw: {})".format(
[elem.shape for elem in elems_flat]
)
)
def _interleave(a, b, axis):
"""Given two Tensors of static shape, interleave them along axis."""
if not (
a.shape[axis] == b.shape[axis] or a.shape[axis] == b.shape[axis] + 1
):
raise ValueError(
"Shapes are incompatible for associative_scan interleaving. "
f"a.shape[{axis}]={a.shape[axis]}, "
f"b.shape[{axis}]={b.shape[axis]}"
)
# we want to get a: [a1, a2], b: [b1, b2]
# to a: [a1, 0, a2, 0], b: [0, b1, 0, b2]
a_shape = list(a.shape)
a_shape[axis] = a.shape[axis] * 2 - 1
b_shape = list(b.shape)
b_shape[axis] = b.shape[axis] * 2 - 1
a_dil = np.zeros(a_shape)
np.copyto(slice_along_axis(a_dil, 0, None, 2, axis), a)
b_dil = np.zeros(b_shape)
np.copyto(slice_along_axis(b_dil, 0, None, 2, axis), b)
View on GitHub (pinned to 7a34a03db6)