This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Asim Siddiqui 87c604370d 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.
2026-11-05 09:49:06 +01:00

167 lines
7.1 KiB
Plaintext

// r3d_ngram_ids.hip -- the hashed row ids of Qwen4-Exp's Per-Layer Embedding.
//
// mixed(k) = t0*mult[0] ^ t1*mult[1] ^ ... ^ tk*mult[k] int64, wrapping
// ids[h] = mixed(b+1) % vocab_sizes[h] + offsets[h] h in block b
//
// One thread a token, looping its heads.
#include <hip/hip_runtime.h>
#include <cstdint>
#include "r3d.h"
#include "r3d_args.h"
#include "r3d_launch.h"
namespace {
__device__ __forceinline__ long long ng_wrap_mul(long long a, long long b) {
return (long long)((unsigned long long)a * (unsigned long long)b);
}
__device__ __forceinline__ long long ng_floor_mod(long long a, long long b) {
const long long r = a % b;
return r < 0 ? r + b : r;
}
enum { kNgMaxShift = 8, kNgThreads = 256 };
__global__ __launch_bounds__(kNgThreads) void r3d_ngram_ids_kernel(
const int32_t* __restrict__ tok, int32_t* __restrict__ state,
const int32_t* __restrict__ cu, const long long* __restrict__ mult,
const long long* __restrict__ vsz, const long long* __restrict__ off,
const int32_t* __restrict__ sidx, const int32_t* __restrict__ hini,
const int32_t* __restrict__ nacc, int32_t* __restrict__ ids,
uint32_t heads, uint32_t ngram, uint32_t per, uint32_t W,
long long eos, uint32_t ids_ld, int32_t* __restrict__ ahead, uint32_t n_ahead,
uint32_t ahead_ld)
{
const uint32_t s = blockIdx.y;
const uint32_t bos = (uint32_t)cu[s];
const uint32_t eot = (uint32_t)cu[s + 1];
const uint32_t ctx = ngram - 1u;
if (eot <= bos) return;
const int32_t slot = sidx ? sidx[s] : (int32_t)s;
if (slot < 0) return;
const uint32_t slen = eot - bos;
const bool warm = !hini || hini[s] != 0;
const long long o = nacc ? (long long)nacc[s] - 1 : 0;
const uint32_t lt = blockIdx.x * kNgThreads + threadIdx.x;
const uint32_t reach = slen + ((ahead && s + 1u == gridDim.y) ? n_ahead : 0u);
if (lt < reach) {
const uint32_t t = bos + lt;
long long prev[kNgMaxShift];
for (uint32_t i = 0; i < ctx; ++i)
prev[i] = warm ? (long long)state[(long long)slot * W + o + i] : eos;
const uint32_t p = ctx + lt;
long long sh[kNgMaxShift];
sh[0] = (long long)tok[t];
bool cut = false;
for (uint32_t k = 1; k < ngram; ++k) {
if (cut || sh[k - 1] == eos) { cut = true; sh[k] = eos; continue; }
const uint32_t q = p - k;
sh[k] = q < ctx ? prev[q] : (long long)tok[bos + (q - ctx)];
}
long long mixed = 0;
int blk = -1;
for (uint32_t k = 0; k < ngram; ++k) {
const long long term = ng_wrap_mul(sh[k], mult[k]);
mixed = k == 0 ? term : (mixed ^ term);
if (k == 0) continue;
++blk;
int32_t* const row = lt < slen ? ids + (long long)t * ids_ld
: ahead + (long long)(lt - slen) * ahead_ld;
for (uint32_t j = 0; j < per; ++j) {
const uint32_t h = (uint32_t)blk * per + j;
row[h] = (int32_t)(ng_floor_mod(mixed, vsz[h]) + off[h]);
}
}
}
__syncthreads();
if (blockIdx.x != 0 || threadIdx.x != 0) return;
int32_t* const win = state + (long long)slot * W;
if (nacc) {
for (uint32_t k = 0; k + 1 < ctx; ++k)
win[k] = warm ? win[o + k + 1] : (int32_t)eos;
for (uint32_t t = 0; t < slen && ctx - 1u + t < W; ++t) win[ctx - 1u + t] = tok[bos + t];
return;
}
long long keep[kNgMaxShift];
for (uint32_t k = 0; k < ctx; ++k) {
const long long src = (long long)slen - (long long)ctx + (long long)k;
keep[k] = src >= 0 ? (long long)tok[bos + src]
: (warm ? (long long)win[o + src + ctx] : eos);
}
for (uint32_t k = 0; k < ctx; ++k) win[k] = (int32_t)keep[k];
}
} // namespace
enum { NG_TOK = 0, NG_STATE, NG_CU, NG_MULT, NG_VSZ, NG_OFF, NG_CIDX, NG_HINIT, NG_NACC, NG_IDS,
NG_AHEAD };
extern "C" int r3d_rad_ngram_ids(const RadArgs* a, RadStream s)
{
const RadTensor* tok = r3d_opt(a, NG_TOK);
const RadTensor* st = r3d_opt(a, NG_STATE);
const RadTensor* cu = r3d_opt(a, NG_CU);
const RadTensor* mult = r3d_opt(a, NG_MULT);
const RadTensor* vsz = r3d_opt(a, NG_VSZ);
const RadTensor* off = r3d_opt(a, NG_OFF);
const RadTensor* cidx = r3d_opt(a, NG_CIDX);
const RadTensor* hini = r3d_opt(a, NG_HINIT);
const RadTensor* nacc = r3d_opt(a, NG_NACC);
const RadTensor* ids = r3d_opt(a, NG_IDS);
const RadTensor* ahd = r3d_opt(a, NG_AHEAD);
if (!r3d_p(tok) || !r3d_wp(st) || !r3d_p(cu) || !r3d_p(mult) ||
!r3d_p(vsz) || !r3d_p(off) || !r3d_wp(ids)) return R3D_NO(RAD_E_INVAL);
if (tok->dtype != RAD_I32 || st->dtype != RAD_I32 || ids->dtype != RAD_I32)
return R3D_NO(RAD_E_DTYPE);
if (cu->dtype != RAD_I32) return R3D_NO(RAD_E_DTYPE);
if (mult->dtype != RAD_I64 || vsz->dtype != RAD_I64 || off->dtype != RAD_I64)
return R3D_NO(RAD_E_DTYPE);
const long long M = r3d_geti_or(a, "M", 0);
const long long heads = r3d_geti_or(a, "heads", 0);
const long long ngram = r3d_geti_or(a, "ngram", 0);
const long long eos = r3d_geti_or(a, "eos", -1);
if (M <= 0 || heads <= 0 || ngram < 2 || eos < 0) return R3D_NO(RAD_E_SHAPE);
if (ngram > kNgMaxShift) return R3D_NO(RAD_E_UNSUPPORTED);
if (heads % (ngram - 1)) return R3D_NO(RAD_E_SHAPE);
const long long per = heads / (ngram - 1), ctx = ngram - 1;
const long long T = ids->rank >= 1 ? ids->shape[0] : M;
const long long ids_ld = ids->rank >= 2 ? ids->stride[0] : heads;
long long A = 0, ahead_ld = heads;
if (ahd && r3d_p(ahd)) {
if (ahd->dtype != RAD_I32) return R3D_NO(RAD_E_DTYPE);
A = ahd->rank >= 1 ? ahd->shape[0] : 0;
ahead_ld = ahd->rank >= 2 ? ahd->stride[0] : heads;
}
if ((long long)tok->shape[0] < T + A) return R3D_NO(RAD_E_SHAPE);
if ((long long)mult->shape[0] < ngram || (long long)vsz->shape[0] < heads ||
(long long)off->shape[0] < heads) return R3D_NO(RAD_E_SHAPE);
const long long N = (long long)cu->shape[0] - 1;
if (N <= 0) return R3D_NO(RAD_E_SHAPE);
const long long W = st->rank >= 2 ? st->shape[st->rank - 1] : ctx;
if (W < ctx) return R3D_NO(RAD_E_SHAPE);
const uint32_t tiles = (uint32_t)((T + A + kNgThreads - 1) / kNgThreads);
dim3 grid(tiles, (uint32_t)N), blk(kNgThreads);
R3D_KLAUNCH(r3d_ngram_ids_kernel, grid, blk, (hipStream_t)s,
(const int32_t*)r3d_p(tok), (int32_t*)r3d_wp(st),
(const int32_t*)r3d_p(cu), (const long long*)r3d_p(mult),
(const long long*)r3d_p(vsz), (const long long*)r3d_p(off),
cidx ? (const int32_t*)r3d_p(cidx) : nullptr,
hini ? (const int32_t*)r3d_p(hini) : nullptr,
nacc ? (const int32_t*)r3d_p(nacc) : nullptr,
(int32_t*)r3d_wp(ids), (uint32_t)heads, (uint32_t)ngram,
(uint32_t)per, (uint32_t)W, eos, (uint32_t)ids_ld,
A > 0 ? (int32_t*)r3d_p(ahd) : nullptr, (uint32_t)A,
(uint32_t)ahead_ld);
return (int)hipGetLastError();
}