Introduce nnfw_support_nnapi (#603)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 12 Apr 2018 01:17:23 +0000 (10:17 +0900)
committer김정현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh0822.kim@samsung.com>
Thu, 12 Apr 2018 01:17:23 +0000 (10:17 +0900)
This commit introduces 'nnfw_support_nnapi' library which provides
various helpers for implementing NNAPI.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
include/support/nnapi/feature/Reader.h [new file with mode: 0644]
include/support/nnapi/feature/Utils.h [new file with mode: 0644]
src/kernel/acl/CMakeLists.txt
src/kernel/acl/src/cl/Conv2D.cpp
src/support/CMakeLists.txt
src/support/nnapi/CMakeLists.txt [new file with mode: 0644]
src/support/nnapi/src/feature/Utils.cpp [new file with mode: 0644]

diff --git a/include/support/nnapi/feature/Reader.h b/include/support/nnapi/feature/Reader.h
new file mode 100644 (file)
index 0000000..7d97c50
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef __NNFW_SUPPORT_NNAPI_FEATURE_READER_H__
+#define __NNFW_SUPPORT_NNAPI_FEATURE_READER_H__
+
+#include "support/nnapi/feature/Utils.h"
+
+#include "util/feature/Shape.h"
+#include "util/feature/Reader.h"
+
+namespace nnfw
+{
+namespace support
+{
+namespace nnapi
+{
+namespace feature
+{
+
+template<typename T> class Reader;
+
+template<> class Reader<float> : public nnfw::util::feature::Reader<float>
+{
+public:
+  Reader(const nnfw::util::feature::Shape &shape, const float *base)
+    : _shape{shape}, _base{base}
+  {
+    // DO NOTHING
+  }
+
+public:
+  float at(uint32_t ch, uint32_t row, uint32_t col) const override
+  {
+    return *(_base + indexOf(_shape, ch, row, col));
+  }
+
+private:
+  nnfw::util::feature::Shape _shape;
+  const float *_base;
+};
+
+} // namespace feature
+} // namespace nnapi
+} // namespace support
+} // namespace nnfw
+
+#endif // __NNFW_SUPPORT_NNAPI_FEATURE_READER_H__
diff --git a/include/support/nnapi/feature/Utils.h b/include/support/nnapi/feature/Utils.h
new file mode 100644 (file)
index 0000000..58932a0
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef __NNFW_SUPPORT_NNAPI_FEATURE_UTILS_H__
+#define __NNFW_SUPPORT_NNAPI_FEATURE_UTILS_H__
+
+#include "util/feature/Shape.h"
+
+#include <cstdint>
+
+namespace nnfw
+{
+namespace support
+{
+namespace nnapi
+{
+namespace feature
+{
+
+uint32_t indexOf(const nnfw::util::feature::Shape &shape, uint32_t ch, uint32_t row, uint32_t col);
+
+} // namespace feature
+} // namespace nnapi
+} // namespace support
+} // namespace nnfw
+
+#endif // __NNFW_SUPPORT_NNAPI_FEATURE_UTILS_H__
index 04a322c..ba0baa6 100644 (file)
@@ -40,6 +40,7 @@ target_include_directories(${LIB_KERNELACL} PUBLIC
                            ${NNFW_ACL_INCLUDES}
                            ${CMAKE_SOURCE_DIR}/include
                            )
+target_link_libraries(${LIB_KERNELACL} nnfw_support_nnapi)
 if (${TARGET_OS} STREQUAL "tizen")
   target_link_libraries(${LIB_KERNELACL} nnfw_util ${NNFW_ACL_LIBS} OpenCL)
 else()
index 4c0736d..ad805b6 100644 (file)
@@ -14,6 +14,8 @@
 #include "util/feature/Reader.h"
 #include "util/feature/TextFormatter.h"
 
+#include "support/nnapi/feature/Reader.h"
+
 namespace nnfw
 {
 namespace support
@@ -23,41 +25,8 @@ namespace nnapi
 namespace feature
 {
 
-template<typename T> class Reader;
-
-template<> class Reader<float> : public nnfw::util::feature::Reader<float>
-{
-public:
-  Reader(const nnfw::util::feature::Shape &shape, const float *base)
-    : _shape{shape}, _base{base}
-  {
-    // DO NOTHING
-  }
-
-public:
-  float at(uint32_t ch, uint32_t row, uint32_t col) const override
-  {
-    return *(_base + getElementOffset(ch, row, col));
-  }
-
-private:
-  uint32_t getElementOffset(uint32_t ch, uint32_t row, uint32_t col) const
-  {
-    uint32_t res = 0;
-
-    // NNAPI assumes that NHWC ordering for feature map
-    res += row * _shape.W * _shape.C;
-    res += col * _shape.C;
-    res += ch;
-
-    return res;
-  }
-
-private:
-  nnfw::util::feature::Shape _shape;
-  const float *_base;
-};
-
+// TODO Extract this function as utility function
+// NOTE It is not a good design to access android::nn::Shape nnfw_support_nnapi lib
 nnfw::util::feature::Shape asFeatureShape(const android::nn::Shape& shape)
 {
   // NNAPI assumes the following ordering:
index 3e0ecbb..c916772 100644 (file)
@@ -1 +1,2 @@
 add_subdirectory(tflite)
+add_subdirectory(nnapi)
diff --git a/src/support/nnapi/CMakeLists.txt b/src/support/nnapi/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d57c691
--- /dev/null
@@ -0,0 +1,5 @@
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+
+add_library(nnfw_support_nnapi ${SOURCES})
+target_include_directories(nnfw_support_nnapi PUBLIC ${CMAKE_SOURCE_DIR}/include)
+target_link_libraries(nnfw_support_nnapi nnfw_util)
diff --git a/src/support/nnapi/src/feature/Utils.cpp b/src/support/nnapi/src/feature/Utils.cpp
new file mode 100644 (file)
index 0000000..f014f4f
--- /dev/null
@@ -0,0 +1,27 @@
+#include "support/nnapi/feature/Utils.h"
+
+namespace nnfw
+{
+namespace support
+{
+namespace nnapi
+{
+namespace feature
+{
+
+uint32_t indexOf(const nnfw::util::feature::Shape &shape, uint32_t ch, uint32_t row, uint32_t col)
+{
+  uint32_t res = 0;
+
+  // NNAPI assumes that NHWC ordering for feature map
+  res += row * shape.W * shape.C;
+  res += col * shape.C;
+  res += ch;
+
+  return res;
+}
+
+} // namespace feature
+} // namespace nnapi
+} // namespace support
+} // namespace nnfw