From f5d9d3a271afa6e4cd692191e2b2db7ed7b7d70e 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: Fri, 1 Jun 2018 15:54:25 +0900 Subject: [PATCH] [core] Add Tensor View (#281) This commit introduces 'nncc::core::ADT::tensor::View' class which allows users to interpret a memory region as a tensor with a specified shape and layout. Signed-off-by: Jonghyun Park --- libs/core/CMakeLists.txt | 1 + libs/core/include/nncc/core/ADT/tensor/View.h | 65 +++++++++++++++++++++++++++ libs/core/src/ADT/tensor/View.test.cpp | 63 ++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 libs/core/include/nncc/core/ADT/tensor/View.h create mode 100644 libs/core/src/ADT/tensor/View.test.cpp diff --git a/libs/core/CMakeLists.txt b/libs/core/CMakeLists.txt index 4e18147..4a83c0b 100644 --- a/libs/core/CMakeLists.txt +++ b/libs/core/CMakeLists.txt @@ -6,6 +6,7 @@ list(REMOVE_ITEM SOURCES ${TESTS}) add_nncc_library(nncc_core SHARED ${HEADERS} ${SOURCES}) set_target_properties(nncc_core PROPERTIES LINKER_LANGUAGE CXX) target_include_directories(nncc_core PUBLIC include) +target_link_libraries(nncc_core PUBLIC nncc_foundation) add_nncc_test(nncc_core_test ${TESTS}) nncc_target_link_libraries(nncc_core_test nncc_core) diff --git a/libs/core/include/nncc/core/ADT/tensor/View.h b/libs/core/include/nncc/core/ADT/tensor/View.h new file mode 100644 index 0000000..96ef5ab --- /dev/null +++ b/libs/core/include/nncc/core/ADT/tensor/View.h @@ -0,0 +1,65 @@ +#ifndef __NNCC_CORE_ADT_TENSOR_VIEW_H__ +#define __NNCC_CORE_ADT_TENSOR_VIEW_H__ + +#include "nncc/core/ADT/tensor/Shape.h" +#include "nncc/core/ADT/tensor/Index.h" +#include "nncc/core/ADT/tensor/Reader.h" +#include "nncc/core/ADT/tensor/Accessor.h" +#include "nncc/core/ADT/tensor/Layout.h" + +#include + +#include + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template class View final : public Reader, public Accessor +{ +public: + explicit View(const Shape &shape, std::unique_ptr &&layout, + std::unique_ptr> &®ion) + : _shape{shape}, _layout{std::move(layout)}, _region{std::move(region)} + { + // DO NOTHING + } + +public: + T at(const Index &index) const override + { + return *(_region->base() + _layout->offset(_shape, index)); + } + +public: + T &at(const Index &index) override { return *(_region->base() + _layout->offset(_shape, index)); } + +public: + const Shape &shape(void) const { return _shape; } + +private: + const Shape _shape; + std::unique_ptr _layout; + +private: + std::unique_ptr> _region; +}; + +template +View make_view(const Shape &shape, std::unique_ptr> &®ion) +{ + std::unique_ptr layout(new LayoutImpl{}); + return View{shape, std::move(layout), std::move(region)}; +} + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_VIEW_H__ diff --git a/libs/core/src/ADT/tensor/View.test.cpp b/libs/core/src/ADT/tensor/View.test.cpp new file mode 100644 index 0000000..6b60167 --- /dev/null +++ b/libs/core/src/ADT/tensor/View.test.cpp @@ -0,0 +1,63 @@ +#include "nncc/core/ADT/tensor/View.h" +#include "nncc/core/ADT/tensor/LexicalLayout.h" + +#include +#include + +#include + +using namespace nncc::core::ADT::tensor; + +using nncc::foundation::make_unique; +using nncc::foundation::ExternalRegion; + +TEST(ADT_TENSOR_VIEW, ctor) +{ + const Shape shape{2, 3}; + + int data[2 * 3] = { + 0, + }; + auto region = make_unique>(data, 2 * 3); + auto view = make_view(shape, std::move(region)); + + ASSERT_EQ(view.shape(), shape); +} + +TEST(ADT_TENSOR_VIEW, read) +{ + const Shape shape{2, 3}; + + int data[2 * 3] = { + 0, + }; + auto region = make_unique>(data, 2 * 3); + const auto view = make_view(shape, std::move(region)); + + LexicalLayout layout{}; + + const Index index{1, 2}; + + ASSERT_EQ(data[layout.offset(shape, index)], 0); + data[layout.offset(shape, index)] = 2; + ASSERT_EQ(view.at(index), 2); +} + +TEST(ADT_TENSOR_VIEW, access) +{ + const Shape shape{2, 3}; + + int data[2 * 3] = { + 0, + }; + auto region = make_unique>(data, 2 * 3); + auto view = make_view(shape, std::move(region)); + + LexicalLayout layout{}; + + const Index index{1, 2}; + + ASSERT_EQ(data[layout.offset(shape, index)], 0); + view.at(index) = 4; + ASSERT_EQ(data[layout.offset(shape, index)], 4); +} -- 2.7.4