From 97dd239c0cb56bb2aa35b707ae5e2b2384154082 Mon Sep 17 00:00:00 2001 From: Liangliang He Date: Fri, 15 Mar 2019 18:58:03 +0800 Subject: [PATCH] Fix tools build for Darwin --- .gitlab-ci.yml | 3 +- docs/development/how_to_debug.rst | 2 - mace/benchmark/benchmark_model.cc | 95 ++++++--------- mace/core/runtime/cpu/cpu_runtime.cc | 12 +- mace/examples/cli/BUILD.bazel | 2 + mace/examples/cli/example.cc | 176 +++++++-------------------- mace/libmace/BUILD.bazel | 2 + mace/libmace/mace_version_script.lds | 3 +- mace/port/BUILD.bazel | 2 +- mace/port/android/env.cc | 8 +- mace/port/android/env.h | 2 +- mace/port/android/logger.cc | 2 +- mace/port/env.cc | 2 +- mace/port/env.h | 6 +- mace/port/env_test.cc | 4 +- mace/port/posix/file_system.cc | 21 ++-- mace/tools/validation/BUILD.bazel | 10 +- mace/tools/validation/mace_run.cc | 85 +++++++------ mace/utils/BUILD.bazel | 25 +++- mace/utils/logging_test.cc | 6 +- mace/utils/tuner.h | 8 +- tools/bazel.rc | 27 ++++ tools/sh_commands.py | 2 + 23 files changed, 227 insertions(+), 278 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 06928c94..11bc1c2e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -195,7 +195,8 @@ extra_tests: GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@v9.git.n.xiaomi.com:deep-computing/generic-mobile-devices.git DEVICE_CONF_FILE=generic-mobile-devices/devices.yml fi - - python tools/bazel_adb_run.py --target="//mace/utils:tuner_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS || exit 1; + - python tools/bazel_adb_run.py --target="//mace/utils:utils_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS || exit 1; + - python tools/bazel_adb_run.py --target="//mace/port:port_test" --device_yml=${DEVICE_CONF_FILE} --run_target=True --stdout_processor=unittest_stdout_processor --target_abis=armeabi-v7a,arm64-v8a,arm64 --target_socs=$TARGET_SOCS || exit 1; so_size_check: stage: so_size_check diff --git a/docs/development/how_to_debug.rst b/docs/development/how_to_debug.rst index a28029da..1f516d28 100644 --- a/docs/development/how_to_debug.rst +++ b/docs/development/how_to_debug.rst @@ -114,8 +114,6 @@ The threshold can be configured through environment variable, e.g. ``MACE_CPP_MI With VLOG, the lower the verbose level, the more likely messages are to be logged. For example, when the threshold is set to 2, both ``VLOG(1)``, ``VLOG(2)`` log messages will be printed, but ``VLOG(3)`` and highers won't. -All expensive logging with ``VLOG`` should be guarded with ``if(VLOG_IS_ON(lvl))`` check to avoid normal run overhead. - By using ``mace_run`` tool, VLOG level can be easily set by option, e.g., .. code:: sh diff --git a/mace/benchmark/benchmark_model.cc b/mace/benchmark/benchmark_model.cc index b0370321..4ff45795 100644 --- a/mace/benchmark/benchmark_model.cc +++ b/mace/benchmark/benchmark_model.cc @@ -21,6 +21,8 @@ #include // NOLINT(build/c++11) #include "gflags/gflags.h" +#include "mace/port/env.h" +#include "mace/port/file_system.h" #include "mace/public/mace.h" #include "mace/utils/logging.h" #include "mace/utils/math.h" @@ -31,24 +33,6 @@ namespace mace { namespace benchmark { -namespace str_util { - -std::vector Split(const std::string &str, char delims) { - std::vector result; - std::string tmp = str; - while (!tmp.empty()) { - size_t next_offset = tmp.find(delims); - result.push_back(tmp.substr(0, next_offset)); - if (next_offset == std::string::npos) { - break; - } else { - tmp = tmp.substr(next_offset + 1); - } - } - return result; -} - -} // namespace str_util void ParseShape(const std::string &str, std::vector *shape) { std::string tmp = str; @@ -229,14 +213,10 @@ int Main(int argc, char **argv) { std::unique_ptr statistician(new OpStat()); - std::vector input_names = - str_util::Split(FLAGS_input_node, ','); - std::vector output_names = - str_util::Split(FLAGS_output_node, ','); - std::vector input_shapes = - str_util::Split(FLAGS_input_shape, ':'); - std::vector output_shapes = - str_util::Split(FLAGS_output_shape, ':'); + std::vector input_names = Split(FLAGS_input_node, ','); + std::vector output_names = Split(FLAGS_output_node, ','); + std::vector input_shapes = Split(FLAGS_input_shape, ':'); + std::vector output_shapes = Split(FLAGS_output_shape, ':'); const size_t input_count = input_shapes.size(); const size_t output_count = output_shapes.size(); @@ -250,9 +230,9 @@ int Main(int argc, char **argv) { } std::vector raw_input_data_formats = - str_util::Split(FLAGS_input_data_format, ','); + Split(FLAGS_input_data_format, ','); std::vector raw_output_data_formats = - str_util::Split(FLAGS_output_data_format, ','); + Split(FLAGS_output_data_format, ','); std::vector input_data_formats(input_count); std::vector output_data_formats(output_count); for (size_t i = 0; i < input_count; ++i) { @@ -302,41 +282,46 @@ int Main(int argc, char **argv) { std::shared_ptr engine; MaceStatus create_engine_status; // Create Engine - std::vector model_graph_data; + std::unique_ptr model_graph_data; if (FLAGS_model_file != "") { - if (!mace::ReadBinaryFile(&model_graph_data, FLAGS_model_file)) { + auto fs = GetFileSystem(); + auto status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_file.c_str(), + &model_graph_data); + if (status != MaceStatus::MACE_SUCCESS) { LOG(FATAL) << "Failed to read file: " << FLAGS_model_file; } } - const unsigned char *model_weights_data = nullptr; - size_t model_weights_data_size = 0; + std::unique_ptr model_weights_data; if (FLAGS_model_data_file != "") { - MemoryMap(FLAGS_model_data_file, - &model_weights_data, - &model_weights_data_size); - MACE_CHECK(model_weights_data != nullptr && model_weights_data_size != 0); + auto fs = GetFileSystem(); + auto status = fs->NewReadOnlyMemoryRegionFromFile( + FLAGS_model_data_file.c_str(), + &model_weights_data); + if (status != MaceStatus::MACE_SUCCESS) { + LOG(FATAL) << "Failed to read file: " << FLAGS_model_data_file; + } + MACE_CHECK(model_weights_data->length() > 0); } #ifdef MODEL_GRAPH_FORMAT_CODE - create_engine_status = - CreateMaceEngineFromCode(FLAGS_model_name, - model_weights_data, - model_weights_data_size, - input_names, - output_names, - config, - &engine); + create_engine_status = CreateMaceEngineFromCode(FLAGS_model_name, + reinterpret_cast(model_weights_data->data()), + model_weights_data->length(), + input_names, + output_names, + config, + &engine); #else - create_engine_status = - CreateMaceEngineFromProto(model_graph_data.data(), - model_graph_data.size(), - model_weights_data, - model_weights_data_size, - input_names, - output_names, - config, - &engine); + create_engine_status = CreateMaceEngineFromProto( + reinterpret_cast(model_graph_data->data()), + model_graph_data->length(), + reinterpret_cast(model_weights_data->data()), + model_weights_data->length(), + input_names, + output_names, + config, + &engine); #endif if (create_engine_status != MaceStatus::MACE_SUCCESS) { LOG(FATAL) << "Create engine error, please check the arguments"; @@ -411,10 +396,6 @@ int Main(int argc, char **argv) { statistician->PrintStat(); - if (model_weights_data != nullptr) { - MemoryUnMap(model_weights_data, model_weights_data_size); - } - return 0; } diff --git a/mace/core/runtime/cpu/cpu_runtime.cc b/mace/core/runtime/cpu/cpu_runtime.cc index 34255e56..76cd33e2 100644 --- a/mace/core/runtime/cpu/cpu_runtime.cc +++ b/mace/core/runtime/cpu/cpu_runtime.cc @@ -47,10 +47,8 @@ MaceStatus SetOpenMPThreadsAndAffinityCPUs(int omp_num_threads, MaceOpenMPThreadCount = omp_num_threads; #ifdef MACE_ENABLE_OPENMP - if (VLOG_IS_ON(1)) { - VLOG(1) << "Set OpenMP threads number: " << omp_num_threads - << ", CPU core IDs: " << MakeString(cpu_ids); - } + VLOG(1) << "Set OpenMP threads number: " << omp_num_threads + << ", CPU core IDs: " << MakeString(cpu_ids); omp_set_schedule(omp_sched_guided, 1); omp_set_num_threads(omp_num_threads); #else @@ -74,9 +72,7 @@ MaceStatus SetOpenMPThreadsAndAffinityCPUs(int omp_num_threads, return MaceStatus::MACE_SUCCESS; #else MaceStatus status = SchedSetAffinity(cpu_ids); - if (VLOG_IS_ON(1)) { - VLOG(1) << "Set affinity without OpenMP: " << MakeString(cpu_ids); - } + VLOG(1) << "Set affinity without OpenMP: " << MakeString(cpu_ids); return status; #endif } @@ -89,7 +85,7 @@ MaceStatus CPURuntime::SetOpenMPThreadsAndAffinityPolicy( void *gemm_context) { // get cpu frequency info std::vector cpu_max_freqs; - MACE_RETURN_IF_ERROR(GetCpuMaxFreq(&cpu_max_freqs)); + MACE_RETURN_IF_ERROR(GetCPUMaxFreq(&cpu_max_freqs)); if (cpu_max_freqs.empty()) { return MaceStatus::MACE_RUNTIME_ERROR; } diff --git a/mace/examples/cli/BUILD.bazel b/mace/examples/cli/BUILD.bazel index 97e42b7d..e661c10b 100644 --- a/mace/examples/cli/BUILD.bazel +++ b/mace/examples/cli/BUILD.bazel @@ -33,6 +33,7 @@ cc_binary( "//mace/codegen:generated_libmace", "//mace/codegen:generated_opencl_binary", "//mace/codegen:generated_opencl_parameter", + "//mace/utils:utils_hdrs", ] + if_hexagon_enabled([ "//third_party/nnlib:libhexagon", ]), @@ -63,5 +64,6 @@ cc_binary( "//mace/codegen:generated_mace_engine_factory", "//mace/codegen:generated_opencl_binary", "//mace/codegen:generated_opencl_parameter", + "//mace/utils:utils_hdrs", ], ) diff --git a/mace/examples/cli/example.cc b/mace/examples/cli/example.cc index 8bcef97a..7e485bc6 100644 --- a/mace/examples/cli/example.cc +++ b/mace/examples/cli/example.cc @@ -27,7 +27,11 @@ #include #include "gflags/gflags.h" +#include "mace/port/env.h" +#include "mace/port/file_system.h" #include "mace/public/mace.h" +#include "mace/utils/logging.h" +#include "mace/utils/string_util.h" // if convert model to code. #ifdef MODEL_GRAPH_FORMAT_CODE #include "mace/codegen/engine/mace_engine_factory.h" @@ -46,97 +50,6 @@ size_t OpenCLParameterSize(); namespace mace { namespace examples { -namespace str_util { - -std::vector Split(const std::string &str, char delims) { - std::vector result; - std::string tmp = str; - while (!tmp.empty()) { - size_t next_offset = tmp.find(delims); - result.push_back(tmp.substr(0, next_offset)); - if (next_offset == std::string::npos) { - break; - } else { - tmp = tmp.substr(next_offset + 1); - } - } - return result; -} - -} // namespace str_util - -namespace { -bool ReadBinaryFile(std::vector *data, - const std::string &filename) { - std::ifstream ifs(filename, std::ios::in | std::ios::binary); - if (!ifs.is_open()) { - return false; - } - ifs.seekg(0, ifs.end); - size_t length = ifs.tellg(); - ifs.seekg(0, ifs.beg); - - data->reserve(length); - data->insert(data->begin(), std::istreambuf_iterator(ifs), - std::istreambuf_iterator()); - if (ifs.fail()) { - return false; - } - ifs.close(); - - return true; -} - -bool MemoryMap(const std::string &file, - const unsigned char **data, - size_t *size) { - bool ret = true; - int fd = open(file.c_str(), O_RDONLY); - if (fd < 0) { - std::cerr << "Failed to open file " << file - << ", error code: " << strerror(errno) << std::endl; - ret = false; - } - struct stat st; - fstat(fd, &st); - *size = static_cast(st.st_size); - - *data = static_cast( - mmap(nullptr, *size, PROT_READ, MAP_PRIVATE, fd, 0)); - if (*data == static_cast(MAP_FAILED)) { - std::cerr << "Failed to map file " << file - << ", error code: " << strerror(errno) << std::endl; - ret = false; - } - - if (close(fd) < 0) { - std::cerr << "Failed to close file " << file - << ", error code: " << strerror(errno) << std::endl; - ret = false; - } - - return ret; -} - -bool MemoryUnMap(const unsigned char *data, - const size_t &size) { - bool ret = true; - if (data == nullptr || size == 0) { - std::cerr << "data is null or size is 0" << std::endl; - ret = false; - } - - if (munmap(const_cast(data), size) < 0) { - std::cerr << "Failed to unmap file, error code: " - << strerror(errno) << std::endl; - ret = false; - } - - return ret; -} - -} // namespace - void ParseShape(const std::string &str, std::vector *shape) { std::string tmp = str; while (!tmp.empty()) { @@ -284,16 +197,26 @@ bool RunModel(const std::vector &input_names, std::shared_ptr engine; MaceStatus create_engine_status; - std::vector model_graph_data; - if (!ReadBinaryFile(&model_graph_data, FLAGS_model_file)) { - std::cerr << "Failed to read file: " << FLAGS_model_file << std::endl; + std::unique_ptr model_graph_data; + if (FLAGS_model_file != "") { + auto fs = GetFileSystem(); + auto status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_file.c_str(), + &model_graph_data); + if (status != MaceStatus::MACE_SUCCESS) { + LOG(FATAL) << "Failed to read file: " << FLAGS_model_file; + } } - const unsigned char *model_weights_data = nullptr; - size_t model_weights_data_size = 0; - if (!MemoryMap(FLAGS_model_data_file, - &model_weights_data, - &model_weights_data_size)) { - std::cerr << "Failed to read file: " << FLAGS_model_data_file << std::endl; + + std::unique_ptr model_weights_data; + if (FLAGS_model_data_file != "") { + auto fs = GetFileSystem(); + auto status = fs->NewReadOnlyMemoryRegionFromFile( + FLAGS_model_data_file.c_str(), + &model_weights_data); + if (status != MaceStatus::MACE_SUCCESS) { + LOG(FATAL) << "Failed to read file: " << FLAGS_model_data_file; + } + MACE_CHECK(model_weights_data->length() > 0); } // Only choose one of the two type based on the `model_graph_format` @@ -301,24 +224,24 @@ bool RunModel(const std::vector &input_names, #ifdef MODEL_GRAPH_FORMAT_CODE // if model_data_format == code, just pass an empty string("") // to model_data_file parameter. - create_engine_status = - CreateMaceEngineFromCode(FLAGS_model_name, - model_weights_data, - model_weights_data_size, - input_names, - output_names, - config, - &engine); + create_engine_status = CreateMaceEngineFromCode( + FLAGS_model_name, + reinterpret_cast(model_weights_data->data()), + model_weights_data->length(), + input_names, + output_names, + config, + &engine); #else - create_engine_status = - CreateMaceEngineFromProto(model_graph_data.data(), - model_graph_data.size(), - model_weights_data, - model_weights_data_size, - input_names, - output_names, - config, - &engine); + create_engine_status = CreateMaceEngineFromProto( + reinterpret_cast(model_graph_data->data()), + model_graph_data->length(), + reinterpret_cast(model_weights_data->data()), + model_weights_data->length(), + input_names, + output_names, + config, + &engine); #endif if (create_engine_status != MaceStatus::MACE_SUCCESS) { @@ -450,10 +373,6 @@ bool RunModel(const std::vector &input_names, } } - if (model_weights_data != nullptr) { - MemoryUnMap(model_weights_data, model_weights_data_size); - } - std::cout << "Finished" << std::endl; return true; @@ -486,13 +405,10 @@ int Main(int argc, char **argv) { << FLAGS_cpu_affinity_policy << std::endl; - std::vector input_names = str_util::Split(FLAGS_input_node, ','); - std::vector output_names = - str_util::Split(FLAGS_output_node, ','); - std::vector input_shapes = - str_util::Split(FLAGS_input_shape, ':'); - std::vector output_shapes = - str_util::Split(FLAGS_output_shape, ':'); + std::vector input_names = Split(FLAGS_input_node, ','); + std::vector output_names = Split(FLAGS_output_node, ','); + std::vector input_shapes = Split(FLAGS_input_shape, ':'); + std::vector output_shapes = Split(FLAGS_output_shape, ':'); const size_t input_count = input_shapes.size(); const size_t output_count = output_shapes.size(); @@ -506,9 +422,9 @@ int Main(int argc, char **argv) { } std::vector raw_input_data_formats = - str_util::Split(FLAGS_input_data_format, ','); + Split(FLAGS_input_data_format, ','); std::vector raw_output_data_formats = - str_util::Split(FLAGS_output_data_format, ','); + Split(FLAGS_output_data_format, ','); std::vector input_data_formats(input_count); std::vector output_data_formats(output_count); for (size_t i = 0; i < input_count; ++i) { diff --git a/mace/libmace/BUILD.bazel b/mace/libmace/BUILD.bazel index 2dba327b..29127a14 100644 --- a/mace/libmace/BUILD.bazel +++ b/mace/libmace/BUILD.bazel @@ -90,6 +90,7 @@ genrule( "//mace/libmace", "//mace/port:port_base", "//mace/port/posix:port_posix", + "//mace/public", "//mace/utils", "//mace/proto:mace_cc", "@com_google_protobuf//:protobuf_lite", @@ -137,6 +138,7 @@ genrule( "$(locations //mace/port/darwin:port_darwin) ", default_value = "", ) + + "$(locations //mace/public:public) " + "$(locations //mace/utils:utils) " + "$(locations //mace/proto:mace_cc) " + "$(locations @com_google_protobuf//:protobuf_lite) " + diff --git a/mace/libmace/mace_version_script.lds b/mace/libmace/mace_version_script.lds index 917ecc47..88f748ed 100644 --- a/mace/libmace/mace_version_script.lds +++ b/mace/libmace/mace_version_script.lds @@ -10,7 +10,7 @@ mace { *GetCapability*; # api for static library of models - *mace*logging*LogMessage*; + *mace*port**; *mace*MaceStatus*; *mace*NetDef*; *mace*MemoryType*; @@ -21,6 +21,7 @@ mace { *mace*OperatorDef*; *mace*ConstTensor*; *mace*Argument*; + *mace*Split*; *mace*MemoryBlock*; *google*protobuf*; diff --git a/mace/port/BUILD.bazel b/mace/port/BUILD.bazel index 30db8945..d23633a6 100644 --- a/mace/port/BUILD.bazel +++ b/mace/port/BUILD.bazel @@ -43,7 +43,7 @@ cc_test( srcs = glob([ "*_test.cc", ]), - linkstatic = 0, + linkstatic = 1, deps = [ ":port", "@gtest//:gtest", diff --git a/mace/port/android/env.cc b/mace/port/android/env.cc index 774511fd..2940d344 100644 --- a/mace/port/android/env.cc +++ b/mace/port/android/env.cc @@ -61,7 +61,7 @@ LogWriter *AndroidEnv::GetLogWriter() { namespace { -int GetCpuCount() { +int GetCPUCount() { int cpu_count = 0; std::string cpu_sys_conf = "/proc/cpuinfo"; std::ifstream f(cpu_sys_conf); @@ -115,9 +115,9 @@ size_t BackTrace(void** buffer, size_t max) { } // namespace -MaceStatus AndroidEnv::GetCpuMaxFreq(std::vector *max_freqs) { +MaceStatus AndroidEnv::GetCPUMaxFreq(std::vector *max_freqs) { MACE_CHECK_NOTNULL(max_freqs); - int cpu_count = GetCpuCount(); + int cpu_count = GetCPUCount(); if (cpu_count < 0) { return MaceStatus::MACE_RUNTIME_ERROR; } @@ -142,7 +142,7 @@ MaceStatus AndroidEnv::GetCpuMaxFreq(std::vector *max_freqs) { f.close(); } - if (VLOG_IS_ON(1)) VLOG(1) << "CPU freq: " << MakeString(*max_freqs); + VLOG(1) << "CPU freq: " << MakeString(*max_freqs); return MaceStatus::MACE_SUCCESS; } diff --git a/mace/port/android/env.h b/mace/port/android/env.h index 04c3b35b..c51c5772 100644 --- a/mace/port/android/env.h +++ b/mace/port/android/env.h @@ -29,7 +29,7 @@ namespace port { class AndroidEnv : public Env { public: int64_t NowMicros() override; - MaceStatus GetCpuMaxFreq(std::vector *max_freqs) override; + MaceStatus GetCPUMaxFreq(std::vector *max_freqs) override; MaceStatus SchedSetAffinity(const std::vector &cpu_ids) override; FileSystem *GetFileSystem() override; LogWriter *GetLogWriter() override; diff --git a/mace/port/android/logger.cc b/mace/port/android/logger.cc index c0fd5f6b..e6f57f43 100644 --- a/mace/port/android/logger.cc +++ b/mace/port/android/logger.cc @@ -48,7 +48,7 @@ void AndroidLogWriter::WriteLogMessage(const char *fname, const char *const partial_name = strrchr(fname, '/'); ss << (partial_name != nullptr ? partial_name + 1 : fname) << ":" << line << " " << message; - __android_log_write(android_log_level, "CRT", ss.str().c_str()); + __android_log_write(android_log_level, "MACE", ss.str().c_str()); // Also log to stderr (for standalone Android apps) and abort. LogWriter::WriteLogMessage(fname, line, severity, message); diff --git a/mace/port/env.cc b/mace/port/env.cc index 10ae4306..b78e1c82 100644 --- a/mace/port/env.cc +++ b/mace/port/env.cc @@ -22,7 +22,7 @@ namespace mace { namespace port { -MaceStatus Env::GetCpuMaxFreq(std::vector *max_freqs) { +MaceStatus Env::GetCPUMaxFreq(std::vector *max_freqs) { return MaceStatus::MACE_UNSUPPORTED; } diff --git a/mace/port/env.h b/mace/port/env.h index 2491f023..af98cc5a 100644 --- a/mace/port/env.h +++ b/mace/port/env.h @@ -38,7 +38,7 @@ class LogWriter; class Env { public: virtual int64_t NowMicros() = 0; - virtual MaceStatus GetCpuMaxFreq(std::vector *max_freqs); + virtual MaceStatus GetCPUMaxFreq(std::vector *max_freqs); virtual MaceStatus SchedSetAffinity(const std::vector &cpu_ids); virtual FileSystem *GetFileSystem() = 0; virtual LogWriter *GetLogWriter() = 0; @@ -58,8 +58,8 @@ inline int64_t NowMicros() { return port::Env::Default()->NowMicros(); } -inline MaceStatus GetCpuMaxFreq(std::vector *max_freqs) { - return port::Env::Default()->GetCpuMaxFreq(max_freqs); +inline MaceStatus GetCPUMaxFreq(std::vector *max_freqs) { + return port::Env::Default()->GetCPUMaxFreq(max_freqs); } inline MaceStatus SchedSetAffinity(const std::vector &cpu_ids) { diff --git a/mace/port/env_test.cc b/mace/port/env_test.cc index 1a615545..d23b5787 100644 --- a/mace/port/env_test.cc +++ b/mace/port/env_test.cc @@ -30,9 +30,9 @@ TEST_F(EnvTest, GetFileSystem) { GetFileSystem(); } -TEST_F(EnvTest, CpuInfo) { +TEST_F(EnvTest, CPUInfo) { std::vector freq; - GetCpuMaxFreq(&freq); + GetCPUMaxFreq(&freq); std::vector cpu_ids; SchedSetAffinity(cpu_ids); } diff --git a/mace/port/posix/file_system.cc b/mace/port/posix/file_system.cc index 236f2215..a7873b96 100644 --- a/mace/port/posix/file_system.cc +++ b/mace/port/posix/file_system.cc @@ -34,7 +34,9 @@ class PosixReadOnlyMemoryRegion : public ReadOnlyMemoryRegion { PosixReadOnlyMemoryRegion(const void* addr, uint64_t length) : addr_(addr), length_(length) {} ~PosixReadOnlyMemoryRegion() override { - munmap(const_cast(addr_), length_); + if (length_ > 0) { + munmap(const_cast(addr_), length_); + } }; const void *data() override { return addr_; }; uint64_t length() override { return length_; }; @@ -56,15 +58,20 @@ MaceStatus PosixFileSystem::NewReadOnlyMemoryRegionFromFile( } else { struct stat st; fstat(fd, &st); - const void* address = + if (st.st_size > 0) { + const void* address = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (address == MAP_FAILED) { - // TODO(heliangliang) check errno - s = MaceStatus(MaceStatus::MACE_RUNTIME_ERROR); + if (address == MAP_FAILED) { + // TODO(heliangliang) check errno + s = MaceStatus(MaceStatus::MACE_RUNTIME_ERROR); + } else { + *result = make_unique(address, st.st_size); + } + close(fd); } else { - *result = make_unique(address, st.st_size); + // Empty file: mmap returns EINVAL (since Linux 2.6.12) length was 0 + *result = make_unique(nullptr, 0); } - close(fd); } return s; } diff --git a/mace/tools/validation/BUILD.bazel b/mace/tools/validation/BUILD.bazel index 7e238c00..d85283ac 100644 --- a/mace/tools/validation/BUILD.bazel +++ b/mace/tools/validation/BUILD.bazel @@ -29,14 +29,8 @@ cc_binary( ] + if_opencl_enabled([ "-DMACE_ENABLE_OPENCL", ]), - linkopts = [ - "-lm", - ] + if_openmp_enabled([ - "-fopenmp" - ]) + if_android([ - "-ldl", - "-pie", - "-llog", + linkopts = if_openmp_enabled([ + "-fopenmp", ]), linkstatic = 0, deps = [ diff --git a/mace/tools/validation/mace_run.cc b/mace/tools/validation/mace_run.cc index d7a911a0..4dd885a5 100644 --- a/mace/tools/validation/mace_run.cc +++ b/mace/tools/validation/mace_run.cc @@ -24,7 +24,8 @@ * --model_data_file=model_data.data \ * --device=GPU */ -#include +#include +#include #include #include #include @@ -34,6 +35,7 @@ #include "gflags/gflags.h" #include "mace/public/mace.h" #include "mace/port/env.h" +#include "mace/port/file_system.h" #include "mace/utils/logging.h" #include "mace/utils/string_util.h" @@ -238,19 +240,24 @@ bool RunModel(const std::string &model_name, } #endif // MACE_ENABLE_OPENCL - std::vector model_graph_data; + std::unique_ptr model_graph_data; if (FLAGS_model_file != "") { - if (!mace::ReadBinaryFile(&model_graph_data, FLAGS_model_file)) { + auto fs = GetFileSystem(); + status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_file.c_str(), + &model_graph_data); + if (status != MaceStatus::MACE_SUCCESS) { LOG(FATAL) << "Failed to read file: " << FLAGS_model_file; } } - const unsigned char *model_weights_data = nullptr; - size_t model_weights_data_size = 0; + std::unique_ptr model_weights_data; if (FLAGS_model_data_file != "") { - MemoryMap(FLAGS_model_data_file, - &model_weights_data, - &model_weights_data_size); + auto fs = GetFileSystem(); + status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_data_file.c_str(), + &model_weights_data); + if (status != MaceStatus::MACE_SUCCESS) { + LOG(FATAL) << "Failed to read file: " << FLAGS_model_data_file; + } } std::shared_ptr engine; @@ -262,8 +269,9 @@ bool RunModel(const std::string &model_name, #ifdef MODEL_GRAPH_FORMAT_CODE create_engine_status = CreateMaceEngineFromCode(model_name, - model_weights_data, - model_weights_data_size, + reinterpret_cast( + model_weights_data->data()), + model_weights_data->length(), input_names, output_names, config, @@ -271,10 +279,12 @@ bool RunModel(const std::string &model_name, #else (void)(model_name); create_engine_status = - CreateMaceEngineFromProto(model_graph_data.data(), - model_graph_data.size(), - model_weights_data, - model_weights_data_size, + CreateMaceEngineFromProto(reinterpret_cast( + model_graph_data->data()), + model_graph_data->length(), + reinterpret_cast( + model_weights_data->data()), + model_weights_data->length(), input_names, output_names, config, @@ -345,18 +355,21 @@ bool RunModel(const std::string &model_name, #ifdef MODEL_GRAPH_FORMAT_CODE create_engine_status = CreateMaceEngineFromCode(model_name, - model_weights_data, - model_weights_data_size, + reinterpret_cast( + model_weights_data->data()), + model_weights_data->length(), input_names, output_names, config, &engine); #else create_engine_status = - CreateMaceEngineFromProto(model_graph_data.data(), - model_graph_data.size(), - model_weights_data, - model_weights_data_size, + CreateMaceEngineFromProto(reinterpret_cast( + model_graph_data->data()), + model_graph_data->length(), + reinterpret_cast( + model_weights_data->data()), + model_weights_data->length(), input_names, output_names, config, @@ -388,22 +401,26 @@ bool RunModel(const std::string &model_name, #ifdef MODEL_GRAPH_FORMAT_CODE create_engine_status = CreateMaceEngineFromCode(model_name, - model_weights_data, - model_weights_data_size, + reinterpret_cast( + model_weights_data->data()), + model_weights_data->length(), input_names, output_names, config, &engine); #else create_engine_status = - CreateMaceEngineFromProto(model_graph_data.data(), - model_graph_data.size(), - model_weights_data, - model_weights_data_size, - input_names, - output_names, - config, - &engine); + CreateMaceEngineFromProto( + reinterpret_cast( + model_graph_data->data()), + model_graph_data->length(), + reinterpret_cast( + model_weights_data->data()), + model_weights_data->length(), + input_names, + output_names, + config, + &engine); #endif } while (create_engine_status != MaceStatus::MACE_SUCCESS); } else { @@ -445,10 +462,6 @@ bool RunModel(const std::string &model_name, << output_size << " done."; } - if (model_weights_data != nullptr) { - MemoryUnMap(model_weights_data, model_weights_data_size); - } - return true; } @@ -492,9 +505,9 @@ int Main(int argc, char **argv) { ParseShape(output_shapes[i], &output_shape_vec[i]); } std::vector raw_input_data_formats = - str_util::Split(FLAGS_input_data_format, ','); + Split(FLAGS_input_data_format, ','); std::vector raw_output_data_formats = - str_util::Split(FLAGS_output_data_format, ','); + Split(FLAGS_output_data_format, ','); std::vector input_data_formats(input_count); std::vector output_data_formats(output_count); for (size_t i = 0; i < input_count; ++i) { diff --git a/mace/utils/BUILD.bazel b/mace/utils/BUILD.bazel index 56b79437..3207441f 100644 --- a/mace/utils/BUILD.bazel +++ b/mace/utils/BUILD.bazel @@ -7,6 +7,22 @@ package( licenses(["notice"]) # Apache 2.0 +cc_library( + name = "utils_hdrs", + hdrs = glob([ + "*.h", + ]), + copts = [ + "-Werror", + "-Wextra", + "-Wno-missing-field-initializers", + ], + deps = [ + "//mace/port:port_api", + "//mace/public", + ], +) + cc_library( name = "utils", srcs = glob( @@ -17,18 +33,15 @@ cc_library( "*_test.cc", ], ), - hdrs = glob([ - "*.h", - ]), copts = [ "-Werror", "-Wextra", "-Wno-missing-field-initializers", ], deps = [ - "//mace/port:port_api", - "//mace/public", + ":utils_hdrs", ], + alwayslink = 1, ) cc_test( @@ -44,7 +57,7 @@ cc_test( "-Wextra", "-Wno-missing-field-initializers", ], - linkstatic = 0, + linkstatic = 1, deps = [ ":utils", "//mace/port", diff --git a/mace/utils/logging_test.cc b/mace/utils/logging_test.cc index 88cee53d..3a33cc96 100644 --- a/mace/utils/logging_test.cc +++ b/mace/utils/logging_test.cc @@ -29,14 +29,12 @@ TEST_F(LoggingTest, Basic) { VLOG(1) << "vlog 1 logging"; VLOG(2) << "vlog 2 logging"; - - if (VLOG_IS_ON(1)) { - VLOG(1) << "vlog 1 logging"; - } } TEST_F(LoggingTest, LogFatal) { +#ifdef GTEST_HAS_DEATH_TEST EXPECT_DEATH(do { LOG(FATAL) << "fatal logging"; } while (false), ""); +#endif } } // namespace diff --git a/mace/utils/tuner.h b/mace/utils/tuner.h index 0a33523c..5d381b04 100644 --- a/mace/utils/tuner.h +++ b/mace/utils/tuner.h @@ -78,16 +78,14 @@ class Tuner { std::vector opt_param = default_param; RetType res = Tune(param_generator, func, timer, &opt_param); VLOG(3) << "Tuning " << param_key - << " retult: " << (VLOG_IS_ON(3) ? MakeString(opt_param) : ""); + << " retult: " << MakeString(opt_param); param_table_[obfucated_param_key] = opt_param; return res; } else { // run if (param_table_.find(obfucated_param_key) != param_table_.end()) { VLOG(3) << param_key << ": " - << (VLOG_IS_ON(3) - ? MakeString(param_table_[obfucated_param_key]) - : ""); + << MakeString(param_table_[obfucated_param_key]); return func(param_table_[obfucated_param_key], nullptr, nullptr); } else { return func(default_param, nullptr, nullptr); @@ -114,7 +112,7 @@ class Tuner { sizeof(params_size)); VLOG(3) << "Write tuning param: " << kp.first.c_str() << ": " - << (VLOG_IS_ON(3) ? MakeString(params) : ""); + << MakeString(params); for (auto ¶m : params) { ofs.write(reinterpret_cast(¶m), sizeof(params_size)); } diff --git a/tools/bazel.rc b/tools/bazel.rc index 85d77637..067cb7e1 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -17,15 +17,41 @@ build:symbol_hidden --copt=-fvisibility=hidden build:android --linkopt=-pie build:android --linkopt=-ldl build:android --linkopt=-llog +build:android --linkopt=-lm build:android --distinct_host_configuration=true build:android --crosstool_top=//external:android/crosstool build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain +# Linux host build, --config linux +build:linux --define linux=true + +# MacOS host build, --config darwin +build:darwin --define darwin=true + +# iOS and other darwin platforms, --config ios +build:ios --define darwin=true +build:ios --distinct_host_configuration=true +build:ios --host_crosstool_top=@bazel_tools//tools/cpp:toolchain +build:ios --cpu=arm64 + +# Linux host build, --config linux +build:linux --define linux=true + +# MacOS host build, --config darwin +build:darwin --define darwin=true + +# iOS and other darwin platforms, --config ios +build:ios --define darwin=true +build:ios --distinct_host_configuration=true +build:ios --host_crosstool_top=@bazel_tools//tools/cpp:toolchain +build:ios --cpu=ios_arm64 + # Usage example: bazel build --config arm_linux_gnueabihf # Used to fix library not find linking issue, see also: # https://github.com/bazelbuild/bazel/issues/6653, # https://github.com/bazelbuild/bazel/issues/6189 +build:arm_linux_gnueabihf --define linux=true build:arm_linux_gnueabihf --spawn_strategy=standalone build:arm_linux_gnueabihf --distinct_host_configuration=true build:arm_linux_gnueabihf --crosstool_top=//tools/arm_compiler:toolchain @@ -39,6 +65,7 @@ build:arm_linux_gnueabihf --copt -Wno-sequence-point build:arm_linux_gnueabihf --copt -Wno-implicit-fallthrough # Usage example: bazel build --config aarch64_linux_gnu +build:aarch64_linux_gnu --define linux=true build:aarch64_linux_gnu --spawn_strategy=standalone build:aarch64_linux_gnu --distinct_host_configuration=true build:aarch64_linux_gnu --crosstool_top=//tools/aarch64_compiler:toolchain diff --git a/tools/sh_commands.py b/tools/sh_commands.py index 0649129f..ac00dc66 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -275,6 +275,8 @@ def bazel_build(target, if abi == "host": bazel_args = ( "build", + "--config", + platform.system().lower(), "--define", "openmp=%s" % str(enable_openmp).lower(), "--define", -- GitLab