refactor(tulip): move cenet/math/ to tulip/

This commit is contained in:
2026-11-22 12:54:57 +01:00
parent 14292b1d97
commit dea973ddd6
19 changed files with 157 additions and 167 deletions
+22 -22
View File
@@ -1,16 +1,16 @@
#ifndef CENET_LAYERS_CONVOLUTIONAL_HPP
#define CENET_LAYERS_CONVOLUTIONAL_HPP
#include "../math/tensor_fwd.hpp"
#include "../math/tensor_shape.hpp"
#include "../math/tensor_traits.hpp"
#include "../optim/adam.hpp"
#include "./layer_fwd.hpp"
#include "./layer_traits.hpp"
#include "cenet/layers/retained.hpp"
#include "cenet/optim/adam.hpp"
#include "./retained.hpp"
#include <cmath>
#include <iostream>
#include <tulip/tensor_fwd.hpp>
#include <tulip/tensor_shape.hpp>
#include <tulip/tensor_traits.hpp>
namespace cenet {
namespace layers {
@@ -24,7 +24,7 @@ struct conv2d final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>;
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);
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_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 =
math::tensor<math::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS>;
tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS>;
weights_type weights{};
biases_type biases{};
@@ -75,7 +75,7 @@ struct conv2d final : public layer_tag {
}
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 oy = 0; oy < OUT_HEIGHT; ++oy) {
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 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);
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;
using weights_type =
math::tensor<math::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS>;
tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS>;
weights_type weights{};
biases_type biases{};
@@ -150,7 +150,7 @@ struct conv2d final : public layer_tag {
template <typename Error>
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();
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 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);
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;
using weights_type =
math::tensor<math::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = math::tensor<math::inline_storage<value_type>, OUT_CHANS>;
tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
using biases_type = tulip::tensor<tulip::inline_storage<value_type>, OUT_CHANS>;
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 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::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_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 {}
@@ -389,14 +389,14 @@ struct flatten final : public layer_tag {
using traits = typename detail::layer_traits<network_layer<flatten, N, I>>;
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");
using value_type = typename input_type::value_type;
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 {}
@@ -407,7 +407,7 @@ struct flatten final : public layer_tag {
template <typename 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
#define CENET_LAYERS_DENSE_HPP
#include "../math/tensor.hpp"
#include "../math/tensor_fwd.hpp"
#include "../optim/adam.hpp"
#include "./layer_fwd.hpp"
#include "./layer_traits.hpp"
#include "./retained.hpp"
#include "cenet/optim/adam.hpp"
#include <cmath>
#include <iostream>
#include <tulip/tensor.hpp>
namespace cenet {
namespace layers {
@@ -28,12 +27,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_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>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>;
using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
weights_type weights;
biases_type biases;
@@ -68,12 +67,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_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>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>;
using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
weights_type weights;
biases_type biases;
@@ -98,12 +97,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_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>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>;
using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
weights_type weigths;
biases_type biases;
@@ -120,12 +119,12 @@ struct dense final : public layer_tag {
using input_type = typename traits::input_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>();
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>;
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>;
using weights_type = tulip::matrix<tulip::inline_storage<value_type>, input_cols, Features>;
using biases_type = tulip::matrix<tulip::inline_storage<value_type>, 1, Features>;
std::size_t time = 0;
+1 -1
View File
@@ -2,8 +2,8 @@
#define CENET_LAYERS_LAYER_TRAITS_HPP
#include "../network_fwd.hpp"
#include "../network_traits.hpp"
#include "./layer_fwd.hpp"
#include "cenet/network_traits.hpp"
#include <cstddef>
#include <type_traits>
+5 -6
View File
@@ -1,8 +1,6 @@
#ifndef CENET_LAYERS_TOKEN_HPP
#define CENET_LAYERS_TOKEN_HPP
#include "../math/tensor.hpp"
#include "../math/tensor_fwd.hpp"
#include "./layer_fwd.hpp"
#include "./layer_traits.hpp"
#include "./retained.hpp"
@@ -12,6 +10,7 @@
#include <cstdint>
#include <iostream>
#include <random>
#include <tulip/tensor.hpp>
#include <vector>
namespace cenet {
@@ -47,8 +46,8 @@ struct tok2vec final : public layer_tag {
static_assert(VocabSize > 0);
static_assert(EmbedDims > 0);
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
using embedding_type = math::matrix<float, VocabSize, EmbedDims>;
using output_type = tulip::matrix<float, tulip::dynamic_extent, EmbedDims>;
using embedding_type = tulip::matrix<float, VocabSize, EmbedDims>;
embedding_type embedding;
@@ -95,8 +94,8 @@ struct tok2vec final : public layer_tag {
static_assert(VocabSize > 0);
static_assert(EmbedDims > 0);
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
using embedding_type = math::matrix<float, VocabSize, EmbedDims>;
using output_type = tulip::matrix<float, tulip::dynamic_extent, EmbedDims>;
using embedding_type = tulip::matrix<float, VocabSize, EmbedDims>;
embedding_type embedding;
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
#define CENET_NETWORK_HPP
#include "./layers/retained.hpp"
#include "./network_traits.hpp"
#include "cenet/layers/retained.hpp"
#include "cenet/math/expr.hpp"
#include "cenet/optim/sgd.hpp"
#include "./optim/sgd.hpp"
#include <cstddef>
#include <istream>
#include <ostream>
#include <tulip/expr.hpp>
#include <tuple>
#include <utility>
@@ -1,16 +1,15 @@
#ifndef CENET_MATH_EXPR_HPP
#define CENET_MATH_EXPR_HPP
#ifndef TULIP_EXPR_HPP
#define TULIP_EXPR_HPP
#include "cenet/math/tensor_traits.hpp"
#include "ops.hpp"
#include "type_traits.hpp"
#include "./ops.hpp"
#include "./tensor_traits.hpp"
#include "./type_traits.hpp"
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
namespace tulip {
struct expression_tag {};
struct expression_node_tag : expression_tag {};
@@ -254,7 +253,6 @@ constexpr decltype(auto) eval(T&& fwd) {
}
}
} // namespace math
} // namespace cenet
} // namespace tulip
#endif // CENET_MATH_EXPR_HPP
#endif // TULIP_EXPR_HPP
@@ -1,17 +1,16 @@
#ifndef CENET_MATH_OPS_HPP
#define CENET_MATH_OPS_HPP
#ifndef TULIP_OPS_HPP
#define TULIP_OPS_HPP
#include "tensor_fwd.hpp"
#include "tensor_shape.hpp"
#include "type_traits.hpp"
#include "./tensor_fwd.hpp"
#include "./tensor_shape.hpp"
#include "./type_traits.hpp"
#include <cstddef>
#include <functional>
#include <stdexcept>
#include <type_traits>
namespace cenet {
namespace math {
namespace tulip {
namespace ops {
namespace detail {
@@ -38,7 +37,7 @@ struct generic_binary_op {
template <typename L, typename R>
static constexpr auto shape(const L& lhs, const R& rhs) {
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");
if constexpr (best_shape::left) {
return lhs.shape();
@@ -191,12 +190,12 @@ struct transpose {
};
// 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 {
template <typename E>
static constexpr bool compatible(const E& expr) {
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;
} else {
return shape_type::size == S::size;
@@ -216,7 +215,6 @@ struct reshape {
};
} // namespace ops
} // namespace math
} // namespace cenet
} // namespace tulip
#endif // CENET_MATH_OPS_HPP
#endif // TULIP_OPS_HPP
@@ -1,11 +1,11 @@
#ifndef CENET_MATH_TENSOR_HPP
#define CENET_MATH_TENSOR_HPP
#ifndef TULIP_TENSOR_HPP
#define TULIP_TENSOR_HPP
#include "expr.hpp"
#include "tensor_fwd.hpp"
#include "tensor_shape.hpp"
#include "tensor_traits.hpp"
#include "tensor_view.hpp"
#include "./expr.hpp"
#include "./tensor_fwd.hpp"
#include "./tensor_shape.hpp"
#include "./tensor_traits.hpp"
#include "./tensor_view.hpp"
#include <cstddef>
#include <cstring>
@@ -13,8 +13,7 @@
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
namespace tulip {
template <typename T, std::size_t... Dims>
class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> {
@@ -273,7 +272,6 @@ private:
shape_type m_shape;
};
} // namespace math
} // namespace cenet
} // namespace tulip
#endif // CENET_MATH_TENSOR_HPP
#endif // TULIP_TENSOR_HPP
@@ -1,11 +1,10 @@
#ifndef CENET_MATH_FWD_HPP
#define CENET_MATH_FWD_HPP
#ifndef TULIP_FWD_HPP
#define TULIP_FWD_HPP
#include <cstddef>
#include <limits>
namespace cenet {
namespace math {
namespace tulip {
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>
using matrix = tensor<T, M, N>;
} // namespace math
} // namespace cenet
} // namespace tulip
#endif // CENET_MATH_FWD_HPP
#endif // TULIP_FWD_HPP
@@ -1,14 +1,13 @@
#ifndef CENET_MATH_TENSOR_SHAPE_HPP
#define CENET_MATH_TENSOR_SHAPE_HPP
#ifndef TULIP_TENSOR_SHAPE_HPP
#define TULIP_TENSOR_SHAPE_HPP
#include "tensor_fwd.hpp"
#include "./tensor_fwd.hpp"
#include <cstddef>
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
namespace tulip {
template <std::size_t... Dims>
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);
}
} // namespace math
} // namespace cenet
} // namespace tulip
#endif // CENET_MATH_TENSOR_SHAPE_HPP
#endif // TULIP_TENSOR_SHAPE_HPP
@@ -1,14 +1,13 @@
#ifndef CENET_MATH_TENSOR_TRAITS_HPP
#define CENET_MATH_TENSOR_TRAITS_HPP
#ifndef TULIP_TENSOR_TRAITS_HPP
#define TULIP_TENSOR_TRAITS_HPP
#include "cenet/math/tensor_shape.hpp"
#include "cenet/math/type_traits.hpp"
#include "tensor_fwd.hpp"
#include "./tensor_fwd.hpp"
#include "./tensor_shape.hpp"
#include "./type_traits.hpp"
#include <type_traits>
namespace cenet {
namespace math {
namespace tulip {
template <typename T>
struct tensor_storage {};
@@ -139,7 +138,6 @@ constexpr decltype(auto) tensor_from_shape(const Shape& shape) {
return tensor;
}
} // namespace math
} // namespace cenet
} // namespace tulip
#endif // CENET_MATH_TENSOR_TRAITS_HPP
#endif // TULIP_TENSOR_TRAITS_HPP
@@ -1,10 +1,10 @@
#ifndef CENET_MATH_TENSOR_VIEW_HPP
#define CENET_MATH_TENSOR_VIEW_HPP
#ifndef TULIP_TENSOR_VIEW_HPP
#define TULIP_TENSOR_VIEW_HPP
#include "expr.hpp"
#include "tensor_fwd.hpp"
#include "tensor_shape.hpp"
#include "tensor_traits.hpp"
#include "./expr.hpp"
#include "./tensor_fwd.hpp"
#include "./tensor_shape.hpp"
#include "./tensor_traits.hpp"
#include <cstddef>
#include <cstring>
@@ -12,8 +12,7 @@
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
namespace tulip {
template <typename T, std::size_t... Dims>
class tensor_view : public expression_tag, public ops_provider<tensor_view<T, Dims...>> {
@@ -132,7 +131,6 @@ private:
shape_type m_shape;
};
} // namespace math
} // namespace cenet
} // namespace tulip
#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