okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
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 { 9const int n = std::clamp (k + 1 ,1 ,K_MAX + 1 ); 10const 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). 13const double u = distinct_ratio [n - 1 ]; 14const double uses_per_expert = (double )n /u ; 15const double cpu = (1.0 - hit_rate )* cpu_all_miss_ms * u * (1.0 + extra_use_cost * (uses_per_expert - 1.0 )); 16const double draft = k * (mtp ?mtp_draft_ms :lookup_draft_ms ); 17return 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. 23mtp_p_ .fill (0.86 ); 24lookup_q_ = {0.35 ,0.6 ,0.8 ,0.92 }; 25} 26 27double Controller ::expected (const double * p ,int k ,bool same ) { 28double e = 1.0 ,run = 1.0 ;// the verify pass always commits one token 29for (int i = 0 ;i < k ;++ i ) { 30run *=same ?p [0 ] :p [i ]; 31e += run ; 32 } 33return e ; 34} 35 36Choice Controller ::choose (int lookup_available ,int lookup_match ,bool mtp_ready )const { 37Choice best ; 38best .tokens_per_ms = 1.0 /cost_ .step_ms (0 , false); 39const double baseline = best .tokens_per_ms ; 40auto consider = [& ](Source s ,int k ,double e ) { 41const double rate = e /cost_ .step_ms (k ,s == Source ::Mtp ); 42if (rate > best .tokens_per_ms )best = Choice {s ,k ,e ,rate }; 43 }; 44const int lk = std::min (lookup_available ,K_MAX ); 45const double q = lookup_q_ [bucket (lookup_match )]; 46for (int k = 1 ;k <=lk ;++ k )consider (Source ::Lookup ,k ,expected (& q ,k , true)); 47if (mtp_ready ) 48for (int k = 1 ;k <=K_MAX ;++ k )consider (Source ::Mtp ,k ,expected (mtp_p_ .data (),k , false)); 49if (best .source != Source ::None && best .tokens_per_ms < baseline * (1.0 + min_gain_ )) { 50Choice none ; 51none .tokens_per_ms = baseline ; 52return none ; 53 } 54return best ; 55} 56 57void Controller ::observe (const Choice & c ,int accepted ,int lookup_match ) { 58if (c .source == Source ::None || c .k <=0 )return ; 59accepted = std::clamp (accepted ,0 ,c .k ); 60// Positions 0..accepted-1 were accepted given their prefix; position `accepted` (if drafted) was rejected. 61const int seen = std::min (c .k ,accepted + 1 ); 62for (int i = 0 ;i < seen ;++ i ) { 63const double hit = i < accepted ?1.0 :0.0 ; 64if (c .source == Source ::Mtp )mtp_p_ [i ]+= ema_ * (hit - mtp_p_ [i ]); 65else 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. 69if (c .source == Source ::Mtp && accepted == c .k && c .k < K_MAX ) 70for (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