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

+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)