Archived
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>
90 lines
5.7 KiB
C
90 lines
5.7 KiB
C
#pragma once
|
|
// r4d_ar_seq.h: the first step of every all-reduce and all-gather kernel (its handshake sequence
|
|
// number and scratch slot) and the wrap-safe wait after the push. Shared by the 2-rank and wide
|
|
// families: kernels sharing an instance's buffers must take the step identically or the ranks
|
|
// disagree.
|
|
//
|
|
// Counter allocation: 2 * nbw words, zeroed once (nbw = block capacity the flag array was sized
|
|
// for), then the landed counter (r4d_ar_link_landed):
|
|
//
|
|
// [0, nbw) Handshake counters, one per block. Block b advances word b once per launch it
|
|
// runs in, publishes it to the peer and waits for the peer's. Per block so a
|
|
// block idle through narrow launches resumes one step past its peer's last flag;
|
|
// the wrap-safe compare needs that gap far under 2^31.
|
|
//
|
|
// [nbw, 2*nbw) Parity counters. Every launch advances all nbw of them once, whatever its block
|
|
// count (block b takes words b, b + nb, b + 2*nb, ...), so each equals the launch
|
|
// count and a block reads the slot parity off its own word.
|
|
//
|
|
// [2*nbw] Landed counter: blocks of the running launch that have seen their peer's push
|
|
// land; back at zero when the launch ends.
|
|
//
|
|
// The slot cannot come from the handshake counter. Consecutive launches on one instance must use
|
|
// opposite scratch slots, since launch k+1 may push while the peer still reduces launch k. Block
|
|
// counts and per-block byte ranges vary by launch, so handshake counters drift apart per block,
|
|
// and a block of launch k+1 could push into the slot a different block of launch k is still
|
|
// reading. The parity counters move in lockstep, so the slot alternates per launch for every block.
|
|
//
|
|
// Cost: one more returning atomic, issued from a second wave so its round trip overlaps the
|
|
// handshake's, plus nbw / nb fire-and-forget atomics over the other threads. Those only need to
|
|
// land before the next launch, which the kernel boundary guarantees. All state is device-resident,
|
|
// so a captured graph replays.
|
|
#include <hip/hip_runtime.h>
|
|
|
|
// Sets `s_seq` to this block's handshake value and `s_slot` to the launch's scratch slot (0 or 1),
|
|
// then barriers so every thread can read both. `nbw` is the capacity the counters were allocated
|
|
// for, and gridDim.x must not exceed it. `steps` handshake values are taken at once (s_seq is the
|
|
// first): a kernel that handshakes more than twice per launch reserves the flag values it
|
|
// publishes, so every kernel on the instance stays in the same monotone flag space. All ranks must
|
|
// take the same number for the same block.
|
|
//
|
|
// The barrier fences LDS only, since it publishes just the two LDS words. A full __syncthreads
|
|
// would wait for the outstanding parity atomics and invalidate the vector caches, putting that
|
|
// traffic on the path to the first push.
|
|
__device__ __forceinline__ void r4d_ar_seq_take(unsigned int* __restrict__ ctrs, unsigned int nbw,
|
|
unsigned int& s_seq, unsigned int& s_slot,
|
|
unsigned int steps = 1u) {
|
|
const unsigned int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
|
unsigned int* const par = ctrs + nbw;
|
|
const unsigned int pt = nt > 32u ? 32u : 0u; // first lane of the second wave, if there is one
|
|
if (tid == 0) s_seq = atomicAdd(&ctrs[b], steps) + 1u;
|
|
if (tid == pt) s_slot = (atomicAdd(&par[b], 1u) + 1u) & 1u;
|
|
for (unsigned int j = b + (tid + 1u) * nb; j < nbw; j += nt * nb) atomicAdd(&par[j], 1u);
|
|
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup", "local");
|
|
__builtin_amdgcn_s_barrier();
|
|
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "workgroup", "local");
|
|
}
|
|
|
|
// The engine's hold on the link, released by the launch (rad_abi.h, rad_kernel_collective
|
|
// returning 2). The word is odd exactly while the engine holds the link for this launch and
|
|
// nothing else writes it meanwhile, so each block's first thread reads it once at the start
|
|
// (r4d_ar_link_word), where the host-memory read overlaps the push instead of delaying the reduce.
|
|
//
|
|
// Every block calls r4d_ar_link_landed once its handshake has seen the peer's push land. The last
|
|
// block knows all of the peer's bytes have arrived, so the link is free for the rest of the
|
|
// kernel; it decrements the word if odd, atomically so it commutes with any barrier counting the
|
|
// same word down. `ctr` counts blocks and returns to zero at the end of the launch. The DMA engine
|
|
// polls the word past every cache, so it is accessed at system scope.
|
|
__device__ __forceinline__ long long r4d_ar_link_word(const long long* gate, unsigned int tid) {
|
|
return gate && tid == 0 ? __hip_atomic_load(gate, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM)
|
|
: 0;
|
|
}
|
|
|
|
__device__ __forceinline__ void r4d_ar_link_landed(long long* gate, long long word,
|
|
unsigned int* ctr, unsigned int nb,
|
|
unsigned int tid) {
|
|
if (!gate || tid != 0) return;
|
|
const unsigned int prev = __hip_atomic_fetch_add(ctr, 1u, __ATOMIC_RELAXED,
|
|
__HIP_MEMORY_SCOPE_AGENT);
|
|
if (prev + 1u != nb) return;
|
|
__hip_atomic_store(ctr, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
|
|
if (word & 1) __hip_atomic_fetch_add(gate, -1LL, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM);
|
|
}
|
|
|
|
// True while `flag` has not reached `want`. The u32 counters wrap, so compare the signed
|
|
// difference: a peer at or past `want` (possibly a launch ahead) reads as arrived across the wrap,
|
|
// where a plain `flag < want` would stop waiting once `want` wraps to a small number.
|
|
__device__ __forceinline__ bool r4d_ar_seq_behind(unsigned int flag, unsigned int want) {
|
|
return (int)(flag - want) < 0;
|
|
}
|