refactor: improve CMakeLists.txt and improve math/
This commit is contained in:
+27
-22
@@ -2,37 +2,42 @@ cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(cenet CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
option(CENET_BUILD_TESTS "Compile cenet tests" OFF)
|
||||
option(CENET_BUILD_EXAMPLES "Compile cenet examples" ON)
|
||||
|
||||
file(GLOB_RECURSE CENET_HEADERS "include/*.hpp")
|
||||
file(GLOB_RECURSE CENET_HEADERS CONFIGURE_DEPENDS "include/*.hpp")
|
||||
add_library(cenet INTERFACE ${CENET_HEADERS})
|
||||
target_include_directories(cenet INTERFACE include/)
|
||||
target_include_directories(cenet INTERFACE include)
|
||||
target_compile_features(cenet INTERFACE cxx_std_17)
|
||||
|
||||
add_executable(xor example/xor.cpp)
|
||||
target_link_libraries(xor PRIVATE cenet)
|
||||
if(CENET_BUILD_EXAMPLES)
|
||||
add_executable(xor example/xor.cpp)
|
||||
target_link_libraries(xor PRIVATE cenet)
|
||||
|
||||
add_executable(mnist example/mnist.cpp)
|
||||
target_link_libraries(mnist PRIVATE cenet)
|
||||
add_executable(mnist example/mnist.cpp)
|
||||
target_link_libraries(mnist PRIVATE cenet)
|
||||
|
||||
add_executable(tokenizer example/tokenizer.cpp)
|
||||
target_link_libraries(tokenizer PRIVATE cenet)
|
||||
add_executable(tokenizer example/tokenizer.cpp)
|
||||
target_link_libraries(tokenizer PRIVATE cenet)
|
||||
|
||||
add_executable(gpt example/gpt.cpp)
|
||||
target_link_libraries(gpt PRIVATE cenet)
|
||||
add_executable(gpt example/gpt.cpp)
|
||||
target_link_libraries(gpt PRIVATE cenet)
|
||||
endif()
|
||||
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
if(CENET_BUILD_TESTS)
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
googletest
|
||||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||
)
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
)
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
|
||||
enable_testing()
|
||||
enable_testing()
|
||||
|
||||
add_executable(math_test test/tensor.cpp)
|
||||
target_link_libraries(math_test GTest::gtest_main cenet)
|
||||
add_executable(math_test test/tensor.cpp)
|
||||
target_link_libraries(math_test GTest::gtest_main cenet)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(math_test)
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(math_test)
|
||||
endif()
|
||||
|
||||
+72
-14
@@ -24,68 +24,126 @@ struct binary_expression;
|
||||
template <typename CRTP>
|
||||
struct ops_provider {
|
||||
template <typename R>
|
||||
binary_expression<CRTP, R, ops::matmul> matmul(R&& rhs) {
|
||||
binary_expression<CRTP&, R, ops::matmul> matmul(R&& rhs) & {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
binary_expression<CRTP, R, ops::matmul> matmul(R&& rhs) const {
|
||||
binary_expression<const CRTP&, R, ops::matmul> matmul(R&& rhs) const& {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
binary_expression<CRTP&&, R, ops::matmul> matmul(R&& rhs) && {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
binary_expression<const CRTP&&, R, ops::matmul> matmul(R&& rhs) const&& {
|
||||
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<CRTP, ops::apply<Func>> apply(Func&& func) {
|
||||
unary_expression<CRTP&, ops::apply<Func>> apply(Func&& func) & {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<CRTP, ops::apply<Func>> apply(Func&& func) const {
|
||||
unary_expression<const CRTP&, ops::apply<Func>> apply(Func&& func) const& {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<CRTP&&, ops::apply<Func>> apply(Func&& func) && {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<const CRTP&&, ops::apply<Func>> apply(Func&& func) const&& {
|
||||
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<CRTP, R, ops::apply<Func>> apply(R&& rhs, Func&& func) {
|
||||
binary_expression<CRTP&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) & {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<CRTP, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const {
|
||||
binary_expression<const CRTP&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const& {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
unary_expression<CRTP, ops::transpose> transpose() {
|
||||
template <typename R, typename Func>
|
||||
binary_expression<CRTP&&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) && {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<const CRTP&&, R, ops::apply<Func>> apply(R&& rhs, Func&& func) const&& {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
unary_expression<CRTP&, ops::transpose> transpose() & {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
unary_expression<CRTP, ops::transpose> transpose() const {
|
||||
unary_expression<const CRTP&, ops::transpose> transpose() const& {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
unary_expression<CRTP&&, ops::transpose> transpose() && {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
unary_expression<const CRTP&&, ops::transpose> transpose() const&& {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<CRTP, ops::reshape<S>> reshape() {
|
||||
unary_expression<CRTP&, ops::reshape<S>> reshape() & {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<CRTP, ops::reshape<S>> reshape() const {
|
||||
unary_expression<const CRTP&, ops::reshape<S>> reshape() const& {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<CRTP&&, ops::reshape<S>> reshape() && {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
unary_expression<const CRTP&&, ops::reshape<S>> reshape() const&& {
|
||||
if (!ops::reshape<S>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
private:
|
||||
decltype(auto) self() & { return reinterpret_cast<CRTP&>(*this); }
|
||||
decltype(auto) self() && { return reinterpret_cast<CRTP&&>(*this); }
|
||||
decltype(auto) self() & { return static_cast<CRTP&>(*this); }
|
||||
decltype(auto) self() && { return static_cast<CRTP&&>(*this); }
|
||||
|
||||
decltype(auto) self() const& { return reinterpret_cast<const CRTP&>(*this); }
|
||||
decltype(auto) self() const&& { return reinterpret_cast<const CRTP&&>(*this); }
|
||||
decltype(auto) self() const& { return static_cast<const CRTP&>(*this); }
|
||||
decltype(auto) self() const&& { return static_cast<const CRTP&&>(*this); }
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -44,8 +44,7 @@ public:
|
||||
template <typename... RtDims,
|
||||
typename = std::enable_if_t<(
|
||||
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||
explicit tensor(RtDims... dimensions)
|
||||
: m_shape(dynamic_shape<Dims...>{}) {
|
||||
explicit tensor(RtDims... dimensions) {
|
||||
set_dimensions(dimensions...);
|
||||
}
|
||||
|
||||
@@ -64,6 +63,7 @@ public:
|
||||
tensor& operator=(tensor&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
m_shape = std::move(other.m_shape);
|
||||
if constexpr (!traits::inline_storage) {
|
||||
m_items = other.m_items;
|
||||
other.m_items = nullptr;
|
||||
@@ -75,31 +75,24 @@ public:
|
||||
|
||||
tensor(const tensor& other)
|
||||
: m_shape(other.m_shape) {
|
||||
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
|
||||
dyn_copy(other.m_items, std::make_index_sequence<sizeof...(Dims)>{});
|
||||
set_dimensions(other.m_shape);
|
||||
dyn_copy(other.m_items);
|
||||
}
|
||||
|
||||
tensor& operator=(const tensor& other) {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
|
||||
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
|
||||
set_dimensions(other.m_shape);
|
||||
dyn_copy(other.m_items);
|
||||
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
// TODO: improve aliasing semantics
|
||||
template <typename E,
|
||||
typename = std::enable_if_t<detail::is_expression_v<E> && !std::is_same_v<std::decay_t<E>, tensor>>>
|
||||
tensor(E&& exprFwd) { // NOLINT
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
if constexpr (!traits::inline_storage) {
|
||||
if constexpr (traits::dynamic) {
|
||||
set_dimensions(exprFwd.shape());
|
||||
} else {
|
||||
m_items = new value_type[traits::size];
|
||||
}
|
||||
}
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
@@ -109,13 +102,7 @@ public:
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator=(E&& exprFwd) {
|
||||
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");
|
||||
|
||||
if constexpr (traits::dynamic) {
|
||||
if (m_shape != exprFwd.shape()) {
|
||||
set_dimensions(exprFwd.shape());
|
||||
}
|
||||
}
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
@@ -167,14 +154,17 @@ public:
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] /= expr;
|
||||
m_items[i] /= expr.coeff(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
view_type view() { return { m_items, m_shape }; }
|
||||
constant_view_type view() const { return { m_items, m_shape }; }
|
||||
view_type view() & { return { m_items, m_shape }; }
|
||||
constant_view_type view() const& { return { m_items, m_shape }; }
|
||||
|
||||
view_type view() && = delete;
|
||||
constant_view_type view() const&& = delete;
|
||||
|
||||
explicit operator view_type() { return view(); }
|
||||
explicit operator constant_view_type() const { return view(); }
|
||||
@@ -190,16 +180,19 @@ public:
|
||||
}
|
||||
public:
|
||||
template <typename... RtDims,
|
||||
typename = std::enable_if_t<(
|
||||
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||
typename = std::enable_if_t<(sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
|
||||
void set_dimensions(RtDims... dimensions) {
|
||||
if (!valid_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...))
|
||||
throw std::runtime_error("invalid dynamic tensor dimensions");
|
||||
throw std::runtime_error("unexpected shape");
|
||||
if constexpr (!traits::inline_storage) {
|
||||
store_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...);
|
||||
|
||||
release();
|
||||
m_items = new value_type[(dimensions * ... * 1)];
|
||||
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
|
||||
} else {
|
||||
store_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...);
|
||||
}
|
||||
}
|
||||
|
||||
template <template <std::size_t...> class Shape, std::size_t... ShapeDims>
|
||||
@@ -246,8 +239,10 @@ private:
|
||||
private:
|
||||
template <typename... RtDims, std::size_t... Is>
|
||||
constexpr void store_dims(std::index_sequence<Is...>, RtDims... dimensions) {
|
||||
if constexpr (!detail::is_static_shape_v<shape_type>) {
|
||||
((m_shape.dimensions[Is] = static_cast<std::size_t>(dimensions)), ...);
|
||||
}
|
||||
}
|
||||
private:
|
||||
template <std::size_t I>
|
||||
constexpr std::size_t dimension() const {
|
||||
@@ -265,10 +260,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t... Is>
|
||||
void dyn_copy(const value_type* src, std::index_sequence<Is...>) {
|
||||
std::memcpy(m_items, src, sizeof(*m_items) * size());
|
||||
}
|
||||
void dyn_copy(const value_type* src) { std::memcpy(m_items, src, sizeof(*m_items) * size()); }
|
||||
|
||||
void release() {
|
||||
if constexpr (!traits::inline_storage) {
|
||||
|
||||
@@ -176,6 +176,11 @@ private:
|
||||
dimensions[I] = first;
|
||||
store_impl<I + 1>(rest...);
|
||||
}
|
||||
|
||||
template <std::size_t I, typename RDim>
|
||||
void store_impl(RDim last) {
|
||||
dimensions[I] = last;
|
||||
}
|
||||
public:
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto score = score_impl<0, Dims...>::value;
|
||||
|
||||
@@ -83,10 +83,10 @@ public:
|
||||
|
||||
void fill(const value_type* source, std::size_t offset = 0, std::size_t length = dynamic_extent) {
|
||||
offset = std::min(offset, size());
|
||||
std::memcpy(m_view + offset, source, std::min(length, size() - offset) * sizeof(*m_view));
|
||||
std::memmove(m_view + offset, source, std::min(length, size() - offset) * sizeof(*m_view));
|
||||
}
|
||||
|
||||
void fill(const tensor_view& view) { std::memcpy(m_view, view.m_view, size() * sizeof(*m_view)); }
|
||||
void fill(const tensor_view& view) { fill(view.m_view, 0, view.size()); }
|
||||
|
||||
template <typename Dist>
|
||||
void fill_random(Dist dist) {
|
||||
|
||||
@@ -21,8 +21,6 @@ public:
|
||||
|
||||
using input_type = typename traits::input_type;
|
||||
using output_type = typename traits::output_type;
|
||||
public:
|
||||
const output_type& get_output() const { return m_output; }
|
||||
public:
|
||||
struct loss {
|
||||
double value;
|
||||
@@ -203,7 +201,6 @@ public:
|
||||
using layer = typename layers_data_gen::template at<I, I, Layers...>::type;
|
||||
private:
|
||||
typename layers_data_gen::type m_layers;
|
||||
output_type m_output;
|
||||
};
|
||||
|
||||
} // namespace cenet
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user