From 48e41fda1ea78a103f09045a873a08a1ea204b94 Mon Sep 17 00:00:00 2001 From: mici81 Date: Wed, 21 Oct 2026 10:07:00 +0000 Subject: [PATCH] the narrow overlap all-reduce at three and eight ranks only on request: R4D_AR_LANE_3_8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The three- and eight-rank `overlap` rows (ar_twoshot_3rank_exact_lane, ar_twoshot_3rank_ti8_lane, ar_twoshot_8rank_exact_lane) took the four-rank lane rows' narrow launch for every collective a plugin issues beside compute: qwen4exp's --prefill-span, gemma4's prefill trunk overlap and the image and video plugins' second-lane collectives included. That changed a default above two ranks without a measurement, and a narrow grid can lose there too: where the all-reduce is the longer of the two (a large message, slow or shared links, seven peers to feed at eight ranks) the step waits on a slower collective. They now launch exactly as the wide rows by default (tArTs38Lane holds tAr's defaults: the same grid, the same bits, the same time); R4D_AR_LANE_3_8=1, read once at load, gives them the four-rank lane's four blocks of 1024. The bits are the same either way. r4d_selftest checks the defaults both ways (failed before the change: nblocks 4 where the wide rows' rule is 0). GUIDE §4.7 (with what to set and what it may gain or cost), §4.5, the environment table, OPS, TP3 and libr4d/README say it. Co-Authored-By: Claude Opus 5.5 (1M context) --- libr4d/r4d_rows.cpp | 54 +++++++++++++++++++++++++++-------------- libr4d/r4d_selftest.cpp | 41 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 2d4fa47d..7406b572 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4206,8 +4206,8 @@ static const RadConstraint cArTsExact8[] = { C_P2P, }; /* The three- and eight-rank two-shots at `overlap` 1, as cArTsExact4Lane: a pipelined prefill's - * all-reduce beside the other piece's compute. Their narrow launch takes the four-rank lane's - * defaults; neither has been tuned on three or eight cards. */ + * all-reduce beside the other piece's compute. They launch as the wide rows unless R4D_AR_LANE_3_8=1 + * (tArTs38Lane); neither launch has been measured on three or eight cards. */ static const RadConstraint cArTsExact3Lane[] = { C_EQ("world_size", 3), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"), C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1), C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS), @@ -4237,7 +4237,7 @@ static const RadConstraint cArTi8Ts3[] = { C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32), C_P2P, }; -/* The same at `overlap` 1 (tArTs4Lane, untuned at three ranks). */ +/* The same at `overlap` 1 (tArTs38Lane: the wide launch unless R4D_AR_LANE_3_8=1). */ static const RadConstraint cArTi8Ts3Lane[] = { C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32), C_EQ("overlap", 1), C_P2P, @@ -5044,6 +5044,24 @@ static const RadTunable tArTs4Lane[] = { { "acq", kArAcq, NELEM(kArAcq), 0, "acquire fence after the flag read" }, { "ts_min", kArTsMin, NELEM(kArTsMin), 32768, "unused here (tAr says why it is declared)" }, }; + +/* The three- and eight-rank two-shots at `overlap` 1 (cArTsExact3Lane, cArTsExact8Lane, cArTi8Ts3Lane). The + * four-rank lane's narrow launch was measured at four ranks; at three and eight it is not, and a narrow + * grid can also lose there: where the all-reduce is the longer of the two (a large message, slow or shared + * links, seven peers to feed at eight ranks) the step waits on a slower collective. So by default these + * rows launch as the wide rows do (tAr's defaults: the same grid, the same bits, the same time) and the + * narrow launch is a deployment's choice, read once at load: R4D_AR_LANE_3_8=1 gives them the four-rank + * lane's four blocks of 1024. Any other value, or unset, is the default. */ +static int64_t ar_lane38_nblocks() { + const char* e = std::getenv("R4D_AR_LANE_3_8"); + return e && e[0] == '1' && !e[1] ? 4 : 0; +} +static const RadTunable tArTs38Lane[] = { + { "nblocks", kArNblocksLane4, NELEM(kArNblocksLane4), ar_lane38_nblocks(), "blocks in the grid. 0 = scale with the message and clamp (the wide rows' launch, the default); 4 under R4D_AR_LANE_3_8=1" }, + { "nthreads", kArNthreads, NELEM(kArNthreads), 1024, "threads per block" }, + { "drain", kArDrain, NELEM(kArDrain), 3, "store-drain wait before the flag: 3 = s_wait_storecnt, 1 = vmcnt" }, + { "acq", kArAcq, NELEM(kArAcq), 0, "acquire fence after the flag read" }, +}; /* The same axes with narrow defaults, for a collective issued beside compute (`overlap` 1). The * wide launch (up to 24 blocks of 1024 threads spinning on peer flags) suits an all-reduce that has * the card to itself; beside a GEMM it slows the GEMM by 1.45-2.1x. Eight blocks of 256 threads @@ -7112,18 +7130,18 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, -/* The row above at `overlap` 1: the same entry point and the same arithmetic, the four-rank lane's - * narrow launch (tArTs4Lane; not tuned on three cards). */ +/* The row above at `overlap` 1: the same entry point and the same arithmetic; the wide launch unless + * R4D_AR_LANE_3_8=1 gives it the four-rank lane's narrow one (tArTs38Lane; not measured on three cards). */ { "ar_twoshot_3rank_exact_lane", "all_reduce", "ar", - "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch; narrow launch beside " - "compute", + "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, beside compute; the wide " + "launch unless R4D_AR_LANE_3_8=1 makes it narrow", "exactly 3 ranks, all peer-accessible, overlap 1; messages above " R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements; shards as even as the word count allows; " "in place; cudagraph-safe", "bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks " "and to the wide row", RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT + 1, - ROW(cArTsExact3Lane), ROW(tArTs4Lane), nullptr, + ROW(cArTsExact3Lane), ROW(tArTs38Lane), nullptr, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, @@ -7166,18 +7184,18 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_8rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, -/* The row above at `overlap` 1: the same entry point and the same arithmetic, the four-rank lane's - * narrow launch (tArTs4Lane; not tuned on eight cards). */ +/* The row above at `overlap` 1: the same entry point and the same arithmetic; the wide launch unless + * R4D_AR_LANE_3_8=1 gives it the four-rank lane's narrow one (tArTs38Lane; not measured on eight cards). */ { "ar_twoshot_8rank_exact_lane", "all_reduce", "ar", - "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch; narrow launch beside " - "compute", + "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, beside compute; the wide " + "launch unless R4D_AR_LANE_3_8=1 makes it narrow", "exactly 8 ranks, all peer-accessible, overlap 1; messages above " R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements; message must shard evenly over the ranks; " "in place; cudagraph-safe", "bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks " "and to the wide row", RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT + 1, - ROW(cArTsExact8Lane), ROW(tArTs4Lane), nullptr, + ROW(cArTsExact8Lane), ROW(tArTs38Lane), nullptr, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_8rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, @@ -7308,17 +7326,17 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, -/* The row above at `overlap` 1: the same entry point and the same arithmetic, the four-rank lane's - * narrow launch (tArTs4Lane; not tuned on three cards). */ +/* The row above at `overlap` 1: the same entry point and the same arithmetic; the wide launch unless + * R4D_AR_LANE_3_8=1 gives it the four-rank lane's narrow one (tArTs38Lane; not measured on three cards). */ { "ar_twoshot_3rank_ti8_lane", "all_reduce", "ar", - "all-reduce (sum), reduce-scatter + all-gather on the tiered-int8 wire -- lossy; narrow launch " - "beside compute", + "all-reduce (sum), reduce-scatter + all-gather on the tiered-int8 wire -- lossy, beside compute; " + "the wide launch unless R4D_AR_LANE_3_8=1 makes it narrow", "exactly 3 ranks, all peer-accessible, overlap 1; groups of 32, split over the shards as evenly as " "they divide; under min_bytes the exact one-shot; cudagraph-safe", "bf16 / fp16 payload, int8 + fp16 scale + u32 record per group on the wire, fp32 accumulate in " "ascending rank order, bit-identical across ranks and to the wide row", RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT + 1, - ROW(cArTi8Ts3Lane), ROW(tArTs4Lane), nullptr, + ROW(cArTi8Ts3Lane), ROW(tArTs38Lane), nullptr, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 6f02e439..31246a3a 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -400,6 +400,47 @@ static void cross_check(const Plugin& pl) { ok("every row that needs two or more ranks requires device.p2p 1 or 0", no_p2p == 0, p2pdetail); + /* The three- and eight-rank `overlap` rows launch as their wide rows unless R4D_AR_LANE_3_8=1 + * (r4d_rows.cpp, tArTs38Lane): every axis default the wide row's, nblocks 4 under the variable. + * The four-rank lane rows keep their measured narrow launch either way. */ + { + const char* env = std::getenv("R4D_AR_LANE_3_8"); + const bool narrow = env && env[0] == '1' && !env[1]; + auto row = [&](const char* n) -> const RadKernelInfo* { + for (int k = 0; k < nk; ++k) + if (!std::strcmp(pl.kernel_at(k)->name, n)) return pl.kernel_at(k); + return nullptr; + }; + auto deflt = [](const RadKernelInfo* r, const char* key) -> long long { + for (int v = 0; v < r->n_tunables; ++v) + if (!std::strcmp(r->tunables[v].key, key)) return (long long)r->tunables[v].deflt; + return -1; + }; + static const char* const pairs[][2] = { + { "ar_twoshot_3rank_exact_lane", "ar_twoshot_3rank_exact" }, + { "ar_twoshot_8rank_exact_lane", "ar_twoshot_8rank_exact" }, + { "ar_twoshot_3rank_ti8_lane", "ar_twoshot_3rank_ti8" }, + }; + int bad = 0; + std::string detail; + for (const auto& pr : pairs) { + const RadKernelInfo* l = row(pr[0]); + const RadKernelInfo* w = row(pr[1]); + if (!l || !w) { ++bad; detail += std::string(pr[0]) + " or its wide row absent; "; continue; } + for (const char* key : { "nblocks", "nthreads", "drain", "acq" }) { + const long long want = narrow && !std::strcmp(key, "nblocks") ? 4 : deflt(w, key); + if (deflt(l, key) != want) { + ++bad; + detail += std::string(pr[0]) + ":" + key + " " + std::to_string(deflt(l, key)) + + " (want " + std::to_string(want) + "); "; + } + } + } + ok(narrow ? "the 3/8-rank overlap rows take the narrow launch (R4D_AR_LANE_3_8=1)" + : "the 3/8-rank overlap rows launch as their wide rows (R4D_AR_LANE_3_8 unset)", + bad == 0, detail); + } + /* The libr4d orderings the integer priority has to preserve. */ int p_ts = -1, p_os = -1, p_dec = -1, p_pre = -1, p_m16 = -1, p_m64 = -1, p_bt = -1; int p_g2dec = -1, p_g2pre = -1, p_g2win = -1;