mace_tools.py 21.0 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

# python tools/mace_tools.py \
Y
yejianwu 已提交
16
#     --config=tools/example.yaml \
17 18 19 20
#     --round=100 \
#     --mode=all

import argparse
Y
yejianwu 已提交
21
import filelock
22
import hashlib
23
import os
L
Liangliang He 已提交
24
import sh
25 26
import subprocess
import sys
27
import urllib
Y
yejianwu 已提交
28
import yaml
L
liuqi 已提交
29
import re
30

31
import sh_commands
L
Liangliang He 已提交
32

33 34
from ConfigParser import ConfigParser

Y
yejianwu 已提交
35

Y
yejianwu 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
def get_target_socs(configs):
    available_socs = sh_commands.adb_get_all_socs()
    target_socs = available_socs
    if hasattr(configs, "target_socs"):
        target_socs = set(configs["target_socs"])
        target_socs = target_socs & available_socs

    if FLAGS.target_socs != "all":
        socs = set(FLAGS.target_socs.split(','))
        target_socs = target_socs & socs
        missing_socs = socs.difference(target_socs)
        if len(missing_socs) > 0:
            print(
                "Error: devices with SoCs are not connected %s" % missing_socs)
            exit(1)

    if not target_socs:
        print("Error: no device to run")
        exit(1)

    return target_socs

58

Y
yejianwu 已提交
59 60 61
def get_data_and_device_type(runtime):
    data_type = ""
    device_type = ""
62

Y
yejianwu 已提交
63 64 65 66 67 68 69 70 71 72 73 74
    if runtime == "dsp":
        data_type = "DT_UINT8"
        device_type = "HEXAGON"
    elif runtime == "gpu":
        data_type = "DT_HALF"
        device_type = "OPENCL"
    elif runtime == "cpu":
        data_type = "DT_FLOAT"
        device_type = "CPU"
    elif runtime == "neon":
        data_type = "DT_FLOAT"
        device_type = "NEON"
75

Y
yejianwu 已提交
76
    return data_type, device_type
77

Y
yejianwu 已提交
78 79

def get_hexagon_mode(configs):
L
Liangliang He 已提交
80 81 82 83 84 85 86
    runtime_list = []
    for model_name in configs["models"]:
        model_runtime = configs["models"][model_name]["runtime"]
        runtime_list.append(model_runtime.lower())

    global_runtime = ""
    if "dsp" in runtime_list:
Y
yejianwu 已提交
87 88 89 90
        return True
    return False


91 92 93 94
def gen_opencl_and_tuning_code(target_soc,
                               target_abi,
                               model_output_dirs,
                               pull_or_not):
95 96 97
    if pull_or_not:
        sh_commands.pull_binaries(
                target_soc, target_abi, model_output_dirs)
98 99 100 101 102 103

    codegen_path = "mace/codegen"

    # generate opencl binary code
    sh_commands.gen_opencl_binary_code(target_soc, model_output_dirs)

Y
yejianwu 已提交
104
    sh_commands.gen_tuning_param_code(
105
            target_soc, model_output_dirs)
Y
yejianwu 已提交
106 107


108 109 110 111 112 113
def model_benchmark_stdout_processor(stdout,
                                     target_soc,
                                     abi,
                                     runtime,
                                     running_round,
                                     tuning):
L
Liangliang He 已提交
114
    metrics = {}
Y
yejianwu 已提交
115 116 117
    for line in stdout:
        if "Aborted" in line:
            raise Exception("Command failed")
L
Liangliang He 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130
        line = line.strip()
        parts = line.split()
        if len(parts) == 6 and parts[0].startswith("time"):
            metrics["%s.create_net_ms" % model_name] = str(float(parts[1]))
            metrics["%s.mace_engine_ctor_ms" % model_name] = str(
                float(parts[2]))
            metrics["%s.init_ms" % model_name] = str(float(parts[3]))
            metrics["%s.warmup_ms" % model_name] = str(float(parts[4]))
            if float(parts[5]) > 0:
                metrics["%s.avg_latency_ms" % model_name] = str(
                    float(parts[5]))
    tags = {
        "ro.board.platform": target_soc,
Y
yejianwu 已提交
131 132
        "abi": abi,
        "runtime": runtime,
L
Liangliang He 已提交
133 134 135 136 137 138
        "round": running_round,  # TODO(yejianwu) change this to source/binary
        "tuning": tuning
    }
    sh_commands.falcon_push_metrics(
        metrics, endpoint="mace_model_benchmark", tags=tags)

Y
yejianwu 已提交
139

Y
yejianwu 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
def tuning_run(runtime,
               target_soc,
               target_abi,
               vlog_level,
               embed_model_data,
               model_output_dir,
               input_nodes,
               output_nodes,
               input_shapes,
               output_shapes,
               model_name,
               device_type,
               running_round,
               restart_round,
               out_of_range_check,
155
               phone_data_dir,
Y
yejianwu 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
               tuning=False,
               limit_opencl_kernel_time=0,
               option_args=""):
    stdout = sh_commands.tuning_run(
            target_soc,
            target_abi,
            vlog_level,
            embed_model_data,
            model_output_dir,
            input_nodes,
            output_nodes,
            input_shapes,
            output_shapes,
            model_name,
            device_type,
            running_round,
            restart_round,
            limit_opencl_kernel_time,
            tuning,
            out_of_range_check,
176
            phone_data_dir,
Y
yejianwu 已提交
177
            option_args)
178
    model_benchmark_stdout_processor(stdout,
179 180 181 182 183
                                     target_soc,
                                     target_abi,
                                     runtime,
                                     running_round,
                                     tuning)
Y
yejianwu 已提交
184 185 186 187 188 189


def build_mace_run_prod(hexagon_mode, runtime, target_soc, target_abi,
                        vlog_level, embed_model_data, model_output_dir,
                        input_nodes, output_nodes, input_shapes, output_shapes,
                        model_name, device_type, running_round, restart_round,
190
                        tuning, limit_opencl_kernel_time, phone_data_dir):
191
    gen_opencl_and_tuning_code(target_soc, target_abi, [], False)
L
Liangliang He 已提交
192
    production_or_not = False
Y
yejianwu 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
    mace_run_target = "//mace/tools/validation:mace_run"
    sh_commands.bazel_build(
            mace_run_target,
            abi=target_abi,
            model_tag=model_name,
            production_mode=False,
            hexagon_mode=hexagon_mode)
    sh_commands.update_mace_run_lib(model_output_dir, target_abi, model_name,
                                    embed_model_data)

    tuning_run(runtime, target_soc, target_abi, vlog_level, embed_model_data,
               model_output_dir, input_nodes, output_nodes, input_shapes,
               output_shapes, model_name, device_type, running_round=0,
206 207
               restart_round=1, out_of_range_check=True,
               phone_data_dir=phone_data_dir, tuning=False)
Y
yejianwu 已提交
208 209 210 211

    tuning_run(runtime, target_soc, target_abi, vlog_level, embed_model_data,
               model_output_dir, input_nodes, output_nodes, input_shapes,
               output_shapes, model_name, device_type, running_round=0,
212 213
               restart_round=1, out_of_range_check=False,
               phone_data_dir=phone_data_dir, tuning=tuning,
Y
yejianwu 已提交
214 215
               limit_opencl_kernel_time=limit_opencl_kernel_time)

216 217
    gen_opencl_and_tuning_code(
            target_soc, target_abi, [model_output_dir], True)
L
Liangliang He 已提交
218
    production_or_not = True
Y
yejianwu 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    sh_commands.bazel_build(
            mace_run_target,
            abi=target_abi,
            model_tag=model_name,
            production_mode=True,
            hexagon_mode=hexagon_mode)
    sh_commands.update_mace_run_lib(model_output_dir, target_abi, model_name,
                                    embed_model_data)


def merge_libs_and_tuning_results(target_soc,
                                  target_abi,
                                  project_name,
                                  output_dir,
                                  model_output_dirs,
                                  hexagon_mode,
                                  embed_model_data):
236 237
    gen_opencl_and_tuning_code(
            target_soc, target_abi, model_output_dirs, False)
Y
yejianwu 已提交
238 239 240 241 242 243 244 245 246
    sh_commands.build_production_code(target_abi)

    sh_commands.merge_libs(target_soc,
                           target_abi,
                           project_name,
                           output_dir,
                           model_output_dirs,
                           hexagon_mode,
                           embed_model_data)
L
Liangliang He 已提交
247

Y
yejianwu 已提交
248

Y
yejianwu 已提交
249 250 251
def get_model_files(model_file_path,
                    model_output_dir,
                    weight_file_path=""):
Y
yejianwu 已提交
252 253
    model_file = ""
    weight_file = ""
L
Liangliang He 已提交
254 255
    if model_file_path.startswith("http://") or \
            model_file_path.startswith("https://"):
Y
yejianwu 已提交
256 257
        model_file = model_output_dir + "/model.pb"
        urllib.urlretrieve(model_file_path, model_file)
Y
yejianwu 已提交
258 259
    else:
        model_file = model_file_path
L
Liangliang He 已提交
260 261 262

    if weight_file_path.startswith("http://") or \
            weight_file_path.startswith("https://"):
Y
yejianwu 已提交
263 264
        weight_file = model_output_dir + "/model.caffemodel"
        urllib.urlretrieve(weight_file_path, weight_file)
Y
yejianwu 已提交
265 266
    else:
        weight_file = weight_file_path
Y
yejianwu 已提交
267 268

    return model_file, weight_file
L
Liangliang He 已提交
269

L
liuqi 已提交
270 271

def md5sum(str):
L
Liangliang He 已提交
272 273 274
    md5 = hashlib.md5()
    md5.update(str)
    return md5.hexdigest()
L
liuqi 已提交
275

276 277

def parse_model_configs():
L
Liangliang He 已提交
278 279 280
    with open(FLAGS.config) as f:
        configs = yaml.load(f)
        return configs
281 282 283


def parse_args():
L
Liangliang He 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    """Parses command line arguments."""
    parser = argparse.ArgumentParser()
    parser.register("type", "bool", lambda v: v.lower() == "true")
    parser.add_argument(
        "--config",
        type=str,
        default="./tool/config",
        help="The global config file of models.")
    parser.add_argument(
        "--output_dir", type=str, default="build", help="The output dir.")
    parser.add_argument(
        "--round", type=int, default=1, help="The model running round.")
    parser.add_argument(
        "--run_seconds",
        type=int,
        default=10,
        help="The model throughput test running seconds.")
    parser.add_argument(
        "--restart_round",
        type=int,
        default=1,
        help="The model restart round.")
    parser.add_argument(
        "--tuning", type="bool", default="true", help="Tune opencl params.")
    parser.add_argument(
        "--mode",
        type=str,
        default="all",
        help="[build|run|validate|merge|all|throughput_test].")
    parser.add_argument(
        "--target_socs",
        type=str,
        default="all",
        help="SoCs to build, comma seperated list (getprop ro.board.platform)")
Y
yejianwu 已提交
318 319 320 321 322
    parser.add_argument(
        "--out_of_range_check",
        type="bool",
        default="false",
        help="Enable out of range check for opencl.")
L
Liangliang He 已提交
323 324
    return parser.parse_known_args()

325

Y
yejianwu 已提交
326
def process_models(project_name, configs, embed_model_data, vlog_level,
327
                   target_soc, target_abi, phone_data_dir, option_args):
Y
yejianwu 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    hexagon_mode = get_hexagon_mode(configs)
    model_output_dirs = []
    for model_name in configs["models"]:
        print '===================', model_name, '==================='
        model_config = configs["models"][model_name]
        input_file_list = model_config.get("validation_inputs_data",
                                           [])
        data_type, device_type = get_data_and_device_type(
                model_config["runtime"])

        for key in ["input_nodes", "output_nodes", "input_shapes",
                    "output_shapes"]:
            if not isinstance(model_config[key], list):
                model_config[key] = [model_config[key]]

        # Create model build directory
        model_path_digest = md5sum(model_config["model_file_path"])
        model_output_dir = "%s/%s/%s/%s/%s/%s/%s" % (
            FLAGS.output_dir, project_name, "build",
            model_name, model_path_digest, target_soc, target_abi)
        model_output_dirs.append(model_output_dir)

        if FLAGS.mode == "build" or FLAGS.mode == "all":
            if os.path.exists(model_output_dir):
                sh.rm("-rf", model_output_dir)
            os.makedirs(model_output_dir)
354 355
            sh_commands.clear_mace_run_data(
                    target_abi, target_soc, phone_data_dir)
Y
yejianwu 已提交
356

Y
yejianwu 已提交
357
        model_file_path, weight_file_path = get_model_files(
Y
yejianwu 已提交
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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
                model_config["model_file_path"],
                model_output_dir,
                model_config.get("weight_file_path", ""))

        if FLAGS.mode == "build" or FLAGS.mode == "run" or \
                FLAGS.mode == "validate" or \
                FLAGS.mode == "benchmark" or FLAGS.mode == "all":
            sh_commands.gen_random_input(model_output_dir,
                                         model_config["input_nodes"],
                                         model_config["input_shapes"],
                                         input_file_list)

        if FLAGS.mode == "build" or FLAGS.mode == "all":
            sh_commands.gen_model_code(
                    "mace/codegen/models/%s" % model_name,
                    model_config["platform"],
                    model_file_path,
                    weight_file_path,
                    model_config["model_sha256_checksum"],
                    ",".join(model_config["input_nodes"]),
                    ",".join(model_config["output_nodes"]),
                    data_type,
                    model_config["runtime"],
                    model_name,
                    ":".join(model_config["input_shapes"]),
                    model_config["dsp_mode"],
                    embed_model_data,
                    model_config["fast_conv"],
                    model_config["obfuscate"])
            build_mace_run_prod(hexagon_mode,
                                model_config["runtime"],
                                target_soc,
                                target_abi,
                                vlog_level,
                                embed_model_data,
                                model_output_dir,
                                model_config["input_nodes"],
                                model_config["output_nodes"],
                                model_config["input_shapes"],
                                model_config["output_shapes"],
                                model_name,
                                device_type,
                                FLAGS.round,
                                FLAGS.restart_round,
                                FLAGS.tuning,
403 404
                                model_config["limit_opencl_kernel_time"],
                                phone_data_dir)
Y
yejianwu 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

        if FLAGS.mode == "run" or FLAGS.mode == "validate" or \
                FLAGS.mode == "all":
            tuning_run(model_config["runtime"],
                       target_soc,
                       target_abi,
                       vlog_level,
                       embed_model_data,
                       model_output_dir,
                       model_config["input_nodes"],
                       model_config["output_nodes"],
                       model_config["input_shapes"],
                       model_config["output_shapes"],
                       model_name,
                       device_type,
                       FLAGS.round,
                       FLAGS.restart_round,
422 423
                       FLAGS.out_of_range_check,
                       phone_data_dir)
Y
yejianwu 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437

        if FLAGS.mode == "benchmark":
            sh_commands.benchmark_model(target_soc,
                                        target_abi,
                                        vlog_level,
                                        embed_model_data,
                                        model_output_dir,
                                        model_config["input_nodes"],
                                        model_config["output_nodes"],
                                        model_config["input_shapes"],
                                        model_config["output_shapes"],
                                        model_name,
                                        device_type,
                                        hexagon_mode,
438
                                        phone_data_dir,
Y
yejianwu 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451
                                        option_args)

        if FLAGS.mode == "validate" or FLAGS.mode == "all":
            sh_commands.validate_model(target_soc,
                                       target_abi,
                                       model_file_path,
                                       weight_file_path,
                                       model_config["platform"],
                                       model_config["runtime"],
                                       model_config["input_nodes"],
                                       model_config["output_nodes"],
                                       model_config["input_shapes"],
                                       model_config["output_shapes"],
452 453
                                       model_output_dir,
                                       phone_data_dir)
Y
yejianwu 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

    if FLAGS.mode == "build" or FLAGS.mode == "merge" or \
            FLAGS.mode == "all":
        merge_libs_and_tuning_results(
            target_soc,
            target_abi,
            project_name,
            FLAGS.output_dir,
            model_output_dirs,
            hexagon_mode,
            embed_model_data)

    if FLAGS.mode == "throughput_test":
        merged_lib_file = FLAGS.output_dir + \
                "/%s/%s/libmace_%s.%s.a" % \
                (project_name, target_abi, project_name, target_soc)
        first_model = configs["models"].values()[0]
        throughput_test_output_dir = "%s/%s/%s/%s" % (
                FLAGS.output_dir, project_name, "build",
                "throughput_test")
        if os.path.exists(throughput_test_output_dir):
            sh.rm("-rf", throughput_test_output_dir)
        os.makedirs(throughput_test_output_dir)
        input_file_list = model_config.get("validation_inputs_data",
                                           [])
        sh_commands.gen_random_input(throughput_test_output_dir,
                                     first_model["input_nodes"],
                                     first_model["input_shapes"],
                                     input_file_list)
        model_tag_dict = {}
        for model_name in configs["models"]:
            runtime = configs["models"][model_name]["runtime"]
            model_tag_dict[runtime] = model_name
        sh_commands.build_run_throughput_test(target_soc,
                                              target_abi,
                                              vlog_level,
                                              FLAGS.run_seconds,
                                              merged_lib_file,
                                              throughput_test_output_dir,
                                              embed_model_data,
                                              model_config["input_nodes"],
                                              model_config["output_nodes"],
                                              model_config["input_shapes"],
                                              model_config["output_shapes"],
                                              model_tag_dict.get("cpu", ""),
                                              model_tag_dict.get("gpu", ""),
500 501
                                              model_tag_dict.get("dsp", ""),
                                              phone_data_dir)
L
Liangliang He 已提交
502

503 504

def main(unused_args):
L
Liangliang He 已提交
505 506 507 508 509 510
    configs = parse_model_configs()

    if FLAGS.mode == "validate":
        FLAGS.round = 1
        FLAGS.restart_round = 1

Y
yejianwu 已提交
511
    project_name = os.path.splitext(os.path.basename(FLAGS.config))[0]
L
Liangliang He 已提交
512 513 514 515 516
    if FLAGS.mode == "build" or FLAGS.mode == "all":
        # Remove previous output dirs
        if not os.path.exists(FLAGS.output_dir):
            os.makedirs(FLAGS.output_dir)
        elif os.path.exists(os.path.join(FLAGS.output_dir, "libmace")):
Y
yejianwu 已提交
517 518
            sh.rm("-rf", os.path.join(FLAGS.output_dir, project_name))
            os.makedirs(os.path.join(FLAGS.output_dir, project_name))
L
Liangliang He 已提交
519

Y
yejianwu 已提交
520 521 522
        # generate source
        sh_commands.gen_mace_version()
        sh_commands.gen_encrypted_opencl_source()
L
Liangliang He 已提交
523 524 525 526

    option_args = ' '.join(
        [arg for arg in unused_args if arg.startswith('--')])

Y
yejianwu 已提交
527
    target_socs = get_target_socs(configs)
L
Liangliang He 已提交
528

Y
yejianwu 已提交
529 530
    embed_model_data = configs.get("embed_model_data", 1)
    vlog_level = configs.get("vlog_level", 0)
531
    phone_data_dir = "/data/local/tmp/mace_run/"
L
Liangliang He 已提交
532 533
    for target_soc in target_socs:
        for target_abi in configs["target_abis"]:
Y
yejianwu 已提交
534 535 536 537 538 539 540 541 542 543 544
            serialno = sh_commands.adb_devices([target_soc]).pop()
            props = sh_commands.adb_getprop_by_serialno(serialno)
            print(
                "============================================================="
            )
            print("Trying to lock device", serialno)
            with sh_commands.device_lock(serialno):
                print("Run on device: %s, %s, %s" % (
                      serialno, props["ro.board.platform"],
                      props["ro.product.model"]))
                process_models(project_name, configs, embed_model_data,
545 546
                               vlog_level, target_soc, target_abi,
                               phone_data_dir, option_args)
L
Liangliang He 已提交
547 548

    if FLAGS.mode == "build" or FLAGS.mode == "all":
Y
yejianwu 已提交
549
        sh_commands.packaging_lib(FLAGS.output_dir, project_name)
Y
yejianwu 已提交
550

551

Y
yejianwu 已提交
552
if __name__ == "__main__":
L
Liangliang He 已提交
553 554
    FLAGS, unparsed = parse_args()
    main(unused_args=[sys.argv[0]] + unparsed)