libr4d: step past the full-card launch cliff in qkv_norm_rope and the row norms

On gfx1201 a launch whose wavefronts fall in the last 32 before a multiple of 2048 (every wave slot
of the R9700's 64 CUs x 2 SIMDs x 16) runs about five times slower than the same work one workgroup
wider, whatever the kernel does. A launch-count sweep on one card (2026-10-17): 256-thread
workgroups take 27.6 us at 253 to 256 of them and 5.2 us at 252 or 257, for a norm, an LDS-only
kernel and a plain copy alike; 128- and 512-thread workgroups hit it at the same wavefront counts.
An empty kernel does not, and moving the data does not move it.

The 31B lands on it: qkv_norm_rope at a tp-4 verify is 16 rows x 16 heads of 8 wavefronts, exactly
a full card (21 us a call against ~6 expected; kbench's one-card 4-row point, 256 workgroups, 17.4
us against 5.3 at 8 rows), and a drafter's 256-row head norm through rmsnorm likewise (21.9 us).

r4d_cliff_rows(rows, per_row, waves) is the row count to launch: `rows` when clear of the window,
else the first count past it. qkv_norm_rope, rmsnorm and rmsnorm_p launch that many and return at
once in a row at or past the real count, so every result is the same bytes. r4d_selftest's
qkv_norm_rope case gains 8 rows of the head-256 class (32 heads a row: 256 workgroups, padded).

The window's two numbers are the R9700's, named as such in r4d_common.h, and apply on every target
libr4d builds for (RDNA4 only). They are not derived from the device: no other card has been
measured, and HIP's multiProcessorCount counts WGPs, not CUs, on RDNA. On an RDNA4 part with fewer
CUs a launch padded here costs one idle row and gives the same bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-21 09:24:52 +02:00
1 parent 13a917910f
commit dffec0b770
4 files changed
+52 -11

No files matched your search

+28
View File
@@ -282,3 +282,31 @@ __host__ __device__ __forceinline__ int r4d_act_threads(int M, int K) {
const int lim = K >= 8192 ? (1 << 24) : (K >= 4096 ? 120 : 72);
return M <= lim ? 512 : 256;
}
// The full-card cliff, measured on the R9700 (gfx1201, 64 CUs). A launch whose wavefronts fall
// in the last 32 before a multiple of 2048 runs about five times slower than the same work one
// workgroup wider, whatever the kernel does: 256-thread workgroups take 27.6 us at 253 to 256 of
// them and 5.2 us at 252 or 257, a norm, an LDS-only kernel and a copy alike. The 31B's
// qkv_norm_rope at a tp-4 verify is exactly 256 such workgroups (16 rows x 16 heads), and a
// drafter's 256-row head norm too.
//
// 2048 is every wave slot of the R9700 (64 CUs x 2 SIMDs x 16). Both numbers are that card's and
// apply on every target libr4d builds for; they are not derived from the device, since no other
// card has been measured and HIP's multiProcessorCount counts WGPs, not CUs, on RDNA. On an RDNA4
// part with fewer CUs a launch padded here costs one idle row and gives the same bytes, and a
// cliff at that part's own full-card size, if it has one, is not stepped past.
//
// r4d_cliff_rows() is the row count to launch for a grid of `rows` x `per_row` workgroups of
// `waves` wavefronts each: `rows` where that is clear of the window, else the first count past it.
// The kernel returns at once in a row at or past `rows`, so the result is unchanged.
constexpr uint32_t kR4dCliffWaves = 2048; // the R9700's wave slots: 64 CUs x 2 SIMDs x 16
constexpr uint32_t kR4dCliffWindow = 32; // the slow window, below each multiple of it
__host__ __device__ __forceinline__ uint32_t r4d_cliff_rows(uint32_t rows, uint32_t per_row,
uint32_t waves) {
uint32_t r = rows;
for (int i = 0; i < 64; ++i, ++r) {
const uint64_t w = (uint64_t)r * per_row * waves % kR4dCliffWaves;
if (w != 0 && w <= kR4dCliffWaves - kR4dCliffWindow) break;
}
return r;
}
+12 -7
View File
@@ -209,9 +209,10 @@ __device__ __forceinline__ float act_f32(float v) { return v; }
template <bool WF32, uint32_t THR, typename TX = uint16_t>
__global__ __launch_bounds__(THR) void rmsnorm_kernel(
const TX* __restrict__ x, const void* __restrict__ w, uint16_t* __restrict__ y,
uint32_t n, float eps, float wadd, float mult, uint32_t x_ld, uint32_t y_ld) {
uint32_t n, float eps, float wadd, float mult, uint32_t x_ld, uint32_t y_ld, uint32_t M) {
__shared__ float lds[THR / kWave];
const uint32_t m = blockIdx.x;
if (m >= M) return; // a row the launch added past the full-card cliff (r4d_cliff_rows)
const TX* __restrict__ xr = x + (size_t)m * x_ld;
uint16_t* __restrict__ yr = y + (size_t)m * y_ld;
@@ -231,9 +232,10 @@ __global__ __launch_bounds__(THR) void rmsnorm_kernel(
// rmsnorm does, so the two agree bit for bit where rmsnorm's gain is one.
__global__ __launch_bounds__(kThreads) void rmsnorm_p_kernel(
const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
uint32_t n, float eps, uint32_t x_ld, uint32_t y_ld) {
uint32_t n, float eps, uint32_t x_ld, uint32_t y_ld, uint32_t M) {
__shared__ float lds[kThreads / kWave];
const uint32_t m = blockIdx.x;
if (m >= M) return; // a row the launch added past the full-card cliff (r4d_cliff_rows)
const uint16_t* __restrict__ xr = x + (size_t)m * x_ld;
uint16_t* __restrict__ yr = y + (size_t)m * y_ld;
float ss = 0.0f;
@@ -771,10 +773,12 @@ extern "C" int r4d_rmsnorm_mult_bf16(long x, long w, long y, int M, int n, float
// columns a thread folds into the sum of squares and the fused kernel must be byte-identical
// to this one followed by the quantiser.
const int thr = r4d_act_threads(M, n);
// A drafter's 256-row head norm at 256 threads is exactly a full card; step past the cliff.
#define R4D_RN_T(wf, T) \
hipLaunchKernelGGL((rmsnorm_kernel<wf, T>), dim3((uint32_t)M), dim3(T), 0, \
hipLaunchKernelGGL((rmsnorm_kernel<wf, T>), dim3(r4d_cliff_rows((uint32_t)M, 1, T / kWave)), \
dim3(T), 0, \
(hipStream_t)stream, (const uint16_t*)x, (const void*)w, (uint16_t*)y, \
(uint32_t)n, eps, wadd, mult, (uint32_t)x_ld, (uint32_t)y_ld)
(uint32_t)n, eps, wadd, mult, (uint32_t)x_ld, (uint32_t)y_ld, (uint32_t)M)
#define R4D_RN(wf) \
do { if (thr == 1024) R4D_RN_T(wf, 1024); \
else if (thr == 512) R4D_RN_T(wf, 512); \
@@ -800,9 +804,10 @@ extern "C" int r4d_rmsnorm_p_bf16(long x, long y, int M, int n, float eps,
if (M <= 0 || n <= 0) return -2;
if (!x_ld) x_ld = n;
if (!y_ld) y_ld = n;
hipLaunchKernelGGL(rmsnorm_p_kernel, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream,
hipLaunchKernelGGL(rmsnorm_p_kernel, dim3(r4d_cliff_rows((uint32_t)M, 1, kThreads / kWave)),
dim3(kThreads), 0, (hipStream_t)stream,
(const uint16_t*)x, (uint16_t*)y, (uint32_t)n, eps,
(uint32_t)x_ld, (uint32_t)y_ld);
(uint32_t)x_ld, (uint32_t)y_ld, (uint32_t)M);
return hipGetLastError() == hipSuccess ? 0 : -4;
}
@@ -1351,7 +1356,7 @@ static int rmsnorm_x_f32(const float* x, const void* w, uint16_t* y, int M, int
#define R4D_RNF_T(wf, T) \
hipLaunchKernelGGL((rmsnorm_kernel<wf, T, float>), dim3((uint32_t)M), dim3(T), 0, \
(hipStream_t)stream, x, w, y, (uint32_t)n, eps, wadd, mult, \
(uint32_t)x_ld, (uint32_t)y_ld)
(uint32_t)x_ld, (uint32_t)y_ld, (uint32_t)M)
#define R4D_RNF(wf) \
do { if (thr == 1024) R4D_RNF_T(wf, 1024); \
else if (thr == 512) R4D_RNF_T(wf, 512); \
+9 -3
View File
@@ -78,10 +78,12 @@ __global__ __launch_bounds__(HD) void qkv_norm_rope_kernel(
const int32_t* __restrict__ pos, const void* __restrict__ qw, const void* __restrict__ kw,
uint32_t nq, uint32_t nkv, uint32_t pq, uint32_t pk, uint32_t pv,
uint32_t q_ld, uint32_t k_ld, uint32_t v_ld, float eps, float wadd,
uint32_t rot, uint32_t nfreq, int den, float theta, float rscale, uint32_t n_pos) {
uint32_t rot, uint32_t nfreq, int den, float theta, float rscale, uint32_t n_pos,
uint32_t m_rows) {
__shared__ float l1[HD / kWave], l2[HD / kWave];
__shared__ uint16_t nb[HD];
const uint32_t m = blockIdx.x, hh = blockIdx.y, t = threadIdx.x;
if (m >= m_rows) return; // a row the launch added past the full-card cliff (r4d_cliff_rows)
const float div = rscale != 0.0f ? rscale : 1.0f;
// rope rotates only the rows its positions cover; a row past them is normed and not rotated
const float p0 = m < n_pos ? (float)pos[m] / div : 0.0f;
@@ -172,12 +174,16 @@ extern "C" int r4d_rad_qkv_norm_rope(const RadArgs* a, RadStream stream) {
const float wadd = (float)r4d_getf(a, "wadd", 0.0);
const float theta = (float)r4d_getf(a, "theta", 10000.0);
const float rsc = (float)r4d_getf(a, "scale", 1.0);
const dim3 grid((uint32_t)M, (uint32_t)(nq + nkv + ((vk || !vn) ? 0 : nkv)));
const uint32_t heads = (uint32_t)(nq + nkv + ((vk || !vn) ? 0 : nkv));
// A tp-4 verify of the 31B is 16 rows x 16 heads of 8 wavefronts: exactly a full card, the
// cliff r4d_cliff_rows steps past with one idle row.
const dim3 grid(r4d_cliff_rows((uint32_t)M, heads, (uint32_t)(hd / kWave)), heads);
#define R4D_QNR_T(HD, WF, VK) emit_rc = R4D_EMIT(stream, (qkv_norm_rope_kernel<HD, WF, VK>), grid, dim3(HD), 0, \
(uint16_t*)r4d_wp(q), (uint16_t*)r4d_wp(k), (uint16_t*)r4d_wp(v), (const int32_t*)r4d_p(ps), \
r4d_p(qw), r4d_p(kw), (uint32_t)nq, (uint32_t)nkv, pq, pk, pv, (uint32_t)q_ld, (uint32_t)k_ld, \
(uint32_t)v_ld, eps, wadd, (uint32_t)rot_l, (uint32_t)nfreq, (int)den, theta, rsc, (uint32_t)Mp)
(uint32_t)v_ld, eps, wadd, (uint32_t)rot_l, (uint32_t)nfreq, (int)den, theta, rsc, (uint32_t)Mp, \
(uint32_t)M)
#define R4D_QNR(HD) \
do { if (qw->dtype == RAD_F32) { if (vk) R4D_QNR_T(HD, true, true); else R4D_QNR_T(HD, true, false); } \
else { if (vk) R4D_QNR_T(HD, false, true); else R4D_QNR_T(HD, false, false); } } while (0)
+3 -1
View File
@@ -10884,7 +10884,9 @@ static void qkv_norm_rope_numeric(const Plugin& pl) {
for (const Cls& cl : { Cls{ 256, 16, 8, 256, "rotary", 10000.0, false, true },
Cls{ 512, 16, 2, 128, "head", 1000000.0, true, true },
Cls{ 128, 16, 8, 128, "rotary", 10000.0, false, false } }) {
for (const int64_t M : { (int64_t)1, (int64_t)4, (int64_t)40 }) {
/* 8 rows of the head-256 class are 8 x 32 workgroups of 8 wavefronts, a full card: the
* launch adds an idle row past the cliff (r4d_cliff_rows), which must not change a byte. */
for (const int64_t M : { (int64_t)1, (int64_t)4, (int64_t)8, (int64_t)40 }) {
const int64_t QD = cl.nq * cl.hd, KD = cl.nkv * cl.hd;
std::vector<uint16_t> hq((size_t)M * QD), hk((size_t)M * KD), hv((size_t)M * KD);
std::vector<uint16_t> hqw((size_t)cl.hd), hkw((size_t)cl.hd);