libr4d: int8 prefill band for ParoQuant int4/int5 (gemm_w4a8i8/w5a8i8 tiled, givens_quant_act_i8g); LinearFP8 uses it above 64 rows

This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent cfe03f8ad8
commit 6091039926
16 files changed
+709 -64

No files matched your search

+35 -29
View File
@@ -26,12 +26,17 @@
// 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 {
@@ -58,14 +63,18 @@ __device__ __forceinline__ void wave_sync() {
// 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.
// QUANT false is givens_rotate: the same rotation, no quantiser, the rotated row written as f16 into
// 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.
template <bool QUANT>
// 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;
@@ -126,7 +135,7 @@ __global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
const float4 r = *(const float4*)(buf + lane * 4u);
float v[4] = { r.x, r.y, r.z, r.w };
if constexpr (!QUANT) {
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;
@@ -139,15 +148,10 @@ __global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
#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 / 448.0f) : 1.0f;
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;
const float inv = 1.0f / sc;
int pk = 0;
pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(v[0] * inv, -448.0f), 448.0f),
fminf(fmaxf(v[1] * inv, -448.0f), 448.0f), pk, false);
pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(v[2] * inv, -448.0f), 448.0f),
fminf(fmaxf(v[3] * inv, -448.0f), 448.0f), pk, true);
*(uint32_t*)(q + (size_t)row * q_ld + (size_t)part * K + c0 + lane * 4u) = (uint32_t)pk;
*(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
@@ -160,8 +164,8 @@ __global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
// 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, bool quant = true) {
if (!x || !pairs || !angle || !rscale || !q || (quant && !s)) return -1;
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;
@@ -169,17 +173,13 @@ static int givens_quant_launch(long x, long pairs, long angle, long rscale, long
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;
const int rc = quant
? R4D_EMIT(sk, givens_quant_act_fp8_kernel<true>, 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)
: R4D_EMIT(sk, givens_quant_act_fp8_kernel<false>, 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);
#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;
}
@@ -189,7 +189,8 @@ 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, bool quant) {
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);
@@ -199,7 +200,8 @@ static int givens_body(const RadArgs* a, const R4dSink& sk, bool quant) {
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 (!quant && q->dtype != RAD_F16) return R4D_NO(RAD_E_DTYPE);
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
@@ -220,14 +222,18 @@ static int givens_body(const RadArgs* a, const R4dSink& sk, bool quant) {
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, quant);
(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, true);
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, false);
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)