-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathone-optimize
More file actions
181 lines (143 loc) · 6.33 KB
/
one-optimize
File metadata and controls
181 lines (143 loc) · 6.33 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env bash
''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" >/dev/null && pwd)" # '''
''''export PY_PATH=${SCRIPT_PATH}/venv/bin/python # '''
''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@" # '''
''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # '''
''''exit 255 # '''
# Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import sys
import onelib.constant as _constant
import onelib.make_cmd as _make_cmd
import onelib.utils as oneutils
# TODO Find better way to suppress trackback on error
sys.tracebacklimit = 0
def _get_parser():
parser = argparse.ArgumentParser(
description='command line tool to optimize circle model')
oneutils.add_default_arg(parser)
## utility arguments
utility_group = parser.add_argument_group('arguments for utility')
utility_group.add_argument('-p',
'--generate_profile_data',
action='store_true',
help='generate profiling data')
utility_group.add_argument(
'--change_outputs',
type=str,
help='Experimental: Change first subgraph output nodes to CSV names')
## circle2circle arguments
circle2circle_group = parser.add_argument_group('arguments for optimization')
# input and output path.
circle2circle_group.add_argument('-i',
'--input_path',
type=str,
help='full filepath of the input file')
circle2circle_group.add_argument('-o',
'--output_path',
type=str,
help='full filepath of the output file')
# optimization pass
for opt in _constant.CONSTANT.OPTIMIZATION_OPTS:
# opt = (option_name, help_message)
circle2circle_group.add_argument('--' + opt[0], action='store_true', help=opt[1])
# optimization option from onecc
parser.add_argument('-O', type=str, help=argparse.SUPPRESS)
return parser
def _verify_arg(parser, args):
"""verify given arguments"""
# check if required arguments is given
missing = []
if not oneutils.is_valid_attr(args, 'input_path'):
missing.append('-i/--input_path')
if not oneutils.is_valid_attr(args, 'output_path'):
missing.append('-o/--output_path')
if len(missing):
parser.error('the following arguments are required: ' + ' '.join(missing))
# default has pre-defined optimization options
default = _get_parser().parse_args()
# check if unrecognized arguments are given
diff = set(dir(args)) - set(dir(default))
if len(diff):
parser.error('the following arguments are unrecognized: ' + ' '.join(diff))
def _parse_arg(parser):
args = parser.parse_args()
# print version
if args.version:
oneutils.print_version_and_exit(__file__)
return args
def _optimize(args):
# get file path to log
dir_path = os.path.dirname(os.path.realpath(__file__))
logfile_path = os.path.realpath(args.output_path) + '.log'
with open(logfile_path, 'wb') as f:
# make a command to optimize circle model
circle2circle_path = os.path.join(dir_path, 'circle2circle')
circle2circle_cmd = _make_cmd.make_circle2circle_cmd(args, circle2circle_path,
getattr(args, 'input_path'),
getattr(args, 'output_path'))
# verbose
if oneutils.is_valid_attr(args, 'verbose'):
circle2circle_cmd.append('--verbose')
if oneutils.is_valid_attr(args, 'change_outputs'):
circle2circle_cmd.append('--change_outputs')
circle2circle_cmd.append(getattr(args, 'change_outputs'))
f.write((' '.join(circle2circle_cmd) + '\n').encode())
# optimize
oneutils.run(circle2circle_cmd, err_prefix="circle2circle", logfile=f)
def _parse_opt(args):
if oneutils.is_valid_attr(args, 'O'):
opt_name_path_dic = dict(
zip(oneutils.get_optimization_list(get_name=True),
oneutils.get_optimization_list()))
config_path = opt_name_path_dic['O' + getattr(args, 'O')]
# group option do not overwrite existing args
oneutils.parse_cfg(config_path, 'one-optimize', args)
# There are several cases to receive the optimization options:
# - Indivisual option
# 1. From command line
# 2. From cfg file
# - Group option
# 3. From command line
#
# Their priority is as follows, since each option can be given simultaneously:
# 1. Indivisual option from command line
# 2. Indivisual option from cfg file
# 3. Group option from command line
#
# To follow their priority, options with higher priority should be parsed first.
#
# DO NOT MODIFY the order of below function calls.
#
# NOTE. Assume all the optimization options must follow 'store_true' only.
# NOTE. Group option from cfg file (`include` in `[onecc]` section) is passed
# as a command line argument.
def main():
# parse arguments
parser = _get_parser()
args = _parse_arg(parser)
# parse configuration file
oneutils.parse_cfg(args.config, 'one-optimize', args)
# parse optimization file
# NOTE if there is a `one-optimize` section in above configuration file as well,
# it will be overwritten
_parse_opt(args)
# verify arguments
_verify_arg(parser, args)
# optimize
_optimize(args)
if __name__ == '__main__':
oneutils.safemain(main, __file__)