From 0a4cdc0f9e33391cc28f7f0d66b26da7348b058b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 12 Apr 2018 10:17:23 +0900 Subject: [PATCH] Introduce nnfw_support_nnapi (#603) This commit introduces 'nnfw_support_nnapi' library which provides various helpers for implementing NNAPI. Signed-off-by: Jonghyun Park --- include/support/nnapi/feature/Reader.h | 45 +++++++++++++++++++++++++++++++++ include/support/nnapi/feature/Utils.h | 24 ++++++++++++++++++ src/kernel/acl/CMakeLists.txt | 1 + src/kernel/acl/src/cl/Conv2D.cpp | 39 +++------------------------- src/support/CMakeLists.txt | 1 + src/support/nnapi/CMakeLists.txt | 5 ++++ src/support/nnapi/src/feature/Utils.cpp | 27 ++++++++++++++++++++ 7 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 include/support/nnapi/feature/Reader.h create mode 100644 include/support/nnapi/feature/Utils.h create mode 100644 src/support/nnapi/CMakeLists.txt create mode 100644 src/support/nnapi/src/feature/Utils.cpp diff --git a/include/support/nnapi/feature/Reader.h b/include/support/nnapi/feature/Reader.h new file mode 100644 index 0000000..7d97c50 --- /dev/null +++ b/include/support/nnapi/feature/Reader.h @@ -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 class Reader; + +template<> class Reader : public nnfw::util::feature::Reader +{ +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 index 0000000..58932a0 --- /dev/null +++ b/include/support/nnapi/feature/Utils.h @@ -0,0 +1,24 @@ +#ifndef __NNFW_SUPPORT_NNAPI_FEATURE_UTILS_H__ +#define __NNFW_SUPPORT_NNAPI_FEATURE_UTILS_H__ + +#include "util/feature/Shape.h" + +#include + +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__ diff --git a/src/kernel/acl/CMakeLists.txt b/src/kernel/acl/CMakeLists.txt index 04a322c..ba0baa6 100644 --- a/src/kernel/acl/CMakeLists.txt +++ b/src/kernel/acl/CMakeLists.txt @@ -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() diff --git a/src/kernel/acl/src/cl/Conv2D.cpp b/src/kernel/acl/src/cl/Conv2D.cpp index 4c0736d..ad805b6 100644 --- a/src/kernel/acl/src/cl/Conv2D.cpp +++ b/src/kernel/acl/src/cl/Conv2D.cpp @@ -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 class Reader; - -template<> class Reader : public nnfw::util::feature::Reader -{ -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: diff --git a/src/support/CMakeLists.txt b/src/support/CMakeLists.txt index 3e0ecbb..c916772 100644 --- a/src/support/CMakeLists.txt +++ b/src/support/CMakeLists.txt @@ -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 index 0000000..d57c691 --- /dev/null +++ b/src/support/nnapi/CMakeLists.txt @@ -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 index 0000000..f014f4f --- /dev/null +++ b/src/support/nnapi/src/feature/Utils.cpp @@ -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 -- 2.7.4