Skip to content

Reshape with allowzero=1 produces wrong shape for zero-size tensor #28449

@ALinrunrun

Description

@ALinrunrun

Describe the issue

ONNX Runtime CPUExecutionProvider produces a different shape from ONNX ReferenceEvaluator for a chained Reshape model involving a zero-size tensor and allowzero=1.

The model first reshapes an input of shape [0, 8, 2] with shape [4, 2, -1], then applies a second Reshape with shape [0, 0, 4] and allowzero=1.

With allowzero=1, the zero values in the target shape should be preserved as explicit zero dimensions. ONNX ReferenceEvaluator returns shape (0, 0, 4), but ONNX Runtime returns (0, 8, 4).

To reproduce

import numpy as np
import onnx.reference
import onnxruntime as ort
from onnx import TensorProto, helper

X = helper.make_tensor_value_info("X", TensorProto.FLOAT, [0, 8, 2])
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [None, None, None])

s1 = helper.make_tensor("s1", TensorProto.INT64, [3], [4, 2, -1])
s2 = helper.make_tensor("s2", TensorProto.INT64, [3], [0, 0, 4])

n1 = helper.make_node("Reshape", ["X", "s1"], ["mid"])
n2 = helper.make_node("Reshape", ["mid", "s2"], ["Y"], allowzero=1)

g = helper.make_graph([n1, n2], "g", [X], [Y], initializer=[s1, s2])
m = helper.make_model(g, opset_imports=[helper.make_opsetid("", 18)])
m.ir_version = 8

x = np.zeros((0, 8, 2), dtype=np.float32)

ref_shape = onnx.reference.ReferenceEvaluator(m).run(None, {"X": x})[0].shape
ort_shape = ort.InferenceSession(
m.SerializeToString(),
providers=["CPUExecutionProvider"],
).run(None, {"X": x})[0].shape

print("ref:", ref_shape)
print("ORT:", ort_shape)
print("PASS=", ref_shape == ort_shape)

Urgency

Expected output

ref: (0, 0, 4)
ORT: (0, 0, 4)
PASS=True

Actual output

ref: (0, 0, 4)
ORT: (0, 8, 4)
PASS=False

Platform

Linux

OS Version

Linux-6.17.0-20-generic-x86_64-with-glibc2.39

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.25.1

ONNX Runtime API

Python

Architecture

X86

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions