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)
# 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)
*/
#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
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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_