bazel_adb_run.py 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/env python

# 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 已提交
12
import random
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import re
import sys

import sh_commands

def stdout_processor(stdout, device_properties, abi):
  pass

def ops_test_stdout_processor(stdout, device_properties, abi):
  stdout_lines = stdout.split("\n")
  for line in stdout_lines:
    if "Aborted" in line or "FAILED" in line:
      raise Exception("Command failed")

def ops_benchmark_stdout_processor(stdout, device_properties, abi):
  stdout_lines = stdout.split("\n")
  metrics = {}
  for line in stdout_lines:
    if "Aborted" in line:
      raise Exception("Command failed")
    line = line.strip()
    parts = line.split()
    if len(parts) == 5 and parts[0].startswith("BM_"):
L
Liangliang He 已提交
36
      metrics["%s.time_ms" % parts[0]] = str(float(parts[1])/1e6)
L
Liangliang He 已提交
37 38
      metrics["%s.input_mb_per_sec" % parts[0]] = parts[3]
      metrics["%s.gmacc_per_sec" % parts[0]] = parts[4]
L
Liangliang He 已提交
39 40 41 42 43 44 45

  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}
  sh_commands.falcon_push_metrics(metrics, tags=tags,
L
Liangliang He 已提交
46
                                  endpoint="mace_ops_benchmark")
47 48 49 50 51 52 53 54 55 56 57 58 59

def parse_args():
  """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",
L
Liangliang He 已提交
60
      help="SoCs(ro.board.platform) to build, comma seperated list or all/random")
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  parser.add_argument(
      "--target",
      type=str,
      default="//...",
      help="Bazel target to build")
  parser.add_argument(
      "--run_target",
      type=bool,
      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")
  return parser.parse_known_args()

def main(unused_args):
  target_socs = None
L
Liangliang He 已提交
85
  if FLAGS.target_socs != "all" and FLAGS.target_socs != "random":
86 87
    target_socs = set(FLAGS.target_socs.split(','))
  target_devices = sh_commands.adb_devices(target_socs=target_socs)
L
Liangliang He 已提交
88 89
  if FLAGS.target_socs == "random":
    target_devices = [random.choice(target_devices)]
90 91 92 93 94 95 96

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

  # generate sources
  sh_commands.gen_encrypted_opencl_source()
L
liuqi 已提交
97
  sh_commands.gen_compiled_opencl_source()
98 99 100 101 102 103
  sh_commands.gen_mace_version()

  for target_abi in target_abis:
    sh_commands.bazel_build(target, abi=target_abi)
    if FLAGS.run_target:
      for serialno in target_devices:
104 105 106
        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
107 108 109 110
        stdouts = sh_commands.adb_run(serialno, host_bin_path, bin_name,
                                      args=FLAGS.args,
                                      opencl_profiling=1,
                                      vlog_level=0,
111 112
                                      device_bin_path="/data/local/tmp/mace",
                                      out_of_range_check=1)
113
        device_properties = sh_commands.adb_getprop_by_serialno(serialno)
114 115 116 117 118
        globals()[FLAGS.stdout_processor](stdouts, device_properties, target_abi)

if __name__ == "__main__":
  FLAGS, unparsed = parse_args()
  main(unused_args=[sys.argv[0]] + unparsed)