From 0778714407fcd0613838c85ed480e5b9b56f02e8 Mon Sep 17 00:00:00 2001 From: yejianwu Date: Tue, 12 Jun 2018 16:03:02 +0800 Subject: [PATCH] update dynamic link --- .../create_a_model_deployment.rst | 4 +- docs/getting_started/how_to_build.rst | 4 +- docs/getting_started/how_to_build_zh.rst | 6 +- .../models/demo_app_models.yaml | 2 +- mace/tools/validation/BUILD | 4 +- tools/converter.py | 56 +++++++++---------- tools/sh_commands.py | 40 +++++++------ 7 files changed, 62 insertions(+), 54 deletions(-) diff --git a/docs/getting_started/create_a_model_deployment.rst b/docs/getting_started/create_a_model_deployment.rst index 92e969e3..4bcb3416 100644 --- a/docs/getting_started/create_a_model_deployment.rst +++ b/docs/getting_started/create_a_model_deployment.rst @@ -40,8 +40,8 @@ Configurations - Whether embedding model weights as the code, default to 0. * - build_type - model build type, can be ['proto', 'code']. 'proto' for converting model to ProtoBuf file and 'code' for converting model to c++ code. - * - dynamic_link - - [optional] link mace library in dynamic method if set to 1, default to 0. + * - linkshared + - [optional] Use dynamic linking for libmace library when setting to 1, or static linking when setting to 0, default to 0. * - model_name - model name. should be unique if there are multiple models. **LIMIT: if build_type is code, model_name will used in c++ code so that model_name must fulfill c++ name specification.** diff --git a/docs/getting_started/how_to_build.rst b/docs/getting_started/how_to_build.rst index 9f332c0c..364a9466 100644 --- a/docs/getting_started/how_to_build.rst +++ b/docs/getting_started/how_to_build.rst @@ -172,7 +172,7 @@ built-in tool when necessary. 3.1 Overview ----------------- MiAI Compute Engine can build either static or shared library (which is -specified by ``dynamic_link`` in YAML model deployment file). +specified by ``linkshared`` in YAML model deployment file). The followings are two use cases. * **Build well tuned library for specific SoCs** @@ -201,7 +201,7 @@ model conversion, compiling, test run, benchmark and correctness validation. .. note:: 1. ``tools/converter.py`` should be run at the root directory of this project. - 2. When ``dynamic_link`` is set to ``1``, ``build_type`` should be ``proto``. + 2. When ``linkshared`` is set to ``1``, ``build_type`` should be ``proto``. And currently only android devices supported. diff --git a/docs/getting_started/how_to_build_zh.rst b/docs/getting_started/how_to_build_zh.rst index 12df73e3..80d90ab9 100644 --- a/docs/getting_started/how_to_build_zh.rst +++ b/docs/getting_started/how_to_build_zh.rst @@ -170,7 +170,7 @@ Caffe目前只支持最新版本,旧版本请使用Caffe的工具进行升级 3.1 简介 --------------------------------------- -Mace目前提供静态库和动态库(可以在\ ``yaml``\ 文件中通过\ ``dynamic_link``\ 指定),有以下两种使用场景。 +Mace目前提供静态库和动态库(可以在\ ``yaml``\ 文件中通过\ ``linkshared``\ 指定),有以下两种使用场景。 **特定SOC库** @@ -195,7 +195,7 @@ Mace目前提供静态库和动态库(可以在\ ``yaml``\ 文件中通过\ `` .. warning:: 1. 必须在项目的根目录下运行\ ``tools/converter.py``\ 脚本。 - 2. 当\ ``dynamic_link``\ 被设置为1时,\ ``build_type``\ 必需设置为\ ``proto``\ 。当前动态链接的方式只支持安卓设备。 + 2. 当\ ``linkshared``\ 被设置为1时,\ ``build_type``\ 必需设置为\ ``proto``\ 。当前动态链接的方式只支持安卓设备。 --------------------------------------- @@ -331,7 +331,7 @@ Mace目前提供静态库和动态库(可以在\ ``yaml``\ 文件中通过\ `` .. note:: - 当\ ``dynamic_link``\ 设置为1时生成动态链接库。 + 当\ ``linkshared``\ 设置为1时生成动态链接库。 * ``./build/${library_name}/library/${target_abi}/libhexagon_controller.so`` diff --git a/docs/getting_started/models/demo_app_models.yaml b/docs/getting_started/models/demo_app_models.yaml index fb39af92..7523cd51 100644 --- a/docs/getting_started/models/demo_app_models.yaml +++ b/docs/getting_started/models/demo_app_models.yaml @@ -8,7 +8,7 @@ embed_model_data: 1 # The build mode for model(s). # 'code' stand for transfer model(s) into cpp code, 'proto' for model(s) in protobuf file(s). build_type: code -dynamic_link: 1 +linkshared: 1 # One yaml config file can contain multi models' config message. models: model_name: # model tag, which will be used in model loading and must be specific. diff --git a/mace/tools/validation/BUILD b/mace/tools/validation/BUILD index d4ccd447..7004ade7 100644 --- a/mace/tools/validation/BUILD +++ b/mace/tools/validation/BUILD @@ -2,7 +2,7 @@ load("//mace:mace.bzl", "if_openmp_enabled", "if_android") cc_binary( - name = "mace_run", + name = "mace_run_static", srcs = ["mace_run.cc"], copts = if_android([ "-DMACE_ENABLE_OPENCL", @@ -17,7 +17,7 @@ cc_binary( ) cc_binary( - name = "mace_run_deps_so", + name = "mace_run_shared", srcs = ["mace_run.cc"], copts = if_android([ "-DMACE_ENABLE_OPENCL", diff --git a/tools/converter.py b/tools/converter.py index 564425c9..99ee6376 100644 --- a/tools/converter.py +++ b/tools/converter.py @@ -51,8 +51,8 @@ CL_COMPILED_BINARY_FILE_NAME = "mace_cl_compiled_program.bin" CODEGEN_BASE_DIR = 'mace/codegen' MODEL_CODEGEN_DIR = CODEGEN_BASE_DIR + '/models' LIBMACE_SO_TARGET = "//mace:libmace.so" -MACE_RUN_TARGET = "//mace/tools/validation:mace_run" -MACE_RUN_TARGET_DEPS_SO = "//mace/tools/validation:mace_run_deps_so" +MACE_RUN_STATIC_TARGET = "//mace/tools/validation:mace_run_static" +MACE_RUN_SHARED_TARGET = "//mace/tools/validation:mace_run_shared" ALL_SOC_TAG = 'all' ABITypeStrs = [ @@ -126,7 +126,7 @@ class YAMLKeyword(object): target_socs = 'target_socs' build_type = 'build_type' embed_model_data = 'embed_model_data' - dynamic_link = 'dynamic_link' + linkshared = 'linkshared' models = 'models' platform = 'platform' model_file_path = 'model_file_path' @@ -282,19 +282,19 @@ def format_model_config(config_file_path): if build_type == BuildType.proto: configs[YAMLKeyword.embed_model_data] = 0 - dynamic_link = configs.get(YAMLKeyword.dynamic_link, "") - if dynamic_link == "": - configs[YAMLKeyword.dynamic_link] = 0 - dynamic_link = 0 - if not isinstance(dynamic_link, int) or dynamic_link < 0 or \ - dynamic_link > 1: + linkshared = configs.get(YAMLKeyword.linkshared, "") + if linkshared == "": + configs[YAMLKeyword.linkshared] = 0 + linkshared = 0 + if not isinstance(linkshared, int) or linkshared < 0 or \ + linkshared > 1: MaceLogger.error(ModuleName.YAML_CONFIG, - "dynamic_link must be 0 or 1. " + "linkshared must be 0 or 1. " "default is 0, for link mace lib statically, " - "1 for dynamic link.") - if build_type == BuildType.code and dynamic_link == 1: + "1 for dynamic linking.") + if build_type == BuildType.code and linkshared == 1: MaceLogger.error(ModuleName.YAML_CONFIG, - "'dynamic_link == 1' only support when " + "'linkshared == 1' only support when " "'build_type == proto'") model_names = configs.get(YAMLKeyword.models, []) @@ -500,8 +500,8 @@ def print_configuration(flags, configs): configs[YAMLKeyword.build_type]]) data.append([YAMLKeyword.embed_model_data, configs[YAMLKeyword.embed_model_data]]) - data.append([YAMLKeyword.dynamic_link, - configs[YAMLKeyword.dynamic_link]]) + data.append([YAMLKeyword.linkshared, + configs[YAMLKeyword.linkshared]]) data.append(["Tuning", flags.tuning]) MaceLogger.summary(StringFormatter.table(header, data, title)) @@ -651,7 +651,7 @@ def build_specific_lib(target_abi, target_soc, serial_num, library_name = configs[YAMLKeyword.library_name] build_type = configs[YAMLKeyword.build_type] embed_model_data = configs[YAMLKeyword.embed_model_data] - dynamic_link = configs[YAMLKeyword.dynamic_link] + linkshared = configs[YAMLKeyword.linkshared] hexagon_mode = get_hexagon_mode(configs) model_output_dirs = [] @@ -662,10 +662,10 @@ def build_specific_lib(target_abi, target_soc, serial_num, os.makedirs(build_tmp_binary_dir) sh_commands.gen_tuning_param_code(model_output_dirs) - if dynamic_link == 0: - mace_run_target = MACE_RUN_TARGET + if linkshared == 0: + mace_run_target = MACE_RUN_STATIC_TARGET else: - mace_run_target = MACE_RUN_TARGET_DEPS_SO + mace_run_target = MACE_RUN_SHARED_TARGET sh_commands.bazel_build( LIBMACE_SO_TARGET, abi=target_abi, @@ -686,7 +686,7 @@ def build_specific_lib(target_abi, target_soc, serial_num, enable_openmp=enable_openmp, address_sanitizer=address_sanitizer ) - sh_commands.update_mace_run_lib(build_tmp_binary_dir, dynamic_link) + sh_commands.update_mace_run_lib(build_tmp_binary_dir, linkshared) binary_changed = False for model_name in configs[YAMLKeyword.models]: @@ -742,7 +742,7 @@ def build_specific_lib(target_abi, target_soc, serial_num, build_type=build_type, opencl_binary_file="", shared_library_dir=get_shared_library_dir(library_name, target_abi), # noqa - dynamic_link=dynamic_link, + linkshared=linkshared, ) pull_opencl_binary_and_tuning_param(target_abi, serial_num, @@ -765,7 +765,7 @@ def build_specific_lib(target_abi, target_soc, serial_num, enable_openmp=enable_openmp, address_sanitizer=address_sanitizer ) - sh_commands.update_mace_run_lib(build_tmp_binary_dir, dynamic_link) + sh_commands.update_mace_run_lib(build_tmp_binary_dir, linkshared) if target_abi == ABIType.host: sh_commands.build_host_libraries(build_type, target_abi) @@ -774,10 +774,10 @@ def build_specific_lib(target_abi, target_soc, serial_num, sh_commands.build_benchmark_model(target_abi, build_tmp_binary_dir, hexagon_mode, - dynamic_link) + linkshared) # generate library - if dynamic_link == 0: + if linkshared == 0: sh_commands.merge_libs(target_soc, serial_num, target_abi, @@ -913,7 +913,7 @@ def run_specific_target(flags, configs, target_abi, build_type = configs[YAMLKeyword.build_type] embed_model_data = configs[YAMLKeyword.embed_model_data] opencl_output_bin_path = "" - dynamic_link = configs[YAMLKeyword.dynamic_link] + linkshared = configs[YAMLKeyword.linkshared] if not configs[YAMLKeyword.target_socs]: build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi, None, None) @@ -1002,7 +1002,7 @@ def run_specific_target(flags, configs, target_abi, address_sanitizer=flags.address_sanitizer, opencl_binary_file=opencl_output_bin_path, shared_library_dir=get_shared_library_dir(library_name, target_abi), # noqa - dynamic_link=dynamic_link, + linkshared=linkshared, ) if flags.validate: model_file_path, weight_file_path = get_model_files_path( @@ -1062,7 +1062,7 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num): build_type = configs[YAMLKeyword.build_type] embed_model_data = configs[YAMLKeyword.embed_model_data] opencl_output_bin_path = "" - dynamic_link = configs[YAMLKeyword.dynamic_link] + linkshared = configs[YAMLKeyword.linkshared] if not configs[YAMLKeyword.target_socs]: build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi, None, None) @@ -1142,7 +1142,7 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num): gpu_priority_hint=flags.gpu_priority_hint, opencl_binary_file=opencl_output_bin_path, shared_library_dir=get_shared_library_dir(library_name, target_abi), # noqa - dynamic_link=dynamic_link) + linkshared=linkshared) def benchmark_model(flags): diff --git a/tools/sh_commands.py b/tools/sh_commands.py index 3dca3971..1f9158bd 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -561,16 +561,20 @@ def gen_random_input(model_output_dir, sh.cp("-f", input_file_list[i], dst_input_file) -def update_mace_run_lib(build_tmp_binary_dir, dynamic_link=0): - mace_run_filepath = build_tmp_binary_dir + "/mace_run" +def update_mace_run_lib(build_tmp_binary_dir, linkshared=0): + if linkshared == 0: + mace_run_filepath = build_tmp_binary_dir + "/mace_run_static" + else: + mace_run_filepath = build_tmp_binary_dir + "/mace_run_shared" + if os.path.exists(mace_run_filepath): sh.rm("-rf", mace_run_filepath) - if dynamic_link == 0: - sh.cp("-f", "bazel-bin/mace/tools/validation/mace_run", + if linkshared == 0: + sh.cp("-f", "bazel-bin/mace/tools/validation/mace_run_static", build_tmp_binary_dir) else: - sh.cp("-f", "bazel-bin/mace/tools/validation/mace_run_deps_so", - "%s/mace_run" % build_tmp_binary_dir) + sh.cp("-f", "bazel-bin/mace/tools/validation/mace_run_shared", + build_tmp_binary_dir) def touch_tuned_file_flag(build_tmp_binary_dir): @@ -641,7 +645,7 @@ def tuning_run(abi, output_file_name="model_out", runtime_failure_ratio=0.0, address_sanitizer=False, - dynamic_link=0): + 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" % @@ -651,13 +655,17 @@ def tuning_run(abi, 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/mace_run" % mace_run_dir, + "%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), @@ -713,14 +721,14 @@ def tuning_run(abi, mace_model_phone_path, serialno) - if dynamic_link == 1: + 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/mace_run" % mace_run_dir, phone_data_dir, + adb_push("%s/%s" % (mace_run_dir, mace_run_target), phone_data_dir, serialno) stdout_buff = [] @@ -741,7 +749,7 @@ def tuning_run(abi, asan_rt_library_names(abi)) ]) adb_cmd.extend([ - "%s/mace_run" % phone_data_dir, + "%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), @@ -1040,12 +1048,12 @@ def packaging_lib(libmace_output_dir, project_name): def build_benchmark_model(abi, model_output_dir, hexagon_mode, - dynamic_link=False): + linkshared=False): benchmark_binary_file = "%s/benchmark_model" % model_output_dir if os.path.exists(benchmark_binary_file): sh.rm("-rf", benchmark_binary_file) - if dynamic_link == 0: + if linkshared == 0: benchmark_target = "//mace/benchmark:benchmark_model" else: benchmark_target = "//mace/benchmark:benchmark_model_deps_so" @@ -1054,7 +1062,7 @@ def build_benchmark_model(abi, hexagon_mode=hexagon_mode) target_bin = "/".join(bazel_target_to_bin(benchmark_target)) - if dynamic_link == 0: + if linkshared == 0: sh.cp("-f", target_bin, model_output_dir) else: sh.cp("-f", target_bin, "%s/benchmark_model" % model_output_dir) @@ -1082,7 +1090,7 @@ def benchmark_model(abi, gpu_perf_hint=3, gpu_priority_hint=3, input_file_name="model_input", - dynamic_link=0): + linkshared=0): print("* Benchmark for %s" % model_tag) mace_model_path = "" @@ -1132,7 +1140,7 @@ def benchmark_model(abi, mace_model_phone_path, serialno) - if dynamic_link == 1: + if linkshared == 1: adb_push("%s/libmace.so" % shared_library_dir, phone_data_dir, serialno) adb_push("%s/libgnustl_shared.so" % shared_library_dir, -- GitLab