feat(math): implement slice for tensor_view

This commit is contained in:
2026-10-28 21:35:43 +01:00
parent a47302d532
commit 94276e68d3
5 changed files with 169 additions and 28 deletions
+61 -8
View File
@@ -2,13 +2,16 @@
#define CENET_LAYER_HPP
#include "mat.hpp"
#include "math/tensor.hpp"
#include "tensor.hpp"
#include <array>
#include <cstddef>
#include <istream>
#include <ostream>
#include <tuple>
#include <type_traits>
#include <vector>
namespace cenet {
@@ -32,13 +35,25 @@ struct is_tensor<tensor<T, Dims...>> : std::true_type {};
template <typename T>
static constexpr auto is_tensor_v = is_tensor<T>::value;
template <typename T>
struct is_tokens : std::false_type {};
template <std::size_t I>
struct is_tokens<std::array<std::uint32_t, I>> : std::true_type {};
template <>
struct is_tokens<std::vector<std::uint32_t>> : std::true_type {};
template <typename T>
static constexpr auto is_tokens_v = is_tokens<T>::value;
} // namespace detail
struct layer_info {
protected:
template <class NN, std::size_t I>
struct input_type_s {
using type = std::tuple_element_t<I - 1, typename NN::layer_types>::output_type;
using type = typename std::tuple_element_t<I - 1, typename NN::layer_types>::output_type;
};
template <class NN>
@@ -57,7 +72,7 @@ class dense_layer final : public layer_info {
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = input_type_s<NN, I>::type;
using input_type = typename input_type_s<NN, I>::type;
static_assert(detail::is_dense_v<input_type>);
using output_type = mat<1, Features, T>;
@@ -161,7 +176,7 @@ class sigmoid_layer final : public layer_info {
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = input_type_s<NN, I>::type;
using input_type = typename input_type_s<NN, I>::type;
static_assert(detail::is_dense_v<input_type>);
using output_type = input_type;
@@ -188,7 +203,7 @@ private:
template <typename Error>
auto backward(const input_type& input, gradient& grad, Error prevErr) const {
return prevErr.hadamard(output.hadamard(static_cast<output_type::inner_type>(1) - output));
return prevErr.hadamard(output.hadamard(static_cast<typename output_type::inner_type>(1) - output));
}
void apply_grad(gradient&, double, double) {}
@@ -201,7 +216,7 @@ class relu_layer final : public layer_info {
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = input_type_s<NN, I>::type;
using input_type = typename input_type_s<NN, I>::type;
using output_type = input_type;
input_type savedInput;
@@ -244,7 +259,7 @@ class conv2d_layer final : public layer_info {
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = input_type_s<NN, I>::type;
using input_type = typename input_type_s<NN, I>::type;
static_assert(detail::is_tensor_v<input_type>);
static_assert(input_type::RANK == 3);
@@ -425,7 +440,7 @@ class maxpool_layer : public layer_info {
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = input_type_s<NN, I>::type;
using input_type = typename input_type_s<NN, I>::type;
static_assert(detail::is_tensor_v<input_type>);
static_assert(std::is_same_v<typename input_type::inner_type, T>);
static_assert(input_type::RANK == 3);
@@ -515,7 +530,7 @@ class flatten_layer : public layer_info {
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = input_type_s<NN, I>::type;
using input_type = typename input_type_s<NN, I>::type;
static_assert(detail::is_tensor_v<input_type>);
static constexpr std::size_t FEATURES = input_type::SIZE;
@@ -552,6 +567,44 @@ private:
};
};
template <std::size_t VocabSize, std::size_t EmbedDims>
class tok2vec_layer : public layer_info {
template <typename Input, typename... Layers>
friend class network;
private:
template <class NN, std::size_t I>
struct inner final {
using input_type = typename input_type_s<NN, I>::type;
static_assert(detail::is_tokens_v<input_type>);
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
output_type output;
struct gradient final {
void reset() {}
};
void dump(std::ostream& os) const {}
void load(std::istream& is) {}
template <typename Dist>
void fill_random(const Dist& dist) {}
auto& forward(const input_type& input) {
output.set_dimensions(input.size(), EmbedDims);
return output;
}
template <typename Error>
auto backward(const input_type& input, gradient& grad, Error prevErr) {
return std::move(prevErr);
}
void apply_grad(gradient& grad, double divisor, double rate) {}
};
};
} // namespace cenet
#endif // CENET_LAYER_HPP
+104 -16
View File
@@ -13,29 +13,122 @@
namespace cenet {
namespace math {
namespace detail {
struct empty_shape {};
template <std::size_t Rank>
struct dynamic_shape {
dynamic_shape() = default;
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= Rank)>>
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
constexpr std::size_t ext_offset = ExtRank - Rank;
for (std::size_t i = 0; i < Rank; ++i) {
dimensions[i] = oth.dimensions[ext_offset + i];
}
}
std::size_t dimensions[Rank]{};
};
} // namespace detail
template <typename T, std::size_t... Dims>
class tensor_view {
template <typename, std::size_t, std::size_t...>
static_assert(std::is_arithmetic_v<T>);
template <typename, std::size_t...>
friend class tensor;
template <typename, std::size_t...>
friend class tensor_view;
public:
using traits = tensor_traits<tensor<T, Dims...>>;
using value_type = traits::value_type;
using value_type = T;
public:
template <typename... Offs,
typename = std::enable_if_t<(
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
auto slice(Offs... offs) {
auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
return inner_view<T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
}
template <typename... Offs,
typename = std::enable_if_t<(
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
auto slice(Offs... offs) const {
const auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
return inner_view<const T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
}
public:
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
auto operator[](Off off) {
return slice(off);
}
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
auto operator[](Off off) const {
return slice(off);
}
private:
template <typename... Offs>
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
return calculate_offset_impl<sizeof...(Offs) - 1, sizeof...(Dims) - sizeof...(Offs)>(offs);
}
template <std::size_t I, std::size_t Start, typename... Offs>
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
if constexpr (I == 0)
return std::get<I>(offs) * dimension<Start + I>();
else
return (calculate_offset_impl<I - 1, Start>(offs) + std::get<I>(offs)) * dimension<Start + I>();
}
private:
template <typename InT, std::size_t Off, std::size_t First, std::size_t... Rest>
auto inner_view(value_type* view, std::index_sequence<First, Rest...>) {
if constexpr (Off == 0)
return tensor_view<InT, First, Rest...>{ view, m_shape };
else
return inner_view<InT, Off - 1>(view, std::index_sequence<Rest...>{});
}
private:
template <std::size_t I>
auto dimension() const {
if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) {
return m_shape.dimensions[I];
} else {
return traits::dimensions[I];
}
}
private:
using shape_type = std::conditional_t<traits::dynamic, detail::dynamic_shape<traits::rank>, detail::empty_shape>;
template <typename Fwd>
tensor_view(value_type* view, Fwd&& shape)
: m_view(view), m_shape(std::forward<Fwd>(shape)) {}
private:
value_type* m_view;
std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> m_strides;
shape_type m_shape;
};
template <typename T, std::size_t... Dims>
class tensor {
public:
using traits = tensor_traits<tensor>;
using value_type = traits::value_type;
using traits = tensor_traits<tensor>;
using value_type = typename traits::value_type;
using constant_value_type = const value_type;
using view_type = tensor_view<value_type, Dims...>;
using constant_view_type = tensor_view<constant_value_type, Dims...>;
public:
tensor() {
if constexpr (traits::dynamic) {
m_items = nullptr;
m_shape = {};
} else if constexpr (traits::inline_storage) {
std::memset(m_items, 0, sizeof(m_items));
} else {
@@ -82,6 +175,9 @@ public:
return *this;
}
public:
view_type view() { return { m_items, m_shape }; }
constant_view_type view() const { return { m_items, m_shape }; }
public:
template <typename... RtDims,
typename = std::enable_if_t<(
@@ -128,18 +224,10 @@ private:
}
}
private:
struct empty_shape {};
template <std::size_t Rank>
struct dynamic_shape {
std::size_t dimensions[Rank];
std::size_t strides[Rank];
};
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<traits::rank>, empty_shape>;
using shape_type = std::conditional_t<traits::dynamic, detail::dynamic_shape<traits::rank>, detail::empty_shape>;
private:
traits::storage_type m_items;
shape_type m_shape;
typename traits::storage_type m_items;
shape_type m_shape;
};
template <typename T, std::size_t M, std::size_t N>
+2 -2
View File
@@ -66,8 +66,8 @@ struct tensor_traits {};
template <typename T, std::size_t... Dims>
struct tensor_traits<tensor<T, Dims...>> {
using tensor_type = tensor<T, Dims...>;
using value_type = tensor_storage<tensor_type>::value_type;
using storage_type = tensor_storage<tensor_type>::storage_type;
using value_type = typename tensor_storage<tensor_type>::value_type;
using storage_type = typename tensor_storage<tensor_type>::storage_type;
static constexpr auto rank = sizeof...(Dims);
static constexpr auto dynamic = is_tensor_dynamic_v<tensor_type>;
+2 -2
View File
@@ -38,7 +38,7 @@ private:
}
public:
using input_type = Input;
using output_type = output_layer_type::output_type;
using output_type = typename output_layer_type::output_type;
public:
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
public:
@@ -103,7 +103,7 @@ public:
private:
std::size_t m_count = 0;
gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
typename gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
};
public:
void backprop(const input_type& input, const output_type& expected, learning_state& state) {