ParoQuant int5/int4: review fixes -- docs, the w5 layout pinned and inverted, zero points checked, int4 import tested

* docs/OPS.md: the dtypes w5a16, w4a16_asym, w5a8i8, w4a8i8_asym, givens_quant_act_i8g, and which rows read pb1/pb2.
* r4d_gemm_w5a16_nt_m64: drop the unused two's-complement dequant copied from w4; the errors name this kernel.
* r4d_layout: r4d_rad_unrelayout_w5 (the inverse, wired to both w5 rows; the int4 rows take w4's); a zero point
  that is not a 4-/5-bit code is refused at load (the int8 prefill kernel reads five bits of it, the f16 one all).
* rad_fp8.h: one linear_is_givens predicate (table dtypes checked for both formats, the zero plane's u8 too);
  declare_rotation declares the tables and rot_stages only, decl_rot_op the op in front of each GEMM.
* tests: relayout_parity pins the w5 stored form and its refusal; checkpoint_test imports z-lab's int4 as a fused
  gate|up (two rotated parts); the selftest's asymmetric planes and part bounds come from one helper each.
* checkpoint: the ParoQuant refusal names both readable forms.
This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent 3491aca6fe
commit a19cbcae85
10 files changed
+312 -187

No files matched your search

+12 -48
View File
@@ -76,54 +76,18 @@ __device__ __forceinline__ v8h fragA(r4d_gu2 p, int u) {
return x.f;
}
// One dword of eight nibbles -> one 16x16x16 f16 B fragment, minus the zero point.
// t0/t1 split the even and odd nibbles into bytes; each v_perm_b32 pairs two of those bytes with
// the constant high byte 0x64, which is the f16 exponent field for 1024. nz carries
// -(1024 + zero) in BOTH halves so one v_pk_add_f16 corrects a pair.
// The unpack is under a percent of this kernel at M=8, where it is at the DRAM roofline and the
// arithmetic hides in the load latency. It is a real fraction at M=64, but that band belongs to
// r4d_gemm_w4a8_nt_m64 -- and with no dequant at all the f16 form still trails the int8 one there,
// because the gap is the matrix instruction and not the dequant. Nothing here is worth a knob.
#ifndef R4D_GEMM_W4_TWOS
#define R4D_GEMM_W4_TWOS 1
#endif
// DO NOT FOLD THE GROUP SCALE INTO THE B FRAGMENT. One v_pk_mul_f16 per pair on the finished
// integer q is exact in f16, removes the second accumulator array entirely and takes the M=64 shape
// from 192 VGPRs to 135 -- and it is SLOWER on most of the drafter's shapes. The multiply lands
// inside the per-fragment dependent chain between the unpack and the WMMA, where the per-group
// epilogue it replaces sits outside it; this kernel is stall-bound, not register-bound.
__device__ __forceinline__ v8h dequant(unsigned w, __half2 nz) {
const unsigned K64 = 0x64646464u;
#if R4D_GEMM_W4_TWOS
// The stored nibble is a 4-bit TWO'S COMPLEMENT code, because that is what
// r4d_gemm_w4a8_nt_m64 needs -- it shifts the nibble into the byte's high half and reads the
// byte signed. This kernel wants OFFSET BINARY, so that 1024 + n minus the constant 1024 + 8
// is the value. The two differ by bit 3 of the nibble, so one XOR per spread dword converts,
// and the zero point is then pinned to 8 for every group. Two instructions per fragment, on
// the kernel whose whole unpack is a fraction of a percent of its time at M=8 -- which is why
// the conversion lives here and not in the int8 kernel, where the unpack is three instructions
// and does matter. One quantised weight, two kernels, dispatched on M.
const unsigned t0 = (w & 0x0F0F0F0Fu) ^ 0x08080808u;
const unsigned t1 = ((w >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
#else
const unsigned t0 = w & 0x0F0F0F0Fu;
const unsigned t1 = (w >> 4) & 0x0F0F0F0Fu;
#endif
union { unsigned u[4]; __half2 h2[4]; v8h f; } x;
x.u[0] = __builtin_amdgcn_perm(K64, t0, 0x05010400u); // nibbles 0, 2
x.u[1] = __builtin_amdgcn_perm(K64, t0, 0x05030402u); // nibbles 4, 6
x.u[2] = __builtin_amdgcn_perm(K64, t1, 0x05010400u); // nibbles 1, 3
x.u[3] = __builtin_amdgcn_perm(K64, t1, 0x05030402u); // nibbles 5, 7
#pragma unroll
for (int i = 0; i < 4; i++) x.h2[i] = __hadd2(x.h2[i], nz);
return x.f;
}
// One dword of eight nibbles plus the byte of their fifth bits -> one 16x16x16 f16 B fragment,
// minus the zero point. Nibble 2e (e < 4) / 2(e-4)+1 is fragment element e, so bits 0..3 of `hb`
// are the fifth bits of the even nibbles' elements and bits 4..7 the odd nibbles'.
// are the fifth bits of the even nibbles' elements and bits 4..7 the odd nibbles'. Each v_perm_b32
// pairs two code bytes with the constant high byte 0x64, the f16 exponent field for 1024, so a half
// is 1024 + code; nz carries -(1024 + zero) in BOTH halves, so one v_pk_add_f16 corrects a pair.
// The codes are offset binary (0..31, or 0..15 without the fifth bit): no sign flip.
__device__ __forceinline__ unsigned spread16(unsigned n) { // 4 bits -> bit 4 of each byte
return ((n * 0x00204081u) & 0x01010101u) << 4;
}
@@ -288,18 +252,18 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i
if (!lda) lda = K;
if (!ldc) ldc = N;
if (M < 1 || M > R4D_GEMM_W4_MAX_M)
throw std::runtime_error("r4d gemm_w4a16_nt_m64: M must be 1..64");
if (N % 16) throw std::runtime_error("r4d gemm_w4a16_nt_m64: N must be a multiple of 16");
throw std::runtime_error("r4d gemm_w5a16_nt_m64: M must be 1..64");
if (N % 16) throw std::runtime_error("r4d gemm_w5a16_nt_m64: N must be a multiple of 16");
if (K % (SK * R4D_GEMM_W4_GROUP))
throw std::runtime_error("r4d gemm_w4a16_nt_m64: K must be divisible by SK*128");
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_w4a16_nt_m64: threads/block > 1024");
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_w4a16_nt_m64: MB must be 1..4");
if (NPW != 1 && NPW != 4) throw std::runtime_error("r4d gemm_w4a16_nt_m64: NPW must be 1 or 4");
throw std::runtime_error("r4d gemm_w5a16_nt_m64: K must be divisible by SK*128");
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_w5a16_nt_m64: threads/block > 1024");
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_w5a16_nt_m64: MB must be 1..4");
if (NPW != 1 && NPW != 4) throw std::runtime_error("r4d gemm_w5a16_nt_m64: NPW must be 1 or 4");
const int ntiles = N / 16, mtiles = (M + 15) / 16;
const int ncols = WV * NPW;
dim3 grid((ntiles + ncols - 1) / ncols, (mtiles + MB - 1) / MB), block(WV * SK * WAVE32);
size_t smem = (size_t)ncols * SK * 256 * sizeof(float);
if (smem > 64 * 1024) throw std::runtime_error("r4d gemm_w4a16_nt_m64: WV*NPW*SK too large");
if (smem > 64 * 1024) throw std::runtime_error("r4d gemm_w5a16_nt_m64: WV*NPW*SK too large");
auto Ap = (const unsigned short*)a;
auto Wp = (const unsigned*)wq;
auto Hp = (const unsigned*)whi;
@@ -316,7 +280,7 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i
case 2: LAUNCH(2, NP, TT); break; \
case 3: LAUNCH(3, NP, TT); break; \
case 4: LAUNCH(4, NP, TT); break; \
default: throw std::runtime_error("r4d gemm_w4a16_nt_m64: MB unsupported"); }
default: throw std::runtime_error("r4d gemm_w5a16_nt_m64: MB unsupported"); }
if (NPW == 1) {
if (NT) { LAUNCH_MB(1, true); } else { LAUNCH_MB(1, false); }
} else {
+45
View File
@@ -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)
@@ -416,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;
}
@@ -501,6 +516,7 @@ extern "C" int r4d_rad_relayout_w5(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) {
if (!sz_zeros_fit(g, pl[1], 5)) return RAD_E_FORMAT;
sz_write(g, pl, -1, (uint32_t*)dst);
return RAD_OK;
}
@@ -526,6 +542,35 @@ extern "C" int r4d_rad_relayout_w5(const RadParam* p, int n_p, int operand, cons
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;
}
/* ---- 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. */
+2
View File
@@ -399,6 +399,8 @@ int r4d_rad_layout_w5(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
int r4d_rad_relayout_w5(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, void*, int64_t);
int r4d_rad_unrelayout_w5(const RadParam*, int, int, const RadEncoding*, const int*,
const void*, int64_t, const RadTensor*, int);
int r4d_rad_layout_mxfp4(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
int r4d_rad_relayout_mxfp4(const RadParam*, int, int, const RadEncoding*, const int*,
+4 -4
View File
@@ -4711,7 +4711,7 @@ static const RadKernelInfo kKernels[] = {
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
ROW(cGemmNtW5), ROW(tGemmSkinny), r4d_tune_valid_gemm,
nullptr, nullptr, r4d_rad_gemm_w5a16_m64, nullptr, r4d_rad_layout_w5, r4d_rad_relayout_w5,
nullptr, nullptr },
nullptr, r4d_rad_unrelayout_w5 },
{ "gemm_w4a16_asym_nt_m64", "gemm_nt_q", "gemm",
"skinny GEMM with a 4-bit ASYMMETRIC weight (z-lab's ParoQuant int4), the 5-bit row's kernel with "
@@ -4722,7 +4722,7 @@ static const RadKernelInfo kKernels[] = {
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
ROW(cGemmNtW4Asym), ROW(tGemmSkinny), r4d_tune_valid_gemm,
nullptr, nullptr, r4d_rad_gemm_w4a16asym_m64, nullptr, r4d_rad_layout_w4, r4d_rad_relayout_w4,
nullptr, nullptr },
nullptr, r4d_rad_unrelayout_w4 },
{ "gemm_w4a8i8_tiled", "gemm_nt_q", "gemm",
"the 4-bit ASYMMETRIC weight x int8 activation GEMM for prefill row counts (z-lab's ParoQuant int4): "
@@ -4734,7 +4734,7 @@ static const RadKernelInfo kKernels[] = {
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
ROW(cGemmNtW4A8I8), NOTUNE,
nullptr, nullptr, r4d_rad_gemm_w4a8i8_tiled, r4d_rad_gemm_mxfp4a8_tiled_scratch,
r4d_rad_layout_w4, r4d_rad_relayout_w4, nullptr, nullptr },
r4d_rad_layout_w4, r4d_rad_relayout_w4, nullptr, r4d_rad_unrelayout_w4 },
{ "gemm_w5a8i8_tiled", "gemm_nt_q", "gemm",
"the 5-bit ASYMMETRIC weight x int8 activation GEMM for prefill row counts (ParoQuant int5): int8 "
@@ -4746,7 +4746,7 @@ static const RadKernelInfo kKernels[] = {
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
ROW(cGemmNtW5A8I8), NOTUNE,
nullptr, nullptr, r4d_rad_gemm_w5a8i8_tiled, r4d_rad_gemm_mxfp4a8_tiled_scratch,
r4d_rad_layout_w5, r4d_rad_relayout_w5, nullptr, nullptr },
r4d_rad_layout_w5, r4d_rad_relayout_w5, nullptr, r4d_rad_unrelayout_w5 },
{ "gemm_w8a16_nt_m64", "gemm_nt_q", "gemm",
"skinny GEMM with an 8-bit weight, 16x16x16 WMMA, split-K reduced in LDS",
+48 -59
View File
@@ -865,6 +865,42 @@ struct Planes {
}
};
/* gemm_nt_q's `pb1`/`pb2` over N rows (ParoQuant): rows [0, e1), [e1, e2), [e2, N) meet copies 0, 1,
* 2 of the activation, side by side in a row; `parts` is how many copies there are. 0 leaves a
* bound out, as the kernels read it. */
struct PartCut {
int64_t e1, e2;
int parts;
PartCut(int64_t N, int64_t pb1, int64_t pb2)
: e1(pb1 > 0 ? std::min(pb1, N) : N),
e2(pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N),
parts(e2 < N ? 3 : (e1 < N ? 2 : 1)) {}
int part_of(int64_t n) const { return (int)(n >= e1) + (int)(n >= e2); }
};
/* An ASYMMETRIC grid's planes for a [N, K] weight: `bits`-bit codes (a bit stream along K), an f16
* scale and a u8 zero point 0 .. 2^bits - 1 a 128 of K, drawn through st_mix32 of their coordinates. */
static Planes asym_planes(int bits, int64_t N, int64_t K) {
Planes p;
p.enc = rad_enc_affine(bits == 4 ? RAD_U4 : RAD_U5, RAD_F16, 1, 128);
rad_enc_add_plane(&p.enc, "zero", RAD_U8, 1, 128);
p.size(N, K);
const int ic = p.find("codes"), is = p.find("scale"), iz = p.find("zero");
const int64_t rb = p.row_bytes(ic), G = K / 128;
const uint32_t top = (1u << bits) - 1;
for (int64_t n = 0; n < N; ++n) {
for (int64_t k = 0; k < K; ++k)
rad_store_code(p.data[(size_t)ic].data() + n * rb, p.enc.plane[ic].dtype, k,
(int)(st_mix32((uint32_t)(n * K + k) ^ 0x5eedu) & top));
for (int64_t g = 0; g < G; ++g) {
const uint32_t h = st_mix32((uint32_t)(n * G + g) ^ 0x2545u);
rad_store_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g, 0.002f * (float)(1 + h % 40));
p.data[(size_t)iz][(size_t)(n * G + g)] = (uint8_t)((h >> 8) & top);
}
}
return p;
}
/* The unnormalised Sylvester Walsh-Hadamard transform of `n` values, in place: (a + b) to the lower
* index and (a - b) to the upper, the transform an "fwhtN" encoding names. H_n H_n = n I. */
static void h_fwht(float* v, int64_t n) {
@@ -3609,10 +3645,8 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
if (!sel(row)) return;
/* PARTS (ParoQuant): the weight's rows [0, pb1), [pb1, pb2), [pb2, N) each meet their own copy
* of the activation, the copies side by side in a row. `K` is one copy's width. */
const int64_t e1 = pb1 > 0 ? std::min(pb1, N) : N;
const int64_t e2 = pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N;
const int parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
auto part_of = [&](int64_t n) { return (int)(n >= e1) + (int)(n >= e2); };
const PartCut cut(N, pb1, pb2);
const int parts = cut.parts;
char nm[160];
std::string ex;
for (const RadParam& p : extra) ex += std::string(" ") + p.key + "=" + std::to_string(p.ival);
@@ -3713,7 +3747,7 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
for (int64_t i = 0; i < M; ++i)
for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) {
double acc = 0;
const int pt = part_of(n);
const int pt = cut.part_of(n);
for (int64_t k = 0; k < K; ++k) {
const double w = (double)rad_load_f32(p.data[(size_t)ic].data(), RAD_FP4E2M1, n * K + k) *
(double)rad_load_f32(p.data[(size_t)is].data(), RAD_E8M0, n * nblk + k / 32);
@@ -3857,38 +3891,17 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int
int64_t pb2 = 0) {
const char* row = "gemm_w5a16_nt_m64";
if (!sel(row)) return;
const int64_t e1 = pb1 > 0 ? std::min(pb1, N) : N;
const int64_t e2 = pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N;
const int parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
auto part_of = [&](int64_t n) { return (int)(n >= e1) + (int)(n >= e2); };
const PartCut cut(N, pb1, pb2);
const int parts = cut.parts;
char nm[160];
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d", row, (long long)M,
(long long)N, (long long)K, parts);
const RadKernelInfo* r = find(pl, row);
if (!r || !r->layout || !r->relayout) { ok(nm, false, "row or hooks absent"); return; }
const int64_t G = K / 128;
Planes p;
p.enc = rad_enc_affine(RAD_U5, RAD_F16, 1, 128);
rad_enc_add_plane(&p.enc, "zero", RAD_U8, 1, 128);
p.size(N, K);
const Planes p = asym_planes(5, N, K);
const int ic = p.find("codes"), is = p.find("scale"), iz = p.find("zero");
const int64_t rb = p.row_bytes(ic);
for (int64_t n = 0; n < N; ++n) {
for (int64_t k = 0; k < K; ++k) {
const uint32_t q = st_mix32((uint32_t)(n * K + k) ^ 0x5eedu) & 31u;
const int64_t bit = k * 5;
uint8_t* b = p.data[(size_t)ic].data() + n * rb + (bit >> 3);
const uint32_t v = q << (bit & 7);
b[0] |= (uint8_t)(v & 0xFF);
if ((bit & 7) > 3) b[1] |= (uint8_t)(v >> 8);
}
for (int64_t g = 0; g < G; ++g) {
const uint32_t h = st_mix32((uint32_t)(n * G + g) ^ 0x2545u);
const float sc = 0.002f * (float)(1 + h % 40);
rad_store_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g, sc);
p.data[(size_t)iz][(size_t)(n * G + g)] = (uint8_t)(8 + (h >> 8) % 20);
}
}
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 128) };
std::vector<uint8_t> wq, ws;
const int rw = stored(r, lp, 2, p, { "codes" }, &wq);
@@ -3938,7 +3951,7 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int
double num = 0, den = 0;
for (int64_t i = 0; i < M; ++i)
for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) {
const int pt = part_of(n);
const int pt = cut.part_of(n);
double acc = 0;
for (int64_t k = 0; k < K; ++k) {
const double q = rad_load_f32(p.data[(size_t)ic].data() + n * rb, RAD_U5, k);
@@ -4048,37 +4061,17 @@ static void wpa8i8_numeric(const Plugin& pl, int bits, int64_t M, int64_t N, int
int64_t pb2 = 0) {
const char* row = bits == 4 ? "gemm_w4a8i8_tiled" : "gemm_w5a8i8_tiled";
if (!sel(row)) return;
const int64_t e1 = pb1 > 0 ? std::min(pb1, N) : N;
const int64_t e2 = pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N;
const int parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
auto part_of = [&](int64_t n) { return (int)(n >= e1) + (int)(n >= e2); };
const PartCut cut(N, pb1, pb2);
const int parts = cut.parts;
char nm[160];
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d", row, (long long)M,
(long long)N, (long long)K, parts);
const RadKernelInfo* r = find(pl, row);
if (!r || !r->layout || !r->relayout) { ok(nm, false, "row or hooks absent"); return; }
const int64_t G = K / 128;
Planes p;
p.enc = rad_enc_affine(bits == 4 ? RAD_U4 : RAD_U5, RAD_F16, 1, 128);
rad_enc_add_plane(&p.enc, "zero", RAD_U8, 1, 128);
p.size(N, K);
const Planes p = asym_planes(bits, N, K);
const int ic = p.find("codes"), is = p.find("scale"), iz = p.find("zero");
const int64_t rb = p.row_bytes(ic);
for (int64_t n = 0; n < N; ++n) {
for (int64_t k = 0; k < K; ++k) {
const uint32_t q = st_mix32((uint32_t)(n * K + k) ^ 0x5eedu) & ((1u << bits) - 1);
const int64_t bit = k * bits;
uint8_t* b = p.data[(size_t)ic].data() + n * rb + (bit >> 3);
const uint32_t v = q << (bit & 7);
b[0] |= (uint8_t)(v & 0xFF);
if ((bit & 7) > 3) b[1] |= (uint8_t)(v >> 8);
}
for (int64_t g = 0; g < G; ++g) {
const uint32_t h = st_mix32((uint32_t)(n * G + g) ^ 0x2545u);
rad_store_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g, 0.002f * (float)(1 + h % 40));
p.data[(size_t)iz][(size_t)(n * G + g)] = (uint8_t)((h >> 8) % (1u << bits));
}
}
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 128), PS("dtype", bits == 4 ? "w4a8i8_asym" : "w5a8i8") };
std::vector<uint8_t> wq, ws;
const int rw = stored(r, lp, 2, p, { "codes" }, &wq);
@@ -4136,18 +4129,14 @@ static void wpa8i8_numeric(const Plugin& pl, int bits, int64_t M, int64_t N, int
double num = 0, den = 0;
for (int64_t i = 0; i < M; i += (M > 64 ? 3 : 1))
for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) {
const int pt = part_of(n);
const int pt = cut.part_of(n);
double acc = 0;
for (int64_t g = 0; g < G; ++g) {
double grp = 0;
const double z = p.data[(size_t)iz][(size_t)(n * G + g)];
const double sc = rad_load_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g);
for (int64_t k = g * 128; k < (g + 1) * 128; ++k) {
const int64_t bit = k * bits;
const uint8_t* rowp = p.data[(size_t)ic].data() + n * rb;
uint32_t v = rowp[bit >> 3];
if ((bit & 7) > 3) v |= (uint32_t)rowp[(bit >> 3) + 1] << 8;
const double q = (v >> (bit & 7)) & ((1u << bits) - 1);
const double q = rad_load_f32(p.data[(size_t)ic].data() + n * rb, p.enc.plane[ic].dtype, k);
grp += (double)av[(size_t)(i * PK + pt * K + k)] * (q - z);
}
acc += grp * sc * (double)as[(size_t)(i * PG + pt * G + g)];