From 2c656e73396ebf0f008d7f95898b7d30e1bf3350 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: Mon, 1 Oct 2018 15:29:14 +0900 Subject: [PATCH] [coco] Introduce GenericKernelLayout (#1688) This commit introduces GenericKernelLayout class which corresponds to GenericFeatureLayout class for feature objects. Signed-off-by: Jonghyun Park --- .../core/include/coco/IR/GenericKernelLayout.h | 52 +++++++++++++++++ contrib/coco/core/include/coco/IR/KernelObject.h | 15 +++-- contrib/coco/core/src/IR/GenericKernelLayout.cpp | 68 ++++++++++++++++++++++ contrib/coco/core/src/IR/KernelObject.cpp | 37 +++--------- 4 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 contrib/coco/core/include/coco/IR/GenericKernelLayout.h create mode 100644 contrib/coco/core/src/IR/GenericKernelLayout.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 index 0000000..14b6d4d --- /dev/null +++ b/contrib/coco/core/include/coco/IR/GenericKernelLayout.h @@ -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 + +#include + +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 void reorder(void) { reorder(LayoutImpl{}); } + +private: + nncc::core::ADT::kernel::Shape _shape; + +private: + std::vector _content; +}; + +} // namespace coco + +#endif // __COCO_IR_GENERIC_KERNEL_LAYOUT_H__ diff --git a/contrib/coco/core/include/coco/IR/KernelObject.h b/contrib/coco/core/include/coco/IR/KernelObject.h index 2113bff..6dd20c5 100644 --- a/contrib/coco/core/include/coco/IR/KernelObject.h +++ b/contrib/coco/core/include/coco/IR/KernelObject.h @@ -18,13 +18,12 @@ #define __COCO_IR_KERNEL_OBJECT_H__ #include "coco/IR/Object.h" +#include "coco/IR/GenericKernelLayout.h" #include "coco/IR/ElemID.h" #include #include -#include - 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 void reorder(void) { reorder(LayoutImpl{}); } +public: + const GenericKernelLayout *layout(void) const { return _layout.get(); } + void layout(std::unique_ptr &&l) { _layout = std::move(l); } + private: - nncc::core::ADT::kernel::Shape _shape; - std::vector _map; + std::unique_ptr _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 index 0000000..eca3ceb --- /dev/null +++ b/contrib/coco/core/src/IR/GenericKernelLayout.cpp @@ -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 + +#include + +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 diff --git a/contrib/coco/core/src/IR/KernelObject.cpp b/contrib/coco/core/src/IR/KernelObject.cpp index a24c903..d5d4913 100644 --- a/contrib/coco/core/src/IR/KernelObject.cpp +++ b/contrib/coco/core/src/IR/KernelObject.cpp @@ -17,20 +17,16 @@ #include "coco/IR/KernelObject.h" #include +#include -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(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 -- 2.7.4