[core.ADT.feature] Introduce Layout interface (#289)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 27 Jun 2018 01:23:11 +0000 (10:23 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 27 Jun 2018 01:23:11 +0000 (10:23 +0900)
This commit introduces 'nncc::core::ADT::feature::Layout' interface
which is almost same as tensor::Layout but specialized for feature map
that convolution uses.

This commit also introduces 'CHWLayout' as an example.

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

diff --git a/libs/core/include/nncc/core/ADT/feature/CHWLayout.h b/libs/core/include/nncc/core/ADT/feature/CHWLayout.h
new file mode 100644 (file)
index 0000000..2f97755
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef __NNCC_CORE_ADT_FEATURE_CHW_LAYOUT_H__
+#define __NNCC_CORE_ADT_FEATURE_CHW_LAYOUT_H__
+
+#include "nncc/core/ADT/feature/Layout.h"
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace feature
+{
+
+struct CHWLayout final : public Layout
+{
+  uint32_t offset(const Shape &, uint32_t ch, uint32_t row, uint32_t col) const override;
+};
+
+} // namespace feature
+} // namespace ADT
+} // namespace core
+} // namespace nncc
+
+#endif // __NNCC_CORE_ADT_FEATURE_CHW_LAYOUT_H__
diff --git a/libs/core/include/nncc/core/ADT/feature/Layout.h b/libs/core/include/nncc/core/ADT/feature/Layout.h
new file mode 100644 (file)
index 0000000..85e3174
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef __NNCC_CORE_ADT_FEATURE_LAYOUT_H__
+#define __NNCC_CORE_ADT_FEATURE_LAYOUT_H__
+
+#include "nncc/core/ADT/feature/Shape.h"
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace feature
+{
+
+struct Layout
+{
+  virtual ~Layout() = default;
+
+  virtual uint32_t offset(const Shape &, uint32_t ch, uint32_t row, uint32_t col) const = 0;
+};
+
+} // namespace feature
+} // namespace ADT
+} // namespace core
+} // namespace nncc
+
+#endif // __NNCC_CORE_ADT_FEATURE_LAYOUT_H__
diff --git a/libs/core/src/ADT/feature/CHWLayout.cpp b/libs/core/src/ADT/feature/CHWLayout.cpp
new file mode 100644 (file)
index 0000000..16ce6c6
--- /dev/null
@@ -0,0 +1,20 @@
+#include "nncc/core/ADT/feature/CHWLayout.h"
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace feature
+{
+
+uint32_t CHWLayout::offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col) const
+{
+  return (ch * shape.height() + row) * shape.width() + col;
+}
+
+} // namespace feature
+} // namespace ADT
+} // namespace core
+} // namespace nncc
diff --git a/libs/core/src/ADT/feature/CHWLayout.test.cpp b/libs/core/src/ADT/feature/CHWLayout.test.cpp
new file mode 100644 (file)
index 0000000..292257f
--- /dev/null
@@ -0,0 +1,29 @@
+#include "nncc/core/ADT/feature/CHWLayout.h"
+
+#include <gtest/gtest.h>
+
+using namespace nncc::core::ADT::feature;
+
+TEST(ADT_FEATURE_CHW_LAYOUT, col_increase)
+{
+  const Shape shape{4, 3, 6};
+  const CHWLayout l;
+
+  ASSERT_EQ(l.offset(shape, 1, 2, 1) + 1, l.offset(shape, 1, 2, 2));
+}
+
+TEST(ADT_FEATURE_CHW_LAYOUT, row_increase)
+{
+  const Shape shape{4, 3, 6};
+  const CHWLayout l;
+
+  ASSERT_EQ(l.offset(shape, 1, 1, 1) + 6, l.offset(shape, 1, 2, 1));
+}
+
+TEST(ADT_FEATURE_CHW_LAYOUT, ch_increase)
+{
+  const Shape shape{4, 3, 6};
+  const CHWLayout l;
+
+  ASSERT_EQ(l.offset(shape, 1, 1, 1) + 6 * 3, l.offset(shape, 2, 1, 1));
+}