bazel_adb_run.py 5.7 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 16 17 18 19 20 21 22

# Must run at root dir of libmace project.
# python tools/bazel_adb_run.py \
#     --target_abis=armeabi-v7a \
#     --target_socs=sdm845
#     --target=//mace/ops:ops_test
#     --stdout_processor=stdout_processor

import argparse
L
Liangliang He 已提交
23
import random
24 25 26 27 28
import re
import sys

import sh_commands

L
Liangliang He 已提交
29

30
def stdout_processor(stdout, device_properties, abi):
L
Liangliang He 已提交
31 32
    pass

33

L
Liangliang He 已提交
34
def unittest_stdout_processor(stdout, device_properties, abi):
L
Liangliang He 已提交
35 36
    stdout_lines = stdout.split("\n")
    for line in stdout_lines:
L
Liangliang He 已提交
37 38
        if "Aborted" in line or "FAILED" in line or \
                "Segmentation fault" in line:
L
Liangliang He 已提交
39 40
            raise Exception("Command failed")

41 42

def ops_benchmark_stdout_processor(stdout, device_properties, abi):
L
Liangliang He 已提交
43 44 45
    stdout_lines = stdout.split("\n")
    metrics = {}
    for line in stdout_lines:
L
Liangliang He 已提交
46
        if "Aborted" in line or "Segmentation fault" in line:
L
Liangliang He 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
            raise Exception("Command failed")
        line = line.strip()
        parts = line.split()
        if len(parts) == 5 and parts[0].startswith("BM_"):
            metrics["%s.time_ms" % parts[0]] = str(float(parts[1]) / 1e6)
            metrics["%s.input_mb_per_sec" % parts[0]] = parts[3]
            metrics["%s.gmacc_per_sec" % parts[0]] = parts[4]

    platform = device_properties["ro.board.platform"].replace(" ", "-")
    model = device_properties["ro.product.model"].replace(" ", "-")
    tags = {
        "ro.board.platform": platform,
        "ro.product.model": model,
        "abi": abi
    }
L
Liangliang He 已提交
62 63
    # sh_commands.falcon_push_metrics(server,
    #    metrics, tags=tags, endpoint="mace_ops_benchmark")
L
Liangliang He 已提交
64

65

李寅 已提交
66 67 68 69 70 71 72 73 74 75 76
# TODO: after merge mace/python/tools and tools are merged,
# define str2bool as common util
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.')


77
def parse_args():
L
Liangliang He 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    """Parses command line arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--target_abis",
        type=str,
        default="armeabi-v7a",
        help="Target ABIs, comma seperated list")
    parser.add_argument(
        "--target_socs",
        type=str,
        default="all",
        help="SoCs (ro.board.platform from getprop) to build, "
        "comma seperated list or all/random")
    parser.add_argument(
        "--target", type=str, default="//...", help="Bazel target to build")
    parser.add_argument(
        "--run_target",
李寅 已提交
95
        type=str2bool,
L
Liangliang He 已提交
96 97 98 99 100 101 102 103
        default=False,
        help="Whether to run the target")
    parser.add_argument("--args", type=str, default="", help="Command args")
    parser.add_argument(
        "--stdout_processor",
        type=str,
        default="stdout_processor",
        help="Stdout processing function, default: stdout_processor")
李寅 已提交
104 105 106 107 108
    parser.add_argument(
        "--enable_neon",
        type=str2bool,
        default=True,
        help="Whether to use neon optimization")
109 110 111 112
    parser.add_argument(
        '--address_sanitizer',
        action="store_true",
        help="Whether to enable AddressSanitizer")
L
Liangliang He 已提交
113 114
    return parser.parse_known_args()

115 116

def main(unused_args):
L
Liangliang He 已提交
117 118 119
    target_socs = None
    if FLAGS.target_socs != "all" and FLAGS.target_socs != "random":
        target_socs = set(FLAGS.target_socs.split(','))
W
wuchenghui 已提交
120
    target_devices = sh_commands.get_target_socs_serialnos(target_socs)
L
Liangliang He 已提交
121
    if FLAGS.target_socs == "random":
122 123 124 125 126 127
        unlocked_devices = \
            [d for d in target_devices if not sh_commands.is_device_locked(d)]
        if len(unlocked_devices) > 0:
            target_devices = [random.choice(unlocked_devices)]
        else:
            target_devices = [random.choice(target_devices)]
L
Liangliang He 已提交
128 129 130 131 132 133

    target = FLAGS.target
    host_bin_path, bin_name = sh_commands.bazel_target_to_bin(target)
    target_abis = FLAGS.target_abis.split(',')

    for target_abi in target_abis:
134 135 136
        sh_commands.bazel_build(target, abi=target_abi,
                                enable_neon=FLAGS.enable_neon,
                                address_sanitizer=FLAGS.address_sanitizer)
L
Liangliang He 已提交
137 138 139 140 141 142 143
        if FLAGS.run_target:
            for serialno in target_devices:
                if target_abi not in set(
                        sh_commands.adb_supported_abis(serialno)):
                    print("Skip device %s which does not support ABI %s" %
                          (serialno, target_abi))
                    continue
144 145 146 147 148 149
                stdouts = sh_commands.adb_run(
                    target_abi,
                    serialno,
                    host_bin_path,
                    bin_name,
                    args=FLAGS.args,
L
liuqi 已提交
150
                    opencl_profiling=True,
151 152
                    vlog_level=0,
                    device_bin_path="/data/local/tmp/mace",
L
liuqi 已提交
153
                    out_of_range_check=True,
154
                    address_sanitizer=FLAGS.address_sanitizer)
L
Liangliang He 已提交
155 156 157 158 159
                device_properties = sh_commands.adb_getprop_by_serialno(
                    serialno)
                globals()[FLAGS.stdout_processor](stdouts, device_properties,
                                                  target_abi)

160 161

if __name__ == "__main__":
L
Liangliang He 已提交
162 163
    FLAGS, unparsed = parse_args()
    main(unused_args=[sys.argv[0]] + unparsed)