libr4d: the exact two-shot all-reduce takes every prefill-sized message

R4D_AR_TWOSHOT_WIDE_MAX_ELEMS was 20971520 -- a 4096-token chunk of a 5120-wide residual -- and the
band above it went back to the one-shot, which sends (ws-1)*N bytes a rank where the two-shot sends
2*(ws-1)/ws*N. At the default --max-num-batched-tokens 8192, Gemma 4 26B-A4B's attention reduce is
8192 x 2816 = 23M elements and its feed-forward reduce, which carries both branches for their own
post-norms, 46M: both over the bound, so every full prefill chunk went one-shot at twice the bytes.

--profile-ops on 4x R9700, tp 4, a 20489-token prefill (chunks of 8192, 8192, 4105) fits one link
rate across all of it: the attention reduces 16.8 ms the three calls (two one-shot, one two-shot)
and the feed-forward reduces ~37 ms (all one-shot), 1.6 s of the 2.7 s prefill. The same rate with
every call two-shot is ~9 and ~19 ms: about 0.8 s less a 20k prefill.

The bound guards the kernel's int word offsets and nothing else -- the scratch already holds a
full message per slot (r4d_ar_entry.hip build()), twice what the two-shot uses -- so it is now
2^28 elements, 2^26 words at fp32. The fold is the one-shot's, fp32 in ascending rank order, so
the band edge moves no bits. Fewer bands also means one scratch set less: the old 20971520 band
was its own instance, ~335 MiB a rank at tp 4.

plugin_test: a 16384-token chunk is two-shot to its end, and the one-shot still takes a message
past the bound. r4d_selftest's three-rank pattern walks a 46137344-element message, past the old
ceiling.

Not yet run on the cards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 committed 2026-10-12 14:40:04 -05:00
1 parent 0d95046f6f
commit a378b41abb
5 files changed
+22 -12

No files matched your search

+6 -2
View File
@@ -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,