def get_version(flags): device_id = get_cur_device_id(flags) if device_id is not None: android_device = device.create_device(flags.target_abi, device_id) target_props = android_device.info() target_soc = target_props["ro.board.platform"] android_ver = (int)(target_props["ro.build.version.release"]) apu_version = get_apu_version(True, android_ver, target_soc) else: apu_version = 4 MaceLogger.warning("Can not get unique device ID, MACE select the" " latest apu version: %s" % apu_version) sys.exit(apu_version)
def get_cur_device_id(flags): run_devices = device.choose_devices(flags.target_abi, "all") run_device = None device_num = len(run_devices) if device_num == 0: # for CI MaceLogger.warning("No Android devices are plugged in, " "you need to copy `apu` so files by yourself.") elif device_num > 1: # for CI MaceLogger.warning("More than one Android devices are plugged in, " "you need to copy `apu` so files by yourself.") else: run_device = run_devices[0] return run_device
def merge_opencl_binaries(opencl_binaries, output_file): platform_info_key = 'mace_opencl_precompiled_platform_info_key' kvs = {} for binary in opencl_binaries: if not os.path.exists(binary): MaceLogger.warning("OpenCL bin %s not found" % binary) continue with open(binary, "rb") as f: binary_array = np.fromfile(f, dtype=np.uint8) idx = 0 size, = struct.unpack("Q", binary_array[idx:idx + 8]) idx += 8 for _ in range(size): key_size, = struct.unpack("i", binary_array[idx:idx + 4]) idx += 4 key, = struct.unpack( str(key_size) + "s", binary_array[idx:idx + key_size]) idx += key_size value_size, = struct.unpack("i", binary_array[idx:idx + 4]) idx += 4 if key == platform_info_key and key in kvs: mace_check( (kvs[key] == binary_array[idx:idx + value_size]).all(), "There exists more than one OpenCL version for models:" " %s vs %s " % (kvs[key], binary_array[idx:idx + value_size])) else: kvs[key] = binary_array[idx:idx + value_size] idx += value_size output_byte_array = bytearray() data_size = len(kvs) output_byte_array.extend(struct.pack("Q", data_size)) for key, value in kvs.items(): key_size = len(key) output_byte_array.extend(struct.pack("i", key_size)) output_byte_array.extend(struct.pack(str(key_size) + "s", key)) value_size = len(value) output_byte_array.extend(struct.pack("i", value_size)) output_byte_array.extend(value) np.array(output_byte_array).tofile(output_file)
def merge_opencl_parameters(params_files, output_file): kvs = {} for params in params_files: if not os.path.exists(params): MaceLogger.warning("Tune param %s not found" % params) continue with open(params, "rb") as f: binary_array = np.fromfile(f, dtype=np.uint8) idx = 0 size, = struct.unpack("Q", binary_array[idx:idx + 8]) idx += 8 for _ in range(size): key_size, = struct.unpack("i", binary_array[idx:idx + 4]) idx += 4 key, = struct.unpack( str(key_size) + "s", binary_array[idx:idx + key_size]) idx += key_size value_size, = struct.unpack("i", binary_array[idx:idx + 4]) idx += 4 kvs[key] = binary_array[idx:idx + value_size] idx += value_size output_byte_array = bytearray() data_size = len(kvs) output_byte_array.extend(struct.pack("Q", data_size)) for key, value in kvs.items(): key_size = len(key) output_byte_array.extend(struct.pack("i", key_size)) output_byte_array.extend(struct.pack(str(key_size) + "s", key)) value_size = len(value) output_byte_array.extend(struct.pack("i", value_size)) output_byte_array.extend(value) np.array(output_byte_array).tofile(output_file)
def run_model_for_device(flags, args, dev, model_name, model_conf): target_abi = flags.target_abi install_dir = run_target.default_install_dir(target_abi) + "/" + model_name sysdir = install_dir + "/interior" dev.mkdir(sysdir) runtime_list = [] for graph_name, graph_conf in model_conf[ModelKeys.subgraphs].items(): runtime = graph_conf[ModelKeys.runtime] runtime_list.append(runtime) mace_check(runtime != DeviceType.APU or target_abi == "arm64-v8a", "APU runtime does only support arm64-v8a") # install models to devices workdir = flags.output + "/" + model_name model_file = model_name + ".pb" model_data_file = model_name + ".data" model_path = workdir + "/model/" + model_file model_data_path = workdir + "/model/" + model_data_file if os.path.exists(model_path) and os.path.exists(model_data_path): dev.install(Target(model_path), install_dir) dev.install(Target(model_data_path), install_dir) else: MaceLogger.warning("No models exist in %s, use --model_file and" " --model_data_file specified in args" % model_path) if ModelKeys.check_tensors in model_conf: model_conf[ModelKeys.output_tensors] = model_conf[ ModelKeys.check_tensors] model_conf[ModelKeys.output_shapes] = model_conf[ ModelKeys.check_shapes] model_file_path = "" if not flags.gencode_model: model_file_path = install_dir + "/" + model_file model_data_file_path = "" if not flags.gencode_param: model_data_file_path = install_dir + "/" + model_data_file input_tensors_info = config_parser.find_input_tensors_info( model_conf[ModelKeys.subgraphs], model_conf[ModelKeys.input_tensors]) output_tensors_info = config_parser.find_output_tensors_info( model_conf[ModelKeys.subgraphs], model_conf[ModelKeys.output_tensors]) model_args = { "model_name": model_name, "model_file": model_file_path, "model_data_file": model_data_file_path, "input_node": ",".join(model_conf[ModelKeys.input_tensors]), "input_shape": join_2d_array(input_tensors_info[ModelKeys.input_shapes]), "output_node": ",".join(model_conf[ModelKeys.output_tensors]), "output_shape": join_2d_array(output_tensors_info[ModelKeys.output_shapes]), "input_data_format": ",".join([ df.name for df in input_tensors_info[ModelKeys.input_data_formats] ]), "output_data_format": ",".join([ df.name for df in output_tensors_info[ModelKeys.output_data_formats] ]) } opts = [ "--%s='%s'" % (arg_key, arg_val) for arg_key, arg_val in model_args.items() ] + args should_generate_data = (flags.validate or flags.tune or "--benchmark" in opts) if should_generate_data: tmpdirname = tempfile.mkdtemp() input_file_prefix = tmpdirname + "/" + model_name if ModelKeys.validation_inputs_data in model_conf: input_tensor = model_conf[ModelKeys.input_tensors] input_data = model_conf[ModelKeys.validation_inputs_data] mace_check( len(input_tensor) == len(input_data), "len(input_tensor) != len(validate_data") for i in range(len(input_tensor)): util.download_or_get_file( model_conf[ModelKeys.validation_inputs_data][i], "", util.formatted_file_name(input_file_prefix, input_tensor[i])) else: generate_input_data(input_file_prefix, model_conf[ModelKeys.input_tensors], input_tensors_info[ModelKeys.input_shapes], input_tensors_info[ModelKeys.input_ranges], input_tensors_info[ModelKeys.input_data_types]) dev.install(Target(tmpdirname), install_dir + "/validate_in") target_input_file = "%s/validate_in/%s" % (install_dir, model_name) target_output_dir = "%s/validate_out" % install_dir dev.mkdir(target_output_dir) target_output_file = target_output_dir + "/" + model_name opts += [ "--input_file=%s" % target_input_file, "--output_file=%s" % target_output_file ] # run envs = flags.envs.split(" ") + ["MACE_INTERNAL_STORAGE_PATH=%s" % sysdir] if flags.tune: envs += [ "MACE_TUNING=1", "MACE_RUN_PARAMETER_PATH=%s/interior/tune_params" % install_dir ] opts += ["--round=0"] mace_check(flags.vlog_level >= 0, "vlog_level should be greater than zeror") envs += ["MACE_CPP_MIN_VLOG_LEVEL=%s" % flags.vlog_level] build_dir = flags.build_dir + "/" + target_abi libs = [] if DeviceType.HEXAGON in runtime_list: libs += ["third_party/nnlib/%s/libhexagon_controller.so" % target_abi] elif runtime == DeviceType.HTA: libs += ["third_party/hta/%s/libhta_hexagon_runtime.so" % target_abi] elif DeviceType.APU in runtime_list: apu_libs = get_apu_so_paths(dev) libs += apu_libs cpp_shared_lib_path = os.path.join(build_dir, "install/lib/libc++_shared.so") if os.path.exists(cpp_shared_lib_path): libs.append(cpp_shared_lib_path) target = Target(build_dir + "/install/bin/mace_run", libs, opts=opts, envs=envs) run_target.run_target(target_abi, install_dir, target, dev) if DeviceType.GPU in runtime_list: opencl_dir = workdir + "/opencl" util.mkdir_p(opencl_dir) dev.pull( Target(install_dir + "/interior/mace_cl_compiled_program.bin"), "%s/%s_compiled_opencl_kernel.%s.%s.bin" % (opencl_dir, model_name, dev.info()["ro.product.model"].replace( ' ', ''), dev.info()["ro.board.platform"])) if flags.tune: dev.pull( Target(install_dir + "/interior/tune_params"), "%s/%s_tuned_opencl_parameter.%s.%s.bin" % (opencl_dir, model_name, dev.info()["ro.product.model"].replace( ' ', ''), dev.info()["ro.board.platform"])) if flags.validate: validate_model_file = util.download_or_get_model( model_conf[ModelKeys.model_file_path], model_conf[ModelKeys.model_sha256_checksum], tmpdirname) validate_weight_file = "" if ModelKeys.weight_file_path in model_conf: validate_weight_file = util.download_or_get_model( model_conf[ModelKeys.weight_file_path], model_conf[ModelKeys.weight_sha256_checksum], tmpdirname) dev.pull(Target(target_output_dir), tmpdirname + "/validate_out") output_file_prefix = tmpdirname + "/validate_out/" + model_name validate.validate(model_conf[ModelKeys.platform], validate_model_file, validate_weight_file, input_file_prefix, output_file_prefix, input_tensors_info[ModelKeys.input_shapes], output_tensors_info[ModelKeys.output_shapes], input_tensors_info[ModelKeys.input_data_formats], output_tensors_info[ModelKeys.output_data_formats], input_tensors_info[ModelKeys.input_tensors], output_tensors_info[ModelKeys.output_tensors], flags.validate_threshold, input_tensors_info[ModelKeys.input_data_types], flags.backend, "", "") if should_generate_data: shutil.rmtree(tmpdirname)
from __future__ import absolute_import from __future__ import division from __future__ import print_function import os from utils import device from utils.util import MaceLogger cwd = os.path.dirname(__file__) # TODO: Remove bazel deps try: device.execute("bazel version") except: # noqa MaceLogger.warning("No bazel, use cmake.") device.execute("bash tools/cmake/cmake-generate-proto-py-host.sh") else: try: device.execute("bazel build //mace/proto:mace_py") device.execute("cp -f bazel-genfiles/mace/proto/mace_pb2.py %s" % cwd) device.execute("bazel build //mace/proto:micro_mem_py") device.execute("cp -f bazel-genfiles/mace/proto/micro_mem_pb2.py %s" % cwd) device.execute("bazel build //third_party/caffe:caffe_py") device.execute( "cp -f bazel-genfiles/third_party/caffe/caffe_pb2.py %s" % cwd) except: # noqa MaceLogger.error("Failed in proto files' building")
default="arm64-v8a", help="Target ABI: only support arm64-v8a") parser.add_argument("--target_soc", type=str, default="all", help="serialno for adb connection") parser.add_argument("--apu_path", type=str, default="", help="path for storing apu so files on device") return parser.parse_known_args() if __name__ == "__main__": flags, args = parse_args() run_devices = device.choose_devices(flags.target_abi, flags.target_soc) device_num = len(run_devices) if device_num == 0: # for CI MaceLogger.warning("No Android devices are plugged in, " "you need to copy `apu` so files by yourself.") elif device_num > 1: # for CI MaceLogger.warning("More than one Android devices are plugged in, " "you need to copy `apu` so files by yourself.") else: device_id = run_devices[0] android_device = device.create_device(flags.target_abi, device_id) apu_so_paths = get_apu_so_paths(android_device) for apu_so_path in apu_so_paths: device.execute("cp -f %s %s" % (apu_so_path, flags.apu_path), True)