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.
53 lines
1.9 KiB
Plaintext
53 lines
1.9 KiB
Plaintext
/* r3d_kv_len_trim.hip -- kv_len_trim: a batch's per-sequence context lengths less `trim`.
|
|
*
|
|
* The prefill overlap runs attention per slice of a one-sequence step: slice A's rows are the step's
|
|
* first rows, so its attention must see the context ending at its own last row, not the step's (rows
|
|
* past it are not yet in the cache). `trim` is the rows after the slice, a runtime count carried by
|
|
* the extent of operand `tail`, whose values are never read.
|
|
*
|
|
* in [n] i32, tail (extent = trim) -> out [n] i32, out[i] = max(in[i] - trim, 0)
|
|
*
|
|
* n <= 64 (a batch), one workgroup.
|
|
*/
|
|
#include <hip/hip_runtime.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include "r3d.h"
|
|
#include "r3d_args.h"
|
|
#include "r3d_launch.h"
|
|
|
|
namespace {
|
|
|
|
__global__ __launch_bounds__(64) void kv_len_trim_kernel(const int32_t* __restrict__ in,
|
|
int32_t* __restrict__ out, int n,
|
|
int trim) {
|
|
const int i = (int)threadIdx.x;
|
|
if (i < n) {
|
|
const int v = in[i] - trim;
|
|
out[i] = v > 0 ? v : 0;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" {
|
|
|
|
int r3d_rad_kv_len_trim(const RadArgs* a, RadStream s)
|
|
{
|
|
if (!a || a->n_t < 3) return R3D_NO(RAD_E_INVAL);
|
|
const RadTensor* in = r3d_opt(a, 0), *tail = r3d_opt(a, 1), *out = r3d_opt(a, 2);
|
|
if (!r3d_p(in) || !r3d_wp(out)) return R3D_NO(RAD_E_INVAL);
|
|
if (in->dtype != RAD_I32 || out->dtype != RAD_I32) return R3D_NO(RAD_E_DTYPE);
|
|
const long long n = r3d_numel(in);
|
|
if (n < 1 || n > 64 || r3d_numel(out) < n) return R3D_NO(RAD_E_SHAPE);
|
|
const long long trim = tail ? r3d_numel(tail) : 0;
|
|
if (trim < 0 || trim > 0x7fffffffLL) return R3D_NO(RAD_E_SHAPE);
|
|
|
|
R3D_KLAUNCH(kv_len_trim_kernel, dim3(1), dim3(64), (hipStream_t)s, (const int32_t*)r3d_p(in),
|
|
(int32_t*)r3d_wp(out), (int)n, (int)trim);
|
|
return (int)hipGetLastError();
|
|
}
|
|
|
|
} // extern "C"
|