Compare commits

...
2 Commits
23 changed files with 301 additions and 167 deletions
+9 -1
View File
@@ -5,10 +5,18 @@ project(cenet CXX)
option(CENET_BUILD_TESTS "Compile cenet tests" OFF) option(CENET_BUILD_TESTS "Compile cenet tests" OFF)
option(CENET_BUILD_EXAMPLES "Compile cenet examples" ON) option(CENET_BUILD_EXAMPLES "Compile cenet examples" ON)
file(GLOB_RECURSE CENET_HEADERS CONFIGURE_DEPENDS "include/*.hpp") file(GLOB_RECURSE TULIP_HEADERS CONFIGURE_DEPENDS "include/tulip/*.hpp")
add_library(tulip INTERFACE ${TULIP_HEADERS})
target_include_directories(tulip INTERFACE include)
target_compile_features(tulip INTERFACE cxx_std_17)
add_library(cenet::tulip ALIAS tulip)
file(GLOB_RECURSE CENET_HEADERS CONFIGURE_DEPENDS "include/cenet/*.hpp")
add_library(cenet INTERFACE ${CENET_HEADERS}) add_library(cenet INTERFACE ${CENET_HEADERS})
target_link_libraries(cenet INTERFACE tulip)
target_include_directories(cenet INTERFACE include) target_include_directories(cenet INTERFACE include)
target_compile_features(cenet INTERFACE cxx_std_17) target_compile_features(cenet INTERFACE cxx_std_17)
add_library(cenet::cenet ALIAS cenet)
if(CENET_BUILD_EXAMPLES) if(CENET_BUILD_EXAMPLES)
add_executable(xor example/xor.cpp) add_executable(xor example/xor.cpp)
+4 -4
View File
@@ -15,7 +15,7 @@
#include <random> #include <random>
#include <vector> #include <vector>
using network_type = cenet::network<cenet::math::tensor<float, 1, 28, 28>, using network_type = cenet::network<tulip::tensor<float, 1, 28, 28>,
cenet::layers::conv2d<8, 3, 1, 1>, cenet::layers::conv2d<8, 3, 1, 1>,
cenet::layers::relu, cenet::layers::relu,
cenet::layers::maxpool<>, cenet::layers::maxpool<>,
@@ -29,14 +29,14 @@ using network_type = cenet::network<cenet::math::tensor<float, 1, 28, 28>,
static std::uniform_real_distribution<> dist(-1, 1); static std::uniform_real_distribution<> dist(-1, 1);
struct data { struct data {
cenet::math::tensor<float, 1, 28, 28> xs{}; tulip::tensor<float, 1, 28, 28> xs{};
cenet::math::matrix<float, 1, 10> ys{}; tulip::matrix<float, 1, 10> ys{};
}; };
using dataset = std::vector<data>; using dataset = std::vector<data>;
template <std::size_t N, typename T> template <std::size_t N, typename T>
static std::size_t col_largest_idx(const cenet::math::tensor_view<T, N>& col) { static std::size_t col_largest_idx(const tulip::tensor_view<T, N>& col) {
std::size_t maxIdx = 0; std::size_t maxIdx = 0;
std::remove_cv_t<T> max = col[0]; std::remove_cv_t<T> max = col[0];
for (std::size_t i = 1; i < N; ++i) { for (std::size_t i = 1; i < N; ++i) {
+5 -5
View File
@@ -1,6 +1,6 @@
#include "cenet/layers/dense.hpp" #include "cenet/layers/dense.hpp"
#include "cenet/loss/mse.hpp" #include "cenet/loss/mse.hpp"
#include "cenet/math/tensor_fwd.hpp" #include "tulip/tensor_fwd.hpp"
#include <cenet/cenet.hpp> #include <cenet/cenet.hpp>
#include <cstddef> #include <cstddef>
@@ -11,7 +11,7 @@
static std::uniform_real_distribution<> dist(-1, 1); static std::uniform_real_distribution<> dist(-1, 1);
int main(void) { int main(void) {
static cenet::math::matrix<cenet::math::inline_storage<float>, 1, 1> s_trainData[4] = {}; static tulip::matrix<tulip::inline_storage<float>, 1, 1> s_trainData[4] = {};
for (std::size_t i = 0; i < 4; ++i) { for (std::size_t i = 0; i < 4; ++i) {
const bool x0 = (i & 1U) != 0; const bool x0 = (i & 1U) != 0;
const bool x1 = (i & 2U) != 0; const bool x1 = (i & 2U) != 0;
@@ -20,7 +20,7 @@ int main(void) {
} }
// network net; // network net;
using network_type = cenet::network<cenet::math::matrix<cenet::math::inline_storage<float>, 1, 2>, using network_type = cenet::network<tulip::matrix<tulip::inline_storage<float>, 1, 2>,
cenet::layers::dense<2>, cenet::layers::dense<2>,
cenet::layers::sigmoid, cenet::layers::sigmoid,
cenet::layers::dense<1>, cenet::layers::dense<1>,
@@ -45,7 +45,7 @@ int main(void) {
learningState.reset(); learningState.reset();
for (std::size_t i = 0; i < 4; ++i) { for (std::size_t i = 0; i < 4; ++i) {
cenet::math::matrix<cenet::math::inline_storage<float>, 1, 2> xs; tulip::matrix<tulip::inline_storage<float>, 1, 2> xs;
xs.coeff(0) = static_cast<float>(i & 1); xs.coeff(0) = static_cast<float>(i & 1);
xs.coeff(1) = static_cast<float>(i > 1); xs.coeff(1) = static_cast<float>(i > 1);
@@ -60,7 +60,7 @@ int main(void) {
double acc = 0.0; double acc = 0.0;
for (std::size_t i = 0; i < 4; ++i) { for (std::size_t i = 0; i < 4; ++i) {
cenet::math::matrix<cenet::math::inline_storage<float>, 1, 2> xs; tulip::matrix<tulip::inline_storage<float>, 1, 2> xs;
xs.coeff(0) = static_cast<float>(i & 1); xs.coeff(0) = static_cast<float>(i & 1);
xs.coeff(1) = static_cast<float>(i > 1); xs.coeff(1) = static_cast<float>(i > 1);
+22 -22
View File
@@ -1,16 +1,16 @@
#ifndef CENET_LAYERS_CONVOLUTIONAL_HPP #ifndef CENET_LAYERS_CONVOLUTIONAL_HPP
#define CENET_LAYERS_CONVOLUTIONAL_HPP #define CENET_LAYERS_CONVOLUTIONAL_HPP
#include "../math/tensor_fwd.hpp" #include "../optim/adam.hpp"
#include "../math/tensor_shape.hpp"
#include "../math/tensor_traits.hpp"
#include "./layer_fwd.hpp" #include "./layer_fwd.hpp"
#include "./layer_traits.hpp" #include "./layer_traits.hpp"
#include "cenet/layers/retained.hpp" #include "./retained.hpp"
#include "cenet/optim/adam.hpp"
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <tulip/tensor_fwd.hpp>
#include <tulip/tensor_shape.hpp>
#include <tulip/tensor_traits.hpp>
namespace cenet { namespace cenet {
namespace layers { namespace layers {
@@ -24,7 +24,7 @@ struct conv2d final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>; using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>;
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
static_assert(math::is_tensor_v<input_type>); static_assert(tulip::is_tensor_v<input_type>);
static_assert(input_type::shape_type::rank == 3); static_assert(input_type::shape_type::rank == 3);
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
@@ -44,11 +44,11 @@ struct conv2d final : public layer_tag {
static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - KernelSize + (2 * Padding)) / Stride) + 1; static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - KernelSize + (2 * Padding)) / Stride) + 1;
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1; static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1;
using output_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>; using output_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>;
using weights_type = using weights_type =
math::tensor<math::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>; tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS>; using biases_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS>;
weights_type weights{}; weights_type weights{};
biases_type biases{}; biases_type biases{};
@@ -75,7 +75,7 @@ struct conv2d final : public layer_tag {
} }
auto feed(const input_type& input) { auto feed(const input_type& input) {
typename output_type::template with_storage<math::heap_storage_tag> output; typename output_type::template with_storage<tulip::heap_storage_tag> output;
for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) { for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) {
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) { for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) { for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
@@ -109,7 +109,7 @@ struct conv2d final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>; using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>;
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
static_assert(math::is_tensor_v<input_type>); static_assert(tulip::is_tensor_v<input_type>);
static_assert(input_type::shape_type::rank == 3); static_assert(input_type::shape_type::rank == 3);
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
@@ -130,8 +130,8 @@ struct conv2d final : public layer_tag {
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1; static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1;
using weights_type = using weights_type =
math::tensor<math::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>; tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS>; using biases_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS>;
weights_type weights{}; weights_type weights{};
biases_type biases{}; biases_type biases{};
@@ -150,7 +150,7 @@ struct conv2d final : public layer_tag {
template <typename Error> template <typename Error>
auto backward(const data<N, I>& data, const input_type& retained, Error prevErr) { auto backward(const data<N, I>& data, const input_type& retained, Error prevErr) {
typename input_type::template with_storage<math::heap_storage_tag> backpropErr; typename input_type::template with_storage<tulip::heap_storage_tag> backpropErr;
backpropErr.view().fill(); backpropErr.view().fill();
for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) { for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) {
@@ -197,7 +197,7 @@ struct conv2d final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>; using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>;
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
static_assert(math::is_tensor_v<input_type>); static_assert(tulip::is_tensor_v<input_type>);
static_assert(input_type::shape_type::rank == 3); static_assert(input_type::shape_type::rank == 3);
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
@@ -218,8 +218,8 @@ struct conv2d final : public layer_tag {
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1; static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1;
using weights_type = using weights_type =
math::tensor<math::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>; tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS>; using biases_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS>;
std::size_t time = 0; std::size_t time = 0;
@@ -278,7 +278,7 @@ struct maxpool final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<maxpool, N, I>>; using traits = typename detail::layer_traits<network_layer<maxpool, N, I>>;
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
static_assert(math::is_tensor_v<input_type>); static_assert(tulip::is_tensor_v<input_type>);
static_assert(input_type::shape_type::rank == 3); static_assert(input_type::shape_type::rank == 3);
static_assert(!input_type::traits::dynamic, "dynamic input to maxpool is currently unsupported"); static_assert(!input_type::traits::dynamic, "dynamic input to maxpool is currently unsupported");
@@ -292,7 +292,7 @@ struct maxpool final : public layer_tag {
static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - PoolSize) / Stride) + 1; static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - PoolSize) / Stride) + 1;
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - PoolSize) / Stride) + 1; static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - PoolSize) / Stride) + 1;
using output_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>; using output_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>;
void dump(std::ostream& os) const {} void dump(std::ostream& os) const {}
@@ -389,14 +389,14 @@ struct flatten final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<flatten, N, I>>; using traits = typename detail::layer_traits<network_layer<flatten, N, I>>;
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
static_assert(math::is_tensor_v<input_type>); static_assert(tulip::is_tensor_v<input_type>);
static_assert(!input_type::traits::dynamic, "dynamic input to flatten is currently unsupported"); static_assert(!input_type::traits::dynamic, "dynamic input to flatten is currently unsupported");
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
static constexpr auto FEATURES = input_type::shape_type::size; static constexpr auto FEATURES = input_type::shape_type::size;
using output_type = math::matrix<value_type, 1, FEATURES>; using output_type = tulip::matrix<value_type, 1, FEATURES>;
void dump(std::ostream& os) const {} void dump(std::ostream& os) const {}
@@ -407,7 +407,7 @@ struct flatten final : public layer_tag {
template <typename Fwd> template <typename Fwd>
auto feed(Fwd&& fwd) { auto feed(Fwd&& fwd) {
return std::forward<Fwd>(fwd).template reshape<math::shape_of<1, FEATURES>>(); return std::forward<Fwd>(fwd).template reshape<tulip::shape_of<1, FEATURES>>();
} }
}; };
+14 -15
View File
@@ -5,15 +5,14 @@
#ifndef CENET_LAYERS_DENSE_HPP #ifndef CENET_LAYERS_DENSE_HPP
#define CENET_LAYERS_DENSE_HPP #define CENET_LAYERS_DENSE_HPP
#include "../math/tensor.hpp" #include "../optim/adam.hpp"
#include "../math/tensor_fwd.hpp"
#include "./layer_fwd.hpp" #include "./layer_fwd.hpp"
#include "./layer_traits.hpp" #include "./layer_traits.hpp"
#include "./retained.hpp" #include "./retained.hpp"
#include "cenet/optim/adam.hpp"
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <tulip/tensor.hpp>
namespace cenet { namespace cenet {
namespace layers { namespace layers {
@@ -28,12 +27,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
using output_type = math::matrix<value_type, 1, Features>; using output_type = tulip::matrix<value_type, 1, Features>;
static constexpr auto input_cols = input_type::shape_type::template sget<1>(); static constexpr auto input_cols = input_type::shape_type::template sget<1>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>; using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>; using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
weights_type weights; weights_type weights;
biases_type biases; biases_type biases;
@@ -68,12 +67,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
using output_type = math::matrix<value_type, 1, Features>; using output_type = tulip::matrix<value_type, 1, Features>;
static constexpr auto input_cols = input_type::shape_type::template sget<1>(); static constexpr auto input_cols = input_type::shape_type::template sget<1>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>; using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>; using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
weights_type weights; weights_type weights;
biases_type biases; biases_type biases;
@@ -98,12 +97,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
using output_type = math::matrix<value_type, 1, Features>; using output_type = tulip::matrix<value_type, 1, Features>;
static constexpr auto input_cols = input_type::shape_type::template sget<1>(); static constexpr auto input_cols = input_type::shape_type::template sget<1>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>; using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>; using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
weights_type weigths; weights_type weigths;
biases_type biases; biases_type biases;
@@ -120,12 +119,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_type; using input_type = typename traits::input_type;
using value_type = typename input_type::value_type; using value_type = typename input_type::value_type;
using output_type = math::matrix<value_type, 1, Features>; using output_type = tulip::matrix<value_type, 1, Features>;
static constexpr auto input_cols = input_type::shape_type::template sget<1>(); static constexpr auto input_cols = input_type::shape_type::template sget<1>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>; using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>; using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
std::size_t time = 0; std::size_t time = 0;
+1 -1
View File
@@ -2,8 +2,8 @@
#define CENET_LAYERS_LAYER_TRAITS_HPP #define CENET_LAYERS_LAYER_TRAITS_HPP
#include "../network_fwd.hpp" #include "../network_fwd.hpp"
#include "../network_traits.hpp"
#include "./layer_fwd.hpp" #include "./layer_fwd.hpp"
#include "cenet/network_traits.hpp"
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
+5 -6
View File
@@ -1,8 +1,6 @@
#ifndef CENET_LAYERS_TOKEN_HPP #ifndef CENET_LAYERS_TOKEN_HPP
#define CENET_LAYERS_TOKEN_HPP #define CENET_LAYERS_TOKEN_HPP
#include "../math/tensor.hpp"
#include "../math/tensor_fwd.hpp"
#include "./layer_fwd.hpp" #include "./layer_fwd.hpp"
#include "./layer_traits.hpp" #include "./layer_traits.hpp"
#include "./retained.hpp" #include "./retained.hpp"
@@ -12,6 +10,7 @@
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
#include <random> #include <random>
#include <tulip/tensor.hpp>
#include <vector> #include <vector>
namespace cenet { namespace cenet {
@@ -47,8 +46,8 @@ struct tok2vec final : public layer_tag {
static_assert(VocabSize > 0); static_assert(VocabSize > 0);
static_assert(EmbedDims > 0); static_assert(EmbedDims > 0);
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>; using output_type = tulip::matrix<float, tulip::dynamic_extent, EmbedDims>;
using embedding_type = math::matrix<float, VocabSize, EmbedDims>; using embedding_type = tulip::matrix<float, VocabSize, EmbedDims>;
embedding_type embedding; embedding_type embedding;
@@ -95,8 +94,8 @@ struct tok2vec final : public layer_tag {
static_assert(VocabSize > 0); static_assert(VocabSize > 0);
static_assert(EmbedDims > 0); static_assert(EmbedDims > 0);
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>; using output_type = tulip::matrix<float, tulip::dynamic_extent, EmbedDims>;
using embedding_type = math::matrix<float, VocabSize, EmbedDims>; using embedding_type = tulip::matrix<float, VocabSize, EmbedDims>;
embedding_type embedding; embedding_type embedding;
embedding_type momentum; embedding_type momentum;
-15
View File
@@ -1,15 +0,0 @@
#ifndef CENET_MATH_TYPE_TRAITS_HPP
#define CENET_MATH_TYPE_TRAITS_HPP
#include <type_traits>
namespace cenet {
namespace math {
template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
}
} // namespace cenet
#endif // CENET_MATH_TYPE_TRAITS_HPP
+3 -3
View File
@@ -1,14 +1,14 @@
#ifndef CENET_NETWORK_HPP #ifndef CENET_NETWORK_HPP
#define CENET_NETWORK_HPP #define CENET_NETWORK_HPP
#include "./layers/retained.hpp"
#include "./network_traits.hpp" #include "./network_traits.hpp"
#include "cenet/layers/retained.hpp" #include "./optim/sgd.hpp"
#include "cenet/math/expr.hpp"
#include "cenet/optim/sgd.hpp"
#include <cstddef> #include <cstddef>
#include <istream> #include <istream>
#include <ostream> #include <ostream>
#include <tulip/expr.hpp>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
@@ -1,16 +1,15 @@
#ifndef CENET_MATH_EXPR_HPP #ifndef TULIP_EXPR_HPP
#define CENET_MATH_EXPR_HPP #define TULIP_EXPR_HPP
#include "cenet/math/tensor_traits.hpp" #include "./ops.hpp"
#include "ops.hpp" #include "./tensor_traits.hpp"
#include "type_traits.hpp" #include "./type_traits.hpp"
#include <stdexcept> #include <stdexcept>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
namespace cenet { namespace tulip {
namespace math {
struct expression_tag {}; struct expression_tag {};
struct expression_node_tag : expression_tag {}; struct expression_node_tag : expression_tag {};
@@ -254,7 +253,6 @@ constexpr decltype(auto) eval(T&& fwd) {
} }
} }
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_EXPR_HPP #endif // TULIP_EXPR_HPP
@@ -1,17 +1,16 @@
#ifndef CENET_MATH_OPS_HPP #ifndef TULIP_OPS_HPP
#define CENET_MATH_OPS_HPP #define TULIP_OPS_HPP
#include "tensor_fwd.hpp" #include "./tensor_fwd.hpp"
#include "tensor_shape.hpp" #include "./tensor_shape.hpp"
#include "type_traits.hpp" #include "./type_traits.hpp"
#include <cstddef> #include <cstddef>
#include <functional> #include <functional>
#include <stdexcept> #include <stdexcept>
#include <type_traits> #include <type_traits>
namespace cenet { namespace tulip {
namespace math {
namespace ops { namespace ops {
namespace detail { namespace detail {
@@ -38,7 +37,7 @@ struct generic_binary_op {
template <typename L, typename R> template <typename L, typename R>
static constexpr auto shape(const L& lhs, const R& rhs) { static constexpr auto shape(const L& lhs, const R& rhs) {
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands"); if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
using best_shape = cenet::math::detail::best_shape<decltype(shape_wrapper(lhs)), decltype(shape_wrapper(rhs))>; using best_shape = tulip::detail::best_shape<decltype(shape_wrapper(lhs)), decltype(shape_wrapper(rhs))>;
static_assert(best_shape::valid, "uncompatible operands"); static_assert(best_shape::valid, "uncompatible operands");
if constexpr (best_shape::left) { if constexpr (best_shape::left) {
return lhs.shape(); return lhs.shape();
@@ -191,12 +190,12 @@ struct transpose {
}; };
// TODO: introduce dynamic reshape // TODO: introduce dynamic reshape
template <typename S, typename = std::enable_if_t<::cenet::math::detail::is_static_shape_v<S>>> template <typename S, typename = std::enable_if_t<::tulip::detail::is_static_shape_v<S>>>
struct reshape { struct reshape {
template <typename E> template <typename E>
static constexpr bool compatible(const E& expr) { static constexpr bool compatible(const E& expr) {
using shape_type = remove_cvref_t<decltype(expr.shape())>; using shape_type = remove_cvref_t<decltype(expr.shape())>;
if constexpr (!::cenet::math::detail::is_static_shape<shape_type>::value) { if constexpr (!::tulip::detail::is_static_shape<shape_type>::value) {
return false; return false;
} else { } else {
return shape_type::size == S::size; return shape_type::size == S::size;
@@ -216,7 +215,6 @@ struct reshape {
}; };
} // namespace ops } // namespace ops
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_OPS_HPP #endif // TULIP_OPS_HPP
+28
View File
@@ -0,0 +1,28 @@
#ifndef TULIP_SIMD_AVX_HPP
#define TULIP_SIMD_AVX_HPP
#include "./traits.hpp"
namespace tulip {
namespace simd {
namespace detail {
#ifdef TULIP_AVX_SUPPORTED
template <>
struct traits<float, avx_isa> {
using pack_type = __m128;
static constexpr std::size_t width = 4;
static pack_type load(const float* ptr) noexcept { return _mm128_loadu_ps(ptr); }
static void store(float* ptr, pack_type value) noexcept { _mm128_storeu_ps(ptr, value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm128_add_ps(lhs, rhs); }
};
#endif // TULIP_AVX_SUPPORTED
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_AVX_HPP
+40
View File
@@ -0,0 +1,40 @@
#ifndef TULIP_SIMD_AVX2_HPP
#define TULIP_SIMD_AVX2_HPP
#include "./traits.hpp"
namespace tulip {
namespace simd {
namespace detail {
#ifdef TULIP_AVX2_SUPPORTED
template <>
struct traits<float, avx2_isa> {
using pack_type = __m256;
static constexpr std::size_t width = 8;
static pack_type load(const float* ptr) noexcept { return _mm256_loadu_ps(ptr); }
static void store(float* ptr, pack_type value) noexcept { _mm256_storeu_ps(ptr, value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256_add_ps(lhs, rhs); }
};
template <>
struct traits<double, avx2_isa> {
using pack_type = __m256d;
static constexpr std::size_t width = 4;
static pack_type load(const double* ptr) noexcept { return _mm256d_loadu_ps(ptr); }
static void store(double* ptr, pack_type value) noexcept { _mm256d_storeu_ps(ptr, value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256d_add_ps(lhs, rhs); }
};
#endif // TULIP_AVX2_SUPPORTED
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_AVX2_HPP
+24
View File
@@ -0,0 +1,24 @@
#ifndef TULIP_SIMD_CONFIG_HPP
#define TULIP_SIMD_CONFIG_HPP
namespace tulip {
namespace simd {
struct scalar_isa {};
struct avx_isa {};
struct avx2_isa {};
#ifdef __AVX__
#define TULIP_AVX_SUPPORTED
#include <immintrin.h>
#endif // __AVX__
#ifdef __AVX2__
#define TULIP_AVX2_SUPPORTED
#include <immintrin.h>
#endif // __AVX2__
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_CONFIG_HPP
+52
View File
@@ -0,0 +1,52 @@
#ifndef TULIP_SIMD_TRAITS_HPP
#define TULIP_SIMD_TRAITS_HPP
#include "./config.hpp"
#include <cstddef>
namespace tulip {
namespace simd {
namespace detail {
template <typename T, typename Isa>
struct traits {};
template <typename T>
struct traits<T, scalar_isa> {
using pack_type = T;
static constexpr std::size_t width = 1;
static pack_type load(const T* ptr) noexcept { return *ptr; }
static void store(T* ptr, pack_type value) noexcept { *ptr = value; }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return lhs + rhs; }
};
template <typename T, typename Isa>
using pack_t = typename traits<T, Isa>::pack_type;
template <typename T, typename Isa>
inline constexpr auto width_v = traits<T, Isa>::width;
template <typename T, typename Isa>
static auto load(const T* ptr) noexcept {
return traits<T, Isa>::load(ptr);
}
template <typename T, typename Isa>
static void store(const T* ptr, pack_t<T, Isa> value) noexcept {
traits<T, Isa>::store(ptr, value);
}
template <typename T, typename Isa>
static auto add(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
return traits<T, Isa>::add(lhs, rhs);
}
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_TRAITS_HPP
@@ -1,11 +1,11 @@
#ifndef CENET_MATH_TENSOR_HPP #ifndef TULIP_TENSOR_HPP
#define CENET_MATH_TENSOR_HPP #define TULIP_TENSOR_HPP
#include "expr.hpp" #include "./expr.hpp"
#include "tensor_fwd.hpp" #include "./tensor_fwd.hpp"
#include "tensor_shape.hpp" #include "./tensor_shape.hpp"
#include "tensor_traits.hpp" #include "./tensor_traits.hpp"
#include "tensor_view.hpp" #include "./tensor_view.hpp"
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
@@ -13,8 +13,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
namespace cenet { namespace tulip {
namespace math {
template <typename T, std::size_t... Dims> template <typename T, std::size_t... Dims>
class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> { class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> {
@@ -273,7 +272,6 @@ private:
shape_type m_shape; shape_type m_shape;
}; };
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_TENSOR_HPP #endif // TULIP_TENSOR_HPP
@@ -1,11 +1,10 @@
#ifndef CENET_MATH_FWD_HPP #ifndef TULIP_FWD_HPP
#define CENET_MATH_FWD_HPP #define TULIP_FWD_HPP
#include <cstddef> #include <cstddef>
#include <limits> #include <limits>
namespace cenet { namespace tulip {
namespace math {
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
@@ -26,7 +25,6 @@ class tensor;
template <typename T, std::size_t M, std::size_t N> template <typename T, std::size_t M, std::size_t N>
using matrix = tensor<T, M, N>; using matrix = tensor<T, M, N>;
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_FWD_HPP #endif // TULIP_FWD_HPP
@@ -1,14 +1,13 @@
#ifndef CENET_MATH_TENSOR_SHAPE_HPP #ifndef TULIP_TENSOR_SHAPE_HPP
#define CENET_MATH_TENSOR_SHAPE_HPP #define TULIP_TENSOR_SHAPE_HPP
#include "tensor_fwd.hpp" #include "./tensor_fwd.hpp"
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
namespace cenet { namespace tulip {
namespace math {
template <std::size_t... Dims> template <std::size_t... Dims>
struct shape {}; struct shape {};
@@ -315,7 +314,6 @@ bool operator!=(const L<LDims...>& lhs, const R<RDims...>& rhs) {
!detail::shape_compare_impl(std::make_index_sequence<sizeof...(LDims)>{}, lhs, rhs); !detail::shape_compare_impl(std::make_index_sequence<sizeof...(LDims)>{}, lhs, rhs);
} }
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_TENSOR_SHAPE_HPP #endif // TULIP_TENSOR_SHAPE_HPP
@@ -1,14 +1,13 @@
#ifndef CENET_MATH_TENSOR_TRAITS_HPP #ifndef TULIP_TENSOR_TRAITS_HPP
#define CENET_MATH_TENSOR_TRAITS_HPP #define TULIP_TENSOR_TRAITS_HPP
#include "cenet/math/tensor_shape.hpp" #include "./tensor_fwd.hpp"
#include "cenet/math/type_traits.hpp" #include "./tensor_shape.hpp"
#include "tensor_fwd.hpp" #include "./type_traits.hpp"
#include <type_traits> #include <type_traits>
namespace cenet { namespace tulip {
namespace math {
template <typename T> template <typename T>
struct tensor_storage {}; struct tensor_storage {};
@@ -139,7 +138,6 @@ constexpr decltype(auto) tensor_from_shape(const Shape& shape) {
return tensor; return tensor;
} }
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_TENSOR_TRAITS_HPP #endif // TULIP_TENSOR_TRAITS_HPP
@@ -1,10 +1,10 @@
#ifndef CENET_MATH_TENSOR_VIEW_HPP #ifndef TULIP_TENSOR_VIEW_HPP
#define CENET_MATH_TENSOR_VIEW_HPP #define TULIP_TENSOR_VIEW_HPP
#include "expr.hpp" #include "./expr.hpp"
#include "tensor_fwd.hpp" #include "./tensor_fwd.hpp"
#include "tensor_shape.hpp" #include "./tensor_shape.hpp"
#include "tensor_traits.hpp" #include "./tensor_traits.hpp"
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
@@ -12,8 +12,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
namespace cenet { namespace tulip {
namespace math {
template <typename T, std::size_t... Dims> template <typename T, std::size_t... Dims>
class tensor_view : public expression_tag, public ops_provider<tensor_view<T, Dims...>> { class tensor_view : public expression_tag, public ops_provider<tensor_view<T, Dims...>> {
@@ -132,7 +131,6 @@ private:
shape_type m_shape; shape_type m_shape;
}; };
} // namespace math } // namespace tulip
} // namespace cenet
#endif // CENET_MATH_TENSOR_VIEW_HPP #endif // TULIP_TENSOR_VIEW_HPP
+13
View File
@@ -0,0 +1,13 @@
#ifndef TULIP_TYPE_TRAITS_HPP
#define TULIP_TYPE_TRAITS_HPP
#include <type_traits>
namespace tulip {
template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
} // namespace tulip
#endif // TULIP_TYPE_TRAITS_HPP
BIN
View File
Binary file not shown.
+24 -24
View File
@@ -1,33 +1,33 @@
// TODO: write actual tests // TODO: write actual tests
#include <algorithm> #include <algorithm>
#include <cenet/math/expr.hpp>
#include <cenet/math/tensor.hpp>
#include <cenet/math/tensor_fwd.hpp>
#include <cstring> #include <cstring>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <tulip/expr.hpp>
#include <tulip/tensor.hpp>
#include <tulip/tensor_fwd.hpp>
using cenet::math::dynamic_extent; using tulip::dynamic_extent;
// Whatever // Whatever
TEST(Tensor, View) { TEST(Tensor, View) {
cenet::math::tensor<float, 3, 2, 2> tensor; tulip::tensor<float, 3, 2, 2> tensor;
auto view = tensor.view()[1][1]; auto view = tensor.view()[1][1];
} }
TEST(Tensor, Ops) { TEST(Tensor, Ops) {
constexpr float lhsArray[2 * 3] = { 1, 2, 3, 4, 5, 6 }; constexpr float lhsArray[2 * 3] = { 1, 2, 3, 4, 5, 6 };
cenet::math::tensor<float, dynamic_extent, 3> lhs; tulip::tensor<float, dynamic_extent, 3> lhs;
lhs.set_dimensions(2, 3); lhs.set_dimensions(2, 3);
lhs.view().fill(lhsArray); lhs.view().fill(lhsArray);
constexpr float rhsArray[2 * 3] = { 6, 5, 4, 3, 2, 1 }; constexpr float rhsArray[2 * 3] = { 6, 5, 4, 3, 2, 1 };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 3> rhs; tulip::tensor<tulip::inline_storage<float>, 2, 3> rhs;
rhs.view().fill(rhsArray); rhs.view().fill(rhsArray);
constexpr float resultArray[2 * 3] = { 6 * 1, 6 * 2, 6 * 3, 6 * 4, 6 * 5, 6 * 6 }; constexpr float resultArray[2 * 3] = { 6 * 1, 6 * 2, 6 * 3, 6 * 4, 6 * 5, 6 * 6 };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 3> result = lhs + rhs - 1; tulip::tensor<tulip::inline_storage<float>, 2, 3> result = lhs + rhs - 1;
result *= lhs; result *= lhs;
for (std::size_t i = 0; i < result.size(); ++i) { for (std::size_t i = 0; i < result.size(); ++i) {
@@ -39,15 +39,15 @@ TEST(Tensor, MatMul) {
constexpr float lhsArray[2 * 2] = { 1, 2, 3, 4 }; constexpr float lhsArray[2 * 2] = { 1, 2, 3, 4 };
constexpr float rhsArray[2 * 2] = { 5, 6, 7, 8 }; constexpr float rhsArray[2 * 2] = { 5, 6, 7, 8 };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> lhs; tulip::tensor<tulip::inline_storage<float>, 2, 2> lhs;
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> rhs; tulip::tensor<tulip::inline_storage<float>, 2, 2> rhs;
lhs.view().fill(lhsArray); lhs.view().fill(lhsArray);
rhs.view().fill(rhsArray); rhs.view().fill(rhsArray);
constexpr float expArray[2 * 2] = { (1 * 5) + (2 * 7), (1 * 6) + (2 * 8), (3 * 5) + (4 * 7), (3 * 6) + (4 * 8) }; constexpr float expArray[2 * 2] = { (1 * 5) + (2 * 7), (1 * 6) + (2 * 8), (3 * 5) + (4 * 7), (3 * 6) + (4 * 8) };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> result = lhs.matmul(rhs); tulip::tensor<tulip::inline_storage<float>, 2, 2> result = lhs.matmul(rhs);
for (std::size_t i = 0; i < result.size(); ++i) { for (std::size_t i = 0; i < result.size(); ++i) {
EXPECT_EQ(result.coeff(i), expArray[i]); EXPECT_EQ(result.coeff(i), expArray[i]);
} }
@@ -58,23 +58,23 @@ TEST(Tensor, MatMul2) {
constexpr float lhsArray[3 * 2] = { 1, 2, 5, 6, 7, 8 }; constexpr float lhsArray[3 * 2] = { 1, 2, 5, 6, 7, 8 };
constexpr float rhsArray[2 * 1] = { 3, 4 }; constexpr float rhsArray[2 * 1] = { 3, 4 };
cenet::math::tensor<cenet::math::inline_storage<float>, 3, 2> lhs; tulip::tensor<tulip::inline_storage<float>, 3, 2> lhs;
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 1> rhs; tulip::tensor<tulip::inline_storage<float>, 2, 1> rhs;
lhs.view().fill(lhsArray); lhs.view().fill(lhsArray);
rhs.view().fill(rhsArray); rhs.view().fill(rhsArray);
constexpr float expArray[3 * 1] = { 11, 39, 53 }; constexpr float expArray[3 * 1] = { 11, 39, 53 };
cenet::math::tensor<cenet::math::inline_storage<float>, 3, 1> result = lhs.matmul(rhs); tulip::tensor<tulip::inline_storage<float>, 3, 1> result = lhs.matmul(rhs);
for (std::size_t i = 0; i < result.size(); ++i) { for (std::size_t i = 0; i < result.size(); ++i) {
EXPECT_EQ(result.coeff(i), expArray[i]); EXPECT_EQ(result.coeff(i), expArray[i]);
} }
} }
TEST(Tensor, Apply) { TEST(Tensor, Apply) {
constexpr float values[2 * 2] = { 1, 2, 3, 4 }; constexpr float values[2 * 2] = { 1, 2, 3, 4 };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> tensor; tulip::tensor<tulip::inline_storage<float>, 2, 2> tensor;
tensor.view().fill(values); tensor.view().fill(values);
constexpr float expected[2 * 2] = { 1, 4, 9, 16 }; constexpr float expected[2 * 2] = { 1, 4, 9, 16 };
@@ -89,15 +89,15 @@ TEST(Tensor, Apply2) {
constexpr float lhsArray[2 * 2] = { 1, 2, 3, 4 }; constexpr float lhsArray[2 * 2] = { 1, 2, 3, 4 };
constexpr float rhsArray[2 * 2] = { 4, 3, 2, 1 }; constexpr float rhsArray[2 * 2] = { 4, 3, 2, 1 };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> lhs; tulip::tensor<tulip::inline_storage<float>, 2, 2> lhs;
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> rhs; tulip::tensor<tulip::inline_storage<float>, 2, 2> rhs;
lhs.view().fill(lhsArray); lhs.view().fill(lhsArray);
rhs.view().fill(rhsArray); rhs.view().fill(rhsArray);
constexpr float expArray[2 * 2] = { 4, 3, 3, 4 }; constexpr float expArray[2 * 2] = { 4, 3, 3, 4 };
cenet::math::tensor<cenet::math::inline_storage<float>, 2, 2> result = tulip::tensor<tulip::inline_storage<float>, 2, 2> result =
lhs.apply(rhs, [](auto x1, auto x2) { return std::max(x1, x2); }); lhs.apply(rhs, [](auto x1, auto x2) { return std::max(x1, x2); });
for (std::size_t i = 0; i < result.size(); ++i) { for (std::size_t i = 0; i < result.size(); ++i) {
EXPECT_EQ(result.coeff(i), expArray[i]); EXPECT_EQ(result.coeff(i), expArray[i]);
@@ -105,7 +105,7 @@ TEST(Tensor, Apply2) {
} }
TEST(Tensor, Offsets) { TEST(Tensor, Offsets) {
cenet::math::tensor<float, 2, 3, 4> tensor; tulip::tensor<float, 2, 3, 4> tensor;
for (std::size_t i = 0; i < tensor.size(); ++i) for (std::size_t i = 0; i < tensor.size(); ++i)
tensor.coeff(i) = static_cast<float>(i); tensor.coeff(i) = static_cast<float>(i);
@@ -116,7 +116,7 @@ TEST(Tensor, Offsets) {
} }
TEST(Tensor, Offsets2) { TEST(Tensor, Offsets2) {
using shape_type = cenet::math::static_shape<2, 3, 4>; using shape_type = tulip::static_shape<2, 3, 4>;
shape_type shape; shape_type shape;