From 15c9f6213b39c93cd69f2f15de4982d689234cb6 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: Thu, 31 May 2018 08:17:48 +0900 Subject: [PATCH] [core.ADT] Add tensor::Layout interface (#244) This commit adds tensor::Layout interface, and tensor::LexicalLayout class which implements Layout interfaces. Layout interface provides 'offset' method which returns an offset of each element in flat array, and LexicalLayout assigns this offset following lexicographical order. Signed-off-by: Jonghyun Park --- libs/core/include/nncc/core/ADT/tensor/Layout.h | 28 ++++++++++++++++ .../include/nncc/core/ADT/tensor/LexicalLayout.h | 27 +++++++++++++++ libs/core/src/ADT/tensor/LexicalLayout.cpp | 35 ++++++++++++++++++++ libs/core/src/ADT/tensor/LexicalLayout.test.cpp | 38 ++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 libs/core/include/nncc/core/ADT/tensor/Layout.h create mode 100644 libs/core/include/nncc/core/ADT/tensor/LexicalLayout.h create mode 100644 libs/core/src/ADT/tensor/LexicalLayout.cpp create mode 100644 libs/core/src/ADT/tensor/LexicalLayout.test.cpp diff --git a/libs/core/include/nncc/core/ADT/tensor/Layout.h b/libs/core/include/nncc/core/ADT/tensor/Layout.h new file mode 100644 index 0000000..68fc067 --- /dev/null +++ b/libs/core/include/nncc/core/ADT/tensor/Layout.h @@ -0,0 +1,28 @@ +#ifndef __NNCC_CORE_ADT_TENSOR_LAYOUT_H__ +#define __NNCC_CORE_ADT_TENSOR_LAYOUT_H__ + +#include "nncc/core/ADT/tensor/Shape.h" +#include "nncc/core/ADT/tensor/Index.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +struct Layout +{ + virtual ~Layout() = default; + + virtual uint32_t offset(const Shape &, const Index &) const = 0; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_LAYOUT_H__ diff --git a/libs/core/include/nncc/core/ADT/tensor/LexicalLayout.h b/libs/core/include/nncc/core/ADT/tensor/LexicalLayout.h new file mode 100644 index 0000000..8012b68 --- /dev/null +++ b/libs/core/include/nncc/core/ADT/tensor/LexicalLayout.h @@ -0,0 +1,27 @@ +#ifndef __NNCC_CORE_ADT_TENSOR_LEXICAL_LAYOUT_H__ +#define __NNCC_CORE_ADT_TENSOR_LEXICAL_LAYOUT_H__ + +#include "nncc/core/ADT/tensor/Layout.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +struct LexicalLayout final : public Layout +{ + ~LexicalLayout() = default; + + uint32_t offset(const Shape &, const Index &) const override; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_LEXICAL_LAYOUT_H__ diff --git a/libs/core/src/ADT/tensor/LexicalLayout.cpp b/libs/core/src/ADT/tensor/LexicalLayout.cpp new file mode 100644 index 0000000..5db8661 --- /dev/null +++ b/libs/core/src/ADT/tensor/LexicalLayout.cpp @@ -0,0 +1,35 @@ +#include "nncc/core/ADT/tensor/LexicalLayout.h" + +#include + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +uint32_t LexicalLayout::offset(const Shape &shape, const Index &index) const +{ + assert(shape.rank() > 0); + assert(shape.rank() == index.rank()); + + const uint32_t rank = shape.rank(); + + uint32_t res = index.at(0); + + for (uint32_t axis = 1; axis < rank; ++axis) + { + res *= shape.dim(axis); + res += index.at(axis); + } + + return res; +} + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc diff --git a/libs/core/src/ADT/tensor/LexicalLayout.test.cpp b/libs/core/src/ADT/tensor/LexicalLayout.test.cpp new file mode 100644 index 0000000..af7c522 --- /dev/null +++ b/libs/core/src/ADT/tensor/LexicalLayout.test.cpp @@ -0,0 +1,38 @@ +#include "nncc/core/ADT/tensor/LexicalLayout.h" + +#include + +#include + +TEST(ADT_TENSOR_LEXICAL_LAYOUT, last) +{ + const nncc::core::ADT::tensor::Shape shape{4, 3, 6}; + const nncc::core::ADT::tensor::Index curr{1, 1, 1}; + const nncc::core::ADT::tensor::Index next{1, 1, 2}; + + const nncc::core::ADT::tensor::LexicalLayout l; + + ASSERT_EQ(l.offset(shape, curr) + 1, l.offset(shape, next)); +} + +TEST(ADT_TENSOR_LEXICAL_LAYOUT, lexical_middle) +{ + const nncc::core::ADT::tensor::Shape shape{4, 3, 6}; + const nncc::core::ADT::tensor::Index curr{1, 1, 1}; + const nncc::core::ADT::tensor::Index next{1, 2, 1}; + + const nncc::core::ADT::tensor::LexicalLayout l; + + ASSERT_EQ(l.offset(shape, curr) + 6, l.offset(shape, next)); +} + +TEST(ADT_TENSOR_LEXICAL_LAYOUT, lexical_first) +{ + const nncc::core::ADT::tensor::Shape shape{4, 3, 6}; + const nncc::core::ADT::tensor::Index curr{1, 1, 1}; + const nncc::core::ADT::tensor::Index next{2, 1, 1}; + + const nncc::core::ADT::tensor::LexicalLayout l; + + ASSERT_EQ(l.offset(shape, curr) + 6 * 3, l.offset(shape, next)); +} -- 2.7.4