// Draft and check: how the engine writes real Kimi's text without paying one // sweep per token. // // A sweep costs about the same whether it covers one new position or eight: // most of it is reading the trunk and the experts from the SSD. So each round // // 1. the drafter guesses the next few tokens after the last one real Kimi // wrote (drafter/dspark.h), from real Kimi's own hidden states; // 2. real Kimi sweeps that last token and the guesses together; // 3. the guesses are kept for as long as each one is the token real Kimi puts // first at that place; real Kimi's own token follows the last one kept, // so even a round whose first guess is wrong yields one token. // // Nothing the drafter says reaches the text unless real Kimi would have written // it at that place itself. And because a position's numbers do not depend on // what it is swept with (docs/exact.md), the text is the same, token for token, // as real Kimi writing alone (`hkimi run --reader-only`). The tests check that. // // The checkpoint real Kimi continues from is put after the last kept token // without sweeping anything again (SeqJob::keep_tail), and the number of tokens // drafted is chosen each round from the drafter's confidence and from what one // more position costs, measured as the answer goes. #pragma once #include #include "drafter/dspark.h" #include "loop/day_loop.h" #include "reader/reader.h" namespace hk { // How many of the drafted tokens to have checked. conf[i]: the chance token i is accepted // when those before it were (empty: 0.8 each). A sweep over 1 + n positions costs // 1 + extra * n in units of a sweep over one, and yields the accepted tokens plus one. // Returns the n in [0, limit] with the most tokens per unit of cost. int best_check_len(const std::vector& conf, int limit, double extra); class DraftSession { public: DraftSession(Reader& reader, DSpark& drafter); // appends `prompt` to the conversation and writes real Kimi's continuation DayResult generate(const std::vector& prompt, const DayOptions& opt, const DayCallbacks& cb = DayCallbacks()); const std::vector& context() const { return seq_; } void reset(); // What one more position adds to a sweep, as a share of a sweep over one: `start` until // enough sweeps of different lengths were timed, then measured. double extra_cost(double start) const; int64_t drafted() const { return drafted_; } int64_t accepted() const { return accepted_; } // tests: every drafted block is also kept here struct Drafted { int anchor_pos = 0; DraftBlock block; int checked = 0, kept = 0; }; std::vector* record = nullptr; private: Reader& reader_; DSpark& drafter_; std::vector seq_; // the conversation: every token given or written SeqCheckpoint ckpt_; // real Kimi's state; ckpt_.pos == drafter_.context() <= seq_.size() int64_t drafted_ = 0, accepted_ = 0; // timings of the sweeps over the last token and n drafted ones, for extra_cost() double sum_n_ = 0, sum_t_ = 0, sum_nn_ = 0, sum_nt_ = 0; int timed_ = 0; int min_n_ = 1 << 30, max_n_ = -1; }; } // namespace hk