[core.ADT] Copyable/movable kernel layout (#385)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 27 Jun 2018 05:20:33 +0000 (14:20 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 27 Jun 2018 05:20:33 +0000 (14:20 +0900)
This commit revises kernel layout implementations similarly as those of
tensor layout.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/core/include/nncc/core/ADT/kernel/Layout.h
libs/core/include/nncc/core/ADT/kernel/NCHWLayout.h
libs/core/src/ADT/kernel/Layout.cpp [new file with mode: 0644]
libs/core/src/ADT/kernel/Layout.test.cpp [new file with mode: 0644]
libs/core/src/ADT/kernel/NCHWLayout.cpp

index dee8051..14f384b 100644 (file)
@@ -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
index 84ee9c8..7d17ed1 100644 (file)
@@ -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 (file)
index 0000000..b6465d5
--- /dev/null
@@ -0,0 +1,22 @@
+#include "nncc/core/ADT/kernel/Layout.h"
+
+#include <cassert>
+
+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 (file)
index 0000000..7e788c0
--- /dev/null
@@ -0,0 +1,40 @@
+#include "nncc/core/ADT/kernel/Layout.h"
+
+#include <gtest/gtest.h>
+
+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);
+}
index 8db04d5..e6a9398 100644 (file)
@@ -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