Archived
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.
56 lines
2.2 KiB
C++
56 lines
2.2 KiB
C++
/* r3d_fwht.h -- a wave-local fast Walsh-Hadamard transform in registers, no LDS, no barrier.
|
|
*
|
|
* One wave holds a HAD-wide block, VPL = HAD/32 contiguous values per lane, so lane l holds
|
|
* indices l*VPL .. l*VPL+VPL-1. A butterfly at stride h pairs index i with i+h:
|
|
*
|
|
* h < VPL both indices in the same lane -- plain register arithmetic
|
|
* h >= VPL the partner is lane l + h/VPL -- one lane exchange with mask h/VPL
|
|
*
|
|
* HAD/VPL is always 32, so the cross-lane masks are 1, 2, 4, 8, 16 at any HAD: five exchanges per
|
|
* register and no barrier. The lane holding the low index keeps (v + partner), the high one
|
|
* (partner - v). The caller still needs one barrier before a whole-row reduction.
|
|
*
|
|
* Unnormalised: the caller folds 1/sqrt(HAD) into whatever scale it already applies.
|
|
*/
|
|
#pragma once
|
|
#include <hip/hip_runtime.h>
|
|
#include "r3d_common.h"
|
|
|
|
/* `hi ? (o - v) : (v + o)` as `o + (hi ? -v : v)`, the sign flip one XOR against a stage-uniform
|
|
* mask, so there is no per-element select. Bit-identical: negation is exact, addition commutes. */
|
|
template <int VPL, int MASK>
|
|
__device__ __forceinline__ void r3d_fwht_xstage(float* v, int lane)
|
|
{
|
|
const unsigned int sm = (lane & MASK) ? 0x80000000u : 0u;
|
|
#pragma unroll
|
|
for (int j = 0; j < VPL; ++j) {
|
|
const float o = r3d_xor_lane<MASK>(v[j]);
|
|
v[j] = __int_as_float(__float_as_uint(v[j]) ^ sm) + o;
|
|
}
|
|
}
|
|
|
|
/* In-register FWHT of a HAD-wide block held VPL = HAD/32 values per lane, lane-major. */
|
|
template <int HAD>
|
|
__device__ __forceinline__ void r3d_fwht_wave(float* v, int lane)
|
|
{
|
|
constexpr int VPL = HAD / 32;
|
|
// in-lane stages: stride 1 .. VPL/2, entirely within this lane's registers
|
|
#pragma unroll
|
|
for (int h = 1; h < VPL; h <<= 1) {
|
|
#pragma unroll
|
|
for (int j = 0; j < VPL; ++j) {
|
|
if ((j & h) == 0) {
|
|
const float a = v[j], b = v[j + h];
|
|
v[j] = a + b;
|
|
v[j + h] = a - b;
|
|
}
|
|
}
|
|
}
|
|
// cross-lane stages: stride VPL*mask for mask = 1, 2, 4, 8, 16
|
|
r3d_fwht_xstage<VPL, 1>(v, lane);
|
|
r3d_fwht_xstage<VPL, 2>(v, lane);
|
|
r3d_fwht_xstage<VPL, 4>(v, lane);
|
|
r3d_fwht_xstage<VPL, 8>(v, lane);
|
|
r3d_fwht_xstage<VPL, 16>(v, lane);
|
|
}
|