Archived
Rotated Qwen3.8-27B checkpoints: ParoQuant MXFP4, int5 bit-plane and z-lab's int4 import (the givens
transform with parts, per-row scales, zero points), the w5a16/w4a16_asym decode GEMMs, the
w4a8i8/w5a8i8 prefill band, the givens_rotate kernel, and the dense per-group rotation
(givens_dense_quant: one 128x128 f16 matrix a part and group on the matrix cores) for the quantising
rotation in prefill. gemm_nt_q gains part bounds (pb1/pb2), an op-schema change an out-of-tree
kernel library has to follow.
Fixes on top: the dense matrices are a container table ("t.dense") that rad-convert composes in f64
from the pairs and angles, and the loader only moves its bits. A container converted before this is
refused at load, naming the re-convert (--reuse copies every unchanged weight). A gemm_nt_q row that
does not read part bounds refuses them instead of ignoring them. libavx implements givens_rotate and
givens_dense_quant.
This commit is contained in:
commit
f6a9467cb5
40 files changed
+4206
-143
No files matched your search
+270
-2
@@ -327,6 +327,18 @@ void sz_write(const WGeom& g, const RadTensor* pl, int zfixed, uint32_t* sz) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Every zero point a code of `bits` bits: what an asymmetric grid's zero is, and what the int8
|
||||
* prefill kernel reads back out of the scale dword (r4d_gemm_wpa8i8_tiled.hip takes five bits of it,
|
||||
* where the f16 kernel takes the whole value -- a larger zero would make the two disagree). */
|
||||
bool sz_zeros_fit(const WGeom& g, const RadTensor& zp, int bits) {
|
||||
const long long nz = g.N * (g.K / g.group);
|
||||
for (long long i = 0; i < nz; ++i) {
|
||||
const int z = zero_at(zp, i);
|
||||
if (z < 0 || z >= (1 << bits)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void sz_read(const WGeom& g, const uint32_t* sz, const RadTensor* pl, bool asym) {
|
||||
const long long nt = g.N / R4D_NTILE, nsz = g.K / g.group;
|
||||
for (long long t = 0; t < nt; ++t)
|
||||
@@ -342,6 +354,26 @@ void sz_read(const WGeom& g, const uint32_t* sz, const RadTensor* pl, bool asym)
|
||||
}
|
||||
}
|
||||
|
||||
/* A ROTATED asymmetric grid (ParoQuant): codes of `codes` dtype, an f16 scale and a u8 zero point a
|
||||
* group, then a `givens` transform and its tables -- the same bytes as the unrotated grid, the
|
||||
* rotation being the activation's. */
|
||||
static bool paro_asym_enc(const RadEncoding* e, uint32_t codes, long long group) {
|
||||
if (!e || !rad_enc_is(e, "affine") || e->n_planes < 3) return false;
|
||||
const bool giv = std::strncmp(e->transform, "givens", RAD_ENC_STR) == 0;
|
||||
if (!giv && e->transform[0]) return false;
|
||||
if (!giv && e->n_planes != 3) return false;
|
||||
if (giv && (!rad_enc_valid(e) || rad_enc_find(e, "t.pairs") < 0)) return false;
|
||||
if (std::strncmp(e->plane[0].role, "codes", RAD_ENC_STR) != 0 || e->plane[0].dtype != codes ||
|
||||
e->plane[0].block[0] != 1 || e->plane[0].block[1] != 1) return false;
|
||||
const int s = rad_enc_find(e, "scale"), z = rad_enc_find(e, "zero");
|
||||
if (s != 1 || z != 2) return false;
|
||||
return e->plane[s].dtype == RAD_F16 && e->plane[s].kind == RAD_PLANE_TILED &&
|
||||
e->plane[s].block[0] == 1 && e->plane[s].block[1] == group &&
|
||||
(e->plane[z].dtype == RAD_U8 || e->plane[z].dtype == codes) &&
|
||||
e->plane[z].kind == RAD_PLANE_TILED && e->plane[z].block[0] == 1 &&
|
||||
e->plane[z].block[1] == group;
|
||||
}
|
||||
|
||||
/* ---- w4: a symmetric grid is i4 codes with an f16 scale; `_asym` is u4 codes with a zero. */
|
||||
int w4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out, WGeom* gout) {
|
||||
@@ -352,7 +384,8 @@ int w4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
|
||||
if (g.K % R4D_W4_KPB) return RAD_E_SHAPE;
|
||||
const bool asym = w4_asym(p, n_p);
|
||||
const bool ok = asym ? (r4d_enc::affine(enc, RAD_U4, RAD_F16, 1, g.group, RAD_U8) ||
|
||||
r4d_enc::affine(enc, RAD_U4, RAD_F16, 1, g.group, RAD_U4))
|
||||
r4d_enc::affine(enc, RAD_U4, RAD_F16, 1, g.group, RAD_U4) ||
|
||||
paro_asym_enc(enc, RAD_U4, g.group))
|
||||
: r4d_enc::affine(enc, RAD_I4, RAD_F16, 1, g.group);
|
||||
if (!ok) return RAD_E_DTYPE;
|
||||
if (gout) *gout = g;
|
||||
@@ -395,6 +428,9 @@ extern "C" int r4d_rad_relayout_w4(const RadParam* p, int n_p, int operand, cons
|
||||
if (dst_bytes != L.bytes) return RAD_E_SHAPE;
|
||||
std::memset(dst, 0, (size_t)dst_bytes);
|
||||
if (operand == Q_BSCALE) {
|
||||
/* a ROTATED (ParoQuant) grid's zero is a 4-bit code */
|
||||
if (w4_asym(p, n_p) && std::strncmp(enc->transform, "givens", RAD_ENC_STR) == 0 &&
|
||||
!sz_zeros_fit(g, pl[1], 4)) return RAD_E_FORMAT;
|
||||
sz_write(g, pl, w4_asym(p, n_p) ? -1 : 8, (uint32_t*)dst);
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -437,6 +473,214 @@ extern "C" int r4d_rad_unrelayout_w4(const RadParam* p, int n_p, int operand,
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ---- w5: ParoQuant's int5, asymmetric: u5 codes, an f16 scale and a u8 zero point (0..31) a 128 of K.
|
||||
* The blob is w4's fragment order for the low four bits of every code (offset binary, no sign flip)
|
||||
* followed by a plane of 32-bit words holding the fifth bits, one a (n-tile, 64 of K, lane): byte s
|
||||
* is k step s of the block and bit e the fragment element e -- the element whose nibble is
|
||||
* 2e (e < 4) or 2(e-4)+1 in dword s of that lane's 16 bytes (r4d_gemm_w5a16_nt_m64.hip). The scale
|
||||
* operand is w4u's (scale, zero) dword plane. A ROTATED weight (a `givens` transform and its
|
||||
* tables beside the first three planes) is the same bytes: the rotation is the activation's. */
|
||||
namespace {
|
||||
bool w5_enc(const RadEncoding* e, long long group) { return paro_asym_enc(e, RAD_U5, group); }
|
||||
|
||||
int w5_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out, WGeom* gout) {
|
||||
if (!out || !pl) return RAD_E_INVAL;
|
||||
WGeom g;
|
||||
int rc = wgeom(p, n_p, R4D_W4_GROUP, &g);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (g.K % R4D_W4_KPB) return RAD_E_SHAPE;
|
||||
if (!w5_enc(enc, g.group)) return RAD_E_DTYPE;
|
||||
if (gout) *gout = g;
|
||||
if (operand == Q_BSCALE)
|
||||
return sz_layout(g, enc, sel, pl, n, true, "r4d.w5.sz.g128", R4D_DT_W4_SZ, 4, out);
|
||||
if (operand != Q_B) return RAD_E_UNSUPPORTED;
|
||||
if (!selects(enc, sel, n, "codes") || !dims(pl[0], g.N, g.K)) return RAD_E_SHAPE;
|
||||
fill(out, "r4d.w5.frag.g128", R4D_DT_W4_FRAG, g.N, g.K, g.N * g.K * 5 / 8);
|
||||
return RAD_OK;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
extern "C" int r4d_rad_layout_w5(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* pl, int n, RadLayout* out) {
|
||||
return w5_enc_layout(p, n_p, operand, enc, sel, pl, n, out, nullptr);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_relayout_w5(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* pl, int n, void* dst,
|
||||
int64_t dst_bytes) {
|
||||
RadLayout L;
|
||||
WGeom g;
|
||||
const int rc = w5_enc_layout(p, n_p, operand, enc, sel, pl, n, &L, &g);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (dst_bytes != L.bytes) return RAD_E_SHAPE;
|
||||
std::memset(dst, 0, (size_t)dst_bytes);
|
||||
if (operand == Q_BSCALE) {
|
||||
if (!sz_zeros_fit(g, pl[1], 5)) return RAD_E_FORMAT;
|
||||
sz_write(g, pl, -1, (uint32_t*)dst);
|
||||
return RAD_OK;
|
||||
}
|
||||
uint32_t* wq = (uint32_t*)dst;
|
||||
uint32_t* hi = wq + g.N * g.K / 8; /* after the N*K/2 bytes of nibbles */
|
||||
const long long rb = (g.K * 5 + 7) / 8;
|
||||
for (long long nn = 0; nn < g.N; ++nn) {
|
||||
const uint8_t* row = pdata(pl[0]) + nn * rb;
|
||||
for (long long k = 0; k < g.K; ++k) {
|
||||
const uint32_t q = rad_load_bits(row, 5, k);
|
||||
long long w;
|
||||
int nib;
|
||||
w4_slot(g, nn, k, &w, &nib);
|
||||
wq[w] |= (q & 15u) << (4 * nib);
|
||||
/* the fifth bit: word (t, kb, lane) = w / 4 (w4_slot's dword index is that * 4 + s);
|
||||
* byte s = w % 4, bit e from the nibble position */
|
||||
const long long hw = w / 4;
|
||||
const int s = (int)(w % 4);
|
||||
const int e = nib % 2 == 0 ? nib / 2 : 4 + nib / 2; /* nibble 2e / 2(e-4)+1 -> e */
|
||||
hi[hw] |= (q >> 4) << (8 * s + e);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_unrelayout_w5(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const void* src, int64_t src_bytes,
|
||||
const RadTensor* pl, int n) {
|
||||
RadLayout L;
|
||||
WGeom g;
|
||||
const int rc = w5_enc_layout(p, n_p, operand, enc, sel, pl, n, &L, &g);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (src_bytes != L.bytes) return RAD_E_SHAPE;
|
||||
if (operand == Q_BSCALE) {
|
||||
sz_read(g, (const uint32_t*)src, pl, true);
|
||||
return RAD_OK;
|
||||
}
|
||||
const uint32_t* wq = (const uint32_t*)src;
|
||||
const uint32_t* hi = wq + g.N * g.K / 8;
|
||||
const long long rb = (g.K * 5 + 7) / 8;
|
||||
uint8_t* d = (uint8_t*)pl[0].data;
|
||||
std::memset(d, 0, (size_t)(g.N * rb));
|
||||
for (long long nn = 0; nn < g.N; ++nn)
|
||||
for (long long k = 0; k < g.K; ++k) {
|
||||
long long w;
|
||||
int nib;
|
||||
w4_slot(g, nn, k, &w, &nib);
|
||||
const int e = nib % 2 == 0 ? nib / 2 : 4 + nib / 2; /* as r4d_rad_relayout_w5 */
|
||||
const uint32_t q = ((wq[w] >> (4 * nib)) & 15u) | (((hi[w / 4] >> (8 * (w % 4) + e)) & 1u) << 4);
|
||||
rad_store_bits(d + nn * rb, 5, k, q);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ---- givens pairs: ParoQuant's rotation pairs (i32 [P*S, K]), stored as they are but CHECKED.
|
||||
* The rotate kernels index a group's 128 LDS slots with them unchecked, so a stage that is not a
|
||||
* pairing of its own group -- an index outside the group, or one column twice -- is refused here,
|
||||
* at load, rather than read out of bounds. Operand 1 of the givens rows; the rest is as stored. */
|
||||
namespace {
|
||||
constexpr int GV_PAIRS_OPERAND = 1;
|
||||
constexpr long long GV_GROUP = 128;
|
||||
|
||||
int gv_pairs_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out) {
|
||||
if (!out || !pl) return RAD_E_INVAL;
|
||||
if (operand != GV_PAIRS_OPERAND) return RAD_E_UNSUPPORTED;
|
||||
const long long K = r4d_pgeti_or(p, n_p, "n", 0);
|
||||
if (K <= 0 || K % GV_GROUP || !selects(enc, sel, n, "t.pairs") || pl[0].rank != 2 ||
|
||||
pl[0].dtype != RAD_I32 || pl[0].shape[1] != K) return RAD_E_SHAPE;
|
||||
fill(out, "r4d.givens.pairs", RAD_I32, pl[0].shape[0], K, pl[0].shape[0] * K * 4);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Every row (a stage of a part) is, group by group, a perfect matching of the group's columns. */
|
||||
bool gv_pairs_ok(const int32_t* pairs, long long rows, long long K) {
|
||||
for (long long r = 0; r < rows; ++r)
|
||||
for (long long g = 0; g < K; g += GV_GROUP) {
|
||||
bool seen[GV_GROUP] = {};
|
||||
for (long long i = 0; i < GV_GROUP; ++i) {
|
||||
const long long c = (long long)pairs[r * K + g + i] - g;
|
||||
if (c < 0 || c >= GV_GROUP || seen[c]) return false;
|
||||
seen[c] = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
extern "C" int r4d_rad_layout_givens_pairs(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out) {
|
||||
return gv_pairs_layout(p, n_p, operand, enc, sel, pl, n, out);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_relayout_givens_pairs(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, void* dst,
|
||||
int64_t dst_bytes) {
|
||||
RadLayout L;
|
||||
const int rc = gv_pairs_layout(p, n_p, operand, enc, sel, pl, n, &L);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (dst_bytes != L.bytes || !pl[0].data) return RAD_E_SHAPE;
|
||||
if (!gv_pairs_ok((const int32_t*)pl[0].data, L.shape[0], L.shape[1])) return RAD_E_FORMAT;
|
||||
std::memcpy(dst, pl[0].data, (size_t)L.bytes);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_unrelayout_givens_pairs(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const void* src, int64_t src_bytes,
|
||||
const RadTensor* pl, int n) {
|
||||
RadLayout L;
|
||||
const int rc = gv_pairs_layout(p, n_p, operand, enc, sel, pl, n, &L);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (src_bytes != L.bytes || !pl[0].data) return RAD_E_SHAPE;
|
||||
std::memcpy(pl[0].data, src, (size_t)L.bytes);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ---- givens_dense: ParoQuant's rotation as one dense matrix a (part, group).
|
||||
*
|
||||
* The dense kernel (r4d_givens_dense.hip) applies a part's Givens stages over each group of 128 columns as
|
||||
* T (x / D), T being their composition. rad-convert composes T and stores it as the "givens" encoding's
|
||||
* "t.dense" table, f16 [P * K/128, 128 * 128], row p * (K/128) + g element j * 128 + i the output j for the
|
||||
* unit input e_i (abi/rad_encoding.h) -- which is the order the kernel stages a (part, group), 32 KiB, whole.
|
||||
* So the layout is the table as stored: a relayout that moves bytes and derives none (abi/rad_abi.h).
|
||||
* Operand 1 of givens_dense_quant; everything else stays as stored. */
|
||||
namespace {
|
||||
constexpr int GD_OPERAND = 1;
|
||||
constexpr long long GD_GROUP = 128;
|
||||
|
||||
int gd_geom(const RadParam* p, int n_p, const RadEncoding* enc, const int* sel, const RadTensor* pl, int n,
|
||||
long long* P, long long* K) {
|
||||
if (n != 1 || std::strncmp(r4d_enc::role(enc, sel, 0), "t.dense", RAD_ENC_STR) != 0) return RAD_E_SHAPE;
|
||||
*K = r4d_pgeti_or(p, n_p, "n", 0);
|
||||
*P = r4d_pgeti_or(p, n_p, "parts", 1);
|
||||
if (*K <= 0 || *K % GD_GROUP || *P < 1) return RAD_E_SHAPE;
|
||||
if (!dims(pl[0], *P * (*K / GD_GROUP), GD_GROUP * GD_GROUP) || pl[0].dtype != RAD_F16) return RAD_E_SHAPE;
|
||||
return RAD_OK;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
extern "C" int r4d_rad_layout_gdense(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* pl, int n, RadLayout* out) {
|
||||
if (!out || !pl) return RAD_E_INVAL;
|
||||
if (operand != GD_OPERAND) return RAD_E_UNSUPPORTED;
|
||||
long long P, K;
|
||||
const int rc = gd_geom(p, n_p, enc, sel, pl, n, &P, &K);
|
||||
if (rc != RAD_OK) return rc;
|
||||
fill(out, "r4d.givens.dense.g128", RAD_F16, P * K, GD_GROUP, P * K * GD_GROUP * 2);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_relayout_gdense(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* pl, int n, void* dst,
|
||||
int64_t dst_bytes) {
|
||||
RadLayout L;
|
||||
int rc = r4d_rad_layout_gdense(p, n_p, operand, enc, sel, pl, n, &L);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (dst_bytes != L.bytes) return RAD_E_SHAPE;
|
||||
std::memcpy(dst, pdata(pl[0]), (size_t)L.bytes);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ---- w8: i8 codes, symmetric about zero, an f16 scale a group. The f16 kernel (a16) reads the
|
||||
* code offset binary with zero 128 in the shared dword; the int8 kernel (a8) reads it signed with a
|
||||
* bare f16 scale plane. */
|
||||
@@ -615,6 +859,29 @@ extern "C" int r4d_rad_unrelayout_w2a8(const RadParam* p, int n_p, int operand,
|
||||
* exponents to [K/32][N], and b_ref is each row's largest exponent -- a reduction over the plane,
|
||||
* which is a derivation any reader of the plane could make and so not a choice. */
|
||||
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_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. */
|
||||
bool mx_paro_enc(const RadEncoding* e, long long K) {
|
||||
if (!e || !rad_enc_is(e, "affine") || std::strncmp(e->transform, "givens", RAD_ENC_STR) != 0) return false;
|
||||
if (e->n_planes < 5 || !rad_enc_valid(e)) return false;
|
||||
if (std::strncmp(e->plane[0].role, "codes", RAD_ENC_STR) != 0 || e->plane[0].dtype != RAD_FP4E2M1 ||
|
||||
e->plane[0].block[0] != 1 || e->plane[0].block[1] != 1) return false;
|
||||
const int s = rad_enc_find(e, "scale");
|
||||
if (s != 1 || e->plane[s].dtype != RAD_E8M0 || e->plane[s].kind != RAD_PLANE_TILED ||
|
||||
e->plane[s].block[0] != 1 || e->plane[s].block[1] != R4D_MXFP4_GROUP) return false;
|
||||
const int p = rad_enc_find(e, "t.pairs"), an = rad_enc_find(e, "t.angle"), sc = rad_enc_find(e, "t.scale");
|
||||
if (p < 0 || an < 0 || sc < 0) return false;
|
||||
if (e->plane[p].dtype != RAD_I32 || e->plane[an].dtype != RAD_F32 || e->plane[sc].dtype != RAD_F32)
|
||||
return false;
|
||||
// the quantiser rotates whole groups of 128 columns, one scale per group
|
||||
return K % 128 == 0 && e->plane[p].extent[1] == K;
|
||||
}
|
||||
|
||||
int mx_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out, WGeom* gout) {
|
||||
if (!out || !pl) return RAD_E_INVAL;
|
||||
@@ -622,7 +889,8 @@ int mx_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
|
||||
int rc = wgeom(p, n_p, R4D_MXFP4_GROUP, &g);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (g.group != R4D_MXFP4_GROUP || g.K % 16) return RAD_E_SHAPE;
|
||||
if (!r4d_enc::affine(enc, RAD_FP4E2M1, RAD_E8M0, 1, R4D_MXFP4_GROUP)) return RAD_E_DTYPE;
|
||||
if (!r4d_enc::affine(enc, RAD_FP4E2M1, RAD_E8M0, 1, R4D_MXFP4_GROUP) &&
|
||||
!mx_paro_enc(enc, g.K)) return RAD_E_DTYPE;
|
||||
if (gout) *gout = g;
|
||||
const long long nblk = g.K / R4D_MXFP4_GROUP;
|
||||
if (operand == Q_BSCALE || operand == Q_BREF) {
|
||||
|
||||
Reference in new issue
Block a user