okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
8.1 KiB173 linesraw
1// Batched windows (Engine::step) against one sequence at a time: each conversation's GEN greedy tokens alone (one row a
2// window, no drafts), then the conversations on lanes decoding together, one row per lane a window; each lane's tokens
3// must equal its conversation's alone. For 1, 2, 3 and 6 lanes (prompts reused round-robin): the window time and the
4// total tokens a second. With FNX_BATCH_DRAFTS (lookup or 1) the same again with drafts in the batched windows
5// (BatchDecoder: several rows a lane, each lane cut back to what it accepts, lanes leaving as they finish), greedy and
6// equal to alone; then a sampled run at 3 lanes, which must only run (its tokens differ by chance). bin/batch_test and
7// the lab's "batch" command.
8#pragma once
9#include <algorithm>
10#include <chrono>
11#include <cstdio>
12#include <random>
13#include <vector>
14
15#include "../src/batch.hpp"
16#include "../src/engine.hpp"
17#include "../src/session.hpp"
18
19inline int batch_check(const fnx::Model& m, fnx::Engine& e, const fnx::Mtp& mtp, int gen, const std::vector<std::vector<int>>& prompts) {
20    e.mtp_attach(mtp);
21    const std::vector<int> eos{248044, 248046};
22    fnx::Sampling greedy;
23    // alone: one row a window
24    std::vector<std::vector<int>> solo(prompts.size());
25    {
26        fnx::Session s(e, 4, 7, eos);
27        s.speculate = false;
28        for (size_t i = 0; i < prompts.size(); ++i) {
29            e.reset();
30            fnx::GenStats st;
31            s.read(prompts[i], st);
32            s.generate(gen, greedy, [&](int t) { solo[i].push_back(t); }, nullptr, st);
33        }
34    }
35    e.reset();
36    std::vector<int> lanes{0};
37    while ((int) lanes.size() < 6) lanes.push_back(e.add_lane());
38    // a lane's tokens against its conversation's alone: every token of the reference up to its first end-of-reply (past
39    // it the plain batch keeps decoding), none missing: a lane that ends early or emits nothing does not match
40    auto matches = [&](const std::vector<int>& got, const std::vector<int>& ref) {
41        size_t want = ref.size();
42        for (size_t j = 0; j < ref.size(); ++j)
43            if (ref[j] == 248044 || ref[j] == 248046) {
44                want = j + 1;
45                break;
46            }
47        return got.size() >= want && std::equal(ref.begin(), ref.begin() + (long) want, got.begin());
48    };
49    bool ok = true;
50    for (int n : {1, 2, 3, 6}) {
51        std::vector<int> use(lanes.begin(), lanes.begin() + n), x(n);
52        std::vector<std::vector<int>> got(n);
53        for (int r = 0; r < n; ++r) {
54            e.use_lane(use[r]);
55            e.reset();
56            fnx::Session s(e, 4, 7, eos, 248045, 0, use[r]);
57            fnx::GenStats st;
58            s.read(prompts[r % prompts.size()], st);
59            x[r] = prompts[r % prompts.size()].back();
60        }
61        double ms = 0;
62        int windows = 0;
63        while ((int) got[0].size() < gen) {
64            const auto t0 = std::chrono::steady_clock::now();
65            const std::vector<int> o = e.step(use, x);
66            ms += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
67            ++windows;
68            for (int r = 0; r < n; ++r) {
69                got[r].push_back(o[r]);
70                x[r] = o[r];
71            }
72        }
73        int same = 0;
74        for (int r = 0; r < n; ++r) same += matches(got[r], solo[r % prompts.size()]);
75        std::printf("%d lanes: %d windows, %.2f ms a window (first one captures its graph), %.1f tokens/s in all; %d of %d lanes match alone\n",
76                    n, windows, ms / windows, n * windows * 1000.0 / ms, same, n);
77        ok = ok && same == n;
78    }
79    std::printf("%s\n", ok ? "batched windows give each lane its tokens alone" : "MISMATCH");
80    if (fnx::batch_drafts() == fnx::BatchDrafts::off) return ok ? 0 : 1;
81    bool dok = true;
82    fnx::BatchDecoder dec(e, 4, 15, eos, fnx::batch_drafts());
83    // the lanes on their prompts, each lane's next row its prompt's last token
84    auto start = [&](int n, const fnx::Sampling& s, std::vector<fnx::BatchLane>& bl) {
85        bl.assign((size_t) n, fnx::BatchLane{});
86        for (int r = 0; r < n; ++r) {
87            e.use_lane(lanes[(size_t) r]);
88            e.reset();
89            fnx::Session sess(e, 4, 7, eos, 248045, 0, lanes[(size_t) r]);
90            fnx::GenStats st;
91            const std::vector<int>& p = prompts[r % prompts.size()];
92            sess.read(p, st);
93            bl[(size_t) r].start(lanes[(size_t) r], s, std::mt19937_64(1000 + r), p);
94        }
95    };
96    // windows until every lane has gen tokens or ended its reply: the tokens per lane, windows, rows, ms
97    auto run = [&](std::vector<fnx::BatchLane>& bl, std::vector<std::vector<int>>& got, int& windows, int& rows, double& ms) {
98        const int n = (int) bl.size();
99        got.assign((size_t) n, {});
100        std::vector<bool> ended((size_t) n, false);
101        windows = rows = 0;
102        ms = 0;
103        for (;;) {
104            std::vector<fnx::BatchLane*> ls;
105            std::vector<int> idx, limit;
106            for (int r = 0; r < n; ++r)
107                if (!ended[(size_t) r] && (int) got[(size_t) r].size() < gen) {
108                    ls.push_back(&bl[(size_t) r]);
109                    idx.push_back(r);
110                    limit.push_back(gen - (int) got[(size_t) r].size());
111                }
112            if (ls.empty()) break;
113            fnx::BatchDecoder::fit(ls);
114            const auto t0 = std::chrono::steady_clock::now();
115            const auto out = dec.window(ls, limit, [&](int i, int t) { got[(size_t) idx[(size_t) i]].push_back(t); });
116            ms += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
117            ++windows;
118            rows += dec.last_rows;
119            for (size_t i = 0; i < out.size(); ++i)
120                if (out[i].eos) ended[(size_t) idx[i]] = true;
121        }
122    };
123    const char* mode = fnx::batch_drafts() == fnx::BatchDrafts::lookup ? "lookup drafts" : "lookup drafts and the draft layer's chain";
124    for (int n : {1, 2, 3, 6}) {
125        std::vector<fnx::BatchLane> bl;
126        start(n, greedy, bl);
127        std::vector<std::vector<int>> got;
128        int windows = 0, rows = 0;
129        double ms = 0;
130        run(bl, got, windows, rows, ms);
131        int same = 0;
132        size_t tokens = 0;
133        long offered = 0, accepted = 0;
134        for (int r = 0; r < n; ++r) {
135            same += matches(got[(size_t) r], solo[r % prompts.size()]);
136            tokens += got[(size_t) r].size();
137            offered += bl[(size_t) r].offered;
138            accepted += bl[(size_t) r].accepted;
139        }
140        std::printf("%d lanes with %s: %d windows, %.1f rows and %.2f ms a window, %.1f tokens/s in all, drafts %ld of %ld; %d of %d "
141                    "lanes match alone\n",
142                    n, mode, windows, (double) rows / windows, ms / windows, tokens * 1000.0 / ms, accepted, offered, same, n);
143        dok = dok && same == n;
144    }
145    {   // sampled (prod's values): it must run; nothing to compare
146        fnx::Sampling s;
147        s.temperature = 0.6f;
148        s.top_p = 0.95f;
149        s.pen_present = 1.0f;
150        std::vector<fnx::BatchLane> bl;
151        start(3, s, bl);
152        std::vector<std::vector<int>> got;
153        int windows = 0, rows = 0;
154        double ms = 0;
155        run(bl, got, windows, rows, ms);
156        bool in_vocab = true;
157        size_t tokens = 0;
158        for (const auto& g : got) {
159            tokens += g.size();
160            for (int t : g) in_vocab = in_vocab && t >= 0 && t < m.vocab;
161        }
162        std::printf("3 lanes sampled (temperature 0.6, top_p 0.95, presence 1.0): %d windows, %.1f rows a window, %.1f tokens/s in all%s\n",
163                    windows, (double) rows / windows, tokens * 1000.0 / ms, in_vocab ? "" : ": A TOKEN OUT OF THE VOCABULARY");
164        dok = dok && in_vocab;
165    }
166    for (int i : lanes) {
167        e.use_lane(i);
168        e.reset();
169    }
170    std::printf("%s\n", dok ? "batched windows with drafts give each lane its tokens alone" : "MISMATCH with drafts");
171    ok = ok && dok;
172    return ok ? 0 : 1;
173}