feat(qwen4exp): support U4 gate with W6NL down experts

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-17 18:35:11 +01:00
1 parent 590dbcc178
commit d1b5a45e31
20 files changed
+618 -79

No files matched your search

+18 -1
View File
@@ -264,7 +264,10 @@ enum {
R4D_DT_MOEW5 = RAD_DT_PLUGIN_BASE + 8,
/* The M2XFP4 GEMMs' stored weight: R4D_DT_MXFP4_FRAG's fragment-order e2m1 codes, then the
* [K/32][N] meta plane the kernel reads beside the E8M0 exponents (r4d_layout.cpp, m2xfp4). */
R4D_DT_M2XFP4_FRAG = RAD_DT_PLUGIN_BASE + 9
R4D_DT_M2XFP4_FRAG = RAD_DT_PLUGIN_BASE + 9,
/* Six-bit expert codes: row-major permuted low nibbles followed by the transposed fifth
* magnitude bit and sign bit, one dword per 32 elements in each bit plane. */
R4D_DT_MOEW6 = RAD_DT_PLUGIN_BASE + 10
};
/* The grouped MoE GEMM's code tables, as the four dwords w4_e4m3's byte permutes read
@@ -307,6 +310,20 @@ static constexpr float kW5nlTable[32] = {
-0.8125f, -2.5f, -4.0f, -5.5f, -7.5f, -9.0f, -11.0f, -12.0f,
-14.0f, -16.0f, -18.0f, -20.0f, -22.0f, -24.0f, -26.0f, -30.0f };
/* W6NL's 32 E4M3-exact positive magnitudes, ordered by the low five code bits; bit 5 is sign.
* The first and second 16 magnitudes are each expanded by w4_expand, then selected by bit 4. */
static constexpr uint32_t kW6nlMagPerm[8] = { 0x4C494336u, 0x504F4E4Du, 0x54535251u, 0x58575655u,
0x5C5B5A59u, 0x605F5E5Du, 0x64636261u, 0x68676665u };
static constexpr float kW6nlTable[64] = {
0.875f, 2.75f, 4.5f, 6.0f, 6.5f, 7.0f, 7.5f, 8.0f,
9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f,
18.0f, 20.0f, 22.0f, 24.0f, 26.0f, 28.0f, 30.0f, 32.0f,
36.0f, 40.0f, 44.0f, 48.0f, 52.0f, 56.0f, 60.0f, 64.0f,
-0.875f, -2.75f, -4.5f, -6.0f, -6.5f, -7.0f, -7.5f, -8.0f,
-9.0f, -10.0f, -11.0f, -12.0f, -13.0f, -14.0f, -15.0f, -16.0f,
-18.0f, -20.0f, -22.0f, -24.0f, -26.0f, -28.0f, -30.0f, -32.0f,
-36.0f, -40.0f, -44.0f, -48.0f, -52.0f, -56.0f, -60.0f, -64.0f };
/* The fixed second-level scale of `w4nl64a8h`, `w4nl32a8h` and `w5nl64a8h`: it multiplies a
* weight's per-(row, 64 or 32 of K) E4M3 scale. A container states it (libquant's scale2_value=),
* and the layout hook compares it with this before serving. Fixed rather than per row because the
+87 -7
View File
@@ -180,7 +180,7 @@ static int moe_w4_nl(const RadParam* p, int n_p) {
const char* d = r4d_pgets(p, n_p, "dtype");
return d && (std::strcmp(d, "w4nla8h") == 0 || std::strcmp(d, "w4nl64a8h") == 0 ||
std::strcmp(d, "w4nl32a8h") == 0 || std::strcmp(d, "w5nl64a8h") == 0 ||
std::strcmp(d, "w4nl64a8i8h") == 0 || std::strcmp(d, "w4nl32a8i8h") == 0);
std::strcmp(d, "w6nl64a8h") == 0 || std::strcmp(d, "w4nl64a8i8h") == 0 || std::strcmp(d, "w4nl32a8i8h") == 0);
}
/* The `a8i8` names are the same stored planes met by an int8 activation (moe_gemm_w4a8i8):
* `w4a8i8` and `w4a8i8h` are w4a8's and w4a8h's, `w4nl64a8i8h` and `w4nl32a8i8h` the subgroup-scaled
@@ -194,6 +194,10 @@ static bool moe_w5(const RadParam* p, int n_p) {
const char* d = r4d_pgets(p, n_p, "dtype");
return d && std::strcmp(d, "w5nl64a8h") == 0;
}
static bool moe_w6(const RadParam* p, int n_p) {
const char* d = r4d_pgets(p, n_p, "dtype");
return d && std::strcmp(d, "w6nl64a8h") == 0;
}
/* `w4nl64a8h` is the codebook form with one E4M3 scale per (row, 64 of K) under a fixed f32
* second level, kW4G64Scale, stored once per weight (`u4*fp8_e4m3[1x64]*f32[*x*]/fwht128` with the
* table). Codes are stored as w4nla8h's. The scale view selects the E4M3 plane and the second
@@ -205,7 +209,7 @@ static int moe_w4_g64(const RadParam* p, int n_p) {
const char* d = r4d_pgets(p, n_p, "dtype");
if (!d) return 0;
return (std::strcmp(d, "w4nl64a8h") == 0 || std::strcmp(d, "w5nl64a8h") == 0 ||
std::strcmp(d, "w4nl64a8i8h") == 0) ? 2
std::strcmp(d, "w6nl64a8h") == 0 || std::strcmp(d, "w4nl64a8i8h") == 0) ? 2
: (std::strcmp(d, "w4nl32a8h") == 0 || std::strcmp(d, "w4nl32a8i8h") == 0) ? 4 : 0;
}
static int moe_w4_rot(const RadParam* p, int n_p) {
@@ -933,11 +937,12 @@ int moew4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding*
char fw[RAD_ENC_STR] = "";
if (moe_w4_rot(p, n_p)) std::snprintf(fw, sizeof fw, "fwht%lld", g.group);
const bool nl = moe_w4_nl(p, n_p);
const bool w5 = moe_w5(p, n_p);
const bool w5 = moe_w5(p, n_p), w6 = moe_w6(p, n_p);
const int g64 = moe_w4_g64(p, n_p); /* scale groups a 128 of K, or 0 */
if (w5 && g.K % 32) return RAD_E_SHAPE; /* a sign word is 32 elements */
if (g64 ? !r4d_enc::affine_table2(enc, w5 ? RAD_U5 : RAD_U4, RAD_F8E4M3, 1, g.group / g64,
RAD_F32, RAD_F32, w5 ? 32 : 16, fw)
if ((w5 || w6) && g.K % 32) return RAD_E_SHAPE; /* a bit-plane word is 32 elements */
if (g64 ? !r4d_enc::affine_table2(enc, w6 ? RAD_U6 : w5 ? RAD_U5 : RAD_U4,
RAD_F8E4M3, 1, g.group / g64,
RAD_F32, RAD_F32, w6 ? 64 : w5 ? 32 : 16, fw)
: nl ? !r4d_enc::affine_table(enc, RAD_U4, RAD_BF16, 1, g.group, RAD_F32, 16, fw)
: !r4d_enc::affine(enc, RAD_I4, RAD_BF16, 1, g.group, RAD_DT_INVALID, fw))
return RAD_E_DTYPE;
@@ -955,11 +960,15 @@ int moew4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding*
return RAD_E_UNSUPPORTED; /* bf16 a row a group, as it is */
}
if (operand != Q_B) return RAD_E_UNSUPPORTED;
if (nl ? !selects(enc, sel, n, "codes", "table") || !dims(pl[1], 1, w5 ? 32 : 16)
if (nl ? !selects(enc, sel, n, "codes", "table") || !dims(pl[1], 1, w6 ? 64 : w5 ? 32 : 16)
: !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 (w6) {
fill(out, "r4d.moew6nh64.w", R4D_DT_MOEW6, g.N, g.K, g.N * (g.K / 4) * 3);
return RAD_OK;
}
if (w5) {
fill(out, "r4d.moew5nh64.w", R4D_DT_MOEW5, g.N, g.K, g.N * (g.K / 8) * 5);
return RAD_OK;
@@ -986,6 +995,12 @@ bool moew5_table_is_kernels(const RadTensor& t) {
if (v[i] != kW5nlTable[i]) return false;
return true;
}
bool moew6_table_is_kernels(const RadTensor& t) {
const float* v = (const float*)t.data;
for (int i = 0; i < 64; ++i)
if (v[i] != kW6nlTable[i]) return false;
return true;
}
/* The expansion's nibble order, applied to a 64-bit word (two dwords). Canonical element 8g+i is
* nibble i of dword g; stored, byte b holds element b low and b+4 high (nibbles n0 n4 n1 n5 n2 n6
@@ -1095,6 +1110,54 @@ void moew5_row(const uint8_t* s, uint8_t* d, int64_t K, uint8_t* nib, bool forwa
}
}
}
/* Six-bit rows: canonical 8 codes in 6 bytes; served as W5's permuted low nibbles and
* two transposed bit planes (magnitude bit 4, sign bit 5). All layouts are K*6/8 bytes. */
void moew6_row(const uint8_t* s, uint8_t* d, int64_t K, uint8_t* nib, bool forward) {
if (forward) {
uint8_t* hi = d + K / 2;
uint8_t* sg = hi + K / 8;
for (int64_t c0 = 0; c0 < K; c0 += 32) {
uint32_t h = 0, sign = 0;
for (int64_t k = c0; k < c0 + 32; k += 8) {
uint64_t x = 0;
std::memcpy(&x, s + k / 8 * 6, 6);
uint32_t lo = 0;
for (int i = 0; i < 8; ++i) {
const uint32_t c = (uint32_t)(x >> (6 * i)) & 63u;
lo |= (c & 15u) << (4 * i);
const uint32_t pos = moew5_sign_bit(k - c0 + i);
h |= ((c >> 4) & 1u) << pos;
sign |= (c >> 5) << pos;
}
std::memcpy(nib + k / 2, &lo, 4);
}
std::memcpy(hi + c0 / 8, &h, 4);
std::memcpy(sg + c0 / 8, &sign, 4);
}
moew4_permute(nib, d, K / 2, true);
return;
}
moew4_permute(s, nib, K / 2, false);
const uint8_t* hi = s + K / 2;
const uint8_t* sg = hi + K / 8;
for (int64_t c0 = 0; c0 < K; c0 += 32) {
uint32_t h = 0, sign = 0;
std::memcpy(&h, hi + c0 / 8, 4);
std::memcpy(&sign, sg + c0 / 8, 4);
for (int64_t k = c0; k < c0 + 32; k += 8) {
uint32_t lo = 0;
std::memcpy(&lo, nib + k / 2, 4);
uint64_t x = 0;
for (int i = 0; i < 8; ++i) {
const uint32_t pos = moew5_sign_bit(k - c0 + i);
const uint64_t c = ((lo >> (4 * i)) & 15u) |
(((h >> pos) & 1u) << 4) | (((sign >> pos) & 1u) << 5);
x |= c << (6 * i);
}
std::memcpy(d + k / 8 * 6, &x, 6);
}
}
}
} /* namespace */
namespace {
@@ -1125,6 +1188,14 @@ int moew4_relayout_by(MoeViewFn view, const RadParam* p, int n_p, int operand,
std::memcpy(dst, pdata(pl[0]), (size_t)L.bytes);
return RAD_OK;
}
if (moe_w6(v.p, v.n)) {
if (!moew6_table_is_kernels(pl[1])) return RAD_E_DTYPE;
const int64_t rb = g.K / 4 * 3;
std::vector<uint8_t> nib((size_t)(g.K / 2));
for (long long nn = 0; nn < g.N; ++nn)
moew6_row(pdata(pl[0]) + nn * rb, (uint8_t*)dst + nn * rb, g.K, nib.data(), true);
return RAD_OK;
}
if (moe_w5(v.p, v.n)) {
if (!moew5_table_is_kernels(pl[1])) return RAD_E_DTYPE;
const int64_t rb = g.K / 8 * 5;
@@ -1154,6 +1225,15 @@ int moew4_unrelayout_by(MoeViewFn view, const RadParam* p, int n_p, int operand,
std::memcpy(pl[1].data, &kW4G64Scale, sizeof kW4G64Scale);
return RAD_OK;
}
if (moe_w6(v.p, v.n)) {
const int64_t rb = g.K / 4 * 3;
std::vector<uint8_t> nib((size_t)(g.K / 2));
for (long long nn = 0; nn < g.N; ++nn)
moew6_row((const uint8_t*)src + nn * rb, (uint8_t*)pl[0].data + nn * rb, g.K,
nib.data(), false);
std::memcpy(pl[1].data, kW6nlTable, sizeof kW6nlTable);
return RAD_OK;
}
if (moe_w5(v.p, v.n)) {
const int64_t rb = g.K / 8 * 5;
std::vector<uint8_t> nib((size_t)(g.K / 2));
+65 -22
View File
@@ -491,6 +491,25 @@ __device__ __forceinline__ Stage8 w5_expand(uint4 p, uint32_t sg, const uint32_t
return r;
}
/* W6NL: low nibbles address both halves of the 32 positive levels. The transposed magnitude
* bit selects a byte from those expansions and the transposed sign bit supplies E4M3's sign. */
__device__ __forceinline__ Stage8 w6_expand(uint4 p, uint32_t mh, uint32_t sg,
const uint32_t (&tab)[4]) {
const uint32_t high[4] = { 0x5C5B5A59u, 0x605F5E5Du, 0x64636261u, 0x68676665u };
Stage8 l = w4_expand(p, tab), h = w4_expand(p, high);
uint32_t a[8] = { l.v.x, l.v.y, l.v.z, l.v.w, l.w.x, l.w.y, l.w.z, l.w.w };
const uint32_t b[8] = { h.v.x, h.v.y, h.v.z, h.v.w, h.w.x, h.w.y, h.w.z, h.w.w };
#pragma unroll
for (uint32_t m = 0; m < 8; ++m) {
const uint32_t top = (mh << m) & 0x80808080u;
const uint32_t mask = top | (top - (top >> 7));
a[m] = (a[m] ^ ((a[m] ^ b[m]) & mask)) | ((sg << m) & 0x80808080u);
}
l.v = make_uint4(a[0], a[1], a[2], a[3]);
l.w = make_uint4(a[4], a[5], a[6], a[7]);
return l;
}
/* ---- the codebook levels against an int8 activation ---------------------------------------------
*
* The iu8 WMMA wants the levels as int8. The two's-complement nibble is its own value
@@ -729,7 +748,7 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
static_assert(kRowS || kBlock % FN == 0,
"a block's N rows must sit in one 128-row weight scale row");
/* Scale groups per 128-wide block: one, or the 64- or 32-column groups at W4 2-4. */
constexpr uint32_t kSub = W4 == 3 ? 4u : (W4 == 2 || W4 == 4) ? 2u : 1u;
constexpr uint32_t kSub = W4 == 3 ? 4u : (W4 == 2 || W4 == 4 || W4 == 5) ? 2u : 1u;
/* What multiplies a subgroup's E4M3 scale: the format's fixed second level, and at I8 the
* levels' unit. */
constexpr float kSubScale = I8 ? kW4G64Scale * kW4nlI8Unit : kW4G64Scale;
@@ -804,7 +823,8 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
constexpr uint32_t kEwTPR = FK / 32, kEwRPP = NTHR / kEwTPR;
constexpr uint32_t kEwBP = (FN + kEwRPP - 1) / kEwRPP;
[[maybe_unused]] uint4 ew_b4[AL && W4 ? kEwBP : 1];
[[maybe_unused]] uint32_t ew_sp[AL && W4 == 4 ? kEwBP : 1];
[[maybe_unused]] uint32_t ew_sp[AL && (W4 == 4 || W4 == 5) ? kEwBP : 1];
[[maybe_unused]] uint32_t ew_hp[AL && W4 == 5 ? kEwBP : 1];
[[maybe_unused]] Stage8 ew_b8[AL && !W4 ? kEwBP : 1];
[[maybe_unused]] bool ew_ok[AL ? kEwBP : 1];
[[maybe_unused]] const uint8_t* ew_wp = nullptr;
@@ -839,8 +859,11 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
if constexpr (W4) {
ew_b4[p] = moe_w4_load<W4, true>(ew_wp, ew_ok[p] ? wn0 + r : 0u,
C.w_ld, sk, true);
if constexpr (W4 == 4)
ew_sp[p] = u32_load_w<true>((const uint32_t*)(wr + (C.k >> 1) + (sk >> 3)));
if constexpr (W4 == 4 || W4 == 5)
ew_sp[p] = u32_load_w<true>((const uint32_t*)(wr + (C.k >> 1) +
(W4 == 5 ? (C.k >> 3) : 0) + (sk >> 3)));
if constexpr (W4 == 5)
ew_hp[p] = u32_load_w<true>((const uint32_t*)(wr + (C.k >> 1) + (sk >> 3)));
} else
ew_b8[p] = stage8_load_w<true>(wr + sk, true);
}
@@ -1110,7 +1133,8 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
* the fold reads them from LDS. Bit-identical. */
Stage8 apre[kAP];
uint4 bpre4[kBP]; /* W4: packed nibbles, expanded at store time as before */
uint32_t spre[W4 == 4 ? kBP : 1]; /* W4 4: the same 32 elements' sign bits */
uint32_t spre[(W4 == 4 || W4 == 5) ? kBP : 1];
uint32_t hpre[W4 == 5 ? kBP : 1];
Stage8 bpre8[kBP]; /* otherwise: the fp8 bytes themselves */
/* kUL: whether each staged row is live. Under kUL every row's tile is loaded (a dead row
* reads a row that exists) and the zero is selected at the LDS store. A guarded load writes
@@ -1213,7 +1237,8 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
bok[p] = ew_ok[p];
if constexpr (W4) {
bpre4[p] = ew_b4[p];
if constexpr (W4 == 4) spre[p] = ew_sp[p];
if constexpr (W4 == 4 || W4 == 5) spre[p] = ew_sp[p];
if constexpr (W4 == 5) hpre[p] = ew_hp[p];
} else
bpre8[p] = ew_b8[p];
continue;
@@ -1221,7 +1246,8 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
/* The dead rows' zeros, as w4_load / stage8_load give them. */
if constexpr (W4) {
bpre4[p] = ew_ok[p] ? ew_b4[p] : make_uint4(0, 0, 0, 0);
if constexpr (W4 == 4) spre[p] = ew_ok[p] ? ew_sp[p] : 0u;
if constexpr (W4 == 4 || W4 == 5) spre[p] = ew_ok[p] ? ew_sp[p] : 0u;
if constexpr (W4 == 5) hpre[p] = ew_ok[p] ? ew_hp[p] : 0u;
} else {
bpre8[p].v = ew_ok[p] ? ew_b8[p].v : make_uint4(0, 0, 0, 0);
bpre8[p].w = ew_ok[p] ? ew_b8[p].w : make_uint4(0, 0, 0, 0);
@@ -1243,10 +1269,12 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
if constexpr (W4) {
bpre4[p] = moe_w4_load<W4, UW>(Wp, kUL && !ok ? wn0 : wn0 + r,
w_ld, cc + sk, kUL || ok);
if constexpr (W4 == 4)
if constexpr (W4 == 4 || W4 == 5)
spre[p] = kUL || ok ? u32_load_w<UW>((const uint32_t*)(wr + (K >> 1) +
((cc + sk) >> 3)))
: 0u;
(W4 == 5 ? (K >> 3) : 0) + ((cc + sk) >> 3))) : 0u;
if constexpr (W4 == 5)
hpre[p] = kUL || ok ? u32_load_w<UW>((const uint32_t*)(wr + (K >> 1) +
((cc + sk) >> 3))) : 0u;
} else
bpre8[p] = stage8_load_w<UW>(wr + cc + sk, kUL || ok);
}
@@ -1286,7 +1314,10 @@ __device__ __forceinline__ void moe_gemm_q_body(R4D_MOEGQ_PARAMS) {
/* a dead row's zero, where the packed form held it before (see bok) */
const bool live = !kUL || bok[p];
const uint4 b4 = live ? bpre4[p] : make_uint4(0, 0, 0, 0);
if constexpr (W4 == 4)
if constexpr (W4 == 5)
stage8_store<FK>(s_b, r, sk, w6_expand(b4, live ? hpre[p] : 0u,
live ? spre[p] : 0u, tab.v));
else if constexpr (W4 == 4)
stage8_store<FK>(s_b, r, sk, w5_expand(b4, live ? spre[p] : 0u, tab.v));
else if constexpr (W4)
stage8_store<FK>(s_b, r, sk, moe_w4_bytes<W4, I8>(b4, tab.v));
@@ -1522,7 +1553,7 @@ template <int W4, bool I8 = false>
__device__ __forceinline__ void moe_down_dec_body(R4D_MOEDD_PARAMS) {
constexpr bool UW = true;
constexpr uint32_t FM = 16, FN = 64, FK = kFK, NTHR = 4 * kWave;
constexpr uint32_t kSub = W4 == 3 ? 4u : (W4 == 2 || W4 == 4) ? 2u : 1u;
constexpr uint32_t kSub = W4 == 3 ? 4u : (W4 == 2 || W4 == 4 || W4 == 5) ? 2u : 1u;
/* moe_gemm_q_kernel's I8, with its per-row scale and its subgroup unit. */
constexpr bool kRowS = W4 != 0 || I8;
constexpr float kSubScale = I8 ? kW4G64Scale * kW4nlI8Unit : kW4G64Scale;
@@ -1659,7 +1690,8 @@ __device__ __forceinline__ void moe_down_dec_body(R4D_MOEDD_PARAMS) {
* zero selected at the LDS store, scale-role registers uninitialised (see the note there). */
Stage8 apre[kAP];
uint4 bpre4[kBP];
uint32_t spre[W4 == 4 ? kBP : 1];
uint32_t spre[(W4 == 4 || W4 == 5) ? kBP : 1];
uint32_t hpre[W4 == 5 ? kBP : 1];
Stage8 bpre8[kBP];
using PwsT = uint32_t; /* a 16-bit scale zero-extended: see u16_load_w */
float pau[1];
@@ -1702,10 +1734,12 @@ __device__ __forceinline__ void moe_down_dec_body(R4D_MOEDD_PARAMS) {
if constexpr (W4) {
const uint8_t* wr = Wp + (size_t)(wn0 + r) * w_ld;
bpre4[p] = moe_w4_load<W4, UW>(Wp, wn0 + r, w_ld, cc + sk, ok);
if constexpr (W4 == 4)
if constexpr (W4 == 4 || W4 == 5)
spre[p] = ok ? u32_load_w<UW>((const uint32_t*)(wr + (K >> 1) +
((cc + sk) >> 3)))
: 0u;
(W4 == 5 ? (K >> 3) : 0) + ((cc + sk) >> 3))) : 0u;
if constexpr (W4 == 5)
hpre[p] = ok ? u32_load_w<UW>((const uint32_t*)(wr + (K >> 1) +
((cc + sk) >> 3))) : 0u;
} else
bpre8[p] = stage8_load_w<UW>(Wp + (size_t)(wn0 + r) * w_ld + cc + sk, ok);
}
@@ -1731,7 +1765,9 @@ __device__ __forceinline__ void moe_down_dec_body(R4D_MOEDD_PARAMS) {
for (uint32_t q = 0; q < kBP; ++q) {
const uint32_t r = sn + q * kRPP;
if (r >= FN) continue;
if constexpr (W4 == 4)
if constexpr (W4 == 5)
stage8_store<FK>(s_b, r, sk, w6_expand(bpre4[q], hpre[q], spre[q], tab.v));
else if constexpr (W4 == 4)
stage8_store<FK>(s_b, r, sk, w5_expand(bpre4[q], spre[q], tab.v));
else if constexpr (W4)
stage8_store<FK>(s_b, r, sk, moe_w4_bytes<W4, I8>(bpre4[q], tab.v));
@@ -2629,7 +2665,8 @@ static inline bool r4d_moe_decode_band(long long T, long long n_expert) {
* the 4-bit formats still take two blocks up to 80 rows (two sequences at depth 3): on Flash-Next
* (w4nl64a8h) a one-stream step at depth 3 is 1.1% shorter at TP4 and no slower at TP2. */
template <int W4>
constexpr long long kMoeGuFk256Rows = W4 == 4 ? (1ll << 40) : (W4 == 2 || W4 == 3) ? 80 : 0;
constexpr long long kMoeGuFk256Rows = (W4 == 4 || W4 == 5) ? (1ll << 40)
: (W4 == 2 || W4 == 3) ? 80 : 0;
/* The decode form's staged K, pinnable for tests: 0 kMoeGuFk256Rows's rule, 1 one 128-wide block
* per pass, 2 two wherever allowed. r4d_selftest compares both byte for byte. */
static int g_moe_fk_pin = 0;
@@ -2848,6 +2885,7 @@ static int moe_gemm_launch(const RadArgs* a, RadStream stream, int ncls = 2) {
* magnitudes, or two's complement for w4a8 and w4a8h. */
const char* wdt = r4d_gets(a, "dtype");
const uint32_t* tab = I8 ? kW4IntPermI8
: W4 == 5 ? kW6nlMagPerm
: W4 == 4 ? kW5nlMagPerm
: (W4 && wdt && (!std::strcmp(wdt, "w4nla8h") ||
!std::strcmp(wdt, "w4nl64a8h") ||
@@ -2860,10 +2898,12 @@ static int moe_gemm_launch(const RadArgs* a, RadStream stream, int ncls = 2) {
long long k, MoeCls* c) -> int {
long long w_ld = 0, ws_ld = 0;
size_t w_s0 = 0, s_s0 = 0;
int rc = wtab_of(w, W4 == 4 ? (uint32_t)R4D_DT_MOEW5
int rc = wtab_of(w, W4 == 5 ? (uint32_t)R4D_DT_MOEW6
: W4 == 4 ? (uint32_t)R4D_DT_MOEW5
: W4 ? (uint32_t)R4D_DT_MOEW4
: I8 ? (uint32_t)RAD_I8 : (uint32_t)RAD_F8E4M3,
W4 == 4 ? 5 : W4 ? 4 : 8, cnt, n, k, &c->w, &w_s0, &w_ld);
W4 == 5 ? 6 : W4 == 4 ? 5 : W4 ? 4 : 8,
cnt, n, k, &c->w, &w_s0, &w_ld);
if (rc != RAD_OK) return rc;
if constexpr (W4 >= 2) {
/* One E4M3 byte per (row, group of 64 or 32 K). The pitch is derived, not read: the
@@ -2875,8 +2915,9 @@ static int moe_gemm_launch(const RadArgs* a, RadStream stream, int ncls = 2) {
if (rc != RAD_OK) return rc;
/* A 5-bit row is its nibbles then its sign bits; the kernel finds the signs K/2 bytes
* in. */
if (W4 == 4 ? (w_ld % 8 || k % 32) : (w_ld % 2)) return R4D_NO(RAD_E_STRIDE);
w_ld = W4 == 4 ? w_ld / 8 * 5 : w_ld / 2;
if (W4 == 5 ? (w_ld % 4 || k % 32) : W4 == 4 ? (w_ld % 8 || k % 32) : (w_ld % 2))
return R4D_NO(RAD_E_STRIDE);
w_ld = W4 == 5 ? w_ld / 4 * 3 : W4 == 4 ? w_ld / 8 * 5 : w_ld / 2;
ws_ld = k / kBlock;
} else if constexpr (W4) {
/* The scale plane is one bf16 per (row, 128 of K), not per 128x128 tile. */
@@ -3138,6 +3179,7 @@ extern "C" int r4d_rad_moe_gemm_w4a8(const RadArgs* a, RadStream stream) {
if (wdt && !std::strcmp(wdt, "w4nl64a8h")) return moe_gemm_launch<2>(a, stream);
if (wdt && !std::strcmp(wdt, "w4nl32a8h")) return moe_gemm_launch<3>(a, stream);
if (wdt && !std::strcmp(wdt, "w5nl64a8h")) return moe_gemm_launch<4>(a, stream);
if (wdt && !std::strcmp(wdt, "w6nl64a8h")) return moe_gemm_launch<5>(a, stream);
return moe_gemm_launch<1>(a, stream);
}
@@ -3168,6 +3210,7 @@ extern "C" int r4d_rad_moe_gemm_w4a8_mod4(const RadArgs* a, RadStream stream) {
if (wdt && !std::strcmp(wdt, "w4nl64a8h")) return moe_gemm_launch<2>(a, stream, 4);
if (wdt && !std::strcmp(wdt, "w4nl32a8h")) return moe_gemm_launch<3>(a, stream, 4);
if (wdt && !std::strcmp(wdt, "w5nl64a8h")) return moe_gemm_launch<4>(a, stream, 4);
if (wdt && !std::strcmp(wdt, "w6nl64a8h")) return moe_gemm_launch<5>(a, stream, 4);
return moe_gemm_launch<1>(a, stream, 4);
}
+1 -1
View File
@@ -5109,7 +5109,7 @@ static const RadConstraint cMoeMx[] = {
static const RadConstraint cMoeGemmW4[] = {
C_GE("M", 1), C_DIV("N", 64), C_DIV("K", 128), C_GE("n_expert", 1), C_LE("n_expert", 65536),
C_GE("top_k", 1), C_EQ("group", 128),
C_IN("dtype", "w4a8 w4a8h w4nla8h w4nl64a8h w4nl32a8h w5nl64a8h"),
C_IN("dtype", "w4a8 w4a8h w4nla8h w4nl64a8h w4nl32a8h w5nl64a8h w6nl64a8h"),
};
/* The op against an int8 activation with an f32 scale per (row, 128 of K): int8 experts with a
* bf16 scale per (row, 128 of K), `i8a8h` rotated 128 wide along K as its activation is; and the
+46 -15
View File
@@ -2327,15 +2327,15 @@ static Planes nl_planes(const std::vector<float>& w, int64_t N, int64_t K, int64
* the stored scale. Planes: codes, scale (E4M3 [1 x gs]), scale.1 (f32 constant), table. With
* `w5`, w5nl64a8h's: u5 codes into kW5nlTable, whose largest entry is 30. */
static Planes nl64_planes(const std::vector<float>& w, int64_t N, int64_t K, int64_t gs = 64,
bool w5 = false) {
const float* tab = w5 ? kW5nlTable : kW4nlTable;
const int nt = w5 ? 32 : 16;
const float top = w5 ? 30.0f : 22.0f;
int w5 = 0) {
const float* tab = w5 == 2 ? kW6nlTable : w5 ? kW5nlTable : kW4nlTable;
const int nt = w5 == 2 ? 64 : w5 ? 32 : 16;
const float top = w5 == 2 ? 64.0f : w5 ? 30.0f : 22.0f;
Planes p;
rad_enc_clear(&p.enc);
rad_enc_copy_str(p.enc.scheme, "affine");
rad_enc_copy_str(p.enc.transform, "fwht128");
rad_enc_add_plane(&p.enc, "codes", w5 ? RAD_U5 : RAD_U4, 1, 1);
rad_enc_add_plane(&p.enc, "codes", w5 == 2 ? RAD_U6 : w5 ? RAD_U5 : RAD_U4, 1, 1);
rad_enc_add_plane(&p.enc, "scale", RAD_F8E4M3, 1, gs);
rad_enc_add_plane(&p.enc, "scale.1", RAD_F32, 0, 0);
rad_enc_add_table(&p.enc, "table", RAD_F32, 1, nt);
@@ -2362,7 +2362,8 @@ static Planes nl64_planes(const std::vector<float>& w, int64_t N, int64_t K, int
int best = 0;
for (int c = 1; c < nt; ++c)
if (std::fabs(u - tab[c]) < std::fabs(u - tab[best])) best = c;
rad_store_code(p.data[0].data() + n * crow, w5 ? RAD_U5 : RAD_U4,
rad_store_code(p.data[0].data() + n * crow,
w5 == 2 ? RAD_U6 : w5 ? RAD_U5 : RAD_U4,
g * 128 + h * gs + j, best);
}
}
@@ -22850,15 +22851,15 @@ static void moe_aligned_down_tile(const Plugin& pl, const RadKernelInfo* row, Ra
* it (`moe_gemm_i8a8`), each with its own four-class twin. */
static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int sub = 0,
bool w5 = false, bool four = false, int64_t top_k = 2, int act8 = 0,
int64_t ag = 128) {
int64_t ag = 128, bool w6 = false) {
const bool g64 = sub > 1, w8 = act8 == 2;
/* Against an int8 activation the codebook is read at its int8 levels (r4d_args.h's kW4nlI8). */
float tab_i8[16];
for (int i = 0; i < 16; ++i)
tab_i8[i] = (float)(i < 8 ? kW4nlI8[i] : -kW4nlI8[i - 8]) * kW4nlI8Unit;
const float* tab = w5 ? kW5nlTable : act8 ? tab_i8 : kW4nlTable;
const float* tab = w6 ? kW6nlTable : w5 ? kW5nlTable : act8 ? tab_i8 : kW4nlTable;
const char* const rname = w8 ? "moe_gemm_i8a8" : act8 ? "moe_gemm_w4a8i8" : "moe_gemm_w4a8";
if (act8 && (w5 || (w8 && sub))) {
if ((w6 && (w5 || sub != 2)) || (act8 && (w5 || w6 || (w8 && sub)))) {
ok((std::string(rname) + " (" + sh.name + ")").c_str(), false, "no such int8 form");
return;
}
@@ -22871,7 +22872,8 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
}
const char* dt = w8 ? "i8a8"
: act8 ? (sub == 4 ? "w4nl32a8i8h" : g64 ? "w4nl64a8i8h" : "w4a8i8")
: w5 ? "w5nl64a8h" : sub == 4 ? "w4nl32a8h" : g64 ? "w4nl64a8h" : "w4a8";
: w6 ? "w6nl64a8h" : w5 ? "w5nl64a8h"
: sub == 4 ? "w4nl32a8h" : g64 ? "w4nl64a8h" : "w4a8";
const std::string what = std::string(rname) + " vs a host decode (T=" +
std::to_string(Tr) + ", " + sh.name +
(g64 || act8 ? std::string(", ") + dt : std::string()) +
@@ -22900,7 +22902,8 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
for (int c = 0; c < 2; ++c) {
const int ow = c ? 6 : 2, os = c ? 7 : 3;
const int64_t kbc = Kc[c] / group;
const int64_t wbytes = w8 ? Nc[c] * Kc[c] : w5 ? Nc[c] * Kc[c] / 8 * 5 : Nc[c] * Kc[c] / 2;
const int64_t wbytes = w8 ? Nc[c] * Kc[c] : w6 ? Nc[c] * Kc[c] / 4 * 3
: w5 ? Nc[c] * Kc[c] / 8 * 5 : Nc[c] * Kc[c] / 2;
wq[c].assign((size_t)(nec[c] * wbytes), 0);
ws[c].assign((size_t)(nec[c] * Nc[c] * kbc * (sub == 4 ? 2 : 1)), 0);
std::vector<float> wsrc((size_t)(Nc[c] * Kc[c]));
@@ -22911,8 +22914,16 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
wsrc[(size_t)(n * Kc[c] + k)] =
(float)(((e * 7 + n * 31 + k * 17) % 61) - 30) / 37.0f;
cp[c].push_back(w8 ? int8_planes(wsrc, Nc[c], Kc[c], RAD_BF16)
: g64 ? nl64_planes(wsrc, Nc[c], Kc[c], 128 / sub, w5)
: g64 ? nl64_planes(wsrc, Nc[c], Kc[c], 128 / sub, w6 ? 2 : w5)
: int4_planes(wsrc, Nc[c], Kc[c], group, RAD_BF16, false));
if (w6) {
/* Exercise every six-bit code, not only the levels the source happened to pick. */
Planes& p = cp[c].back();
for (int64_t n = 0; n < Nc[c]; ++n)
for (int64_t k = 0; k < Kc[c]; ++k)
rad_store_code(p.data[0].data() + n * p.row_bytes(0), RAD_U6, k,
(int)((k + n * 17 + e * 13) % 64));
}
std::vector<uint8_t> w, sc;
const int rw = g64 ? stored(r, lp, ow, cp[c].back(), { "codes", "table" }, &w)
: stored(r, lp, ow, cp[c].back(), { "codes" }, &w);
@@ -22990,13 +23001,15 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
std::vector<RadTensor> ts = {
T(d_a, act8 ? (uint32_t)RAD_I8 : (uint32_t)RAD_F8E4M3, { M, K }),
T(d_as, RAD_F32, { M, kb }),
T(d_w[0], w8 ? (uint32_t)RAD_I8 : w5 ? (uint32_t)R4D_DT_MOEW5 : (uint32_t)R4D_DT_MOEW4,
T(d_w[0], w8 ? (uint32_t)RAD_I8 : w6 ? (uint32_t)R4D_DT_MOEW6
: w5 ? (uint32_t)R4D_DT_MOEW5 : (uint32_t)R4D_DT_MOEW4,
{ nec[0], sh.N0, sh.K0 }),
g64 ? T(d_ws[0], RAD_F8E4M3, { nec[0], sh.N0, sub * sh.K0 / group })
: T(d_ws[0], RAD_BF16, { nec[0], sh.N0, sh.K0 / group }),
T(d_s, RAD_I32, { Tr }),
T(d_o, RAD_I32, { ne + 1 }),
T(d_w[1], w8 ? (uint32_t)RAD_I8 : w5 ? (uint32_t)R4D_DT_MOEW5 : (uint32_t)R4D_DT_MOEW4,
T(d_w[1], w8 ? (uint32_t)RAD_I8 : w6 ? (uint32_t)R4D_DT_MOEW6
: w5 ? (uint32_t)R4D_DT_MOEW5 : (uint32_t)R4D_DT_MOEW4,
{ nec[1], sh.N1, sh.K1 }),
g64 ? T(d_ws[1], RAD_F8E4M3, { nec[1], sh.N1, sub * sh.K1 / group })
: T(d_ws[1], RAD_BF16, { nec[1], sh.N1, sh.K1 / group }),
@@ -23146,7 +23159,7 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
hi->stride[0] = t.stride[0] * 2;
hi->data = (char*)t.data + t.stride[0] * bits / 8;
};
const int wbits = w8 ? 8 : w5 ? 5 : 4, sbits = g64 ? 8 : 16;
const int wbits = w8 ? 8 : w6 ? 6 : w5 ? 5 : 4, sbits = g64 ? 8 : 16;
RadTensor c4w[4], c4s[4];
halves(ts[2], wbits, &c4w[0], &c4w[2]);
halves(ts[3], sbits, &c4s[0], &c4s[2]);
@@ -23190,6 +23203,20 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
moe_dec_variants(pl, r4, a4, d_y, (size_t)(Tr * N), fk2, what4);
}
static void moe_w6_cases(const Plugin& pl) {
const MoeW4Shape sh[] = {
{ "w6 gate/up", 256, 256, 128, 256, 2, 5 },
{ "w6 down sorted", 256, 256, 256, 128, 1, 6, true },
};
for (const auto& s : sh)
for (int64_t t : { (int64_t)24, (int64_t)200 })
moe_w4_gemm(pl, t, s, 2, false, true, 2, 0, 128, true);
/* Flash-Next TP2's actual down geometry, including the odd width and top-k=10. */
const MoeW4Shape qfn = { "Flash-Next TP2 down", 2560, 384, 2560, 256, 1, 20, true };
for (int64_t t : { (int64_t)10, (int64_t)200 })
moe_w4_gemm(pl, t, qfn, 2, false, true, 10, 0, 128, true);
}
/* The four-class grouped GEMM, each class at its own geometry, against a host decode.
*
* moe_w4_gemm's four-class pass pairs geometries (0 with 2, 1 with 3), with class 0 always wide.
@@ -32911,8 +32938,12 @@ static void device_phase(const Plugin& pl) {
for (const int64_t t : { (int64_t)24, (int64_t)200, (int64_t)600 })
moe_w4_gemm(pl, t, sh, 2, true);
}
moe_w6_cases(pl);
}
/* Also expose the W6 tests alone while retaining them in the normal W4 row's test. */
if (sel("moe_gemm_w6nl")) moe_w6_cases(pl);
/* The grouped GEMM against an int8 activation: the same shapes through each row's forms. */
{
static const MoeW4Shape kShapes8[] = {