okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2526f2ff9
main
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" ); 21if (!ok )++ g_fail ; 22} 23bg::Input in (int batch ,int stages ) { 24 bg::Input i ; 25i .batch = batch ; 26i .later_stages = stages ; 27return i ; 28} 29}// namespace 30 31int main () { 32 std::printf ("batch_groups_test\n" ); 33check (bg::auto_groups (0 ,4 )== 1 ,"auto: no layer split, one group" ); 34check (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" ); 36check (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 40const auto r = bg::resolve (in (2 ,1 )); 41check (r .groups == 2 && r .from_auto && r .serial == bg::Serial ::None ,"layer split, --batch 2, nothing given: 2 groups" ); 42 } 43 { 44const auto r = bg::resolve (in (1 ,1 )); 45check (r .groups == 1 && !r .from_auto ,"--batch 1: no default" ); 46 } 47 { 48const auto r = bg::resolve (in (2 ,0 )); 49check (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 ); 54i .batch_mtp = true; 55auto r = bg::resolve (i ); 56check (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" ); 58i .mtp_serial = true; 59r = bg::resolve (i ); 60check (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" ); 62check (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 ); 66i .batch_mtp = true; 67i .mtp_piped = true; 68auto r = bg::resolve (i ); 69check (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" ); 71i .batch = 8 ;// two slots per group: the group's window carries both slots' rows, each with its own drafts 72r = bg::resolve (i ); 73check (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" ); 75i .batch = 4 ; 76i .mtp_serial = true;// asked for the serial windows: those win 77r = bg::resolve (i ); 78check (r .groups == 1 && !r .mtp_piped && r .serial == bg::Serial ::BatchMtp , 79"STRATA_PIPED_MTP with STRATA_BATCH_MTP_SERIAL: one serial group" ); 80i .mtp_serial = false; 81i .batch_mtp = false; 82r = bg::resolve (i ); 83check (r .groups == 4 && !r .mtp_piped && !r .mtp_dropped ,"STRATA_PIPED_MTP without --batch-mtp: nothing to draft" ); 84i .batch_mtp = true; 85i .set = true; 86i .given = 1 ;// one group: the serial windows, where --batch-mtp's own drafts run 87r = bg::resolve (i ); 88check (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 ); 93i .batch_mtp = true; 94i .mtp_piped = true; 95i .set = true; 96i .auto_given = true;// --batch-groups auto 97i .given = 4 ; 98auto r = bg::resolve (i ); 99check (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" ); 101i .auto_given = false;// --batch-groups 4 102r = bg::resolve (i ); 103check (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" ); 105i .given = 2 ;// two slots per group: the group's window carries both slots' rows 106r = bg::resolve (i ); 107check (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) 111check (!bg::nv_serial_wanted (nullptr , false),"NV_SERIAL unset: the pipeline" ); 112check (!bg::nv_serial_wanted ("" , false),"NV_SERIAL empty: the pipeline" ); 113check (!bg::nv_serial_wanted ("0" , false),"NV_SERIAL=0: the pipeline" ); 114check (bg::nv_serial_wanted ("1" , false),"NV_SERIAL=1: serial" ); 115check (bg::nv_serial_wanted ("yes" , false),"NV_SERIAL=yes: serial" ); 116check (bg::nv_serial_wanted (nullptr , true),"STRATA_DEFAULTS=026: serial (v0.2.6's behaviour)" ); 117check (!bg::nv_serial_wanted ("0" , true),"an explicit NV_SERIAL=0 wins over STRATA_DEFAULTS=026" ); 118check (bg::nv_serial_wanted ("" , true),"NV_SERIAL empty and STRATA_DEFAULTS=026: serial" ); 119 bg::Input i = in (4 ,1 ); 120i .nv_serial_faster = bg::nv_serial_wanted (nullptr , false); 121auto r = bg::resolve (i ); 122check (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 ); 126i .batch_mtp = true; 127auto r = bg::resolve (i ); 128check (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)" ); 130i .set = true; 131i .given = 2 ; 132r = bg::resolve (i ); 133check (r .groups == 2 && !r .mtp_piped && !r .mtp_dropped ,"unset, --batch-mtp, --batch-groups 2: not 'dropped' here (as before)" ); 134i .given = 1 ; 135r = bg::resolve (i ); 136check (r .groups == 1 && !r .mtp_piped && !r .mtp_dropped ,"unset, --batch-mtp, --batch-groups 1: serial windows keep the drafts" ); 137i .set = false; 138i .mtp_serial = true; 139r = bg::resolve (i ); 140check (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 ); 145i .adapt_async = true; 146const auto r = bg::resolve (i ); 147check (r .groups == 1 && r .serial == bg::Serial ::AdaptAsync ,"--adapt-async 1, nothing given: one group" ); 148 } 149 { 150 bg::Input i = in (2 ,1 ); 151i .batch_mtp = true; 152i .adapt_async = true; 153check (bg::resolve (i ).serial == bg::Serial ::AdaptAsync ,"both: --adapt-async 1 still gives one group" ); 154i .mtp_serial = true; 155check (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 ); 159i .batch_mtp = true; 160const auto r = bg::resolve (i ); 161check (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 ); 165i .nv_serial_faster = true; 166auto r = bg::resolve (i ); 167check (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" ); 169check (bg::serial_what (r .serial )[0 ]== 's' ,"the reason names the measurement" ); 170check (bg::resolve (in (4 ,1 )).groups == 2 ,"flag off: the pipeline default stays" ); 171i = in (8 ,1 );i .nv_serial_faster = true;// measured too: +15% 172check (bg::resolve (i ).groups == 1 ,"8 slots (measured +15%): serial too" ); 173i = in (4 ,3 );i .nv_serial_faster = true; 174check (bg::resolve (i ).groups == 4 ,"4 cards (not measured): the pipeline stays" ); 175i = in (4 ,1 );i .nv_serial_faster = true;i .batch_mtp = true; 176check (bg::resolve (i ).groups == 2 && bg::resolve (i ).mtp_dropped ,"with --batch-mtp the 0.1.43.1 rule is unchanged" ); 177i = in (4 ,1 );i .nv_serial_faster = true;i .set = true;i .auto_given = true; 178check (bg::resolve (i ).groups == 2 ,"--batch-groups auto given: the pipeline" ); 179i .auto_given = false;i .given = 2 ; 180check (bg::resolve (i ).groups == 2 ,"--batch-groups 2 given: 2" ); 181 } 182 {// explicit values are honoured 183 bg::Input i = in (4 ,1 ); 184i .set = true; 185i .given = 2 ; 186i .batch_mtp = true; 187i .adapt_async = true; 188const auto r = bg::resolve (i ); 189check (r .groups == 2 && !r .from_auto && r .serial == bg::Serial ::None ,"--batch-groups 2 with --batch-mtp: 2" ); 190i .given = 1 ; 191check (bg::resolve (i ).groups == 1 ,"--batch-groups 1: 1 (the opt-out)" ); 192i .given = 4 ;// not checked here: generate.cpp refuses what does not divide 193check (bg::resolve (i ).groups == 4 ,"--batch-groups 4 is returned as given" ); 194 } 195 { 196 bg::Input i = in (2 ,1 ); 197i .set = true; 198i .auto_given = true; 199i .batch_mtp = true; 200const auto r = bg::resolve (i ); 201check (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 ); 206i .set = true; 207i .given = 2 ; 208check (bg::resolve (i ).groups == 2 ,"an explicit number without a split is returned (the engine warns)" ); 209 } 210 211// STRATA_PIPE_ADAPT_EVERY 212check (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" ); 214check (bg::pipe_adapt_every ("-5" )== 0 && bg::pipe_adapt_every ("off" )== 0 ,"pipe adapt: negative or not a number is off" ); 215check (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 ; 218p .every = 128 ; 219p .tier_on = true; 220p .windows = 127 ; 221check (!bg::pipe_adapt_due (p ),"pipe adapt: not before N group windows" ); 222p .windows = 128 ; 223check (bg::pipe_adapt_due (p ),"pipe adapt: due after N group windows" ); 224p .last = 128 ; 225p .windows = 200 ; 226check (!bg::pipe_adapt_due (p ),"pipe adapt: counted again from the last round" ); 227p .windows = 256 ; 228check (bg::pipe_adapt_due (p ),"pipe adapt: due N windows after the last round" ); 229 bg::PipeAdapt q = p ; 230q .every = 0 ; 231check (!bg::pipe_adapt_due (q ),"pipe adapt: never when off" ); 232q = p ; 233q .tier_on = false; 234check (!bg::pipe_adapt_due (q ),"pipe adapt: never with the adaptive tier off" ); 235q = p ; 236q .async_tier = true; 237check (!bg::pipe_adapt_due (q ),"pipe adapt: never beside --adapt-async 1" ); 238q = p ; 239q .prompt_read = true; 240check (!bg::pipe_adapt_due (q ),"pipe adapt: never while a prompt is read beside the slots" ); 241q = p ; 242q .swaps_pending = true; 243check (!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" ); 247return g_fail == 0 ?0 :1 ; 248}