From 67c0172cb7ede1a970f844b26a8bc4936a57d75b 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/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Wed, 27 Jun 2018 14:20:33 +0900 Subject: [PATCH] [core.ADT] Copyable/movable kernel layout (#385) This commit revises kernel layout implementations similarly as those of tensor layout. Signed-off-by: Jonghyun Park --- libs/core/include/nncc/core/ADT/kernel/Layout.h | 18 +++++++--- .../core/include/nncc/core/ADT/kernel/NCHWLayout.h | 5 +-- libs/core/src/ADT/kernel/Layout.cpp | 22 ++++++++++++ libs/core/src/ADT/kernel/Layout.test.cpp | 40 ++++++++++++++++++++++ libs/core/src/ADT/kernel/NCHWLayout.cpp | 12 +++++-- 5 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 libs/core/src/ADT/kernel/Layout.cpp create mode 100644 libs/core/src/ADT/kernel/Layout.test.cpp diff --git a/libs/core/include/nncc/core/ADT/kernel/Layout.h b/libs/core/include/nncc/core/ADT/kernel/Layout.h index dee8051..14f384b 100644 --- a/libs/core/include/nncc/core/ADT/kernel/Layout.h +++ b/libs/core/include/nncc/core/ADT/kernel/Layout.h @@ -12,12 +12,22 @@ namespace ADT namespace kernel { -struct Layout +class Layout { - virtual ~Layout() = default; +public: + using Func = uint32_t (*)(const Shape &, uint32_t n, uint32_t ch, uint32_t row, uint32_t col); - virtual uint32_t offset(const Shape &, uint32_t n, uint32_t ch, uint32_t row, - uint32_t col) const = 0; +public: + Layout(const Func &func); + +public: + uint32_t offset(const Shape &shape, uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const + { + return _func(shape, n, ch, row, col); + } + +private: + Func _func; }; } // namespace kernel diff --git a/libs/core/include/nncc/core/ADT/kernel/NCHWLayout.h b/libs/core/include/nncc/core/ADT/kernel/NCHWLayout.h index 84ee9c8..7d17ed1 100644 --- a/libs/core/include/nncc/core/ADT/kernel/NCHWLayout.h +++ b/libs/core/include/nncc/core/ADT/kernel/NCHWLayout.h @@ -14,10 +14,7 @@ namespace kernel struct NCHWLayout final : public Layout { - ~NCHWLayout() = default; - - uint32_t offset(const Shape &, uint32_t n, uint32_t ch, uint32_t row, - uint32_t col) const override; + NCHWLayout(); }; } // namespace kernel diff --git a/libs/core/src/ADT/kernel/Layout.cpp b/libs/core/src/ADT/kernel/Layout.cpp new file mode 100644 index 0000000..b6465d5 --- /dev/null +++ b/libs/core/src/ADT/kernel/Layout.cpp @@ -0,0 +1,22 @@ +#include "nncc/core/ADT/kernel/Layout.h" + +#include + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace kernel +{ + +Layout::Layout(const Func &func) : _func{func} +{ + // DO NOTHING +} + +} // namespace kernel +} // namespace ADT +} // namespace core +} // namespace nncc diff --git a/libs/core/src/ADT/kernel/Layout.test.cpp b/libs/core/src/ADT/kernel/Layout.test.cpp new file mode 100644 index 0000000..7e788c0 --- /dev/null +++ b/libs/core/src/ADT/kernel/Layout.test.cpp @@ -0,0 +1,40 @@ +#include "nncc/core/ADT/kernel/Layout.h" + +#include + +using nncc::core::ADT::kernel::Shape; +using nncc::core::ADT::kernel::Layout; + +static uint32_t offset_0(const Shape &, uint32_t, uint32_t, uint32_t, uint32_t) { return 0; } +static uint32_t offset_1(const Shape &, uint32_t, uint32_t, uint32_t, uint32_t) { return 1; } + +TEST(ADT_FEATURE_LAYOUT, ctor) +{ + Layout l{offset_0}; + + ASSERT_EQ(l.offset(Shape{4, 3, 6, 5}, 1, 1, 1, 1), 0); +} + +TEST(ADT_FEATURE_LAYOUT, copy) +{ + Layout orig{offset_0}; + Layout copy{offset_1}; + + ASSERT_EQ(copy.offset(Shape{4, 3, 6, 5}, 1, 1, 1, 1), 1); + + copy = orig; + + ASSERT_EQ(copy.offset(Shape{4, 3, 6, 5}, 1, 1, 1, 1), 0); +} + +TEST(ADT_FEATURE_LAYOUT, move) +{ + Layout orig{offset_0}; + Layout move{offset_1}; + + ASSERT_EQ(move.offset(Shape{4, 3, 6, 5}, 1, 1, 1, 1), 1); + + move = std::move(orig); + + ASSERT_EQ(move.offset(Shape{4, 3, 6, 5}, 1, 1, 1, 1), 0); +} diff --git a/libs/core/src/ADT/kernel/NCHWLayout.cpp b/libs/core/src/ADT/kernel/NCHWLayout.cpp index 8db04d5..e6a9398 100644 --- a/libs/core/src/ADT/kernel/NCHWLayout.cpp +++ b/libs/core/src/ADT/kernel/NCHWLayout.cpp @@ -1,5 +1,12 @@ #include "nncc/core/ADT/kernel/NCHWLayout.h" +using nncc::core::ADT::kernel::Shape; + +static uint32_t NCHW_offset(const Shape &shape, uint32_t n, uint32_t ch, uint32_t row, uint32_t col) +{ + return (((n * shape.depth() + ch) * shape.height() + row) * shape.width() + col); +} + namespace nncc { namespace core @@ -9,10 +16,9 @@ namespace ADT namespace kernel { -uint32_t NCHWLayout::offset(const Shape &shape, uint32_t n, uint32_t ch, uint32_t row, - uint32_t col) const +NCHWLayout::NCHWLayout() : Layout{NCHW_offset} { - return (((n * shape.depth() + ch) * shape.height() + row) * shape.width() + col); + // DO NOTHING } } // namespace kernel -- 2.7.4