spec: STRATA_SPEC_COST, a cost-aware MTP window (DraftPolicy::best_rows: the window with the best expected tokens per ms from the chain's probabilities, a measured probability-to-acceptance table and the measured row costs); the trace now logs the probabilities the window was chosen from (A/B arm, off by default)

This commit is contained in:
Niko1221 committed 2026-10-21 14:07:25 +02:00
1 parent 651a56806a
commit 6878105560
3 files changed
+77 -6

No files matched your search

+20 -6
View File
@@ -11031,6 +11031,7 @@ int main(int argc, char** argv) {
else if (use_mtp && mtp.max_t() > S_mtp) mtp.set_max_drafts(S_mtp - 1);
strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096);
strata::spec::DraftPolicy policy(S); // MTP or lookup window, learned over the whole process
const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }(); // cost-aware MTP window (A/B arm)
// The vision path (--vision): GENI <max_new> <embeddings file> <id,id,...> carries images. The file is one
// or more strata-vision records (int32 'SVE1', n, nx, ny, n_embd, then n x n_embd floats) in prompt order;
// each image's rows go to its run of <|image_pad|> tokens, whose M-RoPE positions are mtmd's: t = p,
@@ -14015,6 +14016,7 @@ int main(int argc, char** argv) {
const size_t WS = (size_t) std::max(S, merge_T);
std::vector<int32_t> drafts(DS, 0), window(WS), outv(WS);
std::vector<float> dprob(DS, 0.0f);
std::array<float, 8> dwin_s{}; // the probabilities the current window was chosen from (STRATA_SPEC_COST)
std::vector<int32_t> sbuf((size_t) S, 0);
const bool sfx_on = o.suffix_draft > 0 || o.lookup_chain > 0;
if (sfx_on) {
@@ -14723,11 +14725,14 @@ int main(int argc, char** argv) {
}
while (!pl_ran && !cancelled && produced_n < max_new) {
int T = use_mtp ? S_mtp : 1; // no --mtp: one token a round unless a lookup draft fires
if (use_mtp && req_spec_min_p > 0.0) {
if (use_mtp && spec_cost && !first_window) {
T = policy.best_rows(dprob.data(), S_mtp - 1, S_mtp);
} else if (use_mtp && req_spec_min_p > 0.0) {
T = 1;
while (T < S_mtp && dprob[(size_t) T - 1] >= (float) req_spec_min_p) ++T;
}
if (first_window) T = merge_T > 0 ? merge_T : 1;
std::copy(dprob.begin(), dprob.begin() + (std::ptrdiff_t) std::min<size_t>(dprob.size(), 8), dwin_s.begin());
const bool merged = first_window && merge_T > 0; // the prompt's last tokens and the first token
// a repeat of earlier context (prompt lookup) where the MTP's own first guess agrees: the policy takes it
// when its expected tokens per ms, from the measured acceptance and window costs, beat the MTP window's
@@ -15006,7 +15011,7 @@ int main(int argc, char** argv) {
if (use_mtp && hist_n > 0 && mtp.coupled() && !eos && produced_n < max_new)
mtp.set_draft_history(consumed.data(), (int64_t) consumed.size(), outv[(size_t) a]);
const bool drafted = !use_mtp || eos || produced_n >= max_new ||
mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p);
mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), spec_cost ? 0.0f : (float) req_spec_min_p);
{
const Clock::time_point tw3 = Clock::now();
auto msd = [](Clock::time_point a0, Clock::time_point b0) { return std::chrono::duration<double, std::milli>(b0 - a0).count(); };
@@ -15027,6 +15032,7 @@ int main(int argc, char** argv) {
std::printf("ERR %s\n", err.c_str());
return 1;
}
if (spec_cost && use_mtp && timed_round && !from_sfx) policy.observe_probs(dwin_s.data(), T_mtp - 1, a);
if (timed_round && !eos && chain_n == 0)
policy.observe(from_sfx, T, a, sfx_match,
std::chrono::duration<double, std::milli>(Clock::now() - round0).count());
@@ -16201,6 +16207,10 @@ int main(int argc, char** argv) {
}
// STRATA_SPEC_TRACE=<file> (bench only, off by default): one line per round, "T a sfx ms p1 p2 ..." (window rows, drafts accepted,
// lookup window, round ms, the MTP draft probabilities), for offline window-length policy studies
// STRATA_SPEC_COST=1 (A/B arm, bench): the MTP window by expected tokens per ms at this machine's measured row costs
// (DraftPolicy::best_rows) instead of "draft probability >= --spec-min-p"; the chain is drafted in full
const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }();
std::vector<float> dwin((size_t) o.spec, 0.0f); // the draft probabilities this round's window was chosen from
std::FILE* spec_trace = nullptr;
if (const char* tf = std::getenv("STRATA_SPEC_TRACE"); tf != nullptr && tf[0] != '\0') spec_trace = std::fopen(tf, "w");
std::vector<double> t_cycle_ms((size_t) o.spec + 1, 0.0);
@@ -16209,7 +16219,7 @@ int main(int argc, char** argv) {
// generated token and the MTP's first cell); otherwise the token loop already did that.
bool first_window = native_pack;
if (use_mtp && !first_window &&
!mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), (float) o.spec_min_p)) {
!mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), spec_cost ? 0.0f : (float) o.spec_min_p)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
@@ -16271,11 +16281,14 @@ int main(int argc, char** argv) {
const Clock::time_point t0 = Clock::now();
strata::core::progress_at("spec loop: choosing the window", (int64_t) produced.size());
int T = S_mtp;
if (use_mtp && o.spec_min_p > 0.0) {
if (use_mtp && spec_cost && !first_window) {
T = policy.best_rows(dprob.data(), S_mtp - 1, S_mtp);
} else if (use_mtp && o.spec_min_p > 0.0) {
T = 1;
while (T < S_mtp && dprob[(size_t) T - 1] >= (float) o.spec_min_p) ++T;
}
if (first_window) T = 1;
std::copy(dprob.begin(), dprob.begin() + (std::ptrdiff_t) o.spec, dwin.begin());
if (!t_cycle.empty() && !first_window) T = t_cycle[(size_t) (rounds % (int64_t) t_cycle.size())];
bool from_sfx = false;
int sfx_match = 0;
@@ -16408,7 +16421,7 @@ int main(int argc, char** argv) {
break;
}
const bool drafted = !use_mtp || (int64_t) produced.size() >= max_new ||
mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) o.spec_min_p);
mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), spec_cost ? 0.0f : (float) o.spec_min_p);
if (adapt_thr.joinable()) adapt_thr.join();
if (!adapt_ok) return 1;
if (!drafted) {
@@ -16421,13 +16434,14 @@ int main(int argc, char** argv) {
total_ms += round_ms;
if (spec_trace != nullptr && timed_round) {
std::fprintf(spec_trace, "%d %d %d %.3f", T, a, from_sfx ? 1 : 0, round_ms);
for (int i = 0; i < T_mtp - 1; ++i) std::fprintf(spec_trace, " %.4f", (double) dprob[(size_t) i]);
for (int i = 0; i < o.spec - 1; ++i) std::fprintf(spec_trace, " %.4f", (double) dwin[(size_t) i]);
std::fprintf(spec_trace, "\n");
}
if (!t_cycle.empty() && timed_round && T <= o.spec && t_cycle_seen[(size_t) T]++ > 0) {
t_cycle_ms[(size_t) T] += round_ms;
++t_cycle_n[(size_t) T];
}
if (spec_cost && use_mtp && timed_round && !from_sfx) policy.observe_probs(dwin.data(), T_mtp - 1, a);
if (timed_round && chain_n == 0) policy.observe(from_sfx, T, a, sfx_match, round_ms);
else if (timed_round) policy.observe_chain(T_mtp, chain_n, a, cm, round_ms);
if (rounds % 64 == 0)