Skip to content

Clarify expected behavior for Gather with out-of-bounds indices #28311

@ALinrunrun

Description

@ALinrunrun

Describe the issue

ONNX Runtime raises INVALID_ARGUMENT for Gather when an index is out of bounds.

In this case, params has shape [5], so the valid index range is [-5, 4]. The indices tensor contains 5, and ORT reports:

idx=5 must be within the inclusive range [-5,4]

This appears consistent with the ONNX Gather spec, but other backends may handle the same case differently, such as returning a value instead of raising an error. This report is to confirm whether ORT's strict validation is the intended behavior and whether there is any recommended compatibility handling for models that rely on clamp or zero-fill behavior.

To reproduce

import numpy as np
import onnx
import onnxruntime as ort
from onnx import TensorProto, helper, numpy_helper

params = np.array([10., 20., 30., 40., 50.], dtype=np.float32)
indices = np.array([0, 1, 2, 3, 5, 4], dtype=np.int64)

X = helper.make_tensor_value_info("params", TensorProto.FLOAT, [5])
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [6])
I = numpy_helper.from_array(indices, "indices")

node = helper.make_node("Gather", ["params", "indices"], ["Y"], axis=0)
graph = helper.make_graph([node], "gather_oob", [X], [Y], initializer=[I])
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
model.ir_version = 8

sess = ort.InferenceSession(
    model.SerializeToString(),
    providers=["CPUExecutionProvider"],
)

print("params :", params.tolist())
print("indices:", indices.tolist())
print(sess.run(None, {"params": params})[0])

Urgency

Actual output

params : [10.0, 20.0, 30.0, 40.0, 50.0]
indices: [0, 1, 2, 3, 5, 4]

[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Non-zero status code returned while running Gather node.
Status Message: indices element out of data bounds, idx=5 must be within the inclusive range [-5,4]

Platform

Linux

OS Version

Linux-6.17.0-20-generic-x86_64-with-glibc2.39

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.25.1

ONNX Runtime API

Python

Architecture

X86

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions