[coco] Introduce BCHW Feature Layout (#1595)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 20 Sep 2018 04:04:02 +0000 (13:04 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 20 Sep 2018 04:04:02 +0000 (13:04 +0900)
This commit introduces BCHW feature layout class as a mean to speed up
model import/transformation phases.

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

diff --git a/contrib/coco/core/include/coco/IR/FeatureLayouts.h b/contrib/coco/core/include/coco/IR/FeatureLayouts.h
new file mode 100644 (file)
index 0000000..cbd6af9
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef __COCO_IR_FEATURE_LAYOUTS_H__
+#define __COCO_IR_FEATURE_LAYOUTS_H__
+
+#include "coco/IR/FeatureLayout.h"
+
+#include <memory>
+
+namespace coco
+{
+namespace FeatureLayouts
+{
+
+/**
+ * @brief BCHW Feature Layout
+ */
+class BCHW final : public FeatureLayout
+{
+private:
+  BCHW(uint32_t batch, const nncc::core::ADT::feature::Shape &shape) : _batch{batch}, _shape{shape}
+  {
+    // DO NOTHING
+  }
+
+public:
+  uint32_t batch(void) const override { return _batch; }
+  const nncc::core::ADT::feature::Shape &shape(void) const override { return _shape; }
+
+  ElemID at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const override;
+
+private:
+  uint32_t _batch;
+  nncc::core::ADT::feature::Shape _shape;
+
+public:
+  static std::unique_ptr<BCHW> create(const nncc::core::ADT::feature::Shape &shape);
+};
+
+} // namespace FeatureLayouts
+} // namespace coco
+
+#endif // __COCO_IR_FEATURE_LAYOUTS_H__
diff --git a/contrib/coco/core/src/IR/FeatureLayouts.cpp b/contrib/coco/core/src/IR/FeatureLayouts.cpp
new file mode 100644 (file)
index 0000000..f3f9f85
--- /dev/null
@@ -0,0 +1,32 @@
+#include "coco/IR/FeatureLayouts.h"
+
+#include <nncc/core/ADT/feature/CHWLayout.h>
+
+using namespace nncc::core::ADT::feature;
+
+//
+// BCHW Layout
+//
+namespace coco
+{
+namespace FeatureLayouts
+{
+
+ElemID BCHW::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const
+{
+  static CHWLayout l;
+
+  uint32_t offset = 0;
+  offset += b * num_elements(_shape);
+  offset += l.offset(_shape, ch, row, col);
+  return ElemID{offset};
+}
+
+std::unique_ptr<BCHW> BCHW::create(const nncc::core::ADT::feature::Shape &shape)
+{
+  // NOTE It is impossible to use make_unique here as the constructor is private
+  return std::unique_ptr<BCHW>{new BCHW{1, shape}};
+}
+
+} // namespace FeatureLayouts
+} // namespace coco