libr4d: the dense integer rows take the group they compute -- a Q8_0 weight at group 32 was read as 128

The dense integer kernels compile their group in and read no `group` argument, and neither the
rows nor the layout hooks checked it: w8_enc_layout accepted an `i8*f16[1x32]` weight (a Q8_0
block) at group 32 and the kernel computed it at 128. The twelve dense gemm_nt_q rows now carry
C_EQ("group", ...), and the four hooks (w4, w5, w8, w2a8) answer RAD_E_DTYPE at any other group,
the one code select() counts as a refusal. relayout_parity_test pins the hooks, plugin_test the
rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-28 12:49:18 +01:00
1 parent 385318f81a
commit d73abc28d5
4 files changed
+97 -12

No files matched your search

+4
View File
@@ -365,6 +365,7 @@ int w4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
WGeom g;
int rc = wgeom(p, n_p, R4D_W4_GROUP, &g);
if (rc != RAD_OK) return rc;
if (g.group != R4D_W4_GROUP) return RAD_E_DTYPE;
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) ||
@@ -474,6 +475,7 @@ int w5_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
WGeom g;
int rc = wgeom(p, n_p, R4D_W4_GROUP, &g);
if (rc != RAD_OK) return rc;
if (g.group != R4D_W4_GROUP) return RAD_E_DTYPE;
if (g.K % R4D_W4_KPB) return RAD_E_SHAPE;
if (!w5_enc(enc, g.group)) return RAD_E_DTYPE;
if (gout) *gout = g;
@@ -676,6 +678,7 @@ int w8_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
WGeom g;
int rc = wgeom(p, n_p, R4D_W8_GROUP, &g);
if (rc != RAD_OK) return rc;
if (g.group != R4D_W8_GROUP) return RAD_E_DTYPE;
if (g.K % R4D_W8_KPB) return RAD_E_SHAPE;
if (!r4d_enc::affine(enc, RAD_I8, RAD_F16, 1, g.group)) return RAD_E_DTYPE;
if (gout) *gout = g;
@@ -760,6 +763,7 @@ int w2_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
WGeom g;
int rc = wgeom(p, n_p, R4D_W2_GROUP, &g);
if (rc != RAD_OK) return rc;
if (g.group != R4D_W2_GROUP) return RAD_E_DTYPE;
if (g.K % 128) return RAD_E_SHAPE;
if (!(r4d_enc::affine(enc, RAD_U2, RAD_F16, 1, g.group, RAD_U8) ||
r4d_enc::affine(enc, RAD_U2, RAD_F16, 1, g.group, RAD_U4))) return RAD_E_DTYPE;
+25 -12
View File
@@ -4691,30 +4691,39 @@ static const RadConstraint cGemmNt64[] = {
static const RadConstraint cGemmNtTiled[] = {
C_GE("M", 65), C_LE("M", 65536), C_GE("N", 128), C_DIV("K", 64), C_IN("dtype", "bf16"),
};
/* The dense integer kernels take their group from a build constant and read no `group` argument.
* A declaration at another group gets no kernel rather than a silent 128. */
static const RadConstraint cGemmNtW4[] = {
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w4a16"),
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w4a16"),
};
/* w5a16 serves any M: the dispatcher takes rows past 64 as chunks of 64. */
static const RadConstraint cGemmNtW5[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w5a16"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w5a16"),
};
static const RadConstraint cGemmNtW4Asym[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w4a16_asym"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w4a16_asym"),
};
static const RadConstraint cGemmNtW4A8I8[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w4a8i8_asym"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w4a8i8_asym"),
};
static const RadConstraint cGemmNtW5A8I8[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w5a8i8"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w5a8i8"),
};
static const RadConstraint cGemmNtW2A8[] = {
C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w2a8"),
C_DIV("K", 128), C_DIV("N", 16), C_EQ("group", R4D_W2_GROUP), C_IN("dtype", "w2a8"),
};
static const RadConstraint cGemmNtW8[] = {
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w8a16"),
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W8_GROUP), C_IN("dtype", "w8a16"),
};
static const RadConstraint cGemmNtW4A8[] = {
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w4a8 w4a8_asym"),
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w4a8 w4a8_asym"),
};
static const RadConstraint cGemmNtMxfp4A8[] = {
C_LE("M", 64), C_DIV("K", 32), C_DIV("N", 16), C_IN("dtype", "mxfp4a8"),
@@ -5040,11 +5049,13 @@ static const RadConstraint cDequantW4Bf16[] = {
* K % (SK*group)). Its MAX_M of 32768 is not structural; as a skinny kernel with a tiled sibling
* above 64, it takes the w4a8 row's band. */
static const RadConstraint cGemmNtW8A8[] = {
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w8a8"),
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16),
C_EQ("group", R4D_W8_GROUP), C_IN("dtype", "w8a8"),
};
/* r4d_gemm_w4a8_tiled / _asym_tiled. Entry point: N % 256, K % group, M >= 1. */
static const RadConstraint cGemmW4A8Tiled[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 256), C_IN("dtype", "w4a8 w4a8_asym"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 256),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w4a8 w4a8_asym"),
};
/* r4d_gemm_mxfp4a8_decode. Entry point: M <= 128, N % 16, K % 64, and N up to its counter block's
* 36864. Above the skinny row by priority, so it takes the decode band; the skinny row keeps the
@@ -5095,13 +5106,15 @@ static const RadConstraint cGemmM2xfp4A8Tiled[] = {
/* r4d_gemm_w8a8_tiled: the same kernel at radix 8. Its asym arm is not declared in r4d.h, so
* `w8a8_asym` is absent from the dtype set. */
static const RadConstraint cGemmW8A8Tiled[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 256), C_IN("dtype", "w8a8"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 256),
C_EQ("group", R4D_W8_GROUP), C_IN("dtype", "w8a8"),
};
/* r4d_gemm_w4a8_prefill. Entry point: N % BN and K % group, BN being the variant's block width
* (256 except for the two 1024-thread arms). N % 256 is the default's condition; the shim refuses
* a variant whose BN does not divide N (the per-call half of spec §2.1). */
static const RadConstraint cGemmW4A8Prefill[] = {
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 256), C_IN("dtype", "w4a8"),
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 256),
C_EQ("group", R4D_W4_GROUP), C_IN("dtype", "w4a8"),
};
/* =================================================================================== tunables
+22
View File
@@ -1126,6 +1126,28 @@ TEST(libr4d_resolves_every_band_of_glm_mla_split_by_rows) {
}
}
/* The dense integer gemm_nt_q kernels compile their group in, so their rows take no other: at
* group 64 each dtype is a miss, at 128 it resolves. At M = 1 every skinny, tiled and prefill row
* of these dtypes is a candidate. */
TEST(libr4d_resolves_a_dense_integer_gemm_only_at_its_group) {
const std::string so = libr4d();
if (so.empty()) return;
Registry r;
CHECK(r.load_plugin(so, 0) >= 0);
for (const char* dtype : { "w4a16", "w5a16", "w4a16_asym", "w4a8i8_asym", "w5a8i8", "w2a8",
"w8a16", "w4a8", "w4a8_asym", "w8a8" }) {
Geometry g;
g.set_i("M", 1);
g.set_i("N", 256);
g.set_i("K", 256);
g.set_s("dtype", dtype);
g.set_i("group", 64);
CHECK_EQ(picked(r.select("gemm_nt_q", g, RAD_DOMAIN_DEVICE)), "<none>");
g.set_i("group", 128);
CHECK(r.select("gemm_nt_q", g, RAD_DOMAIN_DEVICE) != nullptr);
}
}
/* ================================================================== constraints that are not intervals */
namespace {
std::string extra_dir() { return home() + "/testkernels/extra"; }
+46
View File
@@ -883,6 +883,52 @@ TEST(a_kernel_refuses_an_encoding_it_does_not_read) {
}
}
/* The dense integer kernels compile their group in and read no `group` argument, so a hook that
* accepted another one would hand the kernel planes it reads at 128. */
TEST(a_dense_int_kernel_refuses_a_group_it_does_not_compute) {
NEED_LIBQUANT();
const int64_t N = 64, K = 256;
const std::vector<float> w = draw(N * K, 28);
auto rtn = [&](const char* codes, const char* zero, bool clamp, int64_t group) {
Opts g;
g.s("codes", codes).i("group", group).s("scale", "f16");
if (zero) g.s("zero", zero);
if (clamp) g.s("clamp", "sym");
Quantised q = quantise(quantizer("rtn"), g, "x", N, K, w);
CHECK_EQ(q.status, RAD_OK);
return q;
};
auto lay = [&](RadLayoutFn f, const char* dtype, int64_t group, const Quantised& q) {
Opts kp;
kp.i("N", N).i("K", K).i("group", group).s("dtype", dtype);
Selection s = select(q, N, K, { "codes" });
RadLayout L{};
return f(kp.data(), kp.n(), B, &q.enc, s.sel.data(), s.t.data(), (int)s.t.size(), &L);
};
const Quantised i4 = rtn("i4", nullptr, false, 64), u5 = rtn("u5", "u8", false, 64);
const Quantised i8 = rtn("i8", nullptr, true, 64), u2 = rtn("u2", "u8", false, 64);
CHECK_EQ(lay(r4d_rad_layout_w4, "w4a8", 64, i4), RAD_E_DTYPE);
CHECK_EQ(lay(r4d_rad_layout_w5, "w5a16", 64, u5), RAD_E_DTYPE);
CHECK_EQ(lay(r4d_rad_layout_w8a16, "w8a16", 64, i8), RAD_E_DTYPE);
CHECK_EQ(lay(r4d_rad_layout_w2a8, "w2a8", 64, u2), RAD_E_DTYPE);
/* llama.cpp's Q8_0 is this encoding, an f16 scale per 32 i8 codes; the w8 kernels would read
* it at 128. */
const RadQuantizerInfo* ggml = quantizer("ggml");
REQUIRE(ggml != nullptr);
Opts o;
o.s("type", "q8_0");
const Quantised q8 = quantise(ggml, o, "x", N, K, w);
REQUIRE_EQ(q8.status, RAD_OK);
CHECK_EQ(lay(r4d_rad_layout_w8a16, "w8a16", 32, q8), RAD_E_DTYPE);
const Quantised i4_128 = rtn("i4", nullptr, false, 128), i8_128 = rtn("i8", nullptr, true, 128);
const Quantised u2_128 = rtn("u2", "u8", false, 128);
CHECK_EQ(lay(r4d_rad_layout_w4, "w4a8", 128, i4_128), RAD_OK);
CHECK_EQ(lay(r4d_rad_layout_w8a16, "w8a16", 128, i8_128), RAD_OK);
CHECK_EQ(lay(r4d_rad_layout_w2a8, "w2a8", 128, u2_128), RAD_OK);
}
/* ParoQuant's rotation pairs are stored as they are, and refused at load when a stage is not a
* pairing within its own group's 128 columns, which the rotate kernels would index out of bounds. */
TEST(givens_pairs_are_stored_as_they_are_and_checked) {