Skip to content

[Shape Inference] Robustness of ConstantOfShape operator #6135

@shaoyuyoung

Description

@shaoyuyoung

Bug Report

Description

The following model is a ConstantOfShape op with a constant input [-10].
Of course, ONNX does not allow ConstantOfShape input to be negative (https://onnx.ai/onnx/operators/onnx__ConstantOfShape.html#inputs).
However, in this case, this model can pass the check, it outputs an invalid dim: -10.
I think it should output at least be unknown :(

Script

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


opset_imports = [helper.make_opsetid("", 20)]
graph = helper.make_graph(
    nodes=[
        helper.make_node(
            "ConstantOfShape",
            inputs=['std.constant'],
            outputs=['output'],
            name='invalid_node'
        )
    ],
    name='ConstantOfShapeGraph',
    inputs=[],
    outputs=[helper.make_tensor_value_info('output', TensorProto.FLOAT, [None])],
    initializer=[numpy_helper.from_array(np.array([-10], dtype=np.int64), name='std.constant')]
)

model = helper.make_model(graph, opset_imports=opset_imports, producer_name='pytorch', ir_version=9)
onnx.checker.check_model(model, full_check=True)
model_shapes = onnx.shape_inference.infer_shapes(model)
onnx.checker.check_model(model_shapes, full_check=True)
print(model_shapes.graph.output)
onnx.save(model_shapes,"model_shapes.onnx")

output

[name: "output"
type {
  tensor_type {
    elem_type: 1
    shape {
      dim {
        dim_value: -10
      }
    }
  }
}
]

Acatually, in some cases, this output will cause all shape inferences of downstream operators to fail

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions