diff --git a/engine/src/inkling2/ink_serve.h b/engine/src/inkling2/ink_serve.h index 32cbab3fe5..c98460c608 100644 --- a/engine/src/inkling2/ink_serve.h +++ b/engine/src/inkling2/ink_serve.h @@ -458,6 +458,20 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& // time is measured on the first 4 decode steps of the first request and refreshed by every plain step after that. const bool spec_adapt = (spec_env && std::string(spec_env) == "adapt") || cfg.bopts.spec_adapt; double gov_t_plain = 0.0, gov_t_spec = 0.0; + // one speculative step (the greedy tree step or the sampled block step) took `ms` and produced `n_emit` tokens + auto gov_step = [&](ActiveReq& rq, int n_emit, double ms) { + gov_t_spec = (gov_t_spec <= 0.0) ? ms : 0.8 * gov_t_spec + 0.2 * ms; + rq.gov_win_steps += 1; + rq.gov_win_emit += n_emit; + if (rq.gov_win_steps >= 12) { + const double per_step = static_cast(rq.gov_win_emit) / rq.gov_win_steps; + const double gain = (gov_t_plain > 0.0 && gov_t_spec > 0.0) ? per_step / (gov_t_spec / gov_t_plain) : 2.0; + rq.gov_gain = (rq.gov_gain < 0.0) ? gain : 0.5 * rq.gov_gain + 0.5 * gain; + if (rq.gov_gain < 0.93) { rq.gov_plain_left = 40; rq.gov_gain = -1.0; } + rq.gov_win_steps = 0; + rq.gov_win_emit = 0; + } + }; // Probe Vision shard lazily (zero VRAM/RAM until first image request) bool images_ok = false; @@ -797,20 +811,7 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& } if (n_emit > 0) { rq.drafts_offered += drafted; - if (spec_adapt) { - const double ms = (now_s() - gov_t0) * 1000.0; - gov_t_spec = (gov_t_spec <= 0.0) ? ms : 0.8 * gov_t_spec + 0.2 * ms; - rq.gov_win_steps += 1; - rq.gov_win_emit += n_emit; - if (rq.gov_win_steps >= 12) { - const double per_step = static_cast(rq.gov_win_emit) / rq.gov_win_steps; - const double gain = (gov_t_plain > 0.0 && gov_t_spec > 0.0) ? per_step / (gov_t_spec / gov_t_plain) : 2.0; - rq.gov_gain = (rq.gov_gain < 0.0) ? gain : 0.5 * rq.gov_gain + 0.5 * gain; - if (rq.gov_gain < 0.93) { rq.gov_plain_left = 40; rq.gov_gain = -1.0; } - rq.gov_win_steps = 0; - rq.gov_win_emit = 0; - } - } + if (spec_adapt) gov_step(rq, n_emit, (now_s() - gov_t0) * 1000.0); int kept = 0; bool done = false; const char* fin = nullptr; @@ -846,6 +847,7 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& int32_t draft_ids[8]{}; std::string derr; const int max_d = std::min(7, rq.max_new - rq.generated - 1); + const double gov_b0 = now_s(); int nd = backend.dspark_draft(rq.slot, rq.next_tok, rq.cur_pos, max_d, draft_ids, derr); if (nd > 0) { backend.snapshot_slot(rq.slot); @@ -906,6 +908,7 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& backend.rollback_slot(rq.slot, rq.cur_pos + accepted); } rq.cur_pos += accepted; + if (spec_adapt) gov_step(rq, accepted, (now_s() - gov_b0) * 1000.0); } else if (fin) { finish_req(rq, fin); } diff --git a/terminal/strata_control/inkling_probe.py b/terminal/strata_control/inkling_probe.py index 1029d6cc4b..b71701e353 100644 --- a/terminal/strata_control/inkling_probe.py +++ b/terminal/strata_control/inkling_probe.py @@ -27,6 +27,9 @@ except ImportError: # run as a script from this folder from inkling_probe_prompts import CHAT_IDS, CODE_IDS MARGIN = 1.10 # the mixed gain drafting has to reach +# how each probe request is decoded: code greedy (coding tools run cold), chat sampled the way a chat client asks (the drafter then takes the +# sampled path, measured 2026-11-23 on an A6000: chat 0.91x sampled where greedy chat was 1.04x with the governor) +GEN_OPTS = {"code": "temperature=0", "chat": "temperature=0.7 top_p=0.95 top_k=64 seed=7"} TOKENS = 128 # tokens decoded per probe request (64 was too short: the A6000 with SQ-20 measured 1.05 at 64 tokens, 1.14 at 128 against 1.19 in the long runs) REPS = 2 # requests per prompt and arm; the median is used READY_TIMEOUT = 900.0 @@ -175,7 +178,7 @@ def _arm(open_engine, args: list[str], prompts: dict, tokens: int, reps: int) -> speeds: dict[str, list[float]] = {k: [] for k in prompts} plan = [("warm", next(iter(prompts.values())), 16)] + [(k, v, tokens) for k in prompts for v in [prompts[k]] for _ in range(reps)] for name, ids, n in plan: - eng.send("GEN %d temperature=0 %s" % (n, ",".join(str(x) for x in ids))) + eng.send("GEN %d %s %s" % (n, GEN_OPTS.get(name, "temperature=0"), ",".join(str(x) for x in ids))) while True: ln = eng.readline(STEP_TIMEOUT) if ln is None or ln == "TIMEOUT" or ln.startswith("ERR"): diff --git a/terminal/tests/terminal/test_inkling_probe.py b/terminal/tests/terminal/test_inkling_probe.py index df5d83668d..d4c3ccbbdc 100644 --- a/terminal/tests/terminal/test_inkling_probe.py +++ b/terminal/tests/terminal/test_inkling_probe.py @@ -27,7 +27,7 @@ class FakeEngine: self.sent.append(line) if line.startswith("GEN"): n = int(line.split()[1]) - ids = line.split()[3].split(",") + ids = line.split()[-1].split(",") kind = "code" if len(ids) == len(P.CODE_IDS) else "chat" tps = self.speeds[kind] self.lines.append("DONE %d %d 10.0 %.1f length 0 0 0" % (n, len(ids), n / tps * 1000.0)) @@ -109,12 +109,22 @@ class Driver(unittest.TestCase): def run_probe(self, speeds=None, fail_arm=None, base=None): started = [] + self.engines = [] + def open_engine(args): started.append(list(args)) - return FakeEngine(args, speeds or self.SPEEDS, fail=(fail_arm is not None and len(started) == fail_arm)) + e = FakeEngine(args, speeds or self.SPEEDS, fail=(fail_arm is not None and len(started) == fail_arm)) + self.engines.append(e) + return e res = P.probe(open_engine, base or ["--native", "m.strata", "--max-context", "32768"], key="k") return res, started + def test_code_is_decoded_greedy_and_chat_sampled(self): + self.run_probe() + sent = [l for l in self.engines[0].sent if l.startswith("GEN")] + self.assertTrue(any(" temperature=0 " in l and l.split()[-1].count(",") + 1 == len(P.CODE_IDS) for l in sent)) + self.assertTrue(any("temperature=0.7 top_p=0.95 top_k=64 seed=7" in l and l.split()[-1].count(",") + 1 == len(P.CHAT_IDS) for l in sent)) + def test_plain_first_then_chain_drafting_and_the_numbers_are_recorded(self): res, started = self.run_probe() self.assertEqual(len(started), 2)