提交 97dd239c 编写于 作者: L Liangliang He

Fix tools build for Darwin

上级 ddcd08ea
...@@ -195,7 +195,8 @@ extra_tests: ...@@ -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 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 DEVICE_CONF_FILE=generic-mobile-devices/devices.yml
fi 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: so_size_check:
stage: so_size_check stage: so_size_check
......
...@@ -114,8 +114,6 @@ The threshold can be configured through environment variable, e.g. ``MACE_CPP_MI ...@@ -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 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. 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., By using ``mace_run`` tool, VLOG level can be easily set by option, e.g.,
.. code:: sh .. code:: sh
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include <thread> // NOLINT(build/c++11) #include <thread> // NOLINT(build/c++11)
#include "gflags/gflags.h" #include "gflags/gflags.h"
#include "mace/port/env.h"
#include "mace/port/file_system.h"
#include "mace/public/mace.h" #include "mace/public/mace.h"
#include "mace/utils/logging.h" #include "mace/utils/logging.h"
#include "mace/utils/math.h" #include "mace/utils/math.h"
...@@ -31,24 +33,6 @@ ...@@ -31,24 +33,6 @@
namespace mace { namespace mace {
namespace benchmark { namespace benchmark {
namespace str_util {
std::vector<std::string> Split(const std::string &str, char delims) {
std::vector<std::string> 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<int64_t> *shape) { void ParseShape(const std::string &str, std::vector<int64_t> *shape) {
std::string tmp = str; std::string tmp = str;
...@@ -229,14 +213,10 @@ int Main(int argc, char **argv) { ...@@ -229,14 +213,10 @@ int Main(int argc, char **argv) {
std::unique_ptr<OpStat> statistician(new OpStat()); std::unique_ptr<OpStat> statistician(new OpStat());
std::vector<std::string> input_names = std::vector<std::string> input_names = Split(FLAGS_input_node, ',');
str_util::Split(FLAGS_input_node, ','); std::vector<std::string> output_names = Split(FLAGS_output_node, ',');
std::vector<std::string> output_names = std::vector<std::string> input_shapes = Split(FLAGS_input_shape, ':');
str_util::Split(FLAGS_output_node, ','); std::vector<std::string> output_shapes = Split(FLAGS_output_shape, ':');
std::vector<std::string> input_shapes =
str_util::Split(FLAGS_input_shape, ':');
std::vector<std::string> output_shapes =
str_util::Split(FLAGS_output_shape, ':');
const size_t input_count = input_shapes.size(); const size_t input_count = input_shapes.size();
const size_t output_count = output_shapes.size(); const size_t output_count = output_shapes.size();
...@@ -250,9 +230,9 @@ int Main(int argc, char **argv) { ...@@ -250,9 +230,9 @@ int Main(int argc, char **argv) {
} }
std::vector<std::string> raw_input_data_formats = std::vector<std::string> raw_input_data_formats =
str_util::Split(FLAGS_input_data_format, ','); Split(FLAGS_input_data_format, ',');
std::vector<std::string> raw_output_data_formats = std::vector<std::string> raw_output_data_formats =
str_util::Split(FLAGS_output_data_format, ','); Split(FLAGS_output_data_format, ',');
std::vector<DataFormat> input_data_formats(input_count); std::vector<DataFormat> input_data_formats(input_count);
std::vector<DataFormat> output_data_formats(output_count); std::vector<DataFormat> output_data_formats(output_count);
for (size_t i = 0; i < input_count; ++i) { for (size_t i = 0; i < input_count; ++i) {
...@@ -302,41 +282,46 @@ int Main(int argc, char **argv) { ...@@ -302,41 +282,46 @@ int Main(int argc, char **argv) {
std::shared_ptr<mace::MaceEngine> engine; std::shared_ptr<mace::MaceEngine> engine;
MaceStatus create_engine_status; MaceStatus create_engine_status;
// Create Engine // Create Engine
std::vector<unsigned char> model_graph_data; std::unique_ptr<mace::port::ReadOnlyMemoryRegion> model_graph_data;
if (FLAGS_model_file != "") { 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; LOG(FATAL) << "Failed to read file: " << FLAGS_model_file;
} }
} }
const unsigned char *model_weights_data = nullptr; std::unique_ptr<mace::port::ReadOnlyMemoryRegion> model_weights_data;
size_t model_weights_data_size = 0;
if (FLAGS_model_data_file != "") { if (FLAGS_model_data_file != "") {
MemoryMap(FLAGS_model_data_file, auto fs = GetFileSystem();
&model_weights_data, auto status = fs->NewReadOnlyMemoryRegionFromFile(
&model_weights_data_size); FLAGS_model_data_file.c_str(),
MACE_CHECK(model_weights_data != nullptr && model_weights_data_size != 0); &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 #ifdef MODEL_GRAPH_FORMAT_CODE
create_engine_status = create_engine_status = CreateMaceEngineFromCode(FLAGS_model_name,
CreateMaceEngineFromCode(FLAGS_model_name, reinterpret_cast<const unsigned char *>(model_weights_data->data()),
model_weights_data, model_weights_data->length(),
model_weights_data_size, input_names,
input_names, output_names,
output_names, config,
config, &engine);
&engine);
#else #else
create_engine_status = create_engine_status = CreateMaceEngineFromProto(
CreateMaceEngineFromProto(model_graph_data.data(), reinterpret_cast<const unsigned char *>(model_graph_data->data()),
model_graph_data.size(), model_graph_data->length(),
model_weights_data, reinterpret_cast<const unsigned char *>(model_weights_data->data()),
model_weights_data_size, model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
&engine); &engine);
#endif #endif
if (create_engine_status != MaceStatus::MACE_SUCCESS) { if (create_engine_status != MaceStatus::MACE_SUCCESS) {
LOG(FATAL) << "Create engine error, please check the arguments"; LOG(FATAL) << "Create engine error, please check the arguments";
...@@ -411,10 +396,6 @@ int Main(int argc, char **argv) { ...@@ -411,10 +396,6 @@ int Main(int argc, char **argv) {
statistician->PrintStat(); statistician->PrintStat();
if (model_weights_data != nullptr) {
MemoryUnMap(model_weights_data, model_weights_data_size);
}
return 0; return 0;
} }
......
...@@ -47,10 +47,8 @@ MaceStatus SetOpenMPThreadsAndAffinityCPUs(int omp_num_threads, ...@@ -47,10 +47,8 @@ MaceStatus SetOpenMPThreadsAndAffinityCPUs(int omp_num_threads,
MaceOpenMPThreadCount = omp_num_threads; MaceOpenMPThreadCount = omp_num_threads;
#ifdef MACE_ENABLE_OPENMP #ifdef MACE_ENABLE_OPENMP
if (VLOG_IS_ON(1)) { VLOG(1) << "Set OpenMP threads number: " << omp_num_threads
VLOG(1) << "Set OpenMP threads number: " << omp_num_threads << ", CPU core IDs: " << MakeString(cpu_ids);
<< ", CPU core IDs: " << MakeString(cpu_ids);
}
omp_set_schedule(omp_sched_guided, 1); omp_set_schedule(omp_sched_guided, 1);
omp_set_num_threads(omp_num_threads); omp_set_num_threads(omp_num_threads);
#else #else
...@@ -74,9 +72,7 @@ MaceStatus SetOpenMPThreadsAndAffinityCPUs(int omp_num_threads, ...@@ -74,9 +72,7 @@ MaceStatus SetOpenMPThreadsAndAffinityCPUs(int omp_num_threads,
return MaceStatus::MACE_SUCCESS; return MaceStatus::MACE_SUCCESS;
#else #else
MaceStatus status = SchedSetAffinity(cpu_ids); 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; return status;
#endif #endif
} }
...@@ -89,7 +85,7 @@ MaceStatus CPURuntime::SetOpenMPThreadsAndAffinityPolicy( ...@@ -89,7 +85,7 @@ MaceStatus CPURuntime::SetOpenMPThreadsAndAffinityPolicy(
void *gemm_context) { void *gemm_context) {
// get cpu frequency info // get cpu frequency info
std::vector<float> cpu_max_freqs; std::vector<float> cpu_max_freqs;
MACE_RETURN_IF_ERROR(GetCpuMaxFreq(&cpu_max_freqs)); MACE_RETURN_IF_ERROR(GetCPUMaxFreq(&cpu_max_freqs));
if (cpu_max_freqs.empty()) { if (cpu_max_freqs.empty()) {
return MaceStatus::MACE_RUNTIME_ERROR; return MaceStatus::MACE_RUNTIME_ERROR;
} }
......
...@@ -33,6 +33,7 @@ cc_binary( ...@@ -33,6 +33,7 @@ cc_binary(
"//mace/codegen:generated_libmace", "//mace/codegen:generated_libmace",
"//mace/codegen:generated_opencl_binary", "//mace/codegen:generated_opencl_binary",
"//mace/codegen:generated_opencl_parameter", "//mace/codegen:generated_opencl_parameter",
"//mace/utils:utils_hdrs",
] + if_hexagon_enabled([ ] + if_hexagon_enabled([
"//third_party/nnlib:libhexagon", "//third_party/nnlib:libhexagon",
]), ]),
...@@ -63,5 +64,6 @@ cc_binary( ...@@ -63,5 +64,6 @@ cc_binary(
"//mace/codegen:generated_mace_engine_factory", "//mace/codegen:generated_mace_engine_factory",
"//mace/codegen:generated_opencl_binary", "//mace/codegen:generated_opencl_binary",
"//mace/codegen:generated_opencl_parameter", "//mace/codegen:generated_opencl_parameter",
"//mace/utils:utils_hdrs",
], ],
) )
...@@ -27,7 +27,11 @@ ...@@ -27,7 +27,11 @@
#include <numeric> #include <numeric>
#include "gflags/gflags.h" #include "gflags/gflags.h"
#include "mace/port/env.h"
#include "mace/port/file_system.h"
#include "mace/public/mace.h" #include "mace/public/mace.h"
#include "mace/utils/logging.h"
#include "mace/utils/string_util.h"
// if convert model to code. // if convert model to code.
#ifdef MODEL_GRAPH_FORMAT_CODE #ifdef MODEL_GRAPH_FORMAT_CODE
#include "mace/codegen/engine/mace_engine_factory.h" #include "mace/codegen/engine/mace_engine_factory.h"
...@@ -46,97 +50,6 @@ size_t OpenCLParameterSize(); ...@@ -46,97 +50,6 @@ size_t OpenCLParameterSize();
namespace mace { namespace mace {
namespace examples { namespace examples {
namespace str_util {
std::vector<std::string> Split(const std::string &str, char delims) {
std::vector<std::string> 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<unsigned char> *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<char>(ifs),
std::istreambuf_iterator<char>());
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<size_t>(st.st_size);
*data = static_cast<const unsigned char *>(
mmap(nullptr, *size, PROT_READ, MAP_PRIVATE, fd, 0));
if (*data == static_cast<const unsigned char *>(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<unsigned char *>(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<int64_t> *shape) { void ParseShape(const std::string &str, std::vector<int64_t> *shape) {
std::string tmp = str; std::string tmp = str;
while (!tmp.empty()) { while (!tmp.empty()) {
...@@ -284,16 +197,26 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -284,16 +197,26 @@ bool RunModel(const std::vector<std::string> &input_names,
std::shared_ptr<mace::MaceEngine> engine; std::shared_ptr<mace::MaceEngine> engine;
MaceStatus create_engine_status; MaceStatus create_engine_status;
std::vector<unsigned char> model_graph_data; std::unique_ptr<mace::port::ReadOnlyMemoryRegion> model_graph_data;
if (!ReadBinaryFile(&model_graph_data, FLAGS_model_file)) { if (FLAGS_model_file != "") {
std::cerr << "Failed to read file: " << FLAGS_model_file << std::endl; 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<mace::port::ReadOnlyMemoryRegion> model_weights_data;
if (!MemoryMap(FLAGS_model_data_file, if (FLAGS_model_data_file != "") {
&model_weights_data, auto fs = GetFileSystem();
&model_weights_data_size)) { auto status = fs->NewReadOnlyMemoryRegionFromFile(
std::cerr << "Failed to read file: " << FLAGS_model_data_file << std::endl; 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` // Only choose one of the two type based on the `model_graph_format`
...@@ -301,24 +224,24 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -301,24 +224,24 @@ bool RunModel(const std::vector<std::string> &input_names,
#ifdef MODEL_GRAPH_FORMAT_CODE #ifdef MODEL_GRAPH_FORMAT_CODE
// if model_data_format == code, just pass an empty string("") // if model_data_format == code, just pass an empty string("")
// to model_data_file parameter. // to model_data_file parameter.
create_engine_status = create_engine_status = CreateMaceEngineFromCode(
CreateMaceEngineFromCode(FLAGS_model_name, FLAGS_model_name,
model_weights_data, reinterpret_cast<const unsigned char *>(model_weights_data->data()),
model_weights_data_size, model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
&engine); &engine);
#else #else
create_engine_status = create_engine_status = CreateMaceEngineFromProto(
CreateMaceEngineFromProto(model_graph_data.data(), reinterpret_cast<const unsigned char *>(model_graph_data->data()),
model_graph_data.size(), model_graph_data->length(),
model_weights_data, reinterpret_cast<const unsigned char *>(model_weights_data->data()),
model_weights_data_size, model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
&engine); &engine);
#endif #endif
if (create_engine_status != MaceStatus::MACE_SUCCESS) { if (create_engine_status != MaceStatus::MACE_SUCCESS) {
...@@ -450,10 +373,6 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -450,10 +373,6 @@ bool RunModel(const std::vector<std::string> &input_names,
} }
} }
if (model_weights_data != nullptr) {
MemoryUnMap(model_weights_data, model_weights_data_size);
}
std::cout << "Finished" << std::endl; std::cout << "Finished" << std::endl;
return true; return true;
...@@ -486,13 +405,10 @@ int Main(int argc, char **argv) { ...@@ -486,13 +405,10 @@ int Main(int argc, char **argv) {
<< FLAGS_cpu_affinity_policy << FLAGS_cpu_affinity_policy
<< std::endl; << std::endl;
std::vector<std::string> input_names = str_util::Split(FLAGS_input_node, ','); std::vector<std::string> input_names = Split(FLAGS_input_node, ',');
std::vector<std::string> output_names = std::vector<std::string> output_names = Split(FLAGS_output_node, ',');
str_util::Split(FLAGS_output_node, ','); std::vector<std::string> input_shapes = Split(FLAGS_input_shape, ':');
std::vector<std::string> input_shapes = std::vector<std::string> output_shapes = Split(FLAGS_output_shape, ':');
str_util::Split(FLAGS_input_shape, ':');
std::vector<std::string> output_shapes =
str_util::Split(FLAGS_output_shape, ':');
const size_t input_count = input_shapes.size(); const size_t input_count = input_shapes.size();
const size_t output_count = output_shapes.size(); const size_t output_count = output_shapes.size();
...@@ -506,9 +422,9 @@ int Main(int argc, char **argv) { ...@@ -506,9 +422,9 @@ int Main(int argc, char **argv) {
} }
std::vector<std::string> raw_input_data_formats = std::vector<std::string> raw_input_data_formats =
str_util::Split(FLAGS_input_data_format, ','); Split(FLAGS_input_data_format, ',');
std::vector<std::string> raw_output_data_formats = std::vector<std::string> raw_output_data_formats =
str_util::Split(FLAGS_output_data_format, ','); Split(FLAGS_output_data_format, ',');
std::vector<DataFormat> input_data_formats(input_count); std::vector<DataFormat> input_data_formats(input_count);
std::vector<DataFormat> output_data_formats(output_count); std::vector<DataFormat> output_data_formats(output_count);
for (size_t i = 0; i < input_count; ++i) { for (size_t i = 0; i < input_count; ++i) {
......
...@@ -90,6 +90,7 @@ genrule( ...@@ -90,6 +90,7 @@ genrule(
"//mace/libmace", "//mace/libmace",
"//mace/port:port_base", "//mace/port:port_base",
"//mace/port/posix:port_posix", "//mace/port/posix:port_posix",
"//mace/public",
"//mace/utils", "//mace/utils",
"//mace/proto:mace_cc", "//mace/proto:mace_cc",
"@com_google_protobuf//:protobuf_lite", "@com_google_protobuf//:protobuf_lite",
...@@ -137,6 +138,7 @@ genrule( ...@@ -137,6 +138,7 @@ genrule(
"$(locations //mace/port/darwin:port_darwin) ", "$(locations //mace/port/darwin:port_darwin) ",
default_value = "", default_value = "",
) + ) +
"$(locations //mace/public:public) " +
"$(locations //mace/utils:utils) " + "$(locations //mace/utils:utils) " +
"$(locations //mace/proto:mace_cc) " + "$(locations //mace/proto:mace_cc) " +
"$(locations @com_google_protobuf//:protobuf_lite) " + "$(locations @com_google_protobuf//:protobuf_lite) " +
......
...@@ -10,7 +10,7 @@ mace { ...@@ -10,7 +10,7 @@ mace {
*GetCapability*; *GetCapability*;
# api for static library of models # api for static library of models
*mace*logging*LogMessage*; *mace*port**;
*mace*MaceStatus*; *mace*MaceStatus*;
*mace*NetDef*; *mace*NetDef*;
*mace*MemoryType*; *mace*MemoryType*;
...@@ -21,6 +21,7 @@ mace { ...@@ -21,6 +21,7 @@ mace {
*mace*OperatorDef*; *mace*OperatorDef*;
*mace*ConstTensor*; *mace*ConstTensor*;
*mace*Argument*; *mace*Argument*;
*mace*Split*;
*mace*MemoryBlock*; *mace*MemoryBlock*;
*google*protobuf*; *google*protobuf*;
......
...@@ -43,7 +43,7 @@ cc_test( ...@@ -43,7 +43,7 @@ cc_test(
srcs = glob([ srcs = glob([
"*_test.cc", "*_test.cc",
]), ]),
linkstatic = 0, linkstatic = 1,
deps = [ deps = [
":port", ":port",
"@gtest//:gtest", "@gtest//:gtest",
......
...@@ -61,7 +61,7 @@ LogWriter *AndroidEnv::GetLogWriter() { ...@@ -61,7 +61,7 @@ LogWriter *AndroidEnv::GetLogWriter() {
namespace { namespace {
int GetCpuCount() { int GetCPUCount() {
int cpu_count = 0; int cpu_count = 0;
std::string cpu_sys_conf = "/proc/cpuinfo"; std::string cpu_sys_conf = "/proc/cpuinfo";
std::ifstream f(cpu_sys_conf); std::ifstream f(cpu_sys_conf);
...@@ -115,9 +115,9 @@ size_t BackTrace(void** buffer, size_t max) { ...@@ -115,9 +115,9 @@ size_t BackTrace(void** buffer, size_t max) {
} // namespace } // namespace
MaceStatus AndroidEnv::GetCpuMaxFreq(std::vector<float> *max_freqs) { MaceStatus AndroidEnv::GetCPUMaxFreq(std::vector<float> *max_freqs) {
MACE_CHECK_NOTNULL(max_freqs); MACE_CHECK_NOTNULL(max_freqs);
int cpu_count = GetCpuCount(); int cpu_count = GetCPUCount();
if (cpu_count < 0) { if (cpu_count < 0) {
return MaceStatus::MACE_RUNTIME_ERROR; return MaceStatus::MACE_RUNTIME_ERROR;
} }
...@@ -142,7 +142,7 @@ MaceStatus AndroidEnv::GetCpuMaxFreq(std::vector<float> *max_freqs) { ...@@ -142,7 +142,7 @@ MaceStatus AndroidEnv::GetCpuMaxFreq(std::vector<float> *max_freqs) {
f.close(); 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; return MaceStatus::MACE_SUCCESS;
} }
......
...@@ -29,7 +29,7 @@ namespace port { ...@@ -29,7 +29,7 @@ namespace port {
class AndroidEnv : public Env { class AndroidEnv : public Env {
public: public:
int64_t NowMicros() override; int64_t NowMicros() override;
MaceStatus GetCpuMaxFreq(std::vector<float> *max_freqs) override; MaceStatus GetCPUMaxFreq(std::vector<float> *max_freqs) override;
MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids) override; MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids) override;
FileSystem *GetFileSystem() override; FileSystem *GetFileSystem() override;
LogWriter *GetLogWriter() override; LogWriter *GetLogWriter() override;
......
...@@ -48,7 +48,7 @@ void AndroidLogWriter::WriteLogMessage(const char *fname, ...@@ -48,7 +48,7 @@ void AndroidLogWriter::WriteLogMessage(const char *fname,
const char *const partial_name = strrchr(fname, '/'); const char *const partial_name = strrchr(fname, '/');
ss << (partial_name != nullptr ? partial_name + 1 : fname) << ":" << line ss << (partial_name != nullptr ? partial_name + 1 : fname) << ":" << line
<< " " << message; << " " << 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. // Also log to stderr (for standalone Android apps) and abort.
LogWriter::WriteLogMessage(fname, line, severity, message); LogWriter::WriteLogMessage(fname, line, severity, message);
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
namespace mace { namespace mace {
namespace port { namespace port {
MaceStatus Env::GetCpuMaxFreq(std::vector<float> *max_freqs) { MaceStatus Env::GetCPUMaxFreq(std::vector<float> *max_freqs) {
return MaceStatus::MACE_UNSUPPORTED; return MaceStatus::MACE_UNSUPPORTED;
} }
......
...@@ -38,7 +38,7 @@ class LogWriter; ...@@ -38,7 +38,7 @@ class LogWriter;
class Env { class Env {
public: public:
virtual int64_t NowMicros() = 0; virtual int64_t NowMicros() = 0;
virtual MaceStatus GetCpuMaxFreq(std::vector<float> *max_freqs); virtual MaceStatus GetCPUMaxFreq(std::vector<float> *max_freqs);
virtual MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids); virtual MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids);
virtual FileSystem *GetFileSystem() = 0; virtual FileSystem *GetFileSystem() = 0;
virtual LogWriter *GetLogWriter() = 0; virtual LogWriter *GetLogWriter() = 0;
...@@ -58,8 +58,8 @@ inline int64_t NowMicros() { ...@@ -58,8 +58,8 @@ inline int64_t NowMicros() {
return port::Env::Default()->NowMicros(); return port::Env::Default()->NowMicros();
} }
inline MaceStatus GetCpuMaxFreq(std::vector<float> *max_freqs) { inline MaceStatus GetCPUMaxFreq(std::vector<float> *max_freqs) {
return port::Env::Default()->GetCpuMaxFreq(max_freqs); return port::Env::Default()->GetCPUMaxFreq(max_freqs);
} }
inline MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids) { inline MaceStatus SchedSetAffinity(const std::vector<size_t> &cpu_ids) {
......
...@@ -30,9 +30,9 @@ TEST_F(EnvTest, GetFileSystem) { ...@@ -30,9 +30,9 @@ TEST_F(EnvTest, GetFileSystem) {
GetFileSystem(); GetFileSystem();
} }
TEST_F(EnvTest, CpuInfo) { TEST_F(EnvTest, CPUInfo) {
std::vector<float> freq; std::vector<float> freq;
GetCpuMaxFreq(&freq); GetCPUMaxFreq(&freq);
std::vector<size_t> cpu_ids; std::vector<size_t> cpu_ids;
SchedSetAffinity(cpu_ids); SchedSetAffinity(cpu_ids);
} }
......
...@@ -34,7 +34,9 @@ class PosixReadOnlyMemoryRegion : public ReadOnlyMemoryRegion { ...@@ -34,7 +34,9 @@ class PosixReadOnlyMemoryRegion : public ReadOnlyMemoryRegion {
PosixReadOnlyMemoryRegion(const void* addr, uint64_t length) PosixReadOnlyMemoryRegion(const void* addr, uint64_t length)
: addr_(addr), length_(length) {} : addr_(addr), length_(length) {}
~PosixReadOnlyMemoryRegion() override { ~PosixReadOnlyMemoryRegion() override {
munmap(const_cast<void *>(addr_), length_); if (length_ > 0) {
munmap(const_cast<void *>(addr_), length_);
}
}; };
const void *data() override { return addr_; }; const void *data() override { return addr_; };
uint64_t length() override { return length_; }; uint64_t length() override { return length_; };
...@@ -56,15 +58,20 @@ MaceStatus PosixFileSystem::NewReadOnlyMemoryRegionFromFile( ...@@ -56,15 +58,20 @@ MaceStatus PosixFileSystem::NewReadOnlyMemoryRegionFromFile(
} else { } else {
struct stat st; struct stat st;
fstat(fd, &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); mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (address == MAP_FAILED) { if (address == MAP_FAILED) {
// TODO(heliangliang) check errno // TODO(heliangliang) check errno
s = MaceStatus(MaceStatus::MACE_RUNTIME_ERROR); s = MaceStatus(MaceStatus::MACE_RUNTIME_ERROR);
} else {
*result = make_unique<PosixReadOnlyMemoryRegion>(address, st.st_size);
}
close(fd);
} else { } else {
*result = make_unique<PosixReadOnlyMemoryRegion>(address, st.st_size); // Empty file: mmap returns EINVAL (since Linux 2.6.12) length was 0
*result = make_unique<PosixReadOnlyMemoryRegion>(nullptr, 0);
} }
close(fd);
} }
return s; return s;
} }
......
...@@ -29,14 +29,8 @@ cc_binary( ...@@ -29,14 +29,8 @@ cc_binary(
] + if_opencl_enabled([ ] + if_opencl_enabled([
"-DMACE_ENABLE_OPENCL", "-DMACE_ENABLE_OPENCL",
]), ]),
linkopts = [ linkopts = if_openmp_enabled([
"-lm", "-fopenmp",
] + if_openmp_enabled([
"-fopenmp"
]) + if_android([
"-ldl",
"-pie",
"-llog",
]), ]),
linkstatic = 0, linkstatic = 0,
deps = [ deps = [
......
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
* --model_data_file=model_data.data \ * --model_data_file=model_data.data \
* --device=GPU * --device=GPU
*/ */
#include <cstdint> #include <malloc.h>
#include <stdint.h>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
...@@ -34,6 +35,7 @@ ...@@ -34,6 +35,7 @@
#include "gflags/gflags.h" #include "gflags/gflags.h"
#include "mace/public/mace.h" #include "mace/public/mace.h"
#include "mace/port/env.h" #include "mace/port/env.h"
#include "mace/port/file_system.h"
#include "mace/utils/logging.h" #include "mace/utils/logging.h"
#include "mace/utils/string_util.h" #include "mace/utils/string_util.h"
...@@ -238,19 +240,24 @@ bool RunModel(const std::string &model_name, ...@@ -238,19 +240,24 @@ bool RunModel(const std::string &model_name,
} }
#endif // MACE_ENABLE_OPENCL #endif // MACE_ENABLE_OPENCL
std::vector<unsigned char> model_graph_data; std::unique_ptr<mace::port::ReadOnlyMemoryRegion> model_graph_data;
if (FLAGS_model_file != "") { 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; LOG(FATAL) << "Failed to read file: " << FLAGS_model_file;
} }
} }
const unsigned char *model_weights_data = nullptr; std::unique_ptr<mace::port::ReadOnlyMemoryRegion> model_weights_data;
size_t model_weights_data_size = 0;
if (FLAGS_model_data_file != "") { if (FLAGS_model_data_file != "") {
MemoryMap(FLAGS_model_data_file, auto fs = GetFileSystem();
&model_weights_data, status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_data_file.c_str(),
&model_weights_data_size); &model_weights_data);
if (status != MaceStatus::MACE_SUCCESS) {
LOG(FATAL) << "Failed to read file: " << FLAGS_model_data_file;
}
} }
std::shared_ptr<mace::MaceEngine> engine; std::shared_ptr<mace::MaceEngine> engine;
...@@ -262,8 +269,9 @@ bool RunModel(const std::string &model_name, ...@@ -262,8 +269,9 @@ bool RunModel(const std::string &model_name,
#ifdef MODEL_GRAPH_FORMAT_CODE #ifdef MODEL_GRAPH_FORMAT_CODE
create_engine_status = create_engine_status =
CreateMaceEngineFromCode(model_name, CreateMaceEngineFromCode(model_name,
model_weights_data, reinterpret_cast<const unsigned char *>(
model_weights_data_size, model_weights_data->data()),
model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
...@@ -271,10 +279,12 @@ bool RunModel(const std::string &model_name, ...@@ -271,10 +279,12 @@ bool RunModel(const std::string &model_name,
#else #else
(void)(model_name); (void)(model_name);
create_engine_status = create_engine_status =
CreateMaceEngineFromProto(model_graph_data.data(), CreateMaceEngineFromProto(reinterpret_cast<const unsigned char *>(
model_graph_data.size(), model_graph_data->data()),
model_weights_data, model_graph_data->length(),
model_weights_data_size, reinterpret_cast<const unsigned char *>(
model_weights_data->data()),
model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
...@@ -345,18 +355,21 @@ bool RunModel(const std::string &model_name, ...@@ -345,18 +355,21 @@ bool RunModel(const std::string &model_name,
#ifdef MODEL_GRAPH_FORMAT_CODE #ifdef MODEL_GRAPH_FORMAT_CODE
create_engine_status = create_engine_status =
CreateMaceEngineFromCode(model_name, CreateMaceEngineFromCode(model_name,
model_weights_data, reinterpret_cast<const unsigned char *>(
model_weights_data_size, model_weights_data->data()),
model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
&engine); &engine);
#else #else
create_engine_status = create_engine_status =
CreateMaceEngineFromProto(model_graph_data.data(), CreateMaceEngineFromProto(reinterpret_cast<const unsigned char *>(
model_graph_data.size(), model_graph_data->data()),
model_weights_data, model_graph_data->length(),
model_weights_data_size, reinterpret_cast<const unsigned char *>(
model_weights_data->data()),
model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
...@@ -388,22 +401,26 @@ bool RunModel(const std::string &model_name, ...@@ -388,22 +401,26 @@ bool RunModel(const std::string &model_name,
#ifdef MODEL_GRAPH_FORMAT_CODE #ifdef MODEL_GRAPH_FORMAT_CODE
create_engine_status = create_engine_status =
CreateMaceEngineFromCode(model_name, CreateMaceEngineFromCode(model_name,
model_weights_data, reinterpret_cast<const unsigned char *>(
model_weights_data_size, model_weights_data->data()),
model_weights_data->length(),
input_names, input_names,
output_names, output_names,
config, config,
&engine); &engine);
#else #else
create_engine_status = create_engine_status =
CreateMaceEngineFromProto(model_graph_data.data(), CreateMaceEngineFromProto(
model_graph_data.size(), reinterpret_cast<const unsigned char *>(
model_weights_data, model_graph_data->data()),
model_weights_data_size, model_graph_data->length(),
input_names, reinterpret_cast<const unsigned char *>(
output_names, model_weights_data->data()),
config, model_weights_data->length(),
&engine); input_names,
output_names,
config,
&engine);
#endif #endif
} while (create_engine_status != MaceStatus::MACE_SUCCESS); } while (create_engine_status != MaceStatus::MACE_SUCCESS);
} else { } else {
...@@ -445,10 +462,6 @@ bool RunModel(const std::string &model_name, ...@@ -445,10 +462,6 @@ bool RunModel(const std::string &model_name,
<< output_size << " done."; << output_size << " done.";
} }
if (model_weights_data != nullptr) {
MemoryUnMap(model_weights_data, model_weights_data_size);
}
return true; return true;
} }
...@@ -492,9 +505,9 @@ int Main(int argc, char **argv) { ...@@ -492,9 +505,9 @@ int Main(int argc, char **argv) {
ParseShape(output_shapes[i], &output_shape_vec[i]); ParseShape(output_shapes[i], &output_shape_vec[i]);
} }
std::vector<std::string> raw_input_data_formats = std::vector<std::string> raw_input_data_formats =
str_util::Split(FLAGS_input_data_format, ','); Split(FLAGS_input_data_format, ',');
std::vector<std::string> raw_output_data_formats = std::vector<std::string> raw_output_data_formats =
str_util::Split(FLAGS_output_data_format, ','); Split(FLAGS_output_data_format, ',');
std::vector<DataFormat> input_data_formats(input_count); std::vector<DataFormat> input_data_formats(input_count);
std::vector<DataFormat> output_data_formats(output_count); std::vector<DataFormat> output_data_formats(output_count);
for (size_t i = 0; i < input_count; ++i) { for (size_t i = 0; i < input_count; ++i) {
......
...@@ -7,6 +7,22 @@ package( ...@@ -7,6 +7,22 @@ package(
licenses(["notice"]) # Apache 2.0 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( cc_library(
name = "utils", name = "utils",
srcs = glob( srcs = glob(
...@@ -17,18 +33,15 @@ cc_library( ...@@ -17,18 +33,15 @@ cc_library(
"*_test.cc", "*_test.cc",
], ],
), ),
hdrs = glob([
"*.h",
]),
copts = [ copts = [
"-Werror", "-Werror",
"-Wextra", "-Wextra",
"-Wno-missing-field-initializers", "-Wno-missing-field-initializers",
], ],
deps = [ deps = [
"//mace/port:port_api", ":utils_hdrs",
"//mace/public",
], ],
alwayslink = 1,
) )
cc_test( cc_test(
...@@ -44,7 +57,7 @@ cc_test( ...@@ -44,7 +57,7 @@ cc_test(
"-Wextra", "-Wextra",
"-Wno-missing-field-initializers", "-Wno-missing-field-initializers",
], ],
linkstatic = 0, linkstatic = 1,
deps = [ deps = [
":utils", ":utils",
"//mace/port", "//mace/port",
......
...@@ -29,14 +29,12 @@ TEST_F(LoggingTest, Basic) { ...@@ -29,14 +29,12 @@ TEST_F(LoggingTest, Basic) {
VLOG(1) << "vlog 1 logging"; VLOG(1) << "vlog 1 logging";
VLOG(2) << "vlog 2 logging"; VLOG(2) << "vlog 2 logging";
if (VLOG_IS_ON(1)) {
VLOG(1) << "vlog 1 logging";
}
} }
TEST_F(LoggingTest, LogFatal) { TEST_F(LoggingTest, LogFatal) {
#ifdef GTEST_HAS_DEATH_TEST
EXPECT_DEATH(do { LOG(FATAL) << "fatal logging"; } while (false), ""); EXPECT_DEATH(do { LOG(FATAL) << "fatal logging"; } while (false), "");
#endif
} }
} // namespace } // namespace
......
...@@ -78,16 +78,14 @@ class Tuner { ...@@ -78,16 +78,14 @@ class Tuner {
std::vector<param_type> opt_param = default_param; std::vector<param_type> opt_param = default_param;
RetType res = Tune<RetType>(param_generator, func, timer, &opt_param); RetType res = Tune<RetType>(param_generator, func, timer, &opt_param);
VLOG(3) << "Tuning " << param_key VLOG(3) << "Tuning " << param_key
<< " retult: " << (VLOG_IS_ON(3) ? MakeString(opt_param) : ""); << " retult: " << MakeString(opt_param);
param_table_[obfucated_param_key] = opt_param; param_table_[obfucated_param_key] = opt_param;
return res; return res;
} else { } else {
// run // run
if (param_table_.find(obfucated_param_key) != param_table_.end()) { if (param_table_.find(obfucated_param_key) != param_table_.end()) {
VLOG(3) << param_key << ": " 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); return func(param_table_[obfucated_param_key], nullptr, nullptr);
} else { } else {
return func(default_param, nullptr, nullptr); return func(default_param, nullptr, nullptr);
...@@ -114,7 +112,7 @@ class Tuner { ...@@ -114,7 +112,7 @@ class Tuner {
sizeof(params_size)); sizeof(params_size));
VLOG(3) << "Write tuning param: " << kp.first.c_str() << ": " VLOG(3) << "Write tuning param: " << kp.first.c_str() << ": "
<< (VLOG_IS_ON(3) ? MakeString(params) : ""); << MakeString(params);
for (auto &param : params) { for (auto &param : params) {
ofs.write(reinterpret_cast<char *>(&param), sizeof(params_size)); ofs.write(reinterpret_cast<char *>(&param), sizeof(params_size));
} }
......
...@@ -17,15 +17,41 @@ build:symbol_hidden --copt=-fvisibility=hidden ...@@ -17,15 +17,41 @@ build:symbol_hidden --copt=-fvisibility=hidden
build:android --linkopt=-pie build:android --linkopt=-pie
build:android --linkopt=-ldl build:android --linkopt=-ldl
build:android --linkopt=-llog build:android --linkopt=-llog
build:android --linkopt=-lm
build:android --distinct_host_configuration=true build:android --distinct_host_configuration=true
build:android --crosstool_top=//external:android/crosstool build:android --crosstool_top=//external:android/crosstool
build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain 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 # Usage example: bazel build --config arm_linux_gnueabihf
# Used to fix library not find linking issue, see also: # Used to fix library not find linking issue, see also:
# https://github.com/bazelbuild/bazel/issues/6653, # https://github.com/bazelbuild/bazel/issues/6653,
# https://github.com/bazelbuild/bazel/issues/6189 # 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 --spawn_strategy=standalone
build:arm_linux_gnueabihf --distinct_host_configuration=true build:arm_linux_gnueabihf --distinct_host_configuration=true
build:arm_linux_gnueabihf --crosstool_top=//tools/arm_compiler:toolchain build:arm_linux_gnueabihf --crosstool_top=//tools/arm_compiler:toolchain
...@@ -39,6 +65,7 @@ build:arm_linux_gnueabihf --copt -Wno-sequence-point ...@@ -39,6 +65,7 @@ build:arm_linux_gnueabihf --copt -Wno-sequence-point
build:arm_linux_gnueabihf --copt -Wno-implicit-fallthrough build:arm_linux_gnueabihf --copt -Wno-implicit-fallthrough
# Usage example: bazel build --config aarch64_linux_gnu # 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 --spawn_strategy=standalone
build:aarch64_linux_gnu --distinct_host_configuration=true build:aarch64_linux_gnu --distinct_host_configuration=true
build:aarch64_linux_gnu --crosstool_top=//tools/aarch64_compiler:toolchain build:aarch64_linux_gnu --crosstool_top=//tools/aarch64_compiler:toolchain
......
...@@ -275,6 +275,8 @@ def bazel_build(target, ...@@ -275,6 +275,8 @@ def bazel_build(target,
if abi == "host": if abi == "host":
bazel_args = ( bazel_args = (
"build", "build",
"--config",
platform.system().lower(),
"--define", "--define",
"openmp=%s" % str(enable_openmp).lower(), "openmp=%s" % str(enable_openmp).lower(),
"--define", "--define",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册