[coco] Introduce GenericKernelLayout (#1688)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 1 Oct 2018 06:29:14 +0000 (15:29 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 1 Oct 2018 06:29:14 +0000 (15:29 +0900)
This commit introduces GenericKernelLayout class which corresponds to
GenericFeatureLayout class for feature objects.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/GenericKernelLayout.h [new file with mode: 0644]
contrib/coco/core/include/coco/IR/KernelObject.h
contrib/coco/core/src/IR/GenericKernelLayout.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/KernelObject.cpp

diff --git a/contrib/coco/core/include/coco/IR/GenericKernelLayout.h b/contrib/coco/core/include/coco/IR/GenericKernelLayout.h
new file mode 100644 (file)
index 0000000..14b6d4d
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __COCO_IR_GENERIC_KERNEL_LAYOUT_H__
+#define __COCO_IR_GENERIC_KERNEL_LAYOUT_H__
+
+#include "coco/IR/ElemID.h"
+
+#include <nncc/core/ADT/kernel/Layout.h>
+
+#include <vector>
+
+namespace coco
+{
+
+class GenericKernelLayout
+{
+public:
+  GenericKernelLayout(const nncc::core::ADT::kernel::Shape &shape);
+
+public:
+  const nncc::core::ADT::kernel::Shape &shape(void) const { return _shape; }
+
+  ElemID &at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col);
+  ElemID at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const;
+
+  void reorder(const nncc::core::ADT::kernel::Layout &l);
+  template <typename LayoutImpl> void reorder(void) { reorder(LayoutImpl{}); }
+
+private:
+  nncc::core::ADT::kernel::Shape _shape;
+
+private:
+  std::vector<ElemID> _content;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_GENERIC_KERNEL_LAYOUT_H__
index 2113bff..6dd20c5 100644 (file)
 #define __COCO_IR_KERNEL_OBJECT_H__
 
 #include "coco/IR/Object.h"
+#include "coco/IR/GenericKernelLayout.h"
 #include "coco/IR/ElemID.h"
 
 #include <nncc/core/ADT/kernel/Shape.h>
 #include <nncc/core/ADT/kernel/Layout.h>
 
-#include <vector>
-
 namespace coco
 {
 
@@ -50,16 +49,22 @@ public:
   const nncc::core::ADT::kernel::Shape &shape(void) const;
 
 public:
+  // Deprecated
   ElemID &at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col);
-  const ElemID &at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const;
+  ElemID at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const;
 
 public:
+  // Deprecated
   void reorder(const nncc::core::ADT::kernel::Layout &l);
+  // Deprecated
   template <typename LayoutImpl> void reorder(void) { reorder(LayoutImpl{}); }
 
+public:
+  const GenericKernelLayout *layout(void) const { return _layout.get(); }
+  void layout(std::unique_ptr<GenericKernelLayout> &&l) { _layout = std::move(l); }
+
 private:
-  nncc::core::ADT::kernel::Shape _shape;
-  std::vector<ElemID> _map;
+  std::unique_ptr<GenericKernelLayout> _layout;
 };
 
 } // namespace coco
diff --git a/contrib/coco/core/src/IR/GenericKernelLayout.cpp b/contrib/coco/core/src/IR/GenericKernelLayout.cpp
new file mode 100644 (file)
index 0000000..eca3ceb
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "coco/IR/GenericKernelLayout.h"
+
+#include <nncc/core/ADT/kernel/NCHWLayout.h>
+
+#include <cassert>
+
+using nncc::core::ADT::kernel::num_elements;
+
+namespace
+{
+
+nncc::core::ADT::kernel::NCHWLayout l;
+
+} // namespace
+
+namespace coco
+{
+
+GenericKernelLayout::GenericKernelLayout(const nncc::core::ADT::kernel::Shape &shape)
+    : _shape{shape}
+{
+  _content.resize(num_elements(_shape));
+}
+
+ElemID &GenericKernelLayout::at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col)
+{
+  return _content.at(l.offset(_shape, n, ch, row, col));
+}
+
+ElemID GenericKernelLayout::at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const
+{
+  return _content.at(l.offset(_shape, n, ch, row, col));
+}
+
+void GenericKernelLayout::reorder(const nncc::core::ADT::kernel::Layout &l)
+{
+  for (uint32_t n = 0; n < shape().count(); ++n)
+  {
+    for (uint32_t ch = 0; ch < shape().depth(); ++ch)
+    {
+      for (uint32_t row = 0; row < shape().height(); ++row)
+      {
+        for (uint32_t col = 0; col < shape().width(); ++col)
+        {
+          at(n, ch, row, col) = ElemID{l.offset(shape(), n, ch, row, col)};
+        }
+      }
+    }
+  }
+}
+
+} // namespace coco
index a24c903..d5d4913 100644 (file)
 #include "coco/IR/KernelObject.h"
 
 #include <nncc/core/ADT/kernel/NCHWLayout.h>
+#include <nncc/foundation/Memory.h>
 
-namespace
-{
-
-static nncc::core::ADT::kernel::NCHWLayout l{};
-
-} // namespace
+using nncc::foundation::make_unique;
 
 namespace coco
 {
 
-KernelObject::KernelObject(const nncc::core::ADT::kernel::Shape &shape) : _shape{shape}
+KernelObject::KernelObject(const nncc::core::ADT::kernel::Shape &shape)
 {
-  _map.resize(nncc::core::ADT::kernel::num_elements(shape));
+  _layout = make_unique<GenericKernelLayout>(shape);
 }
 
 KernelObject::~KernelObject()
@@ -38,33 +34,18 @@ KernelObject::~KernelObject()
   // DO NOTHING
 }
 
-const nncc::core::ADT::kernel::Shape &KernelObject::shape(void) const { return _shape; }
+const nncc::core::ADT::kernel::Shape &KernelObject::shape(void) const { return _layout->shape(); }
 
 ElemID &KernelObject::at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col)
 {
-  return _map.at(l.offset(shape(), n, ch, row, col));
+  return _layout->at(n, ch, row, col);
 }
 
-const ElemID &KernelObject::at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const
+ElemID KernelObject::at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const
 {
-  return _map.at(l.offset(shape(), n, ch, row, col));
+  return _layout->at(n, ch, row, col);
 }
 
-void KernelObject::reorder(const nncc::core::ADT::kernel::Layout &l)
-{
-  for (uint32_t n = 0; n < shape().count(); ++n)
-  {
-    for (uint32_t ch = 0; ch < shape().depth(); ++ch)
-    {
-      for (uint32_t row = 0; row < shape().height(); ++row)
-      {
-        for (uint32_t col = 0; col < shape().width(); ++col)
-        {
-          at(n, ch, row, col) = ElemID{l.offset(shape(), n, ch, row, col)};
-        }
-      }
-    }
-  }
-}
+void KernelObject::reorder(const nncc::core::ADT::kernel::Layout &l) { _layout->reorder(l); }
 
 } // namespace coco