Skip to content

Commit 25ebdee

Browse files
authored
Fix code to enable opset 18 and latest onnxruntime. (#2143)
1. Update Split attributes for opset 18. 2. Update all Reduce* ops for opset 18. 3. Update transpose_optimizer for opset 18. 4. Enable opset 18 in CI tests. --------- Signed-off-by: Jay Zhang <[email protected]>
1 parent de939f3 commit 25ebdee

17 files changed

+274
-89
lines changed

ci_build/azure_pipelines/templates/job_generator.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ parameters:
44
platforms: ['linux']
55
python_versions: ['3.7']
66
tf_versions: ['']
7-
onnx_versions: ['']
8-
onnx_opsets: ['17', '16', '15', '14', '13']
9-
onnx_backends: {onnxruntime: ['1.13.1']}
7+
onnx_versions: ['1.13.1']
8+
onnx_opsets: ['18', '17', '16', '15', '14', '13']
9+
onnx_backends: {onnxruntime: ['1.14.1']}
1010
job: {}
1111
run_setup: 'True'
1212
report_coverage: 'False'

ci_build/azure_pipelines/templates/setup.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ steps:
44
- bash: |
55
set -ex
66
pip install pytest pytest-cov pytest-runner coverage graphviz requests pyyaml pillow pandas parameterized sympy coloredlogs flatbuffers timeout-decorator
7-
pip install $(CI_PIP_TF_NAME) $(CI_PIP_ONNX_NAME)
7+
pip install $(CI_PIP_TF_NAME)
8+
pip install $(CI_PIP_ONNX_NAME)
89
910
# TF < 2.7 reuires numpy <= 1.19, but onnxruntime >= 1.11 requires numpy >= 1.21
1011
if [[ $CI_TF_VERSION < 2.7 ]] && [[ $CI_ONNX_BACKEND == "onnxruntime" ]] ;

ci_build/azure_pipelines/templates/unit_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Run unit test
22

33
parameters:
4-
onnx_opsets: ['17', '16', '15', '14', '13']
4+
onnx_opsets: ['18', '17', '16', '15', '14', '13']
55
skip_tflite_tests: 'True'
66
skip_tfjs_tests: 'True'
77
skip_tf_tests: 'False'

ci_build/azure_pipelines/trimmed_keras2onnx_application_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
INSTALL_KERAS:
5252
UNINSTALL_KERAS:
5353
INSTALL_TENSORFLOW: pip install tensorflow==2.11.0
54-
INSTALL_ORT: pip install onnxruntime==1.13.1
54+
INSTALL_ORT: pip install onnxruntime==1.14.1
5555
INSTALL_KERAS_RESNET: pip install keras-resnet
5656
INSTALL_TRANSFORMERS: pip install transformers==4.2.0
5757
INSTALL_NUMPY:

ci_build/azure_pipelines/trimmed_keras2onnx_unit_test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ jobs:
6161
python.version: '3.8'
6262
ONNX_PATH: onnx==1.12.0
6363
TENSORFLOW_PATH: tensorflow==2.9.0
64-
INSTALL_ORT: pip install onnxruntime==1.12.0
64+
INSTALL_ORT: pip install onnxruntime==1.13.1
6565
INSTALL_NUMPY:
6666

6767
Python310-tf-latest:
6868
python.version: '3.10'
6969
ONNX_PATH: onnx==1.13.1
7070
TENSORFLOW_PATH: tensorflow==2.11.0
71-
INSTALL_ORT: pip install onnxruntime==1.13.1
71+
INSTALL_ORT: pip install onnxruntime==1.14.1
7272
INSTALL_NUMPY:
7373

7474
############ Pure Keras Unit Tests ############

tests/keras2onnx_unit_tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# https://github.com/onnx/tensorflow-onnx/issues/2132
2121
ORT_OPSET_VERSION = {
2222
"1.6.0": 13, "1.7.0": 13, "1.8.0": 14, "1.9.0": 15, "1.10.0": 15, "1.11.0": 16,
23-
"1.12.0": 17, "1.13.0": 17, "1.14.0": 17
23+
"1.12.0": 17, "1.13.0": 17, "1.14.0": 18
2424
}
2525

2626
working_path = os.path.abspath(os.path.dirname(__file__))

tests/test_onnx_shape_inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_split(self):
8787
shapes = [[5, 6, 7]]
8888
dtypes = [TensorProto.FLOAT]
8989
graph = self._create_empty_graph(inputs, shapes, dtypes)
90-
node = graph.make_node("Split", [INPUT1], output_count=2, attr={"axis": 1})
90+
node = graph.make_node("Split", [INPUT1], output_count=2, attr={"axis": 1, "num_outputs": 2})
9191
graph.add_graph_output(node.output[0])
9292
graph.add_graph_output(node.output[1])
9393
self._run_test_case(graph, self._generate_random_inputs(inputs, shapes, dtypes))

tests/test_optimizers.py

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ def test_transpose_with_split(self, input_shape, perm, inner_perm):
127127
output_shape = [output_before_trans[i] for i in perm]
128128
for axis in range(len(input_shape)):
129129
node1 = helper.make_node("Transpose", ["X"], ["Y"], perm=inner_perm, name="trans1")
130-
node2 = helper.make_node("Split", ["Y"], ["Z"], axis=axis, name="split")
130+
if self.config.opset < 18:
131+
node2 = helper.make_node("Split", ["Y"], ["Z"], axis=axis, name="split")
132+
else:
133+
node2 = helper.make_node("Split", ["Y"], ["Z"], axis=axis, name="split", num_outputs=1)
131134
node3 = helper.make_node("Transpose", ["Z"], ["res"], perm=perm, name="trans2")
132135

133136
graph = helper.make_graph(
@@ -148,7 +151,10 @@ def test_transpose_with_split(self, input_shape, perm, inner_perm):
148151
@check_opset_max_version(12, "split attribute changed to input since opset 13")
149152
def test_transpose_with_split_dynamic_shape(self, input_shape, specific_input, output_shape, perm):
150153
node1 = helper.make_node("Transpose", ["X"], ["Y"], perm=perm, name="trans")
151-
node2 = helper.make_node("Split", ["Y"], ["Z"], axis=1, split=[1], name="split")
154+
if self.config.opset < 18:
155+
node2 = helper.make_node("Split", ["Y"], ["Z"], axis=1, split=[1], name="split")
156+
else:
157+
node2 = helper.make_node("Split", ["Y"], ["Z"], axis=1, split=[1], name="split", num_outputs=1)
152158
node3 = helper.make_node("Squeeze", ["Z"], ["B"], name="squeeze")
153159

154160
graph = helper.make_graph(
@@ -1305,6 +1311,7 @@ def test_transpose_reciprocal(self, shape, perm_input, perm_output):
13051311
((1, 3, 4, 5), (1, 3, 1, 1), [0, 2, 3, 1], [0, 3, 1, 2]),
13061312
((1, 3, 4, 5, 6), (1, 3, 1, 1, 1), [0, 2, 3, 4, 1], [0, 4, 1, 2, 3]),
13071313
])
1314+
@check_opset_max_version(17, "ReduceMean from opset <= 17 has axes as an attribute")
13081315
def test_transpose_reducemean(self, input_shape, output_shape, perm_input, perm_output):
13091316
node0 = helper.make_node("Transpose", ["X"], ["Y"], perm=perm_input, name="trans_1")
13101317
node1 = helper.make_node("ReduceMean", ["Y"], ["Z"], axes=list(range(1, len(input_shape) - 1)),
@@ -1322,6 +1329,29 @@ def test_transpose_reducemean(self, input_shape, output_shape, perm_input, perm_
13221329
self.run_transpose_compare(["res"], {"X": np.random.randn(*input_shape).astype(np.float32)},
13231330
model_proto, remaining_transpose_num=0)
13241331

1332+
@parameterized.expand([
1333+
((3, 4, 5), (3, 4, 1), [1], [0, 2, 1], [0, 2, 1]),
1334+
((1, 3, 4, 5), (1, 3, 1, 1), [1, 2], [0, 2, 3, 1], [0, 3, 1, 2]),
1335+
((1, 3, 4, 5, 6), (1, 3, 1, 1, 1), [1, 2, 3], [0, 2, 3, 4, 1], [0, 4, 1, 2, 3]),
1336+
])
1337+
@check_opset_min_version(18, "ReduceMean from opset > 17 has axes as an input")
1338+
def test_transpose_reducemean_18(self, input_shape, output_shape, axes, perm_input, perm_output):
1339+
node0 = helper.make_node("Transpose", ["X"], ["Y"], perm=perm_input, name="trans_1")
1340+
node1 = helper.make_node("ReduceMean", ["Y", "axes"], ["Z"], keepdims=1, name="reducemean")
1341+
node2 = helper.make_node("Transpose", ["Z"], ["res"], perm=perm_output, name="trans_2")
1342+
graph = helper.make_graph(
1343+
[node0, node1, node2],
1344+
"transpose-reducemean-test-18",
1345+
[helper.make_tensor_value_info("X", TensorProto.FLOAT, input_shape)],
1346+
[helper.make_tensor_value_info("res", TensorProto.FLOAT, output_shape)],
1347+
[helper.make_tensor("axes", data_type=TensorProto.INT64,
1348+
dims=[len(axes)], vals=axes)],
1349+
)
1350+
1351+
model_proto = self.make_model(graph, producer_name="onnx-tests")
1352+
self.run_transpose_compare(["res"], {"X": np.random.randn(*input_shape).astype(np.float32)},
1353+
model_proto, remaining_transpose_num=0)
1354+
13251355
@parameterized.expand([
13261356
((3, 4, 5), (3, 4, 1), [1], [0, 2, 1], [0, 2, 1]),
13271357
((1, 3, 4, 5), (1, 3, 4, 1), [2], [0, 2, 3, 1], [0, 3, 1, 2]),
@@ -1357,6 +1387,7 @@ def test_transpose_reducesum(self, input_shape, output_shape, axes, perm_input,
13571387
((1, 3, 4, 5, 6), (1, 3), [1, 2, 3], [0, 2, 3, 4, 1], [0, 1]),
13581388
((1, 3, 4, 5, 6), (), [0, 1, 2, 3, 4], [0, 2, 3, 4, 1], []),
13591389
])
1390+
@check_opset_max_version(17, "ReduceMax from opset <= 17 has axes as an attribute")
13601391
def test_transpose_reducemax(self, input_shape, output_shape, axes, perm_input, perm_output):
13611392
node0 = helper.make_node("Transpose", ["X"], ["Y"], perm=perm_input, name="trans_1")
13621393
node1 = helper.make_node("ReduceMax", ["Y"], ["Z"], axes=axes,
@@ -1377,6 +1408,30 @@ def test_transpose_reducemax(self, input_shape, output_shape, axes, perm_input,
13771408
self.run_transpose_compare(["res"], {"X": np.random.randn(*input_shape).astype(np.float32)},
13781409
model_proto, remaining_transpose_num=0)
13791410

1411+
@parameterized.expand([
1412+
((3, 4, 5), (3, 4, 1), [1], [0, 2, 1], [0, 2, 1]),
1413+
((1, 3, 4, 5), (1, 3, 1, 1), [1, 2], [0, 2, 3, 1], [0, 3, 1, 2]),
1414+
((1, 3, 4, 5, 6), (1, 3, 1, 1, 1), [1, 2, 3], [0, 2, 3, 4, 1], [0, 4, 1, 2, 3]),
1415+
])
1416+
@check_opset_min_version(18, "ReduceMax from opset > 17 has axes as an input")
1417+
def test_transpose_reducemax_18(self, input_shape, output_shape, axes, perm_input, perm_output):
1418+
node0 = helper.make_node("Transpose", ["X"], ["Y"], perm=perm_input, name="trans_1")
1419+
node1 = helper.make_node("ReduceMax", ["Y", "axes"], ["Z"], keepdims=1, name="reducemax")
1420+
node2 = helper.make_node("Transpose", ["Z"], ["res"], perm=perm_output, name="trans_2")
1421+
1422+
graph = helper.make_graph(
1423+
[node0, node1, node2],
1424+
"transpose-reducemax-test-18",
1425+
[helper.make_tensor_value_info("X", TensorProto.FLOAT, input_shape)],
1426+
[helper.make_tensor_value_info("res", TensorProto.FLOAT, output_shape)],
1427+
[helper.make_tensor("axes", data_type=TensorProto.INT64,
1428+
dims=[len(axes)], vals=axes)],
1429+
)
1430+
1431+
model_proto = self.make_model(graph, producer_name="onnx-tests")
1432+
self.run_transpose_compare(["res"], {"X": np.random.randn(*input_shape).astype(np.float32)},
1433+
model_proto, remaining_transpose_num=0)
1434+
13801435
def test_transpose_argmax(self):
13811436
input_shape = [1, 2, 3, 4]
13821437
node0 = helper.make_node("Transpose", ["X"], ["Y"], perm=[0, 2, 3, 1], name="trans_1")
@@ -1858,6 +1913,7 @@ def test_duplicated_duplicated_input(self):
18581913
self.run_merge_duplicated_nodes_compare(["OUT"], {"X": np.random.randn(5, 5).astype(np.float32)}, model_proto,
18591914
op_type="Add", remaining_op_num=2)
18601915

1916+
@check_opset_max_version(17, "ReduceMin from opset <= 17 has axes as an attribute")
18611917
def test_duplicated_duplicated_attributes(self):
18621918
# same attr or not
18631919
node0 = helper.make_node('ReduceMin', inputs=["X"], outputs=["value0"], axes=[0], keepdims=0)
@@ -1877,6 +1933,31 @@ def test_duplicated_duplicated_attributes(self):
18771933
self.run_merge_duplicated_nodes_compare(["OUT"], {"X": np.random.randn(5, 5).astype(np.float32)}, model_proto,
18781934
op_type="ReduceMin", remaining_op_num=2)
18791935

1936+
@check_opset_min_version(18, "ReduceMin from opset > 17 has axes as an input")
1937+
def test_duplicated_duplicated_attributes_18(self):
1938+
# same attr or not
1939+
node0 = helper.make_node('ReduceMin', inputs=["X", "axes0"], outputs=["value0"], keepdims=0)
1940+
node1 = helper.make_node('ReduceMin', inputs=["X", "axes1"], outputs=["value1"], keepdims=0)
1941+
node2 = helper.make_node('ReduceMin', inputs=["X", "axes2"], outputs=["value2"], keepdims=0)
1942+
node3 = helper.make_node('Add', inputs=["value0", "value1"], outputs=["value3"])
1943+
node4 = helper.make_node("Mul", ["value2", "value3"], ["OUT"])
1944+
1945+
graph = helper.make_graph(
1946+
[node0, node1, node2, node3, node4],
1947+
"test_duplicated_duplicated_attributes_18",
1948+
[helper.make_tensor_value_info("X", TensorProto.FLOAT, (5, 5))],
1949+
[helper.make_tensor_value_info("OUT", TensorProto.FLOAT, (5,))],
1950+
[
1951+
helper.make_tensor("axes0", data_type=TensorProto.INT64, dims=[1], vals=[0]),
1952+
helper.make_tensor("axes1", data_type=TensorProto.INT64, dims=[1], vals=[0]),
1953+
helper.make_tensor("axes2", data_type=TensorProto.INT64, dims=[1], vals=[1]),
1954+
]
1955+
)
1956+
1957+
model_proto = self.make_model(graph, producer_name="onnx-tests")
1958+
self.run_merge_duplicated_nodes_compare(["OUT"], {"X": np.random.randn(5, 5).astype(np.float32)}, model_proto,
1959+
op_type="ReduceMin", remaining_op_num=2)
1960+
18801961
def _check_initializer_num(self, graph_proto, num):
18811962
return num == len(graph_proto.initializer)
18821963

@@ -2062,7 +2143,10 @@ def test_reshape_opt_with_mul(self):
20622143
node55 = helper.make_node("Constant", [], ["six"], value=six_tensor)
20632144

20642145
node6 = helper.make_node("Gather", ["S", "g_indices"], ["dims12"])
2065-
node7 = helper.make_node("ReduceProd", ["dims12"], ["dims12_prod"], axes=[0])
2146+
if self.config.opset >= 18:
2147+
node7 = helper.make_node("ReduceProd", ["dims12", "axes"], ["dims12_prod"])
2148+
else:
2149+
node7 = helper.make_node("ReduceProd", ["dims12"], ["dims12_prod"], axes=[0])
20662150
if self.config.opset >= 10:
20672151
node8 = helper.make_node("Slice", ["S", "starts", "ends", ""], ["dim0"])
20682152
else:
@@ -2312,7 +2396,10 @@ def test_const_fold_split(self):
23122396
const_tensor = helper.make_tensor(name='const_tensor', data_type=TensorProto.FLOAT, dims=shape,
23132397
vals=np.random.randn(2, 6, 1).flatten().astype(np.float32))
23142398
node0 = helper.make_node("Constant", [], ["const"], value=const_tensor)
2315-
node1 = helper.make_node("Split", ["const"], ["out1", "out2", "out3"], axis=1)
2399+
if self.config.opset < 18:
2400+
node1 = helper.make_node("Split", ["const"], ["out1", "out2", "out3"], axis=1)
2401+
else:
2402+
node1 = helper.make_node("Split", ["const"], ["out1", "out2", "out3"], axis=1, num_outputs=3)
23162403
node2 = helper.make_node("Sum", ["inp", "out1", "out2", "out3"], ["out4"])
23172404

23182405
graph = helper.make_graph(
@@ -2331,7 +2418,10 @@ def test_const_fold_split_one(self):
23312418
const_tensor = helper.make_tensor(name='const_tensor', data_type=TensorProto.FLOAT, dims=shape,
23322419
vals=np.random.randn(2, 6, 1).flatten().astype(np.float32))
23332420
node0 = helper.make_node("Constant", [], ["const"], value=const_tensor)
2334-
node1 = helper.make_node("Split", ["const"], ["out1"], axis=1)
2421+
if self.config.opset < 18:
2422+
node1 = helper.make_node("Split", ["const"], ["out1"], axis=1)
2423+
else:
2424+
node1 = helper.make_node("Split", ["const"], ["out1"], axis=1, num_outputs=1)
23352425
node2 = helper.make_node("Sum", ["inp", "out1"], ["out4"])
23362426

23372427
graph = helper.make_graph(
@@ -2374,7 +2464,11 @@ def test_const_fold_split_const_splits(self):
23742464
const_tensor = helper.make_tensor(name='const_tensor', data_type=TensorProto.FLOAT, dims=shape,
23752465
vals=np.random.randn(2, 6, 1).flatten().astype(np.float32))
23762466
node0 = helper.make_node("Constant", [], ["const"], value=const_tensor)
2377-
node2 = helper.make_node("Split", ["const"], ["out1", "out2", "out3"], axis=1, split=[1, 3, 2])
2467+
if self.config.opset < 18:
2468+
node2 = helper.make_node("Split", ["const"], ["out1", "out2", "out3"], axis=1, split=[1, 3, 2])
2469+
else:
2470+
node2 = helper.make_node("Split", ["const"], ["out1", "out2", "out3"], axis=1, split=[1, 3, 2],
2471+
num_outputs=3)
23782472
node3 = helper.make_node("Sum", ["inp", "out2"], ["out4"])
23792473

23802474
graph = helper.make_graph(

tf2onnx/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@
5555
# Note: opset 7 and opset 8 came out with IR3 but we need IR4 because of PlaceholderWithDefault
5656
# Refer from https://github.com/onnx/onnx/blob/main/docs/Versioning.md#released-versions
5757
OPSET_TO_IR_VERSION = {
58-
1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3, 7: 4, 8: 4, 9: 4, 10: 5, 11: 6, 12: 7, 13: 7, 14: 7, 15: 8, 16: 8, 17: 8
58+
1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3, 7: 4, 8: 4, 9: 4, 10: 5, 11: 6, 12: 7, 13: 7, 14: 7, 15: 8, 16: 8, 17: 8, 18: 8
5959
}

0 commit comments

Comments
 (0)