libr4d: at eight ranks the exact one-shot ends at 8192 elements, the two-shot at 8 blocks

The exact all-reduce at eight ranks shared the three- and four-rank band: one-shot up to 524288
elements. The one-shot sends (ws-1)*N bytes a rank, seven times the message at eight ranks, where
the two-shot sends 2*(ws-1)/ws*N, and on eight R9700 (PCIe P2P, bf16, warm) the two-shot is the
faster from 16384 elements on. A decode step of eight streams with a draft window of six on
GLM-5.3-Flash (4096 wide, an out-of-tree plugin) reduces 48 x 4096 = 196608 elements: 232 us
one-shot, 68 us two-shot, where all_reduce was 20.7% of the step's device time.

R4D_AR_8RANK_ONESHOT_MAX_ELEMS is 8192, a multiple of 64, the eight-rank two-shot's DIV;
cArTsExact8 starts one element above it. The two-shot's block rule stops at 8 blocks at eight
ranks: every block handshakes with seven peers, and past eight blocks the extra flag rounds cost
more than the reduce bandwidth they add (4 blocks up to 393216 elements, 5 at 524288, 8 instead of
16 from 2097152). Both rows fold in fp32 in ascending rank order, so neither change moves a bit.

  numel      before                    after
  4096       one-shot         22.5 us  one-shot nb 4    12.8 us
  16384      one-shot         39.7     two-shot nb 4    21.5
  131072     one-shot        176.3     two-shot nb 4    52.3
  196608     one-shot nb 16  233.2     two-shot nb 4    68.7
  524288     one-shot        573.7     two-shot nb 5   177.4
  2097152    two-shot nb 16  615.8     two-shot nb 8   610.5
  8388608    two-shot nb 16   2460     two-shot nb 8    2489
  16777216   two-shot nb 16   4999     two-shot nb 8    4824

At 4096 the rule already chose 4 blocks; the 22.5 us was the microbenchmark's, ten launches after
the host-side check, which now warms up with as many launches as it times.

Band edges are the union over every candidate row (core/plugin/bands.cpp), so the edge at 8192
splits the two-, three- and four-rank bands too. Both halves resolve the same row, kernel and
bits, at the cost of one more instance per (width, dtype) whose slot holds 8192 elements. Keeping
the edge to eight ranks would need the band builder to skip rows whose fixed constraints the
geometry rules out (world_size 8 here), a change to every op's bands for one row; plugin_test
states the new edges instead.

r4d_selftest runs the eight-rank rows (ar_oneshot_8rank_exact, ar_twoshot_8rank_exact,
ar_gather_8rank) through the width-parametrised cases of the three- and four-rank ones instead of
skipping them: the pattern's band edge is at 8192 and 8256 there, and a 9-word message must be
refused. --perfar8 numel [iters] [dtype] times both rows, one thread a card, at the block rule
and at 4 to 64 blocks, each checked bit for bit against the fold on every rank. On the cards:
ar_oneshot_8rank_exact 20 ok, ar_twoshot_8rank_exact 16 ok, ar_gather_8rank 5 ok.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-19 10:38:33 +02:00
1 parent 8015d9a639
commit 0eb3d96ce1
6 files changed
+400 -65

No files matched your search

+33 -20
View File
@@ -843,11 +843,14 @@ TEST(an_engine_with_no_card_leaves_a_gpu_librarys_device_rows_out) {
for (const KernelRow& row : r.rows()) CHECK(row.info->domain != RAD_DOMAIN_DEVICE);
}
/* The shipped libr4d picks the all-reduce by message size. At 3, 4 and 8 ranks the exact sum is a
* one-shot for decode-sized messages and a two-shot for prefill chunks; the two-shot rows start one
* element above the one-shot bound, which is a band edge of the ranged `numel`. The band under it
* must stay resolvable for every row with a DIV on `numel` (two-rank wht6 (64), three- and
* four-rank ti8 (32, 128)), or a lossy serve loses its decode band. Only the real plugin can show
/* The shipped libr4d picks the all-reduce by message size. At 3 and 4 ranks the exact sum is a
* one-shot for decode-sized messages and a two-shot for prefill chunks, at 8 ranks a one-shot only
* up to 8192 elements (its seven-fold send loses early); the two-shot rows start one element above
* the one-shot bound, which is a band edge of the ranged `numel`. Edges are the union over every
* candidate row, so the eight-rank edge at 8192 also splits the other widths' bands, each half
* resolving the same row. The band under the one-shot bound must stay resolvable for every row
* with a DIV on `numel` (two-rank wht6 (64), three- and four-rank ti8 (32, 128)), or a lossy
* serve loses its decode band. Only the real plugin can show
* resolution above two ranks on a two-card machine. Geometry: Qwen3.8-Flash-Next, 2560 wide,
* 2048-token chunk. */
TEST(libr4d_resolves_the_all_reduce_by_message_size) {
@@ -868,35 +871,44 @@ TEST(libr4d_resolves_the_all_reduce_by_message_size) {
};
CHECK_EQ(band_names(bands(3, 1, chunk)),
"524288:ar_oneshot_3rank_exact 5242880:ar_twoshot_3rank_exact ");
"8192:ar_oneshot_3rank_exact 524288:ar_oneshot_3rank_exact "
"5242880:ar_twoshot_3rank_exact ");
CHECK_EQ(band_names(bands(4, 1, chunk)),
"524288:ar_oneshot_4rank_exact 5242880:ar_twoshot_4rank_exact ");
"8192:ar_oneshot_4rank_exact 524288:ar_oneshot_4rank_exact "
"5242880:ar_twoshot_4rank_exact ");
CHECK_EQ(band_names(bands(8, 1, chunk)),
"524288:ar_oneshot_8rank_exact 5242880:ar_twoshot_8rank_exact ");
"8192:ar_oneshot_8rank_exact 524288:ar_twoshot_8rank_exact "
"5242880:ar_twoshot_8rank_exact ");
CHECK_EQ(band_names(bands(3, 0, chunk)),
"524288:ar_twoshot_3rank_ti8 5242880:ar_twoshot_3rank_ti8 ");
"8192:ar_twoshot_3rank_ti8 524288:ar_twoshot_3rank_ti8 5242880:ar_twoshot_3rank_ti8 ");
CHECK_EQ(band_names(bands(4, 0, chunk)),
"524288:ar_twoshot_4rank_ti8 5242880:ar_twoshot_4rank_ti8 ");
"8192:ar_twoshot_4rank_ti8 524288:ar_twoshot_4rank_ti8 5242880:ar_twoshot_4rank_ti8 ");
CHECK_EQ(band_names(bands(2, 0, chunk)),
"524288:ar_oneshot_2rank_wht6 5242880:ar_oneshot_2rank_wht6 ");
"8192:ar_oneshot_2rank_wht6 524288:ar_oneshot_2rank_wht6 "
"5242880:ar_oneshot_2rank_wht6 ");
CHECK_EQ(band_names(bands(2, 1, chunk)),
"524288:ar_oneshot_2rank_exact 5242880:ar_oneshot_2rank_exact ");
"8192:ar_oneshot_2rank_exact 524288:ar_oneshot_2rank_exact "
"5242880:ar_oneshot_2rank_exact ");
/* A 16384-token chunk is two-shot to its end: the two-shot's bound guards its word offsets,
* it is not a prefill size, and a chunk past 20971520 elements would otherwise fall back to
* the one-shot's twice-as-heavy send. */
CHECK_EQ(band_names(bands(4, 1, 16384)),
"524288:ar_oneshot_4rank_exact 41943040:ar_twoshot_4rank_exact ");
"8192:ar_oneshot_4rank_exact 524288:ar_oneshot_4rank_exact "
"41943040:ar_twoshot_4rank_exact ");
CHECK_EQ(band_names(bands(3, 1, 16384)),
"524288:ar_oneshot_3rank_exact 41943040:ar_twoshot_3rank_exact ");
"8192:ar_oneshot_3rank_exact 524288:ar_oneshot_3rank_exact "
"41943040:ar_twoshot_3rank_exact ");
/* Above the two-shot's bound the one-shot takes the message back. */
CHECK_EQ(band_names(bands(4, 1, 131072)),
"524288:ar_oneshot_4rank_exact 268435456:ar_twoshot_4rank_exact "
"335544320:ar_oneshot_4rank_exact ");
"8192:ar_oneshot_4rank_exact 524288:ar_oneshot_4rank_exact "
"268435456:ar_twoshot_4rank_exact 335544320:ar_oneshot_4rank_exact ");
/* A step that never exceeds the bound has one band, and it is the one-shot's. */
CHECK_EQ(band_names(bands(4, 1, 128)), "327680:ar_oneshot_4rank_exact ");
/* A step that never exceeds the bound stays on the one-shot, split only at the eight-rank
* edge. */
CHECK_EQ(band_names(bands(4, 1, 128)),
"8192:ar_oneshot_4rank_exact 327680:ar_oneshot_4rank_exact ");
/* Eight ranks have no lossy wire: every band is a miss that names the request. */
const std::vector<Band> none = bands(8, 0, chunk);
@@ -912,9 +924,10 @@ TEST(libr4d_resolves_the_all_reduce_by_message_size) {
for (const Band& b : bands(3, 0, chunk)) CHECK_EQ(b.dom[RAD_DOMAIN_DEVICE].ranged_div, 32ll);
for (const Band& b : bands(4, 0, chunk)) CHECK_EQ(b.dom[RAD_DOMAIN_DEVICE].ranged_div, 128ll);
const std::vector<Band> exact4 = bands(4, 1, chunk);
REQUIRE_EQ(exact4.size(), 2u);
REQUIRE_EQ(exact4.size(), 3u);
CHECK_EQ(exact4[0].dom[RAD_DOMAIN_DEVICE].ranged_div, 0ll);
CHECK_EQ(exact4[1].dom[RAD_DOMAIN_DEVICE].ranged_div, 64ll);
CHECK_EQ(exact4[1].dom[RAD_DOMAIN_DEVICE].ranged_div, 0ll);
CHECK_EQ(exact4[2].dom[RAD_DOMAIN_DEVICE].ranged_div, 64ll);
for (const Band& b : bands(3, 1, chunk)) CHECK_EQ(b.dom[RAD_DOMAIN_DEVICE].ranged_div, 0ll);
}