okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Strata: Qwen3.8-Flash-Next on a 12-24 GB NVIDIA GPU + 64 GB RAM, one-click install01529aaa8

main
3.4 KiB73 linesraw
1// src/spec/controller.cpp - see include/strata/spec/controller.hpp.
2#include "strata/spec/controller.hpp"
3
4#include <algorithm>
5
6namespace strata::spec {
7
8double CostModel::step_ms(int k, bool mtp) const {
9    const int n = std::clamp(k + 1, 1, K_MAX + 1);
10    const double dense = dense_ms * dense_ratio[n - 1];
11    // CPU misses: distinct experts grow as U(n); each extra token routed to a missed expert adds a fraction of a
12    // read. Tokens per distinct expert = n / U(n)-ratio (U measured in units of one token's 10 experts).
13    const double u = distinct_ratio[n - 1];
14    const double uses_per_expert = (double) n / u;
15    const double cpu = (1.0 - hit_rate) * cpu_all_miss_ms * u * (1.0 + extra_use_cost * (uses_per_expert - 1.0));
16    const double draft = k * (mtp ? mtp_draft_ms : lookup_draft_ms);
17    return dense + cpu + sync_ms + draft;
18}
19
20Controller::Controller(CostModel cost, double min_gain, double ema) : cost_(cost), min_gain_(min_gain), ema_(ema) {
21    // Priors: MTP per-position acceptance from flyweight's measurement on this model (0.86 per draft); lookup by
22    // match length from the offline replay (2026-09-23-suffix-lookup). Both are then learned per session.
23    mtp_p_.fill(0.86);
24    lookup_q_ = {0.35, 0.6, 0.8, 0.92};
25}
26
27double Controller::expected(const double* p, int k, bool same) {
28    double e = 1.0, run = 1.0;                                // the verify pass always commits one token
29    for (int i = 0; i < k; ++i) {
30        run *= same ? p[0] : p[i];
31        e += run;
32    }
33    return e;
34}
35
36Choice Controller::choose(int lookup_available, int lookup_match, bool mtp_ready) const {
37    Choice best;
38    best.tokens_per_ms = 1.0 / cost_.step_ms(0, false);
39    const double baseline = best.tokens_per_ms;
40    auto consider = [&](Source s, int k, double e) {
41        const double rate = e / cost_.step_ms(k, s == Source::Mtp);
42        if (rate > best.tokens_per_ms) best = Choice{s, k, e, rate};
43    };
44    const int lk = std::min(lookup_available, K_MAX);
45    const double q = lookup_q_[bucket(lookup_match)];
46    for (int k = 1; k <= lk; ++k) consider(Source::Lookup, k, expected(&q, k, true));
47    if (mtp_ready)
48        for (int k = 1; k <= K_MAX; ++k) consider(Source::Mtp, k, expected(mtp_p_.data(), k, false));
49    if (best.source != Source::None && best.tokens_per_ms < baseline * (1.0 + min_gain_)) {
50        Choice none;
51        none.tokens_per_ms = baseline;
52        return none;
53    }
54    return best;
55}
56
57void Controller::observe(const Choice& c, int accepted, int lookup_match) {
58    if (c.source == Source::None || c.k <= 0) return;
59    accepted = std::clamp(accepted, 0, c.k);
60    // Positions 0..accepted-1 were accepted given their prefix; position `accepted` (if drafted) was rejected.
61    const int seen = std::min(c.k, accepted + 1);
62    for (int i = 0; i < seen; ++i) {
63        const double hit = i < accepted ? 1.0 : 0.0;
64        if (c.source == Source::Mtp) mtp_p_[i] += ema_ * (hit - mtp_p_[i]);
65        else lookup_q_[bucket(lookup_match)] += ema_ * (hit - lookup_q_[bucket(lookup_match)]);
66    }
67    // Everything drafted was accepted: the positions beyond the window were never tested, so without this they
68    // keep their prior forever and the window can never grow. Pull them toward the deepest observed rate.
69    if (c.source == Source::Mtp && accepted == c.k && c.k < K_MAX)
70        for (int i = c.k; i < K_MAX; ++i) mtp_p_[i] += ema_ * (mtp_p_[c.k - 1] - mtp_p_[i]);
71}
72
73}  // namespace strata::spec