diff --git a/libr4d/r4d_fp8_f32scale.hip b/libr4d/r4d_fp8_f32scale.hip index b961814d..d0776e1d 100644 --- a/libr4d/r4d_fp8_f32scale.hip +++ b/libr4d/r4d_fp8_f32scale.hip @@ -203,6 +203,184 @@ __global__ __launch_bounds__(128) void fp8_f32scale_experts(const uint8_t* a, co } } } +/* The small-M grouped form at GLM's MTP gate_up slice, with fp8_f32scale_experts' per-element + * sequence and so its bytes. A draft pass routes one token to eight experts, and the four column + * tiles a block of the form above leave sixteen blocks, each a chain of 64 stages of 256 K. Here a + * block takes one column tile of the y-th touched expert (the grid has min(experts, rows) + 1 + * rows, as at the MXFP4 small-M form), in stages of 512 K, with loads unconditional from clamped + * addresses and masked at the store, barriers that order LDS only, and a 128 of K's eight fragment + * pairs loaded before its WMMA chain. */ +typedef unsigned f8_g16 __attribute__((ext_vector_type(4), aligned(1))); +__device__ __forceinline__ f8_u4 f8_ldg16(const uint8_t* p) { + return *(const __attribute__((address_space(1))) f8_g16*)p; +} +__device__ __forceinline__ float f8_ldgf(const float* p) { + return *(const __attribute__((address_space(1))) float*)p; +} +template +struct F8Small { + static constexpr int Threads = 128, KB = StageK / 128, P = StageK + 8; + static constexpr int AChunks = StageK / Threads, WChunks = 64 * StageK / 16 / Threads; + f8_u4 a[AChunks], w[WChunks]; + float wsc[KB], sa; + uint32_t ok; +}; +template +__global__ __launch_bounds__(128) void fp8_f32scale_small(const uint8_t* a, const float* as, + const void* w0, const void* s0, + const void* w1, const void* s1, + const int* sorted, const int* offsets, + uint16_t* y, Meta m) { + using St = F8Small; + constexpr int KB = St::KB, P = St::P, Threads = St::Threads; + static_assert(St::AChunks <= 8 && St::WChunks <= 16 && KB <= 4 && 16 * KB <= Threads && + 64 * P >= 4 * 513, + "stage chunks must fit the load mask and the offsets the weight tile"); + __shared__ __align__(16) uint8_t qa[16 * P], wb[64 * P]; + __shared__ float sa[16 * KB]; + __shared__ int source[16]; + __shared__ int pick; + const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, col0 = blockIdx.x * 64; + if ((int)blockIdx.y == (int)gridDim.y - 1) { + const int r0 = offsets[m.experts]; + for (long i = tid; i < (long)(m.T - r0) * 64; i += Threads) { + const int c = col0 + (int)(i % 64); + if (c < m.N) y[(long)(r0 + i / 64) * m.yr + c] = 0; + } + return; + } + { + // The offsets in the weight tile, whose bytes are free until the first stage. + int* offs = reinterpret_cast(wb); +#pragma unroll + for (int q = 0; q < 5; ++q) { + const int x = tid + q * Threads; + if (x <= m.experts) offs[x] = offsets[x]; + } + lds_barrier(); + if (wave == 0) { + int want = blockIdx.y, found = -1; + for (int base = 0; base < m.experts; base += 32) { + const int x = base + lane; + const bool live = x < m.experts && offs[x + 1] > offs[x]; + const uint32_t mask = __builtin_amdgcn_ballot_w32(live); + const int n = __builtin_popcount(mask); + if (want < n) { + const uint32_t below = mask & ((1u << lane) - 1u); + found = base + __builtin_ctz(__builtin_amdgcn_ballot_w32( + live && __builtin_popcount(below) == want)); + break; + } + want -= n; + } + if (lane == 0) pick = found; + } + lds_barrier(); + } + const int e = pick; + if (e < 0) return; + const int lo = offsets[e], hi = offsets[e + 1]; + const int parity = e & 1; + const auto* w = entry(parity ? w1 : w0, m.wstep[parity], e / 2); + const auto* ws = (const float*)entry(parity ? s1 : s0, m.sstep[parity], e / 2); + const int S = (m.K + StageK - 1) / StageK, rowbase = (lane >> 4) * 8; + for (int r0 = lo; r0 < hi; r0 += 16) { + lds_barrier(); + if (tid < 16) { + const int r = r0 + tid, flat = r < hi ? sorted[r] : -1; + source[tid] = flat >= 0 ? (m.sorted_a ? r : flat / m.topk) : -1; + } + lds_barrier(); + bool rv[8]; +#pragma unroll + for (int j = 0; j < 8; ++j) rv[j] = source[rowbase + j] >= 0; + auto load = [&](int s, St& L) { + const int k0 = s * StageK; + uint32_t ok = 0; +#pragma unroll + for (int i = 0; i < St::AChunks; ++i) { + const int j = tid + i * Threads, r = j / (StageK / 16), c = (j % (StageK / 16)) * 16, + src = source[r]; + const bool v = src >= 0 && k0 + c < m.K; + __builtin_memcpy(&L.a[i], a + (v ? (long)src * m.ar + k0 + c : 0), 16); + ok |= (uint32_t)v << i; + } +#pragma unroll + for (int i = 0; i < St::WChunks; ++i) { + const int j = tid + i * Threads, r = j / (StageK / 16), c = (j % (StageK / 16)) * 16; + const bool v = k0 + c < m.K; + L.w[i] = f8_ldg16(w + (v ? (long)(col0 + r) * m.wr + k0 + c : 0)); + ok |= (uint32_t)v << (8 + i); + } +#pragma unroll + for (int kb = 0; kb < KB; ++kb) { + const bool v = k0 + kb * 128 < m.K; + L.wsc[kb] = + f8_ldgf(ws + (v ? (long)(col0 / 128) * m.sr + (k0 + kb * 128) / 128 : 0)); + ok |= (uint32_t)v << (24 + kb); + } + { + const int src = source[(tid / KB) & 15], kb = k0 + (tid % KB) * 128; + const bool v = tid < 16 * KB && src >= 0 && kb < m.K; + L.sa = as[v ? (long)src * m.asr + kb / 128 : 0]; + ok |= (uint32_t)v << 28; + } + L.ok = ok; + }; + auto store = [&](const St& L) { +#pragma unroll + for (int i = 0; i < St::AChunks; ++i) { + const int j = tid + i * Threads, r = j / (StageK / 16), c = (j % (StageK / 16)) * 16; + *reinterpret_cast(qa + r * P + c) = + (L.ok >> i) & 1u ? L.a[i] : f8_u4{ 0, 0, 0, 0 }; + } +#pragma unroll + for (int i = 0; i < St::WChunks; ++i) { + const int j = tid + i * Threads, r = j / (StageK / 16), c = (j % (StageK / 16)) * 16; + *reinterpret_cast(wb + r * P + c) = + (L.ok >> (8 + i)) & 1u ? L.w[i] : f8_u4{ 0, 0, 0, 0 }; + } + if (tid < 16 * KB) sa[(tid % KB) * 16 + tid / KB] = (L.ok >> 28) & 1u ? L.sa : 0.f; + }; + auto acc = r4d_fp8::acc_zero(); + St L; + load(0, L); + for (int s = 0; s < S; ++s) { + store(L); + float wsc[KB]; +#pragma unroll + for (int kb = 0; kb < KB; ++kb) wsc[kb] = (L.ok >> (24 + kb)) & 1u ? L.wsc[kb] : 0.f; + lds_barrier(); + if (s + 1 < S) load(s + 1, L); + const int k0 = s * StageK; +#pragma unroll + for (int kb = 0; kb < KB; ++kb) { + if (k0 + kb * 128 >= m.K) break; + r4d_fp8::i2v fa[8], fb[8]; +#pragma unroll + for (int t = 0; t < 8; ++t) { + fa[t] = r4d_fp8::frag(qa, 0, kb * 128 + 16 * t, lane); + fb[t] = r4d_fp8::frag(wb, wave * 16, kb * 128 + 16 * t, lane); + } + auto raw = r4d_fp8::acc_zero(); +#pragma unroll + for (int t = 0; t < 8; ++t) raw = r4d_fp8::mma(fa[t], fb[t], raw); +#pragma unroll + for (int j = 0; j < 8; ++j) { + const float scale = rv[j] ? sa[kb * 16 + rowbase + j] * wsc[kb] : 0; + acc[j] += raw[j] * scale; + } + } + lds_barrier(); + } + const int c = col0 + wave * 16 + (lane & 15); +#pragma unroll + for (int j = 0; j < 8; ++j) { + const int r = r0 + rowbase + j; + if (r < hi) y[(long)r * m.yr + c] = f32_to_bf16(acc[j]); + } + } +} // Both forms below keep fp8_f32scale's arithmetic for every output: per 128-K block the same // eight-mma chain from zero over the same fragments (the bytes at k + 8*(lane/16) of the block's // row segment, which the legacy form copies through LDS unchanged), then acc += raw * (a_scale * @@ -504,6 +682,16 @@ int body(const RadArgs* a, RadStream stream, bool grouped) { m.sstep[0] = ws.stride[0] * 4; m.sstep[1] = so.stride[0] * 4; if (w.shape[0] < (m.experts + 1) / 2 || wo.shape[0] < m.experts / 2) return RAD_E_SHAPE; + /* The small-M form at the MTP layer's gate_up slice up to 384 routed rows: a draft pass routes + * one token, a verify pass a few. Measured there (perfmxf8, cold), expert-major against + * small-M at 1, 2, 4, 8 and 16 tokens: 73.6/36.7, 83.6/65.8, 118.4/114.9, 214.0/203.9 and + * 375.6/343.9 us. Down's K of 256 gains nothing from it (within a few percent, a quarter slower + * at three tokens). */ + if (m.experts == 288 && m.topk == 8 && N == 512 && K == 4096 && m.T <= 384) + return R4D_EMIT(stream, fp8_f32scale_small<512>, + dim3(N / 64, (m.T < m.experts ? m.T : m.experts) + 1), dim3(128), 0, + (const uint8_t*)x.data, (const float*)xs.data, w.data, ws.data, wo.data, + so.data, (const int*)st.data, (const int*)off.data, (uint16_t*)y.data, m); /* The expert-major form at every routed-row count. Measured at GLM's TP8 slices: a long K * (gate_up's 4096) wants the 256 stage, 25 to 40 % ahead of 128, and under 128 routed rows * four tiles a block on top; at down's K of 256 every variant reads within a few percent of diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index af2e207c..1796b119 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -15235,7 +15235,8 @@ static void mx_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E, int64_ const int64_t pbm = pwide || (K > 256 && Tr >= 12288) ? 64 : 32; const bool p64 = glm && Tr >= (P.srt ? 5760 : 769) && (Tr + pbm - 1) / pbm + E <= 65535; - const bool small = glm && Tr <= (P.srt ? 32 : 384); + const bool small = (glm && Tr <= (P.srt ? 32 : 384)) || + (f8 && E == 288 && top_k == 8 && !P.srt && Tr <= 384); const bool expert = !p64 && !small && (f8 || Tr <= (glm ? (P.srt ? 6143 : 768) : 511)); const bool pipe = !p64 && !small && !expert && glm; const int64_t jr = p64 ? pbm : pipe ? 32 : 16; diff --git a/tests/r4d_emu/test_fp8f32.cpp b/tests/r4d_emu/test_fp8f32.cpp index a8eadeec..ed13c58a 100644 --- a/tests/r4d_emu/test_fp8f32.cpp +++ b/tests/r4d_emu/test_fp8f32.cpp @@ -4,7 +4,8 @@ * which serves an activation pitch off 16 bytes, bit for bit: the same fragments, the same mma * chain per 128-K block and the same fold order. Ragged row counts, pitched rows and a K that * leaves the decode form's last wave group partly idle are covered; the output's pitch padding - * stays untouched. */ + * stays untouched. The grouped row at GLM's MTP slices is held to a host restatement + * (grouped_cases). */ #include "rad_abi.h" #include "rad_plugin.h" #include "r4d_plugin.h" @@ -29,6 +30,161 @@ static RadTensor mat(void* p, uint32_t dt, int64_t rows, int64_t cols, int64_t l return t; } +/* The grouped row at GLM's MTP slices (288 experts, top 8) below 384 routed rows -- the small-M + * form at gate_up, the expert-major form at down -- against a host restatement of the + * per-element sequence: per 128 of K in order, the + * 128-product sum of the emulated WMMA chain from zero, folded as raw * (activation scale * + * weight scale) and added. Dropped slots inside runs and past the routed rows, runs longer than an + * A tile, token and sorted rows, pointer tables over three slices a class. */ +static int grouped_cases(int& cases) { + struct G { + const char* name; + int N, K; + bool sorted_a; + std::vector live; + int tail; + const char* kernel; + }; + const G all[] = { + { "MTP gate_up, one token", 512, 4096, false, { 1, 1, 1, 1, 1, 1, 1, 1 }, 0, + "fp8_f32scale_small<512>" }, + { "MTP gate_up, runs past one tile", 512, 4096, false, { 12, 1, 16, 17, 3, 0, 5, 9, 2, 33 }, 2, + "fp8_f32scale_small<512>" }, + { "MTP down, six tokens", 4096, 256, true, { 3, 1, 10, 2, 1, 5, 1, 1, 17, 4 }, + 3, "(fp8_f32scale_experts<128, 4>)" }, + }; + static const int at[] = { 2, 3, 50, 101, 150, 199, 200, 241, 286, 287 }; + int fails = 0; + std::mt19937 rng(77); + std::normal_distribution nd(0.f, 1.f); + std::uniform_real_distribution oct(-6.f, 6.f); + for (const G& g : all) { + const int E = 288, top = 8, N = g.N, K = g.K, kb = K / 128; + std::vector runs(E, 0); + for (size_t i = 0; i < g.live.size(); ++i) runs[at[i]] = g.live[i]; + int routed = 0; + for (int r : runs) routed += r; + const int T = (routed + g.tail + top - 1) / top * top; + const int Arows = g.sorted_a ? T : T / top, ap = K + 16, yp = N + 8; + std::vector a((size_t)Arows * ap); + for (auto& v : a) v = rad_f32_to_fp8e4m3(nd(rng)); + std::vector as((size_t)Arows * kb); + for (auto& v : as) v = std::exp2(oct(rng)); + std::vector offsets(E + 1, 0), sorted(T, -1), owner(T, -1); + for (int e = 0; e < E; ++e) offsets[e + 1] = offsets[e] + runs[e]; + for (int e = 0; e < E; ++e) + for (int r = offsets[e]; r < offsets[e + 1]; ++r) { + owner[r] = e; + sorted[r] = (r * 13 + e) % 11 == 0 ? -1 : (int)(rng() % (uint32_t)T); + } + std::vector w[2]; + std::vector s[2]; + std::vector wt[2], st[2]; + for (int q = 0; q < 2; ++q) { + w[q].resize((size_t)3 * N * K); + s[q].resize((size_t)3 * (N / 128) * kb); + for (auto& v : w[q]) v = rad_f32_to_fp8e4m3(nd(rng) * 0.5f); + for (auto& v : s[q]) v = std::exp2(oct(rng)) * 0.01f; + for (int x = 0; x < E / 2; ++x) { + wt[q].push_back(w[q].data() + (size_t)(x % 3) * N * K); + st[q].push_back(s[q].data() + (size_t)(x % 3) * (N / 128) * kb); + } + } + std::vector ref((size_t)T * yp, 0x7777), got((size_t)T * yp, 0x7777); + for (int r = 0; r < T; ++r) + for (int n = 0; n < N; ++n) { + uint16_t& out = ref[(size_t)r * yp + n]; + out = 0; + if (owner[r] < 0) continue; + const int e = owner[r], q = e & 1; + const bool rv = sorted[r] >= 0; + const int ar = !rv ? -1 : g.sorted_a ? r : sorted[r] / top; + const uint8_t* wr = (const uint8_t*)wt[q][(size_t)(e / 2)] + (size_t)n * K; + const float* sr = (const float*)st[q][(size_t)(e / 2)] + (size_t)(n / 128) * kb; + float acc = 0.f; + for (int b = 0; b < kb; ++b) { + float raw = 0.f; + if (rv) + for (int k = b * 128; k < b * 128 + 128; ++k) + raw += rad_fp8e4m3_to_f32(a[(size_t)ar * ap + k]) * + rad_fp8e4m3_to_f32(wr[k]); + const float scale = rv ? as[(size_t)ar * kb + b] * sr[b] : 0.f; + const float t = raw * scale; + acc = acc + t; + } + uint32_t u; + std::memcpy(&u, &acc, 4); + out = (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); + } + auto ten3 = [](const void* const* tab, uint32_t dt, int64_t n0, int64_t n1, int64_t n2) { + RadTensor t{}; + t.data = (void*)tab; + t.dtype = dt; + t.rank = 3; + t.shape[0] = n0; + t.shape[1] = n1; + t.shape[2] = n2; + t.stride[0] = 0; + t.stride[1] = n2; + t.stride[2] = 1; + return t; + }; + RadTensor t[9] = { mat(a.data(), RAD_F8E4M3, Arows, K, ap), + mat(as.data(), RAD_F32, Arows, kb, kb), + ten3(wt[0].data(), RAD_F8E4M3, E / 2, N, K), + ten3(st[0].data(), RAD_F32, E / 2, N / 128, kb), + RadTensor{}, + RadTensor{}, + ten3(wt[1].data(), RAD_F8E4M3, E / 2, N, K), + ten3(st[1].data(), RAD_F32, E / 2, N / 128, kb), + mat(got.data(), RAD_BF16, T, N, yp) }; + t[4].data = sorted.data(); + t[4].dtype = RAD_I32; + t[4].rank = 1; + t[4].shape[0] = T; + t[4].stride[0] = 1; + t[5].data = offsets.data(); + t[5].dtype = RAD_I32; + t[5].rank = 1; + t[5].shape[0] = E + 1; + t[5].stride[0] = 1; + RadParam p[10]{}; + p[0] = { "M", RAD_P_INT, T / top }; + p[1] = { "N", RAD_P_INT, N }; + p[2] = { "K", RAD_P_INT, K }; + p[3] = { "N_odd", RAD_P_INT, N }; + p[4] = { "K_odd", RAD_P_INT, K }; + p[5] = { "n_expert", RAD_P_INT, E }; + p[6] = { "top_k", RAD_P_INT, top }; + p[7] = { "group", RAD_P_INT, 128 }; + p[8].key = "a_order"; + p[8].kind = RAD_P_STR; + p[8].sval = g.sorted_a ? "sorted" : "token"; + p[9].key = "dtype"; + p[9].kind = RAD_P_STR; + p[9].sval = "fp8a8f32"; + RadArgs ar{}; + ar.t = t; + ar.n_t = 9; + ar.p = p; + ar.n_p = 10; + emu_last_kernel = ""; + ++cases; + const int rc = r4d_rad_moe_fp8_f32scale(&ar, nullptr); + size_t i = 0; + while (i < got.size() && got[i] == ref[i]) ++i; + int live = 0; + for (uint16_t v : ref) live += v != 0 && v != 0x7777; + if (rc != RAD_OK || std::strcmp(emu_last_kernel, g.kernel) || i != got.size() || + live < routed * N / 2) { + std::printf("FAIL %s (T %d): ran %s (want %s), rc %d, first word %zu of %zu, %d live\n", + g.name, T, emu_last_kernel, g.kernel, rc, i, got.size(), live); + ++fails; + } + } + return fails; +} + int main() { std::mt19937 rng(31); std::normal_distribution nd(0.f, 1.f); @@ -113,6 +269,7 @@ int main() { ++fails; } } + fails += grouped_cases(cases); std::printf("%d case(s), %d failure(s)\n", cases, fails); return fails ? 1 : 0; }