def convert(conf, output): if not os.path.exists(output): os.mkdir(output) for model_name, model_conf in conf["models"].items(): model_output = output + "/" + model_name if not os.path.exists(model_output): os.mkdir(model_output) subgraph = model_conf["subgraphs"][0] del model_conf["subgraphs"] model_conf.update(subgraph) model_file = util.download_or_get_file( model_conf["model_file_path"], model_conf["model_sha256_checksum"], model_output) model_conf["model_file_path"] = model_file if "weight_file_path" in model_conf: weight_file = util.download_or_get_file( model_conf["weight_file_path"], model_conf["weight_sha256_checksum"], model_output) model_conf["weight_file_path"] = weight_file # TODO: remove the following after quantize tool is made if "quantize_range_file" in model_conf: range_file = util.download_or_get_file( model_conf["quantize_range_file"], "", model_output) model_conf["quantize_range_file"] = range_file mace_model = convert_model(model_conf) try: visualizer = visualize_model.ModelVisualizer( model_name, mace_model, model_output) visualizer.save_html() except: # noqa print("Failed to visualize model:", sys.exc_info()[0]) model, params = merge_params(mace_model) output_model_file = model_output + "/" + model_name + ".pb" output_params_file = model_output + "/" + model_name + ".data" with open(output_model_file, "wb") as f: f.write(model.SerializeToString()) with open(output_params_file, "wb") as f: f.write(bytearray(params)) with open(output_model_file + "_txt", "w") as f: f.write(str(model))
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)
def run_model_with_conf(flags, args, model_name, model_conf): target_abi = "host" dev = device.HostDevice("host", target_abi) install_dir = "/tmp/micro_run/" + model_name 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_args = { "model_name": model_name, "input_node": ",".join(model_conf[ModelKeys.input_tensors]), "input_shape": join_2d_array(model_conf[ModelKeys.input_shapes]), "output_node": ",".join(model_conf[ModelKeys.output_tensors]), "output_shape": join_2d_array(model_conf[ModelKeys.output_shapes]), "input_data_format": ",".join([df.name for df in model_conf[ModelKeys.input_data_formats]]), "output_data_format": ",".join([df.name for df in model_conf[ModelKeys.output_data_formats]]) } opts = [ "--%s=%s" % (arg_key, arg_val) for arg_key, arg_val in model_args.items() ] + args # generate data start tmp_dir_name = tempfile.mkdtemp() input_file_prefix = tmp_dir_name + "/" + 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], model_conf[ModelKeys.input_shapes], model_conf[ModelKeys.input_ranges], model_conf[ModelKeys.input_data_types]) dev.install(Target(tmp_dir_name), 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 ] # generate data end envs = [] if flags.vlog_level > 0: envs += ["MACE_CPP_MIN_VLOG_LEVEL=%s" % flags.vlog_level] target = Target("build/micro/host/tools/micro_run_static", [], opts=opts, envs=envs) run_target.run_target(target_abi, install_dir, target, device_ids="host") if flags.validate: validate_model_file = util.download_or_get_model( model_conf[ModelKeys.model_file_path], model_conf[ModelKeys.model_sha256_checksum], tmp_dir_name) 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], tmp_dir_name) dev.pull(Target(target_output_dir), tmp_dir_name + "/validate_out") output_file_prefix = tmp_dir_name + "/validate_out/" + model_name validate.validate( model_conf[ModelKeys.platform], validate_model_file, validate_weight_file, input_file_prefix, output_file_prefix, model_conf[ModelKeys.input_shapes], model_conf[ModelKeys.output_shapes], model_conf[ModelKeys.input_data_formats], model_conf[ModelKeys.output_data_formats], model_conf[ModelKeys.input_tensors], model_conf[ModelKeys.output_tensors], flags.validate_threshold, model_conf[ModelKeys.input_data_types], flags.backend, "", "") shutil.rmtree(tmp_dir_name)