Skip to content

Commit 221f693

Browse files
committed
Add test for CoreML reshape with dynamic input shapes
Add a test model (Reshape with dynamic M dimension, shape [-1, 2048]) and a corresponding test case in dynamic_input_test.cc to verify the CoreML EP handles reshape operations with dynamic input shapes.
1 parent 4409789 commit 221f693

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

onnxruntime/test/providers/coreml/dynamic_input_test.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,41 @@ TEST(CoreMLExecutionProviderDynamicInputShapeTest, MobileNetExcerpt) {
8484
}
8585
}
8686

87+
TEST(CoreMLExecutionProviderDynamicInputShapeTest, Reshape) {
88+
constexpr auto model_path = ORT_TSTR("testdata/reshape_with_dynamic_input_shape.onnx");
89+
90+
auto test = [&](const size_t M) {
91+
SCOPED_TRACE(MakeString("M=", M));
92+
std::unordered_map<std::string, std::string> options;
93+
auto coreml_ep = CoreMLProviderFactoryCreator::Create(options)->CreateProvider();
94+
95+
const auto ep_verification_params = EPVerificationParams{
96+
ExpectedEPNodeAssignment::All,
97+
1e-4f,
98+
};
99+
100+
#if defined(__APPLE__)
101+
RandomValueGenerator gen{1234};
102+
// Input is [M, 512], reshape to [-1, 2048] so M must be a multiple of 4.
103+
const auto X_shape = std::vector<int64_t>{static_cast<int64_t>(M * 4), 512};
104+
const auto X_data = gen.Uniform<float>(X_shape, 0.0f, 1.0f);
105+
106+
OrtValue X = CreateInputOrtValueOnCPU<float>(X_shape, X_data);
107+
108+
RunAndVerifyOutputsWithEP(model_path, CurrentTestName(),
109+
std::move(coreml_ep),
110+
{{"X", X}},
111+
ep_verification_params);
112+
#else
113+
TestModelLoad(model_path, std::move(coreml_ep), ep_verification_params.ep_node_assignment);
114+
#endif
115+
};
116+
117+
for (size_t i = 1; i <= 3; ++i) {
118+
test(i);
119+
}
120+
}
121+
87122
TEST(CoreMLExecutionProviderDynamicInputShapeTest, EmptyInputFails) {
88123
constexpr auto model_path = ORT_TSTR("testdata/matmul_with_dynamic_input_shape.onnx");
89124

144 Bytes
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pathlib import Path
2+
3+
import onnx
4+
from onnx import TensorProto, helper
5+
6+
# This model contains a Reshape where:
7+
# - X has shape [M, 512] and `M` is a dynamic dimension.
8+
# - shape is a constant initializer with value [-1, 2048].
9+
# CoreML MIL supports -1 in the shape and can infer the dimension at runtime.
10+
11+
M = "M"
12+
K = 512
13+
N = 2048
14+
15+
graph = helper.make_graph(
16+
[ # nodes
17+
helper.make_node("Reshape", ["X", "shape"], ["Y"], "Reshape"),
18+
],
19+
"ReshapeWithDynamicInputShape", # name
20+
[ # inputs
21+
helper.make_tensor_value_info("X", TensorProto.FLOAT, [M, K]),
22+
],
23+
[ # outputs
24+
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [None, N]),
25+
],
26+
[ # initializers
27+
helper.make_tensor("shape", TensorProto.INT64, [2], [-1, N]),
28+
],
29+
)
30+
31+
opset_imports = [helper.make_operatorsetid("", 19)]
32+
model = helper.make_model(graph, opset_imports=opset_imports)
33+
onnx.save(model, str(Path(__file__).parent / "reshape_with_dynamic_input_shape.onnx"))

0 commit comments

Comments
 (0)