[coco] Declare coco-internal FeatureShape class (#2591)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 10 Dec 2018 08:57:41 +0000 (17:57 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 10 Dec 2018 08:57:41 +0000 (17:57 +0900)
This commit declares coco-internal FeatureShape class with "batch"
method.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/FeatureLayout.h
contrib/coco/core/include/coco/IR/FeatureLayouts.h
contrib/coco/core/include/coco/IR/FeatureShape.h
contrib/coco/core/src/IR/FeatureLayouts.cpp
contrib/coco/core/src/IR/FeatureObject.cpp

index 7fed189..63f02c8 100644 (file)
@@ -39,9 +39,9 @@ struct FeatureLayout
 
   virtual const ID *id(void) const = 0;
 
-  virtual uint32_t batch(void) const = 0;
   virtual const FeatureShape &shape(void) const = 0;
 
+  uint32_t batch(void) const { return shape().batch(); }
   uint32_t depth(void) const { return shape().depth(); }
   uint32_t height(void) const { return shape().height(); }
   uint32_t width(void) const { return shape().width(); }
index 45ff4de..8764f26 100644 (file)
@@ -35,7 +35,7 @@ namespace FeatureLayouts
 class BCHW final : public FeatureLayout
 {
 private:
-  BCHW(uint32_t batch, const FeatureShape &shape) : _batch{batch}, _shape{shape}
+  BCHW(const FeatureShape &shape) : _shape{shape}
   {
     // DO NOTHING
   }
@@ -44,17 +44,15 @@ public:
   static const FeatureLayout::ID *uid(void);
   const FeatureLayout::ID *id(void) const override { return uid(); }
 
-  uint32_t batch(void) const override { return _batch; }
   const FeatureShape &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;
   FeatureShape _shape;
 
 public:
-  static std::unique_ptr<BCHW> create(const FeatureShape &shape);
+  static std::unique_ptr<BCHW> create(const nncc::core::ADT::feature::Shape &shape);
 };
 
 /**
@@ -63,7 +61,7 @@ public:
 class BHWC : public coco::FeatureLayout
 {
 private:
-  BHWC(uint32_t batch, const FeatureShape &shape) : _batch{batch}, _shape{shape}
+  BHWC(const FeatureShape &shape) : _shape{shape}
   {
     // DO NOTHING
   }
@@ -72,17 +70,15 @@ public:
   static const FeatureLayout::ID *uid(void);
   const FeatureLayout::ID *id(void) const override { return uid(); }
 
-  uint32_t batch(void) const override { return _batch; }
   const FeatureShape &shape(void) const override { return _shape; }
 
   coco::ElemID at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const override;
 
 private:
-  uint32_t _batch;
   FeatureShape _shape;
 
 public:
-  static std::unique_ptr<BHWC> create(const FeatureShape &shape);
+  static std::unique_ptr<BHWC> create(const nncc::core::ADT::feature::Shape &shape);
 };
 
 /**
@@ -104,7 +100,7 @@ public:
 class BC : public coco::FeatureLayout
 {
 private:
-  BC(uint32_t batch, const FeatureShape &shape) : _batch{batch}, _shape{shape}
+  BC(const FeatureShape &shape) : _shape{shape}
   {
     // DO NOTHING
   }
@@ -113,17 +109,15 @@ public:
   static const FeatureLayout::ID *uid(void);
   const FeatureLayout::ID *id(void) const override { return uid(); }
 
-  uint32_t batch(void) const override { return _batch; }
   const FeatureShape &shape(void) const override { return _shape; }
 
   coco::ElemID at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const override;
 
 private:
-  uint32_t _batch;
   FeatureShape _shape;
 
 public:
-  static std::unique_ptr<BC> create(const FeatureShape &shape);
+  static std::unique_ptr<BC> create(const nncc::core::ADT::feature::Shape &shape);
 };
 
 /**
@@ -138,7 +132,6 @@ public:
   static const FeatureLayout::ID *uid(void);
   const FeatureLayout::ID *id(void) const override { return uid(); }
 
-  uint32_t batch(void) const override { return _batch; }
   const FeatureShape &shape(void) const override { return _shape; }
 
   ElemID &at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col);
@@ -150,14 +143,13 @@ private:
   uint32_t offset(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const;
 
 private:
-  uint32_t _batch;
   FeatureShape _shape;
 
 private:
   std::vector<ElemID> _content;
 
 public:
-  static std::unique_ptr<Generic> create(const FeatureShape &shape);
+  static std::unique_ptr<Generic> create(const nncc::core::ADT::feature::Shape &shape);
 };
 
 } // namespace FeatureLayouts
index 6ef5dc1..5ea0801 100644 (file)
 namespace coco
 {
 
-using FeatureShape = nncc::core::ADT::feature::Shape;
+/**
+ * @brief The shape of a feature map
+ *
+ * TODO Implement coco's own FeatureShape without "nncc::core::ADT::feature::Shape"
+ */
+class FeatureShape : public nncc::core::ADT::feature::Shape
+{
+public:
+  FeatureShape(uint32_t depth, uint32_t height, uint32_t width)
+      : Shape{depth, height, width}, _batch{1}
+  {
+    // DO NOTHING
+  }
+
+  FeatureShape(const nncc::core::ADT::feature::Shape &shape) : Shape{shape}, _batch{1}
+  {
+    // DO NOTHING
+  }
+
+public:
+  uint32_t batch(void) const { return _batch; }
+
+private:
+  uint32_t _batch;
+};
 
 } // namespace coco
 
index 570621f..fde4b84 100644 (file)
@@ -53,7 +53,7 @@ ElemID BCHW::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const
 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}};
+  return std::unique_ptr<BCHW>{new BCHW{FeatureShape{shape}}};
 }
 
 } // namespace FeatureLayouts
@@ -90,7 +90,7 @@ ElemID BHWC::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const
 std::unique_ptr<BHWC> BHWC::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<BHWC>{new BHWC{1, shape}};
+  return std::unique_ptr<BHWC>{new BHWC{FeatureShape{shape}}};
 }
 
 } // namespace FeatureLayouts
@@ -115,7 +115,7 @@ const FeatureLayout::ID *BC::uid(void)
 
 ElemID BC::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const
 {
-  assert(b < _batch);
+  assert(b < shape().batch());
 
   uint32_t offset = 0;
 
@@ -128,7 +128,7 @@ ElemID BC::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const
 std::unique_ptr<BC> BC::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<BC>{new BC{1, shape}};
+  return std::unique_ptr<BC>{new BC{FeatureShape{shape}}};
 }
 
 } // namespace FeatureLayouts
@@ -142,9 +142,9 @@ namespace coco
 namespace FeatureLayouts
 {
 
-Generic::Generic(const nncc::core::ADT::feature::Shape &shape) : _batch{1}, _shape{shape}
+Generic::Generic(const FeatureShape &shape) : _shape{shape}
 {
-  _content.resize(_batch * num_elements(_shape));
+  _content.resize(_shape.batch() * num_elements(_shape));
 }
 
 const FeatureLayout::ID *Generic::uid(void)
@@ -180,7 +180,7 @@ ElemID Generic::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const
 
 void Generic::reorder(const nncc::core::ADT::feature::Layout &l)
 {
-  assert(_batch == 1);
+  assert(shape().batch() == 1);
 
   for (uint32_t ch = 0; ch < shape().depth(); ++ch)
   {
index 02ce456..46de988 100644 (file)
@@ -26,6 +26,6 @@ FeatureObject::~FeatureObject()
   // DO NOTHING
 }
 
-const nncc::core::ADT::feature::Shape &FeatureObject::shape(void) const { return _layout->shape(); }
+const FeatureShape &FeatureObject::shape(void) const { return _layout->shape(); }
 
 } // namespace coco