From bb10ae3b2683c4073091250a70b2562b5fa51fd8 Mon Sep 17 00:00:00 2001 From: liuqi Date: Thu, 5 Jul 2018 18:02:40 +0800 Subject: [PATCH] Add mace static target for bazel build. --- WORKSPACE | 7 ++ mace/BUILD | 37 +++++++++- mace/benchmark/benchmark_model.cc | 5 ++ mace/codegen/BUILD | 14 ++-- mace/core/BUILD | 1 - mace/core/mace_runtime.cc | 6 ++ mace/examples/cli/BUILD | 2 +- mace/examples/cli/example.cc | 5 ++ mace/mace.bzl | 18 +++++ mace/public/mace_runtime.h | 6 ++ mace/python/tools/BUILD | 7 ++ mace/python/tools/archive_static_lib.py | 40 +++++++++++ mace/python/tools/encrypt_opencl_codegen.py | 18 ++++- mace/tools/git/gen_version_source.sh | 13 ++++ mace/tools/validation/mace_run.cc | 5 ++ mace/utils/BUILD | 1 - mace/utils/tuner.h | 29 ++++++-- tools/bazel.rc | 1 + tools/converter.py | 37 +++++++++- tools/sh_commands.py | 77 +++++++++++++++++---- 20 files changed, 293 insertions(+), 36 deletions(-) create mode 100644 mace/python/tools/archive_static_lib.py diff --git a/WORKSPACE b/WORKSPACE index e62557bd..841d0cbd 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,5 +1,12 @@ workspace(name = "mace") +# generate version and opencl kernel code. +load("//repository/git:git_configure.bzl", "git_version_repository") +load("//repository/opencl-kernel:opencl_kernel_configure.bzl", "encrypt_opencl_kernel_repository") + +git_version_repository(name="local_version_config") +encrypt_opencl_kernel_repository(name="local_opencl_kernel_encrypt") + # proto_library rules implicitly depend on @com_google_protobuf//:protoc, # which is the proto-compiler. # This statement defines the @com_google_protobuf repo. diff --git a/mace/BUILD b/mace/BUILD index ebc0e365..6ceb1329 100644 --- a/mace/BUILD +++ b/mace/BUILD @@ -73,8 +73,41 @@ cc_library( visibility = ["//visibility:public"], ) -cc_library( +genrule( name = "libmace_static", - srcs = ["libmace.a"], + srcs = [ + "//mace/codegen:generated_opencl", + "//mace/codegen:generated_version", + "//mace/core", + "//mace/kernels", + "//mace/ops", + "//mace/utils", + "//mace/proto:mace_cc", + "@com_google_protobuf//:protobuf_lite", + ], + outs = ["libmace.a"], + cmd = "tmp_mri_file=$$(mktemp mace-static-lib-mri.XXXXXXXXXX);" + + "mri_stream=$$(python $(location //mace/python/tools:archive_static_lib) " + + "$(locations //mace/codegen:generated_opencl) " + + "$(locations //mace/codegen:generated_version) " + + "$(locations //mace/core:core) " + + "$(locations //mace/kernels:kernels) " + + "$(locations //mace/ops:ops) " + + "$(locations //mace/utils:utils) " + + "$(locations //mace/proto:mace_cc) " + + "$(locations @com_google_protobuf//:protobuf_lite) " + + "$@ " + + "$$tmp_mri_file);" + + "$(AR) -M <$$tmp_mri_file;" + + "rm -rf $$tmp_mri_file;", + tools = ["//mace/python/tools:archive_static_lib"], visibility = ["//visibility:public"], ) + +cc_library( + name = "libmace_static_lib", + srcs = [":libmace_static"], + linkstatic = 1, + visibility = ["//visibility:public"], +) + diff --git a/mace/benchmark/benchmark_model.cc b/mace/benchmark/benchmark_model.cc index 5be6d777..338317e1 100644 --- a/mace/benchmark/benchmark_model.cc +++ b/mace/benchmark/benchmark_model.cc @@ -191,6 +191,9 @@ DEFINE_int32(warmup_runs, 1, "how many runs to initialize model"); DEFINE_string(opencl_binary_file, "", "compiled opencl binary file path"); +DEFINE_string(opencl_parameter_file, + "", + "tuned OpenCL parameter file path"); DEFINE_string(model_data_file, "", "model data file name, used when EMBED_MODEL_DATA set to 0"); DEFINE_string(model_file, "", @@ -264,6 +267,8 @@ int Main(int argc, char **argv) { std::vector opencl_binary_paths = {FLAGS_opencl_binary_file}; mace::SetOpenCLBinaryPaths(opencl_binary_paths); + + mace::SetOpenCLParameterPath(FLAGS_opencl_parameter_file); } #endif // MACE_ENABLE_OPENCL diff --git a/mace/codegen/BUILD b/mace/codegen/BUILD index a38f3295..52da986c 100644 --- a/mace/codegen/BUILD +++ b/mace/codegen/BUILD @@ -5,6 +5,8 @@ package( default_visibility = ["//visibility:public"], ) +load("//mace:mace.bzl", "mace_version_genrule", "encrypt_opencl_kernel_genrule") + cc_library( name = "generated_models", srcs = glob(["models/*/*.cc"]), @@ -16,15 +18,13 @@ cc_library( ], ) -cc_library( - name = "generated_opencl", - srcs = glob(["opencl/*.cc"]), - copts = ["-Werror", "-Wextra", "-Wno-missing-field-initializers"], -) +mace_version_genrule() + +encrypt_opencl_kernel_genrule() cc_library( - name = "generated_tuning_params", - srcs = ["tuning/tuning_params.cc"], + name = "generated_opencl", + srcs = ["opencl/encrypt_opencl_kernel.cc"], copts = ["-Werror", "-Wextra", "-Wno-missing-field-initializers"], ) diff --git a/mace/core/BUILD b/mace/core/BUILD index c819d800..8e3fb50f 100644 --- a/mace/core/BUILD +++ b/mace/core/BUILD @@ -61,7 +61,6 @@ cc_library( ]), deps = [ "//mace/codegen:generated_version", - "//mace/codegen:generated_tuning_params", "//mace/proto:mace_cc", "//mace/utils", ] + if_android([ diff --git a/mace/core/mace_runtime.cc b/mace/core/mace_runtime.cc index 8fe2b6c3..a93709fb 100644 --- a/mace/core/mace_runtime.cc +++ b/mace/core/mace_runtime.cc @@ -24,4 +24,10 @@ void SetKVStorageFactory(std::shared_ptr storage_factory) { kStorageFactory = storage_factory; } +std::string kOpenCLParameterPath; + +void SetOpenCLParameterPath(const std::string &path) { + kOpenCLParameterPath = path; +} + }; // namespace mace diff --git a/mace/examples/cli/BUILD b/mace/examples/cli/BUILD index 95086ad7..e3507aed 100644 --- a/mace/examples/cli/BUILD +++ b/mace/examples/cli/BUILD @@ -23,7 +23,7 @@ cc_binary( deps = [ "//external:gflags_nothreads", "//mace/codegen:generated_mace_engine_factory", - "//mace:libmace_static", + "//mace:libmace_static_lib", ] + if_hexagon_enabled([ "//third_party/nnlib:libhexagon", ]), diff --git a/mace/examples/cli/example.cc b/mace/examples/cli/example.cc index 32373058..38c09e06 100644 --- a/mace/examples/cli/example.cc +++ b/mace/examples/cli/example.cc @@ -108,6 +108,9 @@ DEFINE_string(output_file, DEFINE_string(opencl_binary_file, "", "compiled opencl binary file path"); +DEFINE_string(opencl_parameter_file, + "", + "tuned OpenCL parameter file path"); DEFINE_string(model_data_file, "", "model data file name, used when EMBED_MODEL_DATA set to 0"); @@ -172,6 +175,8 @@ bool RunModel(const std::vector &input_names, // you should update the binary when OpenCL Driver changed. std::vector opencl_binary_paths = {FLAGS_opencl_binary_file}; mace::SetOpenCLBinaryPaths(opencl_binary_paths); + + mace::SetOpenCLParameterPath(FLAGS_opencl_parameter_file); } #endif // MACE_ENABLE_OPENCL diff --git a/mace/mace.bzl b/mace/mace.bzl index 9e81ee40..506f1dba 100644 --- a/mace/mace.bzl +++ b/mace/mace.bzl @@ -47,3 +47,21 @@ def if_openmp_enabled(a): "//mace:openmp_enabled": a, "//conditions:default": [], }) + + +def mace_version_genrule(): + native.genrule( + name = "mace_version_gen", + srcs = [str(Label("@local_version_config//:gen/version"))], + outs = ["version/version.cc"], + cmd = "cat $(SRCS) > $@;" + ) + +def encrypt_opencl_kernel_genrule(): + native.genrule( + name = "encrypt_opencl_kernel_gen", + srcs = [str(Label("@local_opencl_kernel_encrypt//:gen/encrypt_opencl_kernel"))], + outs = ["opencl/encrypt_opencl_kernel.cc"], + cmd = "cat $(SRCS) > $@;" + ) + diff --git a/mace/public/mace_runtime.h b/mace/public/mace_runtime.h index 7f8b0576..1f92927a 100644 --- a/mace/public/mace_runtime.h +++ b/mace/public/mace_runtime.h @@ -90,6 +90,12 @@ void SetKVStorageFactory(std::shared_ptr storage_factory); __attribute__((visibility("default"))) void SetOpenCLBinaryPaths(const std::vector &paths); +// Just call once. (Not thread-safe) +// Set the path of Generated OpenCL parameter file if you use gpu of specific soc. +// The parameters is the local work group size tuned for specific SOC, which +// may be faster than the general parameters. +void SetOpenCLParameterPath(const std::string &path); + // Set GPU hints, currently only supports Adreno GPU. // // Caution: this function may hurt performance if improper parameters provided. diff --git a/mace/python/tools/BUILD b/mace/python/tools/BUILD index bcbe98e0..f7ccc9dc 100644 --- a/mace/python/tools/BUILD +++ b/mace/python/tools/BUILD @@ -48,3 +48,10 @@ py_binary( "@six_archive//:six", ], ) + +py_binary( + name = "archive_static_lib", + srcs = ["archive_static_lib.py"], + srcs_version = "PY2AND3", + visibility = ["//visibility:public"], +) diff --git a/mace/python/tools/archive_static_lib.py b/mace/python/tools/archive_static_lib.py new file mode 100644 index 00000000..cc788ef7 --- /dev/null +++ b/mace/python/tools/archive_static_lib.py @@ -0,0 +1,40 @@ +# Copyright 2018 Xiaomi, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +import os + +# python encrypt_opencl_codegen.py --cl_kernel_dir=./mace/kernels/opencl/cl/ \ +# --output_path=./mace/codegen/opencl_encrypt/opencl_encrypted_program.cc + +def is_static_lib(lib_name): + return lib_name.endswith('.a') or lib_name.endswith('.lo') + +def merge_libs(input_libs, + output_lib_path, + mri_script): + # make static library + mri_stream = "" + mri_stream += "create %s\n" % output_lib_path + for lib in input_libs: + if is_static_lib(lib): + mri_stream += ("addlib %s\n" % lib) + mri_stream += "save\n" + mri_stream += "end\n" + with open(mri_script, 'w') as tmp: + tmp.write(mri_stream) + + +if __name__ == '__main__': + merge_libs(sys.argv[1:-2], sys.argv[-2], sys.argv[-1]) diff --git a/mace/python/tools/encrypt_opencl_codegen.py b/mace/python/tools/encrypt_opencl_codegen.py index 38e3fea9..cfb9c91e 100644 --- a/mace/python/tools/encrypt_opencl_codegen.py +++ b/mace/python/tools/encrypt_opencl_codegen.py @@ -14,6 +14,7 @@ import argparse import os +import shutil import sys import jinja2 @@ -68,8 +69,21 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path): data_type='unsigned char', variable_name='kEncryptedProgramMap') - if os.path.isfile(output_path): - os.remove(output_path) + output_dir = os.path.dirname(output_path) + if os.path.exists(output_dir): + if os.path.isdir(output_dir): + try: + shutil.rmtree(output_dir) + except OSError: + raise RuntimeError( + "Cannot delete directory %s due to permission " + "error, inspect and remove manually" % output_dir) + else: + raise RuntimeError( + "Cannot delete non-directory %s, inspect ", + "and remove manually" % output_dir) + os.makedirs(output_dir) + with open(output_path, "w") as w_file: w_file.write(cpp_cl_encrypted_kernel) diff --git a/mace/tools/git/gen_version_source.sh b/mace/tools/git/gen_version_source.sh index ac6b70c1..833e4c50 100644 --- a/mace/tools/git/gen_version_source.sh +++ b/mace/tools/git/gen_version_source.sh @@ -13,12 +13,23 @@ # See the License for the specific language governing permissions and # limitations under the License. +MACE_SOURCE_DIR=$(dirname $0) + OUTPUT_FILENAME=$1 if [[ -z "${OUTPUT_FILENAME}}" ]]; then echo "Usage: $0 " exit 1 fi +OUTPUT_DIR=$(dirname $OUTPUT_FILENAME) +if [ -d $OUTPUT_DIR ]; then + rm -rf $OUTPUT_DIR +fi + +mkdir -p $OUTPUT_DIR + +pushd $MACE_SOURCE_DIR + DATE_STR=$(date +%Y%m%d) GIT_VERSION=$(git describe --long --tags) if [[ $? != 0 ]]; then @@ -49,3 +60,5 @@ __attribute__((visibility ("default"))) const char *MaceVersion() { return "MACEVER-${GIT_VERSION}" + 8; } } // namespace mace EOF + +popd diff --git a/mace/tools/validation/mace_run.cc b/mace/tools/validation/mace_run.cc index cba3a926..1f7acd9b 100644 --- a/mace/tools/validation/mace_run.cc +++ b/mace/tools/validation/mace_run.cc @@ -175,6 +175,9 @@ DEFINE_string(output_file, DEFINE_string(opencl_binary_file, "", "compiled opencl binary file path"); +DEFINE_string(opencl_parameter_file, + "", + "tuned OpenCL parameter file path"); DEFINE_string(model_data_file, "", "model data file name, used when EMBED_MODEL_DATA set to 0 or 2"); @@ -209,6 +212,8 @@ bool RunModel(const std::string &model_name, std::vector opencl_binary_paths = {FLAGS_opencl_binary_file}; mace::SetOpenCLBinaryPaths(opencl_binary_paths); + + mace::SetOpenCLParameterPath(FLAGS_opencl_parameter_file); } #endif // MACE_ENABLE_OPENCL diff --git a/mace/utils/BUILD b/mace/utils/BUILD index 69794347..8b5f362b 100644 --- a/mace/utils/BUILD +++ b/mace/utils/BUILD @@ -31,7 +31,6 @@ cc_library( copts = ["-Werror", "-Wextra", "-Wno-missing-field-initializers"], deps = [ "//mace/public", - "//mace/codegen:generated_tuning_params", ], ) diff --git a/mace/utils/tuner.h b/mace/utils/tuner.h index e2a83aac..6df14bdd 100644 --- a/mace/utils/tuner.h +++ b/mace/utils/tuner.h @@ -117,13 +117,28 @@ class Tuner { } inline void ReadRunParamters() { - extern const std::map> - kTuningParamsData; - if (!kTuningParamsData.empty()) { - for (auto it = kTuningParamsData.begin(); it != kTuningParamsData.end(); - ++it) { - param_table_.emplace(it->first, std::vector( - it->second.begin(), it->second.end())); + extern std::string kOpenCLParameterPath; + if (!kOpenCLParameterPath.empty()) { + std::ifstream ifs(kOpenCLParameterPath, std::ios::binary | std::ios::in); + if (ifs.is_open()) { + int64_t num_params = 0; + ifs.read(reinterpret_cast(&num_params), sizeof(num_params)); + while (num_params--) { + int32_t key_size = 0; + ifs.read(reinterpret_cast(&key_size), sizeof(key_size)); + std::string key(key_size, ' '); + ifs.read(&key[0], key_size); + + int32_t params_size = 0; + ifs.read(reinterpret_cast(¶ms_size), sizeof(params_size)); + int32_t params_count = params_size / sizeof(unsigned int); + std::vector params(params_count); + for (int i = 0; i < params_count; ++i) { + ifs.read(reinterpret_cast(¶ms[i]), sizeof(unsigned int)); + } + param_table_.emplace(key, params); + } + ifs.close(); } } else { LOG(INFO) << "There is no tuned parameters."; diff --git a/tools/bazel.rc b/tools/bazel.rc index d5621063..aeaee687 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -9,6 +9,7 @@ build --verbose_failures build --copt=-std=c++11 build --copt=-D_GLIBCXX_USE_C99_MATH_TR1 build --copt=-DMACE_OBFUSCATE_LITERALS +build --define openmp=true # Usage example: bazel build --config android build:android --crosstool_top=//external:android/crosstool diff --git a/tools/converter.py b/tools/converter.py index 2e2d62be..86a11083 100644 --- a/tools/converter.py +++ b/tools/converter.py @@ -53,7 +53,9 @@ BUILD_TMP_GENERAL_OUTPUT_DIR_NAME = 'general' OUTPUT_LIBRARY_DIR_NAME = 'lib' OUTPUT_OPENCL_BINARY_DIR_NAME = 'opencl' OUTPUT_OPENCL_BINARY_FILE_NAME = 'compiled_opencl_kernel' +OUTPUT_OPENCL_PARAMETER_FILE_NAME = 'tuned_opencl_parameter' CL_COMPILED_BINARY_FILE_NAME = "mace_cl_compiled_program.bin" +CL_TUNED_PARAMETER_FILE_NAME = "mace_run.config" CODEGEN_BASE_DIR = 'mace/codegen' MODEL_CODEGEN_DIR = CODEGEN_BASE_DIR + '/models' LIBMACE_SO_TARGET = "//mace:libmace.so" @@ -505,6 +507,21 @@ def get_opencl_binary_output_path(library_name, target_abi, target_soc) +def get_opencl_parameter_output_path(library_name, target_abi, + target_soc, serial_num): + device_name = \ + sh_commands.adb_get_device_name_by_serialno(serial_num) + return '%s/%s/%s/%s/%s_%s.%s.%s.bin' % \ + (BUILD_OUTPUT_DIR, + library_name, + OUTPUT_OPENCL_BINARY_DIR_NAME, + target_abi, + library_name, + OUTPUT_OPENCL_PARAMETER_FILE_NAME, + device_name, + target_soc) + + def get_shared_library_dir(library_name, abi): return '%s/%s/%s/%s' % (BUILD_OUTPUT_DIR, library_name, @@ -689,7 +706,6 @@ def build_specific_lib(target_abi, target_soc, serial_num, sh.rm("-rf", build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir) - sh_commands.gen_tuning_param_code(model_output_dirs) if linkshared == 0: mace_run_name = MACE_RUN_STATIC_NAME mace_run_target = MACE_RUN_STATIC_TARGET @@ -773,6 +789,7 @@ def build_specific_lib(target_abi, target_soc, serial_num, phone_data_dir=PHONE_DATA_DIR, build_type=build_type, opencl_binary_file="", + opencl_parameter_file="", shared_library_dir=get_shared_library_dir(library_name, target_abi), # noqa linkshared=linkshared, ) @@ -786,10 +803,15 @@ def build_specific_lib(target_abi, target_soc, serial_num, opencl_output_bin_path = get_opencl_binary_output_path( library_name, target_abi, target_soc, serial_num ) + opencl_parameter_bin_path = get_opencl_parameter_output_path( + library_name, target_abi, target_soc, serial_num + ) sh_commands.merge_opencl_binaries( model_output_dirs, CL_COMPILED_BINARY_FILE_NAME, opencl_output_bin_path) - sh_commands.gen_tuning_param_code(model_output_dirs) + sh_commands.merge_opencl_parameters( + model_output_dirs, CL_TUNED_PARAMETER_FILE_NAME, + opencl_parameter_bin_path) sh_commands.bazel_build( mace_run_target, abi=target_abi, @@ -950,6 +972,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 = "" + opencl_parameter_path = "" linkshared = configs[YAMLKeyword.linkshared] if not configs[YAMLKeyword.target_socs] or target_abi == ABIType.host: build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi, @@ -960,6 +983,10 @@ def run_specific_target(flags, configs, target_abi, opencl_output_bin_path = get_opencl_binary_output_path( library_name, target_abi, target_soc, serial_num ) + opencl_parameter_path = get_opencl_parameter_output_path( + library_name, target_abi, target_soc, serial_num + ) + mace_check(os.path.exists(build_tmp_binary_dir), ModuleName.RUN, 'You should build before run.') @@ -1051,6 +1078,7 @@ def run_specific_target(flags, configs, target_abi, runtime_failure_ratio=flags.runtime_failure_ratio, address_sanitizer=flags.address_sanitizer, opencl_binary_file=opencl_output_bin_path, + opencl_parameter_file=opencl_parameter_path, shared_library_dir=get_shared_library_dir(library_name, target_abi), # noqa linkshared=linkshared, ) @@ -1114,6 +1142,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 = "" + opencl_parameter_path = "" linkshared = configs[YAMLKeyword.linkshared] if not configs[YAMLKeyword.target_socs] or target_abi == ABIType.host: build_tmp_binary_dir = get_build_binary_dir(library_name, target_abi, @@ -1124,6 +1153,9 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num): opencl_output_bin_path = get_opencl_binary_output_path( library_name, target_abi, target_soc, serial_num ) + opencl_parameter_path = get_opencl_parameter_output_path( + library_name, target_abi, target_soc, serial_num + ) mace_check(os.path.exists(build_tmp_binary_dir), ModuleName.BENCHMARK, 'You should build before benchmark.') @@ -1194,6 +1226,7 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num): gpu_perf_hint=flags.gpu_perf_hint, gpu_priority_hint=flags.gpu_priority_hint, opencl_binary_file=opencl_output_bin_path, + opencl_parameter_file=opencl_parameter_path, shared_library_dir=get_shared_library_dir(library_name, target_abi), # noqa linkshared=linkshared) diff --git a/tools/sh_commands.py b/tools/sh_commands.py index ddbd7c6b..cff2c5aa 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -454,6 +454,54 @@ def merge_opencl_binaries(binaries_dirs, np.array(output_byte_array).tofile(output_file_path) +def merge_opencl_parameters(binaries_dirs, + cl_parameter_file_name, + output_file_path): + cl_bin_dirs = [] + for d in binaries_dirs: + cl_bin_dirs.append(os.path.join(d, "opencl_bin")) + # create opencl binary output dir + opencl_binary_dir = os.path.dirname(output_file_path) + if not os.path.exists(opencl_binary_dir): + sh.mkdir("-p", opencl_binary_dir) + kvs = {} + for binary_dir in cl_bin_dirs: + binary_path = os.path.join(binary_dir, cl_parameter_file_name) + if not os.path.exists(binary_path): + continue + + print 'generate opencl parameter from', binary_path + with open(binary_path, "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 xrange(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.iteritems(): + 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_path) + + def gen_tuning_param_code(model_output_dirs, codegen_path="mace/codegen"): mace_run_param_file = "mace_run.config" @@ -646,6 +694,7 @@ def tuning_run(abi, phone_data_dir, build_type, opencl_binary_file, + opencl_parameter_file, shared_library_dir, omp_num_threads=-1, cpu_affinity_policy=1, @@ -713,9 +762,11 @@ def tuning_run(abi, adb_push("%s/%s.data" % (mace_model_dir, model_tag), phone_data_dir, serialno) - if device_type == common.DeviceType.GPU\ - and os.path.exists(opencl_binary_file): - adb_push(opencl_binary_file, phone_data_dir, serialno) + if device_type == common.DeviceType.GPU: + if os.path.exists(opencl_binary_file): + adb_push(opencl_binary_file, phone_data_dir, serialno) + if os.path.exists(opencl_parameter_file): + adb_push(opencl_parameter_file, phone_data_dir, serialno) adb_push("third_party/nnlib/libhexagon_controller.so", phone_data_dir, serialno) @@ -774,6 +825,8 @@ def tuning_run(abi, "--model_file=%s" % mace_model_phone_path, "--opencl_binary_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_binary_file)), + "--opencl_parameter_file=%s/%s" % + (phone_data_dir, os.path.basename(opencl_parameter_file)), ]) adb_cmd = ' '.join(adb_cmd) cmd_file_name = "%s-%s-%s" % ('cmd_file', model_tag, str(time.time())) @@ -930,7 +983,6 @@ def build_host_libraries(model_build_type, abi): bazel_build("@com_google_protobuf//:protobuf_lite", abi=abi) bazel_build("//mace/proto:mace_cc", abi=abi) bazel_build("//mace/codegen:generated_opencl", abi=abi) - bazel_build("//mace/codegen:generated_tuning_params", abi=abi) bazel_build("//mace/codegen:generated_version", abi=abi) bazel_build("//mace/utils:utils", abi=abi) bazel_build("//mace/core:core", abi=abi) @@ -993,9 +1045,6 @@ def merge_libs(target_soc, mri_stream += ( "addlib " "bazel-bin/mace/codegen/libgenerated_opencl.pic.a\n") - mri_stream += ( - "addlib " - "bazel-bin/mace/codegen/libgenerated_tuning_params.pic.a\n") mri_stream += ( "addlib " "bazel-bin/mace/codegen/libgenerated_version.pic.a\n") @@ -1030,9 +1079,6 @@ def merge_libs(target_soc, mri_stream += ( "addlib " "bazel-bin/mace/codegen/libgenerated_opencl.a\n") - mri_stream += ( - "addlib " - "bazel-bin/mace/codegen/libgenerated_tuning_params.a\n") mri_stream += ( "addlib " "bazel-bin/mace/codegen/libgenerated_version.a\n") @@ -1189,6 +1235,7 @@ def benchmark_model(abi, phone_data_dir, build_type, opencl_binary_file, + opencl_parameter_file, shared_library_dir, omp_num_threads=-1, cpu_affinity_policy=1, @@ -1240,9 +1287,11 @@ def benchmark_model(abi, if not embed_model_data: adb_push("%s/%s.data" % (mace_model_dir, model_tag), phone_data_dir, serialno) - if device_type == common.DeviceType.GPU \ - and os.path.exists(opencl_binary_file): - adb_push(opencl_binary_file, phone_data_dir, serialno) + if device_type == common.DeviceType.GPU: + if os.path.exists(opencl_binary_file): + adb_push(opencl_binary_file, phone_data_dir, serialno) + if os.path.exists(opencl_parameter_file): + adb_push(opencl_parameter_file, phone_data_dir, serialno) mace_model_phone_path = "" if build_type == BuildType.proto: mace_model_phone_path = "%s/%s.pb" % (phone_data_dir, model_tag) @@ -1283,6 +1332,8 @@ def benchmark_model(abi, "--model_file=%s" % mace_model_phone_path, "--opencl_binary_file=%s/%s" % (phone_data_dir, os.path.basename(opencl_binary_file)), + "--opencl_parameter_file=%s/%s" % + (phone_data_dir, os.path.basename(opencl_parameter_file)), ] adb_cmd = ' '.join(adb_cmd) cmd_file_name = "%s-%s-%s" % ('cmd_file', model_tag, str(time.time())) -- GitLab