From 512f8d05aa5afe36c769b6406a840d01183cb94d Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Wed, 19 Sep 2018 17:47:58 +0900 Subject: [PATCH] [coco] Introduce GenericFeatureLayout (#1565) This commit introduces GenericFeatureLayout which is extracted from FeatureObject. GenericFeatureLayout includes code related with mapping from index to each Bag element. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/FeatureObject.h | 6 ++-- .../core/include/coco/IR/GenericFeatureLayout.h | 38 ++++++++++++++++++++++ contrib/coco/core/src/IR/FeatureObject.cpp | 18 +++++----- contrib/coco/core/src/IR/GenericFeatureLayout.cpp | 38 ++++++++++++++++++++++ 4 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 contrib/coco/core/include/coco/IR/GenericFeatureLayout.h create mode 100644 contrib/coco/core/src/IR/GenericFeatureLayout.cpp diff --git a/contrib/coco/core/include/coco/IR/FeatureObject.h b/contrib/coco/core/include/coco/IR/FeatureObject.h index 911a002..b0384e1 100644 --- a/contrib/coco/core/include/coco/IR/FeatureObject.h +++ b/contrib/coco/core/include/coco/IR/FeatureObject.h @@ -3,6 +3,7 @@ #include "coco/IR/Object.h" #include "coco/IR/FeatureObjectInfo.h" +#include "coco/IR/GenericFeatureLayout.h" #include "coco/IR/ElemID.h" #include @@ -36,7 +37,7 @@ public: public: ElemID &at(uint32_t ch, uint32_t row, uint32_t col); - const ElemID &at(uint32_t ch, uint32_t row, uint32_t col) const; + ElemID at(uint32_t ch, uint32_t row, uint32_t col) const; public: void reorder(const nncc::core::ADT::feature::Layout &l); @@ -44,8 +45,7 @@ public: private: std::unique_ptr _info; - - std::vector _map; + std::unique_ptr _layout; }; } // namespace coco diff --git a/contrib/coco/core/include/coco/IR/GenericFeatureLayout.h b/contrib/coco/core/include/coco/IR/GenericFeatureLayout.h new file mode 100644 index 0000000..4ce8adb --- /dev/null +++ b/contrib/coco/core/include/coco/IR/GenericFeatureLayout.h @@ -0,0 +1,38 @@ +#ifndef __COCO_IR_GENERIC_FEATURE_LAYOUT_H__ +#define __COCO_IR_GENERIC_FEATURE_LAYOUT_H__ + +#include "coco/IR/ElemID.h" + +#include + +#include + +namespace coco +{ + +class GenericFeatureLayout +{ +public: + GenericFeatureLayout(const nncc::core::ADT::feature::Shape &shape); + +public: + uint32_t batch(void) const { return _batch; } + const nncc::core::ADT::feature::Shape &shape(void) const { return _shape; } + + ElemID &at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col); + ElemID at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const; + +private: + uint32_t offset(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const; + +private: + uint32_t _batch; + nncc::core::ADT::feature::Shape _shape; + +private: + std::vector _content; +}; + +} // namespace coco + +#endif // __COCO_IR_GENERIC_FEATURE_LAYOUT_H__ diff --git a/contrib/coco/core/src/IR/FeatureObject.cpp b/contrib/coco/core/src/IR/FeatureObject.cpp index 22445a3..15330ed 100644 --- a/contrib/coco/core/src/IR/FeatureObject.cpp +++ b/contrib/coco/core/src/IR/FeatureObject.cpp @@ -2,20 +2,18 @@ #include "coco/IR/FeatureObjectInfo.h" #include +#include -namespace -{ - -static nncc::core::ADT::feature::CHWLayout l{}; +#include -} // namespace +using nncc::foundation::make_unique; namespace coco { FeatureObject::FeatureObject(std::unique_ptr &&info) : _info(std::move(info)) { - _map.resize(nncc::core::ADT::feature::num_elements(shape())); + _layout = make_unique(_info->shape()); } FeatureObject::~FeatureObject() @@ -29,12 +27,14 @@ const nncc::core::ADT::feature::Shape &FeatureObject::shape(void) const { return ElemID &FeatureObject::at(uint32_t ch, uint32_t row, uint32_t col) { - return _map.at(l.offset(shape(), ch, row, col)); + assert(_layout->batch() == 1); + return _layout->at(0, ch, row, col); } -const ElemID &FeatureObject::at(uint32_t ch, uint32_t row, uint32_t col) const +ElemID FeatureObject::at(uint32_t ch, uint32_t row, uint32_t col) const { - return _map.at(l.offset(shape(), ch, row, col)); + assert(_layout->batch() == 1); + return _layout->at(0, ch, row, col); } void FeatureObject::reorder(const nncc::core::ADT::feature::Layout &l) diff --git a/contrib/coco/core/src/IR/GenericFeatureLayout.cpp b/contrib/coco/core/src/IR/GenericFeatureLayout.cpp new file mode 100644 index 0000000..e784042 --- /dev/null +++ b/contrib/coco/core/src/IR/GenericFeatureLayout.cpp @@ -0,0 +1,38 @@ +#include "coco/IR/GenericFeatureLayout.h" + +#include + +using nncc::core::ADT::feature::num_elements; + +namespace coco +{ + +GenericFeatureLayout::GenericFeatureLayout(const nncc::core::ADT::feature::Shape &shape) + : _batch{1}, _shape{shape} +{ + _content.resize(_batch * num_elements(_shape)); +} + +uint32_t GenericFeatureLayout::offset(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const +{ + static nncc::core::ADT::feature::CHWLayout l{}; + + uint32_t res = 0; + + res += b * num_elements(_shape); + res += l.offset(shape(), ch, row, col); + + return res; +} + +ElemID &GenericFeatureLayout::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) +{ + return _content.at(offset(b, ch, row, col)); +} + +ElemID GenericFeatureLayout::at(uint32_t b, uint32_t ch, uint32_t row, uint32_t col) const +{ + return _content.at(offset(b, ch, row, col)); +} + +} // namespace coco -- 2.7.4