[coco] Introduce KernelLayouts module (#1729)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 2 Oct 2018 10:17:45 +0000 (19:17 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 2 Oct 2018 10:17:45 +0000 (19:17 +0900)
This commit introduces KernelLayouts module which provides a set of
pre-defined KernelLayout classes.

The initial implementation includes includes only NCHW layout.

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

diff --git a/contrib/coco/core/include/coco/IR/KernelLayouts.h b/contrib/coco/core/include/coco/IR/KernelLayouts.h
new file mode 100644 (file)
index 0000000..66ffb40
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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_KERNEL_LAYOUTS_H__
+#define __COCO_IR_KERNEL_LAYOUTS_H__
+
+#include "coco/IR/KernelLayout.h"
+
+#include <memory>
+
+namespace coco
+{
+namespace KernelLayouts
+{
+
+/**
+ * @brief NCHW Kernel Layout
+ */
+class NCHW final : public KernelLayout
+{
+private:
+  NCHW(const nncc::core::ADT::kernel::Shape &shape) : _shape{shape}
+  {
+    // DO NOTHING
+  }
+
+public:
+  const nncc::core::ADT::kernel::Shape &shape(void) const override { return _shape; }
+
+  ElemID at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const override;
+
+private:
+  nncc::core::ADT::kernel::Shape _shape;
+
+public:
+  static std::unique_ptr<NCHW> create(const nncc::core::ADT::kernel::Shape &shape);
+};
+
+} // namespace KernelLayouts
+} // namespace coco
+
+#endif // __COCO_IR_KERNEL_LAYOUTS_H__
diff --git a/contrib/coco/core/src/IR/KernelLayouts.cpp b/contrib/coco/core/src/IR/KernelLayouts.cpp
new file mode 100644 (file)
index 0000000..c103966
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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/KernelLayouts.h"
+
+#include <nncc/core/ADT/kernel/NCHWLayout.h>
+
+#include <cassert>
+
+using namespace nncc::core::ADT::kernel;
+
+//
+// NCHW Layout
+//
+namespace coco
+{
+namespace KernelLayouts
+{
+
+ElemID NCHW::at(uint32_t n, uint32_t ch, uint32_t row, uint32_t col) const
+{
+  static NCHWLayout l;
+  return ElemID{l.offset(_shape, n, ch, row, col)};
+}
+
+std::unique_ptr<NCHW> NCHW::create(const nncc::core::ADT::kernel::Shape &shape)
+{
+  // NOTE It is impossible to use make_unique here as the constructor is private
+  return std::unique_ptr<NCHW>{new NCHW{shape}};
+}
+
+} // namespace KernelLayouts
+} // namespace coco