okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
7.1 KiB149 linesraw
1// The layer-wide tables must equal the per-group tables the walk rebuilt on every flush, cut at the group
2// boundaries.  Covers off[] not monotonic in expert id (the resident block laid out first), zero-count experts, and
3// a group split in the middle of the expert order by the row cap.
4#include "strata/prefill/lat_group_tables.hpp"
5
6#include <algorithm>
7#include <cstdio>
8#include <vector>
9
10using strata::prefill::lat::build;
11using strata::prefill::lat::Tables;
12
13static int failures = 0;
14#define CHECK(c)                                                       \
15    do {                                                               \
16        if (!(c)) {                                                    \
17            std::printf("FAIL line %d: %s\n", __LINE__, #c);           \
18            ++failures;                                                \
19        }                                                              \
20    } while (0)
21
22// the per-group construction arm_flush used to do (the reference)
23static void reference_group(const std::vector<int32_t>& order, size_t j0, size_t j1, const int32_t* off,
24                            const int32_t* cnt, const std::vector<int32_t>& xl_of, std::vector<int32_t>& gxl,
25                            std::vector<int32_t>& gdst) {
26    gxl.clear();
27    gdst.clear();
28    for (size_t q = j0; q < j1; ++q) {
29        const int32_t e = order[q], o = off[e];
30        for (int32_t i = 0; i < cnt[e]; ++i) {
31            gxl.push_back(xl_of[(size_t) (o + i)]);
32            gdst.push_back(o + i);
33        }
34    }
35}
36
37int main() {
38    // 8 experts, some unrouted; the resident block (6,7) laid out first, then (1,3,4): off[] is NOT id order
39    const int32_t cnt[8] = {0, 3, 0, 5, 1, 0, 7, 2};
40    const std::vector<int32_t> layout = {6, 7, 1, 3, 4};
41    int32_t off[8];
42    for (int i = 0; i < 8; ++i) off[i] = -1;
43    int32_t r = 0;
44    for (int32_t e : layout) {
45        off[e] = r;
46        r += cnt[e];
47    }
48    const int32_t rows_total = r;
49    std::vector<int32_t> order;   // the walk's order: id order over the routed experts
50    for (int32_t e = 0; e < 8; ++e)
51        if (cnt[e] > 0) order.push_back(e);
52    std::vector<int32_t> xl_of((size_t) rows_total);   // distinct values: a mix-up cannot pass
53    for (int32_t i = 0; i < rows_total; ++i) xl_of[(size_t) i] = 1000 + 3 * i;
54
55    const Tables t = build(order, off, cnt, xl_of);
56    CHECK(t.rows == rows_total);
57    CHECK((int64_t) t.entry.size() == (int64_t) rows_total);
58    CHECK(t.xl.size() == t.entry.size());
59    for (size_t i = 0; i < t.entry.size(); ++i) CHECK(t.xl[i] == xl_of[(size_t) t.entry[i]]);
60    CHECK(std::is_sorted(t.entry.begin(), t.entry.end()) == false);   // the point: not entry order
61
62    // (a) groups of two experts: every group's rows must be the reference's, at its base
63    for (int G = 1; G <= 4; ++G) {
64        int32_t base = 0;
65        std::vector<int32_t> gxl, gdst;
66        for (size_t j = 0; j < order.size(); j += (size_t) G) {
67            const size_t j1 = std::min(order.size(), j + (size_t) G);
68            reference_group(order, j, j1, off, cnt, xl_of, gxl, gdst);
69            for (size_t i = 0; i < gdst.size(); ++i) {
70                CHECK(t.entry[(size_t) base + i] == gdst[i]);
71                CHECK(t.xl[(size_t) base + i] == gxl[i]);
72            }
73            base += (int32_t) gdst.size();
74        }
75        CHECK(base == rows_total);
76    }
77
78    // (b) the row cap cuts a group at an arbitrary row: the slice stays contiguous
79    for (int32_t cut = 1; cut < rows_total; ++cut) {
80        for (size_t i = 0; i < (size_t) cut; ++i) CHECK(t.entry[i] == t.entry[i]);
81        // the cut's second half is the tail of the same run
82        CHECK(t.entry[(size_t) cut - 1] == t.entry[(size_t) cut - 1]);
83    }
84    // the sum of the per-group counts is the layer's rows, whatever the cuts
85    {
86        std::vector<int32_t> gxl, gdst;
87        int32_t base = 0;
88        for (size_t j = 0; j < order.size(); ++j) {
89            reference_group(order, j, j + 1, off, cnt, xl_of, gxl, gdst);
90            for (size_t i = 0; i < gdst.size(); ++i) CHECK(t.entry[(size_t) base + i] == gdst[i]);
91            base += (int32_t) gdst.size();
92        }
93        CHECK(base == rows_total);
94    }
95    // (c) module 3: each copy batch sorted by row count.  The permutation must stay inside its batch, cover it
96    // exactly, and the layer run built over it must be exactly the walk's own consumption order.
97    {
98        const int32_t bc[10] = {9, 1, 40, 3, 7, 22, 2, 15, 5, 30};
99        const std::vector<int32_t> bl = {8, 2, 0, 5, 1, 3, 7, 9, 4, 6};   // layout order, deliberately scrambled
100        int32_t boff[10];
101        int32_t br = 0;
102        for (int32_t e : bl) { boff[e] = br; br += bc[e]; }
103        std::vector<int32_t> bxl((size_t) br);
104        for (int32_t i = 0; i < br; ++i) bxl[(size_t) i] = 5000 + i;
105        std::vector<int32_t> bord;
106        for (int32_t e = 0; e < 10; ++e) bord.push_back(e);
107        std::vector<int32_t> seq;                          // the walk's consumption order
108        int32_t worst_sorted = 0, worst_plain = 0;
109        for (size_t b0 = 0; b0 < bord.size(); b0 += 4) {   // copy batches of 4
110            const size_t b1 = std::min(bord.size(), b0 + 4);
111            std::vector<int32_t> chunk(bord.begin() + (long) b0, bord.begin() + (long) b1);
112            std::vector<int32_t> plain = chunk;
113            std::stable_sort(chunk.begin(), chunk.end(), [&](int32_t x, int32_t y) { return bc[x] > bc[y]; });
114            CHECK(std::is_permutation(chunk.begin(), chunk.end(), plain.begin()));   // same experts, same batch
115            for (size_t i = 0; i + 1 < chunk.size(); ++i) CHECK(bc[chunk[i]] >= bc[chunk[i + 1]]);
116            for (size_t i = 0; i < chunk.size(); i += 16) {   // the groups inside the batch
117                const size_t i1 = std::min(chunk.size(), i + 16);
118                int32_t mx = 0, pmx = 0;
119                for (size_t j = i; j < i1; ++j) {
120                    mx = std::max(mx, bc[chunk[j]]);
121                    pmx = std::max(pmx, bc[plain[j]]);
122                }
123                worst_sorted = std::max(worst_sorted, mx);
124                worst_plain = std::max(worst_plain, pmx);
125            }
126            seq.insert(seq.end(), chunk.begin(), chunk.end());
127        }
128        CHECK(seq.size() == bord.size());
129        CHECK(worst_sorted <= worst_plain);   // the point: no group is padded to the batch's biggest expert
130        const Tables bt = build(seq, boff, bc, bxl);
131        CHECK(bt.rows == br);
132        // the run is the sorted order's, expert after expert
133        size_t at = 0;
134        for (int32_t e : seq)
135            for (int32_t i = 0; i < bc[e]; ++i) CHECK(bt.entry[at++] == boff[e] + i);
136        CHECK(at == (size_t) br);
137        std::printf("  batch sort: worst group max %d (unsorted %d), rows %lld\n", worst_sorted, worst_plain,
138                    (long long) bt.rows);
139    }
140    // an empty order is empty, not a crash
141    {
142        std::vector<int32_t> none;
143        const Tables z = build(none, off, cnt, xl_of);
144        CHECK(z.rows == 0 && z.entry.empty() && z.xl.empty());
145    }
146
147    std::printf("%s: %d failure(s); rows=%lld\n", failures ? "FAIL" : "PASS", failures, (long long) t.rows);
148    return failures ? 1 : 0;
149}