31 bool load(
const std::string&
path,
int vocab_size, std::string*
why) {
32 std::ifstream
f(
path, std::ios::binary);
34 buf_.assign(std::istreambuf_iterator<char>(
f), std::istreambuf_iterator<char>());
35 if (
buf_.size() < 16 || std::memcmp(
buf_.data(),
"EMBERDT1", 8) != 0)
return fail(
why,
"not a draft table");
37 std::memcpy(&vocab,
buf_.data() + 8, 4);
40 if (
static_cast<int>(vocab) != vocab_size)
41 return fail(
why,
"built for vocabulary size " + std::to_string(vocab) +
", the model has " +
42 std::to_string(vocab_size));
43 size_t p = 16 + 8 *
static_cast<size_t>(
orders);
44 if (
buf_.size() <
p)
return fail(
why,
"truncated header");
48 auto pad8 = [](
size_t b) {
return (
b + 7) / 8 * 8; };
50 const size_t n =
ns[
o];
57 std::memcpy(
tabs_[
o].keys.data(),
buf_.data() +
p, 8 *
n);
59 std::memcpy(
tabs_[
o].next.data(),
buf_.data() +
p, 2 *
n);
63 for (
size_t i = 0;
i <
n; ++
i)
65 return fail(
why,
"entries out of range or not ascending");
67 if (
p !=
buf_.size())
return fail(
why,
"trailing bytes");
77 for (
const auto&
t :
tabs_)
s +=
t.n;
82 std::vector<int>
draft(
const std::vector<int>&
h,
int k,
double theta)
const {
84 if (
tabs_.empty() ||
k <= 0)
return d;
86 std::vector<int> ctx(
h.end() - std::min<size_t>(
h.size(),
static_cast<size_t>(
no)),
h.end());
88 while (
static_cast<int>(d.size()) <
k) {
96 if (
static_cast<int>(ctx.size()) >
no) ctx.erase(ctx.begin());
103 x *= 0xBF58476D1CE4E5B9ULL;
105 x *= 0x94D049BB133111EBULL;
119 bool lookup(
const std::vector<int>& ctx,
int*
tok,
double*
c)
const {
120 const int len =
static_cast<int>(ctx.size());
121 for (
int o = std::min(
orders(), len);
o >= 1; --
o) {
123 if (
t.n == 0)
continue;
126 for (
int j = 0;
j <
o; ++
j) {
127 const int v = ctx[len - 1 -
j];
129 if (
j < 4) key |=
static_cast<uint64_t>(
v) << (16 *
j);
134 for (
int j = 4;
j <
o; ++
j)
135 key =
mix64(key ^ ((
static_cast<uint64_t>(ctx[len - 1 -
j]) + 1) * 0x9E3779B97F4A7C15ULL));
138 const auto it = std::lower_bound(
t.keys.begin(),
t.keys.end(), key);
139 if (
it !=
t.keys.end() && *
it == key) {
140 const size_t i =
static_cast<size_t>(
it -
t.keys.begin());
142 *
c =
static_cast<double>(
t.conf[
i]) / 255.0;
149 static bool fail(std::string*
why,
const std::string&
w) {
bool lookup(const std::vector< int > &ctx, int *tok, double *c) const
bool load(const std::string &path, int vocab_size, std::string *why)
std::vector< int > draft(const std::vector< int > &h, int k, double theta) const
std::vector< Order > tabs_
static uint64_t mix64(uint64_t x)
static bool fail(std::string *why, const std::string &w)
const float * p[kIrDepthSourcesMax]
std::vector< uint16_t > next
std::vector< uint64_t > keys
std::vector< uint8_t > conf