From cf9c566d8348510d74c60dabdb51d4085280cf9f Mon Sep 17 00:00:00 2001 From: daquexian Date: Wed, 31 Jul 2019 17:38:17 +0800 Subject: [PATCH 1/5] Remove redundant shaper functions --- common/Shaper.cpp | 12 ------------ include/common/Shaper.h | 27 +++++++++++---------------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/common/Shaper.cpp b/common/Shaper.cpp index 5b64c53..5cf12f9 100644 --- a/common/Shaper.cpp +++ b/common/Shaper.cpp @@ -33,18 +33,6 @@ void Shaper::Conv(const std::string &input_name, paddings[1], paddings[3], paddings[0], paddings[2], weight_name, output_name); } - -void Shaper::Conv(const std::string &input_name, - const std::vector paddings, - const std::vector strides, - const std::vector dilations, - const std::string &weight_name, const std::string &bias_name, - const std::string &output_name) { - (void)bias_name; - Shaper::Conv(input_name, strides, dilations, paddings, weight_name, - output_name); -} - void Shaper::Conv(const std::string &input, const std::string &weight, int32_t padding_left, int32_t padding_right, int32_t padding_top, int32_t padding_bottom, int32_t stride_x, diff --git a/include/common/Shaper.h b/include/common/Shaper.h index 37471a5..b1aac69 100644 --- a/include/common/Shaper.h +++ b/include/common/Shaper.h @@ -15,11 +15,10 @@ class Shaper { static len_t total(const Shape &shape); - void Conv(const std::string &input_name, - const std::string &weight_name, - const std::vector strides, - const std::vector paddings, - const std::string &output_name); + void Conv(const std::string &input_name, const std::string &weight_name, + const std::vector strides, + const std::vector paddings, + const std::string &output_name); void Conv(const std::string &input_name, const std::vector strides, const std::vector dilations, const std::vector paddings, @@ -28,11 +27,6 @@ class Shaper { int32_t padding_left, int32_t padding_right, int32_t padding_top, int32_t padding_bottom, int32_t stride_x, int32_t stride_y, const std::string &output); - void Conv(const std::string &input_name, const std::vector strides, - const std::vector dilations, - const std::vector paddings, - const std::string &weight_name, const std::string &bias_name, - const std::string &output_name); void Conv(const std::string &input_name, int32_t strideX, int32_t strideY, int32_t dilationX, int32_t dilationY, int32_t paddingLeft, int32_t paddingRight, int32_t paddingTop, int32_t paddingBottom, @@ -66,11 +60,11 @@ class Shaper { int32_t endMask, int32_t shrinkAxisMask, const std::string &output_name); void Pool(const std::string &input_name, int32_t padding_left, - int32_t padding_right, int32_t padding_top, - int32_t padding_bottom, int32_t stride_x, int32_t stride_y, - int32_t width, int32_t height, - const std::string &output_name); - void Pool(const std::string &input_name, const std::vector kernel_shape, + int32_t padding_right, int32_t padding_top, + int32_t padding_bottom, int32_t stride_x, int32_t stride_y, + int32_t width, int32_t height, const std::string &output_name); + void Pool(const std::string &input_name, + const std::vector kernel_shape, const std::vector pads, const std::vector strides, const std::string &output_name); @@ -88,7 +82,8 @@ class Shaper { void Affine(const std::string &input_name, const std::string &output_name); void Affine(const std::string &input_name, const std::string &a, const std::string &b, const std::string &output_name); - void Identity(const std::string &input_name, const std::string &output_name); + void Identity(const std::string &input_name, + const std::string &output_name); void BatchToSpace(const std::string &input_name, const std::vector &block_sizes, const std::string &output_name); From 4923e9fe039b3486b02270d02a902217db91928b Mon Sep 17 00:00:00 2001 From: daquexian Date: Wed, 31 Jul 2019 18:52:15 +0800 Subject: [PATCH 2/5] Add getDevices() --- binaries/CMakeLists.txt | 10 ++++++++++ binaries/get_devices.cpp | 23 +++++++++++++++++++++++ common/Shaper.cpp | 3 +-- dnnlibrary/ModelBuilder.cpp | 27 +++++++++++++++++++++++++++ include/dnnlibrary/Device.h | 10 ++++++++++ include/dnnlibrary/ModelBuilder.h | 5 +++-- 6 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 binaries/get_devices.cpp create mode 100644 include/dnnlibrary/Device.h diff --git a/binaries/CMakeLists.txt b/binaries/CMakeLists.txt index ba3ff7f..365d521 100644 --- a/binaries/CMakeLists.txt +++ b/binaries/CMakeLists.txt @@ -37,4 +37,14 @@ if (DNN_BUILD_BIN) if ((NOT DNN_READ_ONNX) OR DNN_SYSTEM_PROTOBUF) treat_warnings_as_errors(ex_model_builder) endif() + + add_executable(get_devices + get_devices.cpp) + target_link_libraries(get_devices + dnnlibrary) + target_include_directories(get_devices + PRIVATE + ${PROJECT_SOURCE_DIR} + ) + endif() diff --git a/binaries/get_devices.cpp b/binaries/get_devices.cpp new file mode 100644 index 0000000..3074cd2 --- /dev/null +++ b/binaries/get_devices.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +#include +#include + +using namespace android::nn::wrapper; +using dnn::ModelBuilder; + +int main() { + ModelBuilder builder; + builder.Prepare(); + const auto devices = builder.getDevices(); + if (devices.has_value()) { + for (const auto &device : devices.value()) { + PNT(device.name, device.feature_level, device.type, device.version); + } + } else { + std::cout << "Cannot get devices" << std::endl; + } +} + diff --git a/common/Shaper.cpp b/common/Shaper.cpp index 5cf12f9..8081e2a 100644 --- a/common/Shaper.cpp +++ b/common/Shaper.cpp @@ -232,8 +232,7 @@ void Shaper::Eltwise(const std::string &input1_name, auto shape1 = shape_map_.at(input1_name); auto shape2 = shape_map_.at(input2_name); // TODO: broadcasting - auto output_shape = - shape1.size() >= shape2.size() ? shape1 : shape2; + auto output_shape = shape1.size() >= shape2.size() ? shape1 : shape2; shape_map_[output_name] = output_shape; } diff --git a/dnnlibrary/ModelBuilder.cpp b/dnnlibrary/ModelBuilder.cpp index 09ba49e..71aed8c 100644 --- a/dnnlibrary/ModelBuilder.cpp +++ b/dnnlibrary/ModelBuilder.cpp @@ -914,4 +914,31 @@ ModelBuilder &ModelBuilder::AllowFp16(const bool allowed) { ModelBuilder::ModelBuilder() : nnapi_(NnApiImplementation()) { } + +dnn::optional> ModelBuilder::getDevices() { + if (nnapi_->android_sdk_version >= __ANDROID_API_Q__) { + uint32_t device_count; + THROW_ON_ERROR(nnapi_->ANeuralNetworks_getDeviceCount(&device_count)); + std::vector devices; + FORZ(i, device_count) { + ANeuralNetworksDevice *nn_device; + nnapi_->ANeuralNetworks_getDevice(i, &nn_device); + const char *nn_name_ptr; + nnapi_->ANeuralNetworksDevice_getName(nn_device, &nn_name_ptr); + const std::string device_name(nn_name_ptr); + int64_t feature_level; + nnapi_->ANeuralNetworksDevice_getFeatureLevel(nn_device, &feature_level); + int type; + nnapi_->ANeuralNetworksDevice_getType(nn_device, &type); + const char *nn_version_ptr; + nnapi_->ANeuralNetworksDevice_getVersion(nn_device, &nn_version_ptr); + const std::string version(nn_version_ptr); + Device device{device_name, feature_level, type, version}; + devices.push_back(device); + } + return devices; + } else { + return dnn::nullopt; + } +} } // namespace dnn diff --git a/include/dnnlibrary/Device.h b/include/dnnlibrary/Device.h new file mode 100644 index 0000000..0e2801a --- /dev/null +++ b/include/dnnlibrary/Device.h @@ -0,0 +1,10 @@ +#include + +namespace dnn { +struct Device { + const std::string name; + const int64_t feature_level; + const int type; + const std::string version; +}; +} // namespace dnn diff --git a/include/dnnlibrary/ModelBuilder.h b/include/dnnlibrary/ModelBuilder.h index 2cdc92c..ca6b37a 100644 --- a/include/dnnlibrary/ModelBuilder.h +++ b/include/dnnlibrary/ModelBuilder.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -103,8 +104,6 @@ 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); @@ -284,6 +283,8 @@ class ModelBuilder { void AddScalarOperands(IndexSeq &indexes, Args... args) { (indexes.push_back(OperandFromScalar(args)), ...); } + + dnn::optional> getDevices(); }; } // namespace dnn #endif // NNAPIEXAMPLE_MODELBUILDER_H From 812bbe013794517235bf6b1ff3dedd1fe71bd25c Mon Sep 17 00:00:00 2001 From: daquexian Date: Wed, 31 Jul 2019 18:52:23 +0800 Subject: [PATCH 3/5] Fix std::nullopt --- dnnlibrary/NeuralNetworksWrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dnnlibrary/NeuralNetworksWrapper.cpp b/dnnlibrary/NeuralNetworksWrapper.cpp index 57be2c9..6e38831 100644 --- a/dnnlibrary/NeuralNetworksWrapper.cpp +++ b/dnnlibrary/NeuralNetworksWrapper.cpp @@ -8,7 +8,7 @@ namespace wrapper { OperandType::OperandType(Type type, std::vector d, float scale, int32_t zeroPoint) - : type(type), dimensions(std::move(d)), channelQuant(std::nullopt) { + : type(type), dimensions(std::move(d)), channelQuant(dnn::nullopt) { if (dimensions.empty()) { if (!isScalarType(type)) { dimensions = {1}; From b80ab3c2f92620ddfe08c3740591acafdd18ba85 Mon Sep 17 00:00:00 2001 From: daquexian Date: Wed, 31 Jul 2019 18:57:07 +0800 Subject: [PATCH 4/5] getDevices -> GetDevices --- binaries/get_devices.cpp | 2 +- dnnlibrary/ModelBuilder.cpp | 2 +- include/dnnlibrary/ModelBuilder.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/binaries/get_devices.cpp b/binaries/get_devices.cpp index 3074cd2..ccfb41a 100644 --- a/binaries/get_devices.cpp +++ b/binaries/get_devices.cpp @@ -11,7 +11,7 @@ using dnn::ModelBuilder; int main() { ModelBuilder builder; builder.Prepare(); - const auto devices = builder.getDevices(); + const auto devices = builder.GetDevices(); if (devices.has_value()) { for (const auto &device : devices.value()) { PNT(device.name, device.feature_level, device.type, device.version); diff --git a/dnnlibrary/ModelBuilder.cpp b/dnnlibrary/ModelBuilder.cpp index 71aed8c..71e98c8 100644 --- a/dnnlibrary/ModelBuilder.cpp +++ b/dnnlibrary/ModelBuilder.cpp @@ -915,7 +915,7 @@ ModelBuilder &ModelBuilder::AllowFp16(const bool allowed) { ModelBuilder::ModelBuilder() : nnapi_(NnApiImplementation()) { } -dnn::optional> ModelBuilder::getDevices() { +dnn::optional> ModelBuilder::GetDevices() { if (nnapi_->android_sdk_version >= __ANDROID_API_Q__) { uint32_t device_count; THROW_ON_ERROR(nnapi_->ANeuralNetworks_getDeviceCount(&device_count)); diff --git a/include/dnnlibrary/ModelBuilder.h b/include/dnnlibrary/ModelBuilder.h index ca6b37a..980b306 100644 --- a/include/dnnlibrary/ModelBuilder.h +++ b/include/dnnlibrary/ModelBuilder.h @@ -284,7 +284,7 @@ class ModelBuilder { (indexes.push_back(OperandFromScalar(args)), ...); } - dnn::optional> getDevices(); + dnn::optional> GetDevices(); }; } // namespace dnn #endif // NNAPIEXAMPLE_MODELBUILDER_H From 29a6a9984fbb075795fa7e746530235bb68245dd Mon Sep 17 00:00:00 2001 From: daquexian Date: Wed, 31 Jul 2019 19:21:53 +0800 Subject: [PATCH 5/5] have no idea why cmake_cxx_standard doesn't work suddenly --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 44b2ce5..561c901 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,9 +22,8 @@ include(cmake/flatbuffers.cmake) configure_flatbuffers() if (${CMAKE_SYSTEM_NAME} STREQUAL "Android") - set (CMAKE_CXX_STANDARD 17) - add_compile_options(-Os) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") if (${DNN_READ_ONNX}) include(cmake/onnx.cmake) configure_onnx()