feat(nn): implement tok2vec layer
This commit is contained in:
+77
-8
@@ -9,6 +9,7 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -577,31 +578,99 @@ private:
|
|||||||
using input_type = typename input_type_s<NN, I>::type;
|
using input_type = typename input_type_s<NN, I>::type;
|
||||||
static_assert(detail::is_tokens_v<input_type>);
|
static_assert(detail::is_tokens_v<input_type>);
|
||||||
|
|
||||||
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
|
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>;
|
||||||
|
|
||||||
output_type output;
|
output_type output;
|
||||||
|
|
||||||
|
embedding_type embedding;
|
||||||
|
|
||||||
struct gradient final {
|
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<const char*>(embedding.data()), sizeof(float) * embedding.size());
|
||||||
|
}
|
||||||
|
|
||||||
void load(std::istream& is) {}
|
void load(std::istream& is) {
|
||||||
|
is.read(reinterpret_cast<char*>(embedding.data()), sizeof(float) * embedding.size());
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Dist>
|
template <typename Dist>
|
||||||
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<float>(EmbedDims));
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < embedding.size(); ++i) {
|
||||||
|
embedding.data()[i] = static_cast<float>(distribution(s_rng)) * scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto& forward(const input_type& input) {
|
auto& forward(const input_type& input) {
|
||||||
output.set_dimensions(input.size(), EmbedDims);
|
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;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Error>
|
template <typename Error>
|
||||||
auto backward(const input_type& input, gradient& grad, Error prevErr) {
|
void backward(const input_type& input, gradient& grad, Error prevErr) {
|
||||||
return std::move(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<double>(grad.embedding.coeff(i)) / divisor;
|
||||||
|
|
||||||
|
grad.momentum.coeff(i) = static_cast<float>(beta1 * grad.momentum.coeff(i) + (1.0 - beta1) * avgGrad);
|
||||||
|
grad.variance.coeff(i) =
|
||||||
|
static_cast<float>(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<float>(rate * momentumCorrected / (std::sqrt(varianceCorrected) + epsilon));
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user