File size: 984 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
#include "loop/day_loop.h"

#include <algorithm>

#include "common/base.h"

namespace hk {

static bool is_stop(const std::vector<int32_t>& stops, int32_t t) { return std::find(stops.begin(), stops.end(), t) != stops.end(); }

std::vector<int32_t> reader_greedy(Reader& reader, const std::vector<int32_t>& prompt, int max_new,
                                   const std::vector<int32_t>& stop_ids) {
    std::vector<int32_t> seq = prompt;
    SeqCheckpoint ck;
    ck.reset(reader.config());
    for (int n = 0; n < max_new; ++n) {
        SeqJob job;
        job.tokens = &seq;
        job.ckpt = &ck;
        job.end = (int)seq.size();
        job.save_at = (int)seq.size();   // each token is processed exactly once
        reader.sweep(job);
        int32_t tok = job.out.back().top_id[0];
        seq.push_back(tok);
        if (is_stop(stop_ids, tok)) break;
    }
    return std::vector<int32_t>(seq.begin() + (std::ptrdiff_t)prompt.size(), seq.end());
}

}  // namespace hk