diff --git a/include/cenet/layer.hpp b/include/cenet/layer.hpp index 6a8044c..a271aba 100644 --- a/include/cenet/layer.hpp +++ b/include/cenet/layer.hpp @@ -2,13 +2,16 @@ #define CENET_LAYER_HPP #include "mat.hpp" +#include "math/tensor.hpp" #include "tensor.hpp" +#include #include #include #include #include #include +#include namespace cenet { @@ -32,13 +35,25 @@ struct is_tensor> : std::true_type {}; template static constexpr auto is_tensor_v = is_tensor::value; +template +struct is_tokens : std::false_type {}; + +template +struct is_tokens> : std::true_type {}; + +template <> +struct is_tokens> : std::true_type {}; + +template +static constexpr auto is_tokens_v = is_tokens::value; + } // namespace detail struct layer_info { protected: template struct input_type_s { - using type = std::tuple_element_t::output_type; + using type = typename std::tuple_element_t::output_type; }; template @@ -57,7 +72,7 @@ class dense_layer final : public layer_info { private: template struct inner final { - using input_type = input_type_s::type; + using input_type = typename input_type_s::type; static_assert(detail::is_dense_v); using output_type = mat<1, Features, T>; @@ -161,7 +176,7 @@ class sigmoid_layer final : public layer_info { private: template struct inner final { - using input_type = input_type_s::type; + using input_type = typename input_type_s::type; static_assert(detail::is_dense_v); using output_type = input_type; @@ -188,7 +203,7 @@ private: template auto backward(const input_type& input, gradient& grad, Error prevErr) const { - return prevErr.hadamard(output.hadamard(static_cast(1) - output)); + return prevErr.hadamard(output.hadamard(static_cast(1) - output)); } void apply_grad(gradient&, double, double) {} @@ -201,7 +216,7 @@ class relu_layer final : public layer_info { private: template struct inner final { - using input_type = input_type_s::type; + using input_type = typename input_type_s::type; using output_type = input_type; input_type savedInput; @@ -244,7 +259,7 @@ class conv2d_layer final : public layer_info { private: template struct inner final { - using input_type = input_type_s::type; + using input_type = typename input_type_s::type; static_assert(detail::is_tensor_v); static_assert(input_type::RANK == 3); @@ -425,7 +440,7 @@ class maxpool_layer : public layer_info { private: template struct inner final { - using input_type = input_type_s::type; + using input_type = typename input_type_s::type; static_assert(detail::is_tensor_v); static_assert(std::is_same_v); static_assert(input_type::RANK == 3); @@ -515,7 +530,7 @@ class flatten_layer : public layer_info { private: template struct inner final { - using input_type = input_type_s::type; + using input_type = typename input_type_s::type; static_assert(detail::is_tensor_v); static constexpr std::size_t FEATURES = input_type::SIZE; @@ -552,6 +567,44 @@ private: }; }; +template +class tok2vec_layer : public layer_info { + template + friend class network; +private: + template + struct inner final { + using input_type = typename input_type_s::type; + static_assert(detail::is_tokens_v); + + using output_type = math::matrix; + output_type output; + + struct gradient final { + void reset() {} + }; + + void dump(std::ostream& os) const {} + + void load(std::istream& is) {} + + template + void fill_random(const Dist& dist) {} + + auto& forward(const input_type& input) { + output.set_dimensions(input.size(), EmbedDims); + return output; + } + + template + 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 diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index e43e987..11c458d 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -13,29 +13,122 @@ namespace cenet { namespace math { +namespace detail { + +struct empty_shape {}; + +template +struct dynamic_shape { + dynamic_shape() = default; + + template = Rank)>> + explicit dynamic_shape(const dynamic_shape& 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 class tensor_view { - template + static_assert(std::is_arithmetic_v); + + template friend class tensor; + + template + friend class tensor_view; public: using traits = tensor_traits>; - using value_type = traits::value_type; + using value_type = T; public: + template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> + 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(ptr, std::index_sequence{}); + } + + template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> + 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(ptr, std::index_sequence{}); + } +public: + template >> + auto operator[](Off off) { + return slice(off); + } + + template >> + auto operator[](Off off) const { + return slice(off); + } +private: + template + std::size_t calculate_offset(const std::tuple& offs) const { + return calculate_offset_impl(offs); + } + + template + std::size_t calculate_offset_impl(const std::tuple& offs) const { + if constexpr (I == 0) + return std::get(offs) * dimension(); + else + return (calculate_offset_impl(offs) + std::get(offs)) * dimension(); + } +private: + template + auto inner_view(value_type* view, std::index_sequence) { + if constexpr (Off == 0) + return tensor_view{ view, m_shape }; + else + return inner_view(view, std::index_sequence{}); + } +private: + template + 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, detail::empty_shape>; + + template + tensor_view(value_type* view, Fwd&& shape) + : m_view(view), m_shape(std::forward(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 class tensor { public: - using traits = tensor_traits; - using value_type = traits::value_type; + using traits = tensor_traits; + using value_type = typename traits::value_type; + using constant_value_type = const value_type; + using view_type = tensor_view; + using constant_view_type = tensor_view; 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 - struct dynamic_shape { - std::size_t dimensions[Rank]; - std::size_t strides[Rank]; - }; - - using shape_type = std::conditional_t, empty_shape>; + using shape_type = std::conditional_t, detail::empty_shape>; private: - traits::storage_type m_items; - shape_type m_shape; + typename traits::storage_type m_items; + shape_type m_shape; }; template diff --git a/include/cenet/math/tensor_traits.hpp b/include/cenet/math/tensor_traits.hpp index 2cd1d5d..40765d8 100644 --- a/include/cenet/math/tensor_traits.hpp +++ b/include/cenet/math/tensor_traits.hpp @@ -66,8 +66,8 @@ struct tensor_traits {}; template struct tensor_traits> { using tensor_type = tensor; - using value_type = tensor_storage::value_type; - using storage_type = tensor_storage::storage_type; + using value_type = typename tensor_storage::value_type; + using storage_type = typename tensor_storage::storage_type; static constexpr auto rank = sizeof...(Dims); static constexpr auto dynamic = is_tensor_dynamic_v; diff --git a/include/cenet/network.hpp b/include/cenet/network.hpp index 0db7fa4..d78c593 100644 --- a/include/cenet/network.hpp +++ b/include/cenet/network.hpp @@ -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().output; } public: @@ -103,7 +103,7 @@ public: private: std::size_t m_count = 0; - gen>::type m_layers; + typename gen>::type m_layers; }; public: void backprop(const input_type& input, const output_type& expected, learning_state& state) { diff --git a/mnist-conv.nn b/mnist-conv.nn index 2669f58..9b81e0d 100644 Binary files a/mnist-conv.nn and b/mnist-conv.nn differ