iq_kernels: decode each IQ weight part once for every column and entry

iq_mmvq, native_gu_kernel and native_down_kernel called the format's dot once
per column (or group entry), re-reading the weight words and redoing the grid
lookups and sign unpacking each time. Each dot is now split into load (weight
side) and apply (activation side, same integer chain and float expression), as
native_mmvq.cu's multi-column traits are: the dense kernel takes 1, 2, 4 or 8
columns per pass (8 at a time past 8), the grouped ones 4 entries per pass
(~48 registers; the default verify window, --spec 4 with the split off, fits
in one pass).

Every column keeps the same calls, lane stride, summation order and warp_sum,
so results are bitwise equal to the old kernels, which STRATA_OLD_IQ_MMVQ=1 (or
iq_set_old_kernels) keeps for A/B timing.

Tests: iq_multi_parity (ctest, GPU, synthetic, no model) checks old vs new
bitwise for every format, ncols 1..8 and 11, groups of 0..11 entries; --bench
times both. iq_parity (real rows from tools/iq_fixture.py) now runs the dot for
1 to 8 columns and requires every column of a multi-column call to equal a
one-column call on it bitwise (exact layout, the default). native_expert_parity
runs the GPU expert with both the decode-once and the per-entry kernels on real
blobs and requires the same bits.
This commit is contained in:
Guillaume PUTIER authored and Niko1221 committed 2026-09-30 20:41:56 +02:00
1 parent abde6fc8c9
commit 45f93fc5ee
6 files changed
+854 -36

No files matched your search

+6
View File
@@ -477,6 +477,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
target_link_libraries(mmvq_multi_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})
add_test(NAME mmvq_multi_parity COMMAND mmvq_multi_parity)
endif()
# the IQ kernels that decode a weight part once for every column/entry, bitwise vs the per-column ones
# (GPU, synthetic, no model; --bench adds old/new timings)
add_executable(iq_multi_parity src/kernels/iq_multi_parity.cpp)
target_include_directories(iq_multi_parity PRIVATE ${_strata_gpu_include_directories})
target_link_libraries(iq_multi_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})
add_test(NAME iq_multi_parity COMMAND iq_multi_parity)
add_executable(shared_expert_parity src/kernels/shared_expert_parity.cpp)
target_include_directories(shared_expert_parity PRIVATE ${_strata_gpu_include_directories})
target_link_libraries(shared_expert_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})
+6
View File
@@ -54,4 +54,10 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long
const int32_t* n_groups, const int32_t* ent_dst, const int32_t* ent_tok, int64_t cap_groups,
int64_t cap_entries, const void* x_q8_1, void* scratch, float* out, void* stream);
/// `iq_mmvq` and `native_expert_grouped` decode each weight part once and apply it to every column / entry;
/// true selects the older kernels that decode it again per column (STRATA_OLD_IQ_MMVQ=1 at startup). Both give
/// bitwise the same results. Set before graph capture; captured graphs keep the kernels they captured.
void iq_set_old_kernels(bool old);
bool iq_old_kernels();
} // namespace strata::kernels
+450 -21
View File
@@ -346,6 +346,332 @@ __global__ void __launch_bounds__(128) mmvq_kernel(const uint8_t* __restrict__ w
}
}
// ---------------------------------------------------------------- decode once, apply to every column
// The kernels above call Fmt<TY>::dot once per (call, column): each column re-reads the weight words and redoes
// the grid lookups and sign unpacking. Here each dot is split, as native_mmvq.cu's multi-column traits are, into
// `load` (everything that depends only on the weight: the signed grid words, the integer scales, the fp16 block
// scale as a float) and `apply` (the activation loads, the dp4a chain in the same order, the same integer scale
// step and the same float expression). `apply(load(...))` does the dot's integer and float operations in the same
// order on the same values, so a column of the kernels below is BITWISE equal to the same column of mmvq_kernel /
// native_gu_kernel / native_down_kernel (iq_multi_parity checks it; STRATA_OLD_IQ_MMVQ=1 keeps the old kernels).
template<int TY> struct Split;
template<> struct Split<16> { // IQ2_XXS
struct W { int g[8]; int ls; float dw; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq2_xxs* bq2 = (const block_iq2_xxs*) vbq + kbx;
const int q2 = get_int_b2(bq2->qs, iqs);
const uint8_t* aux8 = (const uint8_t*) &q2;
const uint32_t aux32 = get_int_b2(bq2->qs, iqs + 1);
W r;
#pragma unroll
for (int k0 = 0; k0 < 8; k0 += 2) {
const uint2 grid_pos = ((const uint2*) iq2xxs_grid)[aux8[k0 / 2]];
const uint32_t signs = unpack_ksigns(aux32 >> (7 * k0 / 2));
const int signs0 = __vcmpne4(signs & 0x08040201, 0);
r.g[k0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
const int signs1 = __vcmpne4(signs & 0x80402010, 0);
r.g[k0 + 1] = __vsub4(grid_pos.y ^ signs1, signs1);
}
r.ls = aux32 >> 27 | 1;
r.dw = __half2float(bq2->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
int sumi = 0;
#pragma unroll
for (int j = 0; j < 8; ++j) sumi = ggml_cuda_dp4a(r.g[j], get_int_b4(bq8_1[iqs / 2].qs, j), sumi);
sumi = sumi * r.ls / 8;
const float d = r.dw * __low2float(bq8_1[iqs / 2].ds);
return d * sumi;
}
};
// IQ2_XS and IQ2_S share the apply: two half sums, two 4-bit scales
struct SplitLs2 {
struct W { int g[8]; int ls0, ls1; float dw; };
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
int sumi0 = 0, sumi1 = 0;
#pragma unroll
for (int j = 0; j < 4; ++j) sumi0 = ggml_cuda_dp4a(r.g[j], get_int_b4(bq8_1[iqs / 2].qs, j), sumi0);
#pragma unroll
for (int j = 4; j < 8; ++j) sumi1 = ggml_cuda_dp4a(r.g[j], get_int_b4(bq8_1[iqs / 2].qs, j), sumi1);
const int sumi = (sumi0 * r.ls0 + sumi1 * r.ls1 + (sumi0 + sumi1) / 2) / 4;
const float d = r.dw * __low2float(bq8_1[iqs / 2].ds);
return d * sumi;
}
};
template<> struct Split<17> : SplitLs2 { // IQ2_XS
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq2_xs* bq2 = (const block_iq2_xs*) vbq + kbx;
const int2 q2_packed = make_int2(get_int_b2(bq2->qs, iqs + 0), get_int_b2(bq2->qs, iqs + 1));
const uint16_t* q2 = (const uint16_t*) &q2_packed;
W r;
r.ls0 = bq2->scales[iqs / 2] & 0x0F;
r.ls1 = bq2->scales[iqs / 2] >> 4;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const uint2 grid_pos = ((const uint2*) iq2xs_grid)[q2[l0 / 2] & 0x1FF];
const uint32_t signs = unpack_ksigns(q2[l0 / 2] >> 9);
const int signs0 = __vcmpne4(signs & 0x08040201, 0);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
const int signs1 = __vcmpne4(signs & 0x80402010, 0);
r.g[l0 + 1] = __vsub4(grid_pos.y ^ signs1, signs1);
}
r.dw = __half2float(bq2->d);
return r;
}
};
template<> struct Split<22> : SplitLs2 { // IQ2_S
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq2_s* bq2 = (const block_iq2_s*) vbq + kbx;
const int qs_packed = get_int_b2(bq2->qs, iqs / 2);
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = bq2->qh[iqs / 2];
const int signs_packed_32 = get_int_b2(bq2->qs, QK_K / 32 + iqs / 2);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
W r;
r.ls0 = bq2->scales[iqs / 2] & 0x0F;
r.ls1 = bq2->scales[iqs / 2] >> 4;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int* grid_pos = (const int*) (iq2s_grid + (qs[l0 / 2] | ((qh << (8 - l0)) & 0x300)));
const int signs0 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x03) << 7) | ((signs_packed_8[l0 / 2] & 0x0C) << 21), 0x00000000);
const int signs1 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x30) << 3) | ((signs_packed_8[l0 / 2] & 0xC0) << 17), 0x00000000);
r.g[l0 + 0] = __vsub4(grid_pos[0] ^ signs0, signs0);
r.g[l0 + 1] = __vsub4(grid_pos[1] ^ signs1, signs1);
}
r.dw = __half2float(bq2->d);
return r;
}
};
template<> struct Split<18> { // IQ3_XXS
struct W { int g[8]; int ls; float dw; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq3_xxs* bq3 = (const block_iq3_xxs*) vbq + kbx;
const int2 q3_packed = make_int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs + 1));
const uint8_t* q3 = (const uint8_t*) &q3_packed;
const uint32_t aux32 = get_int_b2(bq3->qs, QK_K / 16 + iqs / 2);
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int2 grid_pos = make_int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]);
const uint32_t signs = unpack_ksigns(aux32 >> (7 * l0 / 2));
const int signs0 = __vcmpne4(signs & 0x08040201, 0);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
const int signs1 = __vcmpne4(signs & 0x80402010, 0);
r.g[l0 + 1] = __vsub4(grid_pos.y ^ signs1, signs1);
}
r.ls = aux32 >> 28;
r.dw = __half2float(bq3->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
int sumi = 0;
#pragma unroll
for (int j = 0; j < 8; ++j) sumi = ggml_cuda_dp4a(r.g[j], get_int_b4(bq8_1[iqs / 2].qs, j), sumi);
sumi = (r.ls * sumi + sumi / 2) / 2;
const float d = r.dw * __low2float(bq8_1[iqs / 2].ds);
return d * sumi;
}
};
template<> struct Split<21> { // IQ3_S
struct W { int g[8]; int ls; float dw; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq3_s* bq3 = (const block_iq3_s*) vbq + kbx;
const int2 qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1));
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = bq3->qh[iqs / 2];
const int signs_packed_32 = get_int_b2(bq3->signs, iqs / 2);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int2 grid_pos = make_int2(iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],
iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);
const int signs0 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x03) << 7) | ((signs_packed_8[l0 / 2] & 0x0C) << 21), 0x00000000);
const int signs1 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x30) << 3) | ((signs_packed_8[l0 / 2] & 0xC0) << 17), 0x00000000);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
r.g[l0 + 1] = __vsub4(grid_pos.y ^ signs1, signs1);
}
r.ls = 1 + 2 * ((bq3->scales[iqs / 4] >> ((iqs << 1) & 0x04)) & 0x0F);
r.dw = __half2float(bq3->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
int sumi = 0;
#pragma unroll
for (int j = 0; j < 8; ++j) sumi = ggml_cuda_dp4a(r.g[j], get_int_b4(bq8_1[iqs / 2].qs, j), sumi);
sumi *= r.ls;
const float d = r.dw * __low2float(bq8_1[iqs / 2].ds);
return d * sumi;
}
};
template<> struct Split<29> { // IQ1_M
struct W { int g[8]; float delta[4]; int sc0, sc1; float dw; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq1_m* bq1 = (const block_iq1_m*) vbq + kbx;
const int qs_packed = get_int_b4(bq1->qs, iqs);
const uint8_t* qs = (const uint8_t*) &qs_packed;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int qhl = bq1->qh[2 * iqs + l0 / 4] >> (4 * ((l0 / 2) % 2));
const int grid = iq1s_grid_gpu[qs[l0 / 2] | ((qhl & 0x07) << 8)];
r.g[l0 + 0] = (grid >> 0) & 0x0F0F0F0F;
r.g[l0 + 1] = (grid >> 4) & 0x0F0F0F0F;
r.delta[l0 / 2] = -1.0f + IQ1M_DELTA - (qhl & 0x08) * (2.0f * IQ1M_DELTA / 0x08);
}
const uint16_t* sc = (const uint16_t*) bq1->scales;
iq1m_scale_t scale;
scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00F0) | ((sc[2] >> 4) & 0x0F00) | (sc[3] & 0xF000);
r.dw = __half2float(scale.f16);
const int tmp = sc[iqs / 2] >> (6 * (iqs % 2));
r.sc0 = 2 * ((tmp >> 0) & 0x07) + 1;
r.sc1 = 2 * ((tmp >> 3) & 0x07) + 1;
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
int sumi[2] = {0, 0};
float sumf[2] = {0.0f, 0.0f};
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
const int u0 = get_int_b4(bq8_1[iqs].qs, l0 + 0);
const int u1 = get_int_b4(bq8_1[iqs].qs, l0 + 1);
sumi[l0 / 4] = ggml_cuda_dp4a(r.g[l0 + 0], u0, sumi[l0 / 4]);
sumi[l0 / 4] = ggml_cuda_dp4a(r.g[l0 + 1], u1, sumi[l0 / 4]);
int sumy = 0;
sumy = ggml_cuda_dp4a(u0, 0x01010101, sumy);
sumy = ggml_cuda_dp4a(u1, 0x01010101, sumy);
sumf[l0 / 4] += r.delta[l0 / 2] * sumy;
}
const float d = r.dw * __low2float(bq8_1[iqs].ds);
return d * ((sumi[0] + sumf[0]) * r.sc0 + (sumi[1] + sumf[1]) * r.sc1);
}
};
template<> struct Split<20> { // IQ4_NL
struct W { int2 v[2]; float dw; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq4_nl* bq4 = (const block_iq4_nl*) vbq + kbx;
W r;
#pragma unroll
for (int l = 0; l < 2; ++l) r.v[l] = get_int_from_table_16(get_int_b2(bq4->qs, iqs + l), kvalues_iq4nl);
r.dw = __half2float(bq4->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
const int* q8 = (const int*) bq8_1->qs + iqs;
int sumi = 0;
#pragma unroll
for (int l = 0; l < 2; ++l) {
sumi = ggml_cuda_dp4a(r.v[l].x, q8[l + 0], sumi);
sumi = ggml_cuda_dp4a(r.v[l].y, q8[l + 4], sumi);
}
const float d = r.dw * __low2float(bq8_1->ds);
return d * sumi;
}
};
template<> struct Split<23> { // IQ4_XS
struct W { int2 v[4]; int ls; float dw; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_iq4_xs* bq4 = (const block_iq4_xs*) vbq + kbx;
W r;
#pragma unroll
for (int j = 0; j < 4; ++j) r.v[j] = get_int_from_table_16(get_int_b4(bq4->qs, iqs + j), kvalues_iq4nl);
r.ls = ((bq4->scales_l[iqs / 8] >> (iqs & 0x04)) & 0x0F) | (((bq4->scales_h >> (iqs / 2)) & 0x03) << 4);
r.dw = __half2float(bq4->d);
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
int sumi = 0;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const int u0 = get_int_b4(bq8_1[iqs / 4].qs, j + 0);
const int u1 = get_int_b4(bq8_1[iqs / 4].qs, j + 4);
sumi = ggml_cuda_dp4a(r.v[j].x, u0, sumi);
sumi = ggml_cuda_dp4a(r.v[j].y, u1, sumi);
}
sumi *= r.ls - 32;
const float d = r.dw * __low2float(bq8_1[iqs / 4].ds);
return d * sumi;
}
};
template<> struct Split<42> { // Q2_0
struct W { int qx[4], qy[4]; float d2; };
__device__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {
const block_q2_0* bq2_0 = (const block_q2_0*) vbq + kbx;
W r;
r.d2 = bq2_0->d;
const int16_t* qs = (const int16_t*) bq2_0->qs + iqs * 4;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const int q = qs[j];
const int qe = __byte_perm(0x020100FF, 0x020100FF, q >> 0);
const int qo = __byte_perm(0x020100FF, 0x020100FF, q >> 2);
r.qx[j] = __byte_perm(qe, qo, 0x5140);
r.qy[j] = __byte_perm(qe, qo, 0x7362);
}
return r;
}
__device__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {
const block_q8_1* bq8_1_chunk = bq8_1 + iqs;
int sumi = 0;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const int u = get_int_b4(bq8_1_chunk->qs, j * 2 + 0);
const int v = get_int_b4(bq8_1_chunk->qs, j * 2 + 1);
sumi = ggml_cuda_dp4a(u, r.qx[j], sumi);
sumi = ggml_cuda_dp4a(v, r.qy[j], sumi);
}
const float d8 = __low2float(bq8_1_chunk->ds);
return r.d2 * d8 * sumi;
}
};
// One row against the n <= NC activations x + off[0..n) (n >= 1, warp-uniform; offsets in q8_1 blocks, 32-bit to
// spare registers), the whole warp. Per activation this is row_dot: the same calls k, lane-strided the same way,
// summed in the same order, then the same warp_sum. Only the weight side moves out of the per-activation loop.
template<int TY, int NC>
__device__ __forceinline__ void row_dot_multi(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n,
int nb, int lane, float (&s)[NC]) {
using F = Fmt<TY>;
using S = Split<TY>;
#pragma unroll
for (int c = 0; c < NC; ++c) s[c] = 0.0f;
for (int k = lane; k < nb * F::ipb; k += 32) {
const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);
const typename S::W w = S::load(row, kbx, iqs);
#pragma unroll
for (int c = 0; c < NC; ++c)
if (c < n) s[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs);
}
#pragma unroll
for (int c = 0; c < NC; ++c)
if (c < n) s[c] = warp_sum(s[c]);
}
// mmvq_kernel with the columns taken NC at a time. After warp_sum every lane holds the same sum, so lane c stores
// column c.
template<int TY, int NC>
__global__ void __launch_bounds__(128) mmvq_multi_kernel(const uint8_t* __restrict__ w, size_t row_bytes,
const block_q8_1* __restrict__ x, float* __restrict__ y,
int n_in, int n_out, int ncols) {
const int row = blockIdx.x * 4 + threadIdx.y;
if (row >= n_out) return;
const int lane = threadIdx.x;
const int nb = n_in / Fmt<TY>::qk, xb = n_in / 32;
const uint8_t* wr = w + (size_t) row * row_bytes;
for (int c0 = 0; c0 < ncols; c0 += NC) {
const int n = min(NC, ncols - c0);
int off[NC];
#pragma unroll
for (int c = 0; c < NC; ++c) off[c] = (c0 + min(c, n - 1)) * xb;
float s[NC];
row_dot_multi<TY, NC>(wr, x, off, n, nb, lane, s);
#pragma unroll
for (int c = 0; c < NC; ++c)
if (c < n && lane == c) y[(size_t) (c0 + c) * n_out + row] = s[c];
}
}
// ---------------------------------------------------------------- grouped native experts
constexpr int GU_ROWS = 8; // rows per block (one warp each)
@@ -373,6 +699,43 @@ __global__ void __launch_bounds__(256) native_gu_kernel(const unsigned long long
}
}
// native_gu_kernel with the group's entries taken GRP_NC at a time, each weight part decoded once per pass.
// A group has at most one entry per token of the window (kVerifyMaxT = 8; setup writes --spec 4, and a split window
// has halves of <= 4), so 4 takes such windows in one pass; 8 would take ~64-80 registers against ~48 (ptxas -v).
constexpr int GRP_NC = 4;
template<int TG>
__global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned long long* __restrict__ grp_ptr,
const int32_t* __restrict__ grp_start,
const int32_t* __restrict__ n_groups,
const int32_t* __restrict__ ent_tok,
const block_q8_1* __restrict__ xq, NativeExpertLayout L,
float* __restrict__ gate, float* __restrict__ up) {
const int g = blockIdx.y;
if (g >= *n_groups) return;
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int row = blockIdx.x * GU_ROWS + warp; // 0 .. 2*n_ff
if (row >= 2 * L.n_ff) return;
const bool is_up = row >= L.n_ff;
const int r = is_up ? row - (int) L.n_ff : row;
const uint8_t* blob = (const uint8_t*) grp_ptr[g];
const uint8_t* wr = blob + (is_up ? L.up_off : 0) + (size_t) r * L.gu_row;
const int nb = (int) (L.n_embd / Fmt<TG>::qk), xb = (int) (L.n_embd / 32);
const int e0 = grp_start[g], e1 = grp_start[g + 1];
float* dst = is_up ? up : gate;
for (int e = e0; e < e1; e += GRP_NC) {
const int n = min(GRP_NC, e1 - e);
int off[GRP_NC];
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = ent_tok[e + min(c, n - 1)] * xb;
float s[GRP_NC];
row_dot_multi<TG, GRP_NC>(wr, xq, off, n, nb, lane, s);
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n && lane == c) dst[(size_t) (e + c) * L.n_ff + r] = s[c];
}
}
__global__ void swiglu_entries_kernel(const float* __restrict__ gate, const float* __restrict__ up, float* __restrict__ h,
long long n) {
const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x;
@@ -403,6 +766,36 @@ __global__ void __launch_bounds__(256) native_down_kernel(const unsigned long lo
}
}
// native_down_kernel with the entries taken GRP_NC at a time
template<int TD>
__global__ void __launch_bounds__(256) native_down_multi_kernel(const unsigned long long* __restrict__ grp_ptr,
const int32_t* __restrict__ grp_start,
const int32_t* __restrict__ n_groups,
const int32_t* __restrict__ ent_dst,
const block_q8_1* __restrict__ hq, NativeExpertLayout L,
float* __restrict__ out) {
const int g = blockIdx.y;
if (g >= *n_groups) return;
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int r = blockIdx.x * 8 + warp;
if (r >= L.n_embd) return;
const uint8_t* blob = (const uint8_t*) grp_ptr[g];
const uint8_t* wr = blob + L.down_off + (size_t) r * L.d_row;
const int nb = (int) (L.n_ff / Fmt<TD>::qk), hb = (int) (L.n_ff / 32);
const int e0 = grp_start[g], e1 = grp_start[g + 1];
for (int e = e0; e < e1; e += GRP_NC) {
const int n = min(GRP_NC, e1 - e);
int off[GRP_NC];
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = (e + min(c, n - 1)) * hb;
float s[GRP_NC];
row_dot_multi<TD, GRP_NC>(wr, hq, off, n, nb, lane, s);
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n && lane == c) out[(size_t) ent_dst[e + c] * L.n_embd + r] = s[c];
}
}
// ---------------------------------------------------------------- q8_1 (quantize.cu)
__global__ void quantize_q8_1_kernel(const float* __restrict__ x, block_q8_1* __restrict__ y, long long n) {
const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x;
@@ -603,8 +996,45 @@ __global__ void dequant_gu_kernel(int ty, const void* __restrict__ gate, const v
bool is_iq(int t) { return t == 16 || t == 17 || t == 18 || t == 20 || t == 21 || t == 22 || t == 23 || t == 29 || t == 42 || t == 11; }
bool env_on(const char* name) {
const char* v = std::getenv(name);
return v != nullptr && v[0] != '\0' && v[0] != '0';
}
// STRATA_OLD_IQ_MMVQ=1 keeps the per-column kernels (bitwise equal to the new ones; kept for A/B timing)
bool g_old_kernels = env_on("STRATA_OLD_IQ_MMVQ");
template<int TY>
void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int n_in, int n_out, int ncols,
cudaStream_t s) {
const dim3 grid((unsigned) ((n_out + 3) / 4)), block(32, 4);
if (g_old_kernels) mmvq_kernel<TY><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
else if (ncols <= 1) mmvq_multi_kernel<TY, 1><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
else if (ncols == 2) mmvq_multi_kernel<TY, 2><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
else if (ncols <= 4) mmvq_multi_kernel<TY, 4><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
else mmvq_multi_kernel<TY, 8><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); // 8 at a time past 8
}
template<int TG>
void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start,
const int32_t* n_groups, const int32_t* ent_tok, const block_q8_1* X, const NativeExpertLayout& L,
float* gate, float* up) {
if (g_old_kernels) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
else native_gu_multi_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
}
template<int TD>
void launch_down(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start,
const int32_t* n_groups, const int32_t* ent_dst, const block_q8_1* hq, const NativeExpertLayout& L,
float* out) {
if (g_old_kernels) native_down_kernel<TD><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);
else native_down_multi_kernel<TD><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);
}
} // namespace
void iq_set_old_kernels(bool old) { g_old_kernels = old; }
bool iq_old_kernels() { return g_old_kernels; }
bool iq_supported(int t) noexcept { return is_iq(t); }
size_t iq_row_bytes(int t, int64_t n) noexcept {
@@ -631,21 +1061,20 @@ void quantize_q8_1_rows(const float* x, int64_t n_rows, int64_t n_cols, void* y,
}
void iq_mmvq(int t, const void* w, const void* x_q8_1, float* y, int n_in, int n_out, int ncols, void* stream) {
const dim3 grid((unsigned) ((n_out + 3) / 4)), block(32, 4);
const size_t rb = iq_row_bytes(t, n_in);
cudaStream_t s = (cudaStream_t) stream;
const auto* W = (const uint8_t*) w;
const auto* X = (const block_q8_1*) x_q8_1;
switch (t) {
case 16: mmvq_kernel<16><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 17: mmvq_kernel<17><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 18: mmvq_kernel<18><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 20: mmvq_kernel<20><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 21: mmvq_kernel<21><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 22: mmvq_kernel<22><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 23: mmvq_kernel<23><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 29: mmvq_kernel<29><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 42: mmvq_kernel<42><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); break;
case 16: launch_mmvq<16>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 17: launch_mmvq<17>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 18: launch_mmvq<18>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 20: launch_mmvq<20>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 21: launch_mmvq<21>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 22: launch_mmvq<22>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 23: launch_mmvq<23>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 29: launch_mmvq<29>(W, rb, X, y, n_in, n_out, ncols, s); break;
case 42: launch_mmvq<42>(W, rb, X, y, n_in, n_out, ncols, s); break;
default: std::fprintf(stderr, "iq_mmvq: type %d is not supported\n", t); std::exit(1);
}
check("iq_mmvq");
@@ -721,14 +1150,14 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long
const auto* X = (const block_q8_1*) x_q8_1;
const dim3 ggu((unsigned) ((2 * L.n_ff + GU_ROWS - 1) / GU_ROWS), (unsigned) cap_groups);
switch (L.gu_type) {
case 16: native_gu_kernel<16><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 17: native_gu_kernel<17><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 18: native_gu_kernel<18><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 21: native_gu_kernel<21><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 22: native_gu_kernel<22><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 23: native_gu_kernel<23><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 29: native_gu_kernel<29><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 42: native_gu_kernel<42><<<ggu, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 16: launch_gu<16>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 17: launch_gu<17>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 18: launch_gu<18>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 21: launch_gu<21>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 22: launch_gu<22>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 23: launch_gu<23>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 29: launch_gu<29>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
case 42: launch_gu<42>(ggu, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;
default: std::fprintf(stderr, "native_expert_grouped: gate/up type %d\n", L.gu_type); std::exit(1);
}
check("native_expert_grouped/gu");
@@ -737,9 +1166,9 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long
quantize_q8_1_kernel<<<(unsigned) ((nh + 255) / 256), 256, 0, s>>>(h, hq, nh);
const dim3 gd((unsigned) ((L.n_embd + 7) / 8), (unsigned) cap_groups);
switch (L.d_type) {
case 20: native_down_kernel<20><<<gd, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;
case 23: native_down_kernel<23><<<gd, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;
case 42: native_down_kernel<42><<<gd, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;
case 20: launch_down<20>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;
case 23: launch_down<23>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;
case 42: launch_down<42>(gd, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;
default: std::fprintf(stderr, "native_expert_grouped: down type %d\n", L.d_type); std::exit(1);
}
check("native_expert_grouped/down");
+347
View File
@@ -0,0 +1,347 @@
// src/kernels/iq_multi_parity.cpp - the IQ kernels that decode each weight part once for every
// column / entry, against the older kernels that decode it again per column. Synthetic blocks, no model.
//
// build/iq_multi_parity the checks (a ctest; needs the GPU)
// build/iq_multi_parity --bench the checks, then old/new timings of both paths
//
// (1) iq_mmvq, every format it takes, ncols 1..8 and 11 (the > 8 split): the new output must be BITWISE equal to
// the old one (iq_set_old_kernels), and within float rounding of a double reference built from the GPU's own
// dequantizer (validated against gguf-py by iq_parity) and the dequantized q8_1 activations - a gross-error
// guard, not the contract.
// (2) native_expert_grouped, every gate/up format with the down formats: groups of 0..11 entries (more than a pass
// of GRP_NC), unused grid rows (cap_groups > groups), scattered destinations; bitwise, new vs old, the rows it
// must not write included.
//
// Random bytes are valid codes for every format here (all grid indices are in range); only the fp16 block scales
// are set, small enough that the grouped path's SwiGLU output keeps a finite fp16 q8_1 scale.
#include "strata/kernels/f16_bits.hpp"
#include "strata/kernels/iq_kernels.hpp"
#include <cuda_runtime.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <random>
#include <string>
#include <vector>
namespace k = strata::kernels;
namespace {
int g_fail = 0;
void ck(cudaError_t e, const char* w) {
if (e != cudaSuccess) {
std::fprintf(stderr, "CUDA error in %s: %s\n", w, cudaGetErrorString(e));
std::exit(2);
}
}
template<typename T>
T* dalloc(size_t n) {
T* p = nullptr;
ck(cudaMalloc(&p, n * sizeof(T) + 256), "malloc");
ck(cudaMemset(p, 0, n * sizeof(T) + 256), "memset");
return p;
}
const char* name_of(int t) {
switch (t) {
case 16: return "IQ2_XXS";
case 17: return "IQ2_XS";
case 18: return "IQ3_XXS";
case 20: return "IQ4_NL";
case 21: return "IQ3_S";
case 22: return "IQ2_S";
case 23: return "IQ4_XS";
case 29: return "IQ1_M";
case 42: return "Q2_0";
default: return "?";
}
}
int block_values(int t) { return t == 20 ? 32 : t == 42 ? 64 : 256; }
// `rows` rows of `n` values of format t: random bytes, then a finite fp16 scale in every block
std::vector<uint8_t> random_rows(int t, int64_t rows, int64_t n, std::mt19937& rng) {
const size_t rb = k::iq_row_bytes(t, n), bs = k::iq_row_bytes(t, block_values(t));
std::vector<uint8_t> w((size_t) rows * rb);
std::uniform_int_distribution<int> byte(0, 255), ex(2, 8), man(0, 1023), sgn(0, 3);
for (auto& b : w) b = (uint8_t) byte(rng);
for (size_t o = 0; o < w.size(); o += bs) {
if (t == 29) {
// IQ1_M: the fp16 scale is the top nibbles of the four scale words (bytes 48..55); nibble 3 carries the
// sign and the exponent's top bits: 0x1 / 0x2 (or 0x9 / 0xA) keeps it in 2^-11 .. 2^-3
const uint8_t nib = (uint8_t) ((sgn(rng) == 0 ? 0x8 : 0x0) | (1 + (byte(rng) & 1)));
w[o + 55] = (uint8_t) ((w[o + 55] & 0x0F) | (nib << 4));
} else {
const uint16_t h = (uint16_t) ((sgn(rng) == 0 ? 0x8000 : 0) | (ex(rng) << 10) | man(rng)); // 2^-13 .. 2^-6
std::memcpy(&w[o], &h, 2);
}
}
return w;
}
std::vector<float> random_x(size_t n, std::mt19937& rng) {
std::normal_distribution<float> nd(0.f, 1.f);
std::vector<float> x(n);
for (auto& v : x) v = nd(rng);
return x;
}
// q8_1 blocks (fp16 scale, fp16 sum, 32 int8) back to floats
std::vector<double> dequant_q8_1(const std::vector<uint8_t>& q, size_t n) {
std::vector<double> v(n);
for (size_t b = 0; b < n / 32; ++b) {
uint16_t dh;
std::memcpy(&dh, &q[b * 36], 2);
const double d = k::f32_from_f16(dh);
for (int i = 0; i < 32; ++i) v[b * 32 + i] = d * (int8_t) q[b * 36 + 4 + i];
}
return v;
}
// ------------------------------------------------------------------------------------------------ (1) iq_mmvq
void check_mmvq(int t, int n_in, int n_out, cudaStream_t s, std::mt19937& rng) {
const auto w = random_rows(t, n_out, n_in, rng);
uint8_t* dw = dalloc<uint8_t>(w.size());
ck(cudaMemcpy(dw, w.data(), w.size(), cudaMemcpyHostToDevice), "w");
// the double reference's weights: the GPU dequantizer (iq_parity checks it against gguf-py)
float* dwf = dalloc<float>((size_t) n_out * n_in);
k::iq_dequant_f32(t, dw, (int64_t) n_out * n_in, dwf, s);
std::vector<float> wf((size_t) n_out * n_in);
ck(cudaMemcpy(wf.data(), dwf, wf.size() * 4, cudaMemcpyDeviceToHost), "wf");
const int max_cols = 11;
const auto x = random_x((size_t) max_cols * n_in, rng);
float* dx = dalloc<float>(x.size());
ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
const size_t xq_bytes = (size_t) max_cols * (n_in / 32) * 36;
uint8_t* dxq = dalloc<uint8_t>(xq_bytes);
k::quantize_q8_1_rows(dx, max_cols, n_in, dxq, s);
std::vector<uint8_t> xq(xq_bytes);
ck(cudaMemcpy(xq.data(), dxq, xq_bytes, cudaMemcpyDeviceToHost), "xq");
const auto xd = dequant_q8_1(xq, (size_t) max_cols * n_in);
float* dy_old = dalloc<float>((size_t) max_cols * n_out);
float* dy_new = dalloc<float>((size_t) max_cols * n_out);
std::vector<float> y_old((size_t) max_cols * n_out), y_new(y_old.size());
const int cols[] = {1, 2, 3, 4, 5, 6, 7, 8, 11};
int bad = 0;
double worst = 0.0;
for (int nc : cols) {
ck(cudaMemset(dy_old, 0xFF, y_old.size() * 4), "memset");
ck(cudaMemset(dy_new, 0xFF, y_new.size() * 4), "memset");
k::iq_set_old_kernels(true);
k::iq_mmvq(t, dw, dxq, dy_old, n_in, n_out, nc, s);
k::iq_set_old_kernels(false);
k::iq_mmvq(t, dw, dxq, dy_new, n_in, n_out, nc, s);
ck(cudaStreamSynchronize(s), "sync");
ck(cudaMemcpy(y_old.data(), dy_old, y_old.size() * 4, cudaMemcpyDeviceToHost), "y_old");
ck(cudaMemcpy(y_new.data(), dy_new, y_new.size() * 4, cudaMemcpyDeviceToHost), "y_new");
size_t diff = 0;
for (size_t i = 0; i < y_old.size(); ++i) diff += std::memcmp(&y_old[i], &y_new[i], 4) != 0;
double num = 0, den = 0;
bool finite = true;
for (int c = 0; c < nc; ++c)
for (int r = 0; r < n_out; ++r) {
double acc = 0;
for (int i = 0; i < n_in; ++i) acc += (double) wf[(size_t) r * n_in + i] * xd[(size_t) c * n_in + i];
const float g = y_new[(size_t) c * n_out + r];
finite = finite && std::isfinite(g);
num += std::fabs(g - acc);
den += std::fabs(acc);
}
const double rel = num / (den + 1e-30);
worst = std::max(worst, rel);
if (diff != 0 || !finite || !(rel < 1e-2)) {
std::printf(" %-8s %5d x %5d ncols %2d: %zu outputs differ from the old kernel, ref rel %.2e%s FAIL\n",
name_of(t), n_out, n_in, nc, diff, rel, finite ? "" : ", non-finite");
++bad;
}
}
std::printf("%-8s type %2d %5d x %5d iq_mmvq ncols 1..8, 11: %s (ref rel <= %.2e)\n", name_of(t), t, n_out, n_in,
bad ? "FAIL" : "bitwise equal to the old kernel", worst);
g_fail += bad;
cudaFree(dw); cudaFree(dwf); cudaFree(dx); cudaFree(dxq); cudaFree(dy_old); cudaFree(dy_new);
}
// ---------------------------------------------------------------------------------- (2) native_expert_grouped
struct Grouped {
k::NativeExpertLayout L;
int n_groups = 0, n_ent = 0, cap_groups = 0, cap_ent = 0, n_tok = 0;
std::vector<uint8_t*> blobs;
unsigned long long* dptr = nullptr;
int32_t *dstart = nullptr, *dn = nullptr, *ddst = nullptr, *dtok = nullptr;
float* dx = nullptr;
uint8_t *dxq = nullptr, *dscr = nullptr;
float* dout = nullptr;
size_t out_floats = 0;
Grouped(int gu, int dt, int64_t H, int64_t FF, const std::vector<int>& counts, int tokens, std::mt19937& rng) {
L = k::native_expert_layout(gu, dt, H, FF);
n_groups = (int) counts.size();
cap_groups = n_groups + 2; // the extra grid rows must exit
n_tok = tokens;
std::vector<int32_t> start(1, 0), tok, dst;
for (int c : counts) start.push_back(start.back() + c);
n_ent = start.back();
cap_ent = n_ent + 3;
std::uniform_int_distribution<int> tk(0, tokens - 1);
for (int e = 0; e < n_ent; ++e) tok.push_back(tk(rng));
dst.resize(n_ent);
std::iota(dst.begin(), dst.end(), 0);
std::shuffle(dst.begin(), dst.end(), rng);
std::vector<unsigned long long> ptr;
for (int g = 0; g < n_groups; ++g) {
// one blob per group: gate rows | up rows (format gu, H values) | down rows (format dt, FF values)
auto b = random_rows(gu, 2 * FF, H, rng);
const auto d = random_rows(dt, H, FF, rng);
b.insert(b.end(), d.begin(), d.end());
if (b.size() != L.bytes) { std::fprintf(stderr, "blob size %zu != %zu\n", b.size(), L.bytes); std::exit(2); }
uint8_t* db = dalloc<uint8_t>(b.size());
ck(cudaMemcpy(db, b.data(), b.size(), cudaMemcpyHostToDevice), "blob");
blobs.push_back(db);
ptr.push_back((unsigned long long) db);
}
dptr = dalloc<unsigned long long>(cap_groups);
dstart = dalloc<int32_t>(cap_groups + 1);
dn = dalloc<int32_t>(1);
ddst = dalloc<int32_t>(cap_ent);
dtok = dalloc<int32_t>(cap_ent);
ck(cudaMemcpy(dptr, ptr.data(), ptr.size() * 8, cudaMemcpyHostToDevice), "ptr");
ck(cudaMemcpy(dstart, start.data(), start.size() * 4, cudaMemcpyHostToDevice), "start");
ck(cudaMemcpy(dn, &n_groups, 4, cudaMemcpyHostToDevice), "n");
if (n_ent) {
ck(cudaMemcpy(ddst, dst.data(), dst.size() * 4, cudaMemcpyHostToDevice), "dst");
ck(cudaMemcpy(dtok, tok.data(), tok.size() * 4, cudaMemcpyHostToDevice), "tok");
}
const auto x = random_x((size_t) tokens * H, rng);
dx = dalloc<float>(x.size());
ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
dxq = dalloc<uint8_t>((size_t) tokens * (H / 32) * 36);
dscr = dalloc<uint8_t>(k::native_expert_scratch_bytes(cap_ent, FF));
out_floats = (size_t) cap_ent * H;
dout = dalloc<float>(out_floats);
}
~Grouped() {
for (auto* b : blobs) cudaFree(b);
cudaFree(dptr); cudaFree(dstart); cudaFree(dn); cudaFree(ddst); cudaFree(dtok);
cudaFree(dx); cudaFree(dxq); cudaFree(dscr); cudaFree(dout);
}
void run(bool old, cudaStream_t s) {
k::iq_set_old_kernels(old);
k::quantize_q8_1_rows(dx, n_tok, L.n_embd, dxq, s);
k::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, cap_groups, cap_ent, dxq, dscr, dout, s);
k::iq_set_old_kernels(false);
}
std::vector<float> result(bool old, cudaStream_t s) {
ck(cudaMemset(dout, 0xFF, out_floats * 4), "memset"); // NaN rows: must stay NaN where nothing writes
ck(cudaMemset(dscr, 0, k::native_expert_scratch_bytes(cap_ent, L.n_ff)), "memset");
run(old, s);
ck(cudaStreamSynchronize(s), "sync");
std::vector<float> o(out_floats);
ck(cudaMemcpy(o.data(), dout, o.size() * 4, cudaMemcpyDeviceToHost), "out");
return o;
}
};
void check_grouped(int gu, int dt, int64_t H, int64_t FF, cudaStream_t s, std::mt19937& rng) {
// 0..11 entries per group: one pass, a partial pass, two passes and three passes of GRP_NC = 4
Grouped G(gu, dt, H, FF, {1, 3, 4, 5, 0, 8, 2, 11, 1}, 8, rng);
const auto a = G.result(true, s), b = G.result(false, s);
size_t diff = 0, written = 0;
bool finite = true;
for (size_t i = 0; i < a.size(); ++i) {
diff += std::memcmp(&a[i], &b[i], 4) != 0;
if (i < (size_t) G.n_ent * H) { ++written; finite = finite && std::isfinite(b[i]); }
}
const bool ok = diff == 0 && finite;
std::printf("%-8s/%-7s %5lld x %4lld native_expert_grouped, %d groups, %d entries: %s\n", name_of(gu),
name_of(dt), (long long) H, (long long) FF, G.n_groups, G.n_ent,
ok ? "bitwise equal to the old kernels" : "FAIL");
if (!ok) {
std::printf(" %zu of %zu floats differ%s\n", diff, a.size(), finite ? "" : ", non-finite outputs");
++g_fail;
}
}
// ------------------------------------------------------------------------------------------------ --bench
float time_ms(cudaStream_t s, int it, const auto& fn) {
cudaEvent_t e0, e1;
cudaEventCreate(&e0);
cudaEventCreate(&e1);
fn();
cudaEventRecord(e0, s);
for (int i = 0; i < it; ++i) fn();
cudaEventRecord(e1, s);
ck(cudaEventSynchronize(e1), "bench");
float ms = 0;
cudaEventElapsedTime(&ms, e0, e1);
cudaEventDestroy(e0);
cudaEventDestroy(e1);
return ms / it;
}
void bench(cudaStream_t s, std::mt19937& rng) {
std::printf("\n--bench: microseconds per call, old (per-column decode) / new (decode once), idle GPU assumed\n");
const int n_in = 2560, n_out = 8192, it = 200;
for (int t : {16, 17, 18, 21, 22, 29}) {
const auto w = random_rows(t, n_out, n_in, rng);
uint8_t* dw = dalloc<uint8_t>(w.size());
ck(cudaMemcpy(dw, w.data(), w.size(), cudaMemcpyHostToDevice), "w");
const auto x = random_x((size_t) 8 * n_in, rng);
float* dx = dalloc<float>(x.size());
ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
uint8_t* dxq = dalloc<uint8_t>((size_t) 8 * (n_in / 32) * 36);
k::quantize_q8_1_rows(dx, 8, n_in, dxq, s);
float* dy = dalloc<float>((size_t) 8 * n_out);
std::printf("%-8s iq_mmvq %d x %d:", name_of(t), n_out, n_in);
for (int nc = 1; nc <= 8; ++nc) {
float us[2];
for (int old = 1; old >= 0; --old) {
k::iq_set_old_kernels(old != 0);
us[old] = 1e3f * time_ms(s, it, [&] { k::iq_mmvq(t, dw, dxq, dy, n_in, n_out, nc, s); });
}
std::printf(" %d: %.1f/%.1f", nc, us[1], us[0]);
}
std::printf("\n");
k::iq_set_old_kernels(false);
cudaFree(dw); cudaFree(dx); cudaFree(dxq); cudaFree(dy);
}
// the verify window's shape: 2560 x 640 experts, 16 groups of m entries each
for (int gu : {16, 17, 18, 21, 22, 29}) {
std::printf("%-8s/Q2_0 grouped, 16 groups of m entries:", name_of(gu));
for (int m : {1, 2, 3, 4, 8}) {
Grouped G(gu, 42, 2560, 640, std::vector<int>(16, m), 8, rng);
float us[2];
for (int old = 1; old >= 0; --old)
us[old] = 1e3f * time_ms(s, 50, [&] { G.run(old != 0, s); });
std::printf(" m=%d: %.1f/%.1f", m, us[1], us[0]);
}
std::printf("\n");
}
}
} // namespace
int main(int argc, char** argv) {
const bool do_bench = argc > 1 && std::string(argv[1]) == "--bench";
cudaStream_t s;
ck(cudaStreamCreate(&s), "stream");
std::mt19937 rng(18);
for (int t : {16, 17, 18, 20, 21, 22, 23, 29, 42}) {
check_mmvq(t, 2560, 67, s, rng); // the model's n_embd; 67 rows: a partial block of 4 rows
check_mmvq(t, 1024, 5, s, rng);
}
for (int gu : {16, 17, 18, 21, 22, 23, 29, 42}) {
for (int dt : {20, 42}) check_grouped(gu, dt, 2560, 640, s, rng); // the model's shape
check_grouped(gu, 23, 1024, 512, s, rng); // IQ4_XS down needs n_ff % 256 == 0
}
if (do_bench) bench(s, rng);
std::printf("iq_multi_parity: %d failures\n", g_fail);
cudaStreamDestroy(s);
return g_fail ? 1 : 0;
}
+31 -14
View File
@@ -10,7 +10,8 @@
// What is compared, and how: the dequant parity is a MEASURED relative error (sum|got-ref| / sum|ref|) -
// bitwise equality is not claimed and not required; on the generated fixtures it measures 0.00e+00. The
// MMVQ dot (q8_1 activations) must match the float matrix-vector product over that reference within the
// activation rounding (a few 1e-3 relative).
// activation rounding (a few 1e-3 relative), for 1 to 8 columns, every column of a multi-column call bitwise
// equal to a one-column call on it.
#include "strata/kernels/iq_kernels.hpp"
#include "strata/kernels/native_mmvq.hpp"
@@ -18,6 +19,7 @@
#include <cmath>
#include <cstdio>
#include <cstring>
#include <random>
#include <string>
#include <vector>
@@ -61,27 +63,42 @@ int main(int argc, char** argv) {
for (size_t i = 0; i < ref.size(); ++i) { num += std::fabs(got[i] - ref[i]); den += std::fabs(ref[i]); }
dq_err = num / (den + 1e-30);
}
// the dot, two columns
// the dot, ncols 1..8: each column of an ncols call bitwise equal to a one-column call on it (the
// exact multi-column layout, the default), all of them within the activation rounding of the float product
const int MC = 8;
std::mt19937 rng(7);
std::normal_distribution<float> nd(0.f, 1.f);
std::vector<float> x((size_t) 2 * cols);
std::vector<float> x((size_t) MC * cols);
for (auto& v : x) v = nd(rng);
float* dx = nullptr;
void* xq = nullptr;
float* dy = nullptr;
cudaMalloc(&dx, x.size() * 4);
cudaMalloc(&xq, (size_t) 2 * cols / 32 * 36);
cudaMalloc(&dy, (size_t) 2 * rows * 4);
cudaMalloc(&xq, (size_t) MC * cols / 32 * 36);
cudaMalloc(&dy, (size_t) MC * rows * 4);
cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice);
strata::kernels::quantize_q8_1_rows(dx, 2, cols, xq, s);
strata::kernels::quantize_q8_1_rows(dx, MC, cols, xq, s);
std::vector<float> y((size_t) MC * rows), yn(y.size());
int multi_bad = 0;
try {
strata::kernels::native_mmvq(type, dw, xq, dy, cols, rows, 2, s);
for (int c = 0; c < MC; ++c)
strata::kernels::native_mmvq(type, dw, (const uint8_t*) xq + (size_t) c * cols / 32 * 36,
dy + (size_t) c * rows, cols, rows, 1, s);
cudaStreamSynchronize(s);
cudaMemcpy(y.data(), dy, y.size() * 4, cudaMemcpyDeviceToHost);
for (int nc = 2; nc <= MC; ++nc) {
strata::kernels::native_mmvq(type, dw, xq, dy, cols, rows, nc, s);
cudaStreamSynchronize(s);
cudaMemcpy(yn.data(), dy, yn.size() * 4, cudaMemcpyDeviceToHost);
if (strata::kernels::native_mmvq_multi_exact() &&
std::memcmp(yn.data(), y.data(), (size_t) nc * rows * 4) != 0) {
std::printf("%-8s mmvq ncols %d: not bitwise equal to the one-column calls\n", nm, nc);
++multi_bad;
}
}
} catch (const std::exception& e) { std::printf("%-8s mmvq: %s\n", nm, e.what()); ++failures; continue; }
std::vector<float> y((size_t) 2 * rows);
cudaStreamSynchronize(s);
cudaMemcpy(y.data(), dy, y.size() * 4, cudaMemcpyDeviceToHost);
double num = 0, den = 0;
for (int c = 0; c < 2; ++c)
for (int c = 0; c < MC; ++c)
for (int r = 0; r < rows; ++r) {
double acc = 0;
for (int k = 0; k < cols; ++k) acc += (double) ref[(size_t) r * cols + k] * x[(size_t) c * cols + k];
@@ -89,9 +106,9 @@ int main(int argc, char** argv) {
den += std::fabs(acc);
}
const double mm_err = num / (den + 1e-30);
const bool ok = dq_err < 1e-6 && mm_err < 2e-2;
std::printf("%-8s type %2d %4d x %5d dequant rel %.2e mmvq rel %.2e %s\n", nm, type, rows, cols, dq_err, mm_err,
ok ? "ok" : "FAIL");
const bool ok = dq_err < 1e-6 && mm_err < 2e-2 && multi_bad == 0;
std::printf("%-8s type %2d %4d x %5d dequant rel %.2e mmvq rel %.2e (ncols 1..%d) %s\n", nm, type, rows, cols,
dq_err, mm_err, MC, ok ? "ok" : "FAIL");
if (!ok) ++failures;
cudaFree(dw); cudaFree(dq); cudaFree(dx); cudaFree(xq); cudaFree(dy);
}
+14 -1
View File
@@ -4,7 +4,8 @@
//
// (a) float reference: ggml's own dequantizer (`to_float`) and a float SwiGLU expert, (b) the CPU path
// (ggml-cpu vec_dot with its quantized activations), (c) the GPU path (`native_expert_grouped`, q8_1
// activations). (b) and (c) each differ from (a) by their activation rounding only (a few 1e-3 relative).
// activations). (b) and (c) each differ from (a) by their activation rounding only (a few 1e-3 relative). (c) runs
// twice, with the kernels that decode a weight part once for all entries and with the per-entry ones: bitwise equal.
#include "strata/artifact/gguf_reader.hpp"
#include "strata/kernels/cpu/native_expert.hpp"
#include "strata/kernels/cpu/expert.hpp"
@@ -276,6 +277,18 @@ int main(int argc, char** argv) {
strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s);
cudaStreamSynchronize(s);
cudaMemcpy(got_g.data(), dout, got_g.size() * 4, cudaMemcpyDeviceToHost);
{ // the kernels that decode a weight part once for the NT entries, bitwise vs the per-entry ones
std::vector<float> old_g(got_g.size());
const bool was = strata::kernels::iq_old_kernels();
strata::kernels::iq_set_old_kernels(!was);
strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s);
strata::kernels::iq_set_old_kernels(was);
cudaStreamSynchronize(s);
cudaMemcpy(old_g.data(), dout, old_g.size() * 4, cudaMemcpyDeviceToHost);
const bool same = std::memcmp(old_g.data(), got_g.data(), got_g.size() * 4) == 0;
std::printf(" gpu decode-once vs per-entry kernels: %s\n", same ? "bitwise equal" : "MISMATCH");
if (!same) ++failures;
}
cudaFree(dblob); cudaFree(dx); cudaFree(dxq); cudaFree(dscr); cudaFree(dout); cudaFree(dptr);
cudaFree(dstart); cudaFree(dn); cudaFree(ddst); cudaFree(dtok);
}