From aeedae40271675c1915e64be5c8ccfe1da3412db Mon Sep 17 00:00:00 2001 From: Constantin Date: Sun, 25 Oct 2026 07:08:08 -0400 Subject: [PATCH] Pack per-128 I4 experts for coalesced one-token GEMMs Change only the per-128-scale load-time expert layout to 16-column/64-K fragment tiles. Add the register-loaded one-row kernel, keep staged prefill compatible, and leave subgroup-scaled codebooks row-major. Update the stored-layout hashes atomically with the layout and add independent canonical-nibble address checks. --- libr4d/r4d_args.h | 12 ++-- libr4d/r4d_layout.cpp | 55 +++++++++++++--- libr4d/r4d_moe.hip | 114 +++++++++++++++++++++++++++++++-- libr4d/r4d_rows.cpp | 5 +- libr4d/r4d_selftest.cpp | 14 ++-- tests/relayout_parity_test.cpp | 36 ++++++++--- 6 files changed, 200 insertions(+), 36 deletions(-) diff --git a/libr4d/r4d_args.h b/libr4d/r4d_args.h index d6e004c5..de4ca076 100644 --- a/libr4d/r4d_args.h +++ b/libr4d/r4d_args.h @@ -249,13 +249,13 @@ enum { R4D_DT_W2_FRAG = RAD_DT_PLUGIN_BASE + 4, R4D_DT_MXFP4_FRAG = RAD_DT_PLUGIN_BASE + 5, R4D_DT_E8M0 = RAD_DT_PLUGIN_BASE + 6, /* [K/32][N] shared exponents, and Wref[N] */ - /* The grouped MoE GEMM's 4-bit expert weight: row-major packed nibbles, each a code into a - * sixteen-entry table, expanded to E4M3 on the way into LDS. Not R4D_DT_W4_FRAG, which is a - * WMMA fragment permutation for a kernel loading its weight from global; this one stages - * through LDS because its rows are gathered. See r4d_layout.cpp. */ + /* The grouped MoE GEMM's 4-bit expert weight. I4 at per-128 BF16 scales uses 16-column/64-K + * fragment tiles for coalesced register loads and the staged kernels' row-segment gather. + * Subgroup-scaled codebooks keep row-major packed nibbles. Both use an exact E4M3 table; the layout + * tag distinguishes their storage from each other and R4D_DT_W4_FRAG. See r4d_layout.cpp. */ R4D_DT_MOEW4 = RAD_DT_PLUGIN_BASE + 7, - /* The same GEMM's 5-bit expert weight: per row, R4D_DT_MOEW4's nibble plane of the codes' low - * four bits (the magnitude), then the sign bits, one dword per 32 elements in the order the + /* The same GEMM's 5-bit expert weight: per row, the codebook layout's nibble plane of the + * codes' low four bits (the magnitude), then the sign bits, one dword per 32 elements in the order the * kernel merges them (r4d_layout.cpp, moew5_row). A row is 5K/8 bytes. */ R4D_DT_MOEW5 = RAD_DT_PLUGIN_BASE + 8, /* The M2XFP4 GEMMs' stored weight: R4D_DT_MXFP4_FRAG's fragment-order e2m1 codes, then the diff --git a/libr4d/r4d_layout.cpp b/libr4d/r4d_layout.cpp index 28458590..69bae64c 100644 --- a/libr4d/r4d_layout.cpp +++ b/libr4d/r4d_layout.cpp @@ -1096,10 +1096,11 @@ extern "C" int r4d_rad_relayout_m2xfp4(const RadParam* p, int n_p, int operand, return RAD_OK; } -/* ---- the MoE experts. The weight is staged through LDS, so it is not permuted: the int4 plane is - * the canonical one with each dword's nibbles reordered for the expansion (byte b holds element b - * low and element b+4 high), and the fp8 plane is the canonical one unchanged. `w4a8h` is the same - * plane with the weight Hadamard-rotated per group, accepted only from an encoding that says so. */ +/* ---- the MoE experts. I4 at BF16 scales per 128 K uses 16-column/64-K fragment tiles, + * coalescing a wave's register loads without another copy of the weight. The staged prefill + * kernels read the same tiles back in row segments. Subgroup-scaled codebooks keep their + * row-major nibble permutation, and FP8 is unchanged. `w4a8h` has the same packing with the weight Hadamard-rotated per group, + * accepted only from an encoding that says so. */ namespace { int moew4_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) { @@ -1137,12 +1138,14 @@ int moew4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* : !selects(enc, sel, n, "codes")) return RAD_E_SHAPE; if (!dims(pl[0], g.N, g.K)) return RAD_E_SHAPE; + if (!g64 && (g.N % 16 || g.K % 64)) return RAD_E_SHAPE; if (w5) { fill(out, "r4d.moew5nh64.w", R4D_DT_MOEW5, g.N, g.K, g.N * (g.K / 8) * 5); return RAD_OK; } - fill(out, g64 == 4 ? "r4d.moew4nh32.w" : g64 ? "r4d.moew4nh64.w" : nl ? "r4d.moew4nh.w.g128" - : moe_w4_rot(p, n_p) ? "r4d.moew4h.w.g128" : "r4d.moew4.w.g128", + fill(out, g64 == 4 ? "r4d.moew4nh32.w" : g64 ? "r4d.moew4nh64.w" + : nl ? "r4d.moew4nh.w.t16k64.g128" + : moe_w4_rot(p, n_p) ? "r4d.moew4h.w.t16k64.g128" : "r4d.moew4.w.t16k64.g128", R4D_DT_MOEW4, g.N, g.K, g.N * g.K / 2); return RAD_OK; } @@ -1196,10 +1199,39 @@ void moew4_permute(const uint8_t* s, uint8_t* d, int64_t bytes, bool forward) { std::memcpy(d + i, &w, 4); } } +/* Signed-I4 fragment tiles: [N/16][K/64][lane=32][step=4] dwords. A lane owns column + * lane%16 and K bytes step*16 + (lane/16)*8 .. +7, in the nibble order above. Two adjacent + * canonical dwords supply the low/high lane halves; the inverse restores the exact source codes. + * No precision or byte-count change, and no second resident weight plane. */ +void moew4_frag_permute(const uint8_t* src, uint8_t* dst, int64_t N, int64_t K, bool forward) { + for (int64_t nt = 0; nt < N / 16; ++nt) + for (int64_t kb = 0; kb < K / 64; ++kb) + for (int64_t n = 0; n < 16; ++n) + for (int64_t s = 0; s < 4; ++s) { + const size_t canonical = (size_t)((nt * 16 + n) * (K / 2) + kb * 32 + s * 8); + const size_t packed = (size_t)(((nt * (K / 64) + kb) * 32 + n) * 16 + s * 4); + uint64_t x; + if (forward) { + std::memcpy(&x, src + canonical, sizeof x); + x = moew4_nibbles(moew4_bytes(x)); + const uint32_t lo = (uint32_t)x, hi = (uint32_t)(x >> 32); + std::memcpy(dst + packed, &lo, sizeof lo); + std::memcpy(dst + packed + 256, &hi, sizeof hi); + } else { + uint32_t lo, hi; + std::memcpy(&lo, src + packed, sizeof lo); + std::memcpy(&hi, src + packed + 256, sizeof hi); + x = moew4_bytes(moew4_nibbles((uint64_t)lo | ((uint64_t)hi << 32))); + std::memcpy(dst + canonical, &x, sizeof x); + } + } +} + /* One w5nl64a8h row between container and kernel. Stored, a row is K five-bit codes, LSB first * (rad_plugin.h packing), so every eight codes are five bytes. Served, it is the codes' low nibbles - * in canonical 4-bit order (two per byte, element 2i low) put through moew4_permute as w4nla8h's - * are, then their high bits, one dword per 32 elements: K/2 + K/8 bytes, the same count. + * in canonical 4-bit order (two per byte, element 2i low) put through moew4_permute, then their + * high bits, one dword per 32 elements: K/2 + K/8 bytes, the same count. w4nla8h does not use this + * row: per-128 BF16-scale I4, including that codebook, uses the 16-column/64-K fragment tiles. * * The sign dword is transposed for the kernel's merge: element 4m+b of a 32 (byte b of the * expansion's output dword m) is bit 7-m of byte b, so `sg << m` aligns dword m's four signs with @@ -1285,7 +1317,8 @@ int moew4_relayout_by(MoeViewFn view, const RadParam* p, int n_p, int operand, return RAD_OK; } if (moe_w4_nl(v.p, v.n) && !moew4_table_is_kernels(pl[1])) return RAD_E_DTYPE; - moew4_permute(pdata(pl[0]), (uint8_t*)dst, g.N * (g.K / 2), true); + if (moe_w4_g64(v.p, v.n)) moew4_permute(pdata(pl[0]), (uint8_t*)dst, g.N * (g.K / 2), true); + else moew4_frag_permute(pdata(pl[0]), (uint8_t*)dst, g.N, g.K, true); return RAD_OK; } @@ -1313,7 +1346,9 @@ int moew4_unrelayout_by(MoeViewFn view, const RadParam* p, int n_p, int operand, std::memcpy(pl[1].data, kW5nlTable, sizeof kW5nlTable); return RAD_OK; } - moew4_permute((const uint8_t*)src, (uint8_t*)pl[0].data, g.N * (g.K / 2), false); + if (moe_w4_g64(v.p, v.n)) + moew4_permute((const uint8_t*)src, (uint8_t*)pl[0].data, g.N * (g.K / 2), false); + else moew4_frag_permute((const uint8_t*)src, (uint8_t*)pl[0].data, g.N, g.K, false); /* The table the stored codes index is the kernel's: the relayout refused any other. */ if (moe_w4_nl(v.p, v.n)) std::memcpy(pl[1].data, kW4nlTable, sizeof kW4nlTable); return RAD_OK; diff --git a/libr4d/r4d_moe.hip b/libr4d/r4d_moe.hip index 8d8d5e07..8ed82a09 100644 --- a/libr4d/r4d_moe.hip +++ b/libr4d/r4d_moe.hip @@ -537,6 +537,22 @@ __device__ __forceinline__ Stage8 stage8_load_w(const uint8_t* __restrict__ src, } return r; } +/* A 32-K row segment from the per-128-scale I4 fragment tiles, for the LDS-staged kernels. + * Two 64-bit loads gather its four dwords; subgroup-scaled codebooks retain their row loads. */ +template +__device__ __forceinline__ uint4 moe_w4_load(const uint8_t* base, uint32_t row, uint32_t ld, + uint32_t k, bool live) { + if constexpr (W4 != 1) return w4_load_w(base + (size_t)row * ld + k / 2, live); + if (!live) return make_uint4(0, 0, 0, 0); + const size_t word = (((size_t)(row / 16) * (ld / 32) + k / 64) * 32 + row % 16) * 4 + + (k % 64) / 16; + typedef __attribute__((address_space(1))) const uint64_t* gu64; + const auto* p = (const uint64_t*)(base + word * 4); + const uint64_t lo = UW ? *(gu64)p : *p; + const uint64_t hi = UW ? *(gu64)(p + 32) : p[32]; + return make_uint4((uint32_t)lo, (uint32_t)hi, (uint32_t)(lo >> 32), (uint32_t)(hi >> 32)); +} + /* A staged tile row as loaded, or the zero a dead row stages. */ __device__ __forceinline__ Stage8 stage8_keep(const Stage8& s, bool live) { Stage8 r; @@ -786,7 +802,8 @@ void moe_gemm_q_kernel( ew_ok[p] = (j0 + r) < fc; const uint8_t* wr = ew_wp + (size_t)(ew_ok[p] ? wn0 + r : 0u) * C.w_ld; if constexpr (W4) { - ew_b4[p] = w4_load_w(wr + (sk >> 1), true); + ew_b4[p] = moe_w4_load(ew_wp, ew_ok[p] ? wn0 + r : 0u, + C.w_ld, sk, true); if constexpr (W4 == 4) ew_sp[p] = u32_load_w((const uint32_t*)(wr + (C.k >> 1) + (sk >> 3))); } else @@ -1189,7 +1206,8 @@ void moe_gemm_q_kernel( const uint8_t* wr = Wp + (size_t)(kUL && !ok ? wn0 : wn0 + r) * w_ld; if constexpr (kUL) bok[p] = ok; if constexpr (W4) { - bpre4[p] = w4_load_w(wr + ((cc + sk) >> 1), kUL || ok); + bpre4[p] = moe_w4_load(Wp, kUL && !ok ? wn0 : wn0 + r, + w_ld, cc + sk, kUL || ok); if constexpr (W4 == 4) spre[p] = kUL || ok ? u32_load_w((const uint32_t*)(wr + (K >> 1) + ((cc + sk) >> 3))) @@ -1628,7 +1646,7 @@ __global__ __launch_bounds__(4 * kWave) void moe_down_dec_kernel( const bool ok = (j0 + r) < fc; if constexpr (W4) { const uint8_t* wr = Wp + (size_t)(wn0 + r) * w_ld; - bpre4[p] = w4_load_w(wr + ((cc + sk) >> 1), ok); + bpre4[p] = moe_w4_load(Wp, wn0 + r, w_ld, cc + sk, ok); if constexpr (W4 == 4) spre[p] = ok ? u32_load_w((const uint32_t*)(wr + (K >> 1) + ((cc + sk) >> 3))) @@ -1751,6 +1769,75 @@ __global__ __launch_bounds__(4 * kWave) void moe_down_dec_kernel( } } +/* One or a few routed rows at I4 with BF16 scales per 128 K (signed or a 16-entry codebook). + * A block owns one sorted row and 64 columns; + * the two halves of each wave read the same activation row and their own eight K bytes directly + * into WMMA registers. Fragment-tiled weights are coalesced 128-bit loads, spent four times, so no + * retiling, LDS staging, run scan or workgroup barrier is needed. Only accumulator row zero is + * folded/stored. Keep the same eight WMMAs per group and raw * activation_scale * weight_scale + * order as the tiled kernels; the discarded padded rows cannot affect that row. + * + * Sorted rows are resolved independently, so singleton, repeated and empty expert runs, both + * activation orders, parity/mod4 classes and dropped slots all retain their ordinary meaning. */ +__global__ __launch_bounds__(4 * kWave) void moe_w4_rows_kernel( + const uint8_t* __restrict__ Aq, const float* __restrict__ As, + const MoeCls c_even, const MoeCls c_odd, const MoeCls c_2, const MoeCls c_3, + const int32_t* __restrict__ sorted, const int32_t* __restrict__ off, + uint16_t* __restrict__ Y, uint32_t T, uint32_t N, uint32_t n_expert, uint32_t top_k, + uint32_t a_ld, uint32_t as_ld, uint32_t y_ld, uint32_t mode, const MoeTab tab) { + const uint32_t row = blockIdx.x, n0 = blockIdx.y * 64u; + const uint32_t tid = threadIdx.x, lane = tid % kWave, wave = tid / kWave; + if (row >= T || n0 >= N) return; + if (row >= (uint32_t)off[n_expert]) { + if (tid < 64 && n0 + tid < N) Y[(size_t)row * y_ld + n0 + tid] = 0; + return; + } + uint32_t lo = 0, hi = n_expert; + while (lo + 1 < hi) { + const uint32_t mid = (lo + hi) / 2; + if ((uint32_t)off[mid] <= row) lo = mid; + else hi = mid; + } + const uint32_t e = (uint32_t)__builtin_amdgcn_readfirstlane((int)lo); + const uint32_t cshift = (mode >> 1) & 3u, parts = mode >> 8; + const uint32_t cls = e & ((1u << cshift) - 1u); + const MoeCls& C = cls == 0 ? c_even : cls == 1 ? c_odd : cls == 2 ? c_2 : c_3; + const uint32_t fy = N / parts, fc = C.n / parts; + const uint32_t part = n0 / fy, j0 = n0 - part * fy; + if (j0 >= fc) { + if (tid < 64 && n0 + tid < N) Y[(size_t)row * y_ld + n0 + tid] = 0; + return; + } + const uint32_t arow = (mode & 1u) ? row : (uint32_t)sorted[row] / top_k; + const uint32_t col = wave * 16 + frag_c(lane), wn0 = part * fc + j0; + const uint32_t nt = (uint32_t)__builtin_amdgcn_readfirstlane((int)(wn0 / 16 + wave)); + const uint8_t* Wp = uw_ptr(wtab_at(C.w, C.w_s0, e >> cshift)); + const uint16_t* Ws = uw_ptr((const uint16_t*)wtab_at(C.s, C.s_s0, e >> cshift)); + float out = 0.f; + for (uint32_t g = 0; g < C.k / kBlock; ++g) { + const float as = As[(size_t)arow * as_ld + g]; + const float ws = bscale(u16_load_w(Ws + (size_t)(wn0 + col) * C.s_ld + g)); + f8v raw = acc_zero(); +#pragma unroll + for (uint32_t b = 0; b < 2; ++b) { + const r4d_moe_u4v words = *((r4d_moe_gu4)Wp + + ((size_t)nt * (C.w_ld / 32) + 2 * g + b) * kWave + lane); +#pragma unroll + for (uint32_t s = 0; s < 4; ++s) { + const uint32_t kk = g * kBlock + b * 64 + s * 16 + r4d_fp8::frag_k0(lane); + const uint2 a = *(const uint2*)(Aq + (size_t)arow * a_ld + kk); + const uint32_t w = words[s]; + const i2v af = { (int)a.x, (int)a.y }; + const i2v bf = { (int)w4_e4m3(w & 0x0f0f0f0fu, tab.v), + (int)w4_e4m3((w >> 4) & 0x0f0f0f0fu, tab.v) }; + raw = mma(af, bf, raw); + } + } + out += raw[0] * as * ws; + } + if (lane < 16 && n0 + col < N) Y[(size_t)row * y_ld + n0 + col] = f32_to_bf16(out); +} + /* ================================================================== moe_gemm, bf16 * * y[i, :] = a[row(i), :] @ w[e]^T in sorted order, bf16 operands and one f32 accumulator over all @@ -2703,7 +2790,7 @@ static int moe_gemm_launch(const RadArgs* a, RadStream stream, int ncls = 2) { /* The scale plane is one bf16 per (row, 128 of K), not per 128x128 tile. */ rc = wtab_of(ws, RAD_BF16, 16, cnt, n, k / kBlock, &c->s, &s_s0, &ws_ld); if (rc != RAD_OK) return rc; - if (w_ld % 2) return R4D_NO(RAD_E_STRIDE); + if (w_ld < k || w_ld % 64) return R4D_NO(RAD_E_STRIDE); // whole 64-K fragment tiles w_ld /= 2; } else { rc = wtab_of(ws, RAD_BF16, 16, cnt, (n + kBlock - 1) / kBlock, k / kBlock, &c->s, @@ -2738,6 +2825,25 @@ static int moe_gemm_launch(const RadArgs* a, RadStream stream, int ncls = 2) { MoeTab tabv{}; for (int i = 0; i < 4; ++i) tabv.v[i] = tab[i]; + /* The one-token per-128-scale I4 path needs no LDS tile or run scan. Pins select the original + * forms, which are also its byte-for-byte regression oracle. Larger row counts retain the + * existing decode/prefill bands; a small row count alone is not evidence of their crossover. */ + if constexpr (W4 == 1) { + if (T <= top_k && T <= 32 && g_moe_prefill_form == 0 && + g_moe_down_dec_pin == 0 && g_moe_fk_pin == 0) { + emit_rc = R4D_EMIT(stream, (moe_w4_rows_kernel), + dim3((unsigned)T, (unsigned)(N / 64)), dim3(4 * kWave), 0, + (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, + (const int32_t*)sorted->data, (const int32_t*)off->data, + (uint16_t*)Y->data, (uint32_t)T, (uint32_t)N, + (uint32_t)n_expert, (uint32_t)top_k, + (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); + if (emit_rc != RAD_OK) return emit_rc; + R4D_MOE_CHK(); + return RAD_OK; + } + } + /* The band: a decode step takes the (tile, run) form (the kernel's ES note) and runs of several * rows take the aligned 32-row form; r4d_moe_decode_band has the crossover. */ const uint32_t ny = (uint32_t)(N / 64); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 85c2053a..a75f0991 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -8155,8 +8155,9 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_shape_moe_gemm_q }, { "moe_gemm_w4a8", "moe_gemm_q", "moe", - "the same grouped GEMM with the expert weight at FOUR BITS: row-major packed nibbles expanded to " - "E4M3 on the way into LDS, so the WMMA inner loop is the fp8 kernel's untouched", + "grouped FOUR-BIT GEMM: signed I4 in 16-column/64-K fragment tiles, loaded directly into " + "WMMA registers for one-token routing or staged through LDS otherwise; codebook formats keep " + "row-major packed nibbles expanded to E4M3, with the fp8 kernel's WMMA arithmetic", "N a multiple of 64, K a multiple of 128, group 128; the same 16-row/32-row band, and the same " "argument for it", "E4M3 activation with an f32 scale per 128 of K, symmetric int4 weight (codes -8..7) with a " diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index b771f9d4..efe36392 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -21492,8 +21492,9 @@ static const MoeDnShape kMoeDnShapes[] = { * * A checker reads `moe_gemm_w4a8`'s weight through the row's inverse, so the inverse must return * exactly the planes the relayout was given. The arrangement is checked against r4d_layout.cpp's - * statement, not either hook: byte b of a packed dword holds element b low and b+4 high, two's - * complement. The scale plane is stored as is, so its hook must answer RAD_E_UNSUPPORTED + * statement, not either hook: signed-I4 tiles are [N/16][K/64][lane=32][step=4] dwords; + * a lane owns column lane%16 and eight K elements at step*16+(lane/16)*8. Within a dword, + * byte b holds element b low and b+4 high. The scale plane is stored as is, so its hook must answer RAD_E_UNSUPPORTED * ("nothing to arrange"). */ static void moe_w4_unlayout(const Plugin& pl) { const char* what = "moe_gemm_w4a8's arranged plane, vs the stated format and its inverse"; @@ -21524,7 +21525,10 @@ static void moe_w4_unlayout(const Plugin& pl) { for (int64_t n = 0; n < N; ++n) for (int64_t k = 0; k < K; ++k) { const int64_t pos = k & 7; - const int64_t byte = n * (K / 2) + (k >> 3) * 4 + (pos & 3); + const int64_t lane = n % 16 + ((k % 16) / 8) * 16; + const int64_t step = (k % 64) / 16; + const int64_t word = (((n / 16) * (K / 64) + k / 64) * 32 + lane) * 4 + step; + const int64_t byte = word * 4 + (pos & 3); const int nib = (wq[(size_t)byte] >> (pos >= 4 ? 4 : 0)) & 15; const int code = nib >= 8 ? nib - 16 : nib; if (code != code_at(p, n, k)) ++bad_code; @@ -21535,8 +21539,8 @@ static void moe_w4_unlayout(const Plugin& pl) { std::to_string(bad_code) + " code(s) not where the format puts them" + (back ? std::string() : "; inverse: " + why)); - /* w5nl64a8h's row against the same statement: the low four bits of every code where w4a8's - * nibble would be, in a K/2 + K/8-byte row, and the high bit (the sign) of element 4m+b of a + /* w5nl64a8h keeps the same within-dword nibble permutation in a row-major K/2 + K/8-byte + * row (not the signed-I4 fragment tiles), and the high bit (the sign) of element 4m+b of a * 32 at bit 7-m of byte b of that 32's dword, from byte K/2 on. The table is the kernel's, and * the inverse restores both planes. */ const char* what5 = "moe_gemm_w4a8's five-bit arranged plane, vs the stated format and its " diff --git a/tests/relayout_parity_test.cpp b/tests/relayout_parity_test.cpp index b5a3a364..af2dd8a6 100644 --- a/tests/relayout_parity_test.cpp +++ b/tests/relayout_parity_test.cpp @@ -3,10 +3,10 @@ * * A container holds a quantiser's canonical planes; the kernel only arranges them at load * (spec.md ยง4.3). For every stored format, libquant's planes passed through the kernel's relayout - * are pinned to a recorded FNV-1a hash of the stored bytes. The hashes match the containers in - * service, so a recipe reconverts one without moving a served bit, and a relayout or quantiser - * edit that changes a format fails here instead of as a quality drift no downstream test can - * see. Where the kernel has an inverse, it must give the planes back exactly. + * are pinned to a recorded FNV-1a hash of the load-time bytes. A deliberate kernel packing change + * updates those hashes, not the container: its canonical codes must still round-trip exactly. + * Fragment-tiled MoE I4 also checks every nibble's address independently of the packing hooks, + * so updating a golden cannot hide a matching forward/inverse permutation error. * * libr4d needs a HIP toolchain to build but its layouts do not, so their host sources are compiled * into this test (tests/CMakeLists.txt). The n-gram table's hook is libavx's, from the plugin. One @@ -105,6 +105,24 @@ uint64_t stored(const char* label, const Hooks& nf, Opts kp, const char* quant, CHECK_EQ(nf.relayout(kp.data(), kp.n(), op.operand, &q.enc, s.sel.data(), s.t.data(), (int)s.t.size(), got.data(), L.bytes), RAD_OK); } + if (lrc == RAD_OK && L.tag && std::strstr(L.tag, ".w.t16k64.g128")) { + /* Kernel meaning, not the relayout algorithm: lane%16 owns output column n, the + * lane's half owns eight K values, byte j holds k+j low and k+j+4 high. */ + CHECK_EQ(got.size(), (size_t)(R * C / 2)); + if (got.size() != (size_t)(R * C / 2)) return 0; + const auto& codes = q.planes[(size_t)s.sel[0]]; + int64_t bad = 0; + for (int64_t n = 0; n < R; ++n) + for (int64_t k = 0; k < C; ++k) { + const int64_t lane = n % 16 + ((k % 16) / 8) * 16; + const size_t at = (size_t)(((n / 16 * (C / 64) + k / 64) * 32 + lane) + * 16 + (k % 64 / 16) * 4 + k % 4); + const int a = (got[at] >> ((k % 8 / 4) * 4)) & 15; + const int b = (codes[(size_t)(n * (C / 2) + k / 2)] >> ((k % 2) * 4)) & 15; + bad += a != b; + } + CHECK_EQ(bad, 0LL); + } h = fnv(got, h); /* ...and back, where the kernel can be read back at all */ @@ -122,7 +140,7 @@ uint64_t stored(const char* label, const Hooks& nf, Opts kp, const char* quant, return h; } -/* A golden: the hash of the stored bytes as the containers in service hold them. */ +/* A golden of the kernel's load-time bytes, not the container's canonical planes. */ void golden(const char* label, uint64_t got, uint64_t want) { if (got != want) std::fprintf(stderr, " %s: stored hash 0x%016llxull, recorded 0x%016llxull\n", label, @@ -446,7 +464,7 @@ TEST(moe_int4_experts_plain_and_rotated_both_classes) { { { B, { "codes" } }, { BSCALE, { "scale" } } }), stored(dt, nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1, { { 6, { "codes" } }, { 7, { "scale" } } })); - golden(dt, h, rot ? 0xcc329df22f1f6c40ull : 0x15a0093ea83a5a90ull); + golden(dt, h, rot ? 0x4acff22a616a9476ull : 0x565ae6cfe9b94a6aull); } } @@ -467,12 +485,12 @@ TEST(moe_codebook_experts_both_classes) { { { B, { "codes", "table" } }, { BSCALE, { "scale" } } }), stored("w4nla8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1, { { 6, { "codes", "table" } }, { 7, { "scale" } } })); - golden("w4nla8h", h, 0xbb9b202196aef720ull); + golden("w4nla8h", h, 0x874c1a30937ee1d3ull); } TEST(moe_codebook_experts_at_e4m3_scales_a_64) { NEED_LIBQUANT(); - /* w4nl64a8h: the codebook's codes, stored as w4nla8h stores them, and an E4M3 scale per + /* w4nl64a8h: the codebook's codes keep their row-major nibble order, and an E4M3 scale per * (row, 64 of K) under one fixed f32. The scale view selects both; the relayout checks the * second is the kernel's constant and stores the E4M3 plane as is; the inverse gives back * both. A second level of any other value is refused when its bytes are relaid. */ @@ -769,7 +787,7 @@ TEST(moe_int4_gptq_against_a_calibration_hessian) { CHECK(qg.planes[0] != qr.planes[0]); /* the Hessian moved codes */ CHECK(qg.planes[1] == qr.planes[1]); /* and not the scales: static groups */ golden(rot ? "gptq w4a8h" : "gptq w4a8", hg, - rot ? 0x761cbdd0aff4adedull : 0x4a4db9378ca02303ull); + rot ? 0x2006e855d8d09225ull : 0x041995f37ab68a83ull); } }