Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / one-cmds / one-optimize
1 #!/bin/bash
2
3 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #    http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -e
18
19 DRIVER_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20
21 usage()
22 {
23   echo "Optimize circle model."
24   echo "Usage: one-optimize"
25   echo "    --all           Enable all optimization algorithms"
26   echo "    --fuse_bcq      Enable FuseBCQ Pass"
27   echo "    --fuse_instnorm Enable FuseInstanceNormalization Pass"
28   echo "    --resolve_customop_add"
29   echo "                    Enable ResolveCustomOpAddPass Pass"
30   echo "    --resolve_customop_batchmatmul"
31   echo "                    Enable ResolveCustomOpBatchMatMulPass Pass"
32   echo "    --resolve_customop_matmul"
33   echo "                    Enable ResolveCustomOpMatMulPass Pass"
34   echo "    --input_path <path/to/input/circle>"
35   echo "    --output_path <path/to/output/circle>"
36   exit 0
37 }
38
39 OPTIMIZE_all=0
40 OPTIMIZE_fuse_bcq=0
41 OPTIMIZE_fuse_instnorm=0
42 OPTIMIZE_resolve_customop_add=0
43 OPTIMIZE_resolve_customop_batchmatmul=0
44 OPTIMIZE_resolve_customop_matmul=0
45
46 # Parse command-line arguments
47 #
48 while [ "$#" -ne 0 ]; do
49   CUR="$1"
50
51   case $CUR in
52     '--help')
53       usage
54       ;;
55     '--all')
56       OPTIMIZE_all=1
57       shift
58       ;;
59     '--fuse_bcq')
60       OPTIMIZE_fuse_bcq=1
61       shift
62       ;;
63     '--fuse_instnorm')
64       OPTIMIZE_fuse_instnorm=1
65       shift
66       ;;
67     '--resolve_customop_add')
68       OPTIMIZE_resolve_customop_add=1
69       shift
70       ;;
71     '--resolve_customop_batchmatmul')
72       OPTIMIZE_resolve_customop_batchmatmul=1
73       shift
74       ;;
75     '--resolve_customop_matmul')
76       OPTIMIZE_resolve_customop_matmul=1
77       shift
78       ;;
79
80     '--input_path')
81       export INPUT_PATH="$2"
82       shift 2
83       ;;
84     '--output_path')
85       export OUTPUT_PATH="$2"
86       shift 2
87       ;;
88     *)
89       echo "Unknown parameter: ${CUR}"
90       shift
91       ;;
92   esac
93 done
94
95 if [ -z ${INPUT_PATH} ] || [ ! -e ${INPUT_PATH} ]; then
96   echo "Error: input model not found"
97   echo ""
98   usage
99   exit 2
100 fi
101
102 OPTIMIZE_OPTIONS=""
103
104 if [ $OPTIMIZE_all == 1 ]; then
105   OPTIMIZE_OPTIONS+="--all "
106 fi
107 if [ $OPTIMIZE_fuse_bcq == 1 ]; then
108   OPTIMIZE_OPTIONS+="--fuse_bcq "
109 fi
110 if [ $OPTIMIZE_fuse_instnorm == 1 ]; then
111   OPTIMIZE_OPTIONS+="--fuse_instnorm "
112 fi
113 if [ $OPTIMIZE_resolve_customop_add == 1 ]; then
114   OPTIMIZE_OPTIONS+="--resolve_customop_add "
115 fi
116 if [ $OPTIMIZE_resolve_customop_batchmatmul == 1 ]; then
117   OPTIMIZE_OPTIONS+="--resolve_customop_batchmatmul "
118 fi
119 if [ $OPTIMIZE_resolve_customop_matmul == 1 ]; then
120   OPTIMIZE_OPTIONS+="--resolve_customop_matmul "
121 fi
122
123 # remove previous log
124 rm -rf "${OUTPUT_PATH}.log"
125
126 # NOTE do not wrap ${OPTIMIZE_OPTIONS} with ""
127 # optimize circle
128 echo "${DRIVER_PATH}/circle2circle" ${OPTIMIZE_OPTIONS} \
129 "${INPUT_PATH}" "${OUTPUT_PATH}" > "${OUTPUT_PATH}.log"
130
131 "${DRIVER_PATH}/circle2circle" ${OPTIMIZE_OPTIONS} \
132 "${INPUT_PATH}" "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log" 2>&1