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
radiance-sycl/libr3d/r3d_expert_scale_bf16.hip
Asim Siddiqui 34c52ebe96 libr3d: the RDNA3 (gfx11) wave-matrix kernel plugin
A self-contained HIP kernel plugin for gfx11 (RDNA3) that exposes the
libref-declared ops as device rows the engine can tune and dispatch:

  * GEMMs (bf16, f16, int8-activation, vocab) retiled to 16x128 on the
    wave-matrix (WMMA) builtins. The retile removes the 8x wave
    redundancy of the naive tile and stages the activation once per
    16x128 block; the 16-row and 64-row forms share the stage. The k-walk
    double-buffers the 128-deep tile (the next tile stages while the
    current one multiplies), on the m64 and tiled forms.
  * Quantised GEMMs: fp8-e4m3 x e4m3 (tiled and ALU), mxfp4 x a8, w2a8,
    w4a16, w5a16, w8a8 (the prefill heights 17..127 on the wave matrix),
    and the gated (swiglu) form. E4M3 decodes exactly to f16 through a
    256-entry LDS table; the WMMA fragments bit-cast the staged halfs
    (a uint16-to-_Float16 assignment is a value conversion, not a
    reinterpret).
  * Half-precision-accumulate arms (acc = 1) on the m64 wave matrix:
    the f16/bf16 16x16x16 builtins with the compile-time opsel half
    select.
  * Grouped MoE GEMMs (bf16, e4m3 with F32 block scales, mxfp4a8, w4a8),
    retiled the same way, over the run-walk / gather structure.
  * Normalisation (layernorm / rmsnorm) and the quantisation ops.
  * GDN linear-attention, the QSA/KV state ops, and the MoE
    router/scatter/gather family (router_topk_scatter, router_sigmoid_topk
    with the correction bias, moe_gather, moe_gather_inv; the top-k
    router runs to n_expert 512).

Every row implements an op libref declares; the selector (tune) picks
the implementation and the NOTUNE rows carry the untuned default.
Selftest-clean (121 rows, schema + finiteness); values verified against
CPU references per op.
2026-11-01 20:56:25 +01:00

78 lines
3.2 KiB
Plaintext

/* r3d_expert_scale_bf16.hip -- expert_scale: each routing weight times its expert's own scale,
* in place.
*
* The router renormalises its top-k, then multiplies each weight by its expert's per-expert
* scale. Unfused that is two launches per MoE layer (an embed lookup of the [n_expert, 1] scale
* table by expert id, then a mul), each a few dozen elements at decode. Fused:
*
* ew[t, k] = bf16( ew[t, k] * scale[ids[t, k]] )
*
* Bit-identical to the pair: one f32 product of two bf16 values, rounded once to nearest even.
* An id outside the table reads a zero scale, so the product's sign matches too. One thread per
* element; no cross-lane traffic, no wmma.
*/
#include <hip/hip_runtime.h>
#include <cstdint>
#include "r3d.h"
#include "r3d_args.h"
#include "r3d_launch.h"
#include "r3d_codecs.h"
namespace {
__global__ __launch_bounds__(256) void escale_kernel(const int32_t* __restrict__ ids, long long ids_ld,
const unsigned short* __restrict__ sc,
unsigned short* __restrict__ ew, long long ew_ld,
long long rows, int k, int n_expert)
{
const long long e = (long long)blockIdx.x * 256 + threadIdx.x;
if (e >= rows * k) return;
const long long r = e / k;
const int c = (int)(e - r * k);
const int id = ids[r * ids_ld + c];
const float s = (id >= 0 && id < n_expert) ? bf16_to_f32(sc[id]) : 0.0f;
uint16_t* w = (uint16_t*)(ew + r * ew_ld + c);
*w = (uint16_t)f32_to_bf16(bf16_to_f32(*w) * s);
}
// [rows, k] with a dense last axis: its rows and row pitch, or false.
static bool rows_k(const RadTensor* t, int dt, long long k, long long* rows, long long* ld)
{
if (!t || t->dtype != dt || t->rank < 1) return false;
if (t->shape[t->rank - 1] != k || t->stride[t->rank - 1] != 1) return false;
*rows = 1;
for (int i = 0; i + 1 < t->rank; ++i) *rows *= t->shape[i];
*ld = r3d_row_pitch(t);
return *ld >= k || *rows <= 1;
}
} // namespace
extern "C" {
/* operands: ids, w_e (the [n_expert] scale table), ew (inout) */
int r3d_rad_expert_scale_bf16(const RadArgs* a, RadStream s)
{
if (!a || a->n_t < 3) return R3D_NO(RAD_E_INVAL);
const long long k = r3d_geti_or(a, "top_k", 0), ne = r3d_geti_or(a, "n_expert", 0);
if (k <= 0 || k > 64 || ne <= 0 || ne > (1LL << 20)) return R3D_NO(RAD_E_SHAPE);
const RadTensor* I = r3d_opt(a, 0), *S = r3d_opt(a, 1), *W = r3d_opt(a, 2);
if (!r3d_p(I) || !r3d_p(S) || !r3d_wp(W)) return R3D_NO(RAD_E_INVAL);
if (S->dtype != RAD_BF16 || r3d_numel(S) < ne) return R3D_NO(RAD_E_DTYPE);
long long ri = 0, li = 0, rw = 0, lw = 0;
if (!rows_k(I, RAD_I32, k, &ri, &li) || !rows_k(W, RAD_BF16, k, &rw, &lw))
return R3D_NO(RAD_E_SHAPE);
const long long M = rw;
if (M <= 0) return 0;
if (ri < M || M * k > 0x7fffffffLL) return R3D_NO(RAD_E_SHAPE);
const unsigned blocks = (unsigned)((M * k + 255) / 256);
R3D_KLAUNCH(escale_kernel, dim3(blocks), dim3(256), (hipStream_t)s,
(const int32_t*)r3d_p(I), li, (const unsigned short*)r3d_p(S),
(unsigned short*)r3d_wp(W), lw, M, (int)k, (int)ne);
return (int)hipGetLastError();
}
} // extern "C"