Skip to content

Commit

Permalink
Merge pull request #61 from JDAI-CV/update
Browse files Browse the repository at this point in the history
Add GetDevices and misc updates
  • Loading branch information
daquexian authored Jul 31, 2019
2 parents 848b29f + 29a6a99 commit c3cd873
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 35 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions binaries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
23 changes: 23 additions & 0 deletions binaries/get_devices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <chrono>
#include <iostream>
#include <vector>

#include <common/helper.h>
#include <dnnlibrary/ModelBuilder.h>

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;
}
}

15 changes: 1 addition & 14 deletions common/Shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t> paddings,
const std::vector<int32_t> strides,
const std::vector<int32_t> 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,
Expand Down Expand Up @@ -244,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;
}

Expand Down
27 changes: 27 additions & 0 deletions dnnlibrary/ModelBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,31 @@ ModelBuilder &ModelBuilder::AllowFp16(const bool allowed) {

ModelBuilder::ModelBuilder() : nnapi_(NnApiImplementation()) {
}

dnn::optional<std::vector<Device>> ModelBuilder::GetDevices() {
if (nnapi_->android_sdk_version >= __ANDROID_API_Q__) {
uint32_t device_count;
THROW_ON_ERROR(nnapi_->ANeuralNetworks_getDeviceCount(&device_count));
std::vector<Device> 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
2 changes: 1 addition & 1 deletion dnnlibrary/NeuralNetworksWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace wrapper {

OperandType::OperandType(Type type, std::vector<uint32_t> 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};
Expand Down
27 changes: 11 additions & 16 deletions include/common/Shaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t> strides,
const std::vector<int32_t> paddings,
const std::string &output_name);
void Conv(const std::string &input_name, const std::string &weight_name,
const std::vector<int32_t> strides,
const std::vector<int32_t> paddings,
const std::string &output_name);
void Conv(const std::string &input_name, const std::vector<int32_t> strides,
const std::vector<int32_t> dilations,
const std::vector<int32_t> paddings,
Expand All @@ -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<int32_t> strides,
const std::vector<int32_t> dilations,
const std::vector<int32_t> 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,
Expand Down Expand Up @@ -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<int32_t> 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<int32_t> kernel_shape,
const std::vector<int32_t> pads,
const std::vector<int32_t> strides,
const std::string &output_name);
Expand All @@ -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<int32_t> &block_sizes,
const std::string &output_name);
Expand Down
10 changes: 10 additions & 0 deletions include/dnnlibrary/Device.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <string>

namespace dnn {
struct Device {
const std::string name;
const int64_t feature_level;
const int type;
const std::string version;
};
} // namespace dnn
5 changes: 3 additions & 2 deletions include/dnnlibrary/ModelBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <common/Shaper.h>
#include <common/StrKeyMap.h>
#include <common/data_types.h>
#include <dnnlibrary/Device.h>
#include <dnnlibrary/Model.h>
#include <dnnlibrary/NeuralNetworksWrapper.h>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -284,6 +283,8 @@ class ModelBuilder {
void AddScalarOperands(IndexSeq &indexes, Args... args) {
(indexes.push_back(OperandFromScalar(args)), ...);
}

dnn::optional<std::vector<Device>> GetDevices();
};
} // namespace dnn
#endif // NNAPIEXAMPLE_MODELBUILDER_H

0 comments on commit c3cd873

Please sign in to comment.