Skip to content

Commit 848b29f

Browse files
authored
Merge pull request #60 from JDAI-CV/update_wrapper
Update nnapi wrapper from tensorflow
2 parents ab22710 + 4168162 commit 848b29f

14 files changed

Lines changed: 1868 additions & 5974 deletions

binaries/dnn_benchmark.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool hasEnding(std::string const &fullString, std::string const &ending) {
3939
}
4040

4141
auto GetModel(css &daqName, const bool allow_fp16,
42-
const PreferenceCode &compile_preference) {
42+
const int compile_preference) {
4343
std::unique_ptr<Model> model;
4444
ModelBuilder builder;
4545
if (hasEnding(daqName, ".daq")) {
@@ -64,7 +64,7 @@ auto GetModel(css &daqName, const bool allow_fp16,
6464
return model;
6565
}
6666

67-
auto PrefCodeToStr(const PreferenceCode &preference_code) {
67+
auto PrefCodeToStr(const int &preference_code) {
6868
if (preference_code == ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER) {
6969
return "fast single";
7070
}
@@ -129,13 +129,13 @@ int main(int argc, char **argv) {
129129
} \
130130
}
131131

132-
const std::vector<PreferenceCode> preference_candidates{
132+
const std::vector<int> preference_candidates{
133133
ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER,
134134
ANEURALNETWORKS_PREFER_SUSTAINED_SPEED,
135135
ANEURALNETWORKS_PREFER_LOW_POWER};
136136
if (quant) {
137137
uint8_t data[input_len];
138-
uint8_t output[output_len];
138+
float output[output_len];
139139
WARM_UP;
140140
const std::vector<bool> fp16_candidates{false};
141141
BENCHMARK(fp16_candidates, preference_candidates);
@@ -149,7 +149,7 @@ int main(int argc, char **argv) {
149149
WARM_UP;
150150

151151
const std::vector<bool> fp16_candidates =
152-
ModelBuilder::GetAndroidSdkVersion() >= __ANDROID_API_P__
152+
GetAndroidSdkVersion() >= __ANDROID_API_P__
153153
? std::vector<bool>{false, true}
154154
: std::vector<bool>{false};
155155
BENCHMARK(fp16_candidates, preference_candidates);

dnnlibrary/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ set(dnnlibrary_src
44
${PROJECT_SOURCE_DIR}/include/dnnlibrary/ModelBuilder.h
55
${PROJECT_SOURCE_DIR}/include/dnnlibrary/Model.h
66
${PROJECT_SOURCE_DIR}/include/dnnlibrary/DaqReader.h
7-
${PROJECT_SOURCE_DIR}/include/dnnlibrary/NeuralNetworksWrapper.h
7+
${PROJECT_SOURCE_DIR}/include/dnnlibrary/nnapi_implementation.h
88
android_log_helper.h
99
operand_helper.h
1010
flatbuffers_helper.h
1111
ModelBuilder.cpp
1212
Model.cpp
1313
DaqReader.cpp
1414
NeuralNetworksWrapper.cpp
15+
nnapi_implementation.cc
1516
${PROJECT_SOURCE_DIR}/include/common/Shaper.h
1617
${PROJECT_SOURCE_DIR}/common/Shaper.cpp
1718
${PROJECT_SOURCE_DIR}/include/common/StrKeyMap.h

dnnlibrary/DaqReader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ std::string layer_type_to_str(DNN::LayerType type) {
5959
int convert_fuse_code_to_nnapi(const DNN::FuseCode fuse_code) {
6060
switch (fuse_code) {
6161
case DNN::FuseCode::None:
62-
return FuseCode::ANEURALNETWORKS_FUSED_NONE;
62+
return ANEURALNETWORKS_FUSED_NONE;
6363
case DNN::FuseCode::Relu:
64-
return FuseCode::ANEURALNETWORKS_FUSED_RELU;
64+
return ANEURALNETWORKS_FUSED_RELU;
6565
case DNN::FuseCode::Relu1:
66-
return FuseCode::ANEURALNETWORKS_FUSED_RELU1;
66+
return ANEURALNETWORKS_FUSED_RELU1;
6767
case DNN::FuseCode::Relu6:
68-
return FuseCode::ANEURALNETWORKS_FUSED_RELU6;
68+
return ANEURALNETWORKS_FUSED_RELU6;
6969
}
7070
throw std::invalid_argument("Invalid fuse_code");
7171
}

dnnlibrary/Model.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ void Model::PrepareForExecution() {
2929
throw std::invalid_argument(
3030
"Error in PrepareForExecution, compilation_ == nullptr");
3131
}
32-
THROW_ON_ERROR(ANeuralNetworksExecution_create(compilation_, &execution_));
32+
THROW_ON_ERROR(
33+
nnapi_->ANeuralNetworksExecution_create(compilation_, &execution_));
3334
prepared_for_exe_ = true;
3435
}
3536

37+
Model::Model() : nnapi_(NnApiImplementation()) {
38+
}
39+
3640
Model::~Model() {
3741
munmap(data_, data_size_);
38-
ANeuralNetworksModel_free(model_);
39-
ANeuralNetworksCompilation_free(compilation_);
40-
ANeuralNetworksMemory_free(memory_);
42+
nnapi_->ANeuralNetworksModel_free(model_);
43+
nnapi_->ANeuralNetworksCompilation_free(compilation_);
44+
nnapi_->ANeuralNetworksMemory_free(memory_);
4145
}
4246

4347
void Model::SetInputBuffer(const int32_t index, const float *buffer) {
@@ -52,8 +56,8 @@ void Model::SetInputBuffer(const int32_t index, const void *buffer,
5256
const size_t elemsize) {
5357
if (!prepared_for_exe_) PrepareForExecution();
5458
auto size = shaper_.GetSize(input_names_[index]) * elemsize;
55-
THROW_ON_ERROR(ANeuralNetworksExecution_setInput(execution_, index, nullptr,
56-
buffer, size))
59+
THROW_ON_ERROR(nnapi_->ANeuralNetworksExecution_setInput(
60+
execution_, index, nullptr, buffer, size))
5761
}
5862

5963
void Model::SetOutputBuffer(const int32_t index, float *buffer) {
@@ -72,17 +76,18 @@ void Model::SetOutputBuffer(const int32_t index, void *buffer,
7276
const size_t elemsize) {
7377
if (!prepared_for_exe_) PrepareForExecution();
7478
auto size = shaper_.GetSize(output_names_[index]) * elemsize;
75-
THROW_ON_ERROR(ANeuralNetworksExecution_setOutput(execution_, index,
76-
nullptr, buffer, size))
79+
THROW_ON_ERROR(nnapi_->ANeuralNetworksExecution_setOutput(
80+
execution_, index, nullptr, buffer, size))
7781
}
7882

7983
void Model::PredictAfterSetInputBuffer() {
8084
ANeuralNetworksEvent *event = nullptr;
81-
THROW_ON_ERROR(ANeuralNetworksExecution_startCompute(execution_, &event));
82-
THROW_ON_ERROR(ANeuralNetworksEvent_wait(event));
85+
THROW_ON_ERROR(
86+
nnapi_->ANeuralNetworksExecution_startCompute(execution_, &event));
87+
THROW_ON_ERROR(nnapi_->ANeuralNetworksEvent_wait(event));
8388

84-
ANeuralNetworksEvent_free(event);
85-
ANeuralNetworksExecution_free(execution_);
89+
nnapi_->ANeuralNetworksEvent_free(event);
90+
nnapi_->ANeuralNetworksExecution_free(execution_);
8691
prepared_for_exe_ = false;
8792
}
8893

dnnlibrary/ModelBuilder.cpp

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,6 @@ using std::stringstream;
3030
using std::vector;
3131
using namespace android::nn::wrapper;
3232

33-
// Copy from
34-
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/nnapi/nnapi_implementation.cc
35-
int32_t ModelBuilder::GetAndroidSdkVersion() {
36-
const char *sdkProp = "ro.build.version.sdk";
37-
char sdkVersion[PROP_VALUE_MAX];
38-
int length = __system_property_get(sdkProp, sdkVersion);
39-
if (length != 0) {
40-
int32_t result = 0;
41-
for (int i = 0; i < length; ++i) {
42-
int digit = sdkVersion[i] - '0';
43-
if (digit < 0 || digit > 9) {
44-
// Non-numeric SDK version, assume it's higher than expected;
45-
return 0xffff;
46-
}
47-
result = result * 10 + digit;
48-
}
49-
// TODO(levp): remove once SDK gets updated to 29th level
50-
// Upgrade SDK version for pre-release Q to be able to test
51-
// functionality available from SDK level 29.
52-
if (result == 28) {
53-
char versionCodename[PROP_VALUE_MAX];
54-
const char *versionCodenameProp = "ro.build.version.codename";
55-
length =
56-
__system_property_get(versionCodenameProp, versionCodename);
57-
if (length != 0) {
58-
if (versionCodename[0] == 'Q') {
59-
return 29;
60-
}
61-
}
62-
}
63-
return result;
64-
}
65-
return 0;
66-
}
67-
6833
void ModelBuilder::RegisterOperand(const std::string &name,
6934
ModelBuilder::Index index,
7035
const OperandType &operand_type) {
@@ -539,7 +504,7 @@ ModelBuilder::Index ModelBuilder::AddDequantize(const std::string &input,
539504
input_indexes.push_back(input_idx);
540505
shaper_.Identity(input, output);
541506
const OperandType operand_type =
542-
GetOperandType(Type::FLOAT32, shaper_[output]);
507+
GetOperandType(Type::TENSOR_FLOAT32, shaper_[output]);
543508
const auto output_idx = AddOperation(ANEURALNETWORKS_DEQUANTIZE,
544509
input_indexes, operand_type)[0];
545510
RegisterOperand(output, output_idx, operand_type);
@@ -696,7 +661,7 @@ OperandType ModelBuilder::GetOperandType(const QuantInfo &quant_info,
696661
map_type##_operand_map_.end()) { \
697662
const auto index = AddNewOperand({Type::op_type}); \
698663
THROW_ON_ERROR_WITH_NOTE( \
699-
ANeuralNetworksModel_setOperandValue( \
664+
nnapi_->ANeuralNetworksModel_setOperandValue( \
700665
dnn_model_->model_, index, &value, sizeof(value)), \
701666
"value: " + std::to_string(value)); \
702667
map_type##_operand_map_[value] = index; \
@@ -713,15 +678,15 @@ DEFINE_OPERAND_FROM_SCALAR(float, float32, FLOAT32);
713678
ModelBuilder::Index ModelBuilder::AddMissingOperand(
714679
const OperandType &operand_type) {
715680
const auto index = AddNewOperand(operand_type);
716-
THROW_ON_ERROR(ANeuralNetworksModel_setOperandValue(dnn_model_->model_,
717-
index, nullptr, 0));
681+
THROW_ON_ERROR(nnapi_->ANeuralNetworksModel_setOperandValue(
682+
dnn_model_->model_, index, nullptr, 0));
718683
return index;
719684
}
720685

721686
ModelBuilder::Index ModelBuilder::AddNewOperand(
722687
const OperandType &operand_type) {
723-
THROW_ON_ERROR(ANeuralNetworksModel_addOperand(dnn_model_->model_,
724-
&operand_type.operandType));
688+
THROW_ON_ERROR(nnapi_->ANeuralNetworksModel_addOperand(
689+
dnn_model_->model_, &operand_type.operandType));
725690
return next_index_++;
726691
}
727692

@@ -732,7 +697,7 @@ ModelBuilder::Index ModelBuilder::AddTensorFromMemory(const string &name,
732697
throw std::invalid_argument("");
733698
DNN_ASSERT(!dimen.empty(), "");
734699
const auto index = AddNewOperand({Type::TENSOR_FLOAT32, dimen});
735-
THROW_ON_ERROR(ANeuralNetworksModel_setOperandValueFromMemory(
700+
THROW_ON_ERROR(nnapi_->ANeuralNetworksModel_setOperandValueFromMemory(
736701
dnn_model_->model_, index, dnn_model_->memory_,
737702
addr - dnn_model_->data_, Product(dimen) * sizeof(float)));
738703
shaper_.AddShape(name, dimen);
@@ -775,7 +740,7 @@ ModelBuilder::Index ModelBuilder::AddTensorFromBuffer(
775740
typeToStr(operand_type.type));
776741
}
777742
uint32_t index = AddNewOperand(operand_type);
778-
THROW_ON_ERROR(ANeuralNetworksModel_setOperandValue(
743+
THROW_ON_ERROR(nnapi_->ANeuralNetworksModel_setOperandValue(
779744
dnn_model_->model_, index, buffer,
780745
Product(operand_type.dimensions) * element_size));
781746
shaper_.AddShape(name, operand_type.dimensions);
@@ -797,27 +762,28 @@ std::unique_ptr<Model> ModelBuilder::Compile(uint32_t preference) {
797762
}
798763
}
799764
THROW_ON_ERROR_WITH_NOTE(
800-
ANeuralNetworksModel_identifyInputsAndOutputs(
765+
nnapi_->ANeuralNetworksModel_identifyInputsAndOutputs(
801766
dnn_model_->model_, static_cast<uint32_t>(input_index_vec_.size()),
802767
&input_index_vec_[0],
803768
static_cast<uint32_t>(output_index_vec_.size()),
804769
&output_index_vec_[0]),
805770
"on identifyInputsAndOutputs");
806771

807-
THROW_ON_ERROR_WITH_NOTE(ANeuralNetworksModel_finish(dnn_model_->model_),
808-
"on model finish");
772+
THROW_ON_ERROR_WITH_NOTE(
773+
nnapi_->ANeuralNetworksModel_finish(dnn_model_->model_),
774+
"on model finish");
809775

810776
;
811-
THROW_ON_ERROR_WITH_NOTE(ANeuralNetworksCompilation_create(
777+
THROW_ON_ERROR_WITH_NOTE(nnapi_->ANeuralNetworksCompilation_create(
812778
dnn_model_->model_, &dnn_model_->compilation_),
813779
"on create");
814780

815-
THROW_ON_ERROR_WITH_NOTE(ANeuralNetworksCompilation_setPreference(
781+
THROW_ON_ERROR_WITH_NOTE(nnapi_->ANeuralNetworksCompilation_setPreference(
816782
dnn_model_->compilation_, preference),
817783
"on setPreference");
818784

819785
THROW_ON_ERROR_WITH_NOTE(
820-
ANeuralNetworksCompilation_finish(dnn_model_->compilation_),
786+
nnapi_->ANeuralNetworksCompilation_finish(dnn_model_->compilation_),
821787
"on compilation finish");
822788

823789
VLOG(5) << "Finishing.. Here are operands in the model:";
@@ -904,7 +870,7 @@ ModelBuilder::IndexSeq ModelBuilder::AddOperation(
904870
}
905871

906872
THROW_ON_ERROR_WITH_NOTE(
907-
ANeuralNetworksModel_addOperation(
873+
nnapi_->ANeuralNetworksModel_addOperation(
908874
dnn_model_->model_, op, input_indexes.size(), &input_indexes[0],
909875
output_indexes.size(), &output_indexes[0]),
910876
"op = " + std::to_string(op));
@@ -914,16 +880,16 @@ ModelBuilder::IndexSeq ModelBuilder::AddOperation(
914880

915881
void ModelBuilder::Prepare() {
916882
dnn_model_ = std::unique_ptr<Model>(new Model());
917-
const auto ret = ANeuralNetworksModel_create(&dnn_model_->model_);
883+
const auto ret = nnapi_->ANeuralNetworksModel_create(&dnn_model_->model_);
918884
if (ret == ANEURALNETWORKS_OUT_OF_MEMORY) {
919885
throw std::bad_alloc();
920886
}
921887
}
922888

923889
void ModelBuilder::SetMemory(int fd, size_t size, size_t offset) {
924890
ANeuralNetworksMemory *mem = nullptr;
925-
THROW_ON_ERROR(
926-
ANeuralNetworksMemory_createFromFd(size, PROT_READ, fd, offset, &mem));
891+
THROW_ON_ERROR(nnapi_->ANeuralNetworksMemory_createFromFd(
892+
size, PROT_READ, fd, offset, &mem));
927893
dnn_model_->memory_ = mem;
928894
}
929895

@@ -938,10 +904,14 @@ ModelBuilder &ModelBuilder::AddOutput(const std::string &name) {
938904
}
939905

940906
ModelBuilder &ModelBuilder::AllowFp16(const bool allowed) {
941-
if (GetAndroidSdkVersion() >= __ANDROID_API_P__) {
942-
ANeuralNetworksModel_relaxComputationFloat32toFloat16(
907+
if (nnapi_->ANeuralNetworksModel_relaxComputationFloat32toFloat16 !=
908+
nullptr) {
909+
nnapi_->ANeuralNetworksModel_relaxComputationFloat32toFloat16(
943910
dnn_model_->model_, allowed);
944911
}
945912
return *this;
946913
}
914+
915+
ModelBuilder::ModelBuilder() : nnapi_(NnApiImplementation()) {
916+
}
947917
} // namespace dnn

dnnlibrary/NeuralNetworksWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ OperandType::OperandType(Type type, std::vector<uint32_t> d, float scale,
1414
dimensions = {1};
1515
}
1616
} else {
17-
DNN_ASSERT(!isScalarType(type), typeToStr(type));
17+
DNN_ASSERT(!isScalarType(type), typeToStr(type), " ", dimensions);
1818
}
1919
operandType = {
2020
.type = static_cast<int32_t>(type),

dnnlibrary/nnapi_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <string>
55

6-
#include <dnnlibrary/NeuralNetworksWrapper.h>
6+
#include <dnnlibrary/NeuralNetworksTypes.h>
77

88
#define THROW_ON_ERROR(val) \
99
{ \

0 commit comments

Comments
 (0)