diff --git a/docs/user_guide/advanced_usage.rst b/docs/user_guide/advanced_usage.rst index dcef4c6f47a2d8054d998e19717151424f4914e9..c0782ff4b1b2cd082b6f476c6209167a04059875 100644 --- a/docs/user_guide/advanced_usage.rst +++ b/docs/user_guide/advanced_usage.rst @@ -378,6 +378,9 @@ Reduce Library Size - Futher more, if only ``cpu`` device needed, change ``--define opencl=true`` to ``false``. This way will reduce half of library size to about ``700KB`` for ``armeabi-v7a`` and ``1000KB`` for ``arm64-v8a`` + - About ``300KB`` can be reduced when add ``--config symbol_hidden`` building option. It will change + the visibility of inner apis of libmace.so and lead to linking error when load model(s) in ``code``. + * **static library** - The methods in dynamic library can be useful for static library too. In additional, the static diff --git a/mace/codegen/BUILD b/mace/codegen/BUILD index e42a1b13049cc6d24503b2398a1cb19ffcc87a3c..1c76e8dd709343e5cc1e105fac57688a19a991bf 100644 --- a/mace/codegen/BUILD +++ b/mace/codegen/BUILD @@ -44,13 +44,7 @@ cc_library( cc_library( name = "generated_libmace", - srcs = glob(["lib/*.so"]), - visibility = ["//visibility:public"], -) - -cc_library( - name = "generated_libmace_static", - srcs = glob(["lib/*.a"]), + srcs = glob(["lib/*"]), linkstatic = 1, visibility = ["//visibility:public"], ) diff --git a/mace/examples/cli/BUILD b/mace/examples/cli/BUILD index 7767980a533b5291fd906e5b85f8f3f49f6e9910..6599acaef82eb35f7fd2fe7435d1a81fc50c61e5 100644 --- a/mace/examples/cli/BUILD +++ b/mace/examples/cli/BUILD @@ -29,7 +29,7 @@ cc_binary( deps = [ "//external:gflags_nothreads", "//mace/codegen:generated_mace_engine_factory", - "//mace/codegen:generated_libmace_static", + "//mace/codegen:generated_libmace", ] + if_hexagon_enabled([ "//third_party/nnlib:libhexagon", ]), diff --git a/mace/libmace/mace_version_script.lds b/mace/libmace/mace_version_script.lds index 27cc2caa361a930d28c6311a48244f463186f228..76d8f1c2c553e09ac7aaa054999f4af543baa7b1 100644 --- a/mace/libmace/mace_version_script.lds +++ b/mace/libmace/mace_version_script.lds @@ -12,6 +12,20 @@ mace { *SetOpenMPThreadAffinity*; *GetBigLittleCoreIDs*; *MaceVersion*; + + # api for static library of models + *mace*logging*LogMessage*; + *mace*NetDef*; + *mace*MemoryArena*; + *mace*InputInfo*; + *mace*OutputInfo*; + *mace*OutputShape*; + *mace*OperatorDef*; + *mace*ConstTensor*; + *mace*Argument*; + *mace*MemoryBlock*; + *google*protobuf*; + local: *; }; diff --git a/mace/tools/validation/BUILD b/mace/tools/validation/BUILD index 1dad55bca35ad6aaf299e833461819deaa844e59..5a5a8da3ceeb54c652ee05a81018a3774716faa6 100644 --- a/mace/tools/validation/BUILD +++ b/mace/tools/validation/BUILD @@ -42,7 +42,7 @@ cc_binary( deps = [ "//external:gflags_nothreads", "//mace/codegen:generated_mace_engine_factory", + "//mace/codegen:generated_models", "//mace/libmace:libmace_dynamic", - "//mace/utils:utils", ], ) diff --git a/tools/bazel.rc b/tools/bazel.rc index 22357b79d0d76f3a2d628df36d9a23ff31dc74ae..f0907c5230e61365612ed7b48c5d3f37d9f61791 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -11,6 +11,9 @@ build --copt=-D_GLIBCXX_USE_C99_MATH_TR1 build --copt=-DMACE_OBFUSCATE_LITERALS build --copt=-DGEMMLOWP_USE_OPENMP +# Usage example: bazel build --config symbol_hidden +build:symbol_hidden --copt=-fvisibility=hidden + # Usage example: bazel build --config android build:android --crosstool_top=//external:android/crosstool build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain @@ -23,7 +26,6 @@ build:optimization --linkopt=-Wl,--strip-all build:optimization --copt=-ffunction-sections build:optimization --copt=-fdata-sections build:optimization --linkopt=-Wl,--gc-sections -build:optimization --copt=-fvisibility=hidden # Address sanitizer build:asan --strip=never diff --git a/tools/build-standalone-lib.sh b/tools/build-standalone-lib.sh index 59f0b8d8f1a317dff5d181817520cadd177a8245..a08fb3b06c8b1ce3cf66b0f759051145edc82d97 100755 --- a/tools/build-standalone-lib.sh +++ b/tools/build-standalone-lib.sh @@ -44,16 +44,16 @@ fi # build static libraries echo "build static lib for armeabi-v7a + cpu_gpu_dsp" -bazel build --config android --config optimization mace/libmace:libmace_static --define neon=true --define openmp=true --define opencl=true --define hexagon=true --cpu=armeabi-v7a +bazel build --config android --config optimization mace/libmace:libmace_static --config symbol_hidden --define neon=true --define openmp=true --define opencl=true --define hexagon=true --cpu=armeabi-v7a cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/armeabi-v7a/cpu_gpu_dsp/ cp third_party/nnlib/*so $LIB_DIR/armeabi-v7a/cpu_gpu_dsp/ echo "build static lib for armeabi-v7a + cpu_gpu" -bazel build --config android --config optimization mace/libmace:libmace_static --define neon=true --define openmp=true --define opencl=true --cpu=armeabi-v7a +bazel build --config android --config optimization mace/libmace:libmace_static --config symbol_hidden --define neon=true --define openmp=true --define opencl=true --cpu=armeabi-v7a cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/armeabi-v7a/cpu_gpu/ echo "build static lib for arm64-v8a + cpu_gpu" -bazel build --config android --config optimization mace/libmace:libmace_static --define neon=true --define openmp=true --define opencl=true --cpu=arm64-v8a +bazel build --config android --config optimization mace/libmace:libmace_static --config symbol_hidden --define neon=true --define openmp=true --define opencl=true --cpu=arm64-v8a cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/arm64-v8a/cpu_gpu/ if [[ "$OSTYPE" != "darwin"* ]];then diff --git a/tools/converter.py b/tools/converter.py index e78b54bc364fb46ee26449f6fc9f809243d17b80..86b72a9cc69d8b91c9b6e9a5d6fd0e2ce700d1a7 100644 --- a/tools/converter.py +++ b/tools/converter.py @@ -786,7 +786,8 @@ def build_model_lib(configs, address_sanitizer): abi=target_abi, hexagon_mode=hexagon_mode, enable_opencl=get_opencl_mode(configs), - address_sanitizer=address_sanitizer + address_sanitizer=address_sanitizer, + symbol_hidden=True ) sh.cp("-f", MODEL_LIB_PATH, model_lib_output_path) @@ -880,8 +881,10 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer, sh.rm("-rf", build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir) + symbol_hidden = True mace_run_target = MACE_RUN_STATIC_TARGET if mace_lib_type == MACELibType.dynamic: + symbol_hidden = False mace_run_target = MACE_RUN_DYNAMIC_TARGET build_arg = "" if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: @@ -897,6 +900,7 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer, enable_openmp=enable_openmp, enable_opencl=get_opencl_mode(configs), address_sanitizer=address_sanitizer, + symbol_hidden=symbol_hidden, extra_args=build_arg ) sh_commands.update_mace_run_binary(build_tmp_binary_dir, @@ -924,6 +928,7 @@ def build_quantize_stat(configs): quantize_stat_target, abi=ABIType.host, enable_openmp=True, + symbol_hidden=True, extra_args=build_arg ) @@ -943,36 +948,39 @@ def build_example(configs, target_abi, enable_openmp, mace_lib_type): sh.rm("-rf", build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir) + symbol_hidden = True libmace_target = LIBMACE_STATIC_TARGET if mace_lib_type == MACELibType.dynamic: + symbol_hidden = False libmace_target = LIBMACE_SO_TARGET sh_commands.bazel_build(libmace_target, abi=target_abi, enable_openmp=enable_openmp, enable_opencl=get_opencl_mode(configs), - hexagon_mode=hexagon_mode) + hexagon_mode=hexagon_mode, + symbol_hidden=symbol_hidden) if os.path.exists(LIB_CODEGEN_DIR): sh.rm("-rf", LIB_CODEGEN_DIR) sh.mkdir("-p", LIB_CODEGEN_DIR) build_arg = "" + if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: + mace_check(os.path.exists(ENGINE_CODEGEN_DIR), + ModuleName.RUN, + "You should convert model first.") + model_lib_path = get_model_lib_output_path(library_name, + target_abi) + sh.cp("-f", model_lib_path, LIB_CODEGEN_DIR) + build_arg = "--per_file_copt=mace/examples/cli/example.cc@-DMODEL_GRAPH_FORMAT_CODE" # noqa + if mace_lib_type == MACELibType.dynamic: example_target = EXAMPLE_DYNAMIC_TARGET sh.cp("-f", LIBMACE_DYNAMIC_PATH, LIB_CODEGEN_DIR) else: - sh.cp("-f", LIBMACE_STATIC_PATH, LIB_CODEGEN_DIR) - if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: - mace_check(os.path.exists(ENGINE_CODEGEN_DIR), - ModuleName.RUN, - "You should convert model first.") - model_lib_path = get_model_lib_output_path(library_name, - target_abi) - sh.cp("-f", model_lib_path, LIB_CODEGEN_DIR) - build_arg = "--per_file_copt=mace/examples/cli/example.cc@-DMODEL_GRAPH_FORMAT_CODE" # noqa - example_target = EXAMPLE_STATIC_TARGET + sh.cp("-f", LIBMACE_STATIC_PATH, LIB_CODEGEN_DIR) sh_commands.bazel_build(example_target, abi=target_abi, @@ -1325,11 +1333,6 @@ def print_package_summary(package_path): def run_mace(flags): configs = format_model_config(flags) - if flags.mace_lib_type == MACELibType.dynamic and \ - configs[YAMLKeyword.model_graph_format] == ModelFormat.code: - MaceLogger.error(ModuleName.YAML_CONFIG, - "If you want to link MACE dynamic library, " - "you must use file-type MACE model.") clear_build_dirs(configs[YAMLKeyword.library_name]) diff --git a/tools/sh_commands.py b/tools/sh_commands.py index 4969262d55fb9f10d4f44ec5a218fd9e154e2325..f8e09e80cba007a68bda015e7971a0951a9df111 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -279,6 +279,7 @@ def bazel_build(target, enable_neon=True, enable_opencl=True, address_sanitizer=False, + symbol_hidden=False, extra_args=""): print("* Build %s with ABI %s" % (target, abi)) if abi == "host": @@ -307,6 +308,8 @@ def bazel_build(target, bazel_args += ("--config", "asan") else: bazel_args += ("--config", "optimization") + if symbol_hidden: + bazel_args += ("--config", "symbol_hidden") if extra_args: bazel_args += (extra_args, ) print bazel_args