From b904b90d4f4ada35ad295b07c7f2a1446ab6e77d Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=B2=9C=EA=B5=90/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 29 Apr 2019 11:22:13 +0900 Subject: [PATCH] [locomotiv] Internal implementation of 'NodeData' (#3374) Interal 'NodeDataImpl' implements public API of 'NodeData'. This commit also introduces test framework and test for NodeData. Signed-off-by: Cheongyo Bahk --- contrib/locomotiv/CMakeLists.txt | 14 +++++++++ contrib/locomotiv/src/NodeData.cpp | 19 +++++------- contrib/locomotiv/src/NodeData.test.cpp | 54 +++++++++++++++++++++++++++++++++ contrib/locomotiv/src/NodeDataImpl.cpp | 38 +++++++++++++++++++++++ contrib/locomotiv/src/NodeDataImpl.h | 54 +++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 contrib/locomotiv/src/NodeData.test.cpp create mode 100644 contrib/locomotiv/src/NodeDataImpl.cpp create mode 100644 contrib/locomotiv/src/NodeDataImpl.h diff --git a/contrib/locomotiv/CMakeLists.txt b/contrib/locomotiv/CMakeLists.txt index b508ce9..27afa0d 100644 --- a/contrib/locomotiv/CMakeLists.txt +++ b/contrib/locomotiv/CMakeLists.txt @@ -1,4 +1,6 @@ file(GLOB_RECURSE SOURCES "src/*.cpp") +file(GLOB_RECURSE TESTS "src/*.test.cpp") +list(REMOVE_ITEM SOURCES ${TESTS}) add_library(locomotiv SHARED ${SOURCES}) target_include_directories(locomotiv PUBLIC include) @@ -9,3 +11,15 @@ target_link_libraries(locomotiv PUBLIC angkor) # NOTE This will enable strict compilation (warnings as error). # Please refer to the top-level CMakeLists.txt for details target_link_libraries(locomotiv PRIVATE nncc_common) + +if(NOT ENABLE_TEST) + return() +endif(NOT ENABLE_TEST) + +# Google Test is mandatory for internal testing +nncc_find_package(GTest REQUIRED) + +GTest_AddTest(locomotiv_test ${TESTS}) +target_link_libraries(locomotiv_test locomotiv) + +add_test(locomotiv_test locomotiv_test) diff --git a/contrib/locomotiv/src/NodeData.cpp b/contrib/locomotiv/src/NodeData.cpp index 38979a4..f90b98d 100644 --- a/contrib/locomotiv/src/NodeData.cpp +++ b/contrib/locomotiv/src/NodeData.cpp @@ -15,22 +15,19 @@ */ #include "locomotiv/NodeData.h" +#include "NodeDataImpl.h" -namespace +namespace locomotiv { -class NodeDataImpl final : public locomotiv::NodeData, public loco::NodeAnnotation +template <> std::unique_ptr make_data(NodeData::Buffer &buf) { - // To be filled -}; - -} // namespace + return std::unique_ptr(new NodeDataImpl(buf)); +} -namespace locomotiv +template <> std::unique_ptr make_data(NodeData::Buffer &buf) { - -template <> std::unique_ptr make_data(NodeData::Buffer &buffer); - -template <> std::unique_ptr make_data(NodeData::Buffer &buffer); + return std::unique_ptr(new NodeDataImpl(buf)); +} } // namespace locomotiv diff --git a/contrib/locomotiv/src/NodeData.test.cpp b/contrib/locomotiv/src/NodeData.test.cpp new file mode 100644 index 0000000..19e9bfe --- /dev/null +++ b/contrib/locomotiv/src/NodeData.test.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019 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 "locomotiv/NodeData.h" + +#include +#include +#include + +#include + +using nncc::core::ADT::tensor::Index; +using nncc::core::ADT::tensor::Shape; +using nncc::core::ADT::tensor::LexicalLayout; +using nncc::core::ADT::tensor::make_buffer; + +TEST(NodeData, as_s32_buffer_wrapper) +{ + const Shape shape{1}; + auto buf = make_buffer(shape); + buf.at(Index{0}) = 42; + + auto data = locomotiv::make_data(buf); + + ASSERT_EQ(data->dtype(), loco::DataType::S32); + ASSERT_EQ(*(data->shape()), shape); + ASSERT_EQ(data->as_s32_bufptr()->at(Index{0}), 42); +} + +TEST(NodeData, as_f32_buffer_wrapper) +{ + const Shape shape{1}; + auto buf = make_buffer(shape); + buf.at(Index{0}) = 3.14; + + auto data = locomotiv::make_data(buf); + + ASSERT_EQ(data->dtype(), loco::DataType::FLOAT32); + ASSERT_EQ(*(data->shape()), shape); + ASSERT_FLOAT_EQ(data->as_f32_bufptr()->at(Index{0}), 3.14); +} diff --git a/contrib/locomotiv/src/NodeDataImpl.cpp b/contrib/locomotiv/src/NodeDataImpl.cpp new file mode 100644 index 0000000..347b701 --- /dev/null +++ b/contrib/locomotiv/src/NodeDataImpl.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 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 "NodeDataImpl.h" + +namespace locomotiv +{ + +template <> NodeDataImpl::NodeDataImpl(Buffer &buf) +{ + _dtype = loco::DataType::S32; + std::unique_ptr> temp(new Buffer(buf)); + _s32 = std::move(temp); + _shape = const_cast(&(_s32->shape())); +} + +template <> NodeDataImpl::NodeDataImpl(Buffer &buf) +{ + _dtype = loco::DataType::FLOAT32; + std::unique_ptr> temp(new Buffer(buf)); + _f32 = std::move(temp); + _shape = const_cast(&(_f32->shape())); +} + +} // namespace locomotiv diff --git a/contrib/locomotiv/src/NodeDataImpl.h b/contrib/locomotiv/src/NodeDataImpl.h new file mode 100644 index 0000000..f5ef471 --- /dev/null +++ b/contrib/locomotiv/src/NodeDataImpl.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019 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 _LOCOMOTIV_NODEDATAIMPL_H_ +#define _LOCOMOTIV_NODEDATAIMPL_H_ + +#include "locomotiv/NodeData.h" + +namespace locomotiv +{ + +class NodeDataImpl final : public NodeData, public loco::NodeAnnotation +{ +public: + template using Buffer = nncc::core::ADT::tensor::Buffer; + using Shape = nncc::core::ADT::tensor::Shape; + + template NodeDataImpl(Buffer
&buf); + + const loco::DataType &dtype() const override { return _dtype; } + + const Shape *shape() const override { return _shape; } + + const Buffer *as_s32_bufptr() const override { return _s32.get(); } + + const Buffer *as_f32_bufptr() const override { return _f32.get(); } + +private: + loco::DataType _dtype = loco::DataType::Unknown; + Shape *_shape = nullptr; + std::unique_ptr> _s32 = nullptr; + std::unique_ptr> _f32 = nullptr; +}; + +void annot_data(loco::Node *node, std::unique_ptr data); + +const NodeData *annot_data(loco::Node *node); + +} // namespace locomotiv + +#endif // _LOCOMOTIV_NODEDATAIMPL_H_ -- 2.7.4