Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

8.3 KB
Raw Blame
diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp
index e8797a07..ee2de806 100644
--- a/tools/server/server-context.cpp
+++ b/tools/server/server-context.cpp
@@ -215,6 +215,90 @@ struct server_slot {
     common_speculative * spec;
     int32_t spec_depth_max = 0; // no drafting once the sequence is longer than this (0 = always draft)
 
+    // ADAPT (LLAMA_SPEC_ADAPT=1): adaptive draft depth per request. A window of recent
+    // verify outcomes gives the chain acceptance per draft position, an EMA of the step wall time per depth gives
+    // the cost, and every step drafts the depth with the best expected tokens per second (0 = plain step).
+    int32_t adapt_dmax   = 0;       // --spec-draft-n-max
+    int32_t adapt_d_step = -1;      // depth requested for the step in flight (-1: none; 0: plain)
+    int64_t adapt_t_step = 0;       // ggml_time_us() when that step's draft was requested
+    double  adapt_ms[9]  = {0};     // EMA of ms per step by depth drafted (0 = plain)
+    int32_t adapt_n[9]   = {0};
+    int32_t adapt_cnt[9] = {0};     // how often each depth was drafted this request (log)
+    int32_t adapt_steps  = 0;
+    std::vector<std::pair<uint8_t, uint8_t>> adapt_hist;   // (depth drafted, tokens accepted), newest last
+
+    static int    adapt_on()     { static int    v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT");       return e ? atoi(e) : 0;    }(); return v; }
+    static double adapt_kappa()  { static double v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_KAPPA"); return e ? atof(e) : 0.18; }(); return v; }
+    static int    adapt_window() { static int    v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_W");     return e ? atoi(e) : 48;   }(); return v; }
+    static int    adapt_dmin()   { static int    v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_MIN");   return e ? atoi(e) : 0;    }(); return v; }
+    static int    adapt_log()    { static int    v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_LOG");   return e ? atoi(e) : 0;    }(); return v; }
+    static double adapt_hyst()   { static double v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_HYST");  return e ? atof(e) : 0.02; }(); return v; }   // relative gain needed to leave the anchor / shallower depth
+    static int    adapt_explore(){ static int    v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_EXPLORE"); return e ? atoi(e) : 16; }(); return v; }  // try a neighbour every N steps (0 = never)
+    static int    adapt_anchor() { static int    v = [] { const char * e = getenv("LLAMA_SPEC_ADAPT_ANCHOR"); return e ? atoi(e) : 0;   }(); return v; }   // 1: stay at the max depth unless another depth beats it by HYST
+
+    void adapt_reset() {
+        if (adapt_log() && adapt_steps > 0) {
+            fprintf(stderr, "ADAPT slot %d: %d steps, depth counts", id, adapt_steps);
+            for (int d = 0; d <= 8; ++d) if (adapt_cnt[d]) fprintf(stderr, " d%d=%d", d, adapt_cnt[d]);
+            fprintf(stderr, "; ms/step");
+            for (int d = 0; d <= 8; ++d) if (adapt_n[d]) fprintf(stderr, " d%d=%.1f", d, adapt_ms[d]);
+            fprintf(stderr, "\n");
+        }
+        adapt_d_step = -1; adapt_t_step = 0; adapt_steps = 0; adapt_hist.clear();
+        for (int i = 0; i < 9; ++i) { adapt_ms[i] = 0; adapt_n[i] = 0; adapt_cnt[i] = 0; }
+    }
+
+    // outcome of the step in flight: d tokens were drafted (0 = plain), a of them accepted
+    void adapt_record(int d, int a) {
+        if (adapt_d_step < 0) return;
+        const double ms = (double) (ggml_time_us() - adapt_t_step) * 1e-3;
+        const int di = d < 8 ? d : 8;
+        adapt_ms[di] = adapt_n[di] == 0 ? ms : 0.8 * adapt_ms[di] + 0.2 * ms;
+        adapt_n[di]++;
+        if (d > 0) {
+            adapt_hist.emplace_back((uint8_t) d, (uint8_t) a);
+            if ((int) adapt_hist.size() > adapt_window()) adapt_hist.erase(adapt_hist.begin());
+        }
+        adapt_d_step = -1;
+    }
+
+    int adapt_choose() {
+        const int dmax = std::max(0, std::min(adapt_dmax, 8));
+        const int dmin = std::max(0, std::min(adapt_dmin(), dmax));
+        adapt_steps++;
+        double q[8];
+        for (int i = 0; i < dmax; ++i) {
+            double acc = 1.2, tri = 2.0;   // Beta prior: mean 0.6, weight 2
+            for (const auto & h : adapt_hist) if (h.first > i && h.second >= i) { tri += 1; if (h.second > i) acc += 1; }
+            q[i] = acc / tri;
+        }
+        const double kappa = adapt_kappa();
+        double T[9]; int ref = -1;
+        for (int d = 0; d <= dmax; ++d) if (adapt_n[d] > 0 && (ref < 0 || adapt_n[d] > adapt_n[ref])) ref = d;
+        for (int d = 0; d <= dmax; ++d) {
+            if (adapt_n[d] > 0)  T[d] = adapt_ms[d];
+            else if (ref >= 0)   T[d] = adapt_ms[ref] * (1 + kappa * d) / (1 + kappa * ref);
+            else                 T[d] = 1 + kappa * d;
+        }
+        const double hyst = adapt_hyst();
+        double V[9]; double e = 1.0, chain = 1.0;
+        for (int d = 0; d <= dmax; ++d) { if (d > 0) { chain *= q[d - 1]; e += chain; } V[d] = e / T[d]; }
+        int best = dmin; double bestv = -1;
+        if (adapt_anchor()) {          // anchored: the max depth (the release default) unless another depth beats it by HYST
+            best = dmax; bestv = V[dmax];
+            for (int d = dmin; d < dmax; ++d) if (V[d] > V[dmax] * (1 + hyst) && V[d] > bestv) { bestv = V[d]; best = d; }
+        } else {
+            for (int d = dmin; d <= dmax; ++d) if (bestv < 0 || V[d] > bestv * (1 + hyst)) { bestv = V[d]; best = d; }   // hysteresis toward shallower depths
+        }
+        const int ex = adapt_explore();
+        if (ex > 0) {   // exploration keeps the neighbours' time estimates fresh
+            if (adapt_steps % ex == ex / 2 && best + 1 <= dmax)  best = best + 1;
+            else if (adapt_steps % ex == 0 && best - 1 >= dmin) best = best - 1;
+        }
+        adapt_cnt[best]++;
+        return best;
+    }
+
     llama_tokens spec_draft;
     llama_tokens spec_prompt;
     std::vector<int32_t> spec_i_batch;
@@ -347,6 +431,7 @@ struct server_slot {
             spec_i_batch.clear();
             spec_ckpt.clear();
         }
+        adapt_reset();
         generated_tokens.clear();
         generated_token_probs.clear();
         json_schema = json();
@@ -444,7 +529,7 @@ struct server_slot {
         generated_token_probs.push_back(token);
     }
 
-    int get_n_draft_max() const {
+    int get_n_draft_max() {
         GGML_ASSERT(task);
 
         if (!can_speculate()) {
@@ -466,6 +551,10 @@ struct server_slot {
             n_draft_max = std::min(n_draft_max, n_remaining() - 1);
         }
 
+        if (adapt_on() && n_draft_max > 0) {   // ADAPT: the depth with the best expected tokens per second (0 = plain step)
+            n_draft_max = std::min(n_draft_max, adapt_choose());
+        }
+
         SLT_DBG(*this, "max possible draft: %d\n", n_draft_max);
 
         return n_draft_max;
@@ -1253,6 +1342,7 @@ private:
             slot.mem.init(ctx_tgt, ctx_dft);
             slot.spec    = spec.get();
             slot.spec_depth_max = params_base.speculative.draft.n_depth_max;
+            slot.adapt_dmax     = common_speculative_n_max(&params_base.speculative);
             slot.n_ctx   = n_ctx_slot;
 
             slot.stats.speculative = slot.can_speculate();
@@ -2944,6 +3034,7 @@ private:
                 const bool use_ckpt_dft = ctx_dft_seq_rm_type == COMMON_CONTEXT_SEQ_RM_TYPE_FULL;
 
                 const int n_draft_max = slot.get_n_draft_max();
+                if (slot.adapt_on()) { slot.adapt_t_step = ggml_time_us(); slot.adapt_d_step = n_draft_max > 0 ? n_draft_max : 0; }
 
                 if (n_draft_max > 0) {
                     GGML_ASSERT(slot.can_speculate());
@@ -3846,6 +3937,7 @@ private:
             if (slot.can_speculate() && !slot.spec_draft.empty()) {
                 return; // sample using speculative decoding
             }
+            if (slot.adapt_on() && slot.adapt_d_step == 0) slot.adapt_record(0, 0);   // ADAPT: a plain step's time
 
             // shifted according to the current sub-batch
             const int tok_idx = slot.i_batch - off;
@@ -3968,6 +4060,7 @@ private:
                 n_accepted--;
             }
             slot.spec_is_replay = false;
+            if (slot.adapt_on()) slot.adapt_record((int) n_draft, (int) n_accepted);   // ADAPT: this verify step's outcome
 
             slot.stats.update_gen_last();