From: 박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 Date: Fri, 6 Apr 2018 02:15:44 +0000 (+0900) Subject: [Pure ACL Runtime] Implement ANeuralNetworkModel (#477) X-Git-Tag: 0.1~406 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=294bbb7ea2e5123f3475fb5e3a72ae81ed8cc5c1;p=platform%2Fcore%2Fml%2Fnnfw.git [Pure ACL Runtime] Implement ANeuralNetworkModel (#477) This commit partially implements ANeuralNetworkModel in Pure ACL Runtime to record opearnd types. Signed-off-by: Jonghyun Park --- diff --git a/tools/nnapi_bindings/bindings/pure_arm_compute/CMakeLists.txt b/tools/nnapi_bindings/bindings/pure_arm_compute/CMakeLists.txt index 87fdc1b..541d986 100644 --- a/tools/nnapi_bindings/bindings/pure_arm_compute/CMakeLists.txt +++ b/tools/nnapi_bindings/bindings/pure_arm_compute/CMakeLists.txt @@ -3,6 +3,7 @@ if(TARGET arm_compute) add_library(nnapi_pure_arm_compute SHARED ${SOURCES}) target_include_directories(nnapi_pure_arm_compute PUBLIC ${NNAPI_INCLUDE_DIR}) + target_include_directories(nnapi_pure_arm_compute PUBLIC src) target_link_libraries(nnapi_pure_arm_compute arm_compute) set_target_properties(nnapi_pure_arm_compute PROPERTIES OUTPUT_NAME neuralnetworks) endif(TARGET arm_compute) diff --git a/tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.cc b/tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.cc new file mode 100644 index 0000000..bd1cbb0 --- /dev/null +++ b/tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.cc @@ -0,0 +1,42 @@ +#include "internal/Model.h" + +namespace internal +{ +namespace tflite +{ +namespace operand +{ + +Shape::Shape(uint32_t rank) +{ + _dims.resize(rank); +} + +} // namespace operand +} // namespace tflite +} // namespace internal + +namespace internal +{ +namespace tflite +{ +namespace operand +{ + +Index Set::append(const Shape &shape) +{ + int32_t index = _objects.size(); + + _objects.emplace_back(new Object{shape}); + + return Index{index}; +} + +const Object &Set::at(const Index &index) const +{ + return *(_objects.at(index.asInt())); +} + +} // namespace operand +} // namespace tflite +} // namespace internal diff --git a/tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.h b/tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.h new file mode 100644 index 0000000..dad3ef3 --- /dev/null +++ b/tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.h @@ -0,0 +1,129 @@ +#ifndef __INTERNAL_MODEL_H__ +#define __INTERNAL_MODEL_H__ + +namespace internal +{ +namespace tflite +{ +namespace operand +{ + +class Index +{ +public: + explicit Index(int value) : _value{value} + { + // DO NOTHING + } + +public: + int asInt(void) const { return _value; } + +private: + int _value; +}; + +} // namespace operand +} // namespace tflite +} // namespace internal + +#include +#include + +namespace internal +{ +namespace tflite +{ +namespace operand +{ + +struct Shape +{ +public: + Shape(uint32_t rank); + +public: + uint32_t rank(void) const { return _dims.size(); } + +public: + uint32_t dim(uint32_t n) const { return _dims.at(n); } + uint32_t &dim(uint32_t n) { return _dims.at(n); } + +private: + std::vector _dims; +}; + +} // namespace operand +} // namespace tflite +} // namespace internal + +namespace internal +{ +namespace tflite +{ +namespace operand +{ + +class Object +{ +public: + explicit Object(const Shape &shape) : _shape{shape} + { + // DO NOTHING + } + +public: + const Shape &shape(void) const { return _shape; } + +private: + const Shape _shape; +}; + +} // namespace operand +} // namespace tflite +} // namespace internal + +#include + +namespace internal +{ +namespace tflite +{ +namespace operand +{ + +class Set +{ +public: + Index append(const Shape &); + +public: + const Object &at(const Index &) const; + +private: + std::vector> _objects; +}; + +} // namespace operand +} // namespace tflite +} // namespace internal + +namespace internal +{ +namespace tflite +{ + +class Model +{ +public: + operand::Set &operands(void) { return _operands; } + const operand::Set &operands(void) const { return _operands; } + +private: + operand::Set _operands; +}; + +} // namespace tflite +} // namespace internal + +#endif // __INTERNAL_MODEL_H__ diff --git a/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.cc b/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.cc index 3b064f1..7d276c4 100644 --- a/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.cc +++ b/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.cc @@ -1,22 +1,45 @@ #include +#include + #include "model.h" ResultCode ANeuralNetworksModel_create(ANeuralNetworksModel** model) { + *model = new ANeuralNetworksModel{}; + return ANEURALNETWORKS_NO_ERROR; } ResultCode ANeuralNetworksModel_free(ANeuralNetworksModel* model) { + delete model; + return ANEURALNETWORKS_NO_ERROR; } ResultCode ANeuralNetworksModel_addOperand(ANeuralNetworksModel* model, const ANeuralNetworksOperandType *type) { + // ASSUME A tensor operand always consists of fp32 values + // NOTE We do not care about scala operands. + assert(!(type->dimensionCount > 1) || (type->type == 3 /* ANEURALNETWORKS_TENSOR_FLOAT32 */)); + + internal::tflite::operand::Shape shape(type->dimensionCount); + + for (uint32_t axis = 0; axis < type->dimensionCount; ++axis) + { + shape.dim(axis) = type->dimensions[axis]; + } + + model->deref().operands().append(shape); + + // NOTE We do NOT allocate CLTensor here as we do not how to interpret this one. + // TensorFlow Lite may interpret a rank-4 tensor either as a feature map (with batch) or + // a convolution kernel. + return ANEURALNETWORKS_NO_ERROR; } @@ -57,3 +80,11 @@ ResultCode ANeuralNetworksModel_finish(ANeuralNetworksModel* model) { return ANEURALNETWORKS_NO_ERROR; } + +// +// ANeuralNetworksModel +// +ANeuralNetworksModel::ANeuralNetworksModel() : _model{new internal::tflite::Model} +{ + // DO NOTHING +} diff --git a/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.h b/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.h index 8da8f74..3798b90 100644 --- a/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.h +++ b/tools/nnapi_bindings/bindings/pure_arm_compute/src/model.h @@ -1,8 +1,24 @@ #ifndef __MODEL_H__ #define __MODEL_H__ +#include "internal/Model.h" + struct ANeuralNetworksModel { +public: + ANeuralNetworksModel(); + +public: + internal::tflite::Model &deref(void) { return *_model; } + +public: + void release(std::shared_ptr &model) + { + model = _model; + } + +private: + std::shared_ptr _model; }; #endif // __MODEL_H__