Repository navigation
Expand file tree
/
Copy pathdraft_policy.cpp
More file actions
332 lines (302 loc) · 14 KB
/
Copy pathdraft_policy.cpp
File metadata and controls
332 lines (302 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// src/spec/draft_policy.cpp - see include/strata/spec/draft_policy.hpp.
#include "strata/spec/draft_policy.hpp"
#include <algorithm>
#include <cstdio>
namespace strata::spec {
namespace {
// The shape of a round's cost by window size, relative to one token, used only for sizes not measured yet (the
// measured round times replace it). Between the RTX 5070's measured curves: +10 ms per token with every missed expert
// on the CPU (bench/results/2026-09-27-spec/window-cost), flatter with the default CPU/DMA split.
// (sizes 9-16, above the measured curves: +0.4 a token)
constexpr double kShape[DraftPolicy::kMaxT + 1] = {0.0, 1.0, 1.35, 1.7, 2.05, 2.45, 2.85, 3.25, 3.6,
4.0, 4.4, 4.8, 5.2, 5.6, 6.0, 6.4, 6.8};
constexpr double kCostAlpha = 0.1; // EMA weight of a new round time
constexpr double kTokAlpha = 0.05; // EMA weight of a new MTP window outcome
constexpr double kCalDecay = 0.995; // the MTP probability -> acceptance table fades slowly
constexpr double kDecay = 0.97; // lookup counts: older windows fade
// Before a bucket has data: the longer the match, the likelier its continuation (llama.cpp's lookup decoding gates
// on the same thing); worth 4 observations, so a few real windows override it
constexpr double kPriorQ[DraftPolicy::kBuckets] = {0.75, 0.88, 0.93, 0.96};
constexpr double kPriorN = 4.0;
constexpr int kProbes = 3; // a lookup window size is tried this often before its guessed cost can veto it
// A size not measured for this many rounds is tried again when a confident lookup could use it: its cost was set in
// one stretch (a prompt's cache refill, say) and, priced high, it would never be measured again (#1252)
constexpr double kStaleRounds = 300.0;
} // namespace
DraftPolicy::DraftPolicy(int max_t, double margin)
: max_t_(std::clamp(max_t, 1, kMaxT)), margin_(margin) {}
void DraftPolicy::set_sizes(unsigned mask) { sizes_ = mask | 2u; }
void DraftPolicy::disable(int t) {
if (t > 1 && t <= kMaxT) sizes_ &= ~(1u << t);
}
int DraftPolicy::fit(int t) const {
for (t = std::min(t, max_t_); t > 1; --t)
if (allowed(t)) return t;
return 1;
}
int DraftPolicy::bucket(int match) {
return match < 6 ? 0 : match < 12 ? 1 : match < 24 ? 2 : 3;
}
double DraftPolicy::lookup_rate(int match) const {
const int b = bucket(match);
return (ok_[b] + kPriorN * kPriorQ[b]) / (ok_[b] + bad_[b] + kPriorN);
}
double DraftPolicy::cost_ms(int t) const {
t = std::clamp(t, 1, kMaxT);
if (cost_n_[bk_][t] > 0) return cost_[bk_][t];
// scale from the measured sizes, weighting each by how often it was seen
double num = 0.0, den = 0.0;
for (int u = 1; u <= kMaxT; ++u)
if (cost_n_[bk_][u] > 0) {
const double w = std::min(cost_n_[bk_][u], 20.0);
num += w * cost_[bk_][u] * kShape[t] / kShape[u];
den += w;
}
return den > 0 ? num / den : kShape[t];
}
double DraftPolicy::clamp_sample(int t, double ms) const {
if (!robust_) return ms;
const double known = cost_ms(t); // the measured value, or the scaled guess from the other widths
if (cost_n_[bk_][t] == 0) {
// the first use of a width captures its graph: that round says nothing about its cost, the guess from the other widths stands in
for (int u = 1; u <= kMaxT; ++u)
if (cost_n_[bk_][u] > 0) return known;
return ms;
}
return std::min(ms, 2.0 * known);
}
void DraftPolicy::seed_costs(const double* ms, int n, double weight, int bucket) {
bucket = std::clamp(bucket, 0, kCtxBuckets - 1);
for (int t = 1; t <= std::min(n, kMaxT); ++t)
if (ms[t - 1] > 0.0 && cost_n_[bucket][t] == 0) {
cost_[bucket][t] = ms[t - 1];
cost_n_[bucket][t] = weight;
}
}
void DraftPolicy::set_context(long long pos) {
if (!robust_) return;
const int nb = pos < 6000 ? 0 : pos < 24000 ? 1 : 2;
if (nb == bk_) return;
bool has = false;
for (int t = 1; t <= kMaxT; ++t) has = has || cost_n_[nb][t] > 0;
if (!has) { // nothing measured at this length yet: the nearest bucket that has data is the prior (one sample's worth)
for (int d = 1; d < kCtxBuckets && !has; ++d)
for (const int src : {nb - d, nb + d}) {
if (src < 0 || src >= kCtxBuckets || has) continue;
for (int t = 1; t <= kMaxT; ++t)
if (cost_n_[src][t] > 0) {
cost_[nb][t] = cost_[src][t];
cost_n_[nb][t] = 1.0;
last_[nb][t] = rounds_;
has = true;
}
}
}
bk_ = nb;
}
std::string DraftPolicy::cost_table_text() const {
std::string out;
char b[64];
for (int k = 0; k < kCtxBuckets; ++k) {
bool any = false;
for (int t = 1; t <= kMaxT; ++t) any = any || cost_n_[k][t] > 0;
if (!any) continue;
out += (out.empty() ? "" : " | ");
out += k == 0 ? "ctx<6K:" : k == 1 ? "ctx<24K:" : "ctx>=24K:";
for (int t = 1; t <= kMaxT; ++t)
if (cost_n_[k][t] > 0) {
std::snprintf(b, sizeof b, " T%d:%.2f(n=%.0f)", t, cost_[k][t], cost_n_[k][t]);
out += b;
}
}
return out;
}
bool DraftPolicy::stale(int t) const { return cost_n_[bk_][t] > 0 && rounds_ - last_[bk_][t] > kStaleRounds; }
double DraftPolicy::mtp_tokens(int t) const {
if (mtp_n_[t] > 0) return mtp_tok_[t];
return 1.0 + 0.7 * (t - 1); // before any MTP window of this size: a typical acceptance
}
DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const {
Pick p;
p.t = std::clamp(t_mtp, 1, max_t_);
if (lookup_k <= 0) return p;
const double base = mtp_tokens(p.t) / cost_ms(p.t);
const double q = lookup_rate(match);
double e = 1.0, qi = 1.0, best = 0.0;
int best_t = 0;
for (int k = 1; k <= std::min(lookup_k, max_t_ - 1); ++k) {
qi *= q;
e += qi;
if (!allowed(k + 1)) continue;
const double r = e / cost_ms(k + 1);
if (r > best) { best = r; best_t = k + 1; }
}
const int t_full = fit(std::min(lookup_k, max_t_ - 1) + 1);
if (best_t > 0 && best > base * (1.0 + margin_)) {
p.lookup = true;
p.t = best_t;
// a wider size whose cost has gone stale is re-measured, so one slow stretch cannot price it out for good
if (best_t < t_full && q >= 0.85 && stale(t_full)) p.t = t_full;
return p;
}
// a guessed cost can keep the policy from ever measuring a size: the first few times a confident lookup would
// need a size not measured yet, it is tried (verification keeps the output; only the one round's speed is at stake)
if (t_full > p.t && t_full > 1 && (cost_n_[bk_][t_full] - delayed_n_[bk_][t_full] + tried_[bk_][t_full] < kProbes || stale(t_full)) && q >= 0.85) {
p.lookup = true;
p.t = t_full;
}
return p;
}
void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double round_ms) {
t = std::clamp(t, 1, kMaxT);
if (round_ms > 0) {
round_ms = clamp_sample(t, round_ms);
const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[bk_][t] + 1.0)) : kCostAlpha; // a running mean for the first samples
cost_[bk_][t] = cost_n_[bk_][t] > 0 ? (1.0 - alpha) * cost_[bk_][t] + alpha * round_ms : round_ms;
cost_n_[bk_][t] += 1.0;
last_[bk_][t] = rounds_;
}
rounds_ += 1.0;
if (lookup) {
const int b = bucket(match);
ok_[b] = kDecay * ok_[b] + accepted;
bad_[b] = kDecay * bad_[b] + (accepted < t - 1 ? 1.0 : 0.0);
} else {
const double got = accepted + 1.0;
mtp_tok_[t] = mtp_n_[t] > 0 ? (1.0 - kTokAlpha) * mtp_tok_[t] + kTokAlpha * got : got;
mtp_n_[t] += 1.0;
}
}
void DraftPolicy::observe_committed(bool lookup, int t, int accepted, int match) {
t = std::clamp(t, 1, kMaxT);
rounds_ += 1.0;
if (lookup) {
tried_[bk_][t] += 1.0;
last_[bk_][t] = rounds_; // tried now: not stale within this generation
const int b = bucket(match);
ok_[b] = kDecay * ok_[b] + accepted;
bad_[b] = kDecay * bad_[b] + (accepted < t - 1 ? 1.0 : 0.0);
} else {
const double got = accepted + 1.0;
mtp_tok_[t] = mtp_n_[t] > 0 ? (1.0 - kTokAlpha) * mtp_tok_[t] + kTokAlpha * got : got;
mtp_n_[t] += 1.0;
}
}
void DraftPolicy::defer_cost(int64_t end, int t, double round_ms) {
if (round_ms > 0) held_.push_back({end, bk_, std::clamp(t, 1, kMaxT), round_ms});
}
void DraftPolicy::apply_costs(int64_t committed) {
size_t n = 0;
const int bucket_now = bk_;
for (; n < held_.size() && held_[n].end < committed; ++n) {
const HeldCost& h = held_[n];
bk_ = h.bucket; // observe()'s update, in the bucket the round ran in
const double ms = clamp_sample(h.t, h.ms);
const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[bk_][h.t] + 1.0)) : kCostAlpha;
cost_[bk_][h.t] = cost_n_[bk_][h.t] > 0 ? (1.0 - alpha) * cost_[bk_][h.t] + alpha * ms : ms;
cost_n_[bk_][h.t] += 1.0;
delayed_n_[bk_][h.t] += 1.0; // a round already counted by observe_committed
last_[bk_][h.t] = rounds_;
}
bk_ = bucket_now;
held_.erase(held_.begin(), held_.begin() + (std::ptrdiff_t) n);
}
namespace {
int p_bucket(double p) {
const int b = (int) (p * 8.0);
return b < 0 ? 0 : b > 7 ? 7 : b;
}
} // namespace
// Until a bucket has data a draft is as likely to hold as the MTP says (worth kPriorN observations); measured
// acceptance of the MTP chain is not the same as its own softmax (code and thinking text are over-confident).
double DraftPolicy::draft_rate(double p) const {
const int b = p_bucket(p);
const double centre = std::clamp(p, 0.0, 1.0);
return (pa_ok_[b] + kPriorN * centre) / (pa_n_[b] + kPriorN);
}
void DraftPolicy::observe_probs(const float* p, int n, int accepted) {
// draft j was reached when the drafts before it held; it held when j < accepted
for (int j = 0; j < n && j <= accepted; ++j) {
const int b = p_bucket(p[j]);
pa_ok_[b] = kCalDecay * pa_ok_[b] + (j < accepted ? 1.0 : 0.0);
pa_n_[b] = kCalDecay * pa_n_[b] + 1.0;
}
}
bool DraftPolicy::keep_draft(ChainState& st, int idx, float p, int cap) const {
cap = std::clamp(cap, 1, max_t_);
if (idx == 0) {
st = ChainState{};
st.best_v = 1.0 / cost_ms(1);
}
const double a = draft_rate(p);
st.q *= a;
st.e += st.q;
const int rows = idx + 2;
if (rows <= cap) {
const double v = st.e / cost_ms(rows);
if (v > st.best_v * (1.0 + margin_ / 3.0)) {
st.best_v = v;
st.best_rows = rows;
}
// a width not measured yet is tried a few times when the chain is likely to hold that far
if (cost_n_[bk_][rows] < kProbes && st.q >= 0.15) st.best_rows = rows;
}
st.kept = st.best_rows - 1;
if (rows + 1 > cap) return false; // no row left for another draft
// one or two more drafts, each as likely to hold as this one did, could they beat the best width so far?
const double v1 = (st.e + st.q * a) / cost_ms(rows + 1);
const double v2 = rows + 2 <= cap ? (st.e + st.q * a + st.q * a * a) / cost_ms(rows + 2) : 0.0;
return std::max(v1, v2) > st.best_v * (1.0 + margin_ / 3.0) || (cost_n_[bk_][rows + 1] < kProbes && st.q * a >= 0.15);
}
double DraftPolicy::chain_rate(int match) const {
// prior: a chained draft is a lookup continuing the MTP's drafts - weaker evidence than a plain match of that length
constexpr double kChainPrior[kBuckets] = {0.45, 0.65, 0.8, 0.9};
const int b = bucket(match);
return (cok_[b] + kPriorN * kChainPrior[b]) / (cok_[b] + cbad_[b] + kPriorN);
}
int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const {
t_mtp = std::clamp(t_mtp, 1, max_t_);
const int kmax = std::min(k_avail, max_t_ - t_mtp);
if (kmax <= 0) return 0;
const double e0 = mtp_tokens(t_mtp), base = e0 / cost_ms(t_mtp), c = chain_rate(match);
p_mtp = std::clamp(p_mtp, 0.0, 1.0);
double gain = 0.0, ci = 1.0, best = 0.0;
int best_k = 0;
for (int k = 1; k <= kmax; ++k) {
ci *= c;
gain += ci;
if (!allowed(t_mtp + k)) continue;
const double r = (e0 + p_mtp * gain) / cost_ms(t_mtp + k);
if (r > best) { best = r; best_k = k; }
}
const int t_full = fit(t_mtp + kmax), k_full = t_full - t_mtp;
if (best_k > 0 && best > base * (1.0 + margin_)) {
// As choose(): a profitable shorter chain must not keep a stale full size from being measured again.
if (best_k < k_full && p_mtp * c >= 0.6 && stale(t_full)) return k_full;
return best_k;
}
// as choose(): a size whose cost is only guessed is tried a few times when the continuation looks likely
if (k_full > 0 && (cost_n_[bk_][t_full] < kProbes || stale(t_full)) && p_mtp * c >= 0.6) return k_full;
return 0;
}
void DraftPolicy::observe_chain(int t_mtp, int k, int accepted, int match, double round_ms) {
const int t = std::clamp(t_mtp + k, 1, kMaxT);
if (round_ms > 0) {
round_ms = clamp_sample(t, round_ms);
const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[bk_][t] + 1.0)) : kCostAlpha; // a running mean for the first samples
cost_[bk_][t] = cost_n_[bk_][t] > 0 ? (1.0 - alpha) * cost_[bk_][t] + alpha * round_ms : round_ms;
cost_n_[bk_][t] += 1.0;
last_[bk_][t] = rounds_;
}
rounds_ += 1.0;
// the MTP part is an ordinary MTP window as far as its own drafts go
const int mtp_acc = std::min(accepted, t_mtp - 1);
const double got = mtp_acc + 1.0;
mtp_tok_[t_mtp] = mtp_n_[t_mtp] > 0 ? (1.0 - kTokAlpha) * mtp_tok_[t_mtp] + kTokAlpha * got : got;
mtp_n_[t_mtp] += 1.0;
if (accepted < t_mtp - 1) return; // the chain was never reached: it says nothing about the lookup
const int b = bucket(match), ok = accepted - (t_mtp - 1);
cok_[b] = kDecay * cok_[b] + ok;
cbad_[b] = kDecay * cbad_[b] + (ok < k ? 1.0 : 0.0);
}
} // namespace strata::spec