ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
ember_draft::DraftTable Class Reference

#include <draft_table.h>

Collaboration diagram for ember_draft::DraftTable:
Collaboration graph

Classes

struct  Order
 

Public Member Functions

bool load (const std::string &path, int vocab_size, std::string *why)
 
int orders () const
 
size_t entries () const
 
std::vector< int > draft (const std::vector< int > &h, int k, double theta) const
 

Static Public Member Functions

static uint64_t mix64 (uint64_t x)
 

Private Member Functions

bool lookup (const std::vector< int > &ctx, int *tok, double *c) const
 

Static Private Member Functions

static bool fail (std::string *why, const std::string &w)
 

Private Attributes

std::vector< char > buf_
 
std::vector< Order > tabs_
 

Detailed Description

Definition at line 27 of file draft_table.h.

Member Function Documentation

◆ 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;
85 const int no = orders();
86 std::vector<int> ctx(h.end() - std::min<size_t>(h.size(), static_cast<size_t>(no)), h.end());
87 double p = 1.0;
88 while (static_cast<int>(d.size()) < k) {
89 int tok = -1;
90 double c = 0.0;
91 if (!lookup(ctx, &tok, &c)) break;
92 p *= c;
93 if (p < theta) break;
94 d.push_back(tok);
95 ctx.push_back(tok);
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]
Tok tok
float c
size_t j

References c, j, k, lookup(), orders(), p, tabs_, and tok.

Referenced by tinyllama::TinyLlamaSession::generate().

◆ entries()

size_t ember_draft::DraftTable::entries ( ) const
inline

Definition at line 75 of file draft_table.h.

75 {
76 size_t s = 0;
77 for (const auto& t : tabs_) s += t.n;
78 return s;
79 }
int n

References j, and tabs_.

Referenced by tinyllama::corpus_draft_table().

◆ fail()

static bool ember_draft::DraftTable::fail ( std::string *  why,
const std::string &  w 
)
inlinestaticprivate

Definition at line 149 of file draft_table.h.

149 {
150 if (why) *why = w;
151 return false;
152 }
std::string why
Definition graph_ir.cpp:280

References j, and why.

Referenced by load().

◆ 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);
33 if (!f) return fail(why, "cannot open " + path);
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");
36 uint32_t vocab = 0, orders = 0;
37 std::memcpy(&vocab, buf_.data() + 8, 4);
38 std::memcpy(&orders, buf_.data() + 12, 4);
39 if (orders < 1 || orders > 8) return fail(why, "orders must be 1..8");
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");
45 std::vector<uint64_t> ns(orders);
46 std::memcpy(ns.data(), buf_.data() + 16, 8 * orders);
47 tabs_.assign(orders, Order{});
48 auto pad8 = [](size_t b) { return (b + 7) / 8 * 8; };
49 for (uint32_t o = 0; o < orders; ++o) {
50 const size_t n = ns[o];
51 const size_t need = pad8(8 * n) + pad8(2 * n) + pad8(n);
52 if (buf_.size() - p < need) return fail(why, "truncated tables");
53 tabs_[o].n = n;
54 tabs_[o].keys.resize(n);
55 tabs_[o].next.resize(n);
56 tabs_[o].conf.resize(n);
57 std::memcpy(tabs_[o].keys.data(), buf_.data() + p, 8 * n);
58 p += pad8(8 * n);
59 std::memcpy(tabs_[o].next.data(), buf_.data() + p, 2 * n);
60 p += pad8(2 * n);
61 std::memcpy(tabs_[o].conf.data(), buf_.data() + p, n);
62 p += pad8(n);
63 for (size_t i = 0; i < n; ++i)
64 if (tabs_[o].next[i] >= vocab || (i > 0 && tabs_[o].keys[i] <= tabs_[o].keys[i - 1]))
65 return fail(why, "entries out of range or not ascending");
66 }
67 if (p != buf_.size()) return fail(why, "trailing bytes");
68 buf_.clear();
69 buf_.shrink_to_fit();
70 return true;
71 }
std::vector< char > buf_
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()

bool ember_draft::DraftTable::lookup ( const std::vector< int > &  ctx,
int *  tok,
double *  c 
) const
inlineprivate

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) {
122 const Order& t = tabs_[o - 1];
123 if (t.n == 0) continue;
124 uint64_t key = 0;
125 bool ok = true;
126 for (int j = 0; j < o; ++j) {
127 const int v = ctx[len - 1 - j];
128 if (v < 0 || v > 0xFFFF) { ok = false; break; }
129 if (j < 4) key |= static_cast<uint64_t>(v) << (16 * j);
130 }
131 if (!ok) continue;
132 if (o >= 4) {
133 key = mix64(key);
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());
141 *tok = t.next[i];
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)
std::vector< float > v

References c, j, mix64(), orders(), tabs_, tok, and v.

Referenced by draft().

◆ mix64()

static uint64_t ember_draft::DraftTable::mix64 ( uint64_t  x)
inlinestatic

Definition at line 101 of file draft_table.h.

101 {
102 x ^= x >> 30;
103 x *= 0xBF58476D1CE4E5B9ULL;
104 x ^= x >> 27;
105 x *= 0x94D049BB133111EBULL;
106 x ^= x >> 31;
107 return x;
108 }

References j.

Referenced by lookup().

◆ orders()

int ember_draft::DraftTable::orders ( ) const
inline

Definition at line 73 of file draft_table.h.

73{ return static_cast<int>(tabs_.size()); }

References tabs_.

Referenced by tinyllama::corpus_draft_table(), draft(), load(), and lookup().

Member Data Documentation

◆ buf_

std::vector<char> ember_draft::DraftTable::buf_
private

Definition at line 154 of file draft_table.h.

Referenced by load().

◆ tabs_

std::vector<Order> ember_draft::DraftTable::tabs_
private

Definition at line 155 of file draft_table.h.

Referenced by draft(), entries(), load(), lookup(), and orders().


The documentation for this class was generated from the following file: