[locomotiv] Internal implementation of 'NodeData' (#3374)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Mon, 29 Apr 2019 02:22:13 +0000 (11:22 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 29 Apr 2019 02:22:13 +0000 (11:22 +0900)
Interal 'NodeDataImpl' implements public API of 'NodeData'. This commit
also introduces test framework and test for NodeData.

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
contrib/locomotiv/CMakeLists.txt
contrib/locomotiv/src/NodeData.cpp
contrib/locomotiv/src/NodeData.test.cpp [new file with mode: 0644]
contrib/locomotiv/src/NodeDataImpl.cpp [new file with mode: 0644]
contrib/locomotiv/src/NodeDataImpl.h [new file with mode: 0644]

index b508ce9..27afa0d 100644 (file)
@@ -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)
index 38979a4..f90b98d 100644 (file)
  */
 
 #include "locomotiv/NodeData.h"
+#include "NodeDataImpl.h"
 
-namespace
+namespace locomotiv
 {
 
-class NodeDataImpl final : public locomotiv::NodeData, public loco::NodeAnnotation
+template <> std::unique_ptr<NodeData> make_data(NodeData::Buffer<int32_t> &buf)
 {
-  // To be filled
-};
-
-} // namespace
+  return std::unique_ptr<NodeDataImpl>(new NodeDataImpl(buf));
+}
 
-namespace locomotiv
+template <> std::unique_ptr<NodeData> make_data(NodeData::Buffer<float> &buf)
 {
-
-template <> std::unique_ptr<NodeData> make_data(NodeData::Buffer<int32_t> &buffer);
-
-template <> std::unique_ptr<NodeData> make_data(NodeData::Buffer<float> &buffer);
+  return std::unique_ptr<NodeDataImpl>(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 (file)
index 0000000..19e9bfe
--- /dev/null
@@ -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 <nncc/core/ADT/tensor/Shape.h>
+#include <nncc/core/ADT/tensor/Buffer.h>
+#include <nncc/core/ADT/tensor/LexicalLayout.h>
+
+#include <gtest/gtest.h>
+
+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<int32_t, LexicalLayout>(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<float, LexicalLayout>(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 (file)
index 0000000..347b701
--- /dev/null
@@ -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<int32_t> &buf)
+{
+  _dtype = loco::DataType::S32;
+  std::unique_ptr<Buffer<int32_t>> temp(new Buffer<int32_t>(buf));
+  _s32 = std::move(temp);
+  _shape = const_cast<Shape *>(&(_s32->shape()));
+}
+
+template <> NodeDataImpl::NodeDataImpl(Buffer<float> &buf)
+{
+  _dtype = loco::DataType::FLOAT32;
+  std::unique_ptr<Buffer<float>> temp(new Buffer<float>(buf));
+  _f32 = std::move(temp);
+  _shape = const_cast<Shape *>(&(_f32->shape()));
+}
+
+} // namespace locomotiv
diff --git a/contrib/locomotiv/src/NodeDataImpl.h b/contrib/locomotiv/src/NodeDataImpl.h
new file mode 100644 (file)
index 0000000..f5ef471
--- /dev/null
@@ -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 <typename T> using Buffer = nncc::core::ADT::tensor::Buffer<T>;
+  using Shape = nncc::core::ADT::tensor::Shape;
+
+  template <typename DT> NodeDataImpl(Buffer<DT> &buf);
+
+  const loco::DataType &dtype() const override { return _dtype; }
+
+  const Shape *shape() const override { return _shape; }
+
+  const Buffer<int32_t> *as_s32_bufptr() const override { return _s32.get(); }
+
+  const Buffer<float> *as_f32_bufptr() const override { return _f32.get(); }
+
+private:
+  loco::DataType _dtype = loco::DataType::Unknown;
+  Shape *_shape = nullptr;
+  std::unique_ptr<Buffer<int32_t>> _s32 = nullptr;
+  std::unique_ptr<Buffer<float>> _f32 = nullptr;
+};
+
+void annot_data(loco::Node *node, std::unique_ptr<NodeData> data);
+
+const NodeData *annot_data(loco::Node *node);
+
+} // namespace locomotiv
+
+#endif // _LOCOMOTIV_NODEDATAIMPL_H_