okami.mom/strata

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

Niko1221Merge f-026-qwen into f-0243-rel94a4a34b0

main
7.0 KiB135 linesraw
1// adapt_async_default_test: the asynchronous adaptive tier as a measured choice (include/strata/core/adapt_async_default.hpp).
2#include "strata/core/adapt_async_default.hpp"
3
4#include <cstdio>
5#include <cstring>
6
7static int g_fail = 0;
8#define CHECK(c)                                                              \
9    do {                                                                      \
10        if (!(c)) { std::fprintf(stderr, "FAIL %s:%d  %s\n", __FILE__, __LINE__, #c); ++g_fail; } \
11    } while (0)
12
13using strata::core::AdaptAsyncAuto;
14using strata::core::AdaptAsyncSelf;
15using Action = AdaptAsyncSelf::Action;
16using Phase = AdaptAsyncSelf::Phase;
17
18static AdaptAsyncAuto good() {
19    AdaptAsyncAuto a;
20    a.cuda = a.serve = a.adaptive = a.arena = true;
21    return a;
22}
23
24static AdaptAsyncSelf::Config cfg0() {   // no warm-up: the cases below count their windows from the first
25    AdaptAsyncSelf::Config c;
26    c.warmup_windows = 0;
27    return c;
28}
29
30// feed `n` windows of `ms` / `wait` / `tok`; returns the last non-None action
31static Action feed(AdaptAsyncSelf& s, int n, double ms, double wait, int tok) {
32    Action last = Action::None;
33    for (int i = 0; i < n; ++i) {
34        const Action a = s.window(ms, wait, tok);
35        if (a != Action::None) last = a;
36    }
37    return last;
38}
39
40int main() {
41    const char* why = nullptr;
42    // ---- where the engine may choose by measurement
43    CHECK(strata::core::adapt_async_self_decides(good(), &why));
44    { AdaptAsyncAuto a = good(); a.flag = 0; CHECK(!strata::core::adapt_async_self_decides(a, &why)); CHECK(why && std::strstr(why, "given")); }
45    { AdaptAsyncAuto a = good(); a.flag = 1; CHECK(!strata::core::adapt_async_self_decides(a)); }
46    { AdaptAsyncAuto a = good(); a.env = 0; CHECK(!strata::core::adapt_async_self_decides(a, &why)); CHECK(why && std::strstr(why, "STRATA_ADAPT_ASYNC")); }
47    { AdaptAsyncAuto a = good(); a.env = 1; CHECK(strata::core::adapt_async_self_decides(a)); }
48    { AdaptAsyncAuto a = good(); a.env = -1; CHECK(strata::core::adapt_async_self_decides(a)); }
49    { AdaptAsyncAuto a = good(); a.cuda = false; CHECK(!strata::core::adapt_async_self_decides(a)); }
50    { AdaptAsyncAuto a = good(); a.serve = false; CHECK(!strata::core::adapt_async_self_decides(a)); }
51    { AdaptAsyncAuto a = good(); a.adaptive = false; CHECK(!strata::core::adapt_async_self_decides(a)); }   // --adapt-every 100000 gates
52    { AdaptAsyncAuto a = good(); a.arena = false; CHECK(!strata::core::adapt_async_self_decides(a)); }
53    { AdaptAsyncAuto a = good(); a.multi_gpu = true; CHECK(!strata::core::adapt_async_self_decides(a)); }
54    { AdaptAsyncAuto a = good(); a.pipeline_windows = true; CHECK(!strata::core::adapt_async_self_decides(a)); }
55    { AdaptAsyncAuto a = good(); a.batch = true; CHECK(!strata::core::adapt_async_self_decides(a)); }
56    { AdaptAsyncAuto a = good(); a.peer = true; CHECK(!strata::core::adapt_async_self_decides(a)); }
57    { AdaptAsyncAuto a = good(); a.remote_opt = true; CHECK(!strata::core::adapt_async_self_decides(a)); }
58    { AdaptAsyncAuto a = good(); a.adapt_overlap = true; CHECK(!strata::core::adapt_async_self_decides(a)); }
59    CHECK(!strata::core::adapt_async_self_decides(AdaptAsyncAuto{}));
60
61    // ---- the state machine
62    {   // not armed: nothing happens
63        AdaptAsyncSelf s;
64        CHECK(s.phase() == Phase::Off);
65        CHECK(feed(s, 500, 20.0, 5.0, 3) == Action::None);
66    }
67    {   // the wait is small (3% of a window): measure 64 windows, stay blocking, never switch
68        AdaptAsyncSelf s(cfg0()); s.arm();
69        CHECK(feed(s, 63, 20.0, 0.6, 3) == Action::None);
70        CHECK(s.phase() == Phase::Measure);
71        CHECK(feed(s, 1, 20.0, 0.6, 3) == Action::None);
72        CHECK(s.phase() == Phase::Done && !s.kept());
73        CHECK(feed(s, 500, 20.0, 0.6, 3) == Action::None);
74    }
75    {   // the wait is 7%: switch to async after the 64th window; the async windows are 10% faster per token: kept
76        AdaptAsyncSelf s(cfg0()); s.arm();
77        CHECK(feed(s, 63, 20.0, 1.4, 3) == Action::None);
78        CHECK(s.window(20.0, 1.4, 3) == Action::ToAsync);
79        CHECK(s.phase() == Phase::Trial && s.trialing());
80        CHECK(s.wait_fraction() > 0.069 && s.wait_fraction() < 0.071);
81        CHECK(feed(s, 4 + 63, 18.0, 0.1, 3) == Action::None);       // 4 warm-up windows are not counted
82        CHECK(s.window(18.0, 0.1, 3) == Action::None);              // kept: no action
83        CHECK(s.phase() == Phase::Done && s.kept());
84        CHECK(s.trial_ms_per_token() < s.measure_ms_per_token());
85    }
86    {   // the wait is 7% and async is 3% SLOWER per token (> 2%): back to blocking, and it stays there
87        AdaptAsyncSelf s(cfg0()); s.arm();
88        CHECK(feed(s, 64, 20.0, 1.4, 3) == Action::ToAsync);
89        CHECK(feed(s, 4 + 63, 20.6, 0.1, 3) == Action::None);
90        CHECK(s.window(20.6, 0.1, 3) == Action::ToBlocking);
91        CHECK(s.phase() == Phase::Done && !s.kept());
92        CHECK(feed(s, 1000, 19.6, 0.1, 3) == Action::None);
93    }
94    {   // async slower (the CPU-bound box): back
95        AdaptAsyncSelf s(cfg0()); s.arm();
96        CHECK(feed(s, 64, 55.0, 4.0, 3) == Action::ToAsync);
97        CHECK(feed(s, 4 + 63, 58.0, 0.2, 3) == Action::None);
98        CHECK(s.window(58.0, 0.2, 3) == Action::ToBlocking);
99    }
100    {   // async 1% slower or equal: kept (the trial guards against harm, it does not demand a gain)
101        AdaptAsyncSelf s(cfg0()); s.arm();
102        CHECK(feed(s, 64, 20.0, 1.4, 3) == Action::ToAsync);
103        CHECK(feed(s, 4 + 64, 20.2, 0.1, 3) == Action::None);
104        CHECK(s.kept());
105    }
106    {   // 5% wait switches; a 3% gain is kept
107        AdaptAsyncSelf s(cfg0()); s.arm();
108        CHECK(feed(s, 64, 100.0, 5.0, 4) == Action::ToAsync);
109        const double t = 100.0 / 1.0301;
110        CHECK(feed(s, 4 + 64, t, 0.0, 4) == Action::None);
111        CHECK(s.kept());
112    }
113    {   // windows with no token or no time are ignored (they neither count nor move the phase)
114        AdaptAsyncSelf s(cfg0()); s.arm();
115        CHECK(feed(s, 200, 20.0, 5.0, 0) == Action::None);
116        CHECK(feed(s, 200, 0.0, 0.0, 3) == Action::None);
117        CHECK(s.phase() == Phase::Measure);
118    }
119    {   // tokens per window matter: more tokens per window at the same time is faster per token
120        AdaptAsyncSelf s(cfg0()); s.arm();
121        CHECK(feed(s, 64, 20.0, 2.0, 2) == Action::ToAsync);       // 10 ms per token
122        CHECK(feed(s, 4 + 64, 20.0, 0.1, 3) == Action::None);      // 6.7 ms per token: kept
123        CHECK(s.kept());
124    }
125    {   // the default has a warm-up: the first 64 steady windows are not measured (a fresh engine's cache is still settling)
126        AdaptAsyncSelf s; s.arm();
127        CHECK(feed(s, 64, 20.0, 5.0, 3) == Action::None);          // warm-up
128        CHECK(s.phase() == Phase::Measure);
129        CHECK(feed(s, 63, 20.0, 1.4, 3) == Action::None);
130        CHECK(s.window(20.0, 1.4, 3) == Action::ToAsync);          // 128th window: the measurement is the last 64 only
131        CHECK(s.wait_fraction() > 0.069 && s.wait_fraction() < 0.071);
132    }
133    if (g_fail == 0) std::printf("adapt_async_default_test: OK\n");
134    return g_fail == 0 ? 0 : 1;
135}