From 6f26df3c95673109562349b7471764fe8abfb50f Mon Sep 17 00:00:00 2001 From: Anatoliy Talamanov Date: Sat, 6 Mar 2021 11:00:21 +0300 Subject: [PATCH] Introduce expression API (#4) * Introduce expression API Co-authored-by: Liubov Batanina --- CMakeLists.txt | 6 ------ include/deepworks.hpp | 10 ---------- include/deepworks/deepworks.hpp | 5 +++++ include/deepworks/placeholder.hpp | 24 ++++++++++++++++++++++++ include/deepworks/shape.hpp | 9 +++++++++ include/{ => deepworks}/tensor.hpp | 3 ++- src/CMakeLists.txt | 14 +++++++++++++- src/call.cpp | 20 ++++++++++++++++++++ src/call.hpp | 23 +++++++++++++++++++++++ src/call_impl.hpp | 10 ++++++++++ src/deepworks.cpp | 5 ----- src/layer_info.hpp | 10 ++++++++++ src/placeholder.cpp | 21 +++++++++++++++++++++ src/placeholder_impl.hpp | 15 +++++++++++++++ src/tensor.cpp | 3 ++- tests/unit/foo_tests.cpp | 6 ------ tests/unit/test_tensor.cpp | 4 ++-- 17 files changed, 156 insertions(+), 32 deletions(-) delete mode 100644 include/deepworks.hpp create mode 100644 include/deepworks/deepworks.hpp create mode 100644 include/deepworks/placeholder.hpp create mode 100644 include/deepworks/shape.hpp rename include/{ => deepworks}/tensor.hpp (96%) create mode 100644 src/call.cpp create mode 100644 src/call.hpp create mode 100644 src/call_impl.hpp delete mode 100644 src/deepworks.cpp create mode 100644 src/layer_info.hpp create mode 100644 src/placeholder.cpp create mode 100644 src/placeholder_impl.hpp delete mode 100644 tests/unit/foo_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ddf469d777f5cc..6e95accf6b20fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,12 +10,6 @@ project(deepworks) ######################################## set(CMAKE_CXX_STANDARD 17) -######################################## -# Define include directories -######################################## -set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/include) -include_directories(${COMMON_INCLUDES}) - ######################################## # Define output paths ######################################## diff --git a/include/deepworks.hpp b/include/deepworks.hpp deleted file mode 100644 index b51031a9bbd061..00000000000000 --- a/include/deepworks.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include - -namespace deepworks { - -// FIXME: Just stub to test googletests. -int64_t sum(int a, int b); - -} // namespace deepworks diff --git a/include/deepworks/deepworks.hpp b/include/deepworks/deepworks.hpp new file mode 100644 index 00000000000000..d54883b8b864c1 --- /dev/null +++ b/include/deepworks/deepworks.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include +#include +#include diff --git a/include/deepworks/placeholder.hpp b/include/deepworks/placeholder.hpp new file mode 100644 index 00000000000000..67c682f192fb81 --- /dev/null +++ b/include/deepworks/placeholder.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include // shared_ptr + +#include + +namespace deepworks { + +class Call; +class Placeholder { +public: + explicit Placeholder(const deepworks::Shape& shape); + + const Shape& shape() const; + + // NB: Public for test, but not available for user, + // because Impl isn't exported to public API. + Placeholder(const deepworks::Shape& shape, Call call); + struct Impl; + const Impl& impl() const; + std::shared_ptr m_impl; +}; + +} // namespace deepworks diff --git a/include/deepworks/shape.hpp b/include/deepworks/shape.hpp new file mode 100644 index 00000000000000..118a5f759b6acd --- /dev/null +++ b/include/deepworks/shape.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace deepworks { + +using Shape = std::vector; + +} // namespace deepworks; diff --git a/include/tensor.hpp b/include/deepworks/tensor.hpp similarity index 96% rename from include/tensor.hpp rename to include/deepworks/tensor.hpp index e4c6af323e9858..cc89272d24b1c3 100644 --- a/include/tensor.hpp +++ b/include/deepworks/tensor.hpp @@ -3,9 +3,10 @@ #include #include +#include + namespace deepworks { -using Shape = std::vector; using Strides = std::vector; class Tensor { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa01783a0eff0c..2f505eed270e1a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,19 @@ -file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp) +set(SRC_FILES + ${CMAKE_CURRENT_LIST_DIR}/placeholder.cpp + ${CMAKE_CURRENT_LIST_DIR}/call.cpp + ${CMAKE_CURRENT_LIST_DIR}/tensor.cpp + ) + +set(DeepWorks_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/") add_library(${PROJECT_NAME} SHARED ${SRC_FILES}) +# Set public API +target_include_directories(${PROJECT_NAME} + PUBLIC ${DeepWorks_INCLUDE_DIR} + PRIVATE "${PROJECT_NAME}/src" + ) + if (WITH_EIGEN) find_package(Eigen3 3.3 REQUIRED NO_MODULE) target_link_libraries(${PROJECT_NAME} PRIVATE Eigen3::Eigen) diff --git a/src/call.cpp b/src/call.cpp new file mode 100644 index 00000000000000..d43da81bb09c7b --- /dev/null +++ b/src/call.cpp @@ -0,0 +1,20 @@ +#include + +#include "call.hpp" +#include "call_impl.hpp" + +deepworks::Call::Call(deepworks::LayerInfo&& info) + : m_impl(new Call::Impl{std::move(info)}) { +} + +void deepworks::Call::pass(std::vector&& args) { + m_impl->args = std::move(args); +} + +deepworks::Placeholder deepworks::Call::create(const deepworks::Shape& shape) { + return deepworks::Placeholder{shape, *this}; +}; + +const deepworks::Call::Impl& deepworks::Call::impl() const { + return *m_impl; +} diff --git a/src/call.hpp b/src/call.hpp new file mode 100644 index 00000000000000..652f661c30c406 --- /dev/null +++ b/src/call.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include // shared_ptr + +#include "layer_info.hpp" + +namespace deepworks { + +class Placeholder; +struct Call { + Call() = default; + explicit Call(LayerInfo&&); + + void pass(std::vector&& args); + + Placeholder create(const Shape& shape); + + struct Impl; + const Impl& impl() const; + std::shared_ptr m_impl; +}; + +} // namespace deepworks diff --git a/src/call_impl.hpp b/src/call_impl.hpp new file mode 100644 index 00000000000000..be83186502066b --- /dev/null +++ b/src/call_impl.hpp @@ -0,0 +1,10 @@ +#include "call.hpp" + +namespace deepworks { + +struct Call::Impl { + deepworks::LayerInfo info; + std::vector args; +}; + +} // namespace deepworks diff --git a/src/deepworks.cpp b/src/deepworks.cpp deleted file mode 100644 index ead690d27e81c6..00000000000000 --- a/src/deepworks.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "deepworks.hpp" - -int64_t deepworks::sum(int a, int b) { - return static_cast(a) + b; -} diff --git a/src/layer_info.hpp b/src/layer_info.hpp new file mode 100644 index 00000000000000..ffb10fac81c579 --- /dev/null +++ b/src/layer_info.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace deepworks { + +struct LayerInfo { + std::string name; + std::string type; +}; + +} // namespace deepworks diff --git a/src/placeholder.cpp b/src/placeholder.cpp new file mode 100644 index 00000000000000..91b3ce3d7e2072 --- /dev/null +++ b/src/placeholder.cpp @@ -0,0 +1,21 @@ +#include + +#include "call.hpp" +#include "placeholder_impl.hpp" + +deepworks::Placeholder::Placeholder(const deepworks::Shape& shape, + deepworks::Call call) + : m_impl(new deepworks::Placeholder::Impl{shape, call}) { +} + +deepworks::Placeholder::Placeholder(const deepworks::Shape& shape) + : m_impl(new deepworks::Placeholder::Impl{shape, {}}) { +} + +const deepworks::Shape& deepworks::Placeholder::shape() const { + return m_impl->shape; +} + +const deepworks::Placeholder::Impl& deepworks::Placeholder::impl() const { + return *m_impl; +} diff --git a/src/placeholder_impl.hpp b/src/placeholder_impl.hpp new file mode 100644 index 00000000000000..cabc6d6e7ae90e --- /dev/null +++ b/src/placeholder_impl.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "call.hpp" + +namespace deepworks { + +struct Placeholder::Impl { + deepworks::Shape shape; + // NB: The creator, empty optional if it's input. + std::optional call; +}; + +} // namespace deepworks diff --git a/src/tensor.cpp b/src/tensor.cpp index 939b7ee7a849e5..5d1509a7abc85d 100644 --- a/src/tensor.cpp +++ b/src/tensor.cpp @@ -1,4 +1,5 @@ -#include +#include + #include #include diff --git a/tests/unit/foo_tests.cpp b/tests/unit/foo_tests.cpp deleted file mode 100644 index ffd02d6d9d7cfd..00000000000000 --- a/tests/unit/foo_tests.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -TEST(Sum, SimpleTest) { - EXPECT_EQ(5, deepworks::sum(3, 2)); -} diff --git a/tests/unit/test_tensor.cpp b/tests/unit/test_tensor.cpp index 0489caf34cdd5f..79f2e65d7387f0 100644 --- a/tests/unit/test_tensor.cpp +++ b/tests/unit/test_tensor.cpp @@ -1,6 +1,6 @@ -#include #include -#include + +#include TEST(TensorTest, Allocate) { deepworks::Tensor src_tensor;