[core.ADT] Copyable/movable feature layout (#384)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 27 Jun 2018 06:36:03 +0000 (15:36 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 27 Jun 2018 06:36:03 +0000 (15:36 +0900)
This commit revises feature::Layout class similarly as tensor::Layout.

With this commit, one may copy/move layout as if it is a plain value.

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

index 2f97755..2e94f70 100644 (file)
@@ -14,7 +14,7 @@ namespace feature
 
 struct CHWLayout final : public Layout
 {
-  uint32_t offset(const Shape &, uint32_t ch, uint32_t row, uint32_t col) const override;
+  CHWLayout();
 };
 
 } // namespace feature
index 85e3174..0440582 100644 (file)
@@ -12,11 +12,22 @@ namespace ADT
 namespace feature
 {
 
-struct Layout
+class Layout
 {
-  virtual ~Layout() = default;
+public:
+  using Func = uint32_t (*)(const Shape &, uint32_t ch, uint32_t row, uint32_t col);
 
-  virtual uint32_t offset(const Shape &, uint32_t ch, uint32_t row, uint32_t col) const = 0;
+public:
+  explicit Layout(const Func &func);
+
+public:
+  uint32_t offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col) const
+  {
+    return _func(shape, ch, row, col);
+  }
+
+private:
+  Func _func;
 };
 
 } // namespace feature
index 16ce6c6..96f390c 100644 (file)
@@ -1,5 +1,12 @@
 #include "nncc/core/ADT/feature/CHWLayout.h"
 
+using nncc::core::ADT::feature::Shape;
+
+static uint32_t CHW_offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col)
+{
+  return (ch * shape.height() + row) * shape.width() + col;
+}
+
 namespace nncc
 {
 namespace core
@@ -9,9 +16,9 @@ namespace ADT
 namespace feature
 {
 
-uint32_t CHWLayout::offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col) const
+CHWLayout::CHWLayout() : Layout{CHW_offset}
 {
-  return (ch * shape.height() + row) * shape.width() + col;
+  // DO NOTHING
 }
 
 } // namespace feature
diff --git a/libs/core/src/ADT/feature/Layout.cpp b/libs/core/src/ADT/feature/Layout.cpp
new file mode 100644 (file)
index 0000000..bb9edb7
--- /dev/null
@@ -0,0 +1,19 @@
+#include "nncc/core/ADT/feature/Layout.h"
+
+#include <cassert>
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace feature
+{
+
+Layout::Layout(const Func &func) : _func{func} { assert(_func != nullptr); }
+
+} // namespace feature
+} // namespace ADT
+} // namespace core
+} // namespace nncc
diff --git a/libs/core/src/ADT/feature/Layout.test.cpp b/libs/core/src/ADT/feature/Layout.test.cpp
new file mode 100644 (file)
index 0000000..c9d4528
--- /dev/null
@@ -0,0 +1,40 @@
+#include "nncc/core/ADT/feature/Layout.h"
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::feature::Shape;
+using nncc::core::ADT::feature::Layout;
+
+static uint32_t offset_0(const Shape &, uint32_t, uint32_t, uint32_t) { return 0; }
+static uint32_t offset_1(const Shape &, 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}, 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}, 1, 1, 1), 1);
+
+  copy = orig;
+
+  ASSERT_EQ(copy.offset(Shape{4, 3, 6}, 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}, 1, 1, 1), 1);
+
+  move = std::move(orig);
+
+  ASSERT_EQ(move.offset(Shape{4, 3, 6}, 1, 1, 1), 0);
+}