microsoft / onnxscript

ONNX Script enables developers to naturally author ONNX functions and models using a subset of Python.
https://onnxscript.ai/
MIT License
280 stars 53 forks source link

Keyword arg not respected and was treated as positional arg #1566

Open vincentme opened 5 months ago

vincentme commented 5 months ago

behavior

I specified scales = [2.0, 2.0] in op.Resize. Evaluation works in onnxscript itself, but after export to modelproto and run by onnx ReferenceEvaluator or onnxruntime, error occurs and seems that the parameter [2.0, 2.0] was set to roi, scale_factors and output_size are both None.

code to reproduce

from onnxscript import FLOAT, script, opset20 as op
import numpy as np

@script()
def test(img_in: FLOAT["row", "col"]) -> FLOAT["row", "col"]:
    img_out = op.Resize(img_in, scales = [2.0, 2.0], mode = 'linear')
    # img_out = op.Resize(img_in, sizes = [512, 512], mode = 'linear')
    return img_out

img_in = np.random.randn(128, 256).astype(np.float32)
img_out = test(img_in)
model = test.to_model_proto()

from onnx.reference import ReferenceEvaluator
sess = ReferenceEvaluator(model)
feeds = {'img_in': img_in,}
outpus = sess.run(None, feeds)

from onnxruntime import InferenceSession
sess = InferenceSession(model.SerializeToString())
feeds = {'img_in': img_in,}
outpus = sess.run(None, feeds)

error message

raise ValueError("output_size is None and scale_factors is None.")

ValueError: output_size is None and scale_factors is None.

图片

justinchuby commented 5 months ago
<
   ir_version: 9,
   opset_import: ["" : 20]
>
test (float[row,col] img_in) => (float[row,col] img_out) {
   const = Constant <value: tensor = float[2] const {2,2}> ()
   img_out = Resize <mode: string = "linear"> (img_in, const)
}
justinchuby commented 5 months ago

You observation is correct and could be a bug. Please use None for now for any optional inputs. E.g.

img_out = op.Resize(img_in, None, scales = [2.0, 2.0], mode = 'linear')