Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shape Inference] Robustness of ConstantOfShape operator #6135

Open
shaoyuyoung opened this issue May 10, 2024 · 2 comments
Open

[Shape Inference] Robustness of ConstantOfShape operator #6135

shaoyuyoung opened this issue May 10, 2024 · 2 comments
Labels
bug contributions welcome shape inference Issues related to shape inference

Comments

@shaoyuyoung
Copy link

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

@justinchuby justinchuby added shape inference Issues related to shape inference contributions welcome labels May 10, 2024
@justinchuby
Copy link
Contributor

Thank you! We are happy to take a pull request.

@shaoyuyoung
Copy link
Author

cc @jcwchen .

Chun-Wei, can you take a look at this?

It's supposed to be something you're good at

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug contributions welcome shape inference Issues related to shape inference
Projects
None yet
Development

No branches or pull requests

2 participants