#include <draft_table.h>
Definition at line 27 of file draft_table.h.
◆ draft()
| std::vector< int > ember_draft::DraftTable::draft |
( |
const std::vector< int > & |
h, |
|
|
int |
k, |
|
|
double |
theta |
|
) |
| const |
|
inline |
Definition at line 82 of file draft_table.h.
82 {
83 std::vector<int> d;
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());
97 }
98 return d;
99 }
bool lookup(const std::vector< int > &ctx, int *tok, double *c) const
std::vector< Order > tabs_
const float * p[kIrDepthSourcesMax]
References c, j, k, lookup(), orders(), p, tabs_, and tok.
Referenced by tinyllama::TinyLlamaSession::generate().
◆ entries()
| size_t ember_draft::DraftTable::entries |
( |
| ) |
const |
|
inline |
◆ fail()
| static bool ember_draft::DraftTable::fail |
( |
std::string * |
why, |
|
|
const std::string & |
w |
|
) |
| |
|
inlinestaticprivate |
◆ load()
| bool ember_draft::DraftTable::load |
( |
const std::string & |
path, |
|
|
int |
vocab_size, |
|
|
std::string * |
why |
|
) |
| |
|
inline |
Definition at line 31 of file draft_table.h.
31 {
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");
66 }
67 if (
p !=
buf_.size())
return fail(
why,
"trailing bytes");
70 return true;
71 }
static bool fail(std::string *why, const std::string &w)
References buf_, fail(), j, n, orders(), p, tabs_, and why.
Referenced by tinyllama::corpus_draft_table().
◆ lookup()
Definition at line 119 of file draft_table.h.
119 {
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;
125 bool ok = true;
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);
130 }
131 if (!ok) continue;
134 for (
int j = 4;
j <
o; ++
j)
135 key =
mix64(key ^ ((
static_cast<uint64_t>(ctx[len - 1 -
j]) + 1) * 0x9E3779B97F4A7C15ULL));
136 key >>= 16;
137 }
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;
143 return true;
144 }
145 }
146 return false;
147 }
static uint64_t mix64(uint64_t x)
References c, j, mix64(), orders(), tabs_, tok, and v.
Referenced by draft().
◆ mix64()
Definition at line 101 of file draft_table.h.
101 {
103 x *= 0xBF58476D1CE4E5B9ULL;
105 x *= 0x94D049BB133111EBULL;
108 }
References j.
Referenced by lookup().
◆ orders()
| int ember_draft::DraftTable::orders |
( |
| ) |
const |
|
inline |
◆ buf_
| std::vector<char> ember_draft::DraftTable::buf_ |
|
private |
◆ tabs_
| std::vector<Order> ember_draft::DraftTable::tabs_ |
|
private |
The documentation for this class was generated from the following file: