okami.mom/strata

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

Niko1221Merge two-card batch default (owner choice a): two NVIDIA cards pipeline by default2526f2ff9

main
12.7 KiB248 linesraw
1// src/program/batch_groups_test.cpp - how many pipelined slot groups --batch runs with (header-only, CPU).
2//
3//   1. `auto` picks the most groups, at most one per stage, that divide the slots;
4//   2. a layer split with --batch >= 2 and no --batch-groups means `auto` (0.1.41);
5//   3. --batch-mtp (or --adapt-async 1) turns that default into one group, and says which feature did it;
6//      with STRATA_PIPED_MTP and one slot per group, --batch-mtp keeps the pipeline and drafts in it;
7//   4. a number or `auto` given explicitly is honoured, with or without those features;
8//   5. no layer split: the number given (1) stays;
9//   6. STRATA_PIPE_ADAPT_EVERY: off unless a positive number; a round is due after N group windows, never with the tier
10//      off, beside --adapt-async, while a prompt is read or while the last round's copies are in flight.
11#include "strata/program/batch_groups.hpp"
12
13#include <cstdio>
14
15namespace bg = strata::program::batch_groups;
16
17namespace {
18int g_fail = 0;
19void check(bool ok, const char* what) {
20    std::printf("  %-78s %s\n", what, ok ? "ok" : "FAIL");
21    if (!ok) ++g_fail;
22}
23bg::Input in(int batch, int stages) {
24    bg::Input i;
25    i.batch = batch;
26    i.later_stages = stages;
27    return i;
28}
29}  // namespace
30
31int main() {
32    std::printf("batch_groups_test\n");
33    check(bg::auto_groups(0, 4) == 1, "auto: no layer split, one group");
34    check(bg::auto_groups(1, 2) == 2 && bg::auto_groups(1, 4) == 2 && bg::auto_groups(1, 3) == 1,
35          "auto: two GPUs, 2 groups when the slots divide by 2");
36    check(bg::auto_groups(3, 8) == 4 && bg::auto_groups(3, 6) == 3 && bg::auto_groups(3, 2) == 2,
37          "auto: at most one group per GPU, and a divisor of the slots");
38
39    {   // the 0.1.41 default
40        const auto r = bg::resolve(in(2, 1));
41        check(r.groups == 2 && r.from_auto && r.serial == bg::Serial::None, "layer split, --batch 2, nothing given: 2 groups");
42    }
43    {
44        const auto r = bg::resolve(in(1, 1));
45        check(r.groups == 1 && !r.from_auto, "--batch 1: no default");
46    }
47    {
48        const auto r = bg::resolve(in(2, 0));
49        check(r.groups == 1 && !r.from_auto, "no layer split: one group");
50    }
51
52    {   // the production shape: parallel 2 on a 2-GPU split with --batch-mtp
53        bg::Input i = in(2, 1);
54        i.batch_mtp = true;
55        auto r = bg::resolve(i);
56        check(r.groups == 2 && r.from_auto && r.serial == bg::Serial::None && r.mtp_dropped,
57              "--batch-mtp, nothing given: the pipeline wins, --batch-mtp is dropped");
58        i.mtp_serial = true;
59        r = bg::resolve(i);
60        check(r.groups == 1 && r.from_auto && r.serial == bg::Serial::BatchMtp && r.would_be == 2 && !r.mtp_dropped,
61              "STRATA_BATCH_MTP_SERIAL: one group, because of --batch-mtp");
62        check(bg::serial_what(r.serial)[0] == '-', "the reason names the feature");
63    }
64    {   // STRATA_PIPED_MTP: the pipelined groups draft too, with any number of slots per group
65        bg::Input i = in(4, 3);
66        i.batch_mtp = true;
67        i.mtp_piped = true;
68        auto r = bg::resolve(i);
69        check(r.groups == 4 && r.mtp_piped && !r.mtp_dropped && r.serial == bg::Serial::None,
70              "STRATA_PIPED_MTP, 4 slots on 4 GPUs: 4 groups of 1 slot, and the drafts stay");
71        i.batch = 8;   // two slots per group: the group's window carries both slots' rows, each with its own drafts
72        r = bg::resolve(i);
73        check(r.groups == 4 && r.mtp_piped && !r.mtp_dropped && r.serial == bg::Serial::None,
74              "STRATA_PIPED_MTP, 8 slots on 4 GPUs: 4 groups of 2 slots, and the drafts stay");
75        i.batch = 4;
76        i.mtp_serial = true;   // asked for the serial windows: those win
77        r = bg::resolve(i);
78        check(r.groups == 1 && !r.mtp_piped && r.serial == bg::Serial::BatchMtp,
79              "STRATA_PIPED_MTP with STRATA_BATCH_MTP_SERIAL: one serial group");
80        i.mtp_serial = false;
81        i.batch_mtp = false;
82        r = bg::resolve(i);
83        check(r.groups == 4 && !r.mtp_piped && !r.mtp_dropped, "STRATA_PIPED_MTP without --batch-mtp: nothing to draft");
84        i.batch_mtp = true;
85        i.set = true;
86        i.given = 1;   // one group: the serial windows, where --batch-mtp's own drafts run
87        r = bg::resolve(i);
88        check(r.groups == 1 && !r.mtp_piped && !r.mtp_dropped, "STRATA_PIPED_MTP, --batch-groups 1: the serial path, --batch-mtp runs there");
89    }
90    {   // 0.1.49.2 port: --batch-groups given EXPLICITLY (a number or `auto`) must not silently
91        // drop the drafts: the operator asked for those groups, and one slot per group still drafts.
92        bg::Input i = in(4, 3);
93        i.batch_mtp = true;
94        i.mtp_piped = true;
95        i.set = true;
96        i.auto_given = true;   // --batch-groups auto
97        i.given = 4;
98        auto r = bg::resolve(i);
99        check(r.groups == 4 && r.mtp_piped && !r.mtp_dropped && r.serial == bg::Serial::None,
100              "STRATA_PIPED_MTP, --batch-groups auto given: 4 groups of 1 slot, and the drafts stay");
101        i.auto_given = false;  // --batch-groups 4
102        r = bg::resolve(i);
103        check(r.groups == 4 && r.mtp_piped && !r.mtp_dropped && r.serial == bg::Serial::None,
104              "STRATA_PIPED_MTP, --batch-groups 4 given: 4 groups of 1 slot, and the drafts stay");
105        i.given = 2;           // two slots per group: the group's window carries both slots' rows
106        r = bg::resolve(i);
107        check(r.groups == 2 && r.mtp_piped && !r.mtp_dropped,
108              "STRATA_PIPED_MTP, --batch-groups 2 given: 2 groups of 2 slots, and the drafts stay");
109    }
110    {   // 0.2.7: two NVIDIA cards pipeline by default; the serial default is opt-in (STRATA_BATCH_GROUPS_NV_SERIAL=1, STRATA_DEFAULTS=026)
111        check(!bg::nv_serial_wanted(nullptr, false), "NV_SERIAL unset: the pipeline");
112        check(!bg::nv_serial_wanted("", false), "NV_SERIAL empty: the pipeline");
113        check(!bg::nv_serial_wanted("0", false), "NV_SERIAL=0: the pipeline");
114        check(bg::nv_serial_wanted("1", false), "NV_SERIAL=1: serial");
115        check(bg::nv_serial_wanted("yes", false), "NV_SERIAL=yes: serial");
116        check(bg::nv_serial_wanted(nullptr, true), "STRATA_DEFAULTS=026: serial (v0.2.6's behaviour)");
117        check(!bg::nv_serial_wanted("0", true), "an explicit NV_SERIAL=0 wins over STRATA_DEFAULTS=026");
118        check(bg::nv_serial_wanted("", true), "NV_SERIAL empty and STRATA_DEFAULTS=026: serial");
119        bg::Input i = in(4, 1);
120        i.nv_serial_faster = bg::nv_serial_wanted(nullptr, false);
121        auto r = bg::resolve(i);
122        check(r.groups == 2 && r.serial == bg::Serial::None, "2 cards, 4 slots, nothing given: 2 pipelined groups, whatever the residency");
123    }
124    {   // STRATA_PIPED_MTP unset: nothing changes, whatever --batch-mtp and the groups say
125        bg::Input i = in(4, 1);
126        i.batch_mtp = true;
127        auto r = bg::resolve(i);
128        check(r.groups == 2 && !r.mtp_piped && r.mtp_dropped && r.serial == bg::Serial::None,
129              "unset, --batch-mtp, nothing given: the pipeline wins, the drafts are dropped (as before)");
130        i.set = true;
131        i.given = 2;
132        r = bg::resolve(i);
133        check(r.groups == 2 && !r.mtp_piped && !r.mtp_dropped, "unset, --batch-mtp, --batch-groups 2: not 'dropped' here (as before)");
134        i.given = 1;
135        r = bg::resolve(i);
136        check(r.groups == 1 && !r.mtp_piped && !r.mtp_dropped, "unset, --batch-mtp, --batch-groups 1: serial windows keep the drafts");
137        i.set = false;
138        i.mtp_serial = true;
139        r = bg::resolve(i);
140        check(r.groups == 1 && !r.mtp_piped && !r.mtp_dropped && r.serial == bg::Serial::BatchMtp,
141              "unset, STRATA_BATCH_MTP_SERIAL: one serial group (as before)");
142    }
143    {
144        bg::Input i = in(2, 1);
145        i.adapt_async = true;
146        const auto r = bg::resolve(i);
147        check(r.groups == 1 && r.serial == bg::Serial::AdaptAsync, "--adapt-async 1, nothing given: one group");
148    }
149    {
150        bg::Input i = in(2, 1);
151        i.batch_mtp = true;
152        i.adapt_async = true;
153        check(bg::resolve(i).serial == bg::Serial::AdaptAsync, "both: --adapt-async 1 still gives one group");
154        i.mtp_serial = true;
155        check(bg::resolve(i).serial == bg::Serial::BatchMtp, "both with STRATA_BATCH_MTP_SERIAL: --batch-mtp is named");
156    }
157    {   // nothing to give way: the slots do not divide, so auto is one group anyway and there is nothing to say
158        bg::Input i = in(3, 1);
159        i.batch_mtp = true;
160        const auto r = bg::resolve(i);
161        check(r.groups == 1 && r.serial == bg::Serial::None && r.would_be == 1, "auto would be 1 group anyway: no reason");
162    }
163    {   // PROPOSED NVIDIA default: two cards, nothing given: serial windows (measured +12..16% on 2x A4000)
164        bg::Input i = in(4, 1);
165        i.nv_serial_faster = true;
166        auto r = bg::resolve(i);
167        check(r.groups == 1 && r.from_auto && r.serial == bg::Serial::TwoCardsSerialFaster && r.would_be == 2,
168              "nv_serial_faster, 2 cards, 4 slots, nothing given: one group, named");
169        check(bg::serial_what(r.serial)[0] == 's', "the reason names the measurement");
170        check(bg::resolve(in(4, 1)).groups == 2, "flag off: the pipeline default stays");
171        i = in(8, 1); i.nv_serial_faster = true;   // measured too: +15%
172        check(bg::resolve(i).groups == 1, "8 slots (measured +15%): serial too");
173        i = in(4, 3); i.nv_serial_faster = true;
174        check(bg::resolve(i).groups == 4, "4 cards (not measured): the pipeline stays");
175        i = in(4, 1); i.nv_serial_faster = true; i.batch_mtp = true;
176        check(bg::resolve(i).groups == 2 && bg::resolve(i).mtp_dropped, "with --batch-mtp the 0.1.43.1 rule is unchanged");
177        i = in(4, 1); i.nv_serial_faster = true; i.set = true; i.auto_given = true;
178        check(bg::resolve(i).groups == 2, "--batch-groups auto given: the pipeline");
179        i.auto_given = false; i.given = 2;
180        check(bg::resolve(i).groups == 2, "--batch-groups 2 given: 2");
181    }
182    {   // explicit values are honoured
183        bg::Input i = in(4, 1);
184        i.set = true;
185        i.given = 2;
186        i.batch_mtp = true;
187        i.adapt_async = true;
188        const auto r = bg::resolve(i);
189        check(r.groups == 2 && !r.from_auto && r.serial == bg::Serial::None, "--batch-groups 2 with --batch-mtp: 2");
190        i.given = 1;
191        check(bg::resolve(i).groups == 1, "--batch-groups 1: 1 (the opt-out)");
192        i.given = 4;   // not checked here: generate.cpp refuses what does not divide
193        check(bg::resolve(i).groups == 4, "--batch-groups 4 is returned as given");
194    }
195    {
196        bg::Input i = in(2, 1);
197        i.set = true;
198        i.auto_given = true;
199        i.batch_mtp = true;
200        const auto r = bg::resolve(i);
201        check(r.groups == 2 && r.from_auto && r.serial == bg::Serial::None,
202              "--batch-groups auto with --batch-mtp: auto asked for the pipeline, 2 groups");
203    }
204    {
205        bg::Input i = in(2, 0);
206        i.set = true;
207        i.given = 2;
208        check(bg::resolve(i).groups == 2, "an explicit number without a split is returned (the engine warns)");
209    }
210
211    // STRATA_PIPE_ADAPT_EVERY
212    check(bg::pipe_adapt_every(nullptr) == 0 && bg::pipe_adapt_every("") == 0 && bg::pipe_adapt_every("0") == 0,
213          "pipe adapt: unset, empty or 0 is off");
214    check(bg::pipe_adapt_every("-5") == 0 && bg::pipe_adapt_every("off") == 0, "pipe adapt: negative or not a number is off");
215    check(bg::pipe_adapt_every("128") == 128 && bg::pipe_adapt_every("1") == 1, "pipe adapt: a positive number is the interval");
216    {
217        bg::PipeAdapt p;
218        p.every = 128;
219        p.tier_on = true;
220        p.windows = 127;
221        check(!bg::pipe_adapt_due(p), "pipe adapt: not before N group windows");
222        p.windows = 128;
223        check(bg::pipe_adapt_due(p), "pipe adapt: due after N group windows");
224        p.last = 128;
225        p.windows = 200;
226        check(!bg::pipe_adapt_due(p), "pipe adapt: counted again from the last round");
227        p.windows = 256;
228        check(bg::pipe_adapt_due(p), "pipe adapt: due N windows after the last round");
229        bg::PipeAdapt q = p;
230        q.every = 0;
231        check(!bg::pipe_adapt_due(q), "pipe adapt: never when off");
232        q = p;
233        q.tier_on = false;
234        check(!bg::pipe_adapt_due(q), "pipe adapt: never with the adaptive tier off");
235        q = p;
236        q.async_tier = true;
237        check(!bg::pipe_adapt_due(q), "pipe adapt: never beside --adapt-async 1");
238        q = p;
239        q.prompt_read = true;
240        check(!bg::pipe_adapt_due(q), "pipe adapt: never while a prompt is read beside the slots");
241        q = p;
242        q.swaps_pending = true;
243        check(!bg::pipe_adapt_due(q), "pipe adapt: never while the last round's copies are in flight");
244    }
245
246    std::printf("%s\n", g_fail == 0 ? "batch_groups_test: all passed" : "batch_groups_test: FAILED");
247    return g_fail == 0 ? 0 : 1;
248}