[Pure ACL Runtime] Implement ANeuralNetworkModel (#477)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 6 Apr 2018 02:15:44 +0000 (11:15 +0900)
committer김정현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh0822.kim@samsung.com>
Fri, 6 Apr 2018 02:15:44 +0000 (11:15 +0900)
This commit partially implements ANeuralNetworkModel in Pure ACL Runtime
to record opearnd types.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
tools/nnapi_bindings/bindings/pure_arm_compute/CMakeLists.txt
tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.cc [new file with mode: 0644]
tools/nnapi_bindings/bindings/pure_arm_compute/src/internal/Model.h [new file with mode: 0644]
tools/nnapi_bindings/bindings/pure_arm_compute/src/model.cc
tools/nnapi_bindings/bindings/pure_arm_compute/src/model.h

index 87fdc1b..541d986 100644 (file)
@@ -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 (file)
index 0000000..bd1cbb0
--- /dev/null
@@ -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 (file)
index 0000000..dad3ef3
--- /dev/null
@@ -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 <vector>
+#include <cstdint>
+
+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<uint32_t> _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 <memory>
+
+namespace internal
+{
+namespace tflite
+{
+namespace operand
+{
+
+class Set
+{
+public:
+  Index append(const Shape &);
+
+public:
+  const Object &at(const Index &) const;
+
+private:
+  std::vector<std::unique_ptr<Object>> _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__
index 3b064f1..7d276c4 100644 (file)
@@ -1,22 +1,45 @@
 #include <nnapi.h>
 
+#include <cassert>
+
 #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
+}
index 8da8f74..3798b90 100644 (file)
@@ -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<const internal::tflite::Model> &model)
+  {
+    model = _model;
+  }
+
+private:
+  std::shared_ptr<internal::tflite::Model> _model;
 };
 
 #endif // __MODEL_H__