summaryrefslogtreecommitdiff
path: root/compiler/one-cmds/one-build
blob: b21a1624b4d358f9080e57290aa71fe13b1083c8 (plain)
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
182
183
184
185
186
187
188
189
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__)