diff --git a/docs/GUIDE.md b/docs/GUIDE.md index a658d6a7..ac38eeb1 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1166,6 +1166,12 @@ checked against the fold (warm, the slowest rank): With the two Gen4 links retrained to Gen5 x8 the weights read 4,1,1,4,1,1,4,4 and the 16 MiB message takes 1360 µs. +That is 56% of the time the x8 cards' links need for its bytes (763 µs). `--perfar8 N iters bf16 +lockstep,ring,split` times the forms below side by side, and `--timeline` adds every rank's first +push, each step's work and wait and the final assemble, which in one piece are the reduce-scatter +and the all-gather apart; `R4D_SELFTEST_ONE_CARD=1` runs the eight ranks on one card, for their +bits only. + The links are read once, at startup: a link that drops to a lower speed while the server runs keeps its weight until the next start. `RADIANCE_AR8_SHARD_WEIGHTS` overrides them: `off` keeps the shards even, eight comma-separated integers set them, unset or `auto` reads the links. @@ -1173,8 +1179,13 @@ the shards even, eight comma-separated integers set them, unset or `auto` reads same time's bytes when both directions run at once (`r4d_ar_weights_duplex`): x16 64 and x8 8 on the repaired board, whose link time it puts 10% under the `auto` weights', a Gen4 x8 card nothing. `RADIANCE_AR8_PIECES` (1 to 16) fixes the piece count, and `RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS` -and `RADIANCE_AR8_PIECES_MIN_ELEMS` move the two sizes they start at. Every rank must read the -same values; where they differ, every rank stops at startup. +and `RADIANCE_AR8_PIECES_MIN_ELEMS` move the two sizes they start at. `RADIANCE_AR8_FORM` chooses +how the steps run, with the same bits: `lockstep` (unset) as above; `ring` sends the first hop to +the other cards in ring order from the next rank, so no card receives from all seven at once; +`split` does that and replaces each step's handshake, which waits for every card to finish the +whole step, by a flag after each half of it, awaited only where its data is read, so a card whose +half is done goes on with the next piece while a slower one still reduces. Every rank must read +the same values; where they differ, every rank stops at startup. ### 4.8 Host tier for finished conversations diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 5cfb2b09..32463fa6 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -451,8 +451,10 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee long my_flags, long seq_ctrs, long slot_stride16, long inp, long out, long n_elem, long dtype, long rank, long stream, long nblocks, long nthreads, long drain, long acq, long pub, - const int* shard_weights, long pieces, long timeline); -// `timeline`, a diagnostic, is 0 or a device buffer of nblocks * R4D_AR_TL_WORDS int64 that every + const int* shard_weights, long pieces, long timeline, + long form); +// `form` is R4D_AR8_FORM_* (r4d_ar_shards.h), the same on every rank: lockstep, ring or split, +// all with the same bits. `timeline`, a diagnostic, is 0 or a device buffer of nblocks * R4D_AR_TL_WORDS int64 that every // block of the eight-rank two-shot fills with wall-clock stamps (r4d_ar_wide.h): its start, each // handshake's arrival and release, its end. r4d_selftest --perfar8 with --timeline reads it. // The tiered int8 wire on both two-shot hops (r4d_ar_twoshot_Nrank_ti8.hip; codec in diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 400f9147..572f0ac1 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -79,6 +79,7 @@ struct Rendezvous { int w[R4D_MAX_RANKS] = { 0 }; int pieces = 0; long long weights_min = 0, pieces_min = 0; + int form = 0; R4DArLink link; } ar8[R4D_MAX_RANKS]; int failed = 0; @@ -134,6 +135,8 @@ struct ArInst { /* Messages under these sizes (elements) run even and in one piece, whatever is configured. */ long long weights_min = R4D_AR8_MIN_ELEMS_DEFAULT; long long pieces_min = R4D_AR8_MIN_ELEMS_DEFAULT; + /* Its form (RADIANCE_AR8_FORM); a launch's `form` parameter overrides it. */ + int form = R4D_AR8_FORM_LOCKSTEP; }; /* A parameter that may be declared as a range: init gets one band's frozen geometry, and buffers @@ -217,6 +220,21 @@ long long ar8_min_elems(const char* name, std::once_flag* said) { return v; } +/* RADIANCE_AR8_FORM: lockstep (or unset), ring or split. Anything else is lockstep, with one + * warning. */ +int ar8_form() { + const char* e = std::getenv(R4D_AR8_FORM_ENV); + if (!e || !*e) return R4D_AR8_FORM_LOCKSTEP; + const int f = r4d_ar8_form_parse(e); + if (f >= 0) return f; + static std::once_flag said; + std::call_once(said, [e] { + std::fprintf(stderr, "[r4d] %s=\"%s\" ignored (needs lockstep, ring or split); the " + "eight-rank two-shot runs in lockstep\n", R4D_AR8_FORM_ENV, e); + }); + return R4D_AR8_FORM_LOCKSTEP; +} + /* The eight-rank two-shot's choice for one message, a function of its size and the instance * alone: a recorded launch keeps the table and piece count it was recorded with, and replays of * the same size agree with it. */ @@ -240,6 +258,7 @@ int ar8_settle(ArInst* in, int rank, int world) { me.pieces = ar8_pieces(); me.weights_min = ar8_min_elems(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, &said_w); me.pieces_min = ar8_min_elems(R4D_AR8_PIECES_MIN_ENV, &said_p); + me.form = ar8_form(); if ((me.mode == AR8_AUTO || me.mode == AR8_DUPLEX) && !ar8_own_link(&me.link)) me.link = R4DArLink(); { @@ -253,15 +272,24 @@ int ar8_settle(ArInst* in, int rank, int world) { for (int r = 0; r < world; ++r) if (all[r].mode != me.mode || std::memcmp(all[r].w, me.w, sizeof(me.w)) || all[r].pieces != me.pieces || all[r].weights_min != me.weights_min || - all[r].pieces_min != me.pieces_min) { + all[r].pieces_min != me.pieces_min || all[r].form != me.form) { std::fprintf(stderr, "[r4d] ranks %d and %d read different eight-rank two-shot " - "settings (%s, %s and their _MIN_ELEMS)\n", rank, r, - R4D_AR8_SHARD_WEIGHTS_ENV, R4D_AR8_PIECES_ENV); + "settings (%s, %s and their _MIN_ELEMS, %s)\n", rank, r, + R4D_AR8_SHARD_WEIGHTS_ENV, R4D_AR8_PIECES_ENV, R4D_AR8_FORM_ENV); return RAD_E_STATE; } in->pieces = me.pieces; in->weights_min = me.weights_min; in->pieces_min = me.pieces_min; + in->form = me.form; + if (me.form != R4D_AR8_FORM_LOCKSTEP) { + static std::once_flag said_form; + const int f = me.form; + std::call_once(said_form, [f] { + std::fprintf(stderr, "[r4d] eight-rank two-shot form: %s (%s)\n", + f == R4D_AR8_FORM_RING ? "ring" : "split", R4D_AR8_FORM_ENV); + }); + } int w[R4D_MAX_RANKS] = { 0 }; if (me.mode == AR8_SET) { @@ -1183,6 +1211,8 @@ extern "C" int r4d_rad_ar_twoshot_8rank_exact(const RadArgs* a, RadStream s) { const long long pieces = ar8_pieces_for(in, c.numel, asked); /* A diagnostic: a device buffer of nblocks * R4D_AR_TL_WORDS int64 stamps (r4d.h). */ const long long timeline = r4d_pgeti_or(a->p, a->n_p, "timeline", 0); + const long long form = r4d_pgeti_or(a->p, a->n_p, "form", in->form); + if (form < R4D_AR8_FORM_LOCKSTEP || form > R4D_AR8_FORM_SPLIT) return RAD_E_INVAL; R4D_TRY("ar_twoshot_8rank_exact", r4d_ar_twoshot_8rank_exact(in->peer_scratch[0], in->peer_scratch[1], in->peer_scratch[2], in->peer_scratch[3], in->peer_scratch[4], in->peer_scratch[5], @@ -1193,7 +1223,7 @@ extern "C" int r4d_rad_ar_twoshot_8rank_exact(const RadArgs* a, RadStream s) { in->my_scratch, in->my_flags, in->my_seq, in->slot16, c.inp, c.out, c.numel, in->dtype, in->rank, (long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, - c.pub, weights, (long)pieces, (long)timeline)); + c.pub, weights, (long)pieces, (long)timeline, (long)form)); return RAD_OK; } diff --git a/libr4d/r4d_ar_shards.h b/libr4d/r4d_ar_shards.h index b7a825e5..bad8b5b7 100644 --- a/libr4d/r4d_ar_shards.h +++ b/libr4d/r4d_ar_shards.h @@ -13,12 +13,18 @@ #define R4D_AR8_SHARD_WEIGHTS_ENV "RADIANCE_AR8_SHARD_WEIGHTS" #define R4D_AR8_PIECES_ENV "RADIANCE_AR8_PIECES" +// The eight-rank two-shot's form (r4d_ar_twoshot_Nrank_exact.hip): lockstep (unset, the one +// handshake a step), ring (the first hop sent to the ranks in ring order from the next one) or +// split (ring, and a flag after each half of a step, each awaited only where its data is read). +// Every form folds the same words in the same order, so all give the same bits. +#define R4D_AR8_FORM_ENV "RADIANCE_AR8_FORM" // Below these message sizes (elements) the weights and the pieces do not apply: a decode-sized // message pays more for the extra handshakes and the uneven split than its link time saves // (measured on the eight R9700: 196608 elements run fastest even and in one piece, 524288 already // gain from weights and two pieces). #define R4D_AR8_SHARD_WEIGHTS_MIN_ENV "RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS" #define R4D_AR8_PIECES_MIN_ENV "RADIANCE_AR8_PIECES_MIN_ELEMS" +enum { R4D_AR8_FORM_LOCKSTEP = 0, R4D_AR8_FORM_RING = 1, R4D_AR8_FORM_SPLIT = 2 }; enum { R4D_AR_SHARD_WEIGHT_MAX = 65535, R4D_AR8_MIN_ELEMS_DEFAULT = 262144, @@ -36,6 +42,19 @@ inline int r4d_ar8_auto_pieces(long long numel) { return 8; } +// "lockstep", "ring" or "split" as R4D_AR8_FORM_*; -1 for anything else. +inline int r4d_ar8_form_parse(const char* s) { + if (!s) return -1; + const char* names[3] = { "lockstep", "ring", "split" }; + for (int f = 0; f < 3; ++f) { + const char* a = names[f]; + const char* b = s; + while (*a && *a == *b) { ++a; ++b; } + if (!*a && !*b) return f; + } + return -1; +} + // A non-negative decimal integer of at most `max`, nothing else. inline bool r4d_ar_parse_count(const char* s, long long max, long long* out) { if (!s || *s < '0' || *s > '9') return false; diff --git a/libr4d/r4d_ar_twoshot_Nrank_exact.hip b/libr4d/r4d_ar_twoshot_Nrank_exact.hip index 352887fc..e555a84f 100644 --- a/libr4d/r4d_ar_twoshot_Nrank_exact.hip +++ b/libr4d/r4d_ar_twoshot_Nrank_exact.hip @@ -23,6 +23,17 @@ // piece j+1's phase 1 shares the link with piece j's phase 2. A rank with a short shard then // sends and receives at once instead of one direction per phase. // +// Also at 8 ranks, two more forms of the same work (RADIANCE_AR8_FORM, the `form` parameter): +// * ring sends the first hop to the peers in ring order from rank + 1 instead of ascending, so +// at any moment each card receives from one peer rather than all seven from the same one; +// * split also replaces the one handshake a step, which waits for every peer to finish the whole +// step, by a flag after each half (the first hop of the next piece, the reduce of this one), +// each awaited only before its data is read: before reducing piece j the peers' first hop of +// j, before assembling j their reduce of j. A rank whose own half is done goes on with the +// next piece's first hop while a slower one still reduces, so the links run at their own pace +// instead of in lockstep. 2 * pieces + 1 flag values, pieces + 1 steps reserved. +// The words, their owners and their folds are the lockstep form's, so the bits are too. +// // Shares the wide family's flag/seq/scratch set (r4d_ar_wide.h): it publishes in the 2*seq flag // space plus the 2s-1 half-step (with pieces: pieces+1 values from 2s-1 on, after reserving one // step a piece), so one buffer set can carry any mix of one-shot and two-shot launches. The slot @@ -46,7 +57,7 @@ struct R4DArShardOff { // PIECED false fixes pieces at 1, so the unpipelined launch keeps the straight-line kernel. // CHAIN: the chained form's wait (before the sequence take, so a block of the next launch on the // instance never starts ahead of this one's) and add (r4d_chain.h); no block returns early. -template +template __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( const int4* __restrict__ in4, R4DArWidePeers peers, const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags, @@ -60,10 +71,14 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( __shared__ unsigned int s_seq, s_slot; const long long gate_word = r4d_ar_link_word(gate, (unsigned int)tid); // device-resident, replay-safe; `pieces` steps reserve the 2*pieces flag values this launch uses - r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot, (unsigned int)pieces); + // (the split form publishes 2 * pieces + 1 and reserves one step more) + r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot, + (unsigned int)(FORM == R4D_AR8_FORM_SPLIT ? pieces + 1 : pieces)); const unsigned int v0 = 2u * s_seq - 1u; const int parity = (int)s_slot; - long long* const tl = tl_all ? tl_all + (size_t)b * R4D_AR_TL_WORDS : nullptr; + // the split form awaits 2 * pieces times, which the timeline holds up to 8 pieces + long long* const tl = tl_all && (FORM != R4D_AR8_FORM_SPLIT || 4 * pieces + 2 <= R4D_AR_TL_WORDS) + ? tl_all + (size_t)b * R4D_AR_TL_WORDS : nullptr; r4d_ar_tl_stamp(tl, 0); const int sh_max = sh.sh_max; @@ -91,7 +106,8 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( // phase 1: my contribution to each peer-owned shard -> owner's region A auto push1 = [&](int j) { const int p0 = w0 + j * pw, p1 = p0 + pw < w1 ? p0 + pw : w1; - for (int p = 0; p < NPEERS; ++p) { + for (int i = 0; i < NPEERS; ++i) { + const int p = FORM == R4D_AR8_FORM_LOCKSTEP ? i : (i + rank) % NPEERS; const int owner = peers.rank[p]; const int4* s1 = in4 + (size_t)R4D_TS_OFF(owner); int4* dst = pa[p]; @@ -143,25 +159,59 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( * overlap one direction idles in each phase. With one piece the sequence is push, flag 2s-1, * reduce, flag 2s, assemble. A word is touched by the same thread in all three steps, which keeps * the in-place form safe. */ - push1(0); - r4d_ar_handshake(peers, my_flags, b, rank, v0, drain, acq, pub, tl ? tl + 1 : nullptr); - for (int j = 0; j < pieces; ++j) { - if (j + 1 < pieces) push1(j + 1); - reduce2(j); - if (j > 0) assemble(j - 1); - r4d_ar_handshake(peers, my_flags, b, rank, v0 + 1u + (unsigned int)j, drain, acq, pub, - tl ? tl + 3 + 2 * j : nullptr); - } - /* The hold ends after the last handshake, not the first: the reduce is fused with the phase-2 - * push (every reduced word goes to each peer's region B), so until then the link still carries - * this launch's phase-2 bytes. Here every phase-1 and phase-2 remote store has retired behind - * its flag store and every peer's pushes to this rank have been waited on; only the local - * assemble of the last piece is left (r4d_ar_seq.h). */ - r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); - assemble(pieces - 1); - if (tl) { - __syncthreads(); - r4d_ar_tl_stamp(tl, 3 + 2 * pieces); + if constexpr (FORM != R4D_AR8_FORM_SPLIT) { + push1(0); + r4d_ar_handshake(peers, my_flags, b, rank, v0, drain, acq, pub, tl ? tl + 1 : nullptr); + for (int j = 0; j < pieces; ++j) { + if (j + 1 < pieces) push1(j + 1); + reduce2(j); + if (j > 0) assemble(j - 1); + r4d_ar_handshake(peers, my_flags, b, rank, v0 + 1u + (unsigned int)j, drain, acq, pub, + tl ? tl + 3 + 2 * j : nullptr); + } + /* The hold ends after the last handshake, not the first: the reduce is fused with the phase-2 + * push (every reduced word goes to each peer's region B), so until then the link still carries + * this launch's phase-2 bytes. Here every phase-1 and phase-2 remote store has retired behind + * its flag store and every peer's pushes to this rank have been waited on; only the local + * assemble of the last piece is left (r4d_ar_seq.h). */ + r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); + assemble(pieces - 1); + if (tl) { + __syncthreads(); + r4d_ar_tl_stamp(tl, 3 + 2 * pieces); + } + } else { + /* The same steps with a flag after each half. Published in program order on one monotone + * word: first hop of piece 0 at v0, of piece j + 1 at v0 + 2j + 1, the reduce of piece j at + * v0 + 2j + 2; so "peer's first hop of j done" is the word at v0 (j = 0) or v0 + 2j - 1, and + * "peer's reduce of j done" at v0 + 2j + 2. A launch completes only after every peer has + * published its last reduce, which it does inside this launch, so the slot parity argument + * of the lockstep form holds unchanged (r4d_ar_seq.h). */ + int n = 0; + auto tlp = [&]() { long long* p = tl ? tl + 1 + 2 * n : nullptr; ++n; return p; }; + push1(0); + r4d_ar_publish(peers, b, rank, v0, drain, pub); + for (int j = 0; j < pieces; ++j) { + if (j + 1 < pieces) { + push1(j + 1); + r4d_ar_publish(peers, b, rank, v0 + 2u * (unsigned int)j + 1u, drain, pub); + } + r4d_ar_await(peers, my_flags, b, j == 0 ? v0 : v0 + 2u * (unsigned int)j - 1u, acq, + tlp()); + reduce2(j); + r4d_ar_publish(peers, b, rank, v0 + 2u * (unsigned int)j + 2u, drain, pub); + if (j > 0) { + r4d_ar_await(peers, my_flags, b, v0 + 2u * (unsigned int)j, acq, tlp()); + assemble(j - 1); + } + } + r4d_ar_await(peers, my_flags, b, v0 + 2u * (unsigned int)pieces, acq, tlp()); + r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); + assemble(pieces - 1); + if (tl) { + __syncthreads(); + r4d_ar_tl_stamp(tl, 1 + 2 * n); + } } #undef R4D_TS_OFF #undef R4D_TS_LEN @@ -391,13 +441,13 @@ __device__ __forceinline__ void r4d_ar_twoshot_coded_body( unsigned int* gate_ctr, int pieces_arg, long long* tl_all #define R4D_ARTS_ARGS in4, peers, my_base, my_flags, out4, seq_ctrs, sh, slot_stride16, rank, \ drain, acq, pub, gate, gate_ctr, pieces_arg, tl_all -template +template __global__ void r4d_ar_twoshot_wide_exact_kernel(R4D_ARTS_PARAMS) { - r4d_ar_twoshot_wide_exact_body(R4D_ARTS_ARGS); + r4d_ar_twoshot_wide_exact_body(R4D_ARTS_ARGS); } -template +template __global__ void r4dchain_ar_twoshot_wide_exact(R4D_ARTS_PARAMS) { - r4d_ar_twoshot_wide_exact_body(R4D_ARTS_ARGS); + r4d_ar_twoshot_wide_exact_body(R4D_ARTS_ARGS); } #undef R4D_ARTS_ARGS #undef R4D_ARTS_PARAMS @@ -427,7 +477,7 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags, long out, long n_elem, long dtype, long rank, long stream_i, long nblocks, long nthreads, long drain, long acq, long pub, long gate, long gate_ctr, const int* shard_weights, - long pieces, long timeline, bool chain = false) { + long pieces, long timeline, long form, bool chain = false) { constexpr int NPEERS = WS - 1; if (rank < 0 || rank >= WS) throw std::runtime_error("all_reduce: rank " + std::to_string(rank) + @@ -476,6 +526,10 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags, if (pieces < 1 || pieces > R4D_AR_TS_MAX_PIECES) throw std::runtime_error("all_reduce: pieces " + std::to_string(pieces) + " outside 1.." + std::to_string((int)R4D_AR_TS_MAX_PIECES)); + if (form < R4D_AR8_FORM_LOCKSTEP || form > R4D_AR8_FORM_SPLIT || + (WS != 8 && form != R4D_AR8_FORM_LOCKSTEP)) + throw std::runtime_error("all_reduce: form " + std::to_string(form) + " at " + + std::to_string(WS) + " ranks (ring and split are eight-rank forms)"); // a block's share of the longest shard bounds the pieces worth a handshake each const long words_a_block = (sh_max + nb - 1) / nb; const int np = (int)(pieces < words_a_block ? pieces : words_a_block); @@ -504,13 +558,26 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags, } hipStream_t st = (hipStream_t)stream_i; -#define R4D_AR_TS_LAUNCH_P(T, LN, P) \ - (chain ? r4dchain_ar_twoshot_wide_exact \ - : r4d_ar_twoshot_wide_exact_kernel)<<>>( \ +#define R4D_AR_TS_LAUNCH_F(T, LN, P, F) \ + (chain ? r4dchain_ar_twoshot_wide_exact \ + : r4d_ar_twoshot_wide_exact_kernel)<<>>( \ (const int4*)inp, peers, (const int4*)my_scratch, (unsigned int*)my_flags, \ (int4*)out, (unsigned int*)seq_ctrs, sh, (int)slot_stride16, \ (int)rank, (int)drain, (int)acq, (int)pub, (long long*)gate, \ (unsigned int*)gate_ctr, np, (long long*)timeline) +#define R4D_AR_TS_LAUNCH_P(T, LN, P) \ + do { \ + if constexpr (WS == 8) { \ + if (form == R4D_AR8_FORM_RING) \ + R4D_AR_TS_LAUNCH_F(T, LN, P, R4D_AR8_FORM_RING); \ + else if (form == R4D_AR8_FORM_SPLIT) \ + R4D_AR_TS_LAUNCH_F(T, LN, P, R4D_AR8_FORM_SPLIT); \ + else \ + R4D_AR_TS_LAUNCH_F(T, LN, P, R4D_AR8_FORM_LOCKSTEP); \ + } else { \ + R4D_AR_TS_LAUNCH_F(T, LN, P, R4D_AR8_FORM_LOCKSTEP); \ + } \ + } while (0) #define R4D_AR_TS_LAUNCH(T, LN) \ do { \ if (np > 1) R4D_AR_TS_LAUNCH_P(T, LN, true); \ @@ -522,6 +589,7 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags, else throw std::runtime_error("all_reduce: bad dtype"); #undef R4D_AR_TS_LAUNCH #undef R4D_AR_TS_LAUNCH_P +#undef R4D_AR_TS_LAUNCH_F } void r4d_ar_twoshot_3rank_exact(long peer_scratch0, long peer_scratch1, long peer_flags0, @@ -534,7 +602,7 @@ void r4d_ar_twoshot_3rank_exact(long peer_scratch0, long peer_scratch1, long pee /* No link gate at three ranks: the row answers 1, the engine releases by barrier. */ ar_twoshot_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem, dtype, rank, stream, nblocks, nthreads, drain, acq, pub, 0L, 0L, nullptr, - 1L, 0L); + 1L, 0L, 0L); } void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2, @@ -547,7 +615,7 @@ void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long pee const long pf[3] = {peer_flags0, peer_flags1, peer_flags2}; ar_twoshot_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem, dtype, rank, stream, nblocks, nthreads, drain, acq, pub, gate, gate_ctr, - nullptr, 1L, 0L, chain != 0); + nullptr, 1L, 0L, 0L, chain != 0); } /* The coded form's conditions: bf16, a message that shards evenly into whole waves' 32 chunks @@ -602,7 +670,8 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee long my_flags, long seq_ctrs, long slot_stride16, long inp, long out, long n_elem, long dtype, long rank, long stream, long nblocks, long nthreads, long drain, long acq, long pub, - const int* shard_weights, long pieces, long timeline) { + const int* shard_weights, long pieces, long timeline, + long form) { const long ps[7] = {peer_scratch0, peer_scratch1, peer_scratch2, peer_scratch3, peer_scratch4, peer_scratch5, peer_scratch6}; const long pf[7] = {peer_flags0, peer_flags1, peer_flags2, peer_flags3, @@ -610,5 +679,5 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee /* No link gate at eight ranks: the row answers 1, the engine releases by barrier. */ ar_twoshot_launch<8>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem, dtype, rank, stream, nblocks, nthreads, drain, acq, pub, 0L, 0L, - shard_weights, pieces, timeline); + shard_weights, pieces, timeline, form); } diff --git a/libr4d/r4d_ar_wide.h b/libr4d/r4d_ar_wide.h index ce313f7f..c390615d 100644 --- a/libr4d/r4d_ar_wide.h +++ b/libr4d/r4d_ar_wide.h @@ -117,6 +117,50 @@ __device__ __forceinline__ void r4d_ar_handshake(const R4DArWidePeers r4d_ar_acquire(acq); } +// The two halves of r4d_ar_handshake, for a kernel that tells its peers how far it has come before +// it needs to know how far they have (the eight-rank two-shot's split form). Publish: once this +// block's stores have drained, every peer's word [b][rank] takes `val`. +template +__device__ __forceinline__ void r4d_ar_publish(const R4DArWidePeers& peers, int b, + int rank, unsigned int val, int drain, int pub) { + r4d_ar_drain(drain); + __syncthreads(); + if (threadIdx.x == 0) { + if (drain == 2) __threadfence_system(); + if (pub) { + __builtin_amdgcn_fence(__ATOMIC_RELEASE, ""); +#pragma unroll + for (int p = 0; p < NPEERS; ++p) + r4d_ar_store_sys_rlx(&peers.flags[p][(size_t)b * WS + rank], val); + } else { +#pragma unroll + for (int p = 0; p < NPEERS; ++p) + r4d_ar_store_sys_rel(&peers.flags[p][(size_t)b * WS + rank], val); + } + } +} + +// Await: wait until each of this block's words from every peer has reached `val`, then acquire +// what they published before it. `tl` as r4d_ar_handshake's, stamped on entry and on release. +template +__device__ __forceinline__ void r4d_ar_await(const R4DArWidePeers& peers, + unsigned int* __restrict__ my_flags, int b, + unsigned int val, int acq, long long* tl = nullptr) { + const int tid = threadIdx.x; + if (tl && tid == 0) tl[0] = (long long)wall_clock64(); + if (tid < NPEERS) { + const unsigned int* f = &my_flags[(size_t)b * WS + peers.rank[tid]]; + unsigned long long z = 0; + while ((int)(r4d_ar_load_sys_rlx(f) - val) < 0) { + __builtin_amdgcn_s_sleep(2); + if (++z > R4D_AR_SPIN_MAX) __builtin_trap(); // lockstep violation + } + } + __syncthreads(); + if (tl && tid == 0) tl[1] = (long long)wall_clock64(); + r4d_ar_acquire(acq); +} + // The two-shot timeline (layout in r4d_ar_shards.h), one stamp of the device wall clock. static_assert(R4D_AR_TL_BLOCKS == R4D_AR_WIDE_MAX_BLOCKS, "r4d_ar_shards.h restates the ceiling"); __device__ __forceinline__ void r4d_ar_tl_stamp(long long* tl, int i) { diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index f104a698..dafbc6bb 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -22258,10 +22258,39 @@ static void ar_wide_weighted(const Plugin& pl, const std::vector& devs, con mixed); } } - /* The weights from this box's links ("auto", the default) and switched off, at the default - * floors and piece rule: what the engine runs without settings. Bits only, since what auto - * derives depends on the box (the startup line names it). */ - for (const char* mode : { "auto", "off" }) { + /* The ring and split forms (RADIANCE_AR8_FORM) over the same shards and piece counts: other + * link schedules of the same folds, so the oracle is unchanged. The split form takes two flag + * values a piece, so the run changing the piece count from launch to launch is the one a + * wrong reservation would fail. */ + { + ScopedEnv floor_w(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, "0"), floor_k(R4D_AR8_PIECES_MIN_ENV, "0"); + for (const char* form : { "ring", "split" }) { + ScopedEnv env_f(R4D_AR8_FORM_ENV, form); + for (const char* k : { "1", "4", "8" }) { + ScopedEnv env_k(R4D_AR8_PIECES_ENV, k); + for (const char* w : { "", "0,3,1,0,5,1,2,4", "1,0,0,0,0,0,0,0", "4,1,0,4,0,1,4,4" }) { + ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, *w ? w : "off"); + const std::string label = + std::string(name) + " form " + form + " pieces " + k + + (*w ? std::string(" weights ") + w : std::string(" even")); + for (const ArPatternRun& run : *w ? uneven : even) + if (!ar_pattern_check(row, devs, label, W, run)) return; + } + } + ScopedEnv env_k(R4D_AR8_PIECES_ENV, nullptr); + ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, "4,1,0,4,0,1,4,4"); + const ArPatternRun mixed = { + "bf16", { 4194304, 4194304, 8256, 4196352, 72, 4194304, 4194304 }, 3, + { 1, 4, 2, 8, 16, 3, 1 } }; + if (!ar_pattern_check(row, devs, std::string(name) + " form " + form + + " weights 4,1,0,4,0,1,4,4, pieces per launch", W, mixed)) + return; + } + } + /* The weights from this box's links ("auto", the default, and "duplex") and switched off, at + * the default floors and piece rule: what the engine runs without settings. Bits only, since + * what auto and duplex derive depends on the box (the startup line names it). */ + for (const char* mode : { "auto", "duplex", "off" }) { ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, mode); ScopedEnv env_k(R4D_AR8_PIECES_ENV, nullptr); ScopedEnv floor_w(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, nullptr); @@ -22358,6 +22387,7 @@ struct Ar8Perf { std::vector sizes; std::vector rows; std::vector ks; /* per row: the `pieces` parameter, 0 or -1 none */ + std::vector forms; /* per row: the `form` parameter, -1 none */ std::vector nbs; StBarrier* bar = nullptr; int rc_init = RAD_OK; @@ -22428,6 +22458,7 @@ static void ar8_perf_worker(Ar8Perf* t) { std::vector ps = ip; if (nb > 0) ps.push_back(PI("nblocks", nb)); if (t->ks[j] > 0) ps.push_back(PI("pieces", t->ks[j])); + if (t->forms[j] >= 0) ps.push_back(PI("form", t->forms[j])); RadTensor ts[2] = { T(in, dt, { n }), T(out, dt, { n }) }; RadArgs a; std::memset(&a, 0, sizeof(a)); @@ -22544,7 +22575,8 @@ static void ar8_print_timeline(const std::vector& t, size_t cfg, double nst = nst ? std::min(nst, k) : k; } if (nb == 0 || nst < 4) { std::printf(" timeline r%d: no stamps\n", r); continue; } - const int pieces = (nst - 4) / 2; /* start, K + 1 pairs, end */ + /* start, a pair a synchronisation (K + 1 handshakes in lockstep, 2K awaits split), end */ + const int syncs = (nst - 2) / 2; auto ev = [&](int k, bool first) { long long m = v[(size_t)k]; for (int b = 1; b < nb; ++b) { @@ -22558,7 +22590,7 @@ static void ar8_print_timeline(const std::vector& t, size_t cfg, double std::string line; double work = 0, wait = 0; long long prev = t0; - for (int h = 0; h <= pieces; ++h) { + for (int h = 0; h < syncs; ++h) { const long long arr = ev(1 + 2 * h, false), rel = ev(2 + 2 * h, false); const double w1 = (double)(arr - prev) * us, w2 = (double)(rel - arr) * us; work += w1; wait += w2; @@ -22566,11 +22598,11 @@ static void ar8_print_timeline(const std::vector& t, size_t cfg, double w1, w2); prev = rel; } - const long long end = ev(3 + 2 * pieces, false); + const long long end = ev(1 + 2 * syncs, false); const double tail = (double)(end - prev) * us; work += tail; - std::printf(" timeline r%d (%d blocks, K%d): %s | tail %.1f = %.1f us " - "(work %.1f, wait %.1f)\n", r, nb, pieces, line.c_str(), tail, + std::printf(" timeline r%d (%d blocks, %d syncs): %s | tail %.1f = %.1f us " + "(work %.1f, wait %.1f)\n", r, nb, syncs, line.c_str(), tail, (double)(end - t0) * us, work, wait); } } @@ -22609,7 +22641,8 @@ static bool ar8_weights_as_plugin(const std::vector& devs, int* w, std::str return false; } -static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dtype) { +static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dtype, + const char* form_list) { constexpr int W = 8; if (iters <= 0) iters = 200; const bool bf = !std::strcmp(dtype, "bf16"); @@ -22647,18 +22680,39 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty if (hipDeviceGetAttribute(&wall_khz_i, hipDeviceAttributeWallClockRate, devs[0]) != hipSuccess) wall_khz_i = 0; const double wall_khz = (double)wall_khz_i; - std::vector rows; - std::vector ks; - /* The two-shot once a piece count; its instances share one key, so they are one buffer set. */ - for (const char* name : { "ar_oneshot_8rank_exact", "ar_twoshot_8rank_exact" }) { - const RadKernelInfo* r = find(pl, name); - if (!r) { std::printf("--perfar8: row %s absent\n", name); return; } - const bool two = !std::strcmp(name, "ar_twoshot_8rank_exact"); - for (int k : two ? std::vector{ -1, 1, 2, 4, 8 } : std::vector{ 0 }) { - rows.push_back(r); - ks.push_back(k); + /* The forms to time beside each other: "lockstep" alone (the default) passes no `form`, so + * RADIANCE_AR8_FORM decides as in the engine; a list passes each explicitly. */ + std::vector fl; + { + std::string all = form_list ? form_list : "lockstep"; + size_t at = 0; + while (at <= all.size()) { + const size_t comma = all.find(',', at); + const std::string one = all.substr(at, comma == std::string::npos ? std::string::npos + : comma - at); + const int f = r4d_ar8_form_parse(one.c_str()); + if (f < 0) { std::printf("--perfar8: form '%s' (lockstep, ring or split)\n", + one.c_str()); return; } + fl.push_back(f); + if (comma == std::string::npos) break; + at = comma + 1; } } + const bool explicit_forms = !(fl.size() == 1 && fl[0] == R4D_AR8_FORM_LOCKSTEP); + std::vector rows; + std::vector ks, forms; + /* The two-shot once a piece count and form; its instances share one key, so they are one + * buffer set. */ + { + const RadKernelInfo* one = find(pl, "ar_oneshot_8rank_exact"); + const RadKernelInfo* two = find(pl, "ar_twoshot_8rank_exact"); + if (!one || !two) { std::printf("--perfar8: an eight-rank exact row is absent\n"); return; } + rows.push_back(one); ks.push_back(0); forms.push_back(-1); + for (int f : fl) + for (int k : { -1, 1, 2, 4, 8 }) { + rows.push_back(two); ks.push_back(k); forms.push_back(explicit_forms ? f : -1); + } + } /* Ranks sharing a card must have every block resident at once, since a block spins until its * twins on the other ranks arrive: eight ranks of at most 8 blocks of 1024 threads fit an R9700, * and the block rule can ask for 16. */ @@ -22704,7 +22758,7 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty for (int r = 0; r < W; ++r) { Ar8Perf& x = t[(size_t)r]; x.rank = r; x.dev = devs[(size_t)r]; x.iters = iters; x.dtype = dtype; - x.sizes = sizes; x.rows = rows; x.ks = ks; x.nbs = nbs; x.bar = &bar; + x.sizes = sizes; x.rows = rows; x.ks = ks; x.forms = forms; x.nbs = nbs; x.bar = &bar; th.emplace_back(ar8_perf_worker, &x); } for (std::thread& x : th) x.join(); @@ -22735,10 +22789,14 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty /* The piece count the shim runs (before the launcher's words-a-block clamp). */ const int keff = n < kmin ? 1 : ks[j] > 0 ? ks[j] : kenv ? (int)kenv : r4d_ar8_auto_pieces(n); + const char* fam = forms[j] == R4D_AR8_FORM_RING ? "ring" + : forms[j] == R4D_AR8_FORM_SPLIT ? "split" + : forms[j] == R4D_AR8_FORM_LOCKSTEP ? "lockstep" + : "two-shot"; const std::string rname = !two ? std::string("one-shot") - : ks[j] < 0 ? fmt("two-shot set K%d", keff) - : keff != ks[j] ? fmt("two-shot K%d>%d", ks[j], keff) - : fmt("two-shot K%d", ks[j]); + : ks[j] < 0 ? fmt("%s set K%d", fam, keff) + : keff != ks[j] ? fmt("%s K%d>%d", fam, ks[j], keff) + : fmt("%s K%d", fam, ks[j]); const double sent = two ? 2.0 * (W - 1) / W * bytes : (double)(W - 1) * bytes; for (int nb : nbs) { const Ar8Perf& r0 = t[0]; @@ -27179,10 +27237,12 @@ static void usage(void) { " or runs (S contiguous runs). a=bf16 (default) or a=fp8_e4m3 (scale\n" " every 128); y=bf16 (default), y=f16 or y=f32. Each row's t and y are\n" " compared with the first row's; --cold applies.\n" - " --perfar8 numel [iters] [dtype]: the eight-rank exact all-reduce, one thread\n" + " --perfar8 numel [iters] [dtype] [forms]: the eight-rank exact all-reduce, one thread\n" " a card: one-shot and two-shot at the block rule and at 4..64 blocks,\n" " each checked against the rank-order fold; numel 0 runs the decode\n" " and prefill messages at 4096 wide. dtype bf16 (default) or fp32.\n" + " forms: the two-shot's, comma-separated (lockstep, ring, split;\n" + " default lockstep, which leaves RADIANCE_AR8_FORM in charge).\n" " With --timeline each two-shot point is launched once more with its\n" " per-block stamps on, and every rank's push, waits, steps and tail\n" " are printed. R4D_SELFTEST_ONE_CARD=1 runs the eight ranks on one card.\n" @@ -27269,6 +27329,7 @@ int main(int argc, char** argv) { const char* ar8_dt = "bf16"; long long lg_m = 0; int lg_it = 200; + const char* ar8_forms = "lockstep"; long long mx_m = 0, mx_e = 288, mx_k = 8; const char* mx_skew = nullptr; bool mx_f8 = false; @@ -27407,6 +27468,7 @@ int main(int argc, char** argv) { ar8_n = std::strtoll(argv[++i], nullptr, 0); if (i + 1 < argc && argv[i + 1][0] != 0x2d) ar8_it = (int)std::strtol(argv[++i], nullptr, 0); if (i + 1 < argc && argv[i + 1][0] != 0x2d) ar8_dt = argv[++i]; + if (i + 1 < argc && argv[i + 1][0] != 0x2d) ar8_forms = argv[++i]; } else if (!std::strcmp(argv[i], "--perfmhcpre")) { /* mhc_pre and mhc_post at GLM's shape, 16-byte rows against pitched ones. */ if (i + 1 >= argc) { usage(); return 2; } @@ -27570,7 +27632,7 @@ int main(int argc, char** argv) { if (ar8_n >= 0) { if (!load(&pl, path)) return 1; #ifdef RAD_HAVE_HIP - ar8_perf(pl, ar8_n, ar8_it, ar8_dt); + ar8_perf(pl, ar8_n, ar8_it, ar8_dt, ar8_forms); dfree_all(); #else skip("eight-rank all-reduce timing", "built without HIP");