ParoQuant: drop the sequential rotate-and-quantise ops (givens_quant_act_fp8 / givens_quant_act_i8g): givens_dense_quant replaces them; the rotate-only f16 kernel keeps its own file

This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent c17f5fdc89
commit ec65b8cbeb
18 files changed
+318 -486

No files matched your search

+1 -1
View File
@@ -126,7 +126,7 @@ set(R4D_UNITS
"r4d_gemm_fp8a16_nt_m1|"
"r4d_gemm_fp8a8|"
"r4d_quant_act_fp8|"
"r4d_givens_quant_act_fp8|"
"r4d_givens_rotate|"
"r4d_fused_quant_fp8|"
"r4d_gram_accum_fp8|"
"r4d_qsa_block_key_bf16|"
+1 -1
View File
@@ -3,7 +3,7 @@
//
// dequant(Wq)[n][k] = scale[n][k/128] * (code[n][k] - zero[n][k/128]), code in 0..15 or 0..31
//
// -- ParoQuant's int4 (z-lab) and int5 once the activation has been rotated (givens_quant_act_i8g) -- in
// -- ParoQuant's int4 (z-lab) and int5 once the activation has been rotated (givens_dense_quant) -- in
// 16x16x16 int8 WMMA, which issues at the fp8 instruction's rate. The f16 skinny kernel these weights
// also serve (r4d_gemm_w5a16_nt_m64) is capped by the f16 matrix instruction at about 75 TF/s; this is the
// tall-tile form with no such ceiling, built on r4d_gemm_mxfp4a8_tiled.hip's arrangement:
+1 -1
View File
@@ -2,7 +2,7 @@
//
// The Givens stages of a 128-column group compose to one orthogonal 128 x 128 matrix T, so
// y = T (x / D) is a small GEMM: out[m][j] = sum_i T[j][i] * (x[m][i] / D[i]). The sequential kernel
// (r4d_givens_quant_act_fp8.hip) does the same job in `stages` dependent LDS rounds a group and is
// (r4d_givens_rotate.hip) does the same job in `stages` dependent LDS rounds a group and is
// latency bound (~100-130 GB/s of ~640); this one spends ~8x the multiply-adds on the f16 WMMA, which
// is 5 us of math for a 2048-row part, and quantises in the epilogue -- int8 (scale amax / 127, for the
// int8 prefill GEMMs) or E4M3 (amax / 448, for the mxfp4a8 GEMMs), the same scale grid as the
-239
View File
@@ -1,239 +0,0 @@
// ParoQuant's activation side: channel scale, Givens rotations, then the per-(row, 128-column)
// symmetric E4M3 quantiser of r4d_quant_act_fp8.hip. 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 plain MXFP4 GEMM unchanged.
//
// 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 and then quantised
// with ONE scale. 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), each lane takes two pairs a stage, and the absmax is a wave reduction. The
// quantiser after it is quant_act_fp8's, byte for byte.
//
// PARTS. A fused projection (gate|up, q|k|v) was rotated per piece, so a row yields `parts` rotated
// copies, side by side: q is [M, parts * K] and scale [M, parts * K/128]. 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. The rotation is a handful of FMAs on top of that; the
// quantiser's rounding to E4M3 then swallows nearly all of the difference, and rad-kbench-style
// comparisons against the reference allow one code of disagreement per element.
//
// givens_quant_act_i8g is the same producer with an int8 output (scale amax / 127, round to nearest) for
// the int8 prefill GEMMs: after the rotation a group is flat, and 127 uniform levels beat e4m3's three
// mantissa bits for nearly every element.
//
// 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 "r4d_fp8_wmma.h"
#include "r4d_q8.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 and quantises 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.
// Rotate is givens_rotate: the same rotation, no quantiser, the rotated row written as f16 into
// `q` (read as a uint16 plane, y_ld elements a row) for a GEMM that reads a 16-bit activation.
// Fp8 and I8 are the two quantisers (r4d_q8.h: E4M3 with scale amax/448, int8 with amax/127).
enum class GvMode { Rotate, Fp8, I8 };
template <GvMode MODE>
__global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
const uint16_t* __restrict__ x, const int32_t* __restrict__ pairs,
const float* __restrict__ angle, const float* __restrict__ rscale, uint8_t* __restrict__ q,
float* __restrict__ s, uint32_t K, uint32_t nkb, uint32_t parts, uint32_t stages,
uint32_t x_ld, uint32_t q_ld, uint32_t s_ld) {
constexpr bool I8 = MODE == GvMode::I8;
__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);
float v[4] = { r.x, r.y, r.z, r.w };
if constexpr (MODE == GvMode::Rotate) {
// 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*)((uint16_t*)q + (size_t)row * q_ld + (size_t)part * K + c0 + lane * 4u) = o.u;
return;
}
float m = 0.0f;
#pragma unroll
for (int j = 0; j < 4; ++j) m = fmaxf(m, fabsf(v[j]));
m = r4d_wave_max(m);
const float sc = m > 0.0f ? m * (1.0f / q8_top<I8>()) : 1.0f;
if (lane == 0) s[(size_t)row * s_ld + (size_t)part * nkb + g] = sc;
*(uint32_t*)(q + (size_t)row * q_ld + (size_t)part * K + c0 + lane * 4u) =
q8_pack4<I8>(v[0], v[1], v[2], v[3], 1.0f / sc);
}
} // 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
// q [M][parts*K] E4M3
// s [M][parts*K/128] f32
static int givens_quant_launch(long x, long pairs, long angle, long rscale, long q, long s, int M,
int K, int parts, int stages, int x_ld, int q_ld, int s_ld,
const R4dSink& sk, GvMode mode = GvMode::Fp8) {
if (!x || !pairs || !angle || !rscale || !q || (mode != GvMode::Rotate && !s)) return -1;
if (M <= 0 || K <= 0 || parts <= 0 || stages <= 0) return -2;
if (K % (int)kGroup) return -3; // a rotation group cannot straddle the end of a row
if (!x_ld) x_ld = K;
const int nkb = K / (int)kGroup;
if (!q_ld) q_ld = parts * K;
if (!s_ld) s_ld = parts * nkb;
const uint32_t gy = ((uint32_t)(parts * nkb) + kWaves - 1) / kWaves;
#define GV_EMIT(M_) R4D_EMIT(sk, (givens_quant_act_fp8_kernel<M_>), dim3((uint32_t)M, gy), dim3(kThreads), 0, \
(const uint16_t*)x, (const int32_t*)pairs, (const float*)angle, (const float*)rscale, \
(uint8_t*)q, (float*)s, (uint32_t)K, (uint32_t)nkb, (uint32_t)parts, (uint32_t)stages, \
(uint32_t)x_ld, (uint32_t)q_ld, (uint32_t)s_ld)
const int rc = mode == GvMode::I8 ? GV_EMIT(GvMode::I8)
: mode == GvMode::Fp8 ? GV_EMIT(GvMode::Fp8) : GV_EMIT(GvMode::Rotate);
#undef GV_EMIT
if (rc != RAD_OK) return -4;
return (!sk.desc && hipGetLastError() != hipSuccess) ? -4 : 0;
}
extern "C" int r4d_givens_quant_act_fp8_group(void) { return (int)kGroup; }
// ============================================================== the plugin ABI entry point
enum { GV_X = 0, GV_PAIRS = 1, GV_ANGLE = 2, GV_RSCALE = 3, GV_Q = 4, GV_S = 5 };
static int givens_body(const RadArgs* a, const R4dSink& sk, GvMode mode) {
const bool quant = mode != GvMode::Rotate;
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* q = r4d_opt(a, GV_Q);
const RadTensor* sc = quant ? r4d_opt(a, GV_S) : nullptr;
if (!x || !pr || !an || !rs || !q || (quant && !sc)) return R4D_NO(RAD_E_INVAL);
if (x->rank < 2 || q->rank != 2 || (quant && sc->rank != 2) || pr->rank != 2 || an->rank != 2 ||
rs->rank != 2) return R4D_NO(RAD_E_SHAPE);
if (mode == GvMode::Rotate && q->dtype != RAD_F16) return R4D_NO(RAD_E_DTYPE);
if (mode == GvMode::I8 && q->dtype != RAD_I8) 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, as quant_act_fp8 reads them
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 (r4d_geti_or(a, "group", grp) != grp || K % grp || parts < 1 || stages < 1)
return R4D_NO(RAD_E_SHAPE);
if (q->shape[0] != M || q->shape[1] != parts * K) return R4D_NO(RAD_E_SHAPE);
if (quant && (sc->shape[0] != M || sc->shape[1] != parts * nkb)) 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(q->data, 4))
return R4D_NO(RAD_E_ALIGN);
const long long xp = r4d_row_pitch(x), qp = r4d_row_pitch(q), sp = quant ? r4d_row_pitch(sc) : 0;
if (xp < 0 || qp < 0 || sp < 0) return R4D_NO(RAD_E_STRIDE);
const int e = givens_quant_launch(r4d_addr(x), r4d_addr(pr), r4d_addr(an), r4d_addr(rs),
r4d_addr(q), r4d_addr(sc), (int)M, (int)K, (int)parts,
(int)stages, (int)xp, (int)qp, (int)sp, sk, mode);
return e == 0 ? RAD_OK : RAD_E_SHAPE;
}
static int rad_givens_quant_act_fp8_body(const RadArgs* a, const R4dSink& sk) {
return givens_body(a, sk, GvMode::Fp8);
}
static int rad_givens_quant_act_i8g_body(const RadArgs* a, const R4dSink& sk) {
return givens_body(a, sk, GvMode::I8);
}
static int rad_givens_rotate_body(const RadArgs* a, const R4dSink& sk) {
return givens_body(a, sk, GvMode::Rotate);
}
R4D_ENTRY(r4d_rad_givens_quant_act_fp8, rad_givens_quant_act_fp8_body)
R4D_ENTRY(r4d_rad_givens_rotate, rad_givens_rotate_body)
R4D_ENTRY(r4d_rad_givens_quant_act_i8g, rad_givens_quant_act_i8g_body)
+174
View File
@@ -0,0 +1,174 @@
// 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)
+1 -1
View File
@@ -901,7 +901,7 @@ namespace {
/* A ROTATED MXFP4 WEIGHT (ParoQuant): the same two planes -- e2m1 codes, an E8M0 exponent a 32 --
* stored in the basis the "givens" transform names, with the rotation's tables beside them. The
* GEMM reads the codes and exponents exactly as it reads an unrotated weight; the rotation is the
* activation's (givens_quant_act_fp8), and the tables are separate weight operands of that op, so
* activation's (givens_dense_quant), and the tables are separate weight operands of that op, so
* nothing here touches them. What is checked is that they are there and are the kind of
* rotation the quantiser applies: a transform this library does not read would hand the kernel
* codes that decode to other numbers. */
-4
View File
@@ -327,10 +327,6 @@ int r4d_rad_had_quant_act_fp8 (const RadArgs*, RadStream);
int r4d_rad_had_quant_act_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
int r4d_rad_givens_rotate (const RadArgs*, RadStream);
int r4d_rad_givens_rotate_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
int r4d_rad_givens_quant_act_fp8 (const RadArgs*, RadStream);
int r4d_rad_givens_quant_act_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
int r4d_rad_givens_quant_act_i8g (const RadArgs*, RadStream);
int r4d_rad_givens_quant_act_i8g_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
int r4d_rad_rmsnorm_quant_fp8(const RadArgs*, RadStream);
int r4d_rad_gated_quant_fp8 (const RadArgs*, RadStream);
int r4d_rad_gated_quant_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
+8 -61
View File
@@ -1052,10 +1052,10 @@ static const RadOperandSpec oQuantActFp8[] = {
{ "scale", RAD_OPD_OUT, 0 },
};
/* `givens_quant_act_fp8` is ParoQuant's activation side, and libref declared this schema first
* (the loader refuses two that differ). The tables are the "givens" encoding's, as WEIGHT operands;
* see libref/ref_quant.cpp for the contract and r4d_givens_quant_act_fp8.hip for the kernel. */
static const RadParamSpec pGivensQuantAct[] = {
/* `givens_rotate` is ParoQuant's activation side for the 16-bit GEMMs, and libref declared this schema
* first (the loader refuses two that differ). The tables are the "givens" encoding's, as WEIGHT
* operands; see libref/ref_quant.cpp for the contract and r4d_givens_rotate.hip for the kernel. */
static const RadParamSpec pGivensRotate[] = {
{ "M", RAD_P_INT, RAD_REQUIRED },
{ "n", RAD_P_INT, RAD_REQUIRED },
{ "group", RAD_P_INT, RAD_REQUIRED },
@@ -1070,16 +1070,8 @@ static const RadOperandSpec oGivensRotate[] = {
{ "rscale", RAD_OPD_WEIGHT, 0 },
{ "y", RAD_OPD_OUT, 0 },
};
static const RadOperandSpec oGivensQuantAct[] = {
{ "x", RAD_OPD_IN, 0 },
{ "pairs", RAD_OPD_WEIGHT, 0 },
{ "angle", RAD_OPD_WEIGHT, 0 },
{ "rscale", RAD_OPD_WEIGHT, 0 },
{ "q", RAD_OPD_OUT, 0 },
{ "scale", RAD_OPD_OUT, 0 },
};
/* `givens_dense_quant` is givens_quant_act_fp8 with the rotation done by a dense per-group matrix on the
/* `givens_dense_quant` is ParoQuant's activation quantiser, the rotation done by a dense per-group matrix on the
* matrix cores (r4d_givens_dense.hip). The matrices are the weight -- rad-convert composes them from the
* givens tables (r4d_rad_layout_gdense) -- so its tables are `tdense` and the channel scale `rscale`.
* `out` is the quantiser: "i8" (scale amax / 127) or "fp8" (E4M3, amax / 448), as q's dtype says. */
@@ -2352,31 +2344,17 @@ static const RadOpSchema kSchemas[] = {
SCHEMA("had_quant_act_i8g", pQuantActFp8, oQuantActFp8,
"quant_act_i8g with a `group`-wide Hadamard along K first; pairs with an i8 weight stored "
"rotated, its scale carrying the residual factor of `group`."),
SCHEMA("givens_rotate", pGivensQuantAct, oGivensRotate,
"givens_quant_act_fp8 without the quantiser: y = x / rscale rotated by `stages` rounds of "
SCHEMA("givens_rotate", pGivensRotate, oGivensRotate,
"ParoQuant's rotation without a quantiser: y = x / rscale rotated by `stages` rounds of "
"Givens pairs, `parts` copies side by side ([M, parts * n]) in y's own dtype. For a weight "
"whose GEMM reads a 16-bit activation. `group` is the width a pair stays inside."),
SCHEMA("givens_quant_act_fp8", pGivensQuantAct, oGivensQuantAct,
"ParoQuant's activation quantiser: x is divided by the channel scale `rscale`, rotated by "
"`stages` rounds of disjoint Givens pairs (a' = a cos t + b sin t, b' = b cos t - a sin t, "
"pairs as column indices of the row, each inside one scale group), then quantised as "
"quant_act_fp8 does: scale = amax / 448 per `group`. `parts` rotated copies are written "
"side by side -- q is [M, parts * n], scale [M, parts * n / group] -- part p from table "
"rows [p * stages, (p + 1) * stages) of `pairs` and `angle` and row p of `rscale`: a "
"fused projection's pieces were each rotated on their own. Only a weight stored with "
"the \"givens\" transform reads the result; it is NOT quant_act_fp8's output in either "
"direction."),
SCHEMA("givens_dense_quant", pGivensDense, oGivensDense,
"ParoQuant's activation quantiser with the rotation as a dense per-group matrix: y = T (x / "
"rscale) for each of `parts` rotated copies, T the [128, 128] f16 matrix a (part, 128-column "
"group) that its Givens stages compose to, then the block-scaled quantiser `out` names "
"(\"i8\": scale amax / 127, round to nearest; \"fp8\": E4M3, amax / 448). q is [M, parts * n] "
"and scale [M, parts * n / group]; tdense is [parts * n, 128]. The same activation as "
"givens_quant_act_i8g / _fp8 up to the f16 rounding of T."),
SCHEMA("givens_quant_act_i8g", pGivensQuantAct, oGivensQuantAct,
"givens_quant_act_fp8 with an int8 output: the same channel scale and rotations, then "
"quant_act_i8g's quantiser (scale = amax / 127 per `group`, round to nearest). For the int8 "
"prefill GEMMs of a weight stored with the \"givens\" transform."),
"the sequential rotation, up to the f16 rounding of T."),
SCHEMA("had_quant_act_fp8", pQuantActFp8, oQuantActFp8,
"the same, with a `group`-wide Hadamard along K applied BEFORE the absmax. Pairs with "
"an expert weight stored at dtype w4a8h, which carries the matching rotation and the "
@@ -5257,37 +5235,6 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, r4d_rad_had_quant_act_fp8, nullptr, nullptr, nullptr,
r4d_shape_quant_act_fp8, nullptr, nullptr, r4d_rad_had_quant_act_fp8_describe },
/* ParoQuant's rotation and the quantiser in one pass, because the rotation changes the maximum the
* scale is taken over and so cannot be a separate launch ahead of quant_act_fp8 without writing
* and re-reading the rotated row. It exists for the MXFP4 weight stored rotated and for nothing
* else. */
{ "givens_quant_act_fp8", "givens_quant_act_fp8", "quant",
"the channel scale, ParoQuant's Givens rotations, then per-(row, 128-column) symmetric E4M3 "
"quantisation, once for each rotated part of a fused projection",
"n a multiple of 128 -- a rotation group cannot straddle the end of a row -- and group 128, "
"which is both the rotation group and the weight scale's activation block",
"bf16 in, E4M3 out in PLAIN ROW-MAJOR order, parts side by side, with an f32 scale per (row, part, "
"128 columns); the tables are the weight's int32 pairs (checked at load to pair each group's own "
"columns), f32 angles and f32 channel scales",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cHadQuantActFp8), NOTUNE,
nullptr, nullptr, r4d_rad_givens_quant_act_fp8, nullptr, r4d_rad_layout_givens_pairs,
r4d_rad_relayout_givens_pairs, nullptr, r4d_rad_unrelayout_givens_pairs, nullptr,
r4d_rad_givens_quant_act_fp8_describe },
{ "givens_quant_act_i8g", "givens_quant_act_i8g", "quant",
"the channel scale, ParoQuant's Givens rotations, then per-(row, 128-column) symmetric int8 "
"quantisation, once for each rotated part of a fused projection",
"n a multiple of 128 and group 128, as givens_quant_act_fp8's",
"bf16 in, int8 out in PLAIN ROW-MAJOR order, parts side by side, with an f32 scale of amax/127 per "
"(row, part, 128 columns); the tables are the weight's int32 pairs, f32 angles and f32 channel "
"scales",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cHadQuantActFp8), NOTUNE,
nullptr, nullptr, r4d_rad_givens_quant_act_i8g, nullptr, r4d_rad_layout_givens_pairs,
r4d_rad_relayout_givens_pairs, nullptr, r4d_rad_unrelayout_givens_pairs, nullptr,
r4d_rad_givens_quant_act_i8g_describe },
{ "givens_dense_quant", "givens_dense_quant", "quant",
"ParoQuant's rotation as a dense 128 x 128 f16 matrix a (part, group) on the matrix cores, then "
"per-(row, 128-column) symmetric quantisation to int8 or E4M3, once for each rotated part",
+3 -22
View File
@@ -3776,8 +3776,7 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
* the group's maximum, so 7% of it is a bound a wrong pair or a wrong angle cannot meet). */
static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, int stages,
bool rot_only = false, bool i8 = false, bool dense = false) {
const char* row = dense ? "givens_dense_quant" : rot_only ? "givens_rotate"
: i8 ? "givens_quant_act_i8g" : "givens_quant_act_fp8";
const char* row = dense ? "givens_dense_quant" : "givens_rotate";
if (!sel(row)) return;
char nm[160];
std::snprintf(nm, sizeof nm, "%s %s numeric M=%lld K=%lld parts=%d stages=%d", row,
@@ -13182,17 +13181,6 @@ static void device_phase(const Plugin& pl) {
givens_numeric(pl, 300, 6144, 2, 8, true);
}
if (sel("givens_quant_act_i8g")) {
if (!g_case) std::printf("\nParoQuant's rotated activation, int8 output, against libm's rotation\n");
givens_numeric(pl, 1, 128, 1, 8, false, true);
givens_numeric(pl, 3, 5120, 1, 8, false, true);
givens_numeric(pl, 5, 5120, 2, 8, false, true);
givens_numeric(pl, 64, 17408, 1, 8, false, true);
givens_numeric(pl, 300, 6144, 2, 8, false, true);
givens_numeric(pl, 2048, 5120, 1, 8, false, true);
givens_numeric(pl, 2048, 5120, 2, 8, false, true);
givens_numeric(pl, 2048, 17408, 1, 8, false, true);
}
if (sel("givens_dense_quant")) {
if (!g_case) std::printf("\nParoQuant's rotation as dense per-group matrices, int8 and E4M3 output, against libm's rotation\n");
for (int i8 = 0; i8 < 2; ++i8) {
@@ -13203,18 +13191,11 @@ static void device_phase(const Plugin& pl) {
givens_numeric(pl, 300, 6144, 2, 8, false, i8, true);
givens_numeric(pl, 2048, 5120, 1, 8, false, i8, true);
givens_numeric(pl, 2048, 5120, 2, 8, false, i8, true);
givens_numeric(pl, 64, 17408, 1, 8, false, i8, true);
givens_numeric(pl, 2048, 17408, 1, 8, false, i8, true);
givens_numeric(pl, 7, 5120, 3, 2, false, i8, true);
}
}
if (sel("givens_quant_act_fp8")) {
if (!g_case) std::printf("\nParoQuant's rotate-and-quantise, against an f64 rotation\n");
givens_numeric(pl, 1, 128, 1, 8);
givens_numeric(pl, 3, 5120, 1, 8);
givens_numeric(pl, 5, 5120, 2, 8);
givens_numeric(pl, 64, 17408, 1, 8);
givens_numeric(pl, 300, 6144, 2, 8);
givens_numeric(pl, 7, 5120, 3, 2);
}
if (sel("gram_accum")) {
if (!g_case) std::printf("\nthe GPTQ calibration accumulator, against an f64 host Gram\n");