hc_read: the previous decode kernels behind r4d_hc_read_pin_decode, compared byte for byte

The one-grid form and the pipelined tiled up-mix replaced hc_read_a_kernel, hc_up_mix_kernel and
hc_up_mix_tiled_kernel in place. Their previous forms are kept verbatim as *_prev, with
hc_up_mix_prev_launch launching the up-mix as before, and r4d_hc_read_pin_decode(1) selects them:
kernel A, then the up-mix in a second launch, and the tiled up-mix's K passes in sequence. 0, the
default, is the launcher's rule.

r4d_selftest's hc_decode_forms runs both on the same inputs on a real stream at 1 to 7 rows (each
one-grid instantiation), 8, 16, 32, 33 and 64 (the tiled up-mix at both row tiles), with E4M3 and
int8 codes, and compares x, the injected gains, the codes, their scales and the rotated twin byte
for byte. The two forms must dispatch different kernels (r4d_record_dispatches), so an ignored pin
fails: with the hook made a no-op, all 24 checks fail. For that the tiled up-mix now launches
through r4d_emit, so the dispatch log sees it. --perfhc times both under the launcher's prefill rule.

--perfhc M, cold (256 MiB through the caches), one R9700 at 240 W, three runs, medians, previous
-> rule: 1 row 32.3 -> 22.5 us (-30.3 %), 4 rows 34.8 -> 25.4 (-27.0 %), 8 rows 46.2 -> 43.3
(-6.3 %), 32 rows 51.1 -> 48.4 (-5.3 %). r4d_selftest --case hc_read_e4m3: 62 ok, 0 FAIL (24 of
them the new comparisons); hc_read_bf16: 5 ok.

libr4d/isa/res.sh: no hc kernel spills or uses scratch (195 kernels). hc_read_fused_kernel takes
126 to 147 VGPRs (9 or 10 waves). The pipelined tiled up-mix at its 32-row E4M3 tile takes 111
VGPRs where the previous one took 75 (12 waves a SIMD against 16), and is the faster of the two at
8 to 32 rows as measured above.
This commit is contained in:
Adam Zhang authored and StillDeadcode committed 2026-10-28 13:48:43 +01:00
1 parent 222cbe7981
commit 11076c06fc
3 files changed
+637 -9

No files matched your search

+6
View File
@@ -701,6 +701,12 @@ void r4d_moe_gemm_pin_down_dec(int pin);
// (refused likewise, and below 512 rows, whose scratch is not sized for their weight planes).
// All bit-identical; r4d_selftest compares their bytes.
void r4d_hc_read_pin_prefill(int form);
// Test hook: pins the gated residual read's decode kernels. 0 is the launcher's rule (kernels A and
// B as one grid at one to seven rows where the per-stream counters exist, the tiled up-mix's K
// passes pipelined from eight rows), 1 the kernels the rule replaced (A, then the up-mix in a second
// launch; the tiled up-mix's passes in sequence). Bit-identical; r4d_selftest compares their bytes
// and --perfhc times both.
void r4d_hc_read_pin_decode(int form);
// Test hook: pins the MLA matrix row's key load. 0 is the launcher's rule (sixteen-byte vectors
// wherever the cache rows are aligned to them), 1 the element-wise load the rule falls back to.
// Bit-identical; r4d_selftest compares them and --perfmla times both.
+485 -5
View File
@@ -1611,6 +1611,307 @@ static inline int hc_up_rpt(bool f8, int hc, long n) {
/* hc_up_mix_kernel's launch: hc_up's row group; counter slots are (rows / rg) row groups of
* n / kUpTB scale groups. */
/* ============================================================== the previous decode kernels
*
* hc_read's decode kernels as they were before the one-grid form and the pipelined tiled up-mix:
* kernel A, then the up-mix kernel in a second launch, and the tiled up-mix with its K passes in
* sequence. Kept verbatim behind r4d_hc_read_pin_decode(1) so r4d_selftest can compare the
* current forms with them byte for byte and --perfhc can time both from one build. */
static int g_hc_decode_form = 0;
template <bool F8>
__global__ __launch_bounds__(kThreads)
__attribute__((amdgpu_waves_per_eu(F8 ? 9 : 1, F8 ? 9 : 16)))
void hc_read_a_prev_kernel(const unsigned short* __restrict__ h, const float* __restrict__ w,
const void* __restrict__ downv, long dld,
const unsigned short* __restrict__ injw,
unsigned short* __restrict__ dout, unsigned short* __restrict__ inj,
float* __restrict__ scaleout,
long rows, long n, int hc, long lr, long ta, long hrow, long irow,
float eps, float wadd) {
extern __shared__ unsigned short sN[]; /* [hc * n], transposed per 256-element chunk */
__shared__ float s_part[kWaves][kHcMax];
__shared__ float s_scale[kHcMax];
/* One 256-entry staging slot per wave (4 KiB); no barrier, a wave uses only its own. */
__shared__ unsigned short s_stage[kWaves][kWave * 8];
static_assert(sizeof(s_part) + sizeof(s_scale) + sizeof(s_stage) == kHcReadAStaticLds,
"the launcher sizes N's allocation against this kernel's static LDS");
constexpr long kPer = kWave * 8;
const long r = blockIdx.y;
if (r >= rows) return;
const long HN = (long)hc * n;
/* The inject gains get a block of their own for load balance. With ta = kWaves every wave does
* one dot over HN; adding the gains to a column-owning block would give its waves a second
* one, and that slowest block would set the kernel's duration. So the grid is one wider and
* the last block owns no columns. */
const bool pub = (blockIdx.x == gridDim.x - 1);
const int tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
const unsigned short* hp = h + r * hrow;
const unsigned short* __restrict__ down = (const unsigned short*)downv;
/* The transposed layout follows from the shape: the chunk must sit inside one sub-stream
* (n % 256) and every 16-byte group be aligned in LDS and in `h`; otherwise the plain layout,
* with the same N. The E4M3 dot reads N in natural order, so only the bf16 form transposes. */
const bool swz = !F8 && (n % kPer == 0) && (((uintptr_t)hp & 15u) == 0u)
&& (((uintptr_t)down & 15u) == 0u)
&& (!injw || ((uintptr_t)injw & 15u) == 0u);
/* ---- sums of squares, one stream at a time, straight from `h`. One stream at a time keeps
* the accumulator a scalar; an array indexed by runtime `hc` would live in scratch.
*
* This prologue is latency-bound, not traffic-bound: 20 KiB against the block's 320 KiB of
* W_down, yet 6.7 of the kernel's 18.7 us at the decode grid, because ten dependent iterations
* wait on loads with nothing else resident. Issuing kPreSs iterations' loads first turns eight
* serial latencies into one, bit-identically. Wider loads (these bytes are served from the
* 64 MB last-level cache, not DRAM), reading `h` once instead of twice, and deeper prefetch
* were all measured as no gain. */
constexpr int kPreSs = 8; // n / kThreads is 10 a stream at this shape, and 12 spills
for (int c = 0; c < hc; ++c) {
const long cb = (long)c * n;
float acc = 0.0f;
long i = tid;
for (; i + (long)(kPreSs - 1) * kThreads < n; i += (long)kPreSs * kThreads) {
uint32_t hv[kPreSs];
#pragma unroll
for (int p = 0; p < kPreSs; ++p) hv[p] = bfw(hp + cb + i + (long)p * kThreads);
#pragma unroll
for (int p = 0; p < kPreSs; ++p) { const float v = bfw_f(hv[p]); acc += v * v; }
}
for (; i < n; i += kThreads) { const float v = bf2f(hp[cb + i]); acc += v * v; }
acc = wave_sum(acc);
if (lane == 0) s_part[wave][c] = acc;
}
__syncthreads();
if (tid < hc) {
float ss = 0.0f;
for (int k = 0; k < kWaves; ++k) ss += s_part[k][tid];
s_scale[tid] = __frsqrt_rn(ss / (float)n + eps);
/* One workgroup publishes; the others compute the identical value. */
if (pub && scaleout) scaleout[r * kHcMax + tid] = s_scale[tid];
}
__syncthreads();
/* ---- N into LDS, narrowed to bf16 where the reference narrows it. Looped per stream rather
* than computing `j / n`, a 64-bit integer division per element (a software sequence here). */
for (int c = 0; c < hc; ++c) {
const long cb = (long)c * n;
const float sc = s_scale[c];
if (swz) {
/* A thread owns one (chunk, lane) group: logical elements `lane + 32t`, one 16-byte
* store at physical `q`. A wave covers one chunk, so `ch` is wave-uniform and the
* `h` and `w` reads stay coalesced. */
for (long base = 0; base < n; base += (long)kThreads * 8) {
const long q = base + (long)tid * 8;
if (q >= n) break;
const long ch = cb + (q & ~(kPer - 1));
float out[8];
#pragma unroll
for (int t = 0; t < 8; ++t) {
const long li = ch + lane + (long)t * kWave;
out[t] = bfw_f(bfw(hp + li)) * sc * (w[li] + wadd);
}
*(uint4*)(sN + cb + q) = f2bf8(out);
}
} else {
constexpr int kPreScale = 8;
long i = tid;
for (; i + (long)(kPreScale - 1) * kThreads < n; i += (long)kPreScale * kThreads) {
float wv[kPreScale];
uint32_t hv[kPreScale];
#pragma unroll
for (int p = 0; p < kPreScale; ++p) {
wv[p] = w[cb + i + (long)p * kThreads];
hv[p] = bfw(hp + cb + i + (long)p * kThreads);
}
#pragma unroll
for (int p = 0; p < kPreScale; ++p)
sN[cb + i + (long)p * kThreads] = f2bf(bfw_f(hv[p]) * sc * (wv[p] + wadd));
}
for (; i < n; i += kThreads)
sN[cb + i] = f2bf(bf2f(hp[cb + i]) * sc * (w[cb + i] + wadd));
}
}
__syncthreads();
/* ---- this workgroup's columns of W_down, one wave per column. */
const float inv_hc = 1.0f / (float)hc;
const long k0 = (long)blockIdx.x * ta;
for (long k = k0 + wave; k < k0 + ta && k < lr; k += kWaves) {
float acc;
if constexpr (F8) {
acc = hc8_dot_down<kDotU>(sN, (const unsigned char*)downv + k * dld, HN, lane);
} else {
const unsigned short* wr = down + k * HN;
acc = swz ? wave_dot_swz<kDotU>(sN, wr, HN, lane)
: wave_dot_lds<kDotU>(sN, wr, HN, lane, s_stage[wave]);
}
acc = wave_sum(acc);
if (lane == 0) dout[r * lr + k] = f2bf(siluf_(acc * inv_hc));
}
/* ---- the inject gains, on the block that owns no columns and already holds N. */
if (pub && injw && inj) {
for (int c = wave; c < hc; c += kWaves) {
const unsigned short* wr = injw + (long)c * HN;
float acc = swz ? wave_dot_swz<kDotU>(sN, wr, HN, lane)
: wave_dot_lds<kDotU>(sN, wr, HN, lane, s_stage[wave]);
acc = wave_sum(acc);
if (lane == 0) inj[r * irow + c] = f2bf(2.0f * sigmoidf_(acc * inv_hc));
}
}
}
template <int RG, bool F8, bool I8 = false>
__global__ __launch_bounds__(kThreads)
void hc_up_mix_prev_kernel(const void* __restrict__ upv, long uld,
const unsigned short* __restrict__ dbuf,
const unsigned short* __restrict__ h, const float* __restrict__ w,
const float* __restrict__ scalein,
unsigned short* __restrict__ x, unsigned char* __restrict__ q,
float* __restrict__ qs, unsigned char* __restrict__ rq,
float* __restrict__ rqs, unsigned int* __restrict__ ctr,
long rows, long n, int hc, long lr, long hrow, long xrow,
long qrow, long srow, long rqrow, long rsrow, float wadd) {
extern __shared__ float sd[]; /* [RG * lr] */
__shared__ unsigned short s_stage[kWaves][kWave * 8]; /* wave_dot_lds, one slot a wave */
__shared__ unsigned short s_u[RG * kUpRows];
const unsigned short* __restrict__ up = (const unsigned short*)upv;
const long r0 = (long)blockIdx.y * RG;
if (r0 >= rows) return;
const int tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
const int ipb = kUpRows / hc; /* channels this workgroup owns */
const long i0 = (long)blockIdx.x * ipb;
for (long k = tid; k < (long)RG * lr; k += kThreads) {
const long r = k / lr;
sd[k] = (r0 + r < rows) ? bf2f(dbuf[(r0 + r) * lr + (k - r * lr)]) : 0.0f;
}
__syncthreads();
/* Row t of this workgroup's slice of W_up is sub-stream t / ipb of channel i0 + t % ipb. The
* E4M3 form takes each row four threads at a time, as in hc_up_kernel. */
if constexpr (F8) {
static_assert(kUpRows * 4 == kThreads, "four threads a row of W_up");
const int t = tid >> 2, q4 = tid & 3;
const long j = (long)(t / ipb) * n + i0 + t % ipb;
float acc[RG];
hc8_dot_up<RG>(sd, lr, (const unsigned char*)upv + j * uld, q4, acc);
#pragma unroll
for (int r = 0; r < RG; ++r)
if (q4 == 0) s_u[r * kUpRows + t] = f2bf(sigmoidf_(acc[r]));
} else {
for (int t = wave; t < kUpRows; t += kWaves) {
const long j = (long)(t / ipb) * n + i0 + t % ipb;
float acc[RG];
wave_dot_lds_rows<RG>(sd, lr, up + j * lr, lr, lane, s_stage[wave], acc);
#pragma unroll
for (int r = 0; r < RG; ++r) {
const float a = wave_sum(acc[r]);
if (lane == 0) s_u[r * kUpRows + t] = f2bf(sigmoidf_(a));
}
}
}
__syncthreads();
const float inv_hc = 1.0f / (float)hc;
for (int idx = tid; idx < RG * ipb; idx += kThreads) {
const int rl = idx / ipb, ii = idx - rl * ipb;
const long r = r0 + rl;
if (r >= rows) continue;
const long i = i0 + ii;
float acc = 0.0f;
for (int c = 0; c < hc; ++c) {
const long j = (long)c * n + i;
const float Nv = nbf(bf2f(h[r * hrow + j]) * scalein[r * kHcMax + c]
* (w[j] + wadd));
acc += bf2f(s_u[rl * kUpRows + c * ipb + ii]) * Nv;
}
acc *= inv_hc;
x[r * xrow + i] = f2bf(acc);
}
if (!q || !ctr) return;
/* The group's last workgroup quantises it: kUpTB / ipb of them cover one group of a row group. */
const int nb = kUpTB / ipb;
__threadfence();
__syncthreads();
__shared__ unsigned int s_arrived;
const unsigned slot = blockIdx.y * (unsigned)(n / kUpTB) + (unsigned)(i0 / kUpTB);
if (tid == 0)
s_arrived = __hip_atomic_fetch_add(&ctr[slot], 1u, __ATOMIC_ACQ_REL,
__HIP_MEMORY_SCOPE_AGENT);
__syncthreads();
if (s_arrived + 1u != (unsigned)nb) return;
if (tid == 0)
__hip_atomic_store(&ctr[slot], 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
__threadfence();
/* One wave per row (RG <= wave count); a lane takes four of the group's 128 channels, one
* packed dword of codes. */
if (wave >= RG) return;
const long r = r0 + wave;
if (r >= rows) return;
const long base = (i0 / kUpTB) * kUpTB;
const unsigned short* const xr = x + r * xrow + base;
float m = 0.0f;
for (int t = lane; t < kUpTB; t += kWave) m = fmaxf(m, fabsf(bf2f(xr[t])));
m = r4d_wave_max(m);
const float sc = m > 0.0f ? m * (1.0f / q8_top<I8>()) : 1.0f;
const float inv = 1.0f / sc;
if (lane == 0) qs[r * srow + base / kUpTB] = sc;
for (int d = lane; d < kUpTB / 4; d += kWave) {
const int c4 = d * 4;
const uint32_t pk = q8_pack4<I8>(bf2f(xr[c4 + 0]), bf2f(xr[c4 + 1]), bf2f(xr[c4 + 2]),
bf2f(xr[c4 + 3]), inv);
*(unsigned*)(q + r * qrow + base + c4) = (unsigned)pk;
}
if (rq) {
float v[4];
#pragma unroll
for (int j = 0; j < 4; ++j) v[j] = bf2f(xr[lane * 4 + j]);
hc_rot_group(v, lane, rq + r * rqrow + base, rqs + r * rsrow + base / kUpTB);
}
}
static inline int hc_up_mix_prev_launch(const RadTensor* up, long uld, bool f8, bool i8,
const unsigned short* dbuf,
const unsigned short* h, const float* w, const float* scale,
unsigned short* x, unsigned char* q, float* qs,
unsigned char* rq, float* rqs,
unsigned int* ctr, long long rows, long long n, int hc,
long long lr, long long hrow, long long xrow, long long qrow,
long long srow, long long rqrow, long long rsrow, float wadd,
RadStream stream) {
int emit_rc = RAD_OK;
const long rg = hc_up_rg(rows, lr);
const dim3 grid((unsigned)(n / (kUpRows / hc)), (unsigned)((rows + rg - 1) / rg));
const unsigned lds = (unsigned)(rg * lr * sizeof(float));
const void* upp = up->data;
#define HC_UPMIX_CASE3(RG_, F8_, I8_) \
emit_rc = R4D_EMIT(stream, (hc_up_mix_prev_kernel<RG_, F8_, I8_>), grid, dim3(kThreads), lds, \
upp, uld, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr, (long)rows, (long)n, \
hc, \
(long)lr, (long)hrow, (long)xrow, (long)qrow, (long)srow, (long)rqrow, \
(long)rsrow, wadd)
#define HC_UPMIX_CASE(RG_, F8_) \
do { if (i8) HC_UPMIX_CASE3(RG_, F8_, true); else HC_UPMIX_CASE3(RG_, F8_, false); } while (0)
switch (rg) {
case 8: if (f8) HC_UPMIX_CASE(8, true); else HC_UPMIX_CASE(8, false); break;
case 7: if (f8) HC_UPMIX_CASE(7, true); else HC_UPMIX_CASE(7, false); break;
case 6: if (f8) HC_UPMIX_CASE(6, true); else HC_UPMIX_CASE(6, false); break;
case 5: if (f8) HC_UPMIX_CASE(5, true); else HC_UPMIX_CASE(5, false); break;
case 4: if (f8) HC_UPMIX_CASE(4, true); else HC_UPMIX_CASE(4, false); break;
case 3: if (f8) HC_UPMIX_CASE(3, true); else HC_UPMIX_CASE(3, false); break;
case 2: if (f8) HC_UPMIX_CASE(2, true); else HC_UPMIX_CASE(2, false); break;
default: if (f8) HC_UPMIX_CASE(1, true); else HC_UPMIX_CASE(1, false); break;
}
#undef HC_UPMIX_CASE
#undef HC_UPMIX_CASE3
return emit_rc;
}
static inline int hc_up_mix_launch(const RadTensor* up, long uld, bool f8, bool i8,
const unsigned short* dbuf,
const unsigned short* h, const float* w, const float* scale,
@@ -1620,6 +1921,10 @@ static inline int hc_up_mix_launch(const RadTensor* up, long uld, bool f8, bool
long long lr, long long hrow, long long xrow, long long qrow,
long long srow, long long rqrow, long long rsrow, float wadd,
RadStream stream) {
if (g_hc_decode_form == 1)
return hc_up_mix_prev_launch(up, uld, f8, i8, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr,
rows, n, hc, lr, hrow, xrow, qrow, srow, rqrow, rsrow, wadd,
stream);
int emit_rc = RAD_OK;
const long rg = hc_up_rg(rows, lr);
const int rpt = hc_up_rpt(f8, hc, n);
@@ -2512,6 +2817,172 @@ void hc_up_mix_tiled_kernel(const void* __restrict__ upv, long uld,
}
}
/* The tiled up-mix as it was before its K passes were pipelined (see the previous decode
* kernels above; r4d_hc_read_pin_decode(1)). */
template <int RT, int NT, bool F8, bool I8 = false>
__global__ __launch_bounds__(kThreads)
void hc_up_mix_tiled_prev_kernel(const void* __restrict__ upv, long uld,
const unsigned short* __restrict__ dbuf,
const unsigned short* __restrict__ h, const float* __restrict__ w,
const float* __restrict__ scalein,
unsigned short* __restrict__ x, unsigned char* __restrict__ q,
float* __restrict__ qs, unsigned char* __restrict__ rq,
float* __restrict__ rqs, unsigned int* __restrict__ ctr,
long rows, long n, int hc, long lr, long hrow, long xrow,
long qrow, long srow, long rqrow, long rsrow, float wadd) {
__shared__ unsigned short sA[RT * kTAP];
__shared__ unsigned short sB[NT * kTAP];
__shared__ unsigned short s_u[RT * NT];
__shared__ unsigned short s_N[RT * NT];
__shared__ long s_j[NT];
constexpr int MT = RT / 16, NT16 = NT / 16;
constexpr int WM = (MT < kWaves) ? MT : kWaves, WN = kWaves / WM;
constexpr int AM = (MT + WM - 1) / WM, AN = (NT16 + WN - 1) / WN;
const int tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
const int ipb = NT / hc; /* channels this workgroup owns */
const long r0 = (long)blockIdx.y * RT;
const long i0 = (long)blockIdx.x * ipb;
if (r0 >= rows) return;
/* The column map, once: tile column t is sub-stream t % hc of channel i0 + t / hc. */
if (tid < NT) s_j[tid] = (long)(tid % hc) * n + i0 + tid / hc;
__syncthreads();
v8f acc[AM][AN];
#pragma unroll
for (int im = 0; im < AM; ++im)
#pragma unroll
for (int in = 0; in < AN; ++in) acc[im][in] = (v8f){0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
for (long k0 = 0; k0 < lr; k0 += kTKT) {
__syncthreads();
for (int idx = tid; idx < RT * (kTKT / 8); idx += kThreads) {
const int rr = idx / (kTKT / 8), k8 = (idx % (kTKT / 8)) * 8;
long r = r0 + rr; if (r >= rows) r = rows - 1;
const long ka = k0 + k8;
uint4 va = make_uint4(0u, 0u, 0u, 0u);
if (ka + 8 <= lr) va = *(const uint4*)(dbuf + r * lr + ka);
*(uint4*)(sA + rr * kTAP + k8) = va;
}
for (int idx = tid; idx < NT * (kTKT / 8); idx += kThreads) {
const int tt = idx / (kTKT / 8), k8 = (idx % (kTKT / 8)) * 8;
const long k = k0 + k8;
uint4 v = make_uint4(0u, 0u, 0u, 0u);
if (k + 8 <= lr) v = hc_up_stage<F8>(upv, uld, s_j[tt], k, lr);
*(uint4*)(sB + tt * kTAP + k8) = v;
}
__syncthreads();
#pragma unroll
for (int im = 0; im < AM; ++im) {
const int mt = wave / WN + im * WM;
if (mt >= MT) continue;
#pragma unroll
for (int in = 0; in < AN; ++in) {
const int nt = wave % WN + in * WN;
if (nt >= NT16) continue;
acc[im][in] = gemm<kTKT / 16>(sA, kTAP, mt, sB, kTAP, nt, lane, acc[im][in]);
}
}
}
/* N in a separate coalesced pass: in the WMMA epilogue consecutive lanes hold consecutive
* sub-streams, so reading `h` there would stride n elements per lane. Here consecutive threads
* take consecutive channels of one sub-stream, as in hc_mix. */
for (int idx = tid; idx < RT * NT; idx += kThreads) {
const int rl = idx / NT, rem = idx - rl * NT;
const int c = rem / ipb, ii = rem - c * ipb;
const long r = r0 + rl;
if (r >= rows) continue;
const long j = (long)c * n + i0 + ii;
s_N[rl * NT + ii * hc + c] =
f2bf(bf2f(h[r * hrow + j]) * scalein[r * kHcMax + c] * (w[j] + wadd));
}
/* u from the accumulators. */
const int e0 = 8 * (lane >> 4), nn = lane & 15;
#pragma unroll
for (int im = 0; im < AM; ++im) {
const int mt = wave / WN + im * WM;
if (mt >= MT) continue;
#pragma unroll
for (int in = 0; in < AN; ++in) {
const int nt = wave % WN + in * WN;
if (nt >= NT16) continue;
const int t = nt * 16 + nn;
#pragma unroll
for (int e = 0; e < 8; ++e) {
const long r = r0 + mt * 16 + e0 + e;
if (r >= rows) continue;
s_u[(r - r0) * NT + t] = f2bf(sigmoidf_(acc[im][in][e]));
}
}
}
__syncthreads();
const float inv_hc = 1.0f / (float)hc;
for (int idx = tid; idx < RT * ipb; idx += kThreads) {
const int rl = idx / ipb, ii = idx - rl * ipb;
const long r = r0 + rl;
if (r >= rows) continue;
const unsigned short* const up_ = s_u + rl * NT + ii * hc;
const unsigned short* const np_ = s_N + rl * NT + ii * hc;
float a = 0.0f;
for (int c = 0; c < hc; ++c) a += bf2f(up_[c]) * bf2f(np_[c]);
x[r * xrow + i0 + ii] = f2bf(a * inv_hc);
}
if (!q || !ctr) return;
/* The group's last workgroup quantises it; `nb` workgroups cover one 128-channel group. */
const int nb = kUpTB / ipb;
__threadfence();
__syncthreads();
__shared__ unsigned int s_arrived;
const unsigned slot = blockIdx.y * (gridDim.x / nb) + (unsigned)(i0 / kUpTB);
if (tid == 0)
s_arrived = __hip_atomic_fetch_add(&ctr[slot], 1u, __ATOMIC_ACQ_REL,
__HIP_MEMORY_SCOPE_AGENT);
__syncthreads();
if (s_arrived + 1u != (unsigned)nb) return;
if (tid == 0)
__hip_atomic_store(&ctr[slot], 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
__threadfence();
/* The rotated codes need a whole wave per row, while the codes below use kThreads / RT threads
* per row, so the rotation walks the tile's rows a wave at a time first. */
if (rq) {
const long gb = (i0 / kUpTB) * kUpTB;
for (int rl = wave; rl < RT; rl += kWaves) {
const long r = r0 + rl;
if (r >= rows) break;
const unsigned short* const xr = x + r * xrow + gb;
float v[4];
#pragma unroll
for (int j = 0; j < 4; ++j) v[j] = bf2f(xr[lane * 4 + j]);
hc_rot_group(v, lane, rq + r * rqrow + gb, rqs + r * rsrow + gb / kUpTB);
}
}
constexpr int kTPR = kThreads / RT; /* threads a row; RT divides kThreads */
const int rl = tid / kTPR, lr_ = tid % kTPR;
const long r = r0 + rl;
if (r >= rows) return;
const long base = (i0 / kUpTB) * kUpTB;
const unsigned short* const xr = x + r * xrow + base;
float m = 0.0f;
for (int t = lr_; t < kUpTB; t += kTPR) m = fmaxf(m, fabsf(bf2f(xr[t])));
m = r4d_xor_max<kTPR>(m);
const float sc = m > 0.0f ? m * (1.0f / q8_top<I8>()) : 1.0f;
const float inv = 1.0f / sc;
if (lr_ == 0) qs[r * srow + base / kUpTB] = sc;
for (int d = lr_; d < kUpTB / 4; d += kTPR) {
const int c4 = d * 4;
const uint32_t pk = q8_pack4<I8>(bf2f(xr[c4 + 0]), bf2f(xr[c4 + 1]), bf2f(xr[c4 + 2]),
bf2f(xr[c4 + 3]), inv);
*(unsigned*)(q + r * qrow + base + c4) = (unsigned)pk;
}
}
/* ==================================== hc_read, kernels B and B2 in one launch at the prefill tile
*
* At RT 128 hc_up_mix_tiled_kernel's two staging planes plus the A tile take 58 KiB of LDS (one
@@ -2767,16 +3238,22 @@ static inline void hc_up_mix_tiled_launch(int rt, bool f8, bool i8, const void*
long xrow, long qrow, long srow, long rqrow, long rsrow,
float wadd, RadStream s) {
const dim3 gm((unsigned)(n / (kTNT / hc)), (unsigned)((rows + rt - 1) / rt));
/* Through r4d_emit, so r4d_record_dispatches sees which form ran. */
#define HC_UM_K(K_) \
(void)R4D_EMIT(s, K_, gm, dim3(kThreads), 0, up, uld, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr, \
rows, n, hc, lr, hrow, xrow, qrow, srow, rqrow, rsrow, wadd)
#define HC_UM_CASE3(RT_, F8_, I8_) \
hipLaunchKernelGGL((hc_up_mix_tiled_kernel<RT_, kTNT, F8_, I8_>), gm, dim3(kThreads), 0, \
(hipStream_t)s, up, uld, dbuf, h, w, scale, x, q, qs, rq, rqs, ctr, rows, \
n, hc, lr, hrow, xrow, qrow, srow, rqrow, rsrow, wadd)
do { \
if (g_hc_decode_form == 1) HC_UM_K((hc_up_mix_tiled_prev_kernel<RT_, kTNT, F8_, I8_>)); \
else HC_UM_K((hc_up_mix_tiled_kernel<RT_, kTNT, F8_, I8_>)); \
} while (0)
#define HC_UM_CASE(RT_, F8_) \
do { if (i8) HC_UM_CASE3(RT_, F8_, true); else HC_UM_CASE3(RT_, F8_, false); } while (0)
if (f8) { if (rt == 32) HC_UM_CASE(32, true); else HC_UM_CASE(64, true); }
else { if (rt == 32) HC_UM_CASE(32, false); else HC_UM_CASE(64, false); }
#undef HC_UM_CASE
#undef HC_UM_CASE3
#undef HC_UM_K
}
/* The codes of `x` and the rotated codes, after a mix that writes only `x`: hc_quant_rows_kernel
@@ -4278,7 +4755,7 @@ extern "C" int r4d_rad_hc_read(const RadArgs* a, RadStream s) {
/* The preloaded prologue at the shape it is instantiated for (hc 4, n 2560), and
* there kernels A and B as one grid (hc_read_fused_kernel) when the row group is the whole
* step, the up-mix fuses (q's counters or none) and the per-stream counters exist. */
const bool pre = f8 && hc == 4 && n == 10LL * kThreads;
const bool pre = f8 && hc == 4 && n == 10LL * kThreads && g_hc_decode_form != 1;
if (pre) {
const long rgf = hc_up_rg(rows, lr);
const int rpt = hc_up_rpt(true, (int)hc, n);
@@ -4332,7 +4809,9 @@ extern "C" int r4d_rad_hc_read(const RadArgs* a, RadStream s) {
(long)dld, injw ? (const unsigned short*)injw->data : nullptr, dbuf, \
inj ? (unsigned short*)inj->data : nullptr, scale, (long)rows, (long)n, (int)hc, \
(long)lr, (long)ta, (long)hrow, (long)irow, eps, wadd)
if (pre) emit_rc = HC_READ_A((hc_read_a_kernel<true, 4, 10>));
if (g_hc_decode_form == 1)
emit_rc = f8 ? HC_READ_A(hc_read_a_prev_kernel<true>) : HC_READ_A(hc_read_a_prev_kernel<false>);
else if (pre) emit_rc = HC_READ_A((hc_read_a_kernel<true, 4, 10>));
else emit_rc = f8 ? HC_READ_A(hc_read_a_kernel<true>) : HC_READ_A(hc_read_a_kernel<false>);
#undef HC_READ_A
if (emit_rc != RAD_OK) return R4D_NO(RAD_E_DEVICE);
@@ -4384,3 +4863,4 @@ extern "C" int r4d_rad_hc_read(const RadArgs* a, RadStream s) {
}
extern "C" void r4d_hc_read_pin_prefill(int form) { g_hc_prefill_form = form; }
extern "C" void r4d_hc_read_pin_decode(int form) { g_hc_decode_form = form; }
+146 -4
View File
@@ -142,6 +142,7 @@ struct Plugin {
void (*pin_gather)(int) = nullptr;
void (*pin_ddec)(int) = nullptr;
void (*pin_hc)(int) = nullptr;
void (*pin_hc_dec)(int) = nullptr;
void (*pin_qsel)(int) = nullptr;
void (*pin_mla)(int) = nullptr;
void (*mla_stats)(unsigned long long*) = nullptr;
@@ -212,6 +213,7 @@ static bool load(Plugin* pl, const char* path) {
okall &= sym(pl->h, "r4d_moe_gather_pin_form", &pl->pin_gather);
okall &= sym(pl->h, "r4d_moe_gemm_pin_down_dec", &pl->pin_ddec);
okall &= sym(pl->h, "r4d_hc_read_pin_prefill", &pl->pin_hc);
okall &= sym(pl->h, "r4d_hc_read_pin_decode", &pl->pin_hc_dec);
okall &= sym(pl->h, "r4d_qsa_select_pin_form", &pl->pin_qsel);
okall &= sym(pl->h, "r4d_mla_matrix_pin_form", &pl->pin_mla);
okall &= sym(pl->h, "r4d_mla_matrix_stats", &pl->mla_stats);
@@ -18088,6 +18090,129 @@ static void hc_read_i8(const Plugin& pl, int64_t M, bool real_stream, int form)
: "the int8 run changed" + bad);
}
/* The gated residual's read at its decode kernels, byte for byte: the launcher's rule (kernels A and
* B as one grid at one to seven rows, the tiled up-mix pipelined from eight) against the kernels it
* replaced, pinned through r4d_hc_read_pin_decode(1). E4M3 or int8 codes, on a real stream (the
* one-grid form keys its counters on it). The two forms must dispatch different kernels, so a pin
* that is ignored cannot pass by comparing a form with itself. */
static void hc_decode_forms(const Plugin& pl, int64_t M, bool i8) {
const std::string nm = fmt("hc_read_e4m3 decode kernels at M=%lld, %s codes: the rule "
"byte-identical to the previous kernels", (long long)M,
i8 ? "int8" : "E4M3");
const RadKernelInfo* r = find(pl, "hc_read_e4m3");
if (!r) { skip(nm.c_str(), "row absent"); return; }
if (!pl.pin_hc_dec || !pl.record) { skip(nm.c_str(), "no r4d_hc_read_pin_decode"); return; }
const int64_t n = 2560, hc = 4, lr = 320, HN = hc * n, grp = 128, nkb = n / grp;
const int64_t dld = r4d_hc8::row_bytes(HN, r4d_hc8::down_group(HN));
const int64_t uld = r4d_hc8::row_bytes(lr, r4d_hc8::up_group(lr));
uint64_t rs = 0xD1B54A32D192ED03ull ^ (uint64_t)M;
auto rnd = [&]() { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; };
std::vector<uint16_t> hh((size_t)(M * HN));
for (auto& v : hh) v = h_to_bf16((float)((int)(rnd() % 2001) - 1000) / 400.0f);
std::vector<float> wg((size_t)HN);
for (auto& v : wg) v = (float)((int)(rnd() % 201) - 100) / 1000.0f;
auto plane = [&](int64_t rows, int64_t K, int64_t G, int64_t ld) {
std::vector<uint8_t> b((size_t)(rows * ld), 0);
for (int64_t r0 = 0; r0 < rows; ++r0) {
uint8_t* row = b.data() + r0 * ld;
for (int64_t k = 0; k < K; ++k) row[k] = (uint8_t)(8 + rnd() % 111) | ((rnd() & 1) ? 0x80u : 0u);
float* sc = (float*)(row + K);
for (int64_t g = 0; g < K / G; ++g) sc[g] = 0.002f + 0.004f * (float)(rnd() % 1000) / 1000.0f;
}
return b;
};
std::vector<uint8_t> down = plane(lr, HN, r4d_hc8::down_group(HN), dld);
std::vector<uint8_t> up = plane(HN, lr, r4d_hc8::up_group(lr), uld);
std::vector<uint16_t> injw((size_t)(hc * HN));
for (auto& v : injw) v = h_to_bf16((float)((int)(rnd() % 201) - 100) / 50000.0f);
const size_t xb = (size_t)(M * n * 2), ib = (size_t)(M * hc * 2), qb = (size_t)(M * n),
sb = (size_t)(M * nkb * 4);
void* d_h = dalloc(hh.size() * 2);
void* d_w = dalloc(wg.size() * 4);
void* d_dn = dalloc(down.size());
void* d_up = dalloc(up.size());
void* d_iw = dalloc(injw.size() * 2);
void* d_x = dalloc(xb);
void* d_inj = dalloc(ib);
void* d_q = dalloc(qb);
void* d_qs = dalloc(sb);
void* d_rq = dalloc(qb);
void* d_rs = dalloc(sb);
if (!d_h || !d_w || !d_dn || !d_up || !d_iw || !d_x || !d_inj || !d_q || !d_qs || !d_rq || !d_rs) {
ok(nm.c_str(), false, "hipMalloc failed");
return;
}
st_memcpy(d_h, hh.data(), hh.size() * 2, hipMemcpyHostToDevice);
st_memcpy(d_w, wg.data(), wg.size() * 4, hipMemcpyHostToDevice);
st_memcpy(d_dn, down.data(), down.size(), hipMemcpyHostToDevice);
st_memcpy(d_up, up.data(), up.size(), hipMemcpyHostToDevice);
st_memcpy(d_iw, injw.data(), injw.size() * 2, hipMemcpyHostToDevice);
hipStream_t hs = nullptr;
if (hipStreamCreate(&hs) != hipSuccess) { ok(nm.c_str(), false, "stream"); return; }
const uint32_t qdt = i8 ? (uint32_t)RAD_I8 : (uint32_t)RAD_F8E4M3;
std::vector<RadTensor> ts = {
T(d_h, RAD_BF16, { M, HN }), T(d_w, RAD_F32, { HN }),
T(d_dn, RAD_F8E4M3, { lr, HN }), T(d_up, RAD_F8E4M3, { HN, lr }),
T(d_iw, RAD_BF16, { hc, HN }), T(d_x, RAD_BF16, { M, n }),
T(d_inj, RAD_BF16, { M, hc }), T(d_q, qdt, { M, n }),
T(d_qs, RAD_F32, { M, nkb }), T(d_rq, RAD_F8E4M3, { M, n }),
T(d_rs, RAD_F32, { M, nkb }),
};
std::vector<RadParam> ps = {
PI("M", M), PI("n", n), PI("hc", hc), PI("lowrank", lr), PF("eps", 1e-6),
PS("dtype", "bf16"), PF("wadd", 1.0), PI("inject", 1), PI("group", grp),
PI("rotate", 1), PS("mix", "e4m3"),
};
RadArgs a;
std::memset(&a, 0, sizeof(a));
a.t = ts.data(); a.n_t = (int)ts.size();
a.p = ps.data(); a.n_p = (int)ps.size();
if (r->scratch) {
const int64_t need = r->scratch(&a);
if (need > 0) { a.scratch = dalloc((size_t)need); a.scratch_bytes = need; }
}
struct Out { std::vector<uint8_t> x, inj, q, qs, rq, rs; std::vector<const void*> fns; };
Out out[2];
for (int form = 0; form < 2; ++form) {
pl.pin_hc_dec(form);
st_memset(d_x, 0, xb); st_memset(d_q, 0, qb); st_memset(d_qs, 0, sb);
st_memset(d_rq, 0, qb); st_memset(d_rs, 0, sb); st_memset(d_inj, 0, ib);
const void* fns[16] = {};
pl.record(fns, 16);
const int rc = r->launch(&a, (RadStream)hs);
const int nd = pl.record(nullptr, 0);
if (rc != RAD_OK || st_stream_sync(hs) != hipSuccess) {
ok(nm.c_str(), false, fmt("%s rc=%d", form ? "the previous kernels" : "the rule", rc));
pl.pin_hc_dec(0);
(void)hipStreamDestroy(hs);
return;
}
out[form].fns.assign(fns, fns + (nd < 16 ? nd : 16));
auto get = [&](std::vector<uint8_t>& v, void* d, size_t b) {
v.resize(b); st_memcpy(v.data(), d, b, hipMemcpyDeviceToHost);
};
get(out[form].x, d_x, xb); get(out[form].inj, d_inj, ib); get(out[form].q, d_q, qb);
get(out[form].qs, d_qs, sb); get(out[form].rq, d_rq, qb); get(out[form].rs, d_rs, sb);
}
pl.pin_hc_dec(0);
(void)hipStreamDestroy(hs);
std::string bad;
if (out[0].x != out[1].x) bad += " x";
if (out[0].inj != out[1].inj) bad += " inj";
if (out[0].q != out[1].q) bad += " q";
if (out[0].qs != out[1].qs) bad += " qs";
if (out[0].rq != out[1].rq) bad += " rq";
if (out[0].rs != out[1].rs) bad += " rs";
int64_t nz = 0;
for (uint8_t v : out[0].q) nz += v != 0;
const bool distinct = out[0].fns != out[1].fns;
ok(nm.c_str(), bad.empty() && nz > (int64_t)qb / 2 && distinct,
!bad.empty() ? "differs in" + bad
: !distinct ? std::string("both forms dispatched the same kernels")
: fmt("%lld of %zu codes non-zero; %zu and %zu launches", (long long)nz, qb,
out[0].fns.size(), out[1].fns.size()));
}
/* The gated residual's read at a prefill chunk: hc_read_e4m3 at Qwen3.8-Flash-Next's shape (n
* 2560, four streams, lowrank 320, inject gains, the fp8 twin and its rotated twin). Several
* launches at prefill, so also a harness for libr4d/isa/trace.sh. Random codes, small positive
@@ -18248,8 +18373,7 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed, bool
for (size_t f = 1; f < outs.size(); ++f) same(f, kForm[ran[f]]);
if (timed) {
for (const int form : ran) {
if (pl.pin_hc) pl.pin_hc(form);
auto time_now = [&](const char* label) {
hipEvent_t w0, w1;
st_ev_create(&w0); st_ev_create(&w1);
st_ev_record(w0, hs);
@@ -18265,8 +18389,21 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed, bool
const double flop = 2.0 * (double)M * (double)HN * (double)(2 * lr + hc);
std::printf("%s at M=%lld (n %lld, hc %lld, lowrank %lld, inject, fp8 twin + "
"rotated), %s: %.1f us a call, %.1f TFLOP/s\n", rn, (long long)M,
(long long)n, (long long)hc, (long long)lr, kForm[form], us,
(long long)n, (long long)hc, (long long)lr, label, us,
flop / (us * 1e6));
};
for (const int form : ran) {
if (pl.pin_hc) pl.pin_hc(form);
time_now(kForm[form]);
}
/* The decode kernels against the ones they replaced (r4d_hc_read_pin_decode), under the
* launcher's prefill rule. */
if (pl.pin_hc_dec && f8) {
if (pl.pin_hc) pl.pin_hc(0);
pl.pin_hc_dec(1);
time_now("the previous decode kernels");
pl.pin_hc_dec(0);
time_now("the launcher's rule");
}
}
if (pl.pin_hc) pl.pin_hc(0);
@@ -30956,6 +31093,10 @@ static void device_phase(const Plugin& pl) {
for (int form : { 1, 2 }) hc_read_i8(pl, m, true, form);
for (int64_t m : { 700, 2048 }) hc_read_i8(pl, m, true, 3);
}
/* The decode kernels against the ones they replaced: each one-grid instantiation (1 to 7
* rows) and the tiled up-mix at both of its row tiles. */
for (int64_t m : { 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 33, 64 })
for (bool i8 : { false, true }) hc_decode_forms(pl, m, i8);
}
if (sel("hc_read_bf16")) {
if (!g_case) std::printf("\nthe gated residual's read over bf16 mixing matrices at the "
@@ -31420,7 +31561,8 @@ static void usage(void) {
" --perfw2 N K M [iters]: the int2 DFlash2 draft head at every variant it has.\n"
" --perfhc M [iters]: hc_read_e4m3 at Qwen3.8-Flash-Next's shape and M rows, every\n"
" prefill-tile form that serves M (the wide kernels at the wide tile,\n"
" PF2 from M=512) compared byte for byte and timed as one op each;\n"
" PF2 from M=512) compared byte for byte and timed as one op each,\n"
" then the previous decode kernels against the launcher's rule;\n"
" run it under libr4d/isa/trace.sh for its launches.\n"
" --cold MIB: with a --perf mode, time cold: read MIB through the caches before\n"
" every timed launch (256 empties this card's L2 and last level) and\n"