提交 af787702 编写于 作者: L Liangliang He

Merge branch 'code-style' into 'master'

Format code.

See merge request !316
...@@ -2,16 +2,18 @@ ...@@ -2,16 +2,18 @@
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#include "gflags/gflags.h"
#include "mace/public/mace.h" #include <sys/time.h>
#include "mace/utils/logging.h"
#include "mace/benchmark/stat_summarizer.h"
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <thread>
#include <numeric> #include <numeric>
#include <sys/time.h> #include <thread> // NOLINT(build/c++11)
#include "gflags/gflags.h"
#include "mace/public/mace.h"
#include "mace/utils/logging.h"
#include "mace/benchmark/stat_summarizer.h"
namespace mace { namespace mace {
namespace MACE_MODEL_TAG { namespace MACE_MODEL_TAG {
...@@ -24,10 +26,11 @@ extern NetDef CreateNet(const unsigned char *model_data); ...@@ -24,10 +26,11 @@ extern NetDef CreateNet(const unsigned char *model_data);
extern const std::string ModelChecksum(); extern const std::string ModelChecksum();
} } // namespace MACE_MODEL_TAG
} } // namespace mace
namespace mace { namespace mace {
namespace benchmark {
namespace str_util { namespace str_util {
std::vector<std::string> Split(const std::string &str, char delims) { std::vector<std::string> Split(const std::string &str, char delims) {
...@@ -64,8 +67,6 @@ bool SplitAndParseToInts(const std::string &str, ...@@ -64,8 +67,6 @@ bool SplitAndParseToInts(const std::string &str,
} // namespace str_util } // namespace str_util
namespace benchmark {
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()) {
...@@ -96,18 +97,19 @@ inline int64_t NowMicros() { ...@@ -96,18 +97,19 @@ inline int64_t NowMicros() {
bool RunInference(MaceEngine *engine, bool RunInference(MaceEngine *engine,
const std::vector<mace::MaceInputInfo> &input_infos, const std::vector<mace::MaceInputInfo> &input_infos,
std::map<std::string, float*> &output_infos, std::map<std::string, float*> *output_infos,
StatSummarizer *summarizer, StatSummarizer *summarizer,
int64_t *inference_time_us) { int64_t *inference_time_us) {
MACE_CHECK_NOTNULL(output_infos);
RunMetadata run_metadata; RunMetadata run_metadata;
RunMetadata *run_metadata_ptr = nullptr; RunMetadata *run_metadata_ptr = nullptr;
if (summarizer) { if (summarizer) {
run_metadata_ptr = &run_metadata; run_metadata_ptr = &run_metadata;
} }
if (input_infos.size() == 1 && output_infos.size() == 1) { if (input_infos.size() == 1 && output_infos->size() == 1) {
const int64_t start_time = NowMicros(); const int64_t start_time = NowMicros();
bool s = engine->Run(input_infos[0].data, input_infos[0].shape, bool s = engine->Run(input_infos[0].data, input_infos[0].shape,
output_infos.begin()->second, run_metadata_ptr); output_infos->begin()->second, run_metadata_ptr);
const int64_t end_time = NowMicros(); const int64_t end_time = NowMicros();
if (!s) { if (!s) {
...@@ -117,7 +119,7 @@ bool RunInference(MaceEngine *engine, ...@@ -117,7 +119,7 @@ bool RunInference(MaceEngine *engine,
*inference_time_us = end_time - start_time; *inference_time_us = end_time - start_time;
} else { } else {
const int64_t start_time = NowMicros(); const int64_t start_time = NowMicros();
bool s = engine->Run(input_infos, output_infos, run_metadata_ptr); bool s = engine->Run(input_infos, *output_infos, run_metadata_ptr);
const int64_t end_time = NowMicros(); const int64_t end_time = NowMicros();
if (!s) { if (!s) {
...@@ -136,13 +138,14 @@ bool RunInference(MaceEngine *engine, ...@@ -136,13 +138,14 @@ bool RunInference(MaceEngine *engine,
bool Run(MaceEngine *engine, bool Run(MaceEngine *engine,
const std::vector<mace::MaceInputInfo> &input_infos, const std::vector<mace::MaceInputInfo> &input_infos,
std::map<std::string, float*> &output_infos, std::map<std::string, float*> *output_infos,
StatSummarizer *summarizer, StatSummarizer *summarizer,
int num_runs, int num_runs,
double max_time_sec, double max_time_sec,
int64_t sleep_sec, int64_t sleep_sec,
int64_t *total_time_us, int64_t *total_time_us,
int64_t *actual_num_runs) { int64_t *actual_num_runs) {
MACE_CHECK_NOTNULL(output_infos);
*total_time_us = 0; *total_time_us = 0;
LOG(INFO) << "Running benchmark for max " << num_runs << " iterators, max " LOG(INFO) << "Running benchmark for max " << num_runs << " iterators, max "
...@@ -156,7 +159,8 @@ bool Run(MaceEngine *engine, ...@@ -156,7 +159,8 @@ bool Run(MaceEngine *engine,
bool util_max_time = (num_runs <= 0); bool util_max_time = (num_runs <= 0);
for (int i = 0; util_max_time || i < num_runs; ++i) { for (int i = 0; util_max_time || i < num_runs; ++i) {
int64_t inference_time_us = 0; int64_t inference_time_us = 0;
bool s = RunInference(engine, input_infos, output_infos, summarizer, &inference_time_us); bool s = RunInference(engine, input_infos, output_infos,
summarizer, &inference_time_us);
stat.UpdateStat(inference_time_us); stat.UpdateStat(inference_time_us);
(*total_time_us) += inference_time_us; (*total_time_us) += inference_time_us;
++(*actual_num_runs); ++(*actual_num_runs);
...@@ -183,15 +187,18 @@ bool Run(MaceEngine *engine, ...@@ -183,15 +187,18 @@ bool Run(MaceEngine *engine,
} }
DEFINE_string(device, "CPU", "Device [CPU|OPENCL]"); DEFINE_string(device, "CPU", "Device [CPU|OPENCL]");
DEFINE_string(input_node, "input_node0,input_node1", "input nodes, separated by comma"); DEFINE_string(input_node, "input_node0,input_node1",
DEFINE_string(output_node, "output_node0,output_node1", "output nodes, separated by comma"); "input nodes, separated by comma");
DEFINE_string(output_node, "output_node0,output_node1",
"output nodes, separated by comma");
DEFINE_string(input_shape, "", "input shape, separated by colon and comma"); DEFINE_string(input_shape, "", "input shape, separated by colon and comma");
DEFINE_string(output_shape, "", "output shape, separated by colon and comma"); DEFINE_string(output_shape, "", "output shape, separated by colon and comma");
DEFINE_string(input_file, "", "input file name"); DEFINE_string(input_file, "", "input file name");
DEFINE_int32(max_num_runs, 100, "number of runs max"); DEFINE_int32(max_num_runs, 100, "number of runs max");
DEFINE_string(max_time, "10.0", "length to run max"); DEFINE_string(max_time, "10.0", "length to run max");
DEFINE_string(inference_delay, "-1", "delay between runs in seconds"); DEFINE_string(inference_delay, "-1", "delay between runs in seconds");
DEFINE_string(inter_benchmark_delay, "-1", "delay between benchmarks in seconds"); DEFINE_string(inter_benchmark_delay, "-1",
"delay between benchmarks in seconds");
DEFINE_string(benchmark_name, "", "benchmark name"); DEFINE_string(benchmark_name, "", "benchmark name");
DEFINE_bool(show_run_order, true, "whether to list stats by run order"); DEFINE_bool(show_run_order, true, "whether to list stats by run order");
DEFINE_int32(run_order_limit, 0, "how many items to show by run order"); DEFINE_int32(run_order_limit, 0, "how many items to show by run order");
...@@ -203,15 +210,18 @@ DEFINE_bool(show_type, true, "whether to list stats by op type"); ...@@ -203,15 +210,18 @@ DEFINE_bool(show_type, true, "whether to list stats by op type");
DEFINE_bool(show_summary, true, "whether to show a summary of the stats"); DEFINE_bool(show_summary, true, "whether to show a summary of the stats");
DEFINE_bool(show_flops, true, "whether to estimate the model's FLOPs"); DEFINE_bool(show_flops, true, "whether to estimate the model's FLOPs");
DEFINE_int32(warmup_runs, 1, "how many runs to initialize model"); DEFINE_int32(warmup_runs, 1, "how many runs to initialize model");
DEFINE_string(model_data_file, "", "model data file name, used when EMBED_MODEL_DATA set to 0"); DEFINE_string(model_data_file, "",
"model data file name, used when EMBED_MODEL_DATA set to 0");
DEFINE_string(gpu_type, "ADRENO", "ADRENO/MALI"); DEFINE_string(gpu_type, "ADRENO", "ADRENO/MALI");
DEFINE_int32(gpu_perf_hint, 2, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH"); DEFINE_int32(gpu_perf_hint, 2, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH");
DEFINE_int32(gpu_priority_hint, 1, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH"); DEFINE_int32(gpu_priority_hint, 1, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH");
DEFINE_int32(omp_num_threads, 8, "num of openmp threads"); DEFINE_int32(omp_num_threads, 8, "num of openmp threads");
DEFINE_int32(cpu_power_option, 0, "0:DEFAULT/1:HIGH_PERFORMANCE/2:BATTERY_SAVE"); DEFINE_int32(cpu_power_option, 0,
"0:DEFAULT/1:HIGH_PERFORMANCE/2:BATTERY_SAVE");
int Main(int argc, char **argv) { int Main(int argc, char **argv) {
MACE_CHECK(FLAGS_device != "HEXAGON", "Model benchmark tool do not support DSP."); MACE_CHECK(FLAGS_device != "HEXAGON",
"Model benchmark tool do not support DSP.");
gflags::SetUsageMessage("some usage message"); gflags::SetUsageMessage("some usage message");
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
...@@ -228,12 +238,14 @@ int Main(int argc, char **argv) { ...@@ -228,12 +238,14 @@ int Main(int argc, char **argv) {
LOG(INFO) << "output shapes: [" << FLAGS_output_shape << "]"; LOG(INFO) << "output shapes: [" << FLAGS_output_shape << "]";
LOG(INFO) << "Warmup runs: [" << FLAGS_warmup_runs << "]"; LOG(INFO) << "Warmup runs: [" << FLAGS_warmup_runs << "]";
LOG(INFO) << "Num runs: [" << FLAGS_max_num_runs << "]"; LOG(INFO) << "Num runs: [" << FLAGS_max_num_runs << "]";
LOG(INFO) << "Inter-inference delay (seconds): [" << FLAGS_inference_delay << "]"; LOG(INFO) << "Inter-inference delay (seconds): ["
LOG(INFO) << "Inter-benchmark delay (seconds): [" << FLAGS_inter_benchmark_delay << "]"; << FLAGS_inference_delay << "]";
LOG(INFO) << "Inter-benchmark delay (seconds): ["
<< FLAGS_inter_benchmark_delay << "]";
const long int inter_inference_sleep_seconds = const int64_t inter_inference_sleep_seconds =
std::strtol(FLAGS_inference_delay.c_str(), nullptr, 10); std::strtol(FLAGS_inference_delay.c_str(), nullptr, 10);
const long int inter_benchmark_sleep_seconds = const int64_t inter_benchmark_sleep_seconds =
std::strtol(FLAGS_inter_benchmark_delay.c_str(), nullptr, 10); std::strtol(FLAGS_inter_benchmark_delay.c_str(), nullptr, 10);
const double max_benchmark_time_seconds = const double max_benchmark_time_seconds =
std::strtod(FLAGS_max_time.c_str(), nullptr); std::strtod(FLAGS_max_time.c_str(), nullptr);
...@@ -252,7 +264,7 @@ int Main(int argc, char **argv) { ...@@ -252,7 +264,7 @@ int Main(int argc, char **argv) {
stats.reset(new StatSummarizer(stats_options)); stats.reset(new StatSummarizer(stats_options));
DeviceType device_type = CPU; DeviceType device_type = CPU;
if(FLAGS_device == "OPENCL") { if (FLAGS_device == "OPENCL") {
device_type = OPENCL; device_type = OPENCL;
} }
...@@ -264,17 +276,20 @@ int Main(int argc, char **argv) { ...@@ -264,17 +276,20 @@ int Main(int argc, char **argv) {
gpu_type, gpu_type,
static_cast<GPUPerfHint>(FLAGS_gpu_perf_hint), static_cast<GPUPerfHint>(FLAGS_gpu_perf_hint),
static_cast<GPUPriorityHint>(FLAGS_gpu_priority_hint)); static_cast<GPUPriorityHint>(FLAGS_gpu_priority_hint));
} } else if (device_type == CPU) {
else if (device_type == CPU) {
mace::ConfigOmpThreadsAndAffinity( mace::ConfigOmpThreadsAndAffinity(
FLAGS_omp_num_threads, FLAGS_omp_num_threads,
static_cast<CPUPowerOption>(FLAGS_cpu_power_option)); static_cast<CPUPowerOption>(FLAGS_cpu_power_option));
} }
std::vector<std::string> input_names = str_util::Split(FLAGS_input_node, ','); std::vector<std::string> input_names =
std::vector<std::string> output_names = str_util::Split(FLAGS_output_node, ','); str_util::Split(FLAGS_input_node, ',');
std::vector<std::string> input_shapes = str_util::Split(FLAGS_input_shape, ':'); std::vector<std::string> output_names =
std::vector<std::string> output_shapes = str_util::Split(FLAGS_output_shape, ':'); str_util::Split(FLAGS_output_node, ',');
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();
...@@ -298,10 +313,12 @@ int Main(int argc, char **argv) { ...@@ -298,10 +313,12 @@ int Main(int argc, char **argv) {
for (size_t i = 0; i < input_count; ++i) { for (size_t i = 0; i < input_count; ++i) {
int64_t input_size = std::accumulate(input_shape_vec[i].begin(), int64_t input_size = std::accumulate(input_shape_vec[i].begin(),
input_shape_vec[i].end(), 1, std::multiplies<int64_t>()); input_shape_vec[i].end(), 1,
std::multiplies<int64_t>());
input_datas[i].reset(new float[input_size]); input_datas[i].reset(new float[input_size]);
// load input // load input
std::ifstream in_file(FLAGS_input_file + "_" + FormatName(input_names[i]), std::ios::in | std::ios::binary); std::ifstream in_file(FLAGS_input_file + "_" + FormatName(input_names[i]),
std::ios::in | std::ios::binary);
if (in_file.is_open()) { if (in_file.is_open()) {
in_file.read(reinterpret_cast<char *>(input_datas[i].get()), in_file.read(reinterpret_cast<char *>(input_datas[i].get()),
input_size * sizeof(float)); input_size * sizeof(float));
...@@ -317,7 +334,8 @@ int Main(int argc, char **argv) { ...@@ -317,7 +334,8 @@ int Main(int argc, char **argv) {
} }
for (size_t i = 0; i < output_count; ++i) { for (size_t i = 0; i < output_count; ++i) {
int64_t output_size = std::accumulate(output_shape_vec[i].begin(), int64_t output_size = std::accumulate(output_shape_vec[i].begin(),
output_shape_vec[i].end(), 1, std::multiplies<int64_t>()); output_shape_vec[i].end(), 1,
std::multiplies<int64_t>());
output_datas[i].reset(new float[output_size]); output_datas[i].reset(new float[output_size]);
output_infos[output_names[i]] = output_datas[i].get(); output_infos[output_names[i]] = output_datas[i].get();
} }
...@@ -328,7 +346,8 @@ int Main(int argc, char **argv) { ...@@ -328,7 +346,8 @@ int Main(int argc, char **argv) {
if (input_count == 1 && output_count == 1) { if (input_count == 1 && output_count == 1) {
engine_ptr.reset(new mace::MaceEngine(&net_def, device_type)); engine_ptr.reset(new mace::MaceEngine(&net_def, device_type));
} else { } else {
engine_ptr.reset(new mace::MaceEngine(&net_def, device_type, input_names, output_names)); engine_ptr.reset(new mace::MaceEngine(&net_def, device_type,
input_names, output_names));
} }
if (device_type == DeviceType::OPENCL) { if (device_type == DeviceType::OPENCL) {
mace::MACE_MODEL_TAG::UnloadModelData(model_data); mace::MACE_MODEL_TAG::UnloadModelData(model_data);
...@@ -340,7 +359,8 @@ int Main(int argc, char **argv) { ...@@ -340,7 +359,8 @@ int Main(int argc, char **argv) {
int64_t num_warmup_runs = 0; int64_t num_warmup_runs = 0;
if (FLAGS_warmup_runs > 0) { if (FLAGS_warmup_runs > 0) {
bool status = bool status =
Run(engine_ptr.get(), input_infos, output_infos, nullptr, FLAGS_warmup_runs, -1.0, Run(engine_ptr.get(), input_infos, &output_infos, nullptr,
FLAGS_warmup_runs, -1.0,
inter_inference_sleep_seconds, &warmup_time_us, &num_warmup_runs); inter_inference_sleep_seconds, &warmup_time_us, &num_warmup_runs);
if (!status) { if (!status) {
LOG(ERROR) << "Failed at warm up run"; LOG(ERROR) << "Failed at warm up run";
...@@ -354,7 +374,7 @@ int Main(int argc, char **argv) { ...@@ -354,7 +374,7 @@ int Main(int argc, char **argv) {
int64_t no_stat_time_us = 0; int64_t no_stat_time_us = 0;
int64_t no_stat_runs = 0; int64_t no_stat_runs = 0;
bool status = bool status =
Run(engine_ptr.get(), input_infos, output_infos, Run(engine_ptr.get(), input_infos, &output_infos,
nullptr, FLAGS_max_num_runs, max_benchmark_time_seconds, nullptr, FLAGS_max_num_runs, max_benchmark_time_seconds,
inter_inference_sleep_seconds, &no_stat_time_us, &no_stat_runs); inter_inference_sleep_seconds, &no_stat_time_us, &no_stat_runs);
if (!status) { if (!status) {
...@@ -363,7 +383,7 @@ int Main(int argc, char **argv) { ...@@ -363,7 +383,7 @@ int Main(int argc, char **argv) {
int64_t stat_time_us = 0; int64_t stat_time_us = 0;
int64_t stat_runs = 0; int64_t stat_runs = 0;
status = Run(engine_ptr.get(), input_infos, output_infos, status = Run(engine_ptr.get(), input_infos, &output_infos,
stats.get(), FLAGS_max_num_runs, max_benchmark_time_seconds, stats.get(), FLAGS_max_num_runs, max_benchmark_time_seconds,
inter_inference_sleep_seconds, &stat_time_us, &stat_runs); inter_inference_sleep_seconds, &stat_time_us, &stat_runs);
if (!status) { if (!status) {
...@@ -372,8 +392,8 @@ int Main(int argc, char **argv) { ...@@ -372,8 +392,8 @@ int Main(int argc, char **argv) {
LOG(INFO) << "Average inference timings in us: " LOG(INFO) << "Average inference timings in us: "
<< "Warmup: " << "Warmup: "
<< (FLAGS_warmup_runs > 0 ? warmup_time_us / FLAGS_warmup_runs : 0) << ", " << (FLAGS_warmup_runs > 0 ? warmup_time_us / FLAGS_warmup_runs : 0)
<< "no stats: " << no_stat_time_us / no_stat_runs << ", " << ", " << "no stats: " << no_stat_time_us / no_stat_runs << ", "
<< "with stats: " << stat_time_us / stat_runs; << "with stats: " << stat_time_us / stat_runs;
stats->PrintOperatorStats(); stats->PrintOperatorStats();
...@@ -381,7 +401,7 @@ int Main(int argc, char **argv) { ...@@ -381,7 +401,7 @@ int Main(int argc, char **argv) {
return 0; return 0;
} }
} // namespace benchmark } // namespace benchmark
} // namespace mace } // namespace mace
int main(int argc, char **argv) { mace::benchmark::Main(argc, argv); } int main(int argc, char **argv) { mace::benchmark::Main(argc, argv); }
...@@ -19,16 +19,13 @@ ...@@ -19,16 +19,13 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>
#include <thread> #include <thread> // NOLINT(build/c++11)
#include "gflags/gflags.h" #include "gflags/gflags.h"
#include "mace/public/mace.h" #include "mace/public/mace.h"
#include "mace/utils/env_time.h" #include "mace/utils/env_time.h"
#include "mace/utils/logging.h" #include "mace/utils/logging.h"
using namespace std;
using namespace mace;
namespace mace { namespace mace {
#ifdef MACE_CPU_MODEL_TAG #ifdef MACE_CPU_MODEL_TAG
...@@ -73,15 +70,15 @@ extern const std::string ModelChecksum(); ...@@ -73,15 +70,15 @@ extern const std::string ModelChecksum();
} // namespace MACE_DSP_MODEL_TAG } // namespace MACE_DSP_MODEL_TAG
#endif #endif
} // namespace mace namespace benchmark {
void ParseShape(const string &str, vector<int64_t> *shape) { void ParseShape(const std::string &str, std::vector<int64_t> *shape) {
string tmp = str; std::string tmp = str;
while (!tmp.empty()) { while (!tmp.empty()) {
int dim = atoi(tmp.data()); int dim = atoi(tmp.data());
shape->push_back(dim); shape->push_back(dim);
size_t next_offset = tmp.find(","); size_t next_offset = tmp.find(",");
if (next_offset == string::npos) { if (next_offset == std::string::npos) {
break; break;
} else { } else {
tmp = tmp.substr(next_offset + 1); tmp = tmp.substr(next_offset + 1);
...@@ -89,7 +86,7 @@ void ParseShape(const string &str, vector<int64_t> *shape) { ...@@ -89,7 +86,7 @@ void ParseShape(const string &str, vector<int64_t> *shape) {
} }
} }
DeviceType ParseDeviceType(const string &device_str) { DeviceType ParseDeviceType(const std::string &device_str) {
if (device_str.compare("CPU") == 0) { if (device_str.compare("CPU") == 0) {
return DeviceType::CPU; return DeviceType::CPU;
} else if (device_str.compare("NEON") == 0) { } else if (device_str.compare("NEON") == 0) {
...@@ -111,20 +108,23 @@ DEFINE_string(gpu_model_data_file, "", "gpu model data file name"); ...@@ -111,20 +108,23 @@ DEFINE_string(gpu_model_data_file, "", "gpu model data file name");
DEFINE_string(dsp_model_data_file, "", "dsp model data file name"); DEFINE_string(dsp_model_data_file, "", "dsp model data file name");
DEFINE_int32(run_seconds, 10, "run seconds"); DEFINE_int32(run_seconds, 10, "run seconds");
int main(int argc, char **argv) { int Main(int argc, char **argv) {
gflags::SetUsageMessage("some usage message"); gflags::SetUsageMessage("some usage message");
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
LOG(INFO) << "mace version: " << MaceVersion(); LOG(INFO) << "mace version: " << MaceVersion();
LOG(INFO) << "mace git version: " << MaceGitVersion(); LOG(INFO) << "mace git version: " << MaceGitVersion();
#ifdef MACE_CPU_MODEL_TAG #ifdef MACE_CPU_MODEL_TAG
LOG(INFO) << "cpu model checksum: " << mace::MACE_CPU_MODEL_TAG::ModelChecksum(); LOG(INFO) << "cpu model checksum: "
<< mace::MACE_CPU_MODEL_TAG::ModelChecksum();
#endif #endif
#ifdef MACE_GPU_MODEL_TAG #ifdef MACE_GPU_MODEL_TAG
LOG(INFO) << "gpu model checksum: " << mace::MACE_GPU_MODEL_TAG::ModelChecksum(); LOG(INFO) << "gpu model checksum: "
<< mace::MACE_GPU_MODEL_TAG::ModelChecksum();
#endif #endif
#ifdef MACE_DSP_MODEL_TAG #ifdef MACE_DSP_MODEL_TAG
LOG(INFO) << "dsp model checksum: " << mace::MACE_DSP_MODEL_TAG::ModelChecksum(); LOG(INFO) << "dsp model checksum: "
<< mace::MACE_DSP_MODEL_TAG::ModelChecksum();
#endif #endif
LOG(INFO) << "input_shape: " << FLAGS_input_shape; LOG(INFO) << "input_shape: " << FLAGS_input_shape;
LOG(INFO) << "output_shape: " << FLAGS_output_shape; LOG(INFO) << "output_shape: " << FLAGS_output_shape;
...@@ -134,8 +134,8 @@ int main(int argc, char **argv) { ...@@ -134,8 +134,8 @@ int main(int argc, char **argv) {
LOG(INFO) << "dsp_model_data_file: " << FLAGS_dsp_model_data_file; LOG(INFO) << "dsp_model_data_file: " << FLAGS_dsp_model_data_file;
LOG(INFO) << "run_seconds: " << FLAGS_run_seconds; LOG(INFO) << "run_seconds: " << FLAGS_run_seconds;
vector<int64_t> input_shape_vec; std::vector<int64_t> input_shape_vec;
vector<int64_t> output_shape_vec; std::vector<int64_t> output_shape_vec;
ParseShape(FLAGS_input_shape, &input_shape_vec); ParseShape(FLAGS_input_shape, &input_shape_vec);
ParseShape(FLAGS_output_shape, &output_shape_vec); ParseShape(FLAGS_output_shape, &output_shape_vec);
...@@ -151,7 +151,7 @@ int main(int argc, char **argv) { ...@@ -151,7 +151,7 @@ int main(int argc, char **argv) {
std::unique_ptr<float[]> dsp_output_data(new float[output_size]); std::unique_ptr<float[]> dsp_output_data(new float[output_size]);
// load input // load input
ifstream in_file(FLAGS_input_file, ios::in | ios::binary); std::ifstream in_file(FLAGS_input_file, std::ios::in | std::ios::binary);
if (in_file.is_open()) { if (in_file.is_open()) {
in_file.read(reinterpret_cast<char *>(input_data.get()), in_file.read(reinterpret_cast<char *>(input_data.get()),
input_size * sizeof(float)); input_size * sizeof(float));
...@@ -166,7 +166,8 @@ int main(int argc, char **argv) { ...@@ -166,7 +166,8 @@ int main(int argc, char **argv) {
/* --------------------- CPU init ----------------------- */ /* --------------------- CPU init ----------------------- */
LOG(INFO) << "Load & init cpu model and warm up"; LOG(INFO) << "Load & init cpu model and warm up";
const unsigned char *cpu_model_data = const unsigned char *cpu_model_data =
mace::MACE_CPU_MODEL_TAG::LoadModelData(FLAGS_cpu_model_data_file.c_str()); mace::MACE_CPU_MODEL_TAG::LoadModelData(
FLAGS_cpu_model_data_file.c_str());
NetDef cpu_net_def = mace::MACE_CPU_MODEL_TAG::CreateNet(cpu_model_data); NetDef cpu_net_def = mace::MACE_CPU_MODEL_TAG::CreateNet(cpu_model_data);
mace::MaceEngine cpu_engine(&cpu_net_def, DeviceType::CPU); mace::MaceEngine cpu_engine(&cpu_net_def, DeviceType::CPU);
...@@ -182,7 +183,8 @@ int main(int argc, char **argv) { ...@@ -182,7 +183,8 @@ int main(int argc, char **argv) {
/* --------------------- GPU init ----------------------- */ /* --------------------- GPU init ----------------------- */
LOG(INFO) << "Load & init gpu model and warm up"; LOG(INFO) << "Load & init gpu model and warm up";
const unsigned char *gpu_model_data = const unsigned char *gpu_model_data =
mace::MACE_GPU_MODEL_TAG::LoadModelData(FLAGS_gpu_model_data_file.c_str()); mace::MACE_GPU_MODEL_TAG::LoadModelData(
FLAGS_gpu_model_data_file.c_str());
NetDef gpu_net_def = mace::MACE_GPU_MODEL_TAG::CreateNet(gpu_model_data); NetDef gpu_net_def = mace::MACE_GPU_MODEL_TAG::CreateNet(gpu_model_data);
mace::MaceEngine gpu_engine(&gpu_net_def, DeviceType::OPENCL); mace::MaceEngine gpu_engine(&gpu_net_def, DeviceType::OPENCL);
...@@ -199,7 +201,8 @@ int main(int argc, char **argv) { ...@@ -199,7 +201,8 @@ int main(int argc, char **argv) {
/* --------------------- DSP init ----------------------- */ /* --------------------- DSP init ----------------------- */
LOG(INFO) << "Load & init dsp model and warm up"; LOG(INFO) << "Load & init dsp model and warm up";
const unsigned char *dsp_model_data = const unsigned char *dsp_model_data =
mace::MACE_DSP_MODEL_TAG::LoadModelData(FLAGS_gpu_model_data_file.c_str()); mace::MACE_DSP_MODEL_TAG::LoadModelData(
FLAGS_gpu_model_data_file.c_str());
NetDef dsp_net_def = mace::MACE_DSP_MODEL_TAG::CreateNet(dsp_model_data); NetDef dsp_net_def = mace::MACE_DSP_MODEL_TAG::CreateNet(dsp_model_data);
mace::MaceEngine dsp_engine(&dsp_net_def, DeviceType::HEXAGON); mace::MaceEngine dsp_engine(&dsp_net_def, DeviceType::HEXAGON);
...@@ -278,4 +281,11 @@ int main(int argc, char **argv) { ...@@ -278,4 +281,11 @@ int main(int argc, char **argv) {
#endif #endif
LOG(INFO) << "Total throughput: " << total_throughput << " f/s"; LOG(INFO) << "Total throughput: " << total_throughput << " f/s";
return 0;
} }
} // namespace benchmark
} // namespace mace
int main(int argc, char **argv) { mace::benchmark::Main(argc, argv); }
...@@ -3,14 +3,18 @@ ...@@ -3,14 +3,18 @@
// //
#include "mace/benchmark/stat_summarizer.h" #include "mace/benchmark/stat_summarizer.h"
#include "mace/public/mace.h"
#include "mace/utils/logging.h"
#include <iomanip> #include <iomanip>
#include <queue>
#include <iostream> #include <iostream>
#include <queue>
#include <utility>
#include "mace/public/mace.h"
#include "mace/utils/logging.h"
namespace mace { namespace mace {
namespace benchmark {
StatSummarizer::StatSummarizer(const StatSummarizerOptions &options) StatSummarizer::StatSummarizer(const StatSummarizerOptions &options)
: options_(options) {} : options_(options) {}
...@@ -90,7 +94,7 @@ std::string StatSummarizer::HeaderString(const std::string &title) const { ...@@ -90,7 +94,7 @@ std::string StatSummarizer::HeaderString(const std::string &title) const {
stream << "============================== " << title stream << "============================== " << title
<< " ==============================" << std::endl; << " ==============================" << std::endl;
InitField(stream, 14) << "[node type]"; InitField(stream, 24) << "[node type]";
InitField(stream, 9) << "[start]"; InitField(stream, 9) << "[start]";
InitField(stream, 9) << "[first]"; InitField(stream, 9) << "[first]";
InitField(stream, 9) << "[avg ms]"; InitField(stream, 9) << "[avg ms]";
...@@ -114,7 +118,7 @@ std::string StatSummarizer::ColumnString(const StatSummarizer::Detail &detail, ...@@ -114,7 +118,7 @@ std::string StatSummarizer::ColumnString(const StatSummarizer::Detail &detail,
const int64_t times_called = detail.times_called / num_runs(); const int64_t times_called = detail.times_called / num_runs();
std::stringstream stream; std::stringstream stream;
InitField(stream, 14) << detail.type; InitField(stream, 24) << detail.type;
InitField(stream, 9) << start_ms; InitField(stream, 9) << start_ms;
InitField(stream, 9) << first_time_ms; InitField(stream, 9) << first_time_ms;
InitField(stream, 9) << avg_time_ms; InitField(stream, 9) << avg_time_ms;
...@@ -224,7 +228,7 @@ std::string StatSummarizer::GetStatsByNodeType() const { ...@@ -224,7 +228,7 @@ std::string StatSummarizer::GetStatsByNodeType() const {
std::pair<std::string, int64_t>(node_type.first, mem_used)); std::pair<std::string, int64_t>(node_type.first, mem_used));
} }
InitField(stream, 14) << "[Node type]"; InitField(stream, 24) << "[Node type]";
InitField(stream, 9) << "[count]"; InitField(stream, 9) << "[count]";
InitField(stream, 10) << "[avg ms]"; InitField(stream, 10) << "[avg ms]";
InitField(stream, 11) << "[avg %]"; InitField(stream, 11) << "[avg %]";
...@@ -248,7 +252,7 @@ std::string StatSummarizer::GetStatsByNodeType() const { ...@@ -248,7 +252,7 @@ std::string StatSummarizer::GetStatsByNodeType() const {
((entry.first / static_cast<float>(accumulated_us)) * 100.0f); ((entry.first / static_cast<float>(accumulated_us)) * 100.0f);
cdf += percentage; cdf += percentage;
InitField(stream, 14) << node_type; InitField(stream, 24) << node_type;
InitField(stream, 9) << node_type_map_count[node_type]; InitField(stream, 9) << node_type_map_count[node_type];
InitField(stream, 10) << time_per_run_ms; InitField(stream, 10) << time_per_run_ms;
InitField(stream, 10) << percentage << "%"; InitField(stream, 10) << percentage << "%";
...@@ -317,4 +321,5 @@ void StatSummarizer::PrintOperatorStats() const { ...@@ -317,4 +321,5 @@ void StatSummarizer::PrintOperatorStats() const {
} }
} }
} // namespace benchmark
} // namespace mace } // namespace mace
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#ifndef MACE_TOOLS_BENCHMARK_STAT_SUMMARIZER_H_ #ifndef MACE_BENCHMARK_STAT_SUMMARIZER_H_
#define MACE_TOOLS_BENCHMARK_STAT_SUMMARIZER_H_ #define MACE_BENCHMARK_STAT_SUMMARIZER_H_
#include <stdlib.h> #include <stdlib.h>
#include <algorithm>
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include <map> #include <map>
...@@ -17,6 +18,8 @@ namespace mace { ...@@ -17,6 +18,8 @@ namespace mace {
class RunMetadata; class RunMetadata;
namespace benchmark {
template <typename ValueType, typename HighPrecisionValueType = double> template <typename ValueType, typename HighPrecisionValueType = double>
class Stat { class Stat {
public: public:
...@@ -194,6 +197,7 @@ class StatSummarizer { ...@@ -194,6 +197,7 @@ class StatSummarizer {
StatSummarizerOptions options_; StatSummarizerOptions options_;
}; };
} // namespace mace } // namespace benchmark
} // namespace mace
#endif // MACE_TOOLS_BENCHMARK_STAT_SUMMARIZER_H_ #endif // MACE_BENCHMARK_STAT_SUMMARIZER_H_
...@@ -13,4 +13,4 @@ namespace mace { ...@@ -13,4 +13,4 @@ namespace mace {
extern const std::map<std::string, std::vector<unsigned char>> extern const std::map<std::string, std::vector<unsigned char>>
kCompiledProgramMap = {}; kCompiledProgramMap = {};
} // namespace } // namespace mace
...@@ -13,4 +13,4 @@ namespace mace { ...@@ -13,4 +13,4 @@ namespace mace {
extern const std::map<std::string, std::vector<unsigned int>> extern const std::map<std::string, std::vector<unsigned int>>
kTuningParamsData = {}; kTuningParamsData = {};
} // namespace } // namespace mace
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
*/ */
#include <malloc.h> #include <malloc.h>
#include <stdint.h> #include <stdint.h>
#include <sys/time.h>
#include <time.h>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
...@@ -28,9 +26,6 @@ ...@@ -28,9 +26,6 @@
#include "mace/utils/env_time.h" #include "mace/utils/env_time.h"
#include "mace/utils/logging.h" #include "mace/utils/logging.h"
using namespace std;
using namespace mace;
namespace mace { namespace mace {
namespace MACE_MODEL_TAG { namespace MACE_MODEL_TAG {
...@@ -45,6 +40,8 @@ extern const std::string ModelChecksum(); ...@@ -45,6 +40,8 @@ extern const std::string ModelChecksum();
} // namespace MACE_MODEL_TAG } // namespace MACE_MODEL_TAG
} // namespace mace } // namespace mace
namespace mace {
namespace examples {
namespace str_util { namespace str_util {
...@@ -65,13 +62,13 @@ std::vector<std::string> Split(const std::string &str, char delims) { ...@@ -65,13 +62,13 @@ std::vector<std::string> Split(const std::string &str, char delims) {
} // namespace str_util } // namespace str_util
void ParseShape(const string &str, vector<int64_t> *shape) { void ParseShape(const std::string &str, std::vector<int64_t> *shape) {
string tmp = str; std::string tmp = str;
while (!tmp.empty()) { while (!tmp.empty()) {
int dim = atoi(tmp.data()); int dim = atoi(tmp.data());
shape->push_back(dim); shape->push_back(dim);
size_t next_offset = tmp.find(","); size_t next_offset = tmp.find(",");
if (next_offset == string::npos) { if (next_offset == std::string::npos) {
break; break;
} else { } else {
tmp = tmp.substr(next_offset + 1); tmp = tmp.substr(next_offset + 1);
...@@ -87,7 +84,7 @@ std::string FormatName(const std::string input) { ...@@ -87,7 +84,7 @@ std::string FormatName(const std::string input) {
return res; return res;
} }
DeviceType ParseDeviceType(const string &device_str) { DeviceType ParseDeviceType(const std::string &device_str) {
if (device_str.compare("CPU") == 0) { if (device_str.compare("CPU") == 0) {
return DeviceType::CPU; return DeviceType::CPU;
} else if (device_str.compare("NEON") == 0) { } else if (device_str.compare("NEON") == 0) {
...@@ -101,7 +98,7 @@ DeviceType ParseDeviceType(const string &device_str) { ...@@ -101,7 +98,7 @@ DeviceType ParseDeviceType(const string &device_str) {
} }
} }
GPUType ParseGPUType(const string &gpu_type_str) { GPUType ParseGPUType(const std::string &gpu_type_str) {
if (gpu_type_str.compare("ADRENO") == 0) { if (gpu_type_str.compare("ADRENO") == 0) {
return GPUType::ADRENO; return GPUType::ADRENO;
} else if (gpu_type_str.compare("MALI") == 0) { } else if (gpu_type_str.compare("MALI") == 0) {
...@@ -158,12 +155,18 @@ struct mallinfo LogMallinfoChange(struct mallinfo prev) { ...@@ -158,12 +155,18 @@ struct mallinfo LogMallinfoChange(struct mallinfo prev) {
return curr; return curr;
} }
DEFINE_string(input_node, "input_node0,input_node1", "input nodes, separated by comma"); DEFINE_string(input_node, "input_node0,input_node1",
DEFINE_string(input_shape, "1,224,224,3:1,1,1,10", "input shapes, separated by colon and comma"); "input nodes, separated by comma");
DEFINE_string(output_node, "output_node0,output_node1", "output nodes, separated by comma"); DEFINE_string(input_shape, "1,224,224,3:1,1,1,10",
DEFINE_string(output_shape, "1,224,224,2:1,1,1,10", "output shapes, separated by colon and comma"); "input shapes, separated by colon and comma");
DEFINE_string(input_file, "", "input file name | input file prefix for multiple inputs."); DEFINE_string(output_node, "output_node0,output_node1",
DEFINE_string(output_file, "", "output file name | output file prefix for multiple outputs"); "output nodes, separated by comma");
DEFINE_string(output_shape, "1,224,224,2:1,1,1,10",
"output shapes, separated by colon and comma");
DEFINE_string(input_file, "",
"input file name | input file prefix for multiple inputs.");
DEFINE_string(output_file, "",
"output file name | output file prefix for multiple outputs");
DEFINE_string(model_data_file, "", DEFINE_string(model_data_file, "",
"model data file name, used when EMBED_MODEL_DATA set to 0"); "model data file name, used when EMBED_MODEL_DATA set to 0");
DEFINE_string(device, "OPENCL", "CPU/NEON/OPENCL/HEXAGON"); DEFINE_string(device, "OPENCL", "CPU/NEON/OPENCL/HEXAGON");
...@@ -174,7 +177,8 @@ DEFINE_string(gpu_type, "ADRENO", "ADRENO/MALI"); ...@@ -174,7 +177,8 @@ DEFINE_string(gpu_type, "ADRENO", "ADRENO/MALI");
DEFINE_int32(gpu_perf_hint, 2, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH"); DEFINE_int32(gpu_perf_hint, 2, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH");
DEFINE_int32(gpu_priority_hint, 1, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH"); DEFINE_int32(gpu_priority_hint, 1, "0:DEFAULT/1:LOW/2:NORMAL/3:HIGH");
DEFINE_int32(omp_num_threads, 8, "num of openmp threads"); DEFINE_int32(omp_num_threads, 8, "num of openmp threads");
DEFINE_int32(cpu_power_option, 0, "0:DEFAULT/1:HIGH_PERFORMANCE/2:BATTERY_SAVE"); DEFINE_int32(cpu_power_option, 0,
"0:DEFAULT/1:HIGH_PERFORMANCE/2:BATTERY_SAVE");
bool SingleInputAndOutput(const std::vector<int64_t> &input_shape, bool SingleInputAndOutput(const std::vector<int64_t> &input_shape,
const std::vector<int64_t> &output_shape) { const std::vector<int64_t> &output_shape) {
...@@ -197,8 +201,7 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape, ...@@ -197,8 +201,7 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape,
gpu_type, gpu_type,
static_cast<GPUPerfHint>(FLAGS_gpu_perf_hint), static_cast<GPUPerfHint>(FLAGS_gpu_perf_hint),
static_cast<GPUPriorityHint>(FLAGS_gpu_priority_hint)); static_cast<GPUPriorityHint>(FLAGS_gpu_priority_hint));
} } else if (device_type == DeviceType::CPU) {
else if (device_type == DeviceType::CPU) {
mace::ConfigOmpThreadsAndAffinity( mace::ConfigOmpThreadsAndAffinity(
FLAGS_omp_num_threads, FLAGS_omp_num_threads,
static_cast<CPUPowerOption>(FLAGS_cpu_power_option)); static_cast<CPUPowerOption>(FLAGS_cpu_power_option));
...@@ -227,7 +230,8 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape, ...@@ -227,7 +230,8 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape,
std::unique_ptr<float[]> output_data(new float[output_size]); std::unique_ptr<float[]> output_data(new float[output_size]);
// load input // load input
ifstream in_file(FLAGS_input_file + "_" + FormatName(FLAGS_input_node), ios::in | ios::binary); std::ifstream in_file(FLAGS_input_file + "_" + FormatName(FLAGS_input_node),
std::ios::in | std::ios::binary);
if (in_file.is_open()) { if (in_file.is_open()) {
in_file.read(reinterpret_cast<char *>(input_data.get()), in_file.read(reinterpret_cast<char *>(input_data.get()),
input_size * sizeof(float)); input_size * sizeof(float));
...@@ -262,7 +266,7 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape, ...@@ -262,7 +266,7 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape,
if (output_data != nullptr) { if (output_data != nullptr) {
std::string std::string
output_name = FLAGS_output_file + "_" + FormatName(FLAGS_output_node); output_name = FLAGS_output_file + "_" + FormatName(FLAGS_output_node);
ofstream out_file(output_name, ios::binary); std::ofstream out_file(output_name, std::ios::binary);
out_file.write((const char *) (output_data.get()), out_file.write((const char *) (output_data.get()),
output_size * sizeof(float)); output_size * sizeof(float));
out_file.flush(); out_file.flush();
...@@ -279,10 +283,11 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape, ...@@ -279,10 +283,11 @@ bool SingleInputAndOutput(const std::vector<int64_t> &input_shape,
return true; return true;
} }
bool MultipleInputOrOutput(const std::vector<std::string> &input_names, bool MultipleInputOrOutput(
const std::vector<std::vector<int64_t>> &input_shapes, const std::vector<std::string> &input_names,
const std::vector<std::string> &output_names, const std::vector<std::vector<int64_t>> &input_shapes,
const std::vector<std::vector<int64_t>> &output_shapes) { const std::vector<std::string> &output_names,
const std::vector<std::vector<int64_t>> &output_shapes) {
// load model // load model
int64_t t0 = NowMicros(); int64_t t0 = NowMicros();
const unsigned char *model_data = const unsigned char *model_data =
...@@ -302,8 +307,7 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names, ...@@ -302,8 +307,7 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names,
gpu_type, gpu_type,
static_cast<GPUPerfHint>(FLAGS_gpu_perf_hint), static_cast<GPUPerfHint>(FLAGS_gpu_perf_hint),
static_cast<GPUPriorityHint>(FLAGS_gpu_priority_hint)); static_cast<GPUPriorityHint>(FLAGS_gpu_priority_hint));
} } else if (device_type == DeviceType::CPU) {
else if (device_type == DeviceType::CPU) {
mace::ConfigOmpThreadsAndAffinity( mace::ConfigOmpThreadsAndAffinity(
FLAGS_omp_num_threads, FLAGS_omp_num_threads,
static_cast<CPUPowerOption>(FLAGS_cpu_power_option)); static_cast<CPUPowerOption>(FLAGS_cpu_power_option));
...@@ -333,7 +337,8 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names, ...@@ -333,7 +337,8 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names,
std::multiplies<int64_t>()); std::multiplies<int64_t>());
input_datas[i].reset(new float[input_size]); input_datas[i].reset(new float[input_size]);
// load input // load input
ifstream in_file(FLAGS_input_file + "_" + FormatName(input_names[i]), ios::in | ios::binary); std::ifstream in_file(FLAGS_input_file + "_" + FormatName(input_names[i]),
std::ios::in | std::ios::binary);
if (in_file.is_open()) { if (in_file.is_open()) {
in_file.read(reinterpret_cast<char *>(input_datas[i].get()), in_file.read(reinterpret_cast<char *>(input_datas[i].get()),
input_size * sizeof(float)); input_size * sizeof(float));
...@@ -377,8 +382,9 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names, ...@@ -377,8 +382,9 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names,
} }
for (size_t i = 0; i < output_count; ++i) { for (size_t i = 0; i < output_count; ++i) {
std::string output_name = FLAGS_output_file + "_" + FormatName(output_names[i]); std::string output_name = FLAGS_output_file + "_"
ofstream out_file(output_name, ios::binary); + FormatName(output_names[i]);
std::ofstream out_file(output_name, std::ios::binary);
int64_t output_size = int64_t output_size =
std::accumulate(output_shapes[i].begin(), output_shapes[i].end(), 1, std::accumulate(output_shapes[i].begin(), output_shapes[i].end(), 1,
std::multiplies<int64_t>()); std::multiplies<int64_t>());
...@@ -395,7 +401,7 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names, ...@@ -395,7 +401,7 @@ bool MultipleInputOrOutput(const std::vector<std::string> &input_names,
return true; return true;
} }
int main(int argc, char **argv) { int Main(int argc, char **argv) {
gflags::SetUsageMessage("some usage message"); gflags::SetUsageMessage("some usage message");
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
...@@ -419,14 +425,17 @@ int main(int argc, char **argv) { ...@@ -419,14 +425,17 @@ int main(int argc, char **argv) {
LOG(INFO) << "cpu_power_option: " << FLAGS_cpu_power_option; LOG(INFO) << "cpu_power_option: " << FLAGS_cpu_power_option;
std::vector<std::string> input_names = str_util::Split(FLAGS_input_node, ','); std::vector<std::string> input_names = str_util::Split(FLAGS_input_node, ',');
std::vector<std::string> output_names = str_util::Split(FLAGS_output_node, ','); std::vector<std::string> output_names =
std::vector<std::string> input_shapes = str_util::Split(FLAGS_input_shape, ':'); str_util::Split(FLAGS_output_node, ',');
std::vector<std::string> output_shapes = str_util::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();
std::vector<vector<int64_t>> input_shape_vec(input_count); std::vector<std::vector<int64_t>> input_shape_vec(input_count);
std::vector<vector<int64_t>> output_shape_vec(output_count); std::vector<std::vector<int64_t>> output_shape_vec(output_count);
for (size_t i = 0; i < input_count; ++i) { for (size_t i = 0; i < input_count; ++i) {
ParseShape(input_shapes[i], &input_shape_vec[i]); ParseShape(input_shapes[i], &input_shape_vec[i]);
} }
...@@ -447,9 +456,14 @@ int main(int argc, char **argv) { ...@@ -447,9 +456,14 @@ int main(int argc, char **argv) {
output_shape_vec); output_shape_vec);
} }
} }
if(ret) { if (ret) {
return 0; return 0;
} else { } else {
return -1; return -1;
} }
} }
} // namespace examples
} // namespace mace
int main(int argc, char **argv) { mace::examples::Main(argc, argv); }
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#ifndef MACE_CORE_MACE_H_ #ifndef MACE_PUBLIC_MACE_H_
#define MACE_CORE_MACE_H_ #define MACE_PUBLIC_MACE_H_
#include <cstdint> #include <cstdint>
#include <map> #include <map>
...@@ -169,7 +169,7 @@ class NodeInput { ...@@ -169,7 +169,7 @@ class NodeInput {
class OutputShape { class OutputShape {
public: public:
OutputShape(); OutputShape();
OutputShape(const std::vector<int64_t> &dims); OutputShape(const std::vector<int64_t> &dims); // NOLINT(runtime/explicit)
void CopyFrom(const OutputShape &from); void CopyFrom(const OutputShape &from);
public: public:
...@@ -409,9 +409,10 @@ class MaceEngine { ...@@ -409,9 +409,10 @@ class MaceEngine {
float *output, float *output,
RunMetadata *run_metadata); RunMetadata *run_metadata);
// Multiple input or output // Multiple input or output
bool Run(const std::vector<MaceInputInfo> &input, bool Run(
std::map<std::string, float *> &output, const std::vector<MaceInputInfo> &input,
RunMetadata *run_metadata = nullptr); std::map<std::string, float *> &output, // NOLINT(runtime/references)
RunMetadata *run_metadata = nullptr);
MaceEngine(const MaceEngine &) = delete; MaceEngine(const MaceEngine &) = delete;
MaceEngine &operator=(const MaceEngine &) = delete; MaceEngine &operator=(const MaceEngine &) = delete;
...@@ -425,4 +426,4 @@ class MaceEngine { ...@@ -425,4 +426,4 @@ class MaceEngine {
} // namespace mace } // namespace mace
#endif // MACE_CORE_MACE_H_ #endif // MACE_PUBLIC_MACE_H_
...@@ -3,32 +3,32 @@ ...@@ -3,32 +3,32 @@
// //
#include "mace/utils/command_line_flags.h" #include "mace/utils/command_line_flags.h"
#include "mace/utils/logging.h"
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
namespace mace { #include "mace/utils/logging.h"
namespace {
using namespace std; namespace mace {
namespace utils {
bool StringConsume(string &arg, const string &x) { bool StringConsume(const std::string &x, std::string *arg) {
if ((arg.size() >= x.size()) && MACE_CHECK_NOTNULL(arg);
(memcmp(arg.data(), x.data(), x.size()) == 0)) { if ((arg->size() >= x.size()) &&
arg = arg.substr(x.size()); (memcmp(arg->data(), x.data(), x.size()) == 0)) {
*arg = arg->substr(x.size());
return true; return true;
} }
return false; return false;
} }
bool ParseStringFlag(string arg, bool ParseStringFlag(std::string arg,
string flag, std::string flag,
string *dst, std::string *dst,
bool *value_parsing_ok) { bool *value_parsing_ok) {
*value_parsing_ok = true; *value_parsing_ok = true;
if (StringConsume(arg, "--") && StringConsume(arg, flag) && if (StringConsume("--", &arg) && StringConsume(flag, &arg) &&
StringConsume(arg, "=")) { StringConsume("=", &arg)) {
*dst = arg; *dst = arg;
return true; return true;
} }
...@@ -36,13 +36,13 @@ bool ParseStringFlag(string arg, ...@@ -36,13 +36,13 @@ bool ParseStringFlag(string arg,
return false; return false;
} }
bool ParseInt32Flag(string arg, bool ParseInt32Flag(std::string arg,
string flag, std::string flag,
int32_t *dst, int32_t *dst,
bool *value_parsing_ok) { bool *value_parsing_ok) {
*value_parsing_ok = true; *value_parsing_ok = true;
if (StringConsume(arg, "--") && StringConsume(arg, flag) && if (StringConsume("--", &arg) && StringConsume(flag, &arg) &&
StringConsume(arg, "=")) { StringConsume("=", &arg)) {
char extra; char extra;
if (sscanf(arg.data(), "%d%c", dst, &extra) != 1) { if (sscanf(arg.data(), "%d%c", dst, &extra) != 1) {
LOG(ERROR) << "Couldn't interpret value " << arg << " for flag " << flag LOG(ERROR) << "Couldn't interpret value " << arg << " for flag " << flag
...@@ -55,13 +55,13 @@ bool ParseInt32Flag(string arg, ...@@ -55,13 +55,13 @@ bool ParseInt32Flag(string arg,
return false; return false;
} }
bool ParseInt64Flag(string arg, bool ParseInt64Flag(std::string arg,
string flag, std::string flag,
long long *dst, int64_t *dst,
bool *value_parsing_ok) { bool *value_parsing_ok) {
*value_parsing_ok = true; *value_parsing_ok = true;
if (StringConsume(arg, "--") && StringConsume(arg, flag) && if (StringConsume("--", &arg) && StringConsume(flag, &arg) &&
StringConsume(arg, "=")) { StringConsume("=", &arg)) {
char extra; char extra;
if (sscanf(arg.data(), "%lld%c", dst, &extra) != 1) { if (sscanf(arg.data(), "%lld%c", dst, &extra) != 1) {
LOG(ERROR) << "Couldn't interpret value " << arg << " for flag " << flag LOG(ERROR) << "Couldn't interpret value " << arg << " for flag " << flag
...@@ -74,9 +74,10 @@ bool ParseInt64Flag(string arg, ...@@ -74,9 +74,10 @@ bool ParseInt64Flag(string arg,
return false; return false;
} }
bool ParseBoolFlag(string arg, string flag, bool *dst, bool *value_parsing_ok) { bool ParseBoolFlag(std::string arg, std::string flag,
bool *dst, bool *value_parsing_ok) {
*value_parsing_ok = true; *value_parsing_ok = true;
if (StringConsume(arg, "--") && StringConsume(arg, flag)) { if (StringConsume("--", &arg) && StringConsume(flag, &arg)) {
if (arg.empty()) { if (arg.empty()) {
*dst = true; *dst = true;
return true; return true;
...@@ -99,13 +100,13 @@ bool ParseBoolFlag(string arg, string flag, bool *dst, bool *value_parsing_ok) { ...@@ -99,13 +100,13 @@ bool ParseBoolFlag(string arg, string flag, bool *dst, bool *value_parsing_ok) {
return false; return false;
} }
bool ParseFloatFlag(string arg, bool ParseFloatFlag(std::string arg,
string flag, std::string flag,
float *dst, float *dst,
bool *value_parsing_ok) { bool *value_parsing_ok) {
*value_parsing_ok = true; *value_parsing_ok = true;
if (StringConsume(arg, "--") && StringConsume(arg, flag) && if (StringConsume("--", &arg) && StringConsume(flag, &arg) &&
StringConsume(arg, "=")) { StringConsume("=", &arg)) {
char extra; char extra;
if (sscanf(arg.data(), "%f%c", dst, &extra) != 1) { if (sscanf(arg.data(), "%f%c", dst, &extra) != 1) {
LOG(ERROR) << "Couldn't interpret value " << arg << " for flag " << flag LOG(ERROR) << "Couldn't interpret value " << arg << " for flag " << flag
...@@ -118,47 +119,48 @@ bool ParseFloatFlag(string arg, ...@@ -118,47 +119,48 @@ bool ParseFloatFlag(string arg,
return false; return false;
} }
} // namespace } // namespace utils
Flag::Flag(const char *name, int *dst, const string &usage_text) Flag::Flag(const char *name, int *dst, const std::string &usage_text)
: name_(name), type_(TYPE_INT), int_value_(dst), usage_text_(usage_text) {} : name_(name), type_(TYPE_INT), int_value_(dst), usage_text_(usage_text) {}
Flag::Flag(const char *name, long long *dst, const string &usage_text) Flag::Flag(const char *name, int64_t *dst, const std::string &usage_text)
: name_(name), : name_(name),
type_(TYPE_INT64), type_(TYPE_INT64),
int64_value_(dst), int64_value_(dst),
usage_text_(usage_text) {} usage_text_(usage_text) {}
Flag::Flag(const char *name, bool *dst, const string &usage_text) Flag::Flag(const char *name, bool *dst, const std::string &usage_text)
: name_(name), : name_(name),
type_(TYPE_BOOL), type_(TYPE_BOOL),
bool_value_(dst), bool_value_(dst),
usage_text_(usage_text) {} usage_text_(usage_text) {}
Flag::Flag(const char *name, string *dst, const string &usage_text) Flag::Flag(const char *name, std::string *dst, const std::string &usage_text)
: name_(name), : name_(name),
type_(TYPE_STRING), type_(TYPE_STRING),
string_value_(dst), string_value_(dst),
usage_text_(usage_text) {} usage_text_(usage_text) {}
Flag::Flag(const char *name, float *dst, const string &usage_text) Flag::Flag(const char *name, float *dst, const std::string &usage_text)
: name_(name), : name_(name),
type_(TYPE_FLOAT), type_(TYPE_FLOAT),
float_value_(dst), float_value_(dst),
usage_text_(usage_text) {} usage_text_(usage_text) {}
bool Flag::Parse(string arg, bool *value_parsing_ok) const { bool Flag::Parse(std::string arg, bool *value_parsing_ok) const {
bool result = false; bool result = false;
if (type_ == TYPE_INT) { if (type_ == TYPE_INT) {
result = ParseInt32Flag(arg, name_, int_value_, value_parsing_ok); result = utils::ParseInt32Flag(arg, name_, int_value_, value_parsing_ok);
} else if (type_ == TYPE_INT64) { } else if (type_ == TYPE_INT64) {
result = ParseInt64Flag(arg, name_, int64_value_, value_parsing_ok); result = utils::ParseInt64Flag(arg, name_, int64_value_, value_parsing_ok);
} else if (type_ == TYPE_BOOL) { } else if (type_ == TYPE_BOOL) {
result = ParseBoolFlag(arg, name_, bool_value_, value_parsing_ok); result = utils::ParseBoolFlag(arg, name_, bool_value_, value_parsing_ok);
} else if (type_ == TYPE_STRING) { } else if (type_ == TYPE_STRING) {
result = ParseStringFlag(arg, name_, string_value_, value_parsing_ok); result = utils::ParseStringFlag(arg, name_,
string_value_, value_parsing_ok);
} else if (type_ == TYPE_FLOAT) { } else if (type_ == TYPE_FLOAT) {
result = ParseFloatFlag(arg, name_, float_value_, value_parsing_ok); result = utils::ParseFloatFlag(arg, name_, float_value_, value_parsing_ok);
} }
return result; return result;
} }
...@@ -169,7 +171,7 @@ bool Flag::Parse(string arg, bool *value_parsing_ok) const { ...@@ -169,7 +171,7 @@ bool Flag::Parse(string arg, bool *value_parsing_ok) const {
bool result = true; bool result = true;
std::vector<char *> unknown_flags; std::vector<char *> unknown_flags;
for (int i = 1; i < *argc; ++i) { for (int i = 1; i < *argc; ++i) {
if (string(argv[i]) == "--") { if (std::string(argv[i]) == "--") {
while (i < *argc) { while (i < *argc) {
unknown_flags.push_back(argv[i]); unknown_flags.push_back(argv[i]);
++i; ++i;
...@@ -202,8 +204,8 @@ bool Flag::Parse(string arg, bool *value_parsing_ok) const { ...@@ -202,8 +204,8 @@ bool Flag::Parse(string arg, bool *value_parsing_ok) const {
return result && (*argc < 2 || strcmp(argv[1], "--help") != 0); return result && (*argc < 2 || strcmp(argv[1], "--help") != 0);
} }
/*static*/ string Flags::Usage(const string &cmdline, std::string Flags::Usage(const std::string &cmdline,
const std::vector<Flag> &flag_list) { const std::vector<Flag> &flag_list) {
std::stringstream usage_text; std::stringstream usage_text;
usage_text << "usage: " << cmdline << std::endl; usage_text << "usage: " << cmdline << std::endl;
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#ifndef MACE_CORE_COMMAND_LINE_FLAGS_H #ifndef MACE_UTILS_COMMAND_LINE_FLAGS_H_
#define MACE_CORE_COMMAND_LINE_FLAGS_H #define MACE_UTILS_COMMAND_LINE_FLAGS_H_
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -13,7 +13,7 @@ namespace mace { ...@@ -13,7 +13,7 @@ namespace mace {
class Flag { class Flag {
public: public:
Flag(const char *name, int *dst1, const std::string &usage_text); Flag(const char *name, int *dst1, const std::string &usage_text);
Flag(const char *name, long long *dst1, const std::string &usage_text); Flag(const char *name, int64_t *dst1, const std::string &usage_text);
Flag(const char *name, bool *dst, const std::string &usage_text); Flag(const char *name, bool *dst, const std::string &usage_text);
Flag(const char *name, std::string *dst, const std::string &usage_text); Flag(const char *name, std::string *dst, const std::string &usage_text);
Flag(const char *name, float *dst, const std::string &usage_text); Flag(const char *name, float *dst, const std::string &usage_text);
...@@ -26,7 +26,7 @@ class Flag { ...@@ -26,7 +26,7 @@ class Flag {
std::string name_; std::string name_;
enum { TYPE_INT, TYPE_INT64, TYPE_BOOL, TYPE_STRING, TYPE_FLOAT } type_; enum { TYPE_INT, TYPE_INT64, TYPE_BOOL, TYPE_STRING, TYPE_FLOAT } type_;
int *int_value_; int *int_value_;
long long *int64_value_; int64_t *int64_value_;
bool *bool_value_; bool *bool_value_;
std::string *string_value_; std::string *string_value_;
float *float_value_; float *float_value_;
...@@ -50,4 +50,4 @@ class Flags { ...@@ -50,4 +50,4 @@ class Flags {
} // namespace mace } // namespace mace
#endif // MACE_CORE_COMMAND_LINE_FLAGS_H #endif // MACE_UTILS_COMMAND_LINE_FLAGS_H_
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#ifndef MACE_UTILS_ENV_TIME_H #ifndef MACE_UTILS_ENV_TIME_H_
#define MACE_UTILS_ENV_TIME_H #define MACE_UTILS_ENV_TIME_H_
#include <stdint.h> #include <stdint.h>
#include <sys/time.h> #include <sys/time.h>
...@@ -19,4 +19,4 @@ inline int64_t NowMicros() { ...@@ -19,4 +19,4 @@ inline int64_t NowMicros() {
} // namespace mace } // namespace mace
#endif // MACE_UTILS_ENV_TIME_H #endif // MACE_UTILS_ENV_TIME_H_
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <utility>
#include "mace/public/mace.h" #include "mace/public/mace.h"
#include "mace/utils/env_time.h" #include "mace/utils/env_time.h"
...@@ -122,7 +123,7 @@ class LatencyLogger { ...@@ -122,7 +123,7 @@ class LatencyLogger {
VLOG(vlog_level_) << message_ VLOG(vlog_level_) << message_
<< " latency: " << stop_micros - start_micros_ << " us"; << " latency: " << stop_micros - start_micros_ << " us";
} }
}; }
private: private:
const int vlog_level_; const int vlog_level_;
......
...@@ -6,13 +6,15 @@ ...@@ -6,13 +6,15 @@
#define MACE_UTILS_MEMORY_LOGGING_H_ #define MACE_UTILS_MEMORY_LOGGING_H_
#include <malloc.h> #include <malloc.h>
#include <string>
#include "mace/utils/logging.h" #include "mace/utils/logging.h"
namespace mace { namespace mace {
class MallinfoChangeLogger { class MallinfoChangeLogger {
public: public:
MallinfoChangeLogger(const std::string &name) : name_(name) { explicit MallinfoChangeLogger(const std::string &name) : name_(name) {
prev_ = mallinfo(); prev_ = mallinfo();
} }
~MallinfoChangeLogger() { ~MallinfoChangeLogger() {
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include <vector> #include <vector>
namespace mace { namespace mace {
namespace { namespace string_util {
inline void MakeStringInternal(std::stringstream & /*ss*/) {} inline void MakeStringInternal(std::stringstream & /*ss*/) {}
...@@ -27,12 +27,12 @@ inline void MakeStringInternal(std::stringstream &ss, ...@@ -27,12 +27,12 @@ inline void MakeStringInternal(std::stringstream &ss,
MakeStringInternal(ss, args...); MakeStringInternal(ss, args...);
} }
} // namespace } // namespace string_util
template <typename... Args> template <typename... Args>
std::string MakeString(const Args &... args) { std::string MakeString(const Args &... args) {
std::stringstream ss; std::stringstream ss;
MakeStringInternal(ss, args...); string_util::MakeStringInternal(ss, args...);
return ss.str(); return ss.str();
} }
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
#include "mace/utils/timer.h" #include "mace/utils/timer.h"
#include "mace/utils/utils.h" #include "mace/utils/utils.h"
namespace {} // namespace
namespace mace { namespace mace {
extern bool GetTuningParams( extern bool GetTuningParams(
...@@ -127,7 +125,7 @@ class Tuner { ...@@ -127,7 +125,7 @@ class Tuner {
const std::function<RetType(const std::vector<param_type> &, const std::function<RetType(const std::vector<param_type> &,
Timer *, Timer *,
std::vector<param_type> *)> &func, std::vector<param_type> *)> &func,
std::vector<param_type> &params, const std::vector<param_type> &params,
Timer *timer, Timer *timer,
int num_runs, int num_runs,
double *time_us, double *time_us,
......
//
// Copyright (c) 2017 XiaoMi All rights reserved.
//
#include <fstream> #include <fstream>
#include <map> #include <map>
#include <string> #include <string>
......
//
// Copyright (c) 2017 XiaoMi All rights reserved.
//
#include <map> #include <map>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
......
// //
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#include <thread> #include <thread> // NOLINT(build/c++11)
#include "gtest/gtest.h" #include "gtest/gtest.h"
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <sys/time.h> #include <sys/time.h>
#include <sstream> #include <sstream>
#include <string>
#include <utility>
namespace mace { namespace mace {
template <typename Integer> template <typename Integer>
......
// //
// Copyright (c) 2017 XiaoMi All rights reserved. // Copyright (c) 2017 XiaoMi All rights reserved.
// //
#include <thread> #include <thread> // NOLINT(build/c++11)
#include "gtest/gtest.h" #include "gtest/gtest.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册