Skip to content

Commit

Permalink
check api in runtime and make AllowFp16 accessible in api 27
Browse files Browse the repository at this point in the history
  • Loading branch information
daquexian committed Jul 11, 2019
1 parent a804ce1 commit 65b14c9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
21 changes: 8 additions & 13 deletions binaries/dnn_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,12 @@ auto GetModel(css &daqName, const bool allow_fp16,
onnx_reader.ReadOnnx(daqName, builder);
#endif
} else {
throw std::invalid_argument("Wrong model name " + daqName +
". It must end with .daq or .onnx (.onnx is only "
"supported when DNN_READ_ONNX is ON)");
throw std::invalid_argument(
"Wrong model name " + daqName +
". It must end with .daq or .onnx (.onnx is only "
"supported when DNN_READ_ONNX is ON)");
}
#if __ANDROID_API__ >= __ANDROID_API_P__
model = builder.AllowFp16(allow_fp16).Compile(compile_preference);
#else
(void)allow_fp16;
model = builder.Compile(compile_preference);
#endif
return model;
}

Expand Down Expand Up @@ -152,11 +148,10 @@ int main(int argc, char **argv) {

WARM_UP;

#if __ANDROID_API__ >= __ANDROID_API_P__
const std::vector<bool> fp16_candidates{false, true};
#else
const std::vector<bool> fp16_candidates{false};
#endif
const std::vector<bool> fp16_candidates =
ModelBuilder::GetAndroidSdkVersion() >= __ANDROID_API_P__
? std::vector<bool>{false, true}
: std::vector<bool>{false};
BENCHMARK(fp16_candidates, preference_candidates);
}
}
46 changes: 40 additions & 6 deletions dnnlibrary/ModelBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <dnnlibrary/ModelBuilder.h>

#include <sys/mman.h>
#include <sys/system_properties.h>
#include <algorithm>
#include <array>
#include <ctime>
Expand All @@ -29,6 +30,41 @@ using std::stringstream;
using std::vector;
using namespace android::nn::wrapper;

// Copy from
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/nnapi/nnapi_implementation.cc
int32_t ModelBuilder::GetAndroidSdkVersion() {
const char *sdkProp = "ro.build.version.sdk";
char sdkVersion[PROP_VALUE_MAX];
int length = __system_property_get(sdkProp, sdkVersion);
if (length != 0) {
int32_t result = 0;
for (int i = 0; i < length; ++i) {
int digit = sdkVersion[i] - '0';
if (digit < 0 || digit > 9) {
// Non-numeric SDK version, assume it's higher than expected;
return 0xffff;
}
result = result * 10 + digit;
}
// TODO(levp): remove once SDK gets updated to 29th level
// Upgrade SDK version for pre-release Q to be able to test
// functionality available from SDK level 29.
if (result == 28) {
char versionCodename[PROP_VALUE_MAX];
const char *versionCodenameProp = "ro.build.version.codename";
length =
__system_property_get(versionCodenameProp, versionCodename);
if (length != 0) {
if (versionCodename[0] == 'Q') {
return 29;
}
}
}
return result;
}
return 0;
}

void ModelBuilder::RegisterOperand(const std::string &name,
ModelBuilder::Index index,
const OperandType &operand_type) {
Expand Down Expand Up @@ -902,12 +938,10 @@ ModelBuilder &ModelBuilder::AddOutput(const std::string &name) {
}

ModelBuilder &ModelBuilder::AllowFp16(const bool allowed) {
#if __ANDROID_API__ >= __ANDROID_API_P__
ANeuralNetworksModel_relaxComputationFloat32toFloat16(dnn_model_->model_,
allowed);
#else
(void)allowed;
#endif
if (GetAndroidSdkVersion() >= __ANDROID_API_P__) {
ANeuralNetworksModel_relaxComputationFloat32toFloat16(
dnn_model_->model_, allowed);
}
return *this;
}
} // namespace dnn
2 changes: 2 additions & 0 deletions include/dnnlibrary/ModelBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class ModelBuilder {
static const uint32_t PREFERENCE_LOW_POWER =
ANEURALNETWORKS_PREFER_LOW_POWER;

static int32_t GetAndroidSdkVersion();

Index GetBlobIndex(const std::string &blobName);
Shape GetBlobDim(const std::string &blobName);
Shape GetBlobDim(Index index);
Expand Down

0 comments on commit 65b14c9

Please sign in to comment.