File size: 3,264 Bytes
68888be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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 <vector>

#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<float>& 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<int32_t>& prompt, const DayOptions& opt, const DayCallbacks& cb = DayCallbacks());

    const std::vector<int32_t>& 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<Drafted>* record = nullptr;

   private:
    Reader& reader_;
    DSpark& drafter_;
    std::vector<int32_t> 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