MCPcopy Create free account
hub / github.com/Niko1221/Strata / generate

Method generate

engine/src/split/src/session.cpp:398–625  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

396}
397
398Finish Session::generate(int max_new, const Sampling& s, const std::function<void(int)>& emit, const std::atomic<bool>* stop,
399 GenStats& st) {
400 if (prompt_.empty()) throw std::runtime_error("session: generate without a prompt read");
401 e_.use_lane(lane_);
402 if (max_new < 1) throw std::runtime_error("session: max_new must be at least 1");
403 const bool sample = s.temperature > 0.f;
404 int produced = 0; // this call's tokens (st may carry an earlier call's)
405 check_sampling(s);
406 const bool penalize = penalizes(s);
407 // When sampling, MTP drafts are drawn from the draft head's own distribution at s (FNX_DRAFT_SAMPLE=0: its argmax),
408 // with s's penalties applied to its logits (FNX_DRAFT_PEN=0: without). Either way the output's distribution is the
409 // model's: the verify accepts a draft with min(1, p / q) and otherwise draws from max(0, p - q)
410 const bool pen_drafts = penalize && env_int("FNX_DRAFT_PEN", 1) != 0;
411 const bool samp_drafts = sample && env_int("FNX_DRAFT_SAMPLE", 1) != 0;
412 // The MTP chain's length follows acceptance: per chain position, how often a draft that got there stood (decayed,
413 // so it follows the text), and each round the length 1..K with the most tokens expected per ms. A round costs about
414 // ROUND_MS, a verify row DRAFT_ROW_MS more and a draft call after d1 DRAFT_CALL_MS (Q4, lab, 2026-10-25, prose with
415 // K fixed at 1 / 2 / 3 / 4: 12.47 / 14.76 / 16.60 / 18.34 ms a round). FNX_DRAFT_ADAPT=0: always K
416 const bool adapt = env_int("FNX_DRAFT_ADAPT", 1) != 0;
417 constexpr double ROUND_MS = 11.2, DRAFT_ROW_MS = 1.3, DRAFT_CALL_MS = 0.65, DECAY = 0.97;
418 std::vector<double> tried(MAXT, 1.0), stood(MAXT, 0.7); // a prior of 70% at every position
419 auto chain_len = [&](double call_ms) {
420 int best = K_;
421 double best_rate = -1.0, expect = 1.0, reach = 1.0;
422 for (int k = 1; k <= K_; ++k) { // at least d1: its acceptance stays measured (it pays above ~8% anyway)
423 reach *= stood[(size_t) k - 1] / tried[(size_t) k - 1], expect += reach;
424 const double rate = expect / (ROUND_MS + k * DRAFT_ROW_MS + std::max(0, k - 1) * call_ms);
425 if (rate > best_rate) best_rate = rate, best = k;
426 }
427 return best;
428 };
429 std::mt19937_64 rng(s.seed ? *s.seed : std::random_device{}());
430 std::uniform_real_distribution<double> unif(0.0, 1.0);
431 const auto t0 = std::chrono::steady_clock::now();
432 timing_ = &st;
433 std::vector<int> ctx(prompt_); // every token so far, the next window's first row last
434 int x = prompt_.back();
435 std::vector<int> drafts;
436 std::vector<Dist> qs; // per draft, the distribution it was drawn from; empty: a fixed draft (lookup, argmax)
437 bool mtp_round = false; // the drafts are an MTP chain (its acceptance feeds chain_len)
438 int mtp_chains = 0;
439 // one draft from the draft layer for the token after pen_seq (the penalty window before it)
440 auto draft_one = [&](int T, bool from_main, int src_row, const std::vector<int>& toks, int pos0, const std::vector<int>& pen_seq,
441 Dist& q) {
442 Engine::Penalties::Row pen;
443 if (pen_drafts) pen = penalty_row(pen_seq, s);
444 int d = e_.draft(T, from_main, src_row, toks, pos0, pen_drafts ? &pen : nullptr, samp_drafts);
445 st.draft_ms += e_.last_draft_ms, st.draft_gpu_ms += e_.last_draft_gpu_ms, ++st.draft_calls;
446 q = Dist{};
447 if (samp_drafts) {
448 q = make_dist(e_.last_draft_cands, s);
449 d = draw(q, unif(rng));
450 }
451 return d;
452 };
453 // DFlash2 in place of the draft layer: one drafter round after each window gives up to 7 fixed drafts with the selector's probabilities; the window
454 // keeps the drafts while the product of those probabilities stays at or over FNX_DF2_CONF (as the main engine's --draft-conf), at most K
455 const bool df2 = e_.df2_on();

Callers 4

batch_checkFunction · 0.45
lab_checkFunction · 0.45
soloMethod · 0.45
serve_probe.pyFile · 0.45

Calls 15

check_samplingFunction · 0.85
penalizesFunction · 0.85
penalty_rowFunction · 0.85
env_numFunction · 0.85
is_eosFunction · 0.85
lookupFunction · 0.85
moveFunction · 0.85
df2_onMethod · 0.80
max_ctxMethod · 0.80
last_logitsMethod · 0.80
push_backMethod · 0.80
assignMethod · 0.80

Tested by

no test coverage detected