keras-team/keras · error · ValueError

Repeated axis in `axis

Error message

Repeated axis in `axis`: {canonical_axis}

What it means

Second uniqueness check in the JAX sparse backend's axis helper, run after negative indices are resolved to non-negative ones (e.g. a 2D tensor with axis=(-1, 1): both resolve to 1). It catches duplicates invisible to the first check because they were written with different signs.

Solutions

  1. Remove the redundant axis — after resolving negatives, both entries point at the same dimension
  2. Rewrite negative indices as their positive equivalents to spot collisions
  3. Deduplicate the resolved axis list before calling the op
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/jax/sparse.py:53 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/b1cc634851f07033. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/jax/sparse.py:53

    result_dims = len(input_shape)
    if insert_dims:
        result_dims += len(axis)

    # Resolve negative values.
    canonical_axis = []
    for a in axis:
        if not -result_dims <= a < result_dims:
            raise ValueError(
                f"In `axis`, axis {a} is out of bounds for array "
                f"of dimension {result_dims}"
            )
        if a < 0:
            a = a + result_dims
        canonical_axis.append(a)

    # Check uniqueness again after resolving negative values.
    if len(set(canonical_axis)) != len(canonical_axis):
        raise ValueError(f"Repeated axis in `axis`: {canonical_axis}")
    canonical_axis = sorted(canonical_axis)

    # Compute output shape.
    output_shape = list(input_shape)
    for i in canonical_axis:
        if insert_dims:
            output_shape.insert(i, 1)
        else:
            output_shape[i] = 1
    broadcast_dims = [i for i in range(result_dims) if i not in canonical_axis]
    return canonical_axis, output_shape, broadcast_dims


def bcoo_add_indices(x1, x2, sum_duplicates):
    """Add the indices of `x2` to `x1` with zero values.

    Args:
        x1: `BCOO` tensor to add indices to.

View on GitHub (pinned to 7a34a03db6)