Skip to content

[Bug] [Relax][ONNX] Gather import fails when indices come from Shape #20176

Description

@Anemone220

Expected behavior

The Relax ONNX frontend should import a valid ONNX Gather whose indices are produced by Shape.

Actual behavior

The script constructs a valid ONNX model. onnx.checker.check_model and ONNX Runtime session construction succeed. from_onnx fails while converting Gather:

Error converting operator Gather, with inputs: [data, R.shape([2, 3])]
Traceback (most recent call last):
  ...
  File ".../python/tvm/relax/frontend/onnx/onnx_frontend.py", line 1353, in _impl_v13
    indices_dtype = indices.ty.dtype.dtype
AttributeError: 'ShapeType' object has no attribute 'dtype'

Environment

  • OS: macOS 15.6 (Darwin 24.6.0, arm64)
  • Python: 3.12.2
  • TVM: c7b458e (tag v0.26.0; package reports 0.26.dev0)
  • ONNX: 1.17.0
  • ONNX Runtime: 1.21.1
  • Frontend: tvm.relax.frontend.onnx.from_onnx

Steps to reproduce

The following self-contained script constructs the model:

#!/usr/bin/env python3
"""Reproduce the Relax ONNX ShapeType/Gather import failure."""

import onnx
import onnxruntime as ort
from onnx import TensorProto, helper
from tvm.relax.frontend.onnx import from_onnx


def main() -> None:
    data = helper.make_tensor_value_info("data", TensorProto.FLOAT, [4])
    shape_source = helper.make_tensor_value_info(
        "shape_source", TensorProto.FLOAT, [2, 3]
    )
    out = helper.make_tensor_value_info("out", TensorProto.FLOAT, [2])
    graph = helper.make_graph(
        [
            helper.make_node("Shape", ["shape_source"], ["indices"]),
            helper.make_node("Gather", ["data", "indices"], ["out"], axis=0),
        ],
        "shape_as_gather_indices",
        [data, shape_source],
        [out],
    )
    model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 18)])

    onnx.checker.check_model(model)
    ort.InferenceSession(model.SerializeToString(), providers=["CPUExecutionProvider"])
    from_onnx(model, opset=18, keep_params_in_input=True)


if __name__ == "__main__":
    main()

Analysis

Shape is represented as ShapeType, but the Gather importer reads indices.ty.dtype.dtype, assuming TensorType. The failure occurs during ONNX import.

Triage

  • needs-triage

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address ittype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions