Skip to content

Commit

Permalink
Fix wrong naming style in dnn_benchmark and apply clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
daquexian committed May 13, 2019
1 parent 765d546 commit 43e35f1
Showing 1 changed file with 47 additions and 49 deletions.
96 changes: 47 additions & 49 deletions binaries/dnn_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,36 @@ using std::endl;
using std::string;
using Clock = std::chrono::high_resolution_clock;

auto get_model(css &daqName, const bool allowFp16,
const PreferenceCode &compilePreference) {
auto GetModel(css &daq_name, const bool allow_fp16,
const PreferenceCode &compile_preference) {
std::unique_ptr<Model> model;
ModelBuilder builder;
DaqReader daq_reader;
// Set the last argument to true to use mmap. It may be more efficient than
// memory buffer.
daq_reader.ReadDaq(daqName, builder, false);
daq_reader.ReadDaq(daq_name, builder, false);
#if __ANDROID_API__ >= __ANDROID_API_P__
model = builder.AllowFp16(allowFp16)
.Compile(compilePreference);
model = builder.AllowFp16(allow_fp16).Compile(compile_preference);
#else
model = builder.Compile(compilePreference);
model = builder.Compile(compile_preference);
#endif
return model;
}

auto PrefCodeToStr(const PreferenceCode &preferenceCode) {
if (preferenceCode == ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER) {
auto PrefCodeToStr(const PreferenceCode &preference_code) {
if (preference_code == ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER) {
return "fast single";
}
if (preferenceCode == ANEURALNETWORKS_PREFER_SUSTAINED_SPEED) {
if (preference_code == ANEURALNETWORKS_PREFER_SUSTAINED_SPEED) {
return "sustained speed";
}
if (preferenceCode == ANEURALNETWORKS_PREFER_LOW_POWER) {
if (preference_code == ANEURALNETWORKS_PREFER_LOW_POWER) {
return "low power";
}
return "Unknown preference code";
}

// ./dnn_benchmark daqName
// ./dnn_benchmark daq_name
int main(int argc, char **argv) {
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = true;
Expand All @@ -60,74 +59,73 @@ int main(int argc, char **argv) {
if (argc != 5) {
return -1;
}
css daqName = argv[1];
const int numberRunning = std::atoi(argv[2]);
css daq_name = argv[1];
const int number_running = std::atoi(argv[2]);
const bool quant = std::atoi(argv[3]) != 0;

size_t inputLen, outputLen;
size_t input_len, output_len;
{
auto model = get_model(daqName, false,
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER);
inputLen = model->GetSize(model->GetInputs()[0]);
outputLen = model->GetSize(model->GetOutputs()[0]);
auto model = GetModel(daq_name, false,
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER);
input_len = model->GetSize(model->GetInputs()[0]);
output_len = model->GetSize(model->GetOutputs()[0]);
}
#define WARM_UP \
{ \
auto model = get_model(daqName, outputBlob, false, \
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER); \
for (int i = 0; i < 10; i++) { \
model->SetOutputBuffer(0, output); \
model->Predict(std::vector{data}); \
} \
#define WARM_UP \
{ \
auto model = GetModel(daq_name, false, \
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER); \
for (int i = 0; i < 10; i++) { \
model->SetOutputBuffer(0, output); \
model->Predict(std::vector{data}); \
} \
}

#define BENCHMARK(fp16Candidates, preferenceCandidates) \
for (const auto allowFp16 : fp16Candidates) { \
for (const auto compilePreference : preferenceCandidates) { \
auto model = \
get_model(daqName, outputBlob, allowFp16, compilePreference); \
#define BENCHMARK(fp16_candidates, preference_candidates) \
for (const auto allow_fp16 : fp16_candidates) { \
for (const auto compile_preference : preference_candidates) { \
auto model = GetModel(daq_name, allow_fp16, compile_preference); \
const auto t1 = Clock::now(); \
for (int i = 0; i < numberRunning; i++) { \
for (int i = 0; i < number_running; i++) { \
model->SetOutputBuffer(0, output); \
model->Predict(std::vector{data}); \
} \
const auto t2 = Clock::now(); \
const auto totalTime = \
const auto total_time = \
std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1) \
.count(); \
const auto singleTime = 1. * totalTime / numberRunning; \
LOG(INFO) << "AllowFp16: " << allowFp16 \
const auto single_time = 1. * total_time / number_running; \
LOG(INFO) << "AllowFp16: " << allow_fp16 \
<< ", compile preference: " \
<< PrefCodeToStr(compilePreference) \
<< ", time: " << totalTime << "/" << numberRunning \
<< " = " << singleTime; \
<< PrefCodeToStr(compile_preference) \
<< ", time: " << total_time << "/" << number_running \
<< " = " << single_time; \
} \
}

const std::vector<PreferenceCode> preferenceCandidates{
const std::vector<PreferenceCode> preference_candidates{
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER,
ANEURALNETWORKS_PREFER_SUSTAINED_SPEED,
ANEURALNETWORKS_PREFER_LOW_POWER};
if (quant) {
uint8_t data[inputLen];
uint8_t output[outputLen];
uint8_t data[input_len];
uint8_t output[output_len];
WARM_UP;
const std::vector<bool> fp16Candidates{false};
BENCHMARK(fp16Candidates, preferenceCandidates);
const std::vector<bool> fp16_candidates{false};
BENCHMARK(fp16_candidates, preference_candidates);
} else {
float data[inputLen];
FORZ(i, inputLen) {
float data[input_len];
FORZ(i, input_len) {
data[i] = i;
}
float output[outputLen];
float output[output_len];

WARM_UP;

#if __ANDROID_API__ >= __ANDROID_API_P__
const std::vector<bool> fp16Candidates{false, true};
const std::vector<bool> fp16_candidates{false, true};
#else
const std::vector<bool> fp16Candidates{false};
const std::vector<bool> fp16_candidates{false};
#endif
BENCHMARK(fp16Candidates, preferenceCandidates);
BENCHMARK(fp16_candidates, preference_candidates);
}
}

0 comments on commit 43e35f1

Please sign in to comment.