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/libr4d/r4d_givens_rotate.hip
T

175 lines
9.4 KiB
Plaintext

// ParoQuant's rotation, to f16: the channel scale and Givens stages applied to a bf16 activation, for the
// 16-bit-activation GEMMs (w5a16 / w4a16_asym, rows up to 64 -- decode). The weight it pairs with is stored
// as v = w . D . G_1 ... G_S (abi/rad_encoding.h, "givens"), so the product <v, x'> with
// x' = x . D^-1 . G_1 ... G_S is <w, x>: the rotation is the activation's to do, once a token, and the
// weight is read by the GEMM unchanged. The quantising form, for prefill-sized rows and for MXFP4, is
// r4d_givens_dense.hip.
//
// WHY THE ROTATION IS A GROUP AT A TIME. ParoQuant's pairs never leave a group of 128 columns -- that is
// how the checkpoint is trained, and the converter and the loader (r4d_rad_relayout_givens_pairs) refuse
// one that does -- so a group is a closed system:
// its 128 values are rotated by S stages of 64 disjoint pairs. A wave owns a group. The values sit in LDS
// (128 floats a wave, no barrier needed: a wave's LDS accesses are in order, and the pairs of a stage are
// disjoint so no two lanes touch one address) and each lane takes two pairs a stage.
//
// PARTS. A fused projection (gate|up, q|k|v) was rotated per piece, so a row yields `parts` rotated
// copies, side by side: y is [M, parts * K]. A (part, group) pair is one unit of work, one wave's (a row
// is several blocks, grid.y). The tables stack the parts as the "givens" encoding does -- stages rows each
// of pairs and angle, one row of channel scales.
//
// THE CHANNEL SCALE IS A DIVIDE, because the encoding's t.scale is the factor the stored row was
// multiplied by (D), and the activation takes its inverse. A true IEEE divide on both sides: the
// reference (libref) divides in f32 too, so the two agree on this step exactly.
//
// COS AND SIN ARE COMPUTED HERE, in f32, from the stored angle: sincosf is accurate to an ulp or two,
// which is also what libref's libm is.
//
// Returns a negative code rather than throwing, for the reason given in r4d_gemm_fp8a8.hip.
#include "r4d.h"
#include "r4d_emit.h"
#include "r4d_fp8_args.h"
#include <hip/hip_fp16.h>
namespace {
constexpr uint32_t kWave = 32;
constexpr uint32_t kThreads = 256;
constexpr uint32_t kWaves = kThreads / kWave;
constexpr uint32_t kGroup = 128;
constexpr uint32_t kPairsPerLane = kGroup / 2 / kWave; // 2
// kPre stages' tables are loaded and their angles' sine and cosine taken BEFORE the first of them is
// applied, so the dependent chain a stage would otherwise be -- table load, LDS read, rotate, LDS
// write -- overlaps its neighbours' loads. At decode (M of a handful of rows) the kernel is latency
// and not bandwidth: a row is 40 groups of 128 and each group is eight serial stages.
constexpr uint32_t kPre = 8;
__device__ __forceinline__ void wave_sync() {
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "wavefront");
__builtin_amdgcn_wave_barrier();
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "wavefront");
}
// One wave rotates one (part, group) of one row: grid.x is the row, and grid.y spreads a row's
// `parts * nkb` groups over blocks of kWaves waves -- one group a wave, so a row is several blocks and a
// decode step of a few rows still fills the part. A wave that has no group returns at once; nothing in
// the kernel is a block barrier. The rotated row is written as f16 into `y`, y_ld elements a row.
__global__ __launch_bounds__(kThreads) void givens_rotate_kernel(
const uint16_t* __restrict__ x, const int32_t* __restrict__ pairs,
const float* __restrict__ angle, const float* __restrict__ rscale, uint16_t* __restrict__ y,
uint32_t K, uint32_t nkb, uint32_t parts, uint32_t stages, uint32_t x_ld, uint32_t y_ld) {
__shared__ float sbuf[kWaves][kGroup];
const uint32_t row = blockIdx.x;
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
const uint32_t item = blockIdx.y * kWaves + wave;
if (item >= parts * nkb) return;
const uint32_t part = item / nkb, g = item % nkb;
const uint32_t c0 = g * kGroup;
const size_t xrow = (size_t)row * x_ld;
float* buf = sbuf[wave];
// channel scale: v = x / D, four consecutive columns a lane
{
const float4 d = *(const float4*)(rscale + (size_t)part * K + c0 + lane * 4u);
const uint32_t xc = c0 + lane * 4u;
const float v0 = __uint_as_float((uint32_t)x[xrow + xc + 0] << 16) / d.x;
const float v1 = __uint_as_float((uint32_t)x[xrow + xc + 1] << 16) / d.y;
const float v2 = __uint_as_float((uint32_t)x[xrow + xc + 2] << 16) / d.z;
const float v3 = __uint_as_float((uint32_t)x[xrow + xc + 3] << 16) / d.w;
*(float4*)(buf + lane * 4u) = make_float4(v0, v1, v2, v3);
}
for (uint32_t st0 = 0; st0 < stages; st0 += kPre) {
const uint32_t nst = (stages - st0) < kPre ? (stages - st0) : kPre;
int2 ab[kPre][kPairsPerLane];
float th[kPre][kPairsPerLane], cs[kPre][kPairsPerLane], sn[kPre][kPairsPerLane];
#pragma unroll
for (uint32_t k = 0; k < kPre; ++k)
if (k < nst) {
const size_t trow = (size_t)part * stages + st0 + k;
const int32_t* pr = pairs + trow * K + c0;
const float* an = angle + trow * (K / 2u) + g * (kGroup / 2u);
#pragma unroll
for (uint32_t u = 0; u < kPairsPerLane; ++u) {
const uint32_t j = lane + u * kWave;
ab[k][u] = *(const int2*)(pr + 2u * j);
th[k][u] = an[j];
}
}
#pragma unroll
for (uint32_t k = 0; k < kPre; ++k)
if (k < nst)
#pragma unroll
for (uint32_t u = 0; u < kPairsPerLane; ++u) sincosf(th[k][u], &sn[k][u], &cs[k][u]);
wave_sync(); // the scaled row (or the previous chunk's last stage) is in LDS
#pragma unroll
for (uint32_t k = 0; k < kPre; ++k)
if (k < nst) {
#pragma unroll
for (uint32_t u = 0; u < kPairsPerLane; ++u) {
const int ia = ab[k][u].x - (int)c0, ib = ab[k][u].y - (int)c0;
const float va = buf[ia], vb = buf[ib];
buf[ia] = fmaf(cs[k][u], va, sn[k][u] * vb);
buf[ib] = fmaf(cs[k][u], vb, -sn[k][u] * va);
}
wave_sync();
}
}
const float4 r = *(const float4*)(buf + lane * 4u);
const float v[4] = { r.x, r.y, r.z, r.w };
// f16 holds +-65504; a rotated post-norm row is orders below that, and the clamp keeps an outlier
// finite instead of inf
union { __half h[4]; uint2 u; } o;
#pragma unroll
for (int j = 0; j < 4; ++j) o.h[j] = __float2half(fminf(fmaxf(v[j], -65504.0f), 65504.0f));
*(uint2*)(y + (size_t)row * y_ld + (size_t)part * K + c0 + lane * 4u) = o.u;
}
} // namespace
// x [M][K] bf16, `x_ld` elements a row (0 = K)
// pairs [parts*stages][K] int32 column indices; (pairs[2j], pairs[2j+1]) is pair j
// angle [parts*stages][K/2] f32
// rscale [parts][K] f32, the factor the stored row was multiplied by
// y [M][parts*K] f16
enum { GV_X = 0, GV_PAIRS = 1, GV_ANGLE = 2, GV_RSCALE = 3, GV_Y = 4 };
static int givens_rotate_body(const RadArgs* a, const R4dSink& sk) {
const RadTensor* x = r4d_opt(a, GV_X);
const RadTensor* pr = r4d_opt(a, GV_PAIRS);
const RadTensor* an = r4d_opt(a, GV_ANGLE);
const RadTensor* rs = r4d_opt(a, GV_RSCALE);
const RadTensor* y = r4d_opt(a, GV_Y);
if (!x || !pr || !an || !rs || !y) return R4D_NO(RAD_E_INVAL);
if (x->rank < 2 || y->rank != 2 || pr->rank != 2 || an->rank != 2 || rs->rank != 2)
return R4D_NO(RAD_E_SHAPE);
if (y->dtype != RAD_F16) return R4D_NO(RAD_E_DTYPE);
if (pr->dtype != RAD_I32 || an->dtype != RAD_F32 || rs->dtype != RAD_F32)
return R4D_NO(RAD_E_DTYPE);
// rows are dim 0 and everything after it is the row
const long long M = x->shape[0], K = r4d_numel(x) / (x->shape[0] > 0 ? x->shape[0] : 1);
const long long grp = (long long)kGroup, nkb = K / grp;
const long long parts = r4d_geti_or(a, "parts", 1), stages = r4d_geti_or(a, "stages", 0);
if (M <= 0 || r4d_geti_or(a, "group", grp) != grp || K % grp || parts < 1 || stages < 1)
return R4D_NO(RAD_E_SHAPE);
if (y->shape[0] != M || y->shape[1] != parts * K) return R4D_NO(RAD_E_SHAPE);
// the tables are the weight's, contiguous: parts * stages rows of K pairs and K/2 angles
if (pr->shape[0] != parts * stages || pr->shape[1] != K) return R4D_NO(RAD_E_SHAPE);
if (an->shape[0] != parts * stages || an->shape[1] != K / 2) return R4D_NO(RAD_E_SHAPE);
if (rs->shape[0] != parts || rs->shape[1] != K) return R4D_NO(RAD_E_SHAPE);
if (!r4d_aligned(rs->data, 16) || !r4d_aligned(pr->data, 8) || !r4d_aligned(y->data, 4))
return R4D_NO(RAD_E_ALIGN);
const long long xp = r4d_row_pitch(x), yp = r4d_row_pitch(y);
if (xp < 0 || yp < 0) return R4D_NO(RAD_E_STRIDE);
const uint32_t gy = ((uint32_t)(parts * nkb) + kWaves - 1) / kWaves;
const int rc = R4D_EMIT(sk, givens_rotate_kernel, dim3((uint32_t)M, gy), dim3(kThreads), 0,
(const uint16_t*)x->data, (const int32_t*)pr->data, (const float*)an->data,
(const float*)rs->data, (uint16_t*)y->data, (uint32_t)K, (uint32_t)nkb,
(uint32_t)parts, (uint32_t)stages, (uint32_t)(xp ? xp : K), (uint32_t)(yp ? yp : parts * K));
if (rc != RAD_OK) return rc;
return (!sk.desc && hipGetLastError() != hipSuccess) ? RAD_E_DEVICE : RAD_OK;
}
R4D_ENTRY(r4d_rad_givens_rotate, givens_rotate_body)