libr3d: add 8 kernels — MoE GEMM (w4a8, w4a4, mod4), dequant_w4, row_topk_merge, ngram_ids, gemm_nt_q_bias, gemm_nt_q_short

moe_gemm_q w4a8: the 2-class MoE GEMM with bf16 weight scale and bf16
output. E4M3 activation codes, bf16 weight scale (not f32), single
16x128-block i32 wave-matrix config. The w4a8 dtype row is folded into
the existing moe_gemm_q op as a dtype variant, not a separate op.

moe_gemm_w4a4: the 2-class MoE GEMM with int4 activation codes and
int4 weight codes. i32 wave-matrix core (no E4M3 decode table),
symmetric int4 x int4 multiply-accumulate. Single fixed 16x128-block
config.

moe_gemm_q_mod4: the 4-class MoE GEMM. Experts route to class e%4
(entry e/4 within the class). Same E4M3 wave-matrix core as
moe_gemm_q_e4m3, with 8 individual R3dTab parameters (4 weight + 4
scale tables) to stay within the gfx1102 kernel parameter limit.

dequant_w4: packed int4 weight to row-major bf16. Each 32-bit word
holds 8 int4 codes; the scale is f16 in the low 16 bits, the zero
point is -(1024+zero) as f16 in the high 16 bits. One warp per 32
output elements.

row_topk_merge: top-R of gathered per-rank candidate sets. Each rank
contributes a sorted candidate list; the merge selects the top-R
global candidates by score. int32 ids are bit patterns in an f32
plane (memcpy, not cast).

ngram_ids: hashed n-gram row ids for PLE (Per-Layer Embedding).
Computes the row index for each n-gram in the sequence using a
FNV-1a hash folded into the embedding table size.

gemm_nt_q_bias: narrow fp8 GEMM (M <= 16) with per-column bf16 bias
fused into the epilogue. The bias is added to the f32 accumulator
before the single bf16 narrowing at the store. Extends
r3d_gemm_nt_q_fp8_ex with a bias parameter.

gemm_nt_q_short: narrow fp8 GEMM (M <= 16) for short-prompt decode,
unsplit K. Forces sk=1 (no K-split) in r3d_gemm_nt_q_fp8_ex. The
short-prompt path avoids the K-split overhead when K is small.

The two narrow fp8 rows carry the tGemmM16 tune matrix (sk 0/1/2/4/8/16,
wv 1/2/4/8, nt, the two E4M3 decode impls, 16-row tiles) like the other
gemm_nt_q rows, measured per card by rad-tune.

The six new ops adopt the canonical op schemas. The engine enforces
schema identity across kernel libraries: the first library to declare
an op fixes its schema, and a disagreeing plugin is refused whole at
load. libavx loads first, so the specs match the canonical registries:
- moe_gemm_q_mod4: 15 params / 13 operands (the eight weight tables at
  the RAD_OPD_WTAB role); the launcher reads a_order (token/sorted),
  requires group = 128, checks the activation's row count against the
  output, and refuses unequal class shapes (N_1..K_3 differ from N/K)
  as moe_gemm_q does with N_odd/K_odd.
- ngram_ids: 4 params / 11 operands under the canonical names; state
  required; per, W and the ids pitch derive from the operand shapes.
- row_topk_merge: 4 params (+dtype) / 3 operands.
- gemm_nt_q_short: the family's four optional params (pb1, pb2,
  split_n, w_rb) and two optional operands (a_sum, b_ref).
- gemm_nt_q_bias: a_scale required, not optional.
- dequant_w4: the dtype param, twos required, the scale operand named
  ws; the launcher refuses zero rows with RAD_E_SHAPE like every
  other op.

A kernel audit found five launcher param reads the engine can never
populate (the spec's params plus the row's tune axes are the only
sources): pb1/pb2 in the gemm_nt_q_bias launcher, splits in the decode
attention path, and nt/kb/sk in the tiled rows that tune only impl.
Each always took its default; the reads are deleted, behavior-
identical.

All 8 verified bit-exact on gfx1102 (RX 7600, box card 2).
Selftest: 117 rows clean, 0 failed. rad-schemas over libavx, libr3d,
libr4d and libref: 244 shared ops, 0 disagree.
This commit is contained in:
Asim Siddiqui authored and StillDeadcode committed 2026-11-05 09:49:06 +01:00
1 parent 8ab6f2a8b3
commit 87c604370d
19 files changed
+1571 -113

No files matched your search

+11 -5
View File
@@ -125,6 +125,7 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_q_kernel(
const uint8_t* __restrict__ A, const float* __restrict__ AScale,
const uint8_t* __restrict__ B, const void* __restrict__ BScale, int bs16,
uint16_t* __restrict__ Y, float* __restrict__ Part,
const uint16_t* __restrict__ Bias,
int M, int N, int K, int group, int pb1, int a_ld, int y_ld, int kslab, int nkb)
{
__shared__ uint32_t s_a[kMTMax * kAPitch];
@@ -258,7 +259,8 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_q_kernel(
if constexpr (SPLIT) {
Part[((size_t)blockIdx.y * M + m) * N + n0] = acc;
} else {
Y[(size_t)m * y_ld + n0] = f32_to_bf16(acc);
const float bv = Bias ? bf16_to_f32(Bias[n0]) : 0.0f;
Y[(size_t)m * y_ld + n0] = f32_to_bf16(acc + bv);
}
}
@@ -294,7 +296,8 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_q_reduce(
* 0, -1 a null pointer, -2 an unsupported shape, -3 an unreadable pitch, -4 HIP failed. */
extern "C" int r3d_gemm_nt_q_fp8_ex(long a, long a_scale, long b, long b_scale, long y, int M,
int N, int K, int group, int pb1, int pb2, int a_ld, int y_ld,
int sk, int nt, long scratch, int bs_dt, long stream)
int sk, int nt, long scratch, int bs_dt, long stream,
long bias)
{
if (!a || !b || !b_scale || !y) return -1;
if (M < 1 || N < 1 || K < 1) return -2;
@@ -318,6 +321,7 @@ extern "C" int r3d_gemm_nt_q_fp8_ex(long a, long a_scale, long b, long b_scale,
if (sk > 1 && !scratch) return -1;
const int bs16 = (bs_dt == RAD_BF16); /* anything else, 0 included, reads the plane as f32 */
const uint16_t* bias_p = bias ? (const uint16_t*)(uintptr_t)bias : nullptr;
const int mt = M <= 1 ? 1 : M <= 2 ? 2 : M <= 4 ? 4 : M <= 8 ? 8 : kMTMax;
const int kslab = K / sk;
const int nrowb = (N + pb1 - 1) / pb1;
@@ -331,7 +335,8 @@ extern "C" int r3d_gemm_nt_q_fp8_ex(long a, long a_scale, long b, long b_scale,
R3D_KLAUNCH(R3D_KNAME((gemm_q_kernel<MTV, A8V, NTV, true>)), grid, dim3_blk, \
(hipStream_t)stream, (const uint8_t*)a, (const float*)a_scale, \
(const uint8_t*)b, (const void*)b_scale, bs16, (uint16_t*)y, \
(float*)scratch, M, N, K, group, pb1, a_ld, y_ld, kslab, nkb); \
(float*)scratch, (const uint16_t*)nullptr, \
M, N, K, group, pb1, a_ld, y_ld, kslab, nkb); \
const int mn = M * N; \
const int nb = std::min((mn + kBlockThreads - 1) / kBlockThreads, 4096); \
R3D_KLAUNCH(gemm_q_reduce, dim3(nb), dim3(kBlockThreads), (hipStream_t)stream, \
@@ -340,7 +345,8 @@ extern "C" int r3d_gemm_nt_q_fp8_ex(long a, long a_scale, long b, long b_scale,
R3D_KLAUNCH(R3D_KNAME((gemm_q_kernel<MTV, A8V, NTV, false>)), grid, dim3_blk, \
(hipStream_t)stream, (const uint8_t*)a, (const float*)a_scale, \
(const uint8_t*)b, (const void*)b_scale, bs16, (uint16_t*)y, \
(float*)nullptr, M, N, K, group, pb1, a_ld, y_ld, kslab, nkb); \
(float*)nullptr, (const uint16_t*)bias_p, \
M, N, K, group, pb1, a_ld, y_ld, kslab, nkb); \
} \
} while (0)
#define R3D_Q_A8(MTV, A8V) do { if (nt) R3D_Q_NT(MTV, A8V, true); else R3D_Q_NT(MTV, A8V, false); } while (0)
@@ -482,7 +488,7 @@ extern "C" int r3d_rad_gemm_nt_q_fp8(const RadArgs* a, RadStream s)
(int)pb2, (int)a_ld, (int)y_ld, sk, c.nt,
(long)(uintptr_t)(sk > 1 ? a->scratch : nullptr),
(int)BS->dtype, /* bf16 weight_scale_inv or f32 */
(long)(uintptr_t)s);
(long)(uintptr_t)s, 0);
if (e == 0) return RAD_OK;
return R3D_NO(e == -1 ? RAD_E_INVAL : e == -2 ? RAD_E_SHAPE
: e == -3 ? RAD_E_STRIDE : RAD_E_DEVICE);