diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 08552a34..5cfb2b09 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -451,7 +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); + 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 +// 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 // r4d_ar_ti8.h), 3 or 4 ranks. Int8 groups of 32 with an fp16 scale and a per-group record // (T1 plain, T2 top-1-exact, T3 wht32), 38 bytes/group; tiering is deterministic and diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index bb3a3e98..3504edaa 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -1174,6 +1174,8 @@ extern "C" int r4d_rad_ar_twoshot_8rank_exact(const RadArgs* a, RadStream s) { const long long asked = r4d_pgeti_or(a->p, a->n_p, "pieces", in->pieces); if (asked < 0 || asked > R4D_AR_TS_MAX_PIECES) return RAD_E_INVAL; 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); 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], @@ -1184,7 +1186,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)); + c.pub, weights, (long)pieces, (long)timeline)); return RAD_OK; } diff --git a/libr4d/r4d_ar_shards.h b/libr4d/r4d_ar_shards.h index 6d3de9ab..b7a825e5 100644 --- a/libr4d/r4d_ar_shards.h +++ b/libr4d/r4d_ar_shards.h @@ -109,3 +109,9 @@ inline long r4d_ar_shard_max_bound(long n16, int ws, const int* w) { const long b = n16 * wmax / sum + 1; return b < n16 ? b : n16; } + +// The two-shot timeline, a diagnostic (r4d.h, `timeline`): per block R4D_AR_TL_WORDS stamps of the +// device wall clock (hipDeviceAttributeWallClockRate), [0] after the sequence step, an (arrive, +// release) pair per handshake, then the end of the block's last local work, so a launch in K +// pieces fills 2 + 2 * (K + 1). R4D_AR_TL_BLOCKS is the wide family's block ceiling. +enum { R4D_AR_TL_WORDS = 2 + 2 * (16 + 1), R4D_AR_TL_BLOCKS = 64 }; diff --git a/libr4d/r4d_ar_twoshot_Nrank_exact.hip b/libr4d/r4d_ar_twoshot_Nrank_exact.hip index 2b79e64b..352887fc 100644 --- a/libr4d/r4d_ar_twoshot_Nrank_exact.hip +++ b/libr4d/r4d_ar_twoshot_Nrank_exact.hip @@ -34,6 +34,9 @@ #include #include +static_assert(R4D_AR_TL_WORDS >= 4 + 2 * R4D_AR_TS_MAX_PIECES, + "a timeline holds the start, a stamp pair a handshake and the end"); + template struct R4DArShardOff { int off[WS + 1]; @@ -49,7 +52,7 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags, int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, R4DArShardOff sh, int slot_stride16, int rank, int drain, int acq, int pub, - long long* gate, unsigned int* gate_ctr, int pieces_arg) { + long long* gate, unsigned int* gate_ctr, int pieces_arg, long long* tl_all) { if (CHAIN) r4d_chain_wait(); constexpr int NPEERS = WS - 1; const int pieces = PIECED ? pieces_arg : 1; @@ -60,6 +63,8 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot, (unsigned int)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; + r4d_ar_tl_stamp(tl, 0); const int sh_max = sh.sh_max; #define R4D_TS_OFF(s) (sh.off[s]) @@ -139,12 +144,13 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( * 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); + 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); + 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 @@ -153,6 +159,10 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body( * 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); + } #undef R4D_TS_OFF #undef R4D_TS_LEN if (CHAIN) r4d_chain_done(); @@ -378,9 +388,9 @@ __device__ __forceinline__ void r4d_ar_twoshot_coded_body( const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags, \ int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, R4DArShardOff sh, \ int slot_stride16, int rank, int drain, int acq, int pub, long long* gate, \ - unsigned int* gate_ctr, int pieces_arg + 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 + drain, acq, pub, gate, gate_ctr, pieces_arg, tl_all template __global__ void r4d_ar_twoshot_wide_exact_kernel(R4D_ARTS_PARAMS) { r4d_ar_twoshot_wide_exact_body(R4D_ARTS_ARGS); @@ -417,7 +427,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, bool chain = false) { + long pieces, long timeline, bool chain = false) { constexpr int NPEERS = WS - 1; if (rank < 0 || rank >= WS) throw std::runtime_error("all_reduce: rank " + std::to_string(rank) + @@ -500,7 +510,7 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags, (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) + (unsigned int*)gate_ctr, np, (long long*)timeline) #define R4D_AR_TS_LAUNCH(T, LN) \ do { \ if (np > 1) R4D_AR_TS_LAUNCH_P(T, LN, true); \ @@ -524,7 +534,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); + 1L, 0L); } void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2, @@ -537,7 +547,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, chain != 0); + nullptr, 1L, 0L, chain != 0); } /* The coded form's conditions: bf16, a message that shards evenly into whole waves' 32 chunks @@ -592,7 +602,7 @@ 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) { + const int* shard_weights, long pieces, long timeline) { 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, @@ -600,5 +610,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); + shard_weights, pieces, timeline); } diff --git a/libr4d/r4d_ar_wide.h b/libr4d/r4d_ar_wide.h index 97089d3c..ce313f7f 100644 --- a/libr4d/r4d_ar_wide.h +++ b/libr4d/r4d_ar_wide.h @@ -16,6 +16,7 @@ #include #include #include "r4d_ar_seq.h" +#include "r4d_ar_shards.h" #define R4D_AR_SPIN_MAX 4000000000ULL #define R4D_AR_WIDE_MAX_BLOCKS 64 @@ -66,14 +67,19 @@ __device__ __forceinline__ void r4d_ar_acquire(int acq) { // words has reached or passed it (peers may be a step ahead; counters only grow). One spin lane // per source, wrap-safe monotonic compare, never equality. Every rank publishes before it waits, // so there is no cycle; a handshake that cannot complete traps. +// +// `tl`, when not null, takes two wall-clock stamps: when the whole block has issued and drained +// its stores, and when every peer's flag has been seen. A diagnostic: the eight-rank two-shot +// passes it only for a launch asking for its timeline (r4d_ar_tl_stamp). template __device__ __forceinline__ void r4d_ar_handshake(const R4DArWidePeers& peers, unsigned int* __restrict__ my_flags, int b, int rank, unsigned int val, int drain, int acq, - int pub) { + int pub, long long* tl = nullptr) { r4d_ar_drain(drain); __syncthreads(); const int tid = threadIdx.x; + if (tl && tid == 0) tl[0] = (long long)wall_clock64(); if (tid == 0) { if (drain == 2) __threadfence_system(); if (pub == 2) { @@ -107,9 +113,16 @@ __device__ __forceinline__ void r4d_ar_handshake(const R4DArWidePeers } } __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) { + if (tl && threadIdx.x == 0) tl[i] = (long long)wall_clock64(); +} + // Host side: checks stride and grid against the real allocations. Every scratch, flag and seq // pointer comes from hipMalloc / hipIpcOpenMemHandle, so the runtime can report its extent; a // pointer it cannot size is refused. diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index a1bed290..fe4faabe 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -403,6 +403,9 @@ static void cross_check(const Plugin& pl) { /* ================================================================== the device phase */ /* --cold MIB: bytes read through the caches before every timed launch; see st_time_us. */ static int64_t g_cold = 0; +/* --timeline: --perfar8 also records the two-shot's per-block stamps (r4d.h) and prints where each + * rank's time goes. */ +static bool g_timeline = false; /* R4D_SELFTEST_ONE_CARD=1 runs the eight-rank collectives as eight ranks on the first card, each * on its own non-blocking stream: the protocol, the shard table and the arithmetic, with VRAM in @@ -22364,12 +22367,15 @@ struct Ar8Perf { std::vector bad; std::vector period; std::vector> cold; + /* --timeline: per config, the two-shot's stamps (nblocks x R4D_AR_TL_WORDS, -1 unset). */ + std::vector> tl; }; static void ar8_perf_worker(Ar8Perf* t) { const size_t ncfg = t->sizes.size() * t->rows.size() * t->nbs.size(); t->rc.assign(ncfg, RAD_E_STATE); t->bad.assign(ncfg, 0); t->period.assign(ncfg, 0.0f); t->cold.assign(ncfg, {}); + t->tl.assign(ncfg, {}); if (st_set_device(t->dev) != hipSuccess) t->rc_init = RAD_E_DEVICE; if (t->rc_init == RAD_OK && st_one_card() && hipStreamCreateWithFlags(&t->st, hipStreamNonBlocking) != hipSuccess) @@ -22384,10 +22390,12 @@ static void ar8_perf_worker(Ar8Perf* t) { }; for (size_t j = 0; j < t->rows.size() && t->rc_init == RAD_OK; ++j) t->rc_init = t->rows[j]->init(ip.data(), (int)ip.size(), t->rank, t->world, &inst[j]); - void *in = nullptr, *out = nullptr, *csrc = nullptr, *cdst = nullptr; + void *in = nullptr, *out = nullptr, *csrc = nullptr, *cdst = nullptr, *tlbuf = nullptr; + const size_t tl_words = (size_t)R4D_AR_TL_BLOCKS * R4D_AR_TL_WORDS; if (t->rc_init == RAD_OK && (hipMalloc(&in, (size_t)most * esz) != hipSuccess || hipMalloc(&out, (size_t)most * esz) != hipSuccess || + (g_timeline && hipMalloc(&tlbuf, tl_words * sizeof(long long)) != hipSuccess) || (g_cold > 0 && (hipMalloc(&csrc, (size_t)g_cold) != hipSuccess || hipMalloc(&cdst, (size_t)std::min(g_cold, kColdChunk)) != hipSuccess)))) t->rc_init = RAD_E_NOMEM; @@ -22484,6 +22492,23 @@ static void ar8_perf_worker(Ar8Perf* t) { } } t->bar->wait(); + /* One more launch, warm, with the stamps on: a two-shot row reads `timeline`, the + * one-shot ignores it and leaves the buffer unset. */ + if (timed && g_timeline && tlbuf) { + std::vector pt = ps; + pt.push_back(PI("timeline", (int64_t)(uintptr_t)tlbuf)); + a.p = pt.data(); a.n_p = (int)pt.size(); + hip_ck(hipMemsetAsync(tlbuf, 0xff, tl_words * sizeof(long long), t->st), + "hipMemsetAsync", __LINE__); + t->bar->wait(); + t->rows[j]->launch(&a, t->st); + st_stream_sync(t->st); + t->tl[cfg].resize(tl_words); + st_memcpy(t->tl[cfg].data(), tlbuf, tl_words * sizeof(long long), + hipMemcpyDeviceToHost); + a.p = ps.data(); a.n_p = (int)ps.size(); + } + t->bar->wait(); ++cfg; } } @@ -22493,10 +22518,63 @@ static void ar8_perf_worker(Ar8Perf* t) { t->bar->wait(); for (size_t j = 0; j < t->rows.size(); ++j) if (inst[j] && t->rows[j]->fini) t->rows[j]->fini(inst[j]); - for (void* p : { in, out, csrc, cdst }) if (p) st_free(p); + for (void* p : { in, out, csrc, cdst, tlbuf }) if (p) st_free(p); if (t->st) hip_ck(hipStreamDestroy(t->st), "hipStreamDestroy", __LINE__); } +/* The two-shot's stamps for one config as each rank's phases, in microseconds. A rank's event is + * its last block's (the rank has issued everything only when every block has); the start is its + * first block's. Steps follow the kernel: push the first piece, wait for every peer's flag, then a + * step a piece (push the next piece's first hop, reduce and send this piece's second, assemble the + * previous one) and its wait, then the last assemble. With one piece the first push is the + * reduce-scatter's send, the first wait its receipt, the step the reduce and the all-gather's + * send, the second wait its receipt and the tail the local assemble. */ +static void ar8_print_timeline(const std::vector& t, size_t cfg, double khz) { + if (khz <= 0) return; + const int W = (int)t.size(); + for (int r = 0; r < W; ++r) { + const std::vector& v = t[(size_t)r].tl[cfg]; + if (v.empty()) return; + int nb = 0, nst = 0; + for (int b = 0; b < R4D_AR_TL_BLOCKS; ++b) { + int k = 0; + while (k < R4D_AR_TL_WORDS && v[(size_t)b * R4D_AR_TL_WORDS + k] >= 0) ++k; + if (k == 0) break; + nb = b + 1; + 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 */ + auto ev = [&](int k, bool first) { + long long m = v[(size_t)k]; + for (int b = 1; b < nb; ++b) { + const long long x = v[(size_t)b * R4D_AR_TL_WORDS + k]; + m = first ? std::min(m, x) : std::max(m, x); + } + return m; + }; + const double us = 1000.0 / khz; + const long long t0 = ev(0, true); + std::string line; + double work = 0, wait = 0; + long long prev = t0; + for (int h = 0; h <= pieces; ++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; + line += fmt("%s%s %.1f wait %.1f", h ? " | " : "", h ? fmt("s%d", h).c_str() : "push", + w1, w2); + prev = rel; + } + const long long end = ev(3 + 2 * pieces, 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, + (double)(end - t0) * us, work, wait); + } +} + /* The eight-rank two-shot's weights as the plugin derives them at init (r4d_ar_entry.hip, * ar8_settle): RADIANCE_AR8_SHARD_WEIGHTS unset or "auto" from each rank's slowest PCIe hop, "off" * or anything invalid even, eight integers as given; equal derived weights are the even split. @@ -22564,6 +22642,10 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty } if ((int)devs.size() < W) { std::printf("--perfar8: needs 8 gfx1201 cards, found %zu\n", devs.size()); return; } + int wall_khz_i = 0; + 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. */ @@ -22698,6 +22780,7 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty bad ? fmt("%lld elements differ from the fold", (long long)bad).c_str() : "= fold on every rank"); std::printf(" us by rank:%s\n", per_rank.c_str()); + if (g_timeline && two) ar8_print_timeline(t, cfg, wall_khz); ++cfg; } } @@ -27099,7 +27182,9 @@ static void usage(void) { " 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" - " R4D_SELFTEST_ONE_CARD=1 runs the eight ranks on one card.\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" " --perfmxf8 M [iters] [experts] [top_k] [skew]: the same at the FP8 experts with f32\n" " block scales (moe_gemm_fp8a8_f32scale, GLM's MTP layer).\n" " --perfmhcpre M [iters]: mhc_pre (whole, partials alone, finish alone) and\n" @@ -27272,6 +27357,8 @@ int main(int argc, char** argv) { } else if (!std::strcmp(argv[i], "--cold")) { if (i + 1 >= argc) { usage(); return 2; } g_cold = std::strtoll(argv[++i], nullptr, 0) << 20; + } else if (!std::strcmp(argv[i], "--timeline")) { + g_timeline = true; } else if (!std::strcmp(argv[i], "--perfmoe")) { /* The routed-expert GEMM at a prefill chunk of M tokens, every prefill form. */ if (i + 1 >= argc) { usage(); return 2; }