Skip to content

Commit

Permalink
Switch to onnxoptimizer as onnx.optimizer is removed
Browse files Browse the repository at this point in the history
Looks like multiple onnx-ml.proto copies are allowed in protobuf-lite.
See also [1] for the protobuf issue.

[1] onnx/optimizer#38
  • Loading branch information
Chih-Hsuan Yen committed Oct 10, 2021
1 parent eadb951 commit 07f8ffb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/common/data.cpp
/common/data.h
/data/others.txt
/data/*-opt*.onnx

# CCS-generated files
.launches/
Expand Down
Binary file removed data/KWS-DNN_S-opt.onnx
Binary file not shown.
Binary file removed data/mnist-8-opt.onnx
Binary file not shown.
Binary file removed data/squeezenet_cifar10-opt.onnx
Binary file not shown.
34 changes: 16 additions & 18 deletions transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import onnx
import onnx.helper
import onnxoptimizer
import numpy as np

from utils import (
Expand Down Expand Up @@ -264,22 +265,15 @@ def get_prev_node(n):
Constants.LEA_BUFFER_SIZE = lea_buffer_size[args.target]

onnx_opt_model_name = config['onnx_model'].replace('.onnx', '-opt.onnx')
if os.path.exists(onnx_opt_model_name):
onnx_model = onnx.load(onnx_opt_model_name)
else:
onnx_model = onnx.load(config['onnx_model'])
try:
import onnx.optimizer
# https://zhuanlan.zhihu.com/p/41255090
onnx_model = onnx.optimizer.optimize(onnx_model, [
'fuse_add_bias_into_conv',
'fuse_matmul_add_bias_into_gemm',
])
except IndexError:
# Somehow the optimizer cannot handle models transformed from keras2onnx
pass
onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
onnx.save_model(onnx_model, onnx_opt_model_name)
onnx_model = onnx.load(config['onnx_model'])
# https://zhuanlan.zhihu.com/p/41255090
onnx_model = onnxoptimizer.optimize(onnx_model, [
'fuse_add_bias_into_conv',
'fuse_matmul_add_bias_into_gemm',
])

onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
onnx.save_model(onnx_model, onnx_opt_model_name)
g = onnx_model.graph
names = {}

Expand All @@ -301,8 +295,12 @@ def find_initializer(name):
return initializer

def replace_squeeze(node, inp):
axes_name = node.input[1]
axes = find_initializer(axes_name).int64_data
# Since opset 13, axes is an input instead of an attribute
try:
axes_name = node.input[1]
axes = find_initializer(axes_name).int64_data
except IndexError:
axes = get_attr(node, 'axes')
new_dims = [dim for dim_idx, dim in enumerate(inp.dims) if dim_idx not in axes]
# Repeated fields cannot be assigned directly
# https://developers.google.com/protocol-buffers/docs/reference/python-generated#repeated-fields
Expand Down

0 comments on commit 07f8ffb

Please sign in to comment.