keras-team/keras · error · ValueError

`diagonal` requires input tensor with static rank.

Error message

`diagonal` requires input tensor with static rank.

What it means

Raised by keras.ops.numpy.diagonal in the OpenVINO backend when the input's PartialShape has unknown (dynamic) rank. Building the diagonal index ranges requires knowing the rank at graph-construction time, so dynamic-rank tensors are rejected.

Solutions

  1. Supply a tensor whose rank is statically known (e.g. materialize dynamic shapes or rebuild the graph with static shape info)
  2. Reshape the input to a concrete rank before calling diagonal
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/openvino/numpy.py:1673 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/4458a9ff6e533ca3. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/openvino/numpy.py:1673

        cols = ov_opset.add(rng, k_const).output(0)
    else:
        neg_k_const = ov_opset.constant(-k_val, dtype=Type.i32).output(0)
        rows = ov_opset.add(rng, neg_k_const).output(0)
        cols = rng

    rows_expanded = ov_opset.reshape(rows, [-1, 1], False).output(0)
    cols_expanded = ov_opset.reshape(cols, [-1, 1], False).output(0)
    indices = ov_opset.concat([rows_expanded, cols_expanded], 1).output(0)

    result = ov_opset.scatter_nd_update(zeros_mat, indices, v_flat).output(0)
    return OpenVINOKerasTensor(result)


def diagonal(x, offset=0, axis1=0, axis2=1):
    x = get_ov_output(x)
    rank = x.get_partial_shape().rank.get_length()
    if rank is None:
        raise ValueError("`diagonal` requires input tensor with static rank.")
    if rank < 2:
        raise ValueError(
            f"diagonal requires input tensor with rank >= 2.Given rank: {rank}"
        )
    axis1 = canonicalize_axis(axis1, rank)
    axis2 = canonicalize_axis(axis2, rank)
    if axis1 == axis2:
        raise ValueError("`axis1` and `axis2` cannot be the same.")

    perm_order = [axis1, axis2] + [
        i for i in range(rank) if i != axis1 and i != axis2
    ]
    perm_const = ov_opset.constant(perm_order, dtype=Type.i32).output(0)
    x_transposed = ov_opset.transpose(x, perm_const)

    # Build the (row, col) index pairs in the graph so the diagonalized axes
    # may be dynamic.
    zero = ov_opset.constant(0, Type.i32).output(0)

View on GitHub (pinned to 7a34a03db6)