diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 72ef2813..12cfdd27 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -418,8 +418,12 @@ void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers); // Shards are as even as the word count allows (the first n16 % ws take one more word), so a // message need not divide by the rank count -- at three it almost never does. Regions A and B sit // at 0 and the longest shard, so the slot stride must hold twice that. Above this bound the caller -// falls back: -enum { R4D_AR_TWOSHOT_WIDE_MAX_ELEMS = 20971520 }; // a 4096-token hidden-5120 prefill chunk +// falls back to the one-shot. The bound guards the kernel's int word offsets (the message, the +// slot stride) and nothing else: 2^28 elements is 2^26 words at fp32. It was 20971520 -- a +// 4096-token chunk of a 5120-wide residual -- which handed every larger prefill chunk back to the +// one-shot and its (ws-1)*N send: an 8192-token chunk at Gemma 4's 2816 is 23M elements at the +// attention's reduce and 46M at the feed-forward's, which carries both branches. +enum { R4D_AR_TWOSHOT_WIDE_MAX_ELEMS = 268435456 }; void r4d_ar_twoshot_3rank_exact(long peer_scratch0, long peer_scratch1, long peer_flags0, long peer_flags1, long my_scratch, long my_flags, long seq_ctrs, long slot_stride16, long inp, long out, long n_elem, long dtype, diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 2e52c93c..991953a9 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -38,7 +38,7 @@ #include #include -static_assert(R4D_AR_TWOSHOT_WIDE_MAX_ELEMS == 20971520, +static_assert(R4D_AR_TWOSHOT_WIDE_MAX_ELEMS == 268435456, "r4d_rows.cpp restates this bound; libr4d changed it"); static_assert(R4D_MAX_RANKS == 8, "the 8-rank entry point names exactly seven peers"); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index eca5fc73..6e3a5094 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -71,7 +71,7 @@ /* libr4d's R4D_AR_TWOSHOT_WIDE_MAX_ELEMS, restated because this file must not include r4d.h. * r4d_ar_entry.hip static_asserts the two against each other. */ -#define R4D_AR_TWOSHOT_WIDE_MAX_ELEMS 20971520 +#define R4D_AR_TWOSHOT_WIDE_MAX_ELEMS 268435456 /* The largest message, in elements, a 4- or 8-rank exact all-reduce keeps on the one-shot; above it * the two-shot rows take it. 1 MiB of a 16-bit payload. The one-shot sends (ws-1)*N bytes a rank diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 506567ba..f7ad8902 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -11241,7 +11241,7 @@ static void ar_three_rank(const Plugin& pl, const std::vector& devs, const * sums correctly. This one gives every element its own value, so a word reduced by the wrong * owner, gathered from the wrong slot or dropped at a short shard's end is a wrong number at a * known index. The sizes walk the uneven split: words % 3 of 0, 1 and 2, from a 16-word message - * to the row's 20971520-element ceiling, in place (how every block issues it) and once out of + * to past the old 20971520 ceiling, in place (how every block issues it) and once out of * place, back to back on ONE instance so the slot parity and the changing block count are * exercised the way a step's mixed messages exercise them. The oracle is the fold the kernel * claims -- fp32 in ascending rank order, rounded once -- so it is bit for bit, and so it is also @@ -11320,8 +11320,9 @@ static void ar_three_rank_pattern(const Plugin& pl, const std::vector& devs struct Run { const char* dtype; std::vector sizes; int oop; }; const std::vector runs = { /* bf16, the residual: words = numel / 8. 2048 x 2048 rows is words % 3 == 2, x 2049 is - * 0, x 2050 is 1; 128 is 16 words (1), 24008 is 3001 (1); 20971520 the ceiling (1). */ - { "bf16", { 4194304, 128, 4196352, 24008, 4198400, 20971520, 4194304 }, 2 }, + * 0, x 2050 is 1; 128 is 16 words (1), 24008 is 3001 (1); 20971520 the old ceiling (1); + * 46137344, Gemma 4's feed-forward reduce at an 8192-token chunk, past it (1). */ + { "bf16", { 4194304, 128, 4196352, 24008, 4198400, 20971520, 46137344, 4194304 }, 2 }, /* fp32: words = numel / 4. 8196 -> 2049 (0), 4100 -> 1025 (2), 1048580 -> 262145 (2). */ { "fp32", { 8196, 4100, 1048580 }, 0 }, }; diff --git a/tests/plugin_test.cpp b/tests/plugin_test.cpp index ebc1d4d9..d376d606 100644 --- a/tests/plugin_test.cpp +++ b/tests/plugin_test.cpp @@ -901,13 +901,18 @@ TEST(libr4d_resolves_the_all_reduce_by_message_size) { CHECK_EQ(names(bands(2, 1, chunk)), "524288:ar_oneshot_2rank_exact 5242880:ar_oneshot_2rank_exact "); - /* Above the two-shot's cap the one-shot takes the message back. */ + /* A 16384-token chunk is two-shot to its end: the two-shot's bound is a guard on its word + * offsets, not a prefill size, and a chunk past the old 20971520 went back to the one-shot's + * twice-as-heavy send. */ CHECK_EQ(names(bands(4, 1, 16384)), - "524288:ar_oneshot_4rank_exact 20971520:ar_twoshot_4rank_exact " - "41943040:ar_oneshot_4rank_exact "); + "524288:ar_oneshot_4rank_exact 41943040:ar_twoshot_4rank_exact "); CHECK_EQ(names(bands(3, 1, 16384)), - "524288:ar_oneshot_3rank_exact 20971520:ar_twoshot_3rank_exact " - "41943040: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(names(bands(4, 1, 131072)), + "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(names(bands(4, 1, 128)), "327680:ar_oneshot_4rank_exact ");