This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libr4d/r4d_ar_shards.h
T
NunzeandClaude Opus 5.5 adc7f37a70 libr4d: the eight-rank two-shot in pieces, both hops on the link at once, by message size
Each block splits its words (in shard space) into K runs. Step j pushes run j+1's first hop and
reduces and pushes run j's second, then handshakes, so a rank with a short shard sends and
receives at once instead of one direction a hop. A launch takes K+1 handshakes instead of two.
The flag a (block, source) publishes is a monotone counter, so one handshake vouches for every
earlier one, and r4d_ar_seq_take reserves the K steps at once (`steps`, 1 for every other kernel):
one-shots and gathers on the same instance stay in the family's flag space. The regions stay at 0
and the longest shard, so the scratch is as before; every word is touched by the same thread in
the same three steps, so the fold, the bits and the in-place form are unchanged. The launcher
clamps K to the words a block holds, and K 1 is its own instantiation without the loop.

  16 MiB, bf16, warm   K 1      K 2    K 4    K 8
  even                 2841 us  2806   2667   2470
  4,1,0,4,0,1,4,4      2730     2126   1798   1636
  4,2,1,4,1,2,4,4      2495     2121   1940   1922

4,1,0,4,0,1,4,4 in 8 pieces is 42% under the even split. Fitting measured = f x computed + h x
(K+1) at K 4 and 8 gives f = 1.21 and h about 5 us a handshake: what separates 1636 us from the
1.17 ms the Gen4 links need is their efficiency, not the handshakes.

A decode-sized message loses with either:

  numel      even K 1   4,1,0,4,0,1,4,4: K 1 / K 2 / K 4 / K 8
  196608     73 us      85 / 76 / 81 / 113
  524288     214        207 / 166 / 169 / 198
  2097152    699        711 / 581 / 531 / 527
  8388608    2841       2730 / 2126 / 1798 / 1636

so below RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS and RADIANCE_AR8_PIECES_MIN_ELEMS (262144 elements
each) the message runs even and in one piece, on the kernel as it was. Above them, without
RADIANCE_AR8_PIECES, K follows the size (r4d_ar8_auto_pieces): 2 under 1048576 elements, 4 under
4194304, 8 above, where the crossings above lie. RADIANCE_AR8_PIECES (1 to 16) fixes K over the
floor, and a launch's `pieces` parameter (the selftest's) overrides that. Weights and K are a
function of the message size and the instance (ar8_weights_for, ar8_pieces_for), so a replay of
the same size decides the same; the slot is sized for weights only when the largest message is
over the weight floor. Every rank compares both variables and both floors at init.

r4d_selftest, with both floors at 0: the weighted and even sets at K 1, 2 and 4 (1- and 9-word
messages cross the launcher's clamp), and one instance whose K changes from launch to launch (1,
4, 2, 8, 16, 3, 1), which a wrong flag reservation hangs or races. At the default floors: the size
rule over 131072 to 4194304 elements, K 4 on both sides of the piece floor, a 131073-word message
over the weight floor (served only weighted) and a 9-word one under it (refused, and served at a
floor of 64). 192 of 192 on the cards. --perfar8 names the floors and times the two-shot as the
server runs it and at K 1, 2, 4 and 8.

GLM-5.3-Flash at TP8 (an out-of-tree plugin) on these eight cards with 4,1,0,4,0,1,4,4, against
the same build without in the same hour: a 20K-token prefill 3450 tok/s against 2945, 8K 3372
against 2920; eight agent streams 457 tok/s against 458, one 141 against 139. K 8 at every size
instead gave the same prefill (3461) and 436 at eight streams, 113 at one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 10:38:33 +02:00

112 lines
4.1 KiB
C

#pragma once
// r4d_ar_shards.h: the exact two-shot's shard table. Shard s is the 16-byte words
// [off[s], off[s+1]) and is reduced by rank s. Host only and HIP-free: the launcher, the plugin
// shim (which sizes scratch and picks the block count from it) and the selftest (which prints it)
// must derive the same table from the same inputs.
//
// The even split is n16 / ws words a shard with the first n16 % ws shards one word longer. Weights
// split proportionally (floor(n16 * w_s / sum)) and hand the few remaining words, one each, to the
// weighted ranks in ascending order; equal weights therefore reproduce the even split exactly, and
// a zero weight always means an empty shard. Which rank owns a word changes only the link it
// crosses, not its fold (fp32, ascending rank), so every table gives the same bits.
#include <cstdlib>
#define R4D_AR8_SHARD_WEIGHTS_ENV "RADIANCE_AR8_SHARD_WEIGHTS"
#define R4D_AR8_PIECES_ENV "RADIANCE_AR8_PIECES"
// Below these message sizes (elements) the weights and the pieces do not apply: a decode-sized
// message pays more for the extra handshakes and the uneven split than its link time saves
// (measured on the eight R9700: 196608 elements run fastest even and in one piece, 524288 already
// gain from weights and two pieces).
#define R4D_AR8_SHARD_WEIGHTS_MIN_ENV "RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS"
#define R4D_AR8_PIECES_MIN_ENV "RADIANCE_AR8_PIECES_MIN_ELEMS"
enum {
R4D_AR_SHARD_WEIGHT_MAX = 65535,
R4D_AR8_MIN_ELEMS_DEFAULT = 262144,
R4D_AR8_MIN_ELEMS_MAX = 268435456, // the two-shot band's top (r4d.h)
};
// The piece count when RADIANCE_AR8_PIECES is unset, by message size in elements: more pieces
// shorten the one-way first and last steps ((K+1)/K of the link time) but add a handshake each,
// which only a large message amortises (measured at 4,1,0,4,0,1,4,4: K2 best at 524288, K4 at
// 2097152, K8 at 8388608).
inline int r4d_ar8_auto_pieces(long long numel) {
if (numel < 262144) return 1;
if (numel < 1048576) return 2;
if (numel < 4194304) return 4;
return 8;
}
// A non-negative decimal integer of at most `max`, nothing else.
inline bool r4d_ar_parse_count(const char* s, long long max, long long* out) {
if (!s || *s < '0' || *s > '9') return false;
long long v = 0;
while (*s >= '0' && *s <= '9') {
v = v * 10 + (*s++ - '0');
if (v > max) return false;
}
if (*s) return false;
*out = v;
return true;
}
// `ws` non-negative integers separated by commas, each at most R4D_AR_SHARD_WEIGHT_MAX, summing
// to more than zero; nothing else (no blanks, signs or trailing text).
inline bool r4d_ar_shard_weights_parse(const char* s, int ws, int* w) {
if (!s) return false;
long sum = 0;
for (int i = 0; i < ws; ++i) {
if (*s < '0' || *s > '9') return false;
long v = 0;
while (*s >= '0' && *s <= '9') {
v = v * 10 + (*s++ - '0');
if (v > R4D_AR_SHARD_WEIGHT_MAX) return false;
}
w[i] = (int)v;
sum += v;
if (i + 1 < ws) {
if (*s != ',') return false;
++s;
}
}
return *s == '\0' && sum > 0;
}
// Fills off[0..ws] and returns the longest shard. `w` null: the even split.
inline long r4d_ar_shard_table(long n16, int ws, const int* w, long* off) {
long len[16];
if (!w) {
const long base = n16 / ws, rem = n16 % ws;
for (int s = 0; s < ws; ++s) len[s] = base + (s < rem ? 1 : 0);
} else {
long sum = 0;
for (int s = 0; s < ws; ++s) sum += w[s];
long left = n16;
for (int s = 0; s < ws; ++s) {
len[s] = n16 * w[s] / sum;
left -= len[s];
}
for (int s = 0; s < ws && left > 0; ++s)
if (w[s] > 0) { ++len[s]; --left; }
}
long sh_max = 0;
off[0] = 0;
for (int s = 0; s < ws; ++s) {
off[s + 1] = off[s] + len[s];
if (len[s] > sh_max) sh_max = len[s];
}
return sh_max;
}
// The longest shard any message of at most n16 words can get under `w` (null: even): what the
// scratch slot must hold twice.
inline long r4d_ar_shard_max_bound(long n16, int ws, const int* w) {
if (!w) return (n16 + ws - 1) / ws;
long sum = 0, wmax = 0;
for (int s = 0; s < ws; ++s) {
sum += w[s];
if (w[s] > wmax) wmax = w[s];
}
const long b = n16 * wmax / sum + 1;
return b < n16 ? b : n16;
}