Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / one-cmds / one-quantize
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 "Quantize circle model."
24   echo "Usage: one-quantize"
25   echo "    --version         Show version information and exit"
26   echo "    --input_dtype     Input data type (supported: float32, default=float32)"
27   echo "    --quantized_dtype Output quantized data type (supported: uint8, default=uint8)"
28   echo "    --granularity     Quantize granularity (supported: layer, channel, default=layer)"
29   echo "    --min_percentile  Minimum percentile (0.0~100.0, default=1.0)"
30   echo "    --max_percentile  Maximum percentile (0.0~100.0, default=99.0)"
31   echo "    --mode            Record mode (supported: percentile/moving_average, default=percentile)"
32   echo "    --input_path <path/to/input/circle>"
33   echo "    --input_data <path/to/input/data>"
34   echo "    --output_path <path/to/output/circle>"
35   exit 255
36 }
37
38 version()
39 {
40   $DRIVER_PATH/one-version one-quantize
41   exit 255
42 }
43
44 INPUT_DTYPE=float32
45 QUANTIZED_DTYPE=uint8
46 GRANULARITY=layer
47 MIN_PERCENTILE=1
48 MAX_PERCENTILE=99
49 MODE=percentile
50
51 # Parse command-line arguments
52 #
53 while [ "$#" -ne 0 ]; do
54   CUR="$1"
55
56   case $CUR in
57     '--help')
58       usage
59       ;;
60     '--version')
61       version
62       ;;
63
64     '--input_dtype')
65       INPUT_DTYPE="$2"
66       shift 2
67       ;;
68     '--quantized_dtype')
69       QUANTIZED_DTYPE="$2"
70       shift 2
71       ;;
72     '--granularity')
73       GRANULARITY="$2"
74       shift 2
75       ;;
76     '--min_percentile')
77       MIN_PERCENTILE="$2"
78       shift 2
79       ;;
80     '--max_percentile')
81       MAX_PERCENTILE="$2"
82       shift 2
83       ;;
84     '--mode')
85       MODE="$2"
86       shift 2
87       ;;
88
89     '--input_path')
90       INPUT_PATH="$2"
91       shift 2
92       ;;
93     '--input_data')
94       INPUT_DATA="$2"
95       shift 2
96       ;;
97     '--output_path')
98       OUTPUT_PATH="$2"
99       shift 2
100       ;;
101
102     *)
103       echo "Unknown parameter: ${CUR}"
104       shift
105       ;;
106   esac
107 done
108
109 if [ -z ${INPUT_PATH} ] || [ ! -e ${INPUT_PATH} ]; then
110   echo "Error: input model not found"
111   echo ""
112   usage
113 fi
114 if [ -z ${INPUT_DATA} ] || [ ! -e ${INPUT_DATA} ]; then
115   echo "Error: input data not found"
116   echo ""
117   usage
118 fi
119
120 FILE_BASE=$(basename ${OUTPUT_PATH})
121 MODEL_NAME="${FILE_BASE%.*}"
122
123 TMPDIR=$(mktemp -d)
124 trap "{ rm -rf $TMPDIR; }" EXIT
125
126 # remove previous log
127 rm -rf "${OUTPUT_PATH}.log"
128
129 show_err_onexit()
130 {
131   cat "${OUTPUT_PATH}.log"
132 }
133
134 trap show_err_onexit ERR
135
136 # quantize circle
137 echo "${DRIVER_PATH}/circle-quantizer" \
138 --quantize_dequantize_weights ${INPUT_DTYPE} ${QUANTIZED_DTYPE} ${GRANULARITY} \
139 "${INPUT_PATH}" "${TMPDIR}/${MODEL_NAME}.1.circle" > "${OUTPUT_PATH}.log"
140 echo " " >> "${OUTPUT_PATH}.log"
141
142 "${DRIVER_PATH}/circle-quantizer" \
143 --quantize_dequantize_weights ${INPUT_DTYPE} ${QUANTIZED_DTYPE} ${GRANULARITY} \
144 "${INPUT_PATH}" "${TMPDIR}/${MODEL_NAME}.1.circle" >> "${OUTPUT_PATH}.log" 2>&1
145
146 echo " " >> "${OUTPUT_PATH}.log"
147 echo "${DRIVER_PATH}/record-minmax" \
148 --input_model "${TMPDIR}/${MODEL_NAME}.1.circle" \
149 --input_data "${INPUT_DATA}" \
150 --min_percentile ${MIN_PERCENTILE} --max_percentile ${MAX_PERCENTILE} \
151 --mode "${MODE}" \
152 --output_model "${TMPDIR}/${MODEL_NAME}.2.circle" >> "${OUTPUT_PATH}.log" 2>&1
153 echo " " >> "${OUTPUT_PATH}.log"
154
155 "${DRIVER_PATH}/record-minmax" \
156 --input_model "${TMPDIR}/${MODEL_NAME}.1.circle" \
157 --input_data "${INPUT_DATA}" \
158 --min_percentile ${MIN_PERCENTILE} --max_percentile ${MAX_PERCENTILE} \
159 --mode "${MODE}" \
160 --output_model "${TMPDIR}/${MODEL_NAME}.2.circle" >> "${OUTPUT_PATH}.log" 2>&1
161
162 echo " " >> "${OUTPUT_PATH}.log"
163 echo "${DRIVER_PATH}/circle-quantizer" \
164 --quantize_with_minmax ${INPUT_DTYPE} ${QUANTIZED_DTYPE} ${GRANULARITY} \
165 "${TMPDIR}/${MODEL_NAME}.2.circle" "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log" 2>&1
166 echo " " >> "${OUTPUT_PATH}.log"
167
168 "${DRIVER_PATH}/circle-quantizer" \
169 --quantize_with_minmax ${INPUT_DTYPE} ${QUANTIZED_DTYPE} ${GRANULARITY} \
170 "${TMPDIR}/${MODEL_NAME}.2.circle" "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log" 2>&1