okami.mom/strata

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

Niko1221rc1 merge df2-sycl slot-penalty port (e8a6afd5f)f32c3c461

main
4.1 KiB89 linesraw
1// Penalty-history rows of a BATCH window, per slot (strata::kernels::slot_penalty_rows).  Header-only, CPU, no model.
2//
3// Row t of a batch window feeds tokens[t] into slot rows[t]; its history is the slot's reply tail followed by that slot's tokens in this window up to
4// and including row t - what penalty_rows gives a solo window of that slot, whatever the other slots do.
5#include "strata/kernels/sampler.hpp"
6
7#include <cstdio>
8#include <algorithm>
9#include <cstdlib>
10#include <random>
11#include <vector>
12
13using strata::kernels::penalty_rows;
14using strata::kernels::slot_penalty_rows;
15
16namespace {
17int checks = 0;
18void check(bool ok, const char* label) {
19    ++checks;
20    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
21}
22}  // namespace
23
24int main() {
25    std::mt19937 rng(7);
26    constexpr int kSlots = 6, kH = 16;
27    for (int trial = 0; trial < 400; ++trial) {
28        // per slot: a reply tail of 0..40 tokens
29        std::vector<std::vector<int32_t>> tail(kSlots);
30        std::vector<const int32_t*> tails(kSlots, nullptr);
31        std::vector<int64_t> n_tails(kSlots, 0);
32        for (int b = 0; b < kSlots; ++b) {
33            const int n = (int) (rng() % 41);
34            for (int i = 0; i < n; ++i) tail[b].push_back((int32_t) (rng() % 500));
35            tails[b] = tail[b].empty() ? nullptr : tail[b].data();
36            n_tails[b] = (int64_t) tail[b].size();
37        }
38        // a window: 1..8 rows; each slot at most once, or as a contiguous group of 1..3 rows (MTP drafts)
39        std::vector<int> rows; std::vector<int32_t> toks;
40        std::vector<int> order = {0, 1, 2, 3, 4, 5};
41        std::shuffle(order.begin(), order.end(), rng);
42        for (int b : order) {
43            if (rows.size() >= 8) break;
44            if (rng() % 3 == 0) continue;
45            const int g = 1 + (int) (rng() % 3);
46            for (int j = 0; j < g && rows.size() < 8; ++j) { rows.push_back(b); toks.push_back((int32_t) (rng() % 500)); }
47        }
48        if (rows.empty()) { rows.push_back(2); toks.push_back(9); }
49        const int S = (int) rows.size();
50        std::vector<int32_t> out((size_t) S * kH, 12345);
51        slot_penalty_rows(rows.data(), toks.data(), S, tails.data(), n_tails.data(), kH, out.data());
52        // the definition: per slot, penalty_rows over (its tail, its rows of this window)
53        for (int b = 0; b < kSlots; ++b) {
54            std::vector<int> idx;
55            std::vector<int32_t> win;
56            for (int t = 0; t < S; ++t) if (rows[t] == b) { idx.push_back(t); win.push_back(toks[t]); }
57            if (idx.empty()) continue;
58            std::vector<int32_t> ref(idx.size() * kH);
59            penalty_rows(tail[b].data(), (int64_t) tail[b].size(), win.data(), (int) idx.size(), kH, ref.data());
60            for (size_t k = 0; k < idx.size(); ++k)
61                for (int j = 0; j < kH; ++j)
62                    check(out[(size_t) idx[k] * kH + j] == ref[k * kH + j], "slot row equals penalty_rows of the slot alone");
63        }
64    }
65    // ---- fixed cases: an empty tail, the fed-back head counted, other slots' tokens never in a row
66    {
67        const int rows[3] = {0, 1, 1};
68        const int32_t toks[3] = {10, 20, 21};
69        const int32_t t0[2] = {1, 2};
70        const int32_t t1[3] = {7, 8, 9};
71        const int32_t* tails[2] = {t0, t1};
72        const int64_t n_tails[2] = {2, 3};
73        std::vector<int32_t> out(3 * 4, 0);
74        slot_penalty_rows(rows, toks, 3, tails, n_tails, 4, out.data());
75        const int32_t e[12] = {-1, 1, 2, 10,    7, 8, 9, 20,    8, 9, 20, 21};
76        for (int i = 0; i < 12; ++i) check(out[i] == e[i], "fixed case");
77    }
78    {   // a slot with no tail (no penalty history): its row holds the fed token only
79        const int rows[1] = {0};
80        const int32_t toks[1] = {42};
81        const int32_t* tails[1] = {nullptr};
82        const int64_t n_tails[1] = {0};
83        int32_t out[4] = {0, 0, 0, 0};
84        slot_penalty_rows(rows, toks, 1, tails, n_tails, 4, out);
85        check(out[0] == -1 && out[1] == -1 && out[2] == -1 && out[3] == 42, "empty tail");
86    }
87    std::printf("slot_history_test: %d checks passed\n", checks);
88    return 0;
89}