Archived
libr4d: gelu_mul_quant_i8 -- gelu(gate)*up and the w4a8 activation quantiser in one pass
The down projection of a GELU-gated feed-forward on a w4a8 grid is fed by gelu_mul then quant_act_i8, two single-row dispatches at a decode batch. gelu_mul_quant_i8 is the pair in one pass, byte-identical to it: the product formed as gelu_mul_kernel forms it (tanh form, bf16 store), then quant_act_i8's partition. The selftest compares it against the pair at M 1/4/40.
This commit is contained in:
1 parent
d90d90e43a
commit
0a4cf9b893
6 files changed
+185
No files matched your search
@@ -388,6 +388,9 @@ spelling of `act`, not the vocabulary's.
|
||||
| `gated_had_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate_up` → `q`, `scale`, `out_bf16`? |
|
||||
| `gate_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate`, `x` → `q`, `scale`, `out_bf16`? |
|
||||
| `rmsnorm_quant_i8` | `M` `n` `eps`:f64 `dtype` `wadd`:f64/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`?; `w_post`w?, `scalar`w? |
|
||||
| `gelu_mul_quant_i8` | `M` `n` `dtype` | `gate_up` → `q`, `scale`, `out_bf16`? |
|
||||
|
||||
`gelu_mul_quant_i8` is `gelu_mul` with the same quantiser on its pass, byte-identical to the pair.
|
||||
|
||||
`rmsnorm_quant_i8` is `rmsnorm` with `quant_act_i8`'s quantiser on its pass — fragment-order int8
|
||||
codes and one f32 scale per row, the grid `gemm_nt_q` reads at `dtype=w4a8`. With `residual` the
|
||||
|
||||
@@ -143,6 +143,7 @@ set(R4D_UNITS
|
||||
"r4d_quant_act_i8|"
|
||||
"r4d_rmsnorm_quant_i8|"
|
||||
"r4d_qkv_norm_rope|"
|
||||
"r4d_gelu_mul_quant_i8|"
|
||||
"r4d_had_quant_act_i8|"
|
||||
"r4d_rmsnorm_had_quant_i8|"
|
||||
"r4d_ar_ln_had_quant_i8|"
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// r4d_gelu_mul_quant_i8.hip - r4d_gelu_mul_quant_i8: gelu(gate) * up and the per-row int8 quantiser
|
||||
// r4d_gemm_w4a8_nt_m64 reads, in one pass over the row.
|
||||
//
|
||||
// The down projection's input of a GELU-gated feed-forward (Gemma) on a w4a8 grid: unfused it is
|
||||
// gelu_mul and quant_act_i8, two single-row dispatches at a decode batch. This is the pair as one,
|
||||
// BYTE-IDENTICAL to it: the product is formed exactly as gelu_mul_kernel forms it (the tanh form,
|
||||
// the same constants, rounded to bf16), and the absmax, scale and codes are quant_act_i8's over
|
||||
// those bf16 values -- the absmax is a max and so takes no partition, and every code is a function
|
||||
// of its own value and the row scale.
|
||||
#include "r4d.h"
|
||||
#include "r4d_args.h"
|
||||
#include "r4d_emit.h"
|
||||
#include "r4d_common.h"
|
||||
#include <cstdint>
|
||||
|
||||
namespace {
|
||||
|
||||
__device__ __forceinline__ float bf2f(uint16_t h) { return __uint_as_float((uint32_t)h << 16); }
|
||||
|
||||
template <uint32_t THR>
|
||||
__global__ __launch_bounds__(THR) void gelu_mul_quant_i8_kernel(
|
||||
const uint16_t* __restrict__ gu, signed char* __restrict__ q, float* __restrict__ S,
|
||||
uint16_t* __restrict__ yo, uint32_t n, uint32_t gu_ld, uint32_t y_ld) {
|
||||
extern __shared__ uint16_t ybuf[];
|
||||
__shared__ float lm[THR / 32];
|
||||
const uint32_t row = blockIdx.x;
|
||||
const uint16_t* __restrict__ gr = gu + (size_t)row * gu_ld;
|
||||
signed char* __restrict__ qr = q + (size_t)row * n;
|
||||
|
||||
float m = 0.0f;
|
||||
for (uint32_t i = threadIdx.x; i < n; i += THR) {
|
||||
const float g = bf2f(gr[i]);
|
||||
const float k0 = 0.7978845608028654f, k1 = 0.044715f;
|
||||
const float gelu = 0.5f * g * (1.0f + tanhf(k0 * (g + k1 * g * g * g)));
|
||||
const uint16_t y = f32_to_bf16(gelu * bf2f(gr[n + i]));
|
||||
ybuf[i] = y;
|
||||
if (yo) yo[(size_t)row * y_ld + i] = y;
|
||||
m = fmaxf(m, fabsf(bf2f(y)));
|
||||
}
|
||||
m = r4d_wave_max(m);
|
||||
if ((threadIdx.x & 31) == 0) lm[threadIdx.x >> 5] = m;
|
||||
__syncthreads(); // also publishes ybuf to every thread
|
||||
m = lm[0];
|
||||
#pragma unroll
|
||||
for (uint32_t i = 1; i < THR / 32; i++) m = fmaxf(m, lm[i]);
|
||||
const float scale = fmaxf(m, 1e-8f) / 127.f;
|
||||
const float inv = 1.f / scale;
|
||||
if (threadIdx.x == 0) S[row] = scale;
|
||||
// quant_act_i8's fragment order, see r4d_quant_act_i8.hip
|
||||
for (uint32_t i = threadIdx.x; i < n; i += THR) {
|
||||
const uint32_t s = i >> 4, j = i & 15;
|
||||
const uint32_t src = (s << 4) + 8 * ((j & 7) >> 2) + 4 * (j >> 3) + (j & 3);
|
||||
const int t = __float2int_rn(bf2f(ybuf[src]) * inv);
|
||||
qr[i] = (signed char)(t < -128 ? -128 : (t > 127 ? 127 : t));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Operands (r4d_rows.cpp, oGeluMulQuantI8): gate_up [M][2n] bf16 (gate first), q [M][n] int8 in the
|
||||
// fragment order, scale [M][1] f32, out_bf16 [M][n] bf16 optional.
|
||||
enum { GM_GU = 0, GM_Q, GM_S, GM_Y };
|
||||
|
||||
static int rad_gelu_mul_quant_i8_body(const RadArgs* a, const R4dSink& sk) {
|
||||
int emit_rc = RAD_OK;
|
||||
const RadTensor* gu = r4d_opt(a, GM_GU);
|
||||
const RadTensor* q = r4d_opt(a, GM_Q);
|
||||
const RadTensor* s = r4d_opt(a, GM_S);
|
||||
const RadTensor* y = r4d_opt(a, GM_Y);
|
||||
if (!gu || !q || !s) return R4D_NO(RAD_E_INVAL);
|
||||
if (gu->dtype != RAD_BF16 || q->dtype != RAD_I8 || s->dtype != RAD_F32) return R4D_NO(RAD_E_DTYPE);
|
||||
long long M = 0, n = 0;
|
||||
if (r4d_rows_of(q, &M, &n) != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
||||
if (n % 16 || !r4d_contig(q)) return R4D_NO(RAD_E_SHAPE);
|
||||
long long Mg = 0;
|
||||
const long long gp = r4d_rows_pitch(gu, 2 * n, &Mg);
|
||||
if (gp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (Mg < M) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_numel(s) != M || !r4d_contig(s)) return R4D_NO(RAD_E_SHAPE);
|
||||
if (y && r4d_numel(y) != M * n) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long yp = y ? r4d_row_pitch(y) : n;
|
||||
if (yp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
const size_t lds = (size_t)n * sizeof(uint16_t);
|
||||
if (lds > 60 * 1024) return R4D_NO(RAD_E_SHAPE);
|
||||
const int thr = r4d_act_threads((int)M, (int)n);
|
||||
#define R4D_GMQ_T(T) emit_rc = R4D_EMIT(sk, (gelu_mul_quant_i8_kernel<T>), dim3((uint32_t)M), dim3(T), lds, \
|
||||
(const uint16_t*)r4d_p(gu), (signed char*)r4d_wp(q), (float*)r4d_wp(s), (uint16_t*)r4d_wp(y), \
|
||||
(uint32_t)n, (uint32_t)gp, (uint32_t)yp)
|
||||
if (thr == 1024) R4D_GMQ_T(1024);
|
||||
else if (thr == 512) R4D_GMQ_T(512);
|
||||
else if (thr == 256) R4D_GMQ_T(256);
|
||||
else return R4D_NO(RAD_E_INVAL);
|
||||
#undef R4D_GMQ_T
|
||||
return emit_rc;
|
||||
}
|
||||
R4D_ENTRY(r4d_rad_gelu_mul_quant_i8, rad_gelu_mul_quant_i8_body)
|
||||
@@ -303,6 +303,8 @@ int r4d_rad_quant_act_i8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_quant_act_i8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_qkv_norm_rope (const RadArgs*, RadStream);
|
||||
int r4d_rad_qkv_norm_rope_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_gelu_mul_quant_i8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gelu_mul_quant_i8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_rmsnorm_quant_i8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_rmsnorm_quant_i8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_had_quant_act_i8 (const RadArgs*, RadStream);
|
||||
|
||||
@@ -1370,6 +1370,20 @@ static const RadOperandSpec oQkvNormRope[] = {
|
||||
{ "k_w", RAD_OPD_WEIGHT, 0 },
|
||||
};
|
||||
|
||||
/* `gelu_mul_quant_i8` is `gelu_mul` with quant_act_i8's quantiser on its pass: the down projection
|
||||
* input of a GELU-gated feed-forward on the w4a8 grid, byte-identical to the pair. */
|
||||
static const RadParamSpec pGeluMulQuantI8[] = {
|
||||
{ "M", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "n", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "dtype", RAD_P_STR, RAD_REQUIRED },
|
||||
};
|
||||
static const RadOperandSpec oGeluMulQuantI8[] = {
|
||||
{ "gate_up", RAD_OPD_IN, 0 },
|
||||
{ "q", RAD_OPD_OUT, 0 },
|
||||
{ "scale", RAD_OPD_OUT, 0 },
|
||||
{ "out_bf16", RAD_OPD_OUT, 1 },
|
||||
};
|
||||
|
||||
static const RadParamSpec pGatedQuantFp8[] = {
|
||||
{ "M", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "n", RAD_P_INT, RAD_REQUIRED },
|
||||
@@ -2366,6 +2380,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
SCHEMA("qkv_norm_rope", pQkvNormRope, oQkvNormRope,
|
||||
"per-head q and k RMS norms, the weightless v norm and the rotary of q and k over separate "
|
||||
"q, k and v projections, in one dispatch; v from the raw k under v_from_k."),
|
||||
SCHEMA("gelu_mul_quant_i8", pGeluMulQuantI8, oGeluMulQuantI8,
|
||||
"gelu(gate) * up (tanh form) and the per-row int8 quantiser the w4a8 skinny GEMM reads, in "
|
||||
"one pass."),
|
||||
SCHEMA("gated_quant_fp8", pGatedQuantFp8, oGatedQuantFp8,
|
||||
"silu(a)*b or sigmoid(a)*b, then the same quantiser, in one pass."),
|
||||
SCHEMA("gated_had_quant_fp8", pGatedQuantFp8, oGatedQuantFp8,
|
||||
@@ -3229,6 +3246,9 @@ static const RadConstraint cRmsNormQuantI8[] = {
|
||||
static const RadConstraint cQkvNormRope[] = {
|
||||
C_GE("head_dim", 256), C_LE("head_dim", 512), C_DIV("head_dim", 256), C_IN("dtype", "bf16"),
|
||||
};
|
||||
static const RadConstraint cGeluMulQuantI8[] = {
|
||||
C_DIV("n", 16), C_IN("dtype", "bf16"),
|
||||
};
|
||||
static const RadConstraint cHadQuantActI8[] = {
|
||||
C_DIV("n", 16), C_DIV("n", 128), C_LE("group", 512), C_IN("dtype", "bf16"),
|
||||
};
|
||||
@@ -5637,6 +5657,16 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_qkv_norm_rope, nullptr, nullptr, nullptr,
|
||||
nullptr, nullptr, nullptr, r4d_rad_qkv_norm_rope_describe },
|
||||
|
||||
{ "gelu_mul_quant_i8", "gelu_mul_quant_i8", "quant",
|
||||
"gelu(gate) * up, the tanh form, and quant_act_i8's quantiser in one pass over the row",
|
||||
"n a multiple of 16 and n*2 bytes within LDS; one workgroup per row",
|
||||
"bf16 gate_up in, int8 out in the w4a8 GEMM's fragment order with an f32 per-row scale, optional "
|
||||
"bf16 out; the codes taken over the bf16-rounded product",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cGeluMulQuantI8), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_gelu_mul_quant_i8, nullptr, nullptr, nullptr,
|
||||
nullptr, nullptr, nullptr, r4d_rad_gelu_mul_quant_i8_describe },
|
||||
|
||||
{ "had_quant_act_i8", "had_quant_act_i8", "quant",
|
||||
"block Hadamard along K then per-row symmetric int8 quantisation; the rotation is what makes "
|
||||
"int8 activations usable at all: unrotated, the per-row absmax is set by a few outlier channels "
|
||||
|
||||
@@ -4922,6 +4922,55 @@ static void fused_quant_fp8_numeric(const Plugin& pl) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- gelu_mul_quant_i8, against gelu_mul + quant_act_i8 -------------------------------------
|
||||
* Gemma 4 31B's per-rank feed-forward width (10752 of 21504 at two ranks), both batch widths. */
|
||||
static void gelu_mul_quant_i8_numeric(const Plugin& pl) {
|
||||
const RadKernelInfo* gm = find(pl, "gelu_mul_bf16");
|
||||
const RadKernelInfo* qz = find(pl, "quant_act_i8");
|
||||
const RadKernelInfo* fz = find(pl, "gelu_mul_quant_i8");
|
||||
if (!gm || !qz || !fz) { ok("gelu_mul_quant_i8 vs its pair", false, "rows absent"); return; }
|
||||
for (const int64_t M : { (int64_t)1, (int64_t)4, (int64_t)40 }) {
|
||||
const int64_t n = 10752;
|
||||
std::vector<uint16_t> hgu((size_t)M * 2 * n);
|
||||
for (size_t i = 0; i < hgu.size(); ++i)
|
||||
hgu[i] = h_to_bf16(std::ldexp(1.0f, (int)((i / 512) % 5) - 2) * (0.029f * (float)((i * 41) % 103) - 1.5f));
|
||||
void* dgu = dalloc(hgu.size() * 2);
|
||||
void* dy[2] = { dalloc((size_t)M * n * 2), dalloc((size_t)M * n * 2) };
|
||||
void* dq[2] = { dalloc((size_t)M * n), dalloc((size_t)M * n) };
|
||||
void* ds[2] = { dalloc((size_t)M * 4), dalloc((size_t)M * 4) };
|
||||
if (!dgu || !dy[1] || !dq[1] || !ds[1]) { ok("gelu_mul_quant_i8 alloc", false, "hipMalloc failed"); return; }
|
||||
st_memcpy(dgu, hgu.data(), hgu.size() * 2, hipMemcpyHostToDevice);
|
||||
int rc = launch_row(gm, { T(dgu, RAD_BF16, { M, 2 * n }), T(dy[0], RAD_BF16, { M, n }) },
|
||||
{ PI("M", M), PI("n", n), PS("dtype", "bf16") });
|
||||
if (rc == RAD_OK)
|
||||
rc = launch_row(qz, { T(dy[0], RAD_BF16, { M, n }), T(dq[0], RAD_I8, { M, n }), T(ds[0], RAD_F32, { M }),
|
||||
T(nullptr, RAD_F32, {}) },
|
||||
{ PI("M", M), PI("n", n), PI("group", n), PS("dtype", "bf16") });
|
||||
if (rc == RAD_OK)
|
||||
rc = launch_row(fz, { T(dgu, RAD_BF16, { M, 2 * n }), T(dq[1], RAD_I8, { M, n }), T(ds[1], RAD_F32, { M }),
|
||||
T(dy[1], RAD_BF16, { M, n }) },
|
||||
{ PI("M", M), PI("n", n), PS("dtype", "bf16") });
|
||||
const std::string what = "gelu_mul_quant_i8 M=" + std::to_string(M);
|
||||
if (rc != RAD_OK) { ok((what + " vs gelu_mul + quant_act_i8").c_str(), false, "rc=" + std::to_string(rc)); continue; }
|
||||
std::vector<uint16_t> y0((size_t)M * n), y1((size_t)M * n);
|
||||
std::vector<uint8_t> q0((size_t)M * n), q1((size_t)M * n);
|
||||
std::vector<float> s0((size_t)M), s1((size_t)M);
|
||||
st_memcpy(y0.data(), dy[0], y0.size() * 2, hipMemcpyDeviceToHost);
|
||||
st_memcpy(y1.data(), dy[1], y1.size() * 2, hipMemcpyDeviceToHost);
|
||||
st_memcpy(q0.data(), dq[0], q0.size(), hipMemcpyDeviceToHost);
|
||||
st_memcpy(q1.data(), dq[1], q1.size(), hipMemcpyDeviceToHost);
|
||||
st_memcpy(s0.data(), ds[0], s0.size() * 4, hipMemcpyDeviceToHost);
|
||||
st_memcpy(s1.data(), ds[1], s1.size() * 4, hipMemcpyDeviceToHost);
|
||||
int ny = 0, nq = 0, ns = 0, nz = 0;
|
||||
for (size_t i = 0; i < y0.size(); ++i) ny += y0[i] != y1[i];
|
||||
for (size_t i = 0; i < q0.size(); ++i) { nq += q0[i] != q1[i]; nz += q1[i] != 0; }
|
||||
for (size_t i = 0; i < s0.size(); ++i) ns += std::memcmp(&s0[i], &s1[i], 4) != 0;
|
||||
ok((what + " is byte-identical to gelu_mul + quant_act_i8").c_str(), ny == 0 && nq == 0 && ns == 0 && nz > (int)q1.size() / 2,
|
||||
"bf16 differs " + std::to_string(ny) + ", codes " + std::to_string(nq) + ", scales " + std::to_string(ns) +
|
||||
", nonzero codes " + std::to_string(nz));
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- qkv_norm_rope, against the five ops it replaces ------------------------------------------
|
||||
*
|
||||
* rmsnorm_p (v, or the raw k under v_from_k), rmsnorm over the q heads and over the k heads, then
|
||||
@@ -13559,6 +13608,10 @@ static void device_phase(const Plugin& pl) {
|
||||
if (!g_case) std::printf("\nthe fp8 fusions, against the pair each one replaces\n");
|
||||
fused_quant_fp8_numeric(pl);
|
||||
}
|
||||
if (sel("gelu_mul_quant_i8")) {
|
||||
if (!g_case) std::printf("\ngelu_mul_quant_i8, against the pair it replaces\n");
|
||||
gelu_mul_quant_i8_numeric(pl);
|
||||
}
|
||||
if (sel("qkv_norm_rope_bf16")) {
|
||||
if (!g_case) std::printf("\nqkv_norm_rope, against the five ops it replaces\n");
|
||||
qkv_norm_rope_numeric(pl);
|
||||
|
||||
Reference in new issue
Block a user