def build_engine(flags): cmake_shell = os.path.abspath(os.path.dirname( __file__)) + "/../cmake/cmake-build-%s.sh" % flags.target_abi os.environ["BUILD_DIR"] = flags.build_dir + "/" + flags.target_abi if flags.runtime: os.environ["RUNTIME"] = config_parser.parse_device_type( flags.runtime).name if flags.gencode_model: os.environ["RUNMODE"] = "code" device.execute("bash " + cmake_shell)
def run_model_for_device(flags, args, dev, model_name, model_conf): runtime = flags.runtime target_abi = flags.target_abi install_dir = run_target.default_install_dir(target_abi) + "/" + model_name sysdir = install_dir + "/interior" dev.mkdir(sysdir) if not runtime: runtime = model_conf[ModelKeys.runtime] if runtime == DeviceType.CPU_GPU: runtime = DeviceType.GPU else: runtime = config_parser.parse_device_type(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 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(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]]), "device": runtime.name } 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], model_conf[ModelKeys.input_shapes], model_conf[ModelKeys.input_ranges], model_conf[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"] if flags.vlog_level > 0: envs += ["MACE_CPP_MIN_VLOG_LEVEL=%s" % flags.vlog_level] build_dir = flags.build_dir + "/" + target_abi libs = [] if model_conf[ModelKeys.runtime] == DeviceType.HEXAGON: libs += ["third_party/nnlib/%s/libhexagon_controller.so" % target_abi] elif model_conf[ModelKeys.runtime] == DeviceType.APU: apu_libs = get_apu_so_paths(dev) libs += apu_libs target = Target(build_dir + "/install/bin/mace_run", libs, opts=opts, envs=envs) run_target.run_target(target_abi, install_dir, target, device_ids=flags.target_socs) if runtime == DeviceType.GPU: 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, 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, "", "") if should_generate_data: shutil.rmtree(tmpdirname)
def convert_func(flags): configs = config_parser.parse(flags.config) print(configs) library_name = configs[YAMLKeyword.library_name] if not os.path.exists(BUILD_OUTPUT_DIR): os.makedirs(BUILD_OUTPUT_DIR) elif os.path.exists(os.path.join(BUILD_OUTPUT_DIR, library_name)): sh.rm("-rf", os.path.join(BUILD_OUTPUT_DIR, library_name)) os.makedirs(os.path.join(BUILD_OUTPUT_DIR, library_name)) if not os.path.exists(BUILD_DOWNLOADS_DIR): os.makedirs(BUILD_DOWNLOADS_DIR) model_output_dir = \ '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_OUTPUT_DIR_NAME) model_header_dir = \ '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_HEADER_DIR_PATH) # clear output dir if os.path.exists(model_output_dir): sh.rm("-rf", model_output_dir) os.makedirs(model_output_dir) if os.path.exists(model_header_dir): sh.rm("-rf", model_header_dir) if os.path.exists(MODEL_CODEGEN_DIR): sh.rm("-rf", MODEL_CODEGEN_DIR) if os.path.exists(ENGINE_CODEGEN_DIR): sh.rm("-rf", ENGINE_CODEGEN_DIR) if flags.model_data_format: model_data_format = flags.model_data_format else: model_data_format = configs.get(YAMLKeyword.model_data_format, "file") embed_model_data = model_data_format == ModelFormat.code if flags.model_graph_format: model_graph_format = flags.model_graph_format else: model_graph_format = configs.get(YAMLKeyword.model_graph_format, "file") if model_graph_format == ModelFormat.code: os.makedirs(model_header_dir) sh_commands.gen_mace_engine_factory_source( configs[YAMLKeyword.models].keys(), embed_model_data) sh.cp("-f", glob.glob("mace/codegen/engine/*.h"), model_header_dir) convert.convert(configs, MODEL_CODEGEN_DIR) for model_name, model_config in configs[YAMLKeyword.models].items(): model_codegen_dir = "%s/%s" % (MODEL_CODEGEN_DIR, model_name) encrypt.encrypt( model_name, "%s/model/%s.pb" % (model_codegen_dir, model_name), "%s/model/%s.data" % (model_codegen_dir, model_name), config_parser.parse_device_type(model_config[YAMLKeyword.runtime]), model_codegen_dir, bool(model_config.get(YAMLKeyword.obfuscate, 1)), model_graph_format == "code", model_data_format == "code") if model_graph_format == ModelFormat.file: sh.mv("-f", '%s/model/%s.pb' % (model_codegen_dir, model_name), model_output_dir) sh.mv("-f", '%s/model/%s.data' % (model_codegen_dir, model_name), model_output_dir) else: if not embed_model_data: sh.mv("-f", '%s/model/%s.data' % (model_codegen_dir, model_name), model_output_dir) sh.cp("-f", glob.glob("mace/codegen/models/*/code/*.h"), model_header_dir) MaceLogger.summary( StringFormatter.block("Model %s converted" % model_name)) if model_graph_format == ModelFormat.code: build_model_lib(configs, flags.address_sanitizer, flags.debug_mode) print_library_summary(configs)
def convert_func(flags): configs = config_parser.parse(flags.config) print(configs) library_name = configs[YAMLKeyword.library_name] if not os.path.exists(BUILD_OUTPUT_DIR): os.makedirs(BUILD_OUTPUT_DIR) elif os.path.exists(os.path.join(BUILD_OUTPUT_DIR, library_name)): sh.rm("-rf", os.path.join(BUILD_OUTPUT_DIR, library_name)) os.makedirs(os.path.join(BUILD_OUTPUT_DIR, library_name)) if not os.path.exists(BUILD_DOWNLOADS_DIR): os.makedirs(BUILD_DOWNLOADS_DIR) model_output_dir = \ '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_OUTPUT_DIR_NAME) model_header_dir = \ '%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, MODEL_HEADER_DIR_PATH) # clear output dir if os.path.exists(model_output_dir): sh.rm("-rf", model_output_dir) os.makedirs(model_output_dir) if os.path.exists(model_header_dir): sh.rm("-rf", model_header_dir) if os.path.exists(MODEL_CODEGEN_DIR): sh.rm("-rf", MODEL_CODEGEN_DIR) if os.path.exists(ENGINE_CODEGEN_DIR): sh.rm("-rf", ENGINE_CODEGEN_DIR) if flags.quantize_stat: configs[YAMLKeyword.quantize_stat] = flags.quantize_stat if flags.model_data_format: model_data_format = flags.model_data_format else: model_data_format = configs.get(YAMLKeyword.model_data_format, "file") embed_model_data = model_data_format == ModelFormat.code if flags.model_graph_format: model_graph_format = flags.model_graph_format else: model_graph_format = configs.get(YAMLKeyword.model_graph_format, "file") embed_graph_def = model_graph_format == ModelFormat.code if flags.enable_micro: mace_check((not embed_model_data) and (not embed_graph_def), ModuleName.YAML_CONFIG, "You should specify file mode to convert micro model.") if embed_graph_def: os.makedirs(model_header_dir) sh_commands.gen_mace_engine_factory_source( configs[YAMLKeyword.models].keys(), embed_model_data) sh.cp("-f", glob.glob("mace/codegen/engine/*.h"), model_header_dir) convert.convert(configs, MODEL_CODEGEN_DIR, flags.enable_micro) for model_name, model_config in configs[YAMLKeyword.models].items(): if flags.enable_micro: data_type = model_config.get(YAMLKeyword.data_type, "") mace_check( data_type == FPDataType.fp32_fp32.value or data_type == FPDataType.bf16_fp32.value, ModuleName.YAML_CONFIG, "You should specify fp32_fp32 or bf16_fp32 data type " "to convert micro model.") model_codegen_dir = "%s/%s" % (MODEL_CODEGEN_DIR, model_name) encrypt.encrypt( model_name, "%s/model/%s.pb" % (model_codegen_dir, model_name), "%s/model/%s.data" % (model_codegen_dir, model_name), config_parser.parse_device_type(model_config[YAMLKeyword.runtime]), model_codegen_dir, bool(model_config.get(YAMLKeyword.obfuscate, 1)), model_graph_format == "code", model_data_format == "code") if model_graph_format == ModelFormat.file: sh.mv("-f", '%s/model/%s.pb' % (model_codegen_dir, model_name), model_output_dir) sh.mv("-f", '%s/model/%s.data' % (model_codegen_dir, model_name), model_output_dir) if flags.enable_micro: sh.mv( "-f", '%s/model/%s_micro.tar.gz' % (model_codegen_dir, model_name), model_output_dir) else: if not embed_model_data: sh.mv("-f", '%s/model/%s.data' % (model_codegen_dir, model_name), model_output_dir) sh.cp("-f", glob.glob("mace/codegen/models/*/code/*.h"), model_header_dir) MaceLogger.summary( StringFormatter.block("Model %s converted" % model_name)) if model_graph_format == ModelFormat.code: build_model_lib(configs, flags.address_sanitizer, flags.debug_mode) print_library_summary(configs)
for name, model_conf in conf["models"].items(): model_conf = config_parser.normalize_model_config(model_conf) if not flags.model_name or name == flags.model_name: MaceLogger.info("Encrypt model %s" % name) encrypt(name, "build/%s/model/%s.pb" % (name, name), "build/%s/model/%s.data" % (name, name), model_conf[ModelKeys.runtime], codegen_dir + "/" + name, not flags.no_obfuscate, flags.gencode_model, flags.gencode_param) models.append(name) os.rename("%s/%s/%s.pb" % (codegen_dir, name, name), "build/%s/model/%s.pb" % (name, name)) os.rename("%s/%s/%s.data" % (codegen_dir, name, name), "build/%s/model/%s.data" % (name, name)) else: device_type = config_parser.parse_device_type(flags.device) encrypt(flags.model_name, flags.model_file, flags.params_file, device_type, codegen_dir, not flags.no_obfuscate, flags.gencode_model, flags.gencode_param) models.append(flags.model_name) os.rename( "%s/%s/%s.pb" % (codegen_dir, flags.model_name, flags.model_name), "build/%s/model/%s.pb" % (flags.model_name, flags.model_name)) os.rename( "%s/%s/%s.data" % (codegen_dir, flags.model_name, flags.model_name), "build/%s/model/%s.data" % (flags.model_name, flags.model_name)) if flags.gencode_model: gen_mace_engine_factory(models, flags.gencode_param, "mace/codegen/engine")