converter.py 53.6 KB
Newer Older
Y
yejianwu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright 2018 Xiaomi, Inc.  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.
14 15

import argparse
Y
yejianwu 已提交
16
import filelock
L
liuqi 已提交
17
import glob
18
import hashlib
19
import os
L
liuqi 已提交
20
import re
L
Liangliang He 已提交
21
import sh
22 23
import subprocess
import sys
24
import urllib
Y
yejianwu 已提交
25
import yaml
L
liuqi 已提交
26

27
from enum import Enum
28

29
import sh_commands
30
from sh_commands import BuildType
L
Liangliang He 已提交
31

32
from common import CaffeEnvType
33
from common import DeviceType
34 35 36
from common import mace_check
from common import MaceLogger
from common import StringFormatter
37

38 39 40 41
################################
# common definitions
################################
BUILD_OUTPUT_DIR = 'build'
42
PHONE_DATA_DIR = "/data/local/tmp/mace_run"
43
MODEL_OUTPUT_DIR_NAME = 'model'
L
liuqi 已提交
44
MODEL_HEADER_DIR_PATH = 'include/mace/public'
45 46
BUILD_TMP_DIR_NAME = '_tmp'
BUILD_TMP_GENERAL_OUTPUT_DIR_NAME = 'general'
Y
yejianwu 已提交
47
OUTPUT_LIBRARY_DIR_NAME = 'lib'
48
OUTPUT_OPENCL_BINARY_DIR_NAME = 'opencl'
L
liuqi 已提交
49
OUTPUT_OPENCL_BINARY_FILE_NAME = 'compiled_opencl_kernel'
50
CL_COMPILED_BINARY_FILE_NAME = "mace_cl_compiled_program.bin"
L
liuqi 已提交
51 52
CODEGEN_BASE_DIR = 'mace/codegen'
MODEL_CODEGEN_DIR = CODEGEN_BASE_DIR + '/models'
Y
yejianwu 已提交
53
LIBMACE_SO_TARGET = "//mace:libmace.so"
L
liuqi 已提交
54 55 56 57 58 59
MACE_RUN_STATIC_NAME = "mace_run_static"
MACE_RUN_SHARED_NAME = "mace_run_shared"
EXAMPLE_STATIC_NAME = "example_static"
EXAMPLE_SHARED_NAME = "example_shared"
MACE_RUN_STATIC_TARGET = "//mace/tools/validation:" + MACE_RUN_STATIC_NAME
MACE_RUN_SHARED_TARGET = "//mace/tools/validation:" + MACE_RUN_SHARED_NAME
60
ALL_SOC_TAG = 'all'
61 62

ABITypeStrs = [
L
liuqi 已提交
63 64 65
    'armeabi-v7a',
    'arm64-v8a',
    'host',
66
]
L
liuqi 已提交
67 68 69 70 71 72


class ABIType(object):
    armeabi_v7a = 'armeabi-v7a'
    arm64_v8a = 'arm64-v8a'
    host = 'host'
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


PlatformTypeStrs = [
    "tensorflow",
    "caffe",
]
PlatformType = Enum('PlatformType', [(ele, ele) for ele in PlatformTypeStrs],
                    type=str)

RuntimeTypeStrs = [
    "cpu",
    "gpu",
    "dsp",
    "cpu+gpu"
]


class RuntimeType(object):
    cpu = 'cpu'
    gpu = 'gpu'
    dsp = 'dsp'
    cpu_gpu = 'cpu+gpu'


CPUDataTypeStrs = [
    "fp32",
]

CPUDataType = Enum('CPUDataType', [(ele, ele) for ele in CPUDataTypeStrs],
                   type=str)

GPUDataTypeStrs = [
    "fp16_fp32",
    "fp32_fp32",
]

GPUDataType = Enum('GPUDataType', [(ele, ele) for ele in GPUDataTypeStrs],
                   type=str)

L
liuqi 已提交
112 113 114 115 116 117 118
DSPDataTypeStrs = [
    "uint8",
]

DSPDataType = Enum('DSPDataType', [(ele, ele) for ele in DSPDataTypeStrs],
                   type=str)

119 120
WinogradParameters = [0, 2, 4]

121 122 123 124 125 126 127 128 129 130 131 132 133 134

class DefaultValues(object):
    omp_num_threads = -1,
    cpu_affinity_policy = 1,
    gpu_perf_hint = 3,
    gpu_priority_hint = 3,


class YAMLKeyword(object):
    library_name = 'library_name'
    target_abis = 'target_abis'
    target_socs = 'target_socs'
    build_type = 'build_type'
    embed_model_data = 'embed_model_data'
Y
yejianwu 已提交
135
    linkshared = 'linkshared'
136 137 138 139 140 141 142 143 144
    models = 'models'
    platform = 'platform'
    model_file_path = 'model_file_path'
    model_sha256_checksum = 'model_sha256_checksum'
    weight_file_path = 'weight_file_path'
    weight_sha256_checksum = 'weight_sha256_checksum'
    subgraphs = 'subgraphs'
    input_tensors = 'input_tensors'
    input_shapes = 'input_shapes'
李寅 已提交
145
    input_ranges = 'input_ranges'
146 147 148 149 150 151 152 153 154
    output_tensors = 'output_tensors'
    output_shapes = 'output_shapes'
    runtime = 'runtime'
    data_type = 'data_type'
    limit_opencl_kernel_time = 'limit_opencl_kernel_time'
    nnlib_graph_mode = 'nnlib_graph_mode'
    obfuscate = 'obfuscate'
    winograd = 'winograd'
    validation_inputs_data = 'validation_inputs_data'
李寅 已提交
155
    graph_optimize_options = 'graph_optimize_options'  # internal use for now
156 157 158 159 160


class ModuleName(object):
    YAML_CONFIG = 'YAML CONFIG'
    MODEL_CONVERTER = 'Model Converter'
L
liuqi 已提交
161 162
    RUN = 'RUN'
    BENCHMARK = 'Benchmark'
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184


CPP_KEYWORDS = [
    'alignas', 'alignof', 'and', 'and_eq', 'asm', 'atomic_cancel',
    'atomic_commit', 'atomic_noexcept', 'auto', 'bitand', 'bitor',
    'bool', 'break', 'case', 'catch', 'char', 'char16_t', 'char32_t',
    'class', 'compl', 'concept', 'const', 'constexpr', 'const_cast',
    'continue', 'co_await', 'co_return', 'co_yield', 'decltype', 'default',
    'delete', 'do', 'double', 'dynamic_cast', 'else', 'enum', 'explicit',
    'export', 'extern', 'false', 'float', 'for', 'friend', 'goto', 'if',
    'import', 'inline', 'int', 'long', 'module', 'mutable', 'namespace',
    'new', 'noexcept', 'not', 'not_eq', 'nullptr', 'operator', 'or', 'or_eq',
    'private', 'protected', 'public', 'register', 'reinterpret_cast',
    'requires', 'return', 'short', 'signed', 'sizeof', 'static',
    'static_assert', 'static_cast', 'struct', 'switch', 'synchronized',
    'template', 'this', 'thread_local', 'throw', 'true', 'try', 'typedef',
    'typeid', 'typename', 'union', 'unsigned', 'using', 'virtual', 'void',
    'volatile', 'wchar_t', 'while', 'xor', 'xor_eq', 'override', 'final',
    'transaction_safe', 'transaction_safe_dynamic', 'if', 'elif', 'else',
    'endif', 'defined', 'ifdef', 'ifndef', 'define', 'undef', 'include',
    'line', 'error', 'pragma',
]
Y
yejianwu 已提交
185

186

187 188 189
################################
# common functions
################################
190
def parse_device_type(runtime):
Y
yejianwu 已提交
191
    device_type = ""
192

193
    if runtime == RuntimeType.dsp:
194
        device_type = DeviceType.HEXAGON
195
    elif runtime == RuntimeType.gpu:
196
        device_type = DeviceType.GPU
197
    elif runtime == RuntimeType.cpu:
198
        device_type = DeviceType.CPU
199

200
    return device_type
201

Y
yejianwu 已提交
202 203

def get_hexagon_mode(configs):
L
Liangliang He 已提交
204
    runtime_list = []
L
liuqi 已提交
205 206 207 208
    for model_name in configs[YAMLKeyword.models]:
        model_runtime =\
            configs[YAMLKeyword.models][model_name].get(
                YAMLKeyword.runtime, "")
L
Liangliang He 已提交
209 210
        runtime_list.append(model_runtime.lower())

L
liuqi 已提交
211
    if RuntimeType.dsp in runtime_list:
Y
yejianwu 已提交
212 213 214 215
        return True
    return False


216 217 218 219
def md5sum(str):
    md5 = hashlib.md5()
    md5.update(str)
    return md5.hexdigest()
220

Y
yejianwu 已提交
221

222 223 224 225 226 227
def sha256_checksum(fname):
    hash_func = hashlib.sha256()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_func.update(chunk)
    return hash_func.hexdigest()
Y
yejianwu 已提交
228

W
wuchenghui 已提交
229

230 231
def format_model_config(flags):
    with open(flags.config) as f:
232
        configs = yaml.load(f)
W
wuchenghui 已提交
233

234 235
    library_name = configs.get(YAMLKeyword.library_name, "")
    mace_check(len(library_name) > 0,
L
liuqi 已提交
236
               ModuleName.YAML_CONFIG, "library name should not be empty")
237

238 239 240 241
    if flags.target_abis:
        target_abis = flags.target_abis.split(',')
    else:
        target_abis = configs.get(YAMLKeyword.target_abis, [])
242 243
    mace_check((isinstance(target_abis, list) and len(target_abis) > 0),
               ModuleName.YAML_CONFIG, "target_abis list is needed")
244
    configs[YAMLKeyword.target_abis] = target_abis
245 246 247 248 249 250
    for abi in target_abis:
        mace_check(abi in ABITypeStrs,
                   ModuleName.YAML_CONFIG,
                   "target_abis must be in " + str(ABITypeStrs))

    target_socs = configs.get(YAMLKeyword.target_socs, "")
251 252 253 254
    if flags.target_socs:
        configs[YAMLKeyword.target_socs] = \
               [soc.lower() for soc in flags.target_socs.split(',')]
    elif not target_socs:
255 256 257 258
        configs[YAMLKeyword.target_socs] = []
    elif not isinstance(target_socs, list):
        configs[YAMLKeyword.target_socs] = [target_socs]

259 260 261
    configs[YAMLKeyword.target_socs] = \
        [soc.lower() for soc in configs[YAMLKeyword.target_socs]]

L
liuqi 已提交
262 263
    if ABIType.armeabi_v7a in target_abis \
            or ABIType.arm64_v8a in target_abis:
264
        available_socs = sh_commands.adb_get_all_socs()
265 266 267 268 269 270 271
        target_socs = configs[YAMLKeyword.target_socs]
        if ALL_SOC_TAG in target_socs:
            mace_check(available_socs,
                       ModuleName.YAML_CONFIG,
                       "Build for all SOCs plugged in computer, "
                       "you at least plug in one phone")
        else:
272 273 274 275 276 277 278
            for soc in target_socs:
                mace_check(soc in available_socs,
                           ModuleName.YAML_CONFIG,
                           "Build specified SOC library, "
                           "you must plug in a phone using the SOC")

    build_type = BuildType.code
279 280 281 282
    if flags.build_type:
        build_type_str = flags.build_type
    else:
        build_type_str = configs.get(YAMLKeyword.build_type, "")
283 284 285 286 287 288 289 290 291 292
    if build_type_str == BuildType.proto:
        build_type = BuildType.proto
    elif build_type_str == BuildType.code:
        build_type = BuildType.code
    else:
        MaceLogger.error(ModuleName.YAML_CONFIG,
                         "Invalid build type " + build_type_str
                         + ". only support [proto|code] format, "
                         + "proto for converting model to ProtoBuf file, "
                         + "code for converting model to c++ code.")
293
    configs[YAMLKeyword.build_type] = build_type
294 295 296 297 298 299 300 301 302
    embed_model_data = configs.get(YAMLKeyword.embed_model_data, "")
    if embed_model_data == "" or not isinstance(embed_model_data, int) or \
       embed_model_data < 0 or embed_model_data > 1:
        MaceLogger.error(ModuleName.YAML_CONFIG,
                         "embed_model_data must be 0 or 1. "
                         "0 for embed model data to code, 1 not.")
    if build_type == BuildType.proto:
        configs[YAMLKeyword.embed_model_data] = 0

Y
yejianwu 已提交
303 304 305 306 307 308
    linkshared = configs.get(YAMLKeyword.linkshared, "")
    if linkshared == "":
        configs[YAMLKeyword.linkshared] = 0
        linkshared = 0
    if not isinstance(linkshared, int) or linkshared < 0 or \
       linkshared > 1:
Y
yejianwu 已提交
309
        MaceLogger.error(ModuleName.YAML_CONFIG,
Y
yejianwu 已提交
310
                         "linkshared must be 0 or 1. "
Y
yejianwu 已提交
311
                         "default is 0, for link mace lib statically, "
Y
yejianwu 已提交
312 313
                         "1 for dynamic linking.")
    if build_type == BuildType.code and linkshared == 1:
Y
yejianwu 已提交
314
        MaceLogger.error(ModuleName.YAML_CONFIG,
Y
yejianwu 已提交
315
                         "'linkshared == 1' only support when "
Y
yejianwu 已提交
316 317
                         "'build_type == proto'")

318 319 320 321
    model_names = configs.get(YAMLKeyword.models, [])
    mace_check(len(model_names) > 0, ModuleName.YAML_CONFIG,
               "no model found in config file")

L
liuqi 已提交
322
    model_name_reg = re.compile(r'^[a-zA-Z0-9_]+$')
323 324 325 326 327 328 329 330
    for model_name in model_names:
        # check model_name legality
        mace_check(model_name not in CPP_KEYWORDS,
                   ModuleName.YAML_CONFIG,
                   "model name should not be c++ keyword.")
        mace_check((model_name[0] == '_' or model_name[0].isalpha())
                   and bool(model_name_reg.match(model_name)),
                   ModuleName.YAML_CONFIG,
L
liuqi 已提交
331
                   "model name should Meet the c++ naming convention"
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
                   " which start with '_' or alpha"
                   " and only contain alpha, number and '_'")

        model_config = configs[YAMLKeyword.models][model_name]
        platform = model_config.get(YAMLKeyword.platform, "")
        mace_check(platform in PlatformTypeStrs,
                   ModuleName.YAML_CONFIG,
                   "'platform' must be in " + str(PlatformTypeStrs))

        for key in [YAMLKeyword.model_file_path,
                    YAMLKeyword.model_sha256_checksum]:
            value = model_config.get(key, "")
            mace_check(value != "", ModuleName.YAML_CONFIG,
                       "'%s' is necessary" % key)

        weight_file_path = model_config.get(YAMLKeyword.weight_file_path, "")
        if weight_file_path:
            weight_checksum =\
                model_config.get(YAMLKeyword.weight_sha256_checksum, "")
            mace_check(weight_checksum != "", ModuleName.YAML_CONFIG,
                       "'%s' is necessary" %
                       YAMLKeyword.weight_sha256_checksum)
        else:
            model_config[YAMLKeyword.weight_sha256_checksum] = ""

        runtime = model_config.get(YAMLKeyword.runtime, "")
        mace_check(runtime in RuntimeTypeStrs,
                   ModuleName.YAML_CONFIG,
                   "'runtime' must be in " + str(RuntimeTypeStrs))
        if ABIType.host in target_abis:
            mace_check(runtime == RuntimeType.cpu,
                       ModuleName.YAML_CONFIG,
                       "host only support cpu runtime now.")

        data_type = model_config.get(YAMLKeyword.data_type, "")
        if runtime == RuntimeType.cpu_gpu and data_type not in GPUDataTypeStrs:
            model_config[YAMLKeyword.data_type] = \
                GPUDataType.fp16_fp32.value
        elif runtime == RuntimeType.cpu:
            if len(data_type) > 0:
                mace_check(data_type in CPUDataTypeStrs,
                           ModuleName.YAML_CONFIG,
                           "'data_type' must be in " + str(CPUDataTypeStrs)
                           + " for cpu runtime")
            else:
                model_config[YAMLKeyword.data_type] = \
                    CPUDataType.fp32.value
        elif runtime == RuntimeType.gpu:
            if len(data_type) > 0:
                mace_check(data_type in GPUDataTypeStrs,
                           ModuleName.YAML_CONFIG,
                           "'data_type' must be in " + str(GPUDataTypeStrs)
                           + " for gpu runtime")
            else:
                model_config[YAMLKeyword.data_type] =\
                    GPUDataType.fp16_fp32.value
L
liuqi 已提交
388 389 390 391 392 393 394 395 396
        elif runtime == RuntimeType.dsp:
            if len(data_type) > 0:
                mace_check(data_type in DSPDataTypeStrs,
                           ModuleName.YAML_CONFIG,
                           "'data_type' must be in " + str(DSPDataTypeStrs)
                           + " for dsp runtime")
            else:
                model_config[YAMLKeyword.data_type] = \
                    DSPDataType.uint8.value
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

        subgraphs = model_config.get(YAMLKeyword.subgraphs, "")
        mace_check(len(subgraphs) > 0, ModuleName.YAML_CONFIG,
                   "at least one subgraph is needed")

        for subgraph in subgraphs:
            for key in [YAMLKeyword.input_tensors,
                        YAMLKeyword.input_shapes,
                        YAMLKeyword.output_tensors,
                        YAMLKeyword.output_shapes]:
                value = subgraph.get(key, "")
                mace_check(value != "", ModuleName.YAML_CONFIG,
                           "'%s' is necessary in subgraph" % key)
                if not isinstance(value, list):
                    subgraph[key] = [value]
L
liuqi 已提交
412 413 414 415 416 417 418 419
            validation_inputs_data = subgraph.get(
                YAMLKeyword.validation_inputs_data, [])
            if not isinstance(validation_inputs_data, list):
                subgraph[YAMLKeyword.validation_inputs_data] = [
                    validation_inputs_data]
            else:
                subgraph[YAMLKeyword.validation_inputs_data] = \
                    validation_inputs_data
420 421 422 423 424 425
            input_ranges = subgraph.get(
                YAMLKeyword.input_ranges, [])
            if not isinstance(input_ranges, list):
                subgraph[YAMLKeyword.input_ranges] = [input_ranges]
            else:
                subgraph[YAMLKeyword.input_ranges] = input_ranges
W
wuchenghui 已提交
426

427 428 429 430 431 432 433
        for key in [YAMLKeyword.limit_opencl_kernel_time,
                    YAMLKeyword.nnlib_graph_mode,
                    YAMLKeyword.obfuscate,
                    YAMLKeyword.winograd]:
            value = model_config.get(key, "")
            if value == "":
                model_config[key] = 0
L
Liangliang He 已提交
434

435 436 437 438 439 440
        mace_check(model_config[YAMLKeyword.winograd] in WinogradParameters,
                   ModuleName.YAML_CONFIG,
                   "'winograd' parameters must be in "
                   + str(WinogradParameters) +
                   ". 0 for disable winograd convolution")

L
liuqi 已提交
441 442
        weight_file_path = model_config.get(YAMLKeyword.weight_file_path, "")
        model_config[YAMLKeyword.weight_file_path] = weight_file_path
Y
yejianwu 已提交
443

444
    return configs
Y
yejianwu 已提交
445

W
wuchenghui 已提交
446

447 448 449 450
def get_build_binary_dir(library_name, target_abi, target_soc,
                         serial_num):
    if not target_soc or not serial_num:
        binary_path_digest = md5sum(target_abi)
L
liuqi 已提交
451
        binary_path_digest = "%s_%s" % (target_abi, binary_path_digest)
Y
yejianwu 已提交
452
    else:
L
liuqi 已提交
453
        device_name = sh_commands.adb_get_device_name_by_serialno(serial_num)
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        binary_path_digest = md5sum(target_abi + target_soc + serial_num)
        binary_path_digest = "%s_%s_%s" % \
                             (device_name, target_soc, binary_path_digest)
    return "%s/%s/%s/%s" % (
        BUILD_OUTPUT_DIR, library_name, BUILD_TMP_DIR_NAME, binary_path_digest)


def get_build_model_dirs(library_name, model_name, target_abi, target_soc,
                         serial_num, model_file_path):
    model_path_digest = md5sum(model_file_path)
    model_output_base_dir = "%s/%s/%s/%s/%s" % (
        BUILD_OUTPUT_DIR, library_name, BUILD_TMP_DIR_NAME,
        model_name, model_path_digest)

    if target_abi == ABIType.host:
        model_output_dir = "%s/%s" % (model_output_base_dir, target_abi)
    elif not target_soc or not serial_num:
        model_output_dir = "%s/%s/%s" % (
            model_output_base_dir, BUILD_TMP_GENERAL_OUTPUT_DIR_NAME,
            target_abi)
    else:
        device_name = \
            sh_commands.adb_get_device_name_by_serialno(serial_num)
        model_output_dir = "%s/%s_%s/%s" % (
L
liuqi 已提交
478
            model_output_base_dir, device_name,
479
            target_soc, target_abi)
Y
yejianwu 已提交
480

481 482 483 484 485 486
    mace_model_dir = \
        '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_OUTPUT_DIR_NAME)

    return model_output_base_dir, model_output_dir, mace_model_dir


L
liuqi 已提交
487 488 489 490 491
def get_opencl_binary_output_path(library_name, target_abi,
                                  target_soc, serial_num):
    device_name = \
        sh_commands.adb_get_device_name_by_serialno(serial_num)
    return '%s/%s/%s/%s/%s_%s.%s.%s.bin' % \
492 493 494
           (BUILD_OUTPUT_DIR,
            library_name,
            OUTPUT_OPENCL_BINARY_DIR_NAME,
L
liuqi 已提交
495 496 497 498 499
            target_abi,
            library_name,
            OUTPUT_OPENCL_BINARY_FILE_NAME,
            device_name,
            target_soc)
500 501


Y
yejianwu 已提交
502 503 504 505 506 507 508
def get_shared_library_dir(library_name, abi):
    return '%s/%s/%s/%s' % (BUILD_OUTPUT_DIR,
                            library_name,
                            OUTPUT_LIBRARY_DIR_NAME,
                            abi)


509 510 511 512 513 514 515
################################
# build
################################
def pull_opencl_binary_and_tuning_param(target_abi,
                                        serialno,
                                        model_output_dirs):
    sh_commands.pull_binaries(target_abi, serialno, model_output_dirs,
516
                              CL_COMPILED_BINARY_FILE_NAME)
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532


def print_configuration(flags, configs):
    title = "Common Configuration"
    header = ["key", "value"]
    data = list()
    data.append([YAMLKeyword.library_name,
                 configs[YAMLKeyword.library_name]])
    data.append([YAMLKeyword.target_abis,
                 configs[YAMLKeyword.target_abis]])
    data.append([YAMLKeyword.target_socs,
                 configs[YAMLKeyword.target_socs]])
    data.append([YAMLKeyword.build_type,
                 configs[YAMLKeyword.build_type]])
    data.append([YAMLKeyword.embed_model_data,
                 configs[YAMLKeyword.embed_model_data]])
Y
yejianwu 已提交
533 534
    data.append([YAMLKeyword.linkshared,
                 configs[YAMLKeyword.linkshared]])
535
    data.append(["Tuning", flags.disable_tuning])
536
    MaceLogger.summary(StringFormatter.table(header, data, title))
L
Liangliang He 已提交
537

Y
yejianwu 已提交
538

539 540 541
def download_model_files(model_file_path,
                         model_output_dir,
                         weight_file_path=""):
542
    MaceLogger.info("Downloading model, please wait ...")
L
Liangliang He 已提交
543 544
    if model_file_path.startswith("http://") or \
            model_file_path.startswith("https://"):
Y
yejianwu 已提交
545 546
        model_file = model_output_dir + "/model.pb"
        urllib.urlretrieve(model_file_path, model_file)
547 548 549 550 551

    if weight_file_path.startswith("http://") or \
            weight_file_path.startswith("https://"):
        weight_file = model_output_dir + "/model.caffemodel"
        urllib.urlretrieve(weight_file_path, weight_file)
552
    MaceLogger.info("Model downloaded successfully.")
553 554 555 556 557 558 559 560


def get_model_files_path(model_file_path,
                         model_output_dir,
                         weight_file_path=""):
    if model_file_path.startswith("http://") or \
            model_file_path.startswith("https://"):
        model_file = model_output_dir + "/model.pb"
Y
yejianwu 已提交
561 562
    else:
        model_file = model_file_path
L
Liangliang He 已提交
563 564 565

    if weight_file_path.startswith("http://") or \
            weight_file_path.startswith("https://"):
Y
yejianwu 已提交
566
        weight_file = model_output_dir + "/model.caffemodel"
Y
yejianwu 已提交
567 568
    else:
        weight_file = weight_file_path
Y
yejianwu 已提交
569 570

    return model_file, weight_file
L
Liangliang He 已提交
571

L
liuqi 已提交
572

573 574 575 576 577
def convert_model(configs):
    # Remove previous output dirs
    library_name = configs[YAMLKeyword.library_name]
    if not os.path.exists(BUILD_OUTPUT_DIR):
        os.makedirs(BUILD_OUTPUT_DIR)
L
liuqi 已提交
578 579 580
    elif os.path.exists(os.path.join(BUILD_OUTPUT_DIR, library_name)):
        sh.rm("-rf", os.path.join(BUILD_OUTPUT_DIR, library_name))
    os.makedirs(os.path.join(BUILD_OUTPUT_DIR, library_name))
581 582 583

    model_output_dir = \
        '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_OUTPUT_DIR_NAME)
L
liuqi 已提交
584 585
    model_header_dir = \
        '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_HEADER_DIR_PATH)
586 587 588
    if os.path.exists(model_output_dir):
        sh.rm("-rf", model_output_dir)
    os.makedirs(model_output_dir)
L
liuqi 已提交
589 590 591 592 593
    if os.path.exists(model_header_dir):
        sh.rm("-rf", model_header_dir)
    os.makedirs(model_header_dir)
    # copy header files
    sh.cp("-f", glob.glob("mace/public/*.h"), model_header_dir)
594

L
liuqi 已提交
595 596
    sh_commands.gen_mace_engine_factory_source(
        configs[YAMLKeyword.models].keys(),
B
Bin Li 已提交
597 598
        configs[YAMLKeyword.build_type],
        configs[YAMLKeyword.embed_model_data])
L
liuqi 已提交
599 600 601 602
    if configs[YAMLKeyword.build_type] == BuildType.code:
        sh.cp("-f", glob.glob("mace/codegen/engine/*.h"),
              model_header_dir)

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    embed_model_data = configs[YAMLKeyword.embed_model_data]

    sh_commands.clear_model_codegen()
    for model_name in configs[YAMLKeyword.models]:
        MaceLogger.header(
            StringFormatter.block("Convert %s model" % model_name))
        model_config = configs[YAMLKeyword.models][model_name]
        runtime = model_config[YAMLKeyword.runtime]

        # Create model build directory
        model_path_digest = md5sum(
            model_config[YAMLKeyword.model_file_path])

        model_output_base_dir = "%s/%s/%s/%s/%s" % (
            BUILD_OUTPUT_DIR, library_name, BUILD_TMP_DIR_NAME,
            model_name, model_path_digest)

        if os.path.exists(model_output_base_dir):
            sh.rm("-rf", model_output_base_dir)
        os.makedirs(model_output_base_dir)

        download_model_files(
            model_config[YAMLKeyword.model_file_path],
            model_output_base_dir,
            model_config[YAMLKeyword.weight_file_path])

        model_file_path, weight_file_path = get_model_files_path(
            model_config[YAMLKeyword.model_file_path],
            model_output_base_dir,
            model_config[YAMLKeyword.weight_file_path])

        if sha256_checksum(model_file_path) != \
                model_config[YAMLKeyword.model_sha256_checksum]:
            MaceLogger.error(ModuleName.MODEL_CONVERTER,
                             "model file sha256checksum not match")

        if weight_file_path:
            if sha256_checksum(weight_file_path) != \
                    model_config[YAMLKeyword.weight_sha256_checksum]:
                MaceLogger.error(ModuleName.MODEL_CONVERTER,
                                 "weight file sha256checksum not match")

        data_type = model_config[YAMLKeyword.data_type]
        # TODO(liuqi): support multiple subgraphs
        subgraphs = model_config[YAMLKeyword.subgraphs]

L
liuqi 已提交
649
        model_codegen_dir = "%s/%s" % (MODEL_CODEGEN_DIR, model_name)
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
        sh_commands.gen_model_code(
            model_codegen_dir,
            model_config[YAMLKeyword.platform],
            model_file_path,
            weight_file_path,
            model_config[YAMLKeyword.model_sha256_checksum],
            model_config[YAMLKeyword.weight_sha256_checksum],
            ",".join(subgraphs[0][YAMLKeyword.input_tensors]),
            ",".join(subgraphs[0][YAMLKeyword.output_tensors]),
            runtime,
            model_name,
            ":".join(subgraphs[0][YAMLKeyword.input_shapes]),
            model_config[YAMLKeyword.nnlib_graph_mode],
            embed_model_data,
            model_config[YAMLKeyword.winograd],
            model_config[YAMLKeyword.obfuscate],
            configs[YAMLKeyword.build_type],
李寅 已提交
667
            data_type,
李寅 已提交
668
            ",".join(model_config.get(YAMLKeyword.graph_optimize_options, [])))
669

L
liuqi 已提交
670 671 672 673
        if configs[YAMLKeyword.build_type] == BuildType.proto:
            sh.mv("-f",
                  '%s/%s.pb' % (model_codegen_dir, model_name),
                  model_output_dir)
L
liuqi 已提交
674 675 676
            sh.mv("-f",
                  '%s/%s.data' % (model_codegen_dir, model_name),
                  model_output_dir)
L
liuqi 已提交
677 678
        else:
            if not embed_model_data:
L
liuqi 已提交
679
                sh.mv("-f",
L
liuqi 已提交
680
                      '%s/%s.data' % (model_codegen_dir, model_name),
L
liuqi 已提交
681
                      model_output_dir)
L
liuqi 已提交
682 683
            sh.cp("-f", glob.glob("mace/codegen/models/*/*.h"),
                  model_header_dir)
684

L
liuqi 已提交
685
        MaceLogger.summary(
686 687 688 689 690 691 692 693 694
            StringFormatter.block("Model %s converted" % model_name))


def build_specific_lib(target_abi, target_soc, serial_num,
                       configs, tuning, enable_openmp,
                       address_sanitizer):
    library_name = configs[YAMLKeyword.library_name]
    build_type = configs[YAMLKeyword.build_type]
    embed_model_data = configs[YAMLKeyword.embed_model_data]
Y
yejianwu 已提交
695
    linkshared = configs[YAMLKeyword.linkshared]
696 697 698 699 700 701 702 703 704
    hexagon_mode = get_hexagon_mode(configs)
    model_output_dirs = []

    build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi,
                                                target_soc, serial_num)
    if os.path.exists(build_tmp_binary_dir):
        sh.rm("-rf", build_tmp_binary_dir)
    os.makedirs(build_tmp_binary_dir)

705
    sh_commands.gen_tuning_param_code(model_output_dirs)
Y
yejianwu 已提交
706
    if linkshared == 0:
L
liuqi 已提交
707
        mace_run_name = MACE_RUN_STATIC_NAME
Y
yejianwu 已提交
708
        mace_run_target = MACE_RUN_STATIC_TARGET
Y
yejianwu 已提交
709
    else:
L
liuqi 已提交
710
        mace_run_name = MACE_RUN_SHARED_NAME
Y
yejianwu 已提交
711
        mace_run_target = MACE_RUN_SHARED_TARGET
Y
yejianwu 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724
        sh_commands.bazel_build(
            LIBMACE_SO_TARGET,
            abi=target_abi,
            hexagon_mode=hexagon_mode,
            enable_openmp=enable_openmp,
            address_sanitizer=address_sanitizer
        )
        sh_commands.update_libmace_shared_library(serial_num,
                                                  target_abi,
                                                  library_name,
                                                  BUILD_OUTPUT_DIR,
                                                  OUTPUT_LIBRARY_DIR_NAME)

725
    sh_commands.bazel_build(
Y
yejianwu 已提交
726
        mace_run_target,
727 728 729 730 731
        abi=target_abi,
        hexagon_mode=hexagon_mode,
        enable_openmp=enable_openmp,
        address_sanitizer=address_sanitizer
    )
Y
yejianwu 已提交
732
    sh_commands.update_mace_run_lib(build_tmp_binary_dir, linkshared)
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    binary_changed = False

    for model_name in configs[YAMLKeyword.models]:
        model_config = configs[YAMLKeyword.models][model_name]
        model_runtime = model_config[YAMLKeyword.runtime]
        # Create model build directory
        model_output_base_dir, model_output_dir, mace_model_dir = \
            get_build_model_dirs(library_name, model_name, target_abi,
                                 target_soc, serial_num,
                                 model_config[YAMLKeyword.model_file_path])

        model_output_dirs.append(model_output_dir)

        if os.path.exists(model_output_dir):
            sh.rm("-rf", model_output_dir)
        os.makedirs(model_output_dir)

        # build for specified soc
751
        if not address_sanitizer and target_abi != ABIType.host \
752 753 754 755 756 757 758 759 760 761
                and target_soc is not None and \
                model_runtime in [RuntimeType.gpu, RuntimeType.cpu_gpu]:
            sh_commands.clear_phone_data_dir(serial_num, PHONE_DATA_DIR)

            subgraphs = model_config[YAMLKeyword.subgraphs]
            # generate input data
            sh_commands.gen_random_input(
                model_output_dir,
                subgraphs[0][YAMLKeyword.input_tensors],
                subgraphs[0][YAMLKeyword.input_shapes],
李寅 已提交
762
                subgraphs[0][YAMLKeyword.validation_inputs_data],
763
                input_ranges=subgraphs[0][YAMLKeyword.input_ranges])
764 765 766 767 768

            device_type = parse_device_type(RuntimeType.gpu)
            sh_commands.tuning_run(
                abi=target_abi,
                serialno=serial_num,
L
liuqi 已提交
769 770
                target_dir=build_tmp_binary_dir,
                target_name=mace_run_name,
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
                vlog_level=0,
                embed_model_data=embed_model_data,
                model_output_dir=model_output_dir,
                input_nodes=subgraphs[0][YAMLKeyword.input_tensors],
                output_nodes=subgraphs[0][YAMLKeyword.output_tensors],
                input_shapes=subgraphs[0][YAMLKeyword.input_shapes],
                output_shapes=subgraphs[0][YAMLKeyword.output_shapes],
                mace_model_dir=mace_model_dir,
                model_tag=model_name,
                device_type=device_type,
                running_round=0,
                restart_round=1,
                limit_opencl_kernel_time=model_config[YAMLKeyword.limit_opencl_kernel_time],  # noqa
                tuning=tuning,
                out_of_range_check=False,
                phone_data_dir=PHONE_DATA_DIR,
787 788
                build_type=build_type,
                opencl_binary_file="",
Y
yejianwu 已提交
789
                shared_library_dir=get_shared_library_dir(library_name, target_abi),  # noqa
Y
yejianwu 已提交
790
                linkshared=linkshared,
791 792 793 794
            )

            pull_opencl_binary_and_tuning_param(target_abi, serial_num,
                                                [model_output_dir])
795
            sh_commands.touch_tuned_file_flag(build_tmp_binary_dir)
796 797 798
            binary_changed = True

    if binary_changed:
L
liuqi 已提交
799 800 801
        opencl_output_bin_path = get_opencl_binary_output_path(
            library_name, target_abi, target_soc, serial_num
        )
802 803
        sh_commands.merge_opencl_binaries(
            model_output_dirs, CL_COMPILED_BINARY_FILE_NAME,
L
liuqi 已提交
804
            opencl_output_bin_path)
805
        sh_commands.gen_tuning_param_code(model_output_dirs)
806
        sh_commands.bazel_build(
Y
yejianwu 已提交
807
            mace_run_target,
808 809 810 811 812
            abi=target_abi,
            hexagon_mode=hexagon_mode,
            enable_openmp=enable_openmp,
            address_sanitizer=address_sanitizer
        )
Y
yejianwu 已提交
813
        sh_commands.update_mace_run_lib(build_tmp_binary_dir, linkshared)
814 815 816 817 818 819 820

    if target_abi == ABIType.host:
        sh_commands.build_host_libraries(build_type, target_abi)

    # build benchmark_model binary
    sh_commands.build_benchmark_model(target_abi,
                                      build_tmp_binary_dir,
Y
yejianwu 已提交
821
                                      hexagon_mode,
L
liuqi 已提交
822
                                      enable_openmp,
Y
yejianwu 已提交
823
                                      linkshared)
824 825

    # generate library
Y
yejianwu 已提交
826
    if linkshared == 0:
Y
yejianwu 已提交
827 828 829 830 831 832 833 834
        sh_commands.merge_libs(target_soc,
                               serial_num,
                               target_abi,
                               library_name,
                               BUILD_OUTPUT_DIR,
                               OUTPUT_LIBRARY_DIR_NAME,
                               build_type,
                               hexagon_mode)
835

L
liuqi 已提交
836 837 838 839 840 841 842
    # build example binary
    sh_commands.build_example(target_soc, serial_num, target_abi,
                              library_name, BUILD_OUTPUT_DIR,
                              OUTPUT_LIBRARY_DIR_NAME,
                              build_tmp_binary_dir, build_type,
                              hexagon_mode, enable_openmp, linkshared)

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866

def generate_library(configs, tuning, enable_openmp, address_sanitizer):
    MaceLogger.header(StringFormatter.block("Building library"))
    # generate source
    MaceLogger.info('* generate common source files...')
    sh_commands.gen_mace_version()
    sh_commands.gen_encrypted_opencl_source()
    MaceLogger.info('generate common source files done')

    # create build dirs
    library_name = configs[YAMLKeyword.library_name]
    if not os.path.exists(BUILD_OUTPUT_DIR):
        os.makedirs(BUILD_OUTPUT_DIR)
    tmp_build_dir = os.path.join(BUILD_OUTPUT_DIR, library_name,
                                 BUILD_TMP_DIR_NAME)
    if not os.path.exists(tmp_build_dir):
        os.makedirs(tmp_build_dir)
    library_out_dir = os.path.join(BUILD_OUTPUT_DIR, library_name,
                                   OUTPUT_LIBRARY_DIR_NAME)
    if os.path.exists(library_out_dir):
        sh.rm('-rf', library_out_dir)

    target_socs = configs[YAMLKeyword.target_socs]
    for target_abi in configs[YAMLKeyword.target_abis]:
L
liuqi 已提交
867
        if not target_socs or target_abi == ABIType.host:
868 869 870
            build_specific_lib(target_abi, None, None, configs,
                               tuning, enable_openmp, address_sanitizer)
        else:
871 872
            if ALL_SOC_TAG in target_socs:
                target_socs = sh_commands.adb_get_all_socs()
873
            for target_soc in target_socs:
L
liuqi 已提交
874 875 876 877 878 879 880
                serial_nums = \
                    sh_commands.get_target_socs_serialnos([target_soc])
                for serial_num in serial_nums:
                    with sh_commands.device_lock(serial_num):
                        build_specific_lib(target_abi, target_soc, serial_num,
                                           configs, tuning, enable_openmp,
                                           address_sanitizer)
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

    # package library
    sh_commands.packaging_lib(BUILD_OUTPUT_DIR,
                              configs[YAMLKeyword.library_name])


def print_library_summary(configs):
    library_name = configs[YAMLKeyword.library_name]
    title = "Library"
    header = ["key", "value"]
    data = list()
    data.append(["library package",
                 "%s/%s/libmace_%s.tar.gz"
                 % (BUILD_OUTPUT_DIR, library_name, library_name)])
    MaceLogger.summary(StringFormatter.table(header, data, title))


def build_library(flags):
899
    configs = format_model_config(flags)
900 901 902 903 904

    print_configuration(flags, configs)

    convert_model(configs)

905 906
    generate_library(configs, flags.disable_tuning,
                     flags.disable_openmp, flags.address_sanitizer)
907 908 909 910 911 912 913 914 915 916 917 918

    print_library_summary(configs)


################################
# run
################################
def report_run_statistics(stdout,
                          abi,
                          serialno,
                          model_name,
                          device_type,
919 920
                          output_dir,
                          tuned):
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    metrics = [0] * 3
    for line in stdout.split('\n'):
        line = line.strip()
        parts = line.split()
        if len(parts) == 4 and parts[0].startswith("time"):
            metrics[0] = str(float(parts[1]))
            metrics[1] = str(float(parts[2]))
            metrics[2] = str(float(parts[3]))
            break

    device_name = ""
    target_soc = ""
    if abi != "host":
        props = sh_commands.adb_getprop_by_serialno(serialno)
        device_name = props.get("ro.product.model", "")
        target_soc = props.get("ro.board.platform", "")

    report_filename = output_dir + "/report.csv"
    if not os.path.exists(report_filename):
        with open(report_filename, 'w') as f:
            f.write("model_name,device_name,soc,abi,runtime,"
942
                    "init,warmup,run_avg,tuned\n")
943 944

    data_str = "{model_name},{device_name},{soc},{abi},{device_type}," \
945
               "{init},{warmup},{run_avg},{tuned}\n" \
946 947 948 949 950 951 952
        .format(model_name=model_name,
                device_name=device_name,
                soc=target_soc,
                abi=abi,
                device_type=device_type,
                init=metrics[0],
                warmup=metrics[1],
953 954
                run_avg=metrics[2],
                tuned=tuned,
955 956 957 958 959 960 961 962 963 964
                )
    with open(report_filename, 'a') as f:
        f.write(data_str)


def run_specific_target(flags, configs, target_abi,
                        target_soc, serial_num):
    library_name = configs[YAMLKeyword.library_name]
    build_type = configs[YAMLKeyword.build_type]
    embed_model_data = configs[YAMLKeyword.embed_model_data]
L
liuqi 已提交
965
    opencl_output_bin_path = ""
Y
yejianwu 已提交
966
    linkshared = configs[YAMLKeyword.linkshared]
L
liuqi 已提交
967
    if not configs[YAMLKeyword.target_socs] or target_abi == ABIType.host:
968 969 970 971 972
        build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi,
                                                    None, None)
    else:
        build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi,
                                                    target_soc, serial_num)
L
liuqi 已提交
973 974 975
        opencl_output_bin_path = get_opencl_binary_output_path(
            library_name, target_abi, target_soc, serial_num
        )
L
liuqi 已提交
976 977 978
    mace_check(os.path.exists(build_tmp_binary_dir),
               ModuleName.RUN,
               'You should build before run.')
979

L
liuqi 已提交
980 981 982 983 984 985 986 987 988 989 990
    if flags.example:
        if linkshared == 0:
            target_name = EXAMPLE_STATIC_NAME
        else:
            target_name = EXAMPLE_SHARED_NAME
    else:
        if linkshared == 0:
            target_name = MACE_RUN_STATIC_NAME
        else:
            target_name = MACE_RUN_SHARED_NAME

991
    for model_name in configs[YAMLKeyword.models]:
L
liuqi 已提交
992 993 994 995 996 997 998 999
        if target_abi == ABIType.host:
            device_name = ABIType.host
        else:
            device_name =\
                sh_commands.adb_get_device_name_by_serialno(serial_num)
        MaceLogger.header(
            StringFormatter.block(
                "Run model %s on %s" % (model_name, device_name)))
1000 1001 1002 1003
        model_config = configs[YAMLKeyword.models][model_name]
        model_runtime = model_config[YAMLKeyword.runtime]
        subgraphs = model_config[YAMLKeyword.subgraphs]

L
liuqi 已提交
1004
        if not configs[YAMLKeyword.target_socs] or target_abi == ABIType.host:
1005 1006 1007 1008 1009 1010 1011 1012 1013
            model_output_base_dir, model_output_dir, mace_model_dir = \
                get_build_model_dirs(library_name, model_name, target_abi,
                                     None, None,
                                     model_config[YAMLKeyword.model_file_path])
        else:
            model_output_base_dir, model_output_dir, mace_model_dir = \
                get_build_model_dirs(library_name, model_name, target_abi,
                                     target_soc, serial_num,
                                     model_config[YAMLKeyword.model_file_path])
L
liuqi 已提交
1014 1015 1016 1017
        mace_check(os.path.exists(model_output_dir)
                   and os.path.exists(mace_model_dir),
                   ModuleName.RUN,
                   'You should build before run.')
1018 1019 1020 1021 1022 1023 1024 1025
        if target_abi != ABIType.host:
            sh_commands.clear_phone_data_dir(serial_num, PHONE_DATA_DIR)

        # generate input data
        sh_commands.gen_random_input(
            model_output_dir,
            subgraphs[0][YAMLKeyword.input_tensors],
            subgraphs[0][YAMLKeyword.input_shapes],
李寅 已提交
1026
            subgraphs[0][YAMLKeyword.validation_inputs_data],
1027
            input_ranges=subgraphs[0][YAMLKeyword.input_ranges])
1028 1029 1030 1031 1032 1033 1034 1035 1036
        runtime_list = []
        if target_abi == ABIType.host:
            runtime_list.extend([RuntimeType.cpu])
        elif model_runtime == RuntimeType.cpu_gpu:
            runtime_list.extend([RuntimeType.cpu, RuntimeType.gpu])
        else:
            runtime_list.extend([model_runtime])
        for runtime in runtime_list:
            device_type = parse_device_type(runtime)
L
liuqi 已提交
1037

1038 1039 1040
            run_output = sh_commands.tuning_run(
                abi=target_abi,
                serialno=serial_num,
L
liuqi 已提交
1041 1042
                target_dir=build_tmp_binary_dir,
                target_name=target_name,
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
                vlog_level=flags.vlog_level,
                embed_model_data=embed_model_data,
                model_output_dir=model_output_dir,
                input_nodes=subgraphs[0][YAMLKeyword.input_tensors],
                output_nodes=subgraphs[0][YAMLKeyword.output_tensors],
                input_shapes=subgraphs[0][YAMLKeyword.input_shapes],
                output_shapes=subgraphs[0][YAMLKeyword.output_shapes],
                mace_model_dir=mace_model_dir,
                model_tag=model_name,
                device_type=device_type,
                running_round=flags.round,
                restart_round=flags.restart_round,
                limit_opencl_kernel_time=model_config[YAMLKeyword.limit_opencl_kernel_time],  # noqa
                tuning=False,
L
Liangliang He 已提交
1057
                out_of_range_check=flags.gpu_out_of_range_check,
1058 1059 1060 1061 1062 1063 1064 1065
                phone_data_dir=PHONE_DATA_DIR,
                build_type=build_type,
                omp_num_threads=flags.omp_num_threads,
                cpu_affinity_policy=flags.cpu_affinity_policy,
                gpu_perf_hint=flags.gpu_perf_hint,
                gpu_priority_hint=flags.gpu_priority_hint,
                runtime_failure_ratio=flags.runtime_failure_ratio,
                address_sanitizer=flags.address_sanitizer,
L
liuqi 已提交
1066
                opencl_binary_file=opencl_output_bin_path,
Y
yejianwu 已提交
1067
                shared_library_dir=get_shared_library_dir(library_name, target_abi),  # noqa
Y
yejianwu 已提交
1068
                linkshared=linkshared,
1069 1070 1071
            )
            if flags.validate:
                model_file_path, weight_file_path = get_model_files_path(
L
liuqi 已提交
1072
                    model_config[YAMLKeyword.model_file_path],
1073
                    model_output_base_dir,
L
liuqi 已提交
1074
                    model_config[YAMLKeyword.weight_file_path])
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

                sh_commands.validate_model(
                    abi=target_abi,
                    serialno=serial_num,
                    model_file_path=model_file_path,
                    weight_file_path=weight_file_path,
                    platform=model_config[YAMLKeyword.platform],
                    device_type=device_type,
                    input_nodes=subgraphs[0][YAMLKeyword.input_tensors],
                    output_nodes=subgraphs[0][YAMLKeyword.output_tensors],
                    input_shapes=subgraphs[0][YAMLKeyword.input_shapes],
                    output_shapes=subgraphs[0][YAMLKeyword.output_shapes],
                    model_output_dir=model_output_dir,
                    phone_data_dir=PHONE_DATA_DIR,
                    caffe_env=flags.caffe_env)
            if flags.report and flags.round > 0:
                report_run_statistics(
                    run_output, target_abi, serial_num,
1093 1094
                    model_name, device_type, flags.report_dir,
                    sh_commands.is_binary_tuned(build_tmp_binary_dir))
1095 1096 1097


def run_mace(flags):
1098
    configs = format_model_config(flags)
1099 1100

    target_socs = configs[YAMLKeyword.target_socs]
1101
    if not target_socs or ALL_SOC_TAG in target_socs:
1102 1103 1104 1105 1106 1107 1108
        target_socs = sh_commands.adb_get_all_socs()

    for target_abi in configs[YAMLKeyword.target_abis]:
        if target_abi == ABIType.host:
            run_specific_target(flags, configs, target_abi, None, None)
        else:
            for target_soc in target_socs:
L
liuqi 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117
                serial_nums = \
                    sh_commands.get_target_socs_serialnos([target_soc])
                mace_check(serial_nums,
                           ModuleName.RUN,
                           'There is no device with soc: ' + target_soc)
                for serial_num in serial_nums:
                    with sh_commands.device_lock(serial_num):
                        run_specific_target(flags, configs, target_abi,
                                            target_soc, serial_num)
1118 1119 1120 1121 1122 1123 1124 1125 1126


################################
#  benchmark model
################################
def bm_specific_target(flags, configs, target_abi, target_soc, serial_num):
    library_name = configs[YAMLKeyword.library_name]
    build_type = configs[YAMLKeyword.build_type]
    embed_model_data = configs[YAMLKeyword.embed_model_data]
L
liuqi 已提交
1127
    opencl_output_bin_path = ""
Y
yejianwu 已提交
1128
    linkshared = configs[YAMLKeyword.linkshared]
L
liuqi 已提交
1129
    if not configs[YAMLKeyword.target_socs] or target_abi == ABIType.host:
1130 1131 1132 1133 1134
        build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi,
                                                    None, None)
    else:
        build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi,
                                                    target_soc, serial_num)
L
liuqi 已提交
1135 1136 1137
        opencl_output_bin_path = get_opencl_binary_output_path(
            library_name, target_abi, target_soc, serial_num
        )
L
liuqi 已提交
1138 1139 1140
    mace_check(os.path.exists(build_tmp_binary_dir),
               ModuleName.BENCHMARK,
               'You should build before benchmark.')
1141 1142

    for model_name in configs[YAMLKeyword.models]:
L
liuqi 已提交
1143 1144 1145 1146 1147 1148 1149 1150
        if target_abi == ABIType.host:
            device_name = ABIType.host
        else:
            device_name = \
                sh_commands.adb_get_device_name_by_serialno(serial_num)
        MaceLogger.header(
            StringFormatter.block(
                "Benchmark model %s on %s" % (model_name, device_name)))
1151 1152 1153 1154
        model_config = configs[YAMLKeyword.models][model_name]
        model_runtime = model_config[YAMLKeyword.runtime]
        subgraphs = model_config[YAMLKeyword.subgraphs]

L
liuqi 已提交
1155
        if not configs[YAMLKeyword.target_socs] or target_abi == ABIType.host:
1156 1157 1158 1159 1160 1161 1162 1163 1164
            model_output_base_dir, model_output_dir, mace_model_dir = \
                get_build_model_dirs(library_name, model_name, target_abi,
                                     None, None,
                                     model_config[YAMLKeyword.model_file_path])
        else:
            model_output_base_dir, model_output_dir, mace_model_dir = \
                get_build_model_dirs(library_name, model_name, target_abi,
                                     target_soc, serial_num,
                                     model_config[YAMLKeyword.model_file_path])
L
liuqi 已提交
1165 1166 1167 1168
        mace_check(os.path.exists(model_output_dir)
                   and os.path.exists(mace_model_dir),
                   ModuleName.BENCHMARK,
                   'You should build before benchmark.')
1169 1170 1171 1172 1173 1174 1175
        if target_abi != ABIType.host:
            sh_commands.clear_phone_data_dir(serial_num, PHONE_DATA_DIR)

        sh_commands.gen_random_input(
            model_output_dir,
            subgraphs[0][YAMLKeyword.input_tensors],
            subgraphs[0][YAMLKeyword.input_shapes],
李寅 已提交
1176
            subgraphs[0][YAMLKeyword.validation_inputs_data],
1177
            input_ranges=subgraphs[0][YAMLKeyword.input_ranges])
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
        runtime_list = []
        if target_abi == ABIType.host:
            runtime_list.extend([RuntimeType.cpu])
        elif model_runtime == RuntimeType.cpu_gpu:
            runtime_list.extend([RuntimeType.cpu, RuntimeType.gpu])
        else:
            runtime_list.extend([model_runtime])
        for runtime in runtime_list:
            device_type = parse_device_type(runtime)
            sh_commands.benchmark_model(
                abi=target_abi,
                serialno=serial_num,
                benchmark_binary_dir=build_tmp_binary_dir,
                vlog_level=0,
                embed_model_data=embed_model_data,
                model_output_dir=model_output_dir,
                input_nodes=subgraphs[0][YAMLKeyword.input_tensors],
                output_nodes=subgraphs[0][YAMLKeyword.output_tensors],
                input_shapes=subgraphs[0][YAMLKeyword.input_shapes],
                output_shapes=subgraphs[0][YAMLKeyword.output_shapes],
                mace_model_dir=mace_model_dir,
                model_tag=model_name,
                device_type=device_type,
                phone_data_dir=PHONE_DATA_DIR,
                build_type=build_type,
                omp_num_threads=flags.omp_num_threads,
                cpu_affinity_policy=flags.cpu_affinity_policy,
                gpu_perf_hint=flags.gpu_perf_hint,
1206
                gpu_priority_hint=flags.gpu_priority_hint,
Y
yejianwu 已提交
1207 1208
                opencl_binary_file=opencl_output_bin_path,
                shared_library_dir=get_shared_library_dir(library_name, target_abi),  # noqa
Y
yejianwu 已提交
1209
                linkshared=linkshared)
1210 1211 1212


def benchmark_model(flags):
1213
    configs = format_model_config(flags)
1214 1215

    target_socs = configs[YAMLKeyword.target_socs]
1216
    if not target_socs or ALL_SOC_TAG in target_socs:
1217 1218 1219
        target_socs = sh_commands.adb_get_all_socs()

    for target_abi in configs[YAMLKeyword.target_abis]:
L
liuqi 已提交
1220
        if target_abi == ABIType.host:
1221 1222 1223
            bm_specific_target(flags, configs, target_abi, None, None)
        else:
            for target_soc in target_socs:
L
liuqi 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232
                serial_nums = \
                    sh_commands.get_target_socs_serialnos([target_soc])
                mace_check(serial_nums,
                           ModuleName.BENCHMARK,
                           'There is no device with soc: ' + target_soc)
                for serial_num in serial_nums:
                    with sh_commands.device_lock(serial_num):
                        bm_specific_target(flags, configs, target_abi,
                                           target_soc, serial_num)
L
liuqi 已提交
1233

1234

L
liuqi 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
################################
# Parsing arguments
################################
def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')


def str_to_caffe_env_type(v):
    if v.lower() == 'docker':
1249
        return CaffeEnvType.DOCKER
L
liuqi 已提交
1250
    elif v.lower() == 'local':
1251
        return CaffeEnvType.LOCAL
L
liuqi 已提交
1252 1253 1254 1255
    else:
        raise argparse.ArgumentTypeError('[docker | local] expected.')


1256
def parse_args():
L
Liangliang He 已提交
1257
    """Parses command line arguments."""
1258 1259 1260
    all_type_parent_parser = argparse.ArgumentParser(add_help=False)
    all_type_parent_parser.add_argument(
        '--config',
L
Liangliang He 已提交
1261
        type=str,
1262
        default="",
L
liuqi 已提交
1263
        required=True,
1264
        help="the path of model yaml configuration file.")
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    all_type_parent_parser.add_argument(
        "--build_type",
        type=str,
        default="",
        help="Model build type, can be ['proto', 'code'].")
    all_type_parent_parser.add_argument(
        "--target_abis",
        type=str,
        default="",
        help="Target ABIs, comma seperated list.")
1275 1276 1277 1278 1279
    all_type_parent_parser.add_argument(
        "--target_socs",
        type=str,
        default="",
        help="Target SOCs, comma seperated list.")
1280 1281 1282 1283
    build_run_parent_parser = argparse.ArgumentParser(add_help=False)
    build_run_parent_parser.add_argument(
        '--address_sanitizer',
        action="store_true",
L
liuqi 已提交
1284
        help="Whether to use address sanitizer to check memory error")
1285 1286
    run_bm_parent_parser = argparse.ArgumentParser(add_help=False)
    run_bm_parent_parser.add_argument(
W
wuchenghui 已提交
1287 1288
        "--omp_num_threads",
        type=int,
1289
        default=DefaultValues.omp_num_threads,
W
wuchenghui 已提交
1290
        help="num of openmp threads")
1291
    run_bm_parent_parser.add_argument(
W
wuchenghui 已提交
1292 1293
        "--cpu_affinity_policy",
        type=int,
1294
        default=DefaultValues.cpu_affinity_policy,
W
wuchenghui 已提交
1295
        help="0:AFFINITY_NONE/1:AFFINITY_BIG_ONLY/2:AFFINITY_LITTLE_ONLY")
1296
    run_bm_parent_parser.add_argument(
W
wuchenghui 已提交
1297 1298
        "--gpu_perf_hint",
        type=int,
1299
        default=DefaultValues.gpu_perf_hint,
W
wuchenghui 已提交
1300
        help="0:DEFAULT/1:LOW/2:NORMAL/3:HIGH")
1301
    run_bm_parent_parser.add_argument(
W
wuchenghui 已提交
1302 1303
        "--gpu_priority_hint",
        type=int,
1304
        default=DefaultValues.gpu_priority_hint,
W
wuchenghui 已提交
1305
        help="0:DEFAULT/1:LOW/2:NORMAL/3:HIGH")
1306 1307 1308 1309 1310 1311 1312 1313 1314

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()
    build = subparsers.add_parser(
        'build',
        parents=[all_type_parent_parser, build_run_parent_parser],
        help='build model library and test tools')
    build.set_defaults(func=build_library)
    build.add_argument(
1315 1316 1317
        '--disable_tuning',
        action="store_false",
        help="Disable tuning the parameters for the GPU of specified SoC.")
1318
    build.add_argument(
1319
        "--disable_openmp",
1320
        action="store_false",
1321
        help="Disable openmp for multiple thread.")
1322 1323 1324 1325 1326 1327 1328 1329
    run = subparsers.add_parser(
        'run',
        parents=[all_type_parent_parser, run_bm_parent_parser,
                 build_run_parent_parser],
        help='run model in command line')
    run.set_defaults(func=run_mace)
    run.add_argument(
        "--round",
L
Liangliang He 已提交
1330
        type=int,
1331 1332 1333 1334 1335
        default=1,
        help="The model running round.")
    run.add_argument(
        "--validate",
        action="store_true",
1336 1337
        help="whether to verify the results are consistent with "
             "the frameworks.")
1338
    run.add_argument(
L
liuqi 已提交
1339 1340 1341
        "--caffe_env",
        type=str_to_caffe_env_type,
        default='docker',
1342 1343
        help="[docker | local] you can specific caffe environment for"
             " validation. local environment or caffe docker image.")
1344 1345 1346 1347
    run.add_argument(
        "--vlog_level",
        type=int,
        default=0,
1348
        help="[1~5]. Verbose log level for debug.")
1349
    run.add_argument(
L
Liangliang He 已提交
1350
        "--gpu_out_of_range_check",
1351 1352 1353 1354 1355 1356
        action="store_true",
        help="Enable out of memory check for gpu.")
    run.add_argument(
        "--restart_round",
        type=int,
        default=1,
1357
        help="restart round between run.")
1358 1359 1360 1361 1362 1363
    run.add_argument(
        "--report",
        action="store_true",
        help="print run statistics report.")
    run.add_argument(
        "--report_dir",
1364 1365
        type=str,
        default="",
1366 1367
        help="print run statistics report.")
    run.add_argument(
李寅 已提交
1368 1369 1370 1371
        "--runtime_failure_ratio",
        type=float,
        default=0.0,
        help="[mock runtime failure ratio].")
L
liuqi 已提交
1372 1373 1374 1375
    run.add_argument(
        "--example",
        action="store_true",
        help="whether to run example.")
1376 1377 1378 1379 1380 1381
    benchmark = subparsers.add_parser(
        'benchmark',
        parents=[all_type_parent_parser, run_bm_parent_parser,
                 build_run_parent_parser],
        help='benchmark model for detail information')
    benchmark.set_defaults(func=benchmark_model)
L
Liangliang He 已提交
1382 1383
    return parser.parse_known_args()

1384

Y
yejianwu 已提交
1385
if __name__ == "__main__":
1386 1387
    flags, unparsed = parse_args()
    flags.func(flags)