okami.mom/strata

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

Niko1221rc1 merge df2-sycl (903badd90): window_rule.hpp/window_rule_test.cpp add/add (kept the mainline draft_on rule, added SYCL's lookup-skip helpers), drafting.py CONF_STEPS (kept the engine-slot steps, added BACKEND_CONF sycl 0.6), SYCL generate.cpp sets wr.draft_onb5abd7792

main
6.7 KiB115 linesraw
1// sycl/src/core/dflash2_ring_test.cpp - DF2-SYCL: the drafter's ring log on an Intel GPU.  A drafter with the real Q4_0 pack and a random head fills its K/V ring past
2// the wrap (positions 0..2999 of one conversation), the ring is snapshotted, a longer turn overwrites part of it (3000..3199), and a request that resumes at
3// 3000 asks begin_request for the window [n - 2047, 3000): the slots a later position took must be rebuilt from the log, bitwise the original rows.  Then another
4// conversation takes the ring (the first one is parked in the stash), and the first comes back: the whole window is rebuilt.
5//   dflash2_ring_test PACK
6#define DPCT_PROFILING_ENABLED
7#include <sycl/sycl.hpp>
8#include <dpct/dpct.hpp>
9#include "strata/core/dflash2.hpp"
10#include "strata/kernels/dflash2.hpp"
11
12#include <algorithm>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <vector>
17
18using strata::core::Dflash2Drafter;
19
20static uint32_t rs = 777;
21static float frand() { rs = rs * 1664525u + 1013904223u; return (float) ((rs >> 8) & 0xffff) / 65536.0f - 0.5f; }
22
23static int g_fail = 0;
24static void check(bool ok, const char* what) { std::printf("  %-100s %s\n", what, ok ? "ok" : "FAIL"); if (!ok) ++g_fail; }
25
26int main(int argc, char** argv) {
27    if (argc < 2) { std::fprintf(stderr, "usage: dflash2_ring_test PACK\n"); return 2; }
28    sycl::queue* q = &dpct::get_in_order_queue();
29    std::printf("device: %s\n", q->get_device().get_info<sycl::info::device::name>().c_str());
30    Dflash2Drafter d;
31    std::string err;
32    if (!d.load(argv[1], 0, err)) { std::fprintf(stderr, "load: %s\n", err.c_str()); return 1; }
33    const int HR = 4096, RB = 2560 / 32 * 34;
34    std::vector<uint8_t> hw((size_t) HR * RB);
35    for (auto& b : hw) b = (uint8_t) (rs = rs * 1664525u + 1013904223u, rs >> 12);
36    for (size_t r = 0; r < (size_t) HR * (2560 / 32); ++r) { hw[r * 34] = 0x00; hw[r * 34 + 1] = 0x1c; }   // finite scales
37    uint8_t* head = sycl::malloc_device<uint8_t>(hw.size(), *q);
38    q->memcpy(head, hw.data(), hw.size()).wait();
39    auto embed = [](const int32_t*, int n, float* out, sycl::queue* s) { s->memset(out, 0, (size_t) n * 2560 * 4); };
40    if (!d.bind_raw(embed, head, 8, HR, {}, err)) { std::fprintf(stderr, "bind: %s\n", err.c_str()); return 1; }
41    const int TAPN = 5 * 2560, CH = 64;
42    float* taps = sycl::malloc_device<float>((size_t) CH * TAPN, *q);
43    std::vector<float> ht((size_t) CH * TAPN);
44    auto feed = [&](const std::vector<int32_t>& ids, int64_t p0, int64_t p1) {
45        for (int64_t p = p0; p < p1; p += CH) {
46            const int m = (int) std::min<int64_t>(CH, p1 - p);
47            for (auto& v : ht) v = frand() * 2.0f;
48            q->memcpy(taps, ht.data(), (size_t) m * TAPN * 4).wait();
49            if (!d.context(taps, m, p, err)) { std::fprintf(stderr, "context: %s\n", err.c_str()); std::exit(1); }
50            d.note_rows(ids.data() + p, m, p);
51        }
52    };
53    std::vector<int32_t> big(4000), other(1000);
54    for (int i = 0; i < 4000; ++i) big[(size_t) i] = (i * 7919) % 5000;
55    for (int i = 0; i < 1000; ++i) other[(size_t) i] = (i * 104729 + 17) % 5000;
56    std::printf("ring log %s\n", d.ring_log_on() ? "on" : "OFF");
57
58    d.reset();
59    bool cold = false;
60    d.begin_request(big.data(), 3031, 0, &cold);
61    feed(big, 0, 3000);
62    std::vector<float> kvA, kvB, kvC;
63    std::vector<int64_t> tgA, tgB, tgC;
64    if (!d.debug_ring(kvA, tgA, err)) { std::fprintf(stderr, "%s\n", err.c_str()); return 1; }
65    feed(big, 3000, 3200);   // a longer turn: positions 3000..3199 take the slots of 936..1135
66    const size_t per = (size_t) Dflash2Drafter::kRing * 8 * 128;
67    auto compare = [&](const std::vector<float>& kv, const std::vector<int64_t>& tg, int64_t lo, int64_t hi, const char* what) {
68        int64_t bad_tag = 0, bad_kv = 0;
69        for (int64_t p = lo; p < hi; ++p) {
70            const int s = (int) (p % Dflash2Drafter::kRing);
71            if ((tg[(size_t) s] & 0xffffffffll) != p) { ++bad_tag; continue; }
72            bool differs = false;
73            for (int l = 0; l < 5 && !differs; ++l)
74                for (int kvsel = 0; kvsel < 2 && !differs; ++kvsel)
75                    for (int h = 0; h < 8 && !differs; ++h) {
76                        const size_t off = ((size_t) l * 2 + kvsel) * per + ((size_t) h * Dflash2Drafter::kRing + s) * 128;
77                        if (std::memcmp(&kv[off], &kvA[off], 128 * 4) != 0) differs = true;
78                    }
79            if (differs) ++bad_kv;
80        }
81        char b[200];
82        std::snprintf(b, sizeof b, "%s: positions %lld..%lld, %lld wrong tags, %lld K/V rows differ from the original update", what, (long long) lo, (long long) hi - 1,
83                      (long long) bad_tag, (long long) bad_kv);
84        check(bad_tag == 0 && bad_kv == 0, b);
85    };
86    if (!d.debug_ring(kvB, tgB, err)) return 1;
87    {
88        int64_t bad = 0;
89        for (int64_t p = 983; p < 1136; ++p) if ((tgB[(size_t) (p % Dflash2Drafter::kRing)] & 0xffffffffll) != p) ++bad;
90        check(bad > 0, "before the request, slots of positions 983..1135 hold later positions (the test can tell)");
91    }
92    d.begin_request(big.data(), 3030, 3000, &cold);
93    std::printf("  repeat/rewind: rebuilt %lld rows (missing %lld) in %.2f ms, cold %d\n", (long long) d.last_rebuilt, (long long) d.last_missing, d.last_ms, (int) cold);
94    check(d.last_rebuilt > 0 && d.last_missing == 0 && !cold, "begin_request rebuilt rows from the log, none missing, not cold");
95    if (!d.debug_ring(kvC, tgC, err)) return 1;
96    compare(kvC, tgC, 3030 - 2047, 3000, "after the rebuild");
97
98    // another conversation takes the ring; the first one is parked, then comes back
99    d.begin_request(other.data(), 400, 0, &cold);
100    feed(other, 0, 400);
101    check(d.ring_stats.stash_stores >= 1, "another conversation starting at 0: the first lineage is kept in the stash as a branch");
102    d.begin_request(big.data(), 3030, 3000, &cold);
103    std::printf("  parked lineage back: rebuilt %lld rows (missing %lld) in %.2f ms, swapped %d, cold %d\n", (long long) d.last_rebuilt, (long long) d.last_missing, d.last_ms, (int) d.last_swapped, (int) cold);
104    check(d.last_swapped && d.last_rebuilt >= 1900 && d.last_missing == 0 && !cold, "the parked lineage came back, the whole window rebuilt, none missing");
105    if (!d.debug_ring(kvC, tgC, err)) return 1;
106    compare(kvC, tgC, 3030 - 2047, 3000, "after the parked lineage came back");
107
108    // an edited prompt: the history differs at 2500, resume 2800: not covered, no stash match: starts over, cold
109    std::vector<int32_t> edit = big;
110    edit[2500] ^= 1;
111    d.begin_request(edit.data(), 2830, 2800, &cold);
112    check(cold || d.last_missing > 0, "an edited conversation: the drafter reports it has no log of the window (cold)");
113    std::printf("%s\n", g_fail ? "SOME CHECKS FAILED" : "ALL RING CHECKS PASSED");
114    return g_fail ? 1 : 0;
115}