Archived
sched: --draft-suffix on, SuffixDecoding as a step selector on the MTP chain
After each commit the scheduler looks in the sequence's own prompt and output for the latest earlier occurrence of its last --draft-suffix-min-match tokens; the next step then verifies what followed it (up to --draft-suffix-max rows, cut where the product of the follower frequencies falls under --draft-suffix-min-score) in place of the head's draft. The verify window becomes the larger of the two while the head keeps its own rounds. A sequence whose history ended in a recurring pattern is not issued ahead, since the step is built after the commit where the lookup can read the committed end. It works with the fixed MTP chain, with --draft-depth adaptive and with --draft-verify traversal. A suffix chain carries no coupled row (Request::draft_suffix): the sampler binds none of the head's staged states to its rows, so each row is the target's plain draw and the rows are leaves of the traversal walk; mtp_rrs_test checks the law with a negative control that fails. --draft-verify block is refused. The window is at most 7 rows on Flash-Next (the delta-net fold's register window). On Flash-Next IQR-V4 (TP2) on our branch, window 7 on chain 3 with coupling: +1.4% overall, +2.9% on repetitive tasks, +11% on quoting, free chat unchanged; beside adaptive depth +0.2% [-1.0, +1.5] overall, code +1.0%. Off by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e78d484e60
commit
b8bca7885a
22 files changed
+1112
-9
No files matched your search
@@ -62,6 +62,10 @@ struct StepEntry {
|
||||
* acceptance but not the adaptive window's estimate of the draft head (DraftController,
|
||||
* draft.h). */
|
||||
bool ngram = false;
|
||||
/* The drafts are a SuffixDecoding proposal (--draft-suffix, suffix.h): a chain of the
|
||||
* sequence's own history, verified with no coupled row (Request::draft_suffix). Implies
|
||||
* `ngram`. */
|
||||
bool suffix = false;
|
||||
/* Device row: the card overwrites its tokens, positions, rollback count and slots from the
|
||||
* StepSlot before the pass runs (advance.h); `ctx_len` is then only an upper bound for the
|
||||
* host-side ceilings. dev_tok/dev_out/dev_aux are advance.h's row descriptor: the verify
|
||||
|
||||
@@ -128,14 +128,14 @@ double DraftController::yield(const DraftAdapt& s, int d) const {
|
||||
}
|
||||
|
||||
int DraftController::window_of(const DraftAdapt& s) const {
|
||||
if (!adapt_) return p_.max_depth;
|
||||
if (!adapt_) return next_depth();
|
||||
if (s.depth <= 0) return 0;
|
||||
const int D = step_d_ > 0 ? step_d_ : p_.max_depth;
|
||||
return s.depth < D ? s.depth : D;
|
||||
}
|
||||
|
||||
int DraftController::window_for(DraftAdapt& s) const {
|
||||
if (!adapt_) return p_.max_depth;
|
||||
if (!adapt_) return next_depth();
|
||||
if (s.depth <= 0) start(s);
|
||||
return window_of(s);
|
||||
}
|
||||
|
||||
+5
-2
@@ -60,6 +60,9 @@ class DraftController {
|
||||
public:
|
||||
struct Params {
|
||||
int max_depth = 0; /* the deployment's window; 0 disables speculation entirely */
|
||||
/* The head's own rounds when the window is wider than they are (--draft-suffix: a suffix
|
||||
* step verifies more rows than the head drafts); 0 = max_depth. */
|
||||
int head_depth = 0;
|
||||
/* The adaptive policy: `--draft-window adaptive` with a serial drafter, decided by
|
||||
* Scheduler::init. Off, the depth is max_depth. */
|
||||
bool adapt = false;
|
||||
@@ -114,7 +117,7 @@ public:
|
||||
|
||||
/* The draft depth for the next step; 0 means do not speculate. Constant: under the adaptive
|
||||
* policy it is the ceiling, and window_for() is the window. */
|
||||
int next_depth() const { return p_.max_depth; }
|
||||
int next_depth() const { return p_.head_depth > 0 ? p_.head_depth : p_.max_depth; }
|
||||
|
||||
/* ---------------------------------------------------------- the adaptive policy */
|
||||
bool adaptive() const { return adapt_; }
|
||||
@@ -146,7 +149,7 @@ public:
|
||||
/* The deepest window a step at depth D carries: D capped at the deepest stop it was chosen
|
||||
* over; the ceiling before the first choice or with the policy off. Proposals from outside
|
||||
* the head (prompt lookup) are held to it, so they add no deeper pass shape. */
|
||||
int step_window() const { return adapt_ && step_w_ > 0 ? step_w_ : p_.max_depth; }
|
||||
int step_window() const { return adapt_ && step_w_ > 0 ? step_w_ : next_depth(); }
|
||||
|
||||
/* T(R): a drafting decode step of `q_rows` query rows (every sequence's verified token and
|
||||
* its drafts) took `ms` since the end of the previous step, the head's rounds included. Fed
|
||||
|
||||
@@ -164,6 +164,13 @@ struct SchedMetrics {
|
||||
int64_t lookup_held = 0;
|
||||
int64_t lookup_held_drafts = 0;
|
||||
int64_t lookup_held_hits = 0;
|
||||
/* --draft-suffix (suffix.h): verifies that carried a suffix chain, the rows they carried and the
|
||||
* drafts accepted; decode steps not issued ahead because the sequence was in a recurring
|
||||
* pattern (where the lookup could run after the commit instead). */
|
||||
int64_t suffix_steps = 0;
|
||||
int64_t suffix_rows = 0;
|
||||
int64_t suffix_accepted = 0;
|
||||
int64_t suffix_held_ahead = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -215,6 +215,7 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
|
||||
DraftController::Params dp;
|
||||
dp.max_depth = cfg.n_spec;
|
||||
dp.head_depth = cfg.n_spec_head > 0 && cfg.n_spec_head < cfg.n_spec ? cfg.n_spec_head : 0;
|
||||
/* The adaptive window (draft.h) prices a draft by the measured cost of the step it adds to.
|
||||
* A block drafter proposes its block in one pass, but each verified draft is still a verify
|
||||
* row, which is not free on a MoE that reads each row's routed experts. So it takes the same
|
||||
@@ -749,6 +750,7 @@ void Scheduler::close_request(SchedReq& s, ReqState st, const char* reason, bool
|
||||
if (cfg_.prefix_cache && pc_) pc_->release(*s.req);
|
||||
/* Prompt lookup's index is this text's alone. */
|
||||
s.ngram_ix.reset();
|
||||
s.suffix_ix.reset();
|
||||
if (lookup_gate_on_ && s.look.started && s.look.proposed > 0)
|
||||
RAD_DEBUG("prompt lookup: request %llu made %lld proposals, held back %lld (%lld drafts, "
|
||||
"%lld of those scored came true); a %d-draft verify costs %.2f plain steps",
|
||||
@@ -1088,6 +1090,24 @@ int Scheduler::draft_ngram(SchedReq& s, int depth, int* match) const {
|
||||
LookupGate::kLongMatch);
|
||||
}
|
||||
|
||||
/* --draft-suffix: SuffixDecoding's chain for the sequence's committed history (suffix.h). */
|
||||
int Scheduler::suffix_propose(SchedReq& s, int32_t* out, int* match) {
|
||||
if (match) *match = -1;
|
||||
if (!s.req) return 0;
|
||||
const Request& r = *s.req;
|
||||
TokenSeq seq;
|
||||
seq.a = r.prompt.data();
|
||||
seq.na = (int64_t)r.prompt.size();
|
||||
seq.b = r.output.data();
|
||||
seq.nb = (int64_t)r.output.size();
|
||||
const SuffixIndex::Out o =
|
||||
s.suffix_ix.propose(seq, r.id, cfg_.draft_suffix_min_match,
|
||||
std::min(cfg_.draft_suffix_max, (int)RAD_SCHED_MAX_SPEC),
|
||||
cfg_.draft_suffix_min_score, out);
|
||||
if (match && o.n > 0) *match = o.match;
|
||||
return o.n;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ chunking */
|
||||
int64_t Scheduler::chunk_end(const Request& r, int64_t n_target) const {
|
||||
if (rswa_windows_.empty() || !kv_->has_runtime_window(r.id)) return n_target;
|
||||
@@ -2031,7 +2051,8 @@ const RadBatch* Scheduler::step() {
|
||||
const int64_t Q = std::max<int64_t>(geo_.quantum, 1);
|
||||
/* An eighth of this step's budget, which under a span is shared_tok for a shared step. */
|
||||
const int64_t share_tok = std::max<int64_t>(Q, budget / 8 / Q * Q);
|
||||
const int64_t dec_row_tok = 1 + std::max<int64_t>(depth, draft_.step_window());
|
||||
const int64_t dec_row_tok = 1 + std::max<int64_t>({ (int64_t)depth, (int64_t)draft_.step_window(),
|
||||
cfg_.draft_suffix ? (int64_t)cfg_.draft_suffix_max : 0 });
|
||||
int64_t behind_dec = 0, behind_pf = 0;
|
||||
if (share_step) {
|
||||
for (int32_t slot : running_) {
|
||||
@@ -2120,7 +2141,24 @@ const RadBatch* Scheduler::step() {
|
||||
* drafts this step and no deeper. */
|
||||
const int cap = draft_.adaptive() ? draft_cap(s) : depth;
|
||||
bool ngram = false;
|
||||
if (!r.may_speculate) s.n_draft = 0;
|
||||
bool suffix = false;
|
||||
/* --draft-suffix: a sequence whose own history ends in a recurring pattern verifies that
|
||||
* pattern's earlier continuation in place of the head's draft (the head still drafted
|
||||
* behind the step before, so a miss costs nothing but that draft). A function of the
|
||||
* committed text alone, so the choice never reads a draw; the rows it verifies carry no
|
||||
* coupled state (Request::draft_suffix below), so each is the target's plain draw. */
|
||||
if (cfg_.draft_suffix && r.may_speculate) {
|
||||
int32_t buf[RAD_SCHED_MAX_SPEC];
|
||||
const int np = suffix_propose(s, buf);
|
||||
if (np > 0) {
|
||||
std::copy(buf, buf + np, s.draft);
|
||||
s.n_draft = np;
|
||||
s.draft_tree = false;
|
||||
suffix = true;
|
||||
}
|
||||
}
|
||||
if (suffix) ngram = true;
|
||||
else if (!r.may_speculate) s.n_draft = 0;
|
||||
else if (s.n_draft == 0) {
|
||||
/* As the only drafter under the adaptive window, the lookup is the head: its
|
||||
* proposal is cut to the sequence's own window. */
|
||||
@@ -2148,6 +2186,7 @@ const RadBatch* Scheduler::step() {
|
||||
}
|
||||
if (!ngram && s.n_draft > cap) s.n_draft = cap;
|
||||
s.draft_ngram = ngram;
|
||||
s.draft_suffix = suffix;
|
||||
/* No verified position at or past the context end: nothing past ctx_limit_ - 1 has a
|
||||
* rotary row or block-table column. commit() finishes a sequence before it reaches
|
||||
* the limit, so the one token itself always fits. */
|
||||
@@ -2227,6 +2266,11 @@ const RadBatch* Scheduler::step() {
|
||||
e.n_draft = s.n_draft;
|
||||
e.is_prefill = prefill;
|
||||
e.ngram = !prefill && s.draft_ngram && s.n_draft > 0;
|
||||
e.suffix = e.ngram && s.draft_suffix;
|
||||
/* A suffix chain has no model node: the coupled sampler binds no state to any row of it
|
||||
* (core/sample/sampler.cpp), whatever stale state the head's own draft for these positions
|
||||
* left. */
|
||||
r.draft_suffix = e.suffix;
|
||||
/* A tree drafter's parents, unless prompt lookup replaced its proposal with a chain. */
|
||||
e.draft_parent = (!prefill && s.draft_tree && !e.ngram) ? s.draft_par : nullptr;
|
||||
r.draft_parent = e.n_spec ? e.draft_parent : nullptr;
|
||||
@@ -2676,6 +2720,10 @@ const RadBatch* Scheduler::step_next_locked() {
|
||||
* that cannot rewind decides at commit whether the next step may speculate; one that can,
|
||||
* or no grammar, keeps its answer. */
|
||||
if (!e.produces_token || !r.speculation_fixed) return nullptr;
|
||||
/* --draft-suffix: a sequence whose history ended in a recurring pattern at the last commit
|
||||
* is likely to continue it; its next step is built after the commit, where the lookup can
|
||||
* read the committed end (a step issued ahead is made on the card from the head's drafts). */
|
||||
if (cfg_.draft_suffix && s.suffix_hot) { ++m_.suffix_held_ahead; return nullptr; }
|
||||
/* A SCORED row (Request::token_scores, a /v1/systemone question) that the step in flight
|
||||
* brings to max_tokens gets no step issued ahead: it would run a whole forward pass for a
|
||||
* request that finishes at the commit, and max_tokens=1 pays a decode step after its
|
||||
@@ -2966,6 +3014,11 @@ int Scheduler::commit(StepResult& res) {
|
||||
draft_.yield(s.adapt, now), est);
|
||||
}
|
||||
}
|
||||
if (e.suffix) {
|
||||
++m_.suffix_steps;
|
||||
m_.suffix_rows += e.n_spec;
|
||||
m_.suffix_accepted += a;
|
||||
}
|
||||
m_.draft_tokens += e.n_spec;
|
||||
m_.draft_accepted += a;
|
||||
server::sink_drafts(r.sink, e.n_spec, a);
|
||||
@@ -2973,7 +3026,16 @@ int Scheduler::commit(StepResult& res) {
|
||||
}
|
||||
s.n_draft = 0;
|
||||
r.n_draft = 0;
|
||||
s.draft_suffix = false;
|
||||
r.draft_suffix = false;
|
||||
s.entry = -1;
|
||||
/* --draft-suffix: does the history now end in a pattern that recurred? (step_next_locked) */
|
||||
if (cfg_.draft_suffix && e.produces_token && r.state == ReqState::Running) {
|
||||
int32_t buf[RAD_SCHED_MAX_SPEC];
|
||||
s.suffix_hot = r.may_speculate && suffix_propose(s, buf) > 0;
|
||||
} else {
|
||||
s.suffix_hot = false;
|
||||
}
|
||||
|
||||
/* A request the next step carries finishes now but keeps its blocks, which that step
|
||||
* writes; its commit finds the request not running and returns them. */
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "lookup.h"
|
||||
#include "metrics.h"
|
||||
#include "ngram.h"
|
||||
#include "suffix.h"
|
||||
#include "diag/diagnose.h"
|
||||
|
||||
#include <chrono>
|
||||
@@ -82,6 +83,8 @@ struct SchedReq {
|
||||
void drafter_reset() {
|
||||
n_draft = 0;
|
||||
draft_next = 0;
|
||||
draft_suffix = false;
|
||||
suffix_hot = false;
|
||||
conv_cursor = 0;
|
||||
draft_filled = 0;
|
||||
draft_lo = 0;
|
||||
@@ -93,6 +96,13 @@ struct SchedReq {
|
||||
DraftAdapt adapt;
|
||||
/* The proposal the step being planned carries came from prompt lookup (StepEntry::ngram). */
|
||||
bool draft_ngram = false;
|
||||
/* --draft-suffix (suffix.h): the proposal the step being planned carries is a suffix chain
|
||||
* (StepEntry::suffix); and whether the last commit left the sequence's history ending in a
|
||||
* recurring pattern, which keeps the next step from being issued ahead (it is built after the
|
||||
* commit, where the lookup can run). */
|
||||
bool draft_suffix = false;
|
||||
bool suffix_hot = false;
|
||||
SuffixIndex suffix_ix;
|
||||
/* Prompt lookup's index over prompt ++ output (ngram.h), built at the sequence's first lookup
|
||||
* and extended by each later one. It depends on the tokens alone, so preemption keeps it; it
|
||||
* is keyed by request id, so a recycled slot starts over, and a closed request frees it. */
|
||||
@@ -651,6 +661,9 @@ private:
|
||||
* far the match behind the proposal runs back (NgramIndex::propose), -1 when the proposal did
|
||||
* not come from the index (--debug-draft-oracle). */
|
||||
int draft_ngram(SchedReq& s, int depth, int* match = nullptr) const;
|
||||
/* --draft-suffix: the suffix proposal for `s`'s next step, over its committed history, into
|
||||
* `out` (RAD_SCHED_MAX_SPEC entries). Returns its length, 0 for none. */
|
||||
int suffix_propose(SchedReq& s, int32_t* out, int* match = nullptr);
|
||||
/* Prompt lookup is the deployment's only drafter and --draft-window adaptive is on: its
|
||||
* proposals feed each sequence's estimate and are capped by its window, as a head's are. */
|
||||
bool lookup_adapt_ = false;
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
/* suffix.h -- SuffixDecoding's proposal for a hybrid step selector (--draft-suffix, DESIGN-suffix.md).
|
||||
*
|
||||
* SuffixDecoding (Oliaro et al., arXiv 2411.04975) drafts from a suffix tree of earlier text: for
|
||||
* the longest suffix of the current context that occurred before, it expands the tokens that
|
||||
* followed, each node scored by the product of its followers' frequencies (D, the expected number of
|
||||
* accepted tokens along its path). Here the tree is the per-request one (prompt ++ output of THIS
|
||||
* request; an offline study found a cross-request tree adds nothing on this workload), reduced to what
|
||||
* a chain needs:
|
||||
*
|
||||
* - the earlier occurrence comes from the existing NgramIndex (the --spec-ngram matcher): the
|
||||
* latest earlier occurrence of the longest recurring suffix of `min_match..min_match+3` tokens;
|
||||
* - the frequency rule comes from two count tables over `min_match`-token contexts: how often a
|
||||
* context was followed by anything, and by each token. A proposal token at depth j has
|
||||
* C_j = count(context_j + token_j) / count(context_j) with context_j the last `min_match`
|
||||
* tokens of the sequence extended by the proposal so far, and the chain is cut where its path
|
||||
* score D_j = prod C_i falls under `min_score`.
|
||||
*
|
||||
* It is a heuristic and only that: every proposed token is verified (the sampler's own draw must
|
||||
* equal it), so a collision in the tables, a stale count or a wrong match changes how many tokens a
|
||||
* step lands, never which tokens come out. The tables key on 64-bit hashes and store 32 bits of the
|
||||
* key; a clash merges two contexts' counts.
|
||||
*
|
||||
* The index is incremental: each call indexes the tokens committed since the last one, prompt
|
||||
* included at the first, so a sequence is indexed once over its lifetime. Memory is about 8 bytes a
|
||||
* token per table and per NgramIndex pattern length (4 lengths).
|
||||
*/
|
||||
#pragma once
|
||||
#include "ngram.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
|
||||
class SuffixIndex {
|
||||
public:
|
||||
/* NgramIndex tries pattern lengths min_match .. min_match + kExtra, longest first: the proposal
|
||||
* follows the latest occurrence of the longest recurring suffix in that range. */
|
||||
enum { kExtra = 3, kMaxK = 12 };
|
||||
|
||||
/* Forget the sequence; `owner` is the request id the next lookups are for. */
|
||||
void reset(uint64_t owner = 0) {
|
||||
ix_.reset(owner);
|
||||
followed_.clear();
|
||||
follow_.clear();
|
||||
filled_ = 0;
|
||||
k_ = 0;
|
||||
}
|
||||
uint64_t owner() const { return ix_.owner(); }
|
||||
|
||||
struct Out {
|
||||
int n = 0; /* tokens proposed */
|
||||
int match = 0; /* how far the match behind the proposal runs back (>= min_match) */
|
||||
float score = 0.0f; /* D of the last proposed node: the chance the whole chain is taken */
|
||||
float expect = 0.0f; /* sum of D along the chain: expected accepted tokens */
|
||||
};
|
||||
|
||||
/* The proposal for `seq`: at most `max_rows` tokens into `out` (the index is brought up to the
|
||||
* sequence first). n == 0 when the last `min_match` tokens did not occur before, or the first
|
||||
* token's own score is under `min_score`. 1 <= min_match <= kMaxK. */
|
||||
Out propose(const TokenSeq& seq, uint64_t owner, int min_match, int max_rows, float min_score,
|
||||
int32_t* out) {
|
||||
Out r;
|
||||
if (min_match < 1 || min_match > kMaxK || max_rows < 1 || seq.size() <= min_match) return r;
|
||||
if (ix_.owner() != owner || k_ != min_match) { reset(owner); k_ = min_match; }
|
||||
const int64_t L = seq.size();
|
||||
if (L < filled_) { reset(owner); k_ = min_match; }
|
||||
index_to(seq, L);
|
||||
int match = 0;
|
||||
const int n = ix_.propose(seq, min_match, min_match + kExtra, max_rows, out, &match, 64);
|
||||
if (n <= 0) return r;
|
||||
/* Score the chain: the context moves forward over the proposal's own tokens. */
|
||||
int32_t win[kMaxK];
|
||||
for (int i = 0; i < k_; ++i) win[i] = seq[L - k_ + i];
|
||||
float d = 1.0f, sum = 0.0f;
|
||||
int m = 0;
|
||||
for (; m < n; ++m) {
|
||||
const uint64_t h = hash_ctx(win, k_);
|
||||
const uint32_t a = followed_.get(h);
|
||||
const uint32_t b = follow_.get(mix(h, (uint64_t)(uint32_t)out[m]));
|
||||
if (a == 0 || b == 0) break; /* a path the history never took */
|
||||
const float c = (float)b / (float)a;
|
||||
if (d * c < min_score) break;
|
||||
d *= c;
|
||||
sum += d;
|
||||
for (int i = 0; i + 1 < k_; ++i) win[i] = win[i + 1];
|
||||
win[k_ - 1] = out[m];
|
||||
}
|
||||
r.n = m;
|
||||
r.match = match;
|
||||
r.score = m > 0 ? d : 0.0f;
|
||||
r.expect = sum;
|
||||
return r;
|
||||
}
|
||||
|
||||
int64_t indexed() const { return filled_; }
|
||||
size_t bytes() const { return ix_.bytes() + (followed_.t.capacity() + follow_.t.capacity()) * 8; }
|
||||
|
||||
private:
|
||||
/* Open addressing; an entry is key (the high 32 bits of a 64-bit hash, never 0) << 32 | count.
|
||||
* The probe starts from the stored key, so a rehash needs nothing else. */
|
||||
struct Table {
|
||||
std::vector<uint64_t> t;
|
||||
uint64_t mask = 0;
|
||||
int64_t n = 0;
|
||||
void clear() { t.clear(); t.shrink_to_fit(); mask = 0; n = 0; }
|
||||
static uint32_t key_of(uint64_t h) { return (uint32_t)(h >> 32) | 1u; }
|
||||
static uint64_t start(uint32_t key) { return ((uint64_t)key * 0x9e3779b97f4a7c15ull) >> 29; }
|
||||
uint32_t get(uint64_t h) const {
|
||||
if (t.empty()) return 0;
|
||||
const uint32_t key = key_of(h);
|
||||
for (uint64_t i = start(key) & mask;; i = (i + 1) & mask) {
|
||||
const uint64_t e = t[i];
|
||||
if (e == 0) return 0;
|
||||
if ((uint32_t)(e >> 32) == key) return (uint32_t)e;
|
||||
}
|
||||
}
|
||||
void bump(uint64_t h) {
|
||||
if (t.empty() || (uint64_t)(n + 1) * 2 > mask + 1) grow();
|
||||
const uint32_t key = key_of(h);
|
||||
for (uint64_t i = start(key) & mask;; i = (i + 1) & mask) {
|
||||
uint64_t& e = t[i];
|
||||
if (e == 0) { e = (uint64_t)key << 32 | 1u; ++n; return; }
|
||||
if ((uint32_t)(e >> 32) == key) { if ((uint32_t)e != 0xffffffffu) ++e; return; }
|
||||
}
|
||||
}
|
||||
void grow() {
|
||||
std::vector<uint64_t> old;
|
||||
old.swap(t);
|
||||
const uint64_t cap = old.empty() ? 1024 : (mask + 1) * 2;
|
||||
t.assign(cap, 0);
|
||||
mask = cap - 1;
|
||||
for (uint64_t e : old) {
|
||||
if (!e) continue;
|
||||
for (uint64_t i = start((uint32_t)(e >> 32)) & mask;; i = (i + 1) & mask)
|
||||
if (t[i] == 0) { t[i] = e; break; }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NgramIndex ix_;
|
||||
Table followed_; /* a k-token context -> how many times anything followed it */
|
||||
Table follow_; /* a k-token context and a token -> how many times that token did */
|
||||
int64_t filled_ = 0; /* positions < filled_ are counted */
|
||||
int k_ = 0;
|
||||
|
||||
static uint64_t mix(uint64_t h, uint64_t v) {
|
||||
h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
|
||||
h *= 0xff51afd7ed558ccdull;
|
||||
h ^= h >> 32;
|
||||
return h;
|
||||
}
|
||||
static uint64_t hash_ctx(const int32_t* t, int k) {
|
||||
uint64_t h = 0x84222325cbf29ce4ull;
|
||||
for (int i = 0; i < k; ++i) h = mix(h, (uint64_t)(uint32_t)t[i]);
|
||||
return h;
|
||||
}
|
||||
/* Count, for every position e in [filled_, upto) with k tokens before it, the context
|
||||
* seq[e-k..e-1] followed by seq[e]. */
|
||||
void index_to(const TokenSeq& seq, int64_t upto) {
|
||||
int32_t win[kMaxK];
|
||||
for (int64_t e = filled_ < k_ ? k_ : filled_; e < upto; ++e) {
|
||||
for (int i = 0; i < k_; ++i) win[i] = seq[e - k_ + i];
|
||||
const uint64_t h = hash_ctx(win, k_);
|
||||
followed_.bump(h);
|
||||
follow_.bump(mix(h, (uint64_t)(uint32_t)seq[e]));
|
||||
}
|
||||
filled_ = upto;
|
||||
}
|
||||
};
|
||||
|
||||
} /* namespace rad */
|
||||
Reference in new issue
Block a user