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>
119 lines
5.7 KiB
C++
119 lines
5.7 KiB
C++
#pragma once
|
|
// r4d_ar_wide.h: shared transport of the wide (ws>2) all-reduce family. Every wide kernel
|
|
// (one-shot exact, two-shot exact, two-shot ti8) publishes in the 2*seq flag space (the two-shots
|
|
// add the 2s-1 half-step), advances seq_ctrs[b] once per AR (a two-shot in pieces by its piece
|
|
// count, reserving the extra flag values it publishes), and lays scratch out as 2*ws slots of
|
|
// one stride (parity x source rank). The parity is the launch's, from the parity counters in
|
|
// r4d_ar_seq.h, so seq_ctrs is 2 * R4D_AR_WIDE_MAX_BLOCKS words. The constants sizing the shared
|
|
// flag/seq/scratch set are defined only here so they cannot drift apart.
|
|
//
|
|
// The 2-rank pair is not here: it has its own flag layout (one word per block, no ws stride), an
|
|
// acquire-on-load spin and its own block ceiling.
|
|
#include <hip/hip_runtime.h>
|
|
#include <hip/hip_fp16.h>
|
|
#include <hip/hip_bf16.h>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include "r4d_ar_seq.h"
|
|
|
|
#define R4D_AR_SPIN_MAX 4000000000ULL
|
|
#define R4D_AR_WIDE_MAX_BLOCKS 64
|
|
#define R4D_AR_NB_DESIGN 16
|
|
#define R4D_AR_WIDE_MAX_PEERS 7
|
|
|
|
// Ascending peer rank throughout; rank[] maps a spin lane to its global source rank. The
|
|
// scratch word type is the one per-family difference (int4 exact wire, char ti8 wire).
|
|
template <typename S, int NPEERS>
|
|
struct R4DArWidePeers {
|
|
S* scratch[NPEERS];
|
|
unsigned int* flags[NPEERS];
|
|
int rank[NPEERS];
|
|
};
|
|
|
|
__device__ __forceinline__ void r4d_ar_store_sys_rel(unsigned int* p, unsigned int v) {
|
|
__hip_atomic_store(p, v, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_SYSTEM);
|
|
}
|
|
__device__ __forceinline__ void r4d_ar_store_sys_rlx(unsigned int* p, unsigned int v) {
|
|
__hip_atomic_store(p, v, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM);
|
|
}
|
|
__device__ __forceinline__ unsigned int r4d_ar_load_sys_rlx(const unsigned int* p) {
|
|
return __hip_atomic_load(p, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM);
|
|
}
|
|
__device__ __forceinline__ float r4d_ar_to_f(const float& x) { return x; }
|
|
__device__ __forceinline__ float r4d_ar_to_f(const __half& x) { return __half2float(x); }
|
|
__device__ __forceinline__ float r4d_ar_to_f(const __hip_bfloat16& x) { return (float)x; }
|
|
__device__ __forceinline__ void r4d_ar_from_f(float v, float& o) { o = v; }
|
|
__device__ __forceinline__ void r4d_ar_from_f(float v, __half& o) { o = __float2half(v); }
|
|
__device__ __forceinline__ void r4d_ar_from_f(float v, __hip_bfloat16& o) { o = (__hip_bfloat16)v; }
|
|
|
|
// drain: 0 none, 1 all-thread threadfence_system, 2 single-thread (applied by the publisher
|
|
// inside the handshake), 3 s_wait_storecnt 0.
|
|
__device__ __forceinline__ void r4d_ar_drain(int drain) {
|
|
if (drain == 1) __threadfence_system();
|
|
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
|
|
}
|
|
// acq: 0 acquire-only fence (global_inv, no writeback half), 1 full threadfence_system,
|
|
// 2 none -- fine-grained scratch only.
|
|
__device__ __forceinline__ void r4d_ar_acquire(int acq) {
|
|
if (acq == 1) __threadfence_system();
|
|
else if (acq != 2) __builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "");
|
|
}
|
|
|
|
// Publish `val` to every peer's flag word [b][rank], then wait until each of this block's own
|
|
// words has reached or passed it (peers may be a step ahead; counters only grow). One spin lane
|
|
// per source, wrap-safe monotonic compare, never equality. Every rank publishes before it waits,
|
|
// so there is no cycle; a handshake that cannot complete traps.
|
|
template <int WS, typename S, int NPEERS>
|
|
__device__ __forceinline__ void r4d_ar_handshake(const R4DArWidePeers<S, NPEERS>& peers,
|
|
unsigned int* __restrict__ my_flags, int b,
|
|
int rank, unsigned int val, int drain, int acq,
|
|
int pub) {
|
|
r4d_ar_drain(drain);
|
|
__syncthreads();
|
|
const int tid = threadIdx.x;
|
|
if (tid == 0) {
|
|
if (drain == 2) __threadfence_system();
|
|
if (pub) {
|
|
// One system writeback for the whole publish, then relaxed stores. Per-peer release stores
|
|
// would add writebacks that order nothing, since the flags go to different peers.
|
|
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "");
|
|
#pragma unroll
|
|
for (int p = 0; p < NPEERS; ++p)
|
|
r4d_ar_store_sys_rlx(&peers.flags[p][(size_t)b * WS + rank], val);
|
|
} else {
|
|
#pragma unroll
|
|
for (int p = 0; p < NPEERS; ++p)
|
|
r4d_ar_store_sys_rel(&peers.flags[p][(size_t)b * WS + rank], val);
|
|
}
|
|
}
|
|
if (tid < NPEERS) {
|
|
const unsigned int* f = &my_flags[(size_t)b * WS + peers.rank[tid]];
|
|
unsigned long long z = 0;
|
|
while ((int)(r4d_ar_load_sys_rlx(f) - val) < 0) {
|
|
__builtin_amdgcn_s_sleep(2);
|
|
if (++z > R4D_AR_SPIN_MAX) __builtin_trap(); // lockstep violation
|
|
}
|
|
}
|
|
__syncthreads();
|
|
r4d_ar_acquire(acq);
|
|
}
|
|
|
|
// Host side: checks stride and grid against the real allocations. Every scratch, flag and seq
|
|
// pointer comes from hipMalloc / hipIpcOpenMemHandle, so the runtime can report its extent; a
|
|
// pointer it cannot size is refused.
|
|
inline void r4d_ar_check_extent(long p, long need, const char* prefix, const char* what) {
|
|
hipDeviceptr_t base = nullptr;
|
|
size_t sz = 0;
|
|
hipError_t e = hipMemGetAddressRange(&base, &sz, (hipDeviceptr_t)(uintptr_t)p);
|
|
if (e != hipSuccess)
|
|
throw std::runtime_error(std::string(prefix) + ": " + what +
|
|
" is not an allocation the runtime can size (" +
|
|
hipGetErrorString(e) + ")");
|
|
const long avail = (long)(((char*)base + sz) - (char*)(uintptr_t)p);
|
|
if (avail < need)
|
|
throw std::runtime_error(std::string(prefix) + ": " + what + " backs " +
|
|
std::to_string(avail) + " bytes from the given pointer, needs " +
|
|
std::to_string(need));
|
|
}
|