def master_clear(): printer = StatusPrinter(indent=0) printer("Clearing Device") with Indent(printer): with Indent(printer): d = get_connected_device(printer=printer) cmd = ['shell', 'am', 'broadcast', '-a', 'android.intent.action.MASTER_CLEAR', '-n', 'android/com.android.server.MasterClearReceiver'] printer('\'' + ' '.join(cmd) + '\'') adb(cmd) sleep(d.get_shutdown_delay())
def prepare_new_app(config): """Init a new app, build it, and launch it on a connected device. :param config: :return: """ app_dir = "tmp/test_benchmarks/" config["app_dir"] = app_dir #: Create an app to to test if exists(app_dir): #: If using an emulator enable forwarding if "emulator-" in sh.adb("devices"): sh.adb("forward", "tcp:8888", "tcp:8888") return # App already made # if config['app_built']: # return # App already made # else: # #: Cleanup the old app # cleanup_app(config) enamlnative = sh.Command("./enaml-native") print( enamlnative( "init", "Benchmarks", "com.codelv.enamlnative.benchmarks", "tmp/test_benchmarks/", ) ) config["app_built"] = True with cd(join(app_dir, "Benchmarks")): with source_activated("venv", "enaml-native") as enamlnative: #: Now build python print(enamlnative("build-python")) #: Build and do a gradle sync, this will NOT include jni and native libs! print(enamlnative("build-android")) #: Now build python (again) to put them in the correct spot print(enamlnative("build-python")) #: Now try to run it and see if it crashes #: Requires emulator or device assert len(sh.adb("devices").strip().split("\n")) > 0, ( "No device is connected, " "can't test the build!" ) #: Flush logcat sh.adb("logcat", "--clear") #: Do a build and run print(enamlnative("run-android")) #: Wait a few seconds #: If using an emulator enable forwarding if "emulator-" in sh.adb("devices"): sh.adb("forward", "tcp:8888", "tcp:8888")
def bazel_build(serialno, target, abi, frameworks, runtimes): print("* Build %s with ABI %s" % (target, abi)) if abi == "host": bazel_args = ( "build", target, ) else: bazel_args = ( "build", target, "--config", "android", "--cpu=%s" % abi, "--action_env=ANDROID_NDK_HOME=%s" % os.environ["ANDROID_NDK_HOME"], ) for framework in frameworks: bazel_args += ("--define", "%s=true" % framework.lower()) if "DSP" in runtimes and abi == "armeabi-v7a": with device_lock(serialno): output = sh.adb("-s", serialno, "shell", "ls /system/lib/libcdsprpc.so") if "No such file or directory" in output: print("/system/lib/libcdsprpc.so does not exists! Skip DSP.") else: bazel_args += ("--define", "dsp=true") sh.bazel(_fg=True, *bazel_args) print("Build done!\n")
def adb_push_file(src_file, dst_dir, serialno): src_checksum = file_checksum(src_file) dst_file = os.path.join(dst_dir, os.path.basename(src_file)) stdout_buff = [] sh.adb("-s", serialno, "shell", "md5sum", dst_file, _out=lambda line: stdout_buff.append(line)) dst_checksum = stdout_buff[0].split()[0] if src_checksum == dst_checksum: print("Equal checksum with %s and %s" % (src_file, dst_file)) else: print("Push %s to %s" % (src_file, dst_dir)) sh.adb("-s", serialno, "push", src_file, dst_dir)
def exec_command(self, command, *args, **kwargs): if self.system == SystemType.android: return sh.adb('-s', self.address, 'shell', command, *args, **kwargs) elif self.system == SystemType.arm_linux: return sh.ssh('%s@%s' % (self.username, self.address), command, *args, **kwargs)
def print_device_packages(): listing = str(grep(egrep(adb(['shell', 'dumpsys', 'package', '*']), r'Package..com\.clover|versionName'), ['Package', '-A1'])) package_names = re.findall(r'Package \[(.*)\].*', listing) versions = list(map(lambda x : x.strip(), re.findall(r'versionName=(.*)', listing))) package2version = {} for p, v in zip(package_names, versions): package2version[p] = v print(json.dumps(package2version, indent=4))
def list_adb_device(self): adb_list = sh.adb('devices').stdout.decode('utf-8') \ .strip().split('\n')[1:] adb_list = [tuple(pair.split('\t')) for pair in adb_list] devices = [] for adb in adb_list: adb_device = self._create_adb_device(adb) devices.append(adb_device) return devices
def capture_screen(self): """ Use adb shell screencap :return: CV2Img """ img = CV2Img() result = sed(adb("shell", "screencap", "-p"), 's/\r$//') return img.load_binary(result.stdout)
def adb_devices(): serialnos = [] p = re.compile(r'(\S+)\s+device') for line in split_stdout(sh.adb("devices")): m = p.match(line) if m: serialnos.append(m.group(1)) return serialnos
def adb_devices(): serialnos = [] p = re.compile(r'(\w+)\s+device') for line in split_stdout(sh.adb("devices")): m = p.match(line) if m: serialnos.append(m.group(1)) return serialnos
def adb_getprop_by_serialno(serialno): outputs = sh.adb("-s", serialno, "shell", "getprop") raw_props = split_stdout(outputs) props = {} p = re.compile(r'\[(.+)\]: \[(.+)\]') for raw_prop in raw_props: m = p.match(raw_prop) if m: props[m.group(1)] = m.group(2) return props
def get_props(self): outputs = sh.adb("-s", self.address, "shell", "getprop") raw_props = sh_commands.split_stdout(outputs) props = {} p = re.compile(r'\[(.+)\]: \[(.+)\]') for raw_prop in raw_props: m = p.match(raw_prop) if m: props[m.group(1)] = m.group(2) return props
def adb_push_file(src_file, dst_dir, serialno, silent=False): if not os.path.isfile(src_file): print("Not file, skip pushing " + src_file) return src_checksum = bench_utils.file_checksum(src_file) dst_file = os.path.join(dst_dir, os.path.basename(src_file)) stdout_buff = [] try: sh.adb("-s", serialno, "shell", "md5sum", dst_file, _out=lambda line: stdout_buff.append(line)) except sh.ErrorReturnCode_1: print("Push %s to %s" % (src_file, dst_dir)) sh.adb("-s", serialno, "push", src_file, dst_dir) else: dst_checksum = stdout_buff[0].split()[0] if src_checksum == dst_checksum: if not silent: print("Equal checksum with %s and %s" % (src_file, dst_file)) else: if not silent: print("Push %s to %s" % (src_file, dst_dir)) sh.adb("-s", serialno, "push", src_file, dst_dir)
def bazel_build(serialno, target, abi, executor, device_types): print("* Build %s for %s with ABI %s" % (target, base_pb2.ExecutorType.Name(executor), abi)) if abi == "host": bazel_args = ( "build", target, ) else: bazel_args = ( "build", target, "--config", "android", "--cpu=%s" % abi, "--action_env=ANDROID_NDK_HOME=%s" % os.environ["ANDROID_NDK_HOME"], ) bazel_args += ("--define", "%s=true" % base_pb2.ExecutorType.Name(executor).lower()) if executor == base_pb2.MACE: bazel_args += ("--define", "neon=true") bazel_args += ("--define", "openmp=true") bazel_args += ("--define", "opencl=true") bazel_args += ("--define", "quantize=true") avail_device_types = copy.copy(device_types) if base_pb2.DSP in avail_device_types: avail_device_types.remove(base_pb2.DSP) if abi == "armeabi-v7a": with device_lock(serialno): try: output = sh.adb( "-s", serialno, "shell", "ls /system/vendor/lib/rfsa/adsp/libhexagon_nn_skel.so" ) # noqa except sh.ErrorReturnCode_1: print( "/system/vendor/lib/rfsa/adsp/libhexagon_nn_skel.so does not exists! Skip DSP." ) # noqa else: if "No such file or directory" in output: print( "/system/vendor/lib/rfsa/adsp/libhexagon_nn_skel.so does not exists! Skip DSP." ) # noqa else: avail_device_types.append(base_pb2.DSP) bazel_args += ("--define", "hexagon=true") sh.bazel(_fg=True, *bazel_args) print("Build done!\n") return avail_device_types
def push_precision_files(serialno, device_bin_path, input_dir): sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_bin_path) adb_push("aibench/benchmark/imagenet/imagenet_blacklist.txt", device_bin_path, serialno) adb_push("aibench/benchmark/imagenet/imagenet_groundtruth_labels.txt", device_bin_path, serialno) adb_push("aibench/benchmark/imagenet/mobilenet_model_labels.txt", device_bin_path, serialno) if input_dir != "": imagenet_input_path = device_bin_path + "/inputs/" print("Pushing images from %s to %s ..." % (input_dir, imagenet_input_path)) sh.adb("-s", serialno, "shell", "mkdir -p %s" % imagenet_input_path) sh.adb("-s", serialno, "push", input_dir, imagenet_input_path) base_dir = os.path.basename(input_dir) \ if input_dir[-1] != '/' else os.path.basename(input_dir[:-1]) sh.adb("-s", serialno, "shell", "mv %s/* %s" % (imagenet_input_path + base_dir, imagenet_input_path))
def get_cpu_mask(serialno): freq_list = [] cpu_id = 0 cpu_mask = '' while True: try: freq_list.append( int(sh.adb("-s", serialno, "shell", "cat /sys/devices/system/cpu/cpu%d" "/cpufreq/cpuinfo_max_freq" % cpu_id))) except (ValueError, sh.ErrorReturnCode_1): break else: cpu_id += 1 for freq in freq_list: cpu_mask = '1' + cpu_mask if freq == max(freq_list) else '0' + cpu_mask return str(hex(int(cpu_mask, 2)))[2:]
def cmd(cmds, **kwargs): proc = adb(*cmds, _bg=True) start, interval, timeout = 0, 0.1, int(kwargs.get('timeout', 10)) while start < timeout: sleep(interval) start += interval if not proc.process.alive: break else: proc.kill() try: proc.wait() except: pass return { 'stdout': proc.stdout, 'stderr': proc.stderr, 'returncode': proc.exit_code }
def list_adb_device(cls): adb_list = sh.adb('devices').stdout.decode('utf-8'). \ strip().split('\n')[1:] adb_list = [tuple(pair.split('\t')) for pair in adb_list] devices = [] for adb in adb_list: prop = sh_commands.adb_getprop_by_serialno(adb[0]) android = { YAMLKeyword.device_name: prop['ro.product.model'].replace(' ', ''), YAMLKeyword.target_abis: prop['ro.product.cpu.abilist'].split(','), YAMLKeyword.target_socs: prop['ro.board.platform'], YAMLKeyword.system: SystemType.android, YAMLKeyword.address: adb[0], YAMLKeyword.username: '', } devices.append(android) return devices
def bazel_build(serialno, target, abi, executors, device_types): print("* Build %s with ABI %s" % (target, abi)) if abi == "host": bazel_args = ( "build", target, ) else: bazel_args = ( "build", target, "--config", "android", "--cpu=%s" % abi, "--action_env=ANDROID_NDK_HOME=%s" % os.environ["ANDROID_NDK_HOME"], ) for executor in executors: bazel_args += ("--define", "%s=true" % base_pb2.ExecutorType.Name(executor).lower()) bazel_args += ("--define", "neon=true") bazel_args += ("--define", "openmp=true") bazel_args += ("--define", "opencl=true") bazel_args += ("--define", "quantize=true") if base_pb2.DSP in device_types and abi == "armeabi-v7a": with device_lock(serialno): try: output = sh.adb("-s", serialno, "shell", "ls /system/lib/libcdsprpc.so") except sh.ErrorReturnCode_1: print("/system/lib/libcdsprpc.so does not exists! Skip DSP.") else: if "No such file or directory" in output: print("/system/lib/libcdsprpc.so does not exists! Skip DSP.") # noqa else: bazel_args += ("--define", "dsp=true") bazel_args += ("--define", "hexagon=true") sh.bazel( _fg=True, *bazel_args) print("Build done!\n")
def adb_run(abi, serialno, host_bin_path, bin_name, args="", opencl_profiling=True, vlog_level=0, device_bin_path="/data/local/tmp/mace", out_of_range_check=True, address_sanitizer=False): host_bin_full_path = "%s/%s" % (host_bin_path, bin_name) device_bin_full_path = "%s/%s" % (device_bin_path, bin_name) props = adb_getprop_by_serialno(serialno) print( "=====================================================================" ) print("Trying to lock device %s" % serialno) with device_lock(serialno): print("Run on device: %s, %s, %s" % (serialno, props["ro.board.platform"], props["ro.product.model"])) sh.adb("-s", serialno, "shell", "rm -rf %s" % device_bin_path) sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_bin_path) adb_push(host_bin_full_path, device_bin_full_path, serialno) ld_preload = "" if address_sanitizer: adb_push(find_asan_rt_library(abi), device_bin_path, serialno) ld_preload = "LD_PRELOAD=%s/%s" % (device_bin_path, asan_rt_library_names(abi)), opencl_profiling = 1 if opencl_profiling else 0 out_of_range_check = 1 if out_of_range_check else 0 print("Run %s" % device_bin_full_path) stdout_buff = [] process_output = make_output_processor(stdout_buff) sh.adb( "-s", serialno, "shell", ld_preload, "MACE_OUT_OF_RANGE_CHECK=%d" % out_of_range_check, "MACE_OPENCL_PROFILING=%d" % opencl_profiling, "MACE_CPP_MIN_VLOG_LEVEL=%d" % vlog_level, device_bin_full_path, args, _tty_in=True, _out=process_output, _err_to_out=True) return "".join(stdout_buff)
def windows_size(self): """ adb shell dumpsys display | grep mBaseDisplayInfo :return: """ #real 1080 X 1920 real_size_pattern = r"real (\d+) x (\d+)," #density 480 (480.0 x 480.0) dpi, #density_pattern = re.compile(r"density (\d+) \((\d+.\d+ x \d+.\d+)\) dpi,") result = grep(adb("shell", "dumpsys", "display"), "mBaseDisplayInfo").__str__() match = re.search(real_size_pattern, result) if match: size = (int(match.group(1)), int(match.group(2))) else: size = None return size
def test_examples_for_real(platforms, path): """ This builds an actuall app and does full system benchmarks on loading app examples """ if 'TRAVIS' in os.environ: return #: Doesn't work on travis #: Pretty hackish but whatever prepare_new_app(config) #: Load the code dir_path = os.path.abspath(os.path.split(os.path.dirname(__file__))[0]) enaml_file = os.path.join(dir_path, 'examples', os.path.normpath(path)) with open(enaml_file, 'rb') as f: source = f.read() #: Trigger a reload r = requests.post("http://localhost:8888/", json={ "type": "reload", "files": { 'view.enaml': source }, }).json() assert r['ok'], "Failed to reload {}!".format(enaml_file) #: TODO need a way to know when everything is done... #: should read the log unil it stops time.sleep(5) #: Flush logcat #: Save it stats = parse_stats(sh.adb('logcat', '-d')) config['stats'][enaml_file] = stats #: Save it data = json.dumps(config, indent=2) with open('tmp/stats.json', 'w') as f: f.write(data)
def can_talk(local, remote, printer): printer("Pinging {} -> {}".format(remote, local)) with Indent(printer): printer('''adb shell 'ping -c 4 {} && echo SUCCESS || echo FAIL' '''.format(local)) with Indent(printer): remote2local = str(adb(['shell', 'ping -c 4 {} && echo SUCCESS || echo FAIL'.format(local)])) printer(remote2local) if 'SUCCESS' in remote2local: printer("Pinging {} -> {}".format(local, remote)) with Indent(printer): printer('ping -c 4 {}'.format(local)) with Indent(printer): try: local2remote = ping(['-c', '4', remote]) except sh.ErrorReturnCode as err: local2remote = err printer(local2remote) if local2remote.exit_code == 0: return True return False
def send_key(self, keycode): return adb("shell", "input", "keyevent", keycode)
def clear_phone_data_dir(serialno, phone_data_dir): sh.adb("-s", serialno, "shell", "rm -rf %s" % phone_data_dir)
def drag_and_drop(self, start_x, start_y, end_x, end_y, duration=None): adb("shell", "input", "swipe", start_x, start_y, end_x, end_y, duration)
def exec_command(self, command, *args, **kwargs): return sh.adb('-s', self.address, 'shell', command, *args, **kwargs)
def adb_push(src_path, dst_path, serialno): print("Push %s to %s" % (src_path, dst_path)) sh.adb("-s", serialno, "push", src_path, dst_path)
def rm(self, file): if self.system == SystemType.android: sh.adb('-s', self.address, 'shell', 'rm', '-rf', file, _fg=True) elif self.system == SystemType.arm_linux: self.exec_command('rm -rf {}'.format(file), _fg=True)
def adb_pull(src_path, dst_path, serialno): try: sh.adb("-s", serialno, "pull", src_path, dst_path) except Exception as e: six.print_("Error msg: %s" % e, file=sys.stderr)
def adb_pull(src_path, dst_path, serialno): print("Pull %s to %s" % (src_path, dst_path)) try: sh.adb("-s", serialno, "pull", src_path, dst_path) except Exception as e: print("Error msg: %s" % e.stderr)
def tap(self, x, y, duration=None): if duration: adb("shell", "input", "swipe", x, y, x, y, duration) else: adb("shell", "input", "tap", x, y)
def tuning_run(abi, serialno, mace_run_dir, vlog_level, embed_model_data, model_output_dir, input_nodes, output_nodes, input_shapes, output_shapes, mace_model_dir, model_tag, device_type, running_round, restart_round, limit_opencl_kernel_time, tuning, out_of_range_check, phone_data_dir, build_type, opencl_binary_file, shared_library_dir, omp_num_threads=-1, cpu_affinity_policy=1, gpu_perf_hint=3, gpu_priority_hint=3, input_file_name="model_input", output_file_name="model_out", runtime_failure_ratio=0.0, address_sanitizer=False, linkshared=0): print("* Run '%s' with round=%s, restart_round=%s, tuning=%s, " "out_of_range_check=%s, omp_num_threads=%s, cpu_affinity_policy=%s, " "gpu_perf_hint=%s, gpu_priority_hint=%s" % (model_tag, running_round, restart_round, str(tuning), str(out_of_range_check), omp_num_threads, cpu_affinity_policy, gpu_perf_hint, gpu_priority_hint)) mace_model_path = "" if build_type == BuildType.proto: mace_model_path = "%s/%s.pb" % (mace_model_dir, model_tag) if linkshared == 0: mace_run_target = "mace_run_static" else: mace_run_target = "mace_run_shared" if abi == "host": p = subprocess.Popen( [ "env", "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUNTIME_FAILURE_RATIO=%f" % runtime_failure_ratio, "%s/%s" % (mace_run_dir, mace_run_target), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (model_output_dir, input_file_name), "--output_file=%s/%s" % (model_output_dir, output_file_name), "--model_data_file=%s/%s.data" % (mace_model_dir, model_tag), "--device=%s" % device_type, "--round=%s" % running_round, "--restart_round=%s" % restart_round, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_path, ], stderr=subprocess.PIPE, stdout=subprocess.PIPE) out, err = p.communicate() stdout = err + out print stdout print("Running finished!\n") return stdout else: sh.adb("-s", serialno, "shell", "mkdir", "-p", phone_data_dir) internal_storage_dir = create_internal_storage_dir( serialno, phone_data_dir) for input_name in input_nodes: formatted_name = common.formatted_file_name(input_file_name, input_name) adb_push("%s/%s" % (model_output_dir, formatted_name), phone_data_dir, serialno) if address_sanitizer: adb_push(find_asan_rt_library(abi), phone_data_dir, serialno) if not embed_model_data: adb_push("%s/%s.data" % (mace_model_dir, model_tag), phone_data_dir, serialno) if device_type == common.DeviceType.GPU\ and os.path.exists(opencl_binary_file): adb_push(opencl_binary_file, phone_data_dir, serialno) adb_push("third_party/nnlib/libhexagon_controller.so", phone_data_dir, serialno) mace_model_phone_path = "" if build_type == BuildType.proto: mace_model_phone_path = "%s/%s.pb" % (phone_data_dir, model_tag) adb_push(mace_model_path, mace_model_phone_path, serialno) if linkshared == 1: adb_push("%s/libmace.so" % shared_library_dir, phone_data_dir, serialno) adb_push("%s/libgnustl_shared.so" % shared_library_dir, phone_data_dir, serialno) adb_push("%s/%s" % (mace_run_dir, mace_run_target), phone_data_dir, serialno) stdout_buff = [] process_output = make_output_processor(stdout_buff) adb_cmd = [ "LD_LIBRARY_PATH=%s" % phone_data_dir, "MACE_TUNING=%s" % int(tuning), "MACE_OUT_OF_RANGE_CHECK=%s" % int(out_of_range_check), "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUN_PARAMETER_PATH=%s/mace_run.config" % phone_data_dir, "MACE_INTERNAL_STORAGE_PATH=%s" % internal_storage_dir, "MACE_LIMIT_OPENCL_KERNEL_TIME=%s" % limit_opencl_kernel_time, "MACE_RUNTIME_FAILURE_RATIO=%f" % runtime_failure_ratio, ] if address_sanitizer: adb_cmd.extend([ "LD_PRELOAD=%s/%s" % (phone_data_dir, asan_rt_library_names(abi)) ]) adb_cmd.extend([ "%s/%s" % (phone_data_dir, mace_run_target), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (phone_data_dir, input_file_name), "--output_file=%s/%s" % (phone_data_dir, output_file_name), "--model_data_file=%s/%s.data" % (phone_data_dir, model_tag), "--device=%s" % device_type, "--round=%s" % running_round, "--restart_round=%s" % restart_round, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_phone_path, "--opencl_binary_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_binary_file)), ]) adb_cmd = ' '.join(adb_cmd) cmd_file_name = "%s-%s-%s" % ('cmd_file', model_tag, str(time.time())) adb_cmd_file = "%s/%s" % (phone_data_dir, cmd_file_name) tmp_cmd_file = "%s/%s" % ('/tmp', cmd_file_name) with open(tmp_cmd_file, 'w') as cmd_file: cmd_file.write(adb_cmd) adb_push(tmp_cmd_file, adb_cmd_file, serialno) os.remove(tmp_cmd_file) sh.adb( "-s", serialno, "shell", "sh", adb_cmd_file, _tty_in=True, _out=process_output, _err_to_out=True) stdout = "".join(stdout_buff) if not stdout_success(stdout): common.MaceLogger.error("Mace Run", "Mace run failed.") sh.adb( "-s", serialno, "shell", "rm", adb_cmd_file, _fg=True) print("Running finished!\n") return stdout
def create_internal_storage_dir(serialno, phone_data_dir): internal_storage_dir = "%s/interior/" % phone_data_dir sh.adb("-s", serialno, "shell", "mkdir", "-p", internal_storage_dir) return internal_storage_dir
def exec_command(self, command, *args, **kwargs): if self.system == SystemType.android: sh.adb('-s', self.address, 'shell', command, *args, **kwargs) elif self.system == SystemType.arm_linux: sh.ssh('{}@{}'.format(self.username, self.address), command, *args, **kwargs)
def adb_push(src_path, dst_path, serialno): sh.adb("-s", serialno, "push", src_path, dst_path)
def benchmark_model(abi, serialno, benchmark_binary_dir, vlog_level, embed_model_data, model_output_dir, mace_model_dir, input_nodes, output_nodes, input_shapes, output_shapes, model_tag, device_type, phone_data_dir, build_type, opencl_binary_file, shared_library_dir, omp_num_threads=-1, cpu_affinity_policy=1, gpu_perf_hint=3, gpu_priority_hint=3, input_file_name="model_input", linkshared=0): print("* Benchmark for %s" % model_tag) if linkshared == 0: benchmark_model_target = "benchmark_model_static" else: benchmark_model_target = "benchmark_model_shared" mace_model_path = "" if build_type == BuildType.proto: mace_model_path = "%s/%s.pb" % (mace_model_dir, model_tag) if abi == "host": p = subprocess.Popen( [ "env", "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "%s/%s" % (benchmark_binary_dir, benchmark_model_target), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (model_output_dir, input_file_name), "--model_data_file=%s/%s.data" % (mace_model_dir, model_tag), "--device=%s" % device_type, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_path, ]) p.wait() else: sh.adb("-s", serialno, "shell", "mkdir", "-p", phone_data_dir) internal_storage_dir = create_internal_storage_dir( serialno, phone_data_dir) for input_name in input_nodes: formatted_name = common.formatted_file_name(input_file_name, input_name) adb_push("%s/%s" % (model_output_dir, formatted_name), phone_data_dir, serialno) if not embed_model_data: adb_push("%s/%s.data" % (mace_model_dir, model_tag), phone_data_dir, serialno) if device_type == common.DeviceType.GPU \ and os.path.exists(opencl_binary_file): adb_push(opencl_binary_file, phone_data_dir, serialno) mace_model_phone_path = "" if build_type == BuildType.proto: mace_model_phone_path = "%s/%s.pb" % (phone_data_dir, model_tag) adb_push(mace_model_path, mace_model_phone_path, serialno) if linkshared == 1: adb_push("%s/libmace.so" % shared_library_dir, phone_data_dir, serialno) adb_push("%s/libgnustl_shared.so" % shared_library_dir, phone_data_dir, serialno) adb_push("%s/%s" % (benchmark_binary_dir, benchmark_model_target), phone_data_dir, serialno) adb_cmd = [ "LD_LIBRARY_PATH=%s" % phone_data_dir, "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUN_PARAMETER_PATH=%s/mace_run.config" % phone_data_dir, "MACE_INTERNAL_STORAGE_PATH=%s" % internal_storage_dir, "MACE_OPENCL_PROFILING=1", "%s/%s" % (phone_data_dir, benchmark_model_target), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (phone_data_dir, input_file_name), "--model_data_file=%s/%s.data" % (phone_data_dir, model_tag), "--device=%s" % device_type, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_phone_path, "--opencl_binary_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_binary_file)), ] adb_cmd = ' '.join(adb_cmd) cmd_file_name = "%s-%s-%s" % ('cmd_file', model_tag, str(time.time())) adb_cmd_file = "%s/%s" % (phone_data_dir, cmd_file_name) tmp_cmd_file = "%s/%s" % ('/tmp', cmd_file_name) with open(tmp_cmd_file, 'w') as cmd_file: cmd_file.write(adb_cmd) adb_push(tmp_cmd_file, adb_cmd_file, serialno) os.remove(tmp_cmd_file) sh.adb( "-s", serialno, "shell", "sh", adb_cmd_file, _fg=True) sh.adb( "-s", serialno, "shell", "rm", adb_cmd_file, _fg=True) print("Benchmark done!\n")
def benchmark_model(abi, serialno, benchmark_binary_dir, benchmark_binary_name, vlog_level, embed_model_data, model_output_dir, mace_model_dir, input_nodes, output_nodes, input_shapes, output_shapes, model_tag, device_type, phone_data_dir, model_graph_format, opencl_binary_file, opencl_parameter_file, libmace_dynamic_library_path, omp_num_threads=-1, cpu_affinity_policy=1, gpu_perf_hint=3, gpu_priority_hint=3, input_file_name="model_input", link_dynamic=False): print("* Benchmark for %s" % model_tag) mace_model_path = "" if model_graph_format == ModelFormat.file: mace_model_path = "%s/%s.pb" % (mace_model_dir, model_tag) if abi == "host": p = subprocess.Popen( [ "env", "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "%s/%s" % (benchmark_binary_dir, benchmark_binary_name), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (model_output_dir, input_file_name), "--model_data_file=%s/%s.data" % (mace_model_dir, model_tag), "--device=%s" % device_type, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_path, ]) p.wait() else: sh.adb("-s", serialno, "shell", "mkdir", "-p", phone_data_dir) internal_storage_dir = create_internal_storage_dir( serialno, phone_data_dir) for input_name in input_nodes: formatted_name = common.formatted_file_name(input_file_name, input_name) adb_push("%s/%s" % (model_output_dir, formatted_name), phone_data_dir, serialno) if not embed_model_data: adb_push("%s/%s.data" % (mace_model_dir, model_tag), phone_data_dir, serialno) if device_type == common.DeviceType.GPU: if os.path.exists(opencl_binary_file): adb_push(opencl_binary_file, phone_data_dir, serialno) if os.path.exists(opencl_parameter_file): adb_push(opencl_parameter_file, phone_data_dir, serialno) mace_model_phone_path = "" if model_graph_format == ModelFormat.file: mace_model_phone_path = "%s/%s.pb" % (phone_data_dir, model_tag) adb_push(mace_model_path, mace_model_phone_path, serialno) if link_dynamic: adb_push(libmace_dynamic_library_path, phone_data_dir, serialno) adb_push("%s/%s" % (benchmark_binary_dir, benchmark_binary_name), phone_data_dir, serialno) adb_cmd = [ "LD_LIBRARY_PATH=%s" % phone_data_dir, "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUN_PARAMETER_PATH=%s/mace_run.config" % phone_data_dir, "MACE_INTERNAL_STORAGE_PATH=%s" % internal_storage_dir, "MACE_OPENCL_PROFILING=1", "%s/%s" % (phone_data_dir, benchmark_binary_name), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (phone_data_dir, input_file_name), "--model_data_file=%s/%s.data" % (phone_data_dir, model_tag), "--device=%s" % device_type, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_phone_path, "--opencl_binary_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_binary_file)), "--opencl_parameter_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_parameter_file)), ] adb_cmd = ' '.join(adb_cmd) cmd_file_name = "%s-%s-%s" % ('cmd_file', model_tag, str(time.time())) adb_cmd_file = "%s/%s" % (phone_data_dir, cmd_file_name) tmp_cmd_file = "%s/%s" % ('/tmp', cmd_file_name) with open(tmp_cmd_file, 'w') as cmd_file: cmd_file.write(adb_cmd) adb_push(tmp_cmd_file, adb_cmd_file, serialno) os.remove(tmp_cmd_file) sh.adb( "-s", serialno, "shell", "sh", adb_cmd_file, _fg=True) sh.adb( "-s", serialno, "shell", "rm", adb_cmd_file, _fg=True) print("Benchmark done!\n")
def benchmark_model(self, abi, benchmark_binary_dir, benchmark_binary_name, vlog_level, embed_model_data, model_output_dir, mace_model_dir, input_nodes, output_nodes, input_shapes, output_shapes, model_tag, device_type, model_graph_format, opencl_binary_file, opencl_parameter_file, libmace_dynamic_library_path, omp_num_threads=-1, cpu_affinity_policy=1, gpu_perf_hint=3, gpu_priority_hint=3, input_file_name='model_input', link_dynamic=False): six.print_('* Benchmark for %s' % model_tag) mace_model_path = '' if model_graph_format == ModelFormat.file: mace_model_path = '%s/%s.pb' % (mace_model_dir, model_tag) if abi == ABIType.host: libmace_dynamic_lib_dir_path = \ os.path.dirname(libmace_dynamic_library_path) p = subprocess.Popen([ 'env', 'LD_LIBRARY_PATH=%s' % libmace_dynamic_lib_dir_path, 'MACE_CPP_MIN_VLOG_LEVEL=%s' % vlog_level, '%s/%s' % (benchmark_binary_dir, benchmark_binary_name), '--model_name=%s' % model_tag, '--input_node=%s' % ','.join(input_nodes), '--output_node=%s' % ','.join(output_nodes), '--input_shape=%s' % ':'.join(input_shapes), '--output_shapes=%s' % ':'.join(output_shapes), '--input_file=%s/%s' % (model_output_dir, input_file_name), '--model_data_file=%s/%s.data' % (mace_model_dir, model_tag), '--device=%s' % device_type, '--omp_num_threads=%s' % omp_num_threads, '--cpu_addinity_policy=%s' % cpu_affinity_policy, '--gpu_perf_hint=%s' % gpu_perf_hint, '--gpu_priority_hint=%s' % gpu_priority_hint, '--model_file=%s' % mace_model_path ]) p.wait() elif self.system in [SystemType.android, SystemType.arm_linux]: self.exec_command('mkdir -p %s' % self.data_dir) internal_storage_dir = self.create_internal_storage_dir() for input_name in input_nodes: formatted_name = formatted_file_name(input_file_name, input_name) self.push('%s/%s' % (model_output_dir, formatted_name), self.data_dir) if not embed_model_data: self.push('%s/%s.data' % (mace_model_dir, model_tag), self.data_dir) if device_type == common.DeviceType.GPU: if os.path.exists(opencl_binary_file): self.push(opencl_binary_file, self.data_dir) if os.path.exists(opencl_parameter_file): self.push(opencl_parameter_file, self.data_dir) mace_model_device_path = '' if model_graph_format == ModelFormat.file: mace_model_device_path = '%s/%s.pb' % \ (self.data_dir, model_tag) self.push(mace_model_path, mace_model_device_path) if link_dynamic: self.push(libmace_dynamic_library_path, self.data_dir) if self.system == SystemType.android: sh_commands.push_depended_so_libs( libmace_dynamic_library_path, abi, self.data_dir, self.address) self.rm('%s/%s' % (self.data_dir, benchmark_binary_name)) self.push('%s/%s' % (benchmark_binary_dir, benchmark_binary_name), self.data_dir) cmd = [ 'LD_LIBRARY_PATH=%s' % self.data_dir, 'MACE_CPP_MIN_VLOG_LEVEL=%s' % vlog_level, 'MACE_RUN_PARAMETER_PATH=%s/mace_run.config' % self.data_dir, 'MACE_INTERNAL_STORAGE_PATH=%s' % internal_storage_dir, 'MACE_OPENCL_PROFILING=1', '%s/%s' % (self.data_dir, benchmark_binary_name), '--model_name=%s' % model_tag, '--input_node=%s' % ','.join(input_nodes), '--output_node=%s' % ','.join(output_nodes), '--input_shape=%s' % ':'.join(input_shapes), '--output_shape=%s' % ':'.join(output_shapes), '--input_file=%s/%s' % (self.data_dir, input_file_name), '--model_data_file=%s/%s.data' % (self.data_dir, model_tag), '--device=%s' % device_type, '--omp_num_threads=%s' % omp_num_threads, '--cpu_affinity_policy=%s' % cpu_affinity_policy, '--gpu_perf_hint=%s' % gpu_perf_hint, '--gpu_priority_hint=%s' % gpu_priority_hint, '--model_file=%s' % mace_model_device_path, '--opencl_binary_file=%s/%s' % (self.data_dir, os.path.basename(opencl_binary_file)), '--opencl_parameter_file=%s/%s' % (self.data_dir, os.path.basename(opencl_parameter_file)) ] cmd = ' '.join(cmd) cmd_file_name = '%s-%s-%s' % \ ('cmd_file', model_tag, str(time.time())) cmd_file_path = '%s/%s' % (self.data_dir, cmd_file_name) tmp_cmd_file = '%s/%s' % ('/tmp', cmd_file_name) with open(tmp_cmd_file, 'w') as f: f.write(cmd) self.push(tmp_cmd_file, cmd_file_path) os.remove(tmp_cmd_file) if self.system == SystemType.android: sh.adb('-s', self.address, 'shell', 'sh', cmd_file_path, _fg=True) elif self.system == SystemType.arm_linux: sh.ssh('%s@%s' % (self.username, self.address), 'sh', cmd_file_path, _fg=True) self.rm(cmd_file_path) six.print_('Benchmark done! \n')
def tuning_run(abi, serialno, target_dir, target_name, vlog_level, embed_model_data, model_output_dir, input_nodes, output_nodes, input_shapes, output_shapes, mace_model_dir, model_tag, device_type, running_round, restart_round, limit_opencl_kernel_time, tuning, out_of_range_check, phone_data_dir, model_graph_format, opencl_binary_file, opencl_parameter_file, libmace_dynamic_library_path, omp_num_threads=-1, cpu_affinity_policy=1, gpu_perf_hint=3, gpu_priority_hint=3, input_file_name="model_input", output_file_name="model_out", runtime_failure_ratio=0.0, address_sanitizer=False, link_dynamic=False): print("* Run '%s' with round=%s, restart_round=%s, tuning=%s, " "out_of_range_check=%s, omp_num_threads=%s, cpu_affinity_policy=%s, " "gpu_perf_hint=%s, gpu_priority_hint=%s" % (model_tag, running_round, restart_round, str(tuning), str(out_of_range_check), omp_num_threads, cpu_affinity_policy, gpu_perf_hint, gpu_priority_hint)) mace_model_path = "" if model_graph_format == ModelFormat.file: mace_model_path = "%s/%s.pb" % (mace_model_dir, model_tag) if abi == "host": libmace_dynamic_lib_path = \ os.path.dirname(libmace_dynamic_library_path) p = subprocess.Popen( [ "env", "LD_LIBRARY_PATH=%s" % libmace_dynamic_lib_path, "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUNTIME_FAILURE_RATIO=%f" % runtime_failure_ratio, "%s/%s" % (target_dir, target_name), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (model_output_dir, input_file_name), "--output_file=%s/%s" % (model_output_dir, output_file_name), "--model_data_file=%s/%s.data" % (mace_model_dir, model_tag), "--device=%s" % device_type, "--round=%s" % running_round, "--restart_round=%s" % restart_round, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_path, ], stderr=subprocess.PIPE, stdout=subprocess.PIPE) out, err = p.communicate() stdout = err + out print stdout print("Running finished!\n") else: sh.adb("-s", serialno, "shell", "mkdir", "-p", phone_data_dir) internal_storage_dir = create_internal_storage_dir( serialno, phone_data_dir) for input_name in input_nodes: formatted_name = common.formatted_file_name(input_file_name, input_name) adb_push("%s/%s" % (model_output_dir, formatted_name), phone_data_dir, serialno) if address_sanitizer: adb_push(find_asan_rt_library(abi), phone_data_dir, serialno) if not embed_model_data: adb_push("%s/%s.data" % (mace_model_dir, model_tag), phone_data_dir, serialno) if device_type == common.DeviceType.GPU: if os.path.exists(opencl_binary_file): adb_push(opencl_binary_file, phone_data_dir, serialno) if os.path.exists(opencl_parameter_file): adb_push(opencl_parameter_file, phone_data_dir, serialno) adb_push("third_party/nnlib/libhexagon_controller.so", phone_data_dir, serialno) mace_model_phone_path = "" if model_graph_format == ModelFormat.file: mace_model_phone_path = "%s/%s.pb" % (phone_data_dir, model_tag) adb_push(mace_model_path, mace_model_phone_path, serialno) if link_dynamic: adb_push(libmace_dynamic_library_path, phone_data_dir, serialno) adb_push("%s/%s" % (target_dir, target_name), phone_data_dir, serialno) stdout_buff = [] process_output = make_output_processor(stdout_buff) adb_cmd = [ "LD_LIBRARY_PATH=%s" % phone_data_dir, "MACE_TUNING=%s" % int(tuning), "MACE_OUT_OF_RANGE_CHECK=%s" % int(out_of_range_check), "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUN_PARAMETER_PATH=%s/mace_run.config" % phone_data_dir, "MACE_INTERNAL_STORAGE_PATH=%s" % internal_storage_dir, "MACE_LIMIT_OPENCL_KERNEL_TIME=%s" % limit_opencl_kernel_time, "MACE_RUNTIME_FAILURE_RATIO=%f" % runtime_failure_ratio, ] if address_sanitizer: adb_cmd.extend([ "LD_PRELOAD=%s/%s" % (phone_data_dir, asan_rt_library_names(abi)) ]) adb_cmd.extend([ "%s/%s" % (phone_data_dir, target_name), "--model_name=%s" % model_tag, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (phone_data_dir, input_file_name), "--output_file=%s/%s" % (phone_data_dir, output_file_name), "--model_data_file=%s/%s.data" % (phone_data_dir, model_tag), "--device=%s" % device_type, "--round=%s" % running_round, "--restart_round=%s" % restart_round, "--omp_num_threads=%s" % omp_num_threads, "--cpu_affinity_policy=%s" % cpu_affinity_policy, "--gpu_perf_hint=%s" % gpu_perf_hint, "--gpu_priority_hint=%s" % gpu_priority_hint, "--model_file=%s" % mace_model_phone_path, "--opencl_binary_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_binary_file)), "--opencl_parameter_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_parameter_file)), ]) adb_cmd = ' '.join(adb_cmd) cmd_file_name = "%s-%s-%s" % ('cmd_file', model_tag, str(time.time())) adb_cmd_file = "%s/%s" % (phone_data_dir, cmd_file_name) tmp_cmd_file = "%s/%s" % ('/tmp', cmd_file_name) with open(tmp_cmd_file, 'w') as cmd_file: cmd_file.write(adb_cmd) adb_push(tmp_cmd_file, adb_cmd_file, serialno) os.remove(tmp_cmd_file) sh.adb( "-s", serialno, "shell", "sh", adb_cmd_file, _tty_in=True, _out=process_output, _err_to_out=True) stdout = "".join(stdout_buff) if not stdout_success(stdout): common.MaceLogger.error("Mace Run", "Mace run failed.") sh.adb( "-s", serialno, "shell", "rm", adb_cmd_file, _fg=True) print("Running finished!\n") return stdout
def build_run_throughput_test(abi, serialno, vlog_level, run_seconds, merged_lib_file, model_input_dir, embed_model_data, input_nodes, output_nodes, input_shapes, output_shapes, cpu_model_tag, gpu_model_tag, dsp_model_tag, phone_data_dir, strip="always", input_file_name="model_input"): print("* Build and run throughput_test") model_tag_build_flag = "" if cpu_model_tag: model_tag_build_flag += "--copt=-DMACE_CPU_MODEL_TAG=%s " % \ cpu_model_tag if gpu_model_tag: model_tag_build_flag += "--copt=-DMACE_GPU_MODEL_TAG=%s " % \ gpu_model_tag if dsp_model_tag: model_tag_build_flag += "--copt=-DMACE_DSP_MODEL_TAG=%s " % \ dsp_model_tag sh.cp("-f", merged_lib_file, "mace/benchmark/libmace_merged.a") sh.bazel( "build", "-c", "opt", "--strip", strip, "--verbose_failures", "//mace/benchmark:model_throughput_test", "--crosstool_top=//external:android/crosstool", "--host_crosstool_top=@bazel_tools//tools/cpp:toolchain", "--cpu=%s" % abi, "--copt=-std=c++11", "--copt=-D_GLIBCXX_USE_C99_MATH_TR1", "--copt=-Werror=return-type", "--copt=-O3", "--define", "neon=true", "--define", "openmp=true", model_tag_build_flag, _fg=True) sh.rm("mace/benchmark/libmace_merged.a") sh.adb("-s", serialno, "shell", "mkdir", "-p", phone_data_dir) adb_push("%s/%s_%s" % (model_input_dir, input_file_name, ",".join(input_nodes)), phone_data_dir, serialno) adb_push("bazel-bin/mace/benchmark/model_throughput_test", phone_data_dir, serialno) if not embed_model_data: adb_push("codegen/models/%s/%s.data" % cpu_model_tag, phone_data_dir, serialno) adb_push("codegen/models/%s/%s.data" % gpu_model_tag, phone_data_dir, serialno) adb_push("codegen/models/%s/%s.data" % dsp_model_tag, phone_data_dir, serialno) adb_push("third_party/nnlib/libhexagon_controller.so", phone_data_dir, serialno) sh.adb( "-s", serialno, "shell", "LD_LIBRARY_PATH=%s" % phone_data_dir, "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUN_PARAMETER_PATH=%s/mace_run.config" % phone_data_dir, "%s/model_throughput_test" % phone_data_dir, "--input_node=%s" % ",".join(input_nodes), "--output_node=%s" % ",".join(output_nodes), "--input_shape=%s" % ":".join(input_shapes), "--output_shape=%s" % ":".join(output_shapes), "--input_file=%s/%s" % (phone_data_dir, input_file_name), "--cpu_model_data_file=%s/%s.data" % (phone_data_dir, cpu_model_tag), "--gpu_model_data_file=%s/%s.data" % (phone_data_dir, gpu_model_tag), "--dsp_model_data_file=%s/%s.data" % (phone_data_dir, dsp_model_tag), "--run_seconds=%s" % run_seconds, _fg=True) print("throughput_test done!\n")