okami.mom/strata

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

Niko1221Merge f-0216-core into f-0216-allbd0e878f4

main
4.8 KiB102 linesraw
1// The MMQ row-table margin (PR #5108): every index the stream-k fixup and the main kernel can read stays inside the table.
2// No GPU: the reads are enumerated from the layout the way mul_mat_q_stream_k_fixup computes them
3// (ids_dst[col_low + jt*J + j], jt < ceil(max_rows / J), j < J, col_low = the expert's first row).
4#include <algorithm>
5#include <cstdint>
6#include <cstdio>
7#include <random>
8#include <vector>
9
10#include "strata/prefill/mmq_ids_margin.hpp"
11
12using namespace strata::prefill::mmq;
13
14static int failed = 0;
15#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
16
17// the highest index (exclusive) one launch can read: bounds has n + 1 entries, max_rows the widest expert, J any tile width
18static int64_t fixup_read_end(const std::vector<int64_t>& bounds, int64_t max_rows, int J) {
19    const int64_t tiles = (max_rows + J - 1) / J;
20    int64_t end = 0;
21    for (size_t e = 0; e + 1 < bounds.size(); ++e)
22        end = std::max(end, bounds[e] + tiles * J);              // every expert's fixup reads whole tiles from its first row
23    return end;
24}
25static int64_t main_read_end(const std::vector<int64_t>& bounds, int J) {
26    int64_t end = 0;
27    for (size_t e = 0; e + 1 < bounds.size(); ++e)
28        if (bounds[e + 1] > bounds[e]) end = std::max(end, bounds[e + 1] + J - 1);   // up to J - 1 rows after the expert's last
29    return end;
30}
31
32int main() {
33    // the constants follow ggml's loop J = 8 .. 128
34    CHECK(kMmqTileColsMax == 128 && kMmqTileColsStep == 8);
35
36    // arithmetic: roundup(max_rows, J) + J
37    int64_t m = -1;
38    CHECK(mmq_ids_margin_rows(0, 128, &m) && m == 128);
39    CHECK(mmq_ids_margin_rows(1, 128, &m) && m == 256);
40    CHECK(mmq_ids_margin_rows(128, 128, &m) && m == 256);
41    CHECK(mmq_ids_margin_rows(129, 128, &m) && m == 384);
42    CHECK(mmq_ids_margin_rows(3072, 128, &m) && m == 3200);
43    CHECK(mmq_ids_margin_rows(3072, 8, &m) && m == 3080);
44    CHECK(mmq_ids_table_rows(30720, 3072) == 30720 + 3200);
45    CHECK(mmq_ids_table_rows(0, 0) == 128);
46
47    // checked arithmetic: nothing wraps to a small table
48    m = 77;
49    CHECK(!mmq_ids_margin_rows(-1, 128, &m) && m == 77);
50    CHECK(!mmq_ids_margin_rows(10, 0, &m) && !mmq_ids_margin_rows(10, -8, &m));
51    CHECK(!mmq_ids_margin_rows(INT64_MAX, 128, &m));
52    CHECK(!mmq_ids_margin_rows(INT64_MAX - 100, 128, &m));
53    CHECK(mmq_ids_table_rows(UINT64_MAX - 5, 10) == 0);
54    CHECK(mmq_ids_table_rows(10, (uint64_t) INT64_MAX + 1) == 0);
55    CHECK(mmq_ids_table_rows(UINT64_MAX, 0) == 0);
56
57    // synthetic layouts: the table is T*K rows + the margin, T tokens, K = 10 experts per token; late experts, empty tiles
58    const int64_t K = 10;
59    std::mt19937_64 rng(5108);
60    for (int iter = 0; iter < 2000; ++iter) {
61        const int64_t T = 1 + (int64_t) (rng() % (iter < 1000 ? 70 : 4096));
62        const int NE = 1 + (int) (rng() % 64);
63        std::vector<int64_t> cnt((size_t) NE, 0);
64        int64_t left = T * K, widest = 0;
65        // experts take at most one row per token; some are empty, the last ones often are
66        for (int e = 0; e < NE && left > 0; ++e) {
67            const int64_t c = std::min<int64_t>({(int64_t) (rng() % ((uint64_t) T + 1)), T, left});
68            cnt[(size_t) e] = c;
69            left -= c;
70        }
71        // whatever is left goes to experts that still have room (the rows must add up to T*K when they can)
72        for (int e = 0; e < NE && left > 0; ++e) {
73            const int64_t room = T - cnt[(size_t) e], c = std::min(room, left);
74            cnt[(size_t) e] += c;
75            left -= c;
76        }
77        std::vector<int64_t> bounds{0};
78        int64_t total = 0;
79        for (int64_t c : cnt) { total += c; bounds.push_back(total); widest = std::max(widest, c); }
80        CHECK(widest <= T);
81        const int64_t table = (int64_t) mmq_ids_table_rows((uint64_t) total, (uint64_t) T);
82        for (int J = kMmqTileColsStep; J <= kMmqTileColsMax; J += kMmqTileColsStep) {
83            // the launch uses max_rows = the widest expert here; the margin is sized for max_rows <= T
84            CHECK(fixup_read_end(bounds, widest, J) <= table);
85            CHECK(fixup_read_end(bounds, T, J) <= table);       // even a launch whose grid is sized for T
86            CHECK(main_read_end(bounds, J) <= table);
87        }
88    }
89
90    // the exact table of before the fix does fail on the late-expert layout the PR found (the guard this test pins)
91    {
92        const int64_t T = 100;
93        std::vector<int64_t> bounds{0, 100, 200, 200, 200};      // two real experts, then two empty late ones at row T*2
94        const int64_t exact = 200;
95        CHECK(fixup_read_end(bounds, 100, 128) > exact);
96        CHECK(fixup_read_end(bounds, 100, 128) <= (int64_t) mmq_ids_table_rows(200, (uint64_t) T));
97    }
98
99    if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
100    std::puts("mmq_ids_margin_test: ok");
101    return 0;
102}