diff options
Diffstat (limited to 'compiler/one-cmds/one-build')
-rw-r--r-- | compiler/one-cmds/one-build | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/compiler/one-cmds/one-build b/compiler/one-cmds/one-build new file mode 100644 index 000000000..b21a1624b --- /dev/null +++ b/compiler/one-cmds/one-build @@ -0,0 +1,190 @@ +#!/usr/bin/env bash +''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && 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 configparser +import os +import sys + +import onelib.utils as oneutils + +# TODO Find better way to suppress trackback on error +# This suppression is applied only to `one-build` +sys.tracebacklimit = 0 + + +def _get_parser(): + parser = argparse.ArgumentParser( + description='command line tool to run ONE drivers in customized order') + + oneutils.add_default_arg(parser) + + opt_name_list = oneutils.get_optimization_list(get_name=True) + opt_name_list = ['-' + s for s in opt_name_list] + if not opt_name_list: + opt_help_message = '(No available optimization options)' + else: + opt_help_message = '(Available optimization options: ' + ', '.join( + opt_name_list) + ')' + opt_help_message = 'optimization name to use ' + opt_help_message + parser.add_argument('-O', type=str, metavar='OPTIMIZATION', help=opt_help_message) + + return parser + + +def _parse_arg(parser): + args = parser.parse_args() + # print version + if args.version: + oneutils.print_version_and_exit(__file__) + + return args + + +def _verify_arg(parser, args): + """verify given arguments""" + # check if required arguments is given + if not oneutils.is_valid_attr(args, 'config'): + parser.error('-C/--config argument is required') + # check if given optimization option exists + opt_name_list = oneutils.get_optimization_list(get_name=True) + opt_name_list = [oneutils.remove_prefix(s, 'O') for s in opt_name_list] + if oneutils.is_valid_attr(args, 'O'): + if ' ' in getattr(args, 'O'): + parser.error('Not allowed to have space in the optimization name') + if not getattr(args, 'O') in opt_name_list: + parser.error('Invalid optimization option') + + +def _get_driver_name(driver_name): + return { + 'one-import-bcq': 'one-import-bcq', + 'one-import-tf': 'one-import-tf', + 'one-import-tflite': 'one-import-tflite', + 'one-import-onnx': 'one-import-onnx', + 'one-optimize': 'one-optimize', + 'one-quantize': 'one-quantize', + 'one-partition': 'one-partition', + 'one-pack': 'one-pack', + 'one-codegen': 'one-codegen' + }[driver_name] + + +def parse_cfg(args): + config = configparser.ConfigParser() + config.optionxform = str + parsed = config.read(os.path.expanduser(getattr(args, 'config'))) + if not parsed: + raise FileNotFoundError('Not found given configuration file') + return config + + +def _is_available_driver(config, driver_name): + return config.has_option('one-build', driver_name) and config.getboolean( + 'one-build', driver_name) + + +def _verify_cfg(driver_list, config): + if not config.has_section('one-build'): + raise ImportError('[one-build] section is required in configuraion file') + + import_driver_cnt = 0 + if _is_available_driver(config, 'one-import-tf'): + import_driver_cnt += 1 + if _is_available_driver(config, 'one-import-tflite'): + import_driver_cnt += 1 + if _is_available_driver(config, 'one-import-bcq'): + import_driver_cnt += 1 + if _is_available_driver(config, 'one-import-onnx'): + import_driver_cnt += 1 + if import_driver_cnt > 1: + raise AssertionError('Only one import-* driver can be executed') + + +# verify given optimization option file +def _verify_opt(args): + if oneutils.is_valid_attr(args, 'O'): + config = configparser.ConfigParser() + config.optionxform = str + opt_name_path_dic = dict( + zip(oneutils.get_optimization_list(get_name=True), + oneutils.get_optimization_list())) + parsed = config.read(opt_name_path_dic['O' + getattr(args, 'O')]) + # check if given optimization option file exists + if not parsed: + raise FileNotFoundError('Not found given optimization configuration file') + # check if given optimization option file only has `one-optimize` section + if len(config.sections()) == 1 and config.sections()[0] == 'one-optimize': + pass + else: + raise AssertionError( + 'Optimization configuration file only allowed to have a \'one-optimize\' section' + ) + + +def main(): + # parse arguments + # since the configuration file path is required first, + # parsing of the configuration file proceeds after this. + parser = _get_parser() + args = _parse_arg(parser) + + # verify arguments + _verify_arg(parser, args) + + # parse configuration file + config = parse_cfg(args) + + # verify configuration file + bin_dir = os.path.dirname(os.path.realpath(__file__)) + import_drivers_dict = oneutils.detect_one_import_drivers(bin_dir) + transform_drivers = [ + 'one-optimize', 'one-quantize', 'one-pack', 'one-codegen', 'one-profile', + 'one-partition' + ] + _verify_cfg(import_drivers_dict, config) + + # verify optimization option file + _verify_opt(args) + + # get sections to run + section_to_run = [] + for d in list(import_drivers_dict) + transform_drivers: + if _is_available_driver(config, d): + section_to_run.append(d) + + # run + dir_path = os.path.dirname(os.path.realpath(__file__)) + for section in section_to_run: + if section in import_drivers_dict: + # we already has driver name in dict + driver_name = import_drivers_dict[section] + else: + driver_name = _get_driver_name(section) + driver_path = os.path.join(dir_path, driver_name) + cmd = [driver_path, '--config', getattr(args, 'config'), '--section', section] + if section == 'one-optimize' and oneutils.is_valid_attr(args, 'O'): + cmd += ['-O', getattr(args, 'O')] + oneutils.run(cmd) + + +if __name__ == '__main__': + oneutils.safemain(main, __file__) |