microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.89k stars 2.95k forks source link

Wrong output shape due to MergeShape failure #11870

Open ganler opened 2 years ago

ganler commented 2 years ago

Describe the bug MergeShape fails and ONNXRuntime returns results in a wrong shape with warnings below:

 [W:onnxruntime:, graph.cc:106 MergeShapeInfo] Error merging shape info for output. 'onnx::Add_15' source:{5,50,1} target:{1,5,50,1}. Falling back to lenient merge.
 [W:onnxruntime:, execution_frame.cc:806 VerifyOutputSizes] Expected shape from model of {1,5,50,1} does not match actual shape of {5,50,1} for output o0

Urgency none

System information

To Reproduce

The bug-triggering sub-graph is attcahed: model.zip

import onnx
import onnxruntime as ort
import numpy as np

onnx_model = onnx.load("model.onnx")
sess = ort.InferenceSession(onnx._serialize(onnx_model))
parallel_val = np.ones((1, 50, 1), dtype=np.float32)
res = sess.run(["o0"], {f'i{i}': parallel_val for i in range(4)})
assert res[0].shape == (1, 5, 50, 1)

Expected behavior This subgraph should return a shape of (1, 5, 50, 1) but actually (5, 50, 1).

Additional context This seems to be a optimization bug that if I disable all optimization the model can pass.

hariharans29 commented 2 years ago

Hmmm, you're right - just by hand computing the shapes of each operator, the output shape should have been [1, 5, 50, 1].

Need to check which graph optimization (or corresponding optimized kernel) causes this as this graph is a good example where many optimizations can kick in at the same time - ConstantFolding, MatMulTranspose, and MatMulAdd...

RCLaplace commented 3 weeks ago

I am learning onnx model, I also encountered this problem, can anyone teach me how to disable all optimizations