diff --git a/include/cenet/layer.hpp b/include/cenet/layer.hpp index a271aba..26cb855 100644 --- a/include/cenet/layer.hpp +++ b/include/cenet/layer.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -577,31 +578,99 @@ private: using input_type = typename input_type_s::type; static_assert(detail::is_tokens_v); - using output_type = math::matrix; + static_assert(VocabSize > 0); + static_assert(EmbedDims > 0); + + using output_type = math::matrix; + using embedding_type = math::matrix; + output_type output; + embedding_type embedding; + struct gradient final { - void reset() {} + void reset() { embedding.view().fill(0); } + + embedding_type embedding; + embedding_type momentum; + embedding_type variance; + + std::size_t time = 0; }; - void dump(std::ostream& os) const {} + void dump(std::ostream& os) const { + os.write(reinterpret_cast(embedding.data()), sizeof(float) * embedding.size()); + } - void load(std::istream& is) {} + void load(std::istream& is) { + is.read(reinterpret_cast(embedding.data()), sizeof(float) * embedding.size()); + } template - void fill_random(const Dist& dist) {} + void fill_random(const Dist& dist) { + static std::random_device s_rd; + static std::mt19937 s_rng(s_rd()); + + auto distribution = dist; + + const float scale = 1.0F / std::sqrt(static_cast(EmbedDims)); + + for (std::size_t i = 0; i < embedding.size(); ++i) { + embedding.data()[i] = static_cast(distribution(s_rng)) * scale; + } + } auto& forward(const input_type& input) { output.set_dimensions(input.size(), EmbedDims); + for (std::size_t i = 0; i < input.size(); ++i) { + if (input[i] >= VocabSize) throw std::out_of_range("invalid token!"); + output.view()[i].fill(embedding.view()[input[i]]); + } return output; } template - auto backward(const input_type& input, gradient& grad, Error prevErr) { - return std::move(prevErr); + void backward(const input_type& input, gradient& grad, Error prevErr) { + const auto errorShape = prevErr.shape(); + if (errorShape.template get<0>() != input.size() || errorShape.template get<1>() != EmbedDims) { + throw std::invalid_argument("unexpected embedding gradient shape"); + } + + for (std::size_t i = 0; i < input.size(); ++i) { + grad.embedding.view()[input[i]] += prevErr.view()[i]; + } } - void apply_grad(gradient& grad, double divisor, double rate) {} + void apply_grad(gradient& grad, double divisor, double rate) { + if (divisor <= 0.0) return; + + ++grad.time; + + constexpr double beta1 = 0.9; + constexpr double beta2 = 0.999; + constexpr double epsilon = 1e-8; + + const double correction1 = 1.0 - std::pow(beta1, grad.time); + const double correction2 = 1.0 - std::pow(beta2, grad.time); + + float* weights = embedding.data(); + float* momentum = grad.momentum.data(); + float* variance = grad.variance.data(); + + for (std::size_t i = 0; i < embedding.size(); ++i) { + const double avgGrad = static_cast(grad.embedding.coeff(i)) / divisor; + + grad.momentum.coeff(i) = static_cast(beta1 * grad.momentum.coeff(i) + (1.0 - beta1) * avgGrad); + grad.variance.coeff(i) = + static_cast(beta2 * grad.variance.coeff(i) + (1.0 - beta2) * avgGrad * avgGrad); + + const double momentumCorrected = grad.momentum.coeff(i) / correction1; + const double varianceCorrected = grad.variance.coeff(i) / correction2; + + embedding.coeff(i) -= + static_cast(rate * momentumCorrected / (std::sqrt(varianceCorrected) + epsilon)); + } + } }; };