1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| from pathlib import Path
|
| import onnx
| from onnx import TensorProto, helper
|
| # This model contains a MatMul where:
| # - A has shape [M, K] and `M` is a dynamic dimension.
| # - B is an initializer with shape [K, N].
| # - This is important for the CoreML EP which only handles the case where B is an initializer.
|
| # M is dynamic
| M = "M"
| K = 2
| N = 4
|
| graph = helper.make_graph(
| [ # nodes
| helper.make_node("MatMul", ["A", "B"], ["Y"], "MatMul"),
| ],
| "MatMulWithDynamicInputShape", # name
| [ # inputs
| helper.make_tensor_value_info("A", TensorProto.FLOAT, [M, K]),
| ],
| [ # outputs
| helper.make_tensor_value_info("Y", TensorProto.FLOAT, [M, N]),
| ],
| [ # initializers
| helper.make_tensor("B", TensorProto.FLOAT, [K, N], [float(i) for i in range(K * N)]),
| ],
| )
|
| opset_imports = [helper.make_operatorsetid("", 19)]
| model = helper.make_model(graph, opset_imports=opset_imports)
| onnx.save(model, str(Path(__file__).parent / "matmul_with_dynamic_input_shape.onnx"))
|
|