okami.mom/strata

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

Niko1221rc1 merge df2-ring (09ec63b88)f7e24459b

main
11.1 KiB207 linesraw
1// src/core/dflash2_ring_test.cpp - the drafter's ring log and rebuild (Dflash2Drafter::begin_request), against the ring an uninterrupted update sequence left.
2//
3//     dflash2_ring_test PACK [device]
4//
5// Context rows are fed with deterministic pseudo-random taps (a function of the position, so any lineage can be refed), for lineage A up to a point P, the ring is
6// copied out, then the update goes on far enough to wrap the ring (and, in the other scenarios, another lineage takes over).  begin_request must then rebuild the
7// window the first draft reads, [n - 2047, resume), and the K/V bytes of every rebuilt slot must equal, bit for bit, those of the copy taken at P:
8//   1 rewind     : A fed to P + 600, rewound to P (a repeated or rewound prefix)             -> rebuild from the live log
9//   2 parked     : A fed to P, B fed (different tokens and taps), A resumed at P             -> swap from the stash, rebuild
10//   3 branch     : A fed to P + 700, a request branches at P - 300 with other tokens, then A's old branch resumes at P -> stash copy (shared chunks, copy on write)
11//   4 off        : STRATA_DF2_RING_LOG=0 (env set before the run): the same rewind finds the window blank (the old behaviour), reported not asserted
12//   5 evicted    : a stash too small to keep A: the resume is cold (missing rows reported, *cold set)
13// Exit 0 when every assertion holds.
14#include "strata/core/dflash2.hpp"
15
16#include <cuda_runtime.h>
17
18#include <cmath>
19#include <cstdint>
20#include <cstdio>
21#include <cstdlib>
22#include <cstring>
23#include <string>
24#include <vector>
25
26using strata::core::Dflash2Drafter;
27
28namespace {
29int g_fail = 0;
30void check(bool ok, const char* what) {
31    std::printf("  %-100s %s\n", what, ok ? "ok" : "FAIL");
32    if (!ok) ++g_fail;
33}
34
35constexpr int kTap = 12800;   // 5 taps x 2560
36
37float tap_value(uint32_t lineage, int64_t pos, int i) {
38    uint32_t x = (uint32_t) pos * 2654435761u ^ (uint32_t) i * 40503u ^ lineage * 97531u;
39    x ^= x >> 15; x *= 2246822519u; x ^= x >> 13; x *= 3266489917u; x ^= x >> 16;
40    return ((float) (x & 0xFFFF) / 32768.0f - 1.0f) * (1.0f + 0.25f * (float) (i % 7));
41}
42
43// feeds the rows [from, to) of `lineage` (tokens: token_of(lineage, pos)) as context updates, 256 rows a call, and notes the tokens
44int32_t token_of(uint32_t lineage, int64_t pos) { return (int32_t) (1000 + ((uint32_t) pos * 7919u + lineage * 104729u) % 50000u); }
45
46bool feed(Dflash2Drafter& d, float* dev, uint32_t lineage, int64_t from, int64_t to, std::string& err) {
47    std::vector<float> h((size_t) 256 * kTap);
48    for (int64_t p0 = from; p0 < to; p0 += 256) {
49        const int64_t n = std::min<int64_t>(256, to - p0);
50        for (int64_t r = 0; r < n; ++r)
51            for (int i = 0; i < kTap; ++i) h[(size_t) r * kTap + (size_t) i] = tap_value(lineage, p0 + r, i);
52        if (cudaMemcpy(dev, h.data(), (size_t) n * kTap * 4, cudaMemcpyHostToDevice) != cudaSuccess) { err = "upload"; return false; }
53        if (!d.context(dev, n, p0, err)) return false;
54        std::vector<int32_t> tk((size_t) n);
55        for (int64_t r = 0; r < n; ++r) tk[(size_t) r] = token_of(lineage, p0 + r);
56        d.note_rows(tk.data(), n, p0);
57    }
58    return true;
59}
60
61std::vector<int32_t> ids_of(uint32_t lineage, int64_t n) {
62    std::vector<int32_t> v((size_t) n);
63    for (int64_t p = 0; p < n; ++p) v[(size_t) p] = token_of(lineage, p);
64    return v;
65}
66
67// bytes of slot s of layer l: K and V, 8 heads x 128 floats
68bool slot_equal(const std::vector<float>& a, const std::vector<float>& b, int slot) {
69    const size_t per = (size_t) Dflash2Drafter::kRing * 8 * 128;
70    for (int l = 0; l < 5; ++l)
71        for (int kv = 0; kv < 2; ++kv)
72            for (int h = 0; h < 8; ++h) {
73                const size_t off = ((size_t) l * 2 + (size_t) kv) * per + ((size_t) h * Dflash2Drafter::kRing + (size_t) slot) * 128;
74                if (std::memcmp(&a[off], &b[off], 128 * 4) != 0) return false;
75            }
76    return true;
77}
78
79// every slot of the positions [lo, hi) holds the row `ref` has (same bytes), tagged with the drafter's current epoch and that position
80int count_bad(Dflash2Drafter& d, const std::vector<float>& ref, int64_t lo, int64_t hi, int* wrong_tag) {
81    std::vector<float> now;
82    std::vector<int64_t> tags;
83    std::string err;
84    if (!d.debug_ring(now, tags, err)) { std::printf("debug_ring: %s\n", err.c_str()); return 1 << 20; }
85    int bad = 0;
86    *wrong_tag = 0;
87    for (int64_t p = lo; p < hi; ++p) {
88        const int slot = (int) (p % Dflash2Drafter::kRing);
89        if ((tags[(size_t) slot] & 0xFFFFFFFFll) != p || tags[(size_t) slot] < 0) { ++*wrong_tag; ++bad; continue; }
90        if (!slot_equal(now, ref, slot)) ++bad;
91    }
92    return bad;
93}
94}  // namespace
95
96int main(int argc, char** argv) {
97    if (argc < 2) { std::printf("usage: dflash2_ring_test PACK [device]\n"); return 2; }
98    const int device = argc > 2 ? std::atoi(argv[2]) : 0;
99    const bool log_off_run = std::getenv("STRATA_DF2_RING_LOG") != nullptr && std::getenv("STRATA_DF2_RING_LOG")[0] == '0';
100    cudaSetDevice(device);
101    std::string err;
102    const int64_t P = 4227, N = 4232;   // the resumed prefix and the prompt (the agent prompt's numbers)
103    float* dev = nullptr;
104    cudaMalloc((void**) &dev, (size_t) 256 * kTap * 4);
105    std::printf("dflash2_ring_test%s\n", log_off_run ? " (STRATA_DF2_RING_LOG=0)" : "");
106
107    auto fresh = [&](Dflash2Drafter& d) -> bool {
108        if (!d.load(argv[1], device, err)) { std::printf("load: %s\n", err.c_str()); return false; }
109        return true;
110    };
111    std::vector<float> ref;
112    std::vector<int64_t> reftags;
113    {   // ---- 1 rewind
114        std::printf("1 rewind (fed to %lld + 600, rewound to %lld, prompt %lld)\n", (long long) P, (long long) P, (long long) N);
115        Dflash2Drafter d;
116        if (!fresh(d)) return 2;
117        if (!feed(d, dev, 1, 0, P, err)) { std::printf("feed: %s\n", err.c_str()); return 2; }
118        if (!d.debug_ring(ref, reftags, err)) return 2;
119        if (!feed(d, dev, 1, P, P + 600, err)) return 2;
120        const std::vector<int32_t> ids = ids_of(1, N);
121        bool cold = false;
122        d.begin_request(ids.data(), N, P, &cold);
123        int wt = 0;
124        const int bad = count_bad(d, ref, N - 2047, P, &wt);
125        std::printf("    rebuilt %lld rows in %.1f ms, missing %lld, slots wrong %d (wrong tag %d), cold %d\n", (long long) d.last_rebuilt, d.last_ms, (long long) d.last_missing, bad, wt, (int) cold);
126        if (log_off_run) {
127            check(d.last_rebuilt == 0, "log off: nothing is rebuilt (the pre-log behaviour)");
128            check(bad > 0, "log off: the overwritten part of the window is blank (the defect this fixes)");
129        } else {
130            check(bad == 0, "every slot of [n - 2047, resume) holds its row, bitwise the original update's");
131            check(d.last_rebuilt == (P + 600 - Dflash2Drafter::kRing) - (N - 2047), "only the overwritten slots were rebuilt (the others were already right)");
132            check(!cold, "not cold");
133        }
134        // the ring must go on working: feed the prompt's remaining rows and a decode row, no stray tags
135        if (!feed(d, dev, 1, P, N, err)) return 2;
136    }
137    if (log_off_run) {
138        std::printf(g_fail == 0 ? "dflash2_ring_test: ok\n" : "dflash2_ring_test: FAILED\n");
139        return g_fail == 0 ? 0 : 1;
140    }
141    {   // ---- 2 parked
142        std::printf("2 parked (A to %lld, B to 3000, A resumed at %lld)\n", (long long) P, (long long) P);
143        Dflash2Drafter d;
144        if (!fresh(d)) return 2;
145        if (!feed(d, dev, 1, 0, P, err)) return 2;
146        {   // B takes over properly: a request for B from scratch
147            const std::vector<int32_t> idsB = ids_of(2, 3000);
148            bool cold = false;
149            d.begin_request(idsB.data(), 3000, 0, &cold);
150            if (!feed(d, dev, 2, 0, 3000, err)) return 2;
151        }
152        const std::vector<int32_t> ids = ids_of(1, N);
153        bool cold = true;
154        d.begin_request(ids.data(), N, P, &cold);
155        int wt = 0;
156        const int bad = count_bad(d, ref, N - 2047, P, &wt);
157        std::printf("    stash hits %lld, rebuilt %lld rows in %.1f ms (swap %d), slots wrong %d (wrong tag %d), stash %.1f MiB\n", (long long) d.ring_stats.stash_hits,
158                    (long long) d.last_rebuilt, d.last_ms, (int) d.last_swapped, bad, wt, (double) d.ring_stash_bytes() / 1048576.0);
159        check(d.ring_stats.stash_hits == 1 && d.last_swapped, "the parked lineage came back from the stash");
160        check(bad == 0, "every slot of [n - 2047, resume) holds its row, bitwise the original update's");
161        check(d.last_rebuilt == P - (N - 2047), "the whole window below resume was rebuilt (a new epoch)");
162        check(!cold, "not cold");
163    }
164    {   // ---- 3 branch
165        std::printf("3 branch (A to %lld, a request at %lld with other tokens, the old branch resumed at %lld)\n", (long long) (P + 700), (long long) (P - 300), (long long) P);
166        Dflash2Drafter d;
167        if (!fresh(d)) return 2;
168        if (!feed(d, dev, 1, 0, P + 700, err)) return 2;
169        // reference at P: the ring of an uninterrupted feed to P is `ref` (same lineage, same taps)
170        {   // a request that shares A up to P - 300 and then goes its own way (lineage 3 after that point)
171            std::vector<int32_t> ids = ids_of(1, P - 300);
172            for (int64_t p = P - 300; p < P - 300 + 400; ++p) ids.push_back(token_of(3, p));
173            bool cold = false;
174            d.begin_request(ids.data(), (int64_t) ids.size(), P - 300, &cold);
175            if (!feed(d, dev, 3, P - 300, P - 300 + 400, err)) return 2;   // (rows after the branch point: lineage 3's taps)
176        }
177        const std::vector<int32_t> ids = ids_of(1, N);
178        bool cold = true;
179        d.begin_request(ids.data(), N, P, &cold);
180        int wt = 0;
181        const int bad = count_bad(d, ref, N - 2047, P, &wt);
182        std::printf("    stash hits %lld, rebuilt %lld rows, missing %lld, slots wrong %d (wrong tag %d)\n", (long long) d.ring_stats.stash_hits, (long long) d.last_rebuilt,
183                    (long long) d.last_missing, bad, wt);
184        check(d.ring_stats.stash_hits >= 1, "the old branch was kept (copy on write) and came back");
185        check(bad == 0, "the old branch's rows are bitwise the original update's, not the new branch's");
186        check(!cold, "not cold");
187    }
188    {   // ---- 5 evicted
189        std::printf("5 evicted (stash budget 0)\n");
190        setenv("STRATA_DF2_LOG_MIB", "0", 1);
191        Dflash2Drafter d;
192        if (!fresh(d)) return 2;
193        unsetenv("STRATA_DF2_LOG_MIB");
194        if (!feed(d, dev, 1, 0, P, err)) return 2;
195        const std::vector<int32_t> idsB = ids_of(2, 3000);
196        bool cold = false;
197        d.begin_request(idsB.data(), 3000, 0, &cold);
198        if (!feed(d, dev, 2, 0, 3000, err)) return 2;
199        const std::vector<int32_t> ids = ids_of(1, N);
200        cold = false;
201        d.begin_request(ids.data(), N, P, &cold);
202        std::printf("    stash hits %lld, rebuilt %lld, missing %lld, cold %d\n", (long long) d.ring_stats.stash_hits, (long long) d.last_rebuilt, (long long) d.last_missing, (int) cold);
203        check(cold && d.last_missing > 0 && d.last_rebuilt == 0, "no log of the lineage: cold, the missing rows are reported");
204    }
205    std::printf(g_fail == 0 ? "dflash2_ring_test: ok\n" : "dflash2_ring_test: FAILED\n");
206    return g_fail == 0 ? 0 : 1;
207}