diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp index 7d46389d..afd1ef72 100644 --- a/src/core/mtp.cpp +++ b/src/core/mtp.cpp @@ -1257,7 +1257,9 @@ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* if (!mtp_catchup_all()) T = a + 1; const bool cp = coupled_active_; // coupled draft sampling for this request: its own graphs if (!capture_round(T, cp, err)) return false; - const int max_steps = std::min(max_t_ - 1, max_drafts_); + // Draft j writes its K/V at cell p + a + j, and the K/V ends at max_cells (its page table has no page past it): + // near the context's end the round drafts only the cells that exist + const int max_steps = (int) std::min(std::min(max_t_ - 1, max_drafts_), st_.max_cells - (p + a)); for (int j = 1; j < max_steps; ++j) if (!capture_step(j, cp, err)) return false; const Clock::time_point t0 = Clock::now(); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3248acb1..e76e582b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -12237,13 +12237,18 @@ int main(int argc, char** argv) { if (pk.lookup) { T = pk.t; from_sfx = true; } } } + // The window's rows sit at p .. p + T - 1, and past the context there is no cell to verify them in: near + // its end the window takes only the rows that fit (the admission checked the prompt and max_new, which a + // full window can overrun) + const int room = (int) std::min(o.spec, o.max_context - p); + T = std::min(T, room); // --lookup-chain: what followed an earlier occurrence of the context + the MTP's drafts, after them int chain_n = 0, cm = 0; - if (o.lookup_chain > 0 && use_mtp && !first_window && !from_sfx && T < o.spec) { + if (o.lookup_chain > 0 && use_mtp && !first_window && !from_sfx && T < room) { int csrc = -1; const int nt = chain_tail(sfx, drafts.data(), T - 1, ctail); chain_n = strata::spec::propose_from_sources(lookup_src, ctail.data(), nt, T - 1, - std::min(o.lookup_chain, o.spec - T), o.lookup_chain_min, + std::min(o.lookup_chain, room - T), o.lookup_chain_min, cbuf.data(), &cm, &csrc); if (chain_n > 0 && std::getenv("STRATA_LOOKUP_CHAIN_FIXED") == nullptr) { double p_mtp = 1.0;