mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 23:38:47 +00:00
perf(inkling2/cuda): prefill MoE for sm_80..89 takes 64 tokens of an expert per tile, bit for bit the same results
k_ink_moe_sf4_mx8_fp16tc works on 16 tokens of one expert at a time, so at a 2048-token chunk (about 128 tokens per expert) every FP4 / FP8 weight tile is decoded to FP16 again for each group of 16 tokens and the expert's activation tile is staged from global memory 8 times per weight block. k_ink_moe_pf_fp16tc (new file ink_moe_prefill.cuh) takes 64 tokens per tile for chunks of 256 tokens or more on sm_80..89: the weight tile is decoded once per 64 tokens and the m16n8k16 MMAs run 8 times per decoded tile. Each output element is accumulated as fma(scale of the 16-block, mma(16 weights, 16 activations), acc) in the same k order as before and the MMA result of a (row, token) pair does not depend on the other tokens of the tile, so the logits do not change. STRATA_INK_PF_TILE=0 keeps the 16-token kernel. RTX 4090, UD-Q4_K_XL, prompt of 4,096 real-text tokens, chunk 2048, alternating A/B of the variable over 2 rounds (a fresh engine each): prefill 2,402.5 / 2,402.5 tok/s with the 16-token kernel, 2,985.7 / 2,984.2 with the 64-token tile (+24.3%, spread 0.1%); the generated tokens have the same hash in every start. The dumped logits of 2,100 positions are bit-identical between the two settings at chunks 2048, 1024, 512 and 256.
This commit is contained in:
1 parent
fb5973183e
commit
0c655e75ec
2 files changed
+267
-1
No files matched your search
@@ -3595,6 +3595,8 @@ struct InkLayerMoeInfo {
|
||||
uint32_t sk_dn_mask = 0;
|
||||
};
|
||||
|
||||
#include "ink_moe_prefill.cuh" // k_ink_moe_pf_fp16tc: the 64-token-tile prefill MoE (sm_80..89), see the header
|
||||
|
||||
template <int MAX_M>
|
||||
inline void launch_ink_moe_gu_sk_mask(
|
||||
dim3 grid, cudaStream_t st, uint32_t mask,
|
||||
@@ -3676,7 +3678,12 @@ inline void launch_ink_moe_dispatch_only(
|
||||
const bool use_sm120_tc = (!force_exact_dec && M >= 24 && sm120::available());
|
||||
const bool use_fp16_tc = (!use_sm120_tc && M >= 24 && ink_has_int8_tc());
|
||||
const bool use_wmma_tc = (!use_sm120_tc && !use_fp16_tc && M >= 24 && ink_has_wmma_f16());
|
||||
const int tile_rows = use_wmma_tc ? ((M <= 128) ? 16 : 64)
|
||||
// Prefill chunks on sm_80..89: 64-token tiles per expert (the weight tile is decoded once per 64 tokens, not per 16); same arithmetic,
|
||||
// bit-identical results. STRATA_INK_PF_TILE=0 keeps the 16-token kernel.
|
||||
static const bool pf_tile_env = [] { const char* e = std::getenv("STRATA_INK_PF_TILE"); return !(e && e[0] == '0'); }();
|
||||
const bool use_pf_tile = pf_tile_env && use_fp16_tc && M >= kInkPrefillTileMinM && (info.has_sf4 || info.has_mx8);
|
||||
const int tile_rows = use_pf_tile ? kInkPrefillTileRows
|
||||
: use_wmma_tc ? ((M <= 128) ? 16 : 64)
|
||||
: ((M <= 128 || use_fp16_tc) ? 16 : 64);
|
||||
k_ink_moe_bin<<<1, 128, 0, st>>>(experts, ws.topk_idx, M, tile_rows, ws.tb);
|
||||
ink_moe_mark(1);
|
||||
@@ -3730,6 +3737,13 @@ inline void launch_ink_moe_dispatch_only(
|
||||
sm120::moe_mx8_gu_tc(sec4_base, experts, ws.tb, ws.xq_mx8, ws.xsc_mx8, ws.hq_mx8, ws.hsc_mx8, mt, tile_rows, st);
|
||||
sm120::moe_mx8_dn_tc(sec4_base, experts, ws.tb, ws.hq_mx8, ws.hsc_mx8, ws.topk_w, ws.y_pair, mt, tile_rows, st);
|
||||
}
|
||||
} else if (use_fp16_tc && use_pf_tile) {
|
||||
const int mt = ink_moe_max_tiles(M, kInkPrefillTileRows);
|
||||
const unsigned grid = (unsigned)std::min(std::max(1, mt * 18), 4096);
|
||||
k_ink_moe_pf_fp16tc<true, kInkPrefillTileRows / 8><<<grid, 256, 0, st>>>(sec4_base, experts, ws.tb, x_f32, nullptr, ws.h_f32);
|
||||
ink_moe_mark(2);
|
||||
k_ink_moe_pf_fp16tc<false, kInkPrefillTileRows / 8><<<grid, 256, 0, st>>>(sec4_base, experts, ws.tb, ws.h_f32, ws.topk_w, ws.y_pair);
|
||||
ink_moe_mark(3);
|
||||
} else if (use_fp16_tc) {
|
||||
const int mt = ink_moe_max_tiles(M, 16);
|
||||
const unsigned grid = (unsigned)std::min(std::max(1, mt * 18), 2048);
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
// ink_moe_prefill.cuh: the prefill MoE for sm_80..sm_89 (SF4 and MX8 experts): the same exact FP16 tensor-core arithmetic as
|
||||
// k_ink_moe_sf4_mx8_fp16tc, with a bigger token tile per expert.
|
||||
//
|
||||
// Why: k_ink_moe_sf4_mx8_fp16tc works on 16 tokens of one expert at a time, so at a 2048-token prefill chunk (about 128 tokens per
|
||||
// expert) every FP4 / FP8 weight tile is decoded to FP16 again for each group of 16 tokens (8 times) and the activation tile of an
|
||||
// expert is staged from global memory 8 times per weight block. Here one block takes TR = 8 * NT tokens (64 by default): the weight tile
|
||||
// is decoded once per 64 tokens, and the m16n8k16 MMAs run NT times per decoded tile.
|
||||
//
|
||||
// Arithmetic (so the results do not change): each output element (weight row, token) is accumulated as acc = fma(scale_of_the_16_block,
|
||||
// mma_m16n8k16(16 weights, 16 activations), acc) in the same k order as the 16-token kernel, and the MMA result of one (row, token)
|
||||
// pair does not depend on the other tokens in the tile. The kernel is therefore bit-identical to k_ink_moe_sf4_mx8_fp16tc.
|
||||
//
|
||||
// Included from ink_moe_cuda.cuh after the helpers it uses (sf4_decode16_h2, mx8_decode16_h2, e4m3_to_f32, ue8m0_to_f32,
|
||||
// mma_m16n8k16_f16, situ_f32) and before launch_ink_moe_dispatch_only, which calls it for M >= kInkPrefillTileMinM.
|
||||
#pragma once
|
||||
|
||||
constexpr int kInkPrefillTileMinM = 256; // below this a chunk has too few tokens per expert for a 64-token tile to pay
|
||||
constexpr int kInkPrefillTileRows = 64; // tokens per tile (NT = 8 n-tiles of 8); the bin kernel must be called with tile_rows = this
|
||||
|
||||
template <bool GU, int NT>
|
||||
__global__ void __launch_bounds__(256) k_ink_moe_pf_fp16tc(
|
||||
const uint8_t* __restrict__ sec4_base,
|
||||
const InkExpertDev* __restrict__ experts,
|
||||
InkMoeTables tb,
|
||||
const float* __restrict__ act_f32, // GU: x_f32 [M, 2304]; DN: h_f32 [P, 1152]
|
||||
const float* __restrict__ topk_w, // DN only
|
||||
float* __restrict__ out_buf) { // GU: h_f32 [P, 1152]; DN: y_pair [P, 2304]
|
||||
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800)
|
||||
constexpr int kWR = 128;
|
||||
constexpr int TR = 8 * NT;
|
||||
constexpr int K = GU ? kInkHidden : kInkInter; // 2304 or 1152
|
||||
constexpr int ROWS_N = GU ? (2 * kInkInter) : kInkHidden; // 2304
|
||||
constexpr int NFB = ROWS_N / kWR; // 18 blocks of 128 weight rows
|
||||
constexpr int N32 = K / 32; // 72 (GU) or 36 (DN)
|
||||
|
||||
__shared__ alignas(16) __half s_w[kWR][40];
|
||||
__shared__ alignas(16) __half s_a[TR][40];
|
||||
__shared__ float s_wsc[kWR][2];
|
||||
__shared__ int32_t s_row[TR];
|
||||
__shared__ float s_rw[TR];
|
||||
|
||||
const int tid = threadIdx.x;
|
||||
const int warp = tid >> 5; // 0..7: each warp owns 16 weight rows
|
||||
const int lane = tid & 31;
|
||||
const int g = lane >> 2;
|
||||
const int tig = lane & 3;
|
||||
|
||||
const int total_tiles = *tb.n_tiles;
|
||||
const int nwork = total_tiles * NFB;
|
||||
|
||||
for (int w = blockIdx.x; w < nwork; w += gridDim.x) {
|
||||
const int2 tl = tb.tiles[w / NFB];
|
||||
const int fb = w % NFB;
|
||||
const int e = tl.x;
|
||||
const int row0 = tl.y;
|
||||
const InkExpertDev ed = experts[e];
|
||||
const int fmt = GU ? (int)ed.gu_code : (int)ed.dn_code;
|
||||
if (ed.tier > 1 || (fmt != FMT_SF4 && fmt != FMT_MX8)) continue;
|
||||
const int nrows = min(TR, tb.off[e + 1] - row0);
|
||||
if (nrows <= 0) continue;
|
||||
const int nnt = (nrows + 7) >> 3; // n-tiles of 8 tokens that hold a real row (the rest of the tile is skipped)
|
||||
|
||||
const uint8_t* body = sec4_base + ed.body_off;
|
||||
float w_outer = 1.0f;
|
||||
if (fmt == FMT_SF4) {
|
||||
float hdr[4];
|
||||
memcpy(hdr, body, 16);
|
||||
w_outer = GU ? (hdr[0] / hdr[1]) : (hdr[2] / hdr[3]);
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
if (tid < TR) {
|
||||
if (tid < nrows) {
|
||||
s_row[tid] = GU ? tb.tok_idx[row0 + tid] : (row0 + tid);
|
||||
if constexpr (!GU) s_rw[tid] = topk_w[tb.pairs[row0 + tid]];
|
||||
} else {
|
||||
s_row[tid] = -1;
|
||||
if constexpr (!GU) s_rw[tid] = 0.0f;
|
||||
}
|
||||
}
|
||||
// rows of the tile past the expert's last token: zero activations (their results are never stored)
|
||||
for (int i = tid; i < (TR - nrows) * 16; i += 256) {
|
||||
const int r = nrows + (i >> 4);
|
||||
reinterpret_cast<uint32_t*>(s_a[r])[i & 15] = 0u;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
float acc[NT][4];
|
||||
#pragma unroll
|
||||
for (int n = 0; n < NT; ++n) { acc[n][0] = acc[n][1] = acc[n][2] = acc[n][3] = 0.0f; }
|
||||
const int wr0 = 16 * warp + g;
|
||||
const int wr1 = wr0 + 8;
|
||||
|
||||
auto phys_row_fn = [&](int r) -> int {
|
||||
return GU ? (2 * (fb * 64 + 8 * (r >> 4) + (r & 7)) + ((r >> 3) & 1))
|
||||
: (fb * kWR + r);
|
||||
};
|
||||
|
||||
// activations of this k32 step -> s_a (every thread helps: nrows * 8 float4 loads)
|
||||
auto load_acts = [&](int k32) {
|
||||
for (int t = tid; t < nrows * 8; t += 256) {
|
||||
const int r = t >> 3;
|
||||
const int j = t & 7;
|
||||
const float4 fv = *reinterpret_cast<const float4*>(act_f32 + (size_t)s_row[r] * K + k32 * 32 + j * 4);
|
||||
__half2* da = reinterpret_cast<__half2*>(s_a[r]);
|
||||
da[2 * j + 0] = __floats2half2_rn(fminf(fmaxf(fv.x, -65504.0f), 65504.0f),
|
||||
fminf(fmaxf(fv.y, -65504.0f), 65504.0f));
|
||||
da[2 * j + 1] = __floats2half2_rn(fminf(fmaxf(fv.z, -65504.0f), 65504.0f),
|
||||
fminf(fmaxf(fv.w, -65504.0f), 65504.0f));
|
||||
}
|
||||
};
|
||||
|
||||
if (fmt == FMT_SF4) {
|
||||
constexpr size_t W_P_ROW = K / 2;
|
||||
constexpr size_t W_S_ROW = K / 16;
|
||||
const uint8_t* base_p = GU ? (body + 16) : (body + ed.dn_off);
|
||||
const uint8_t* base_s = base_p + (size_t)ROWS_N * W_P_ROW;
|
||||
const int pr_t = (tid < kWR) ? phys_row_fn(tid) : 0;
|
||||
const uint4* wp16 = reinterpret_cast<const uint4*>(base_p + (size_t)pr_t * W_P_ROW);
|
||||
const uint16_t* ws2 = reinterpret_cast<const uint16_t*>(base_s + (size_t)pr_t * W_S_ROW);
|
||||
|
||||
for (int k32 = 0; k32 < N32; ++k32) {
|
||||
if (tid < kWR) {
|
||||
const uint4 v = wp16[k32];
|
||||
const uint16_t sp = ws2[k32];
|
||||
s_wsc[tid][0] = e4m3_to_f32((uint8_t)(sp & 0xFFu)) * w_outer;
|
||||
s_wsc[tid][1] = e4m3_to_f32((uint8_t)(sp >> 8)) * w_outer;
|
||||
__half2 w0[8], w1[8];
|
||||
sf4_decode16_h2(v.x, v.y, w0);
|
||||
sf4_decode16_h2(v.z, v.w, w1);
|
||||
__half2* dw = reinterpret_cast<__half2*>(s_w[tid]);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) { dw[j] = w0[j]; dw[8 + j] = w1[j]; }
|
||||
}
|
||||
load_acts(k32);
|
||||
__syncthreads();
|
||||
|
||||
#pragma unroll
|
||||
for (int sub = 0; sub < 2; ++sub) {
|
||||
const int ko = sub * 16;
|
||||
const float sc0 = s_wsc[wr0][sub];
|
||||
const float sc1 = s_wsc[wr1][sub];
|
||||
const uint32_t a0 = *reinterpret_cast<const uint32_t*>(s_w[wr0] + ko + 2 * tig);
|
||||
const uint32_t a1 = *reinterpret_cast<const uint32_t*>(s_w[wr1] + ko + 2 * tig);
|
||||
const uint32_t a2 = *reinterpret_cast<const uint32_t*>(s_w[wr0] + ko + 8 + 2 * tig);
|
||||
const uint32_t a3 = *reinterpret_cast<const uint32_t*>(s_w[wr1] + ko + 8 + 2 * tig);
|
||||
#pragma unroll
|
||||
for (int n = 0; n < NT; ++n) {
|
||||
if (n < nnt) {
|
||||
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_a[8 * n + g] + ko + 2 * tig);
|
||||
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_a[8 * n + g] + ko + 8 + 2 * tig);
|
||||
float cv[4] = {};
|
||||
mma_m16n8k16_f16(cv, a0, a1, a2, a3, b0, b1);
|
||||
acc[n][0] = fmaf(sc0, cv[0], acc[n][0]);
|
||||
acc[n][1] = fmaf(sc0, cv[1], acc[n][1]);
|
||||
acc[n][2] = fmaf(sc1, cv[2], acc[n][2]);
|
||||
acc[n][3] = fmaf(sc1, cv[3], acc[n][3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (k32 + 1 < N32) __syncthreads();
|
||||
}
|
||||
} else {
|
||||
constexpr size_t W_P_ROW = K;
|
||||
constexpr size_t W_S_ROW = K / 32;
|
||||
const uint8_t* base_p = GU ? body : (body + ed.dn_off);
|
||||
const uint8_t* base_s = base_p + (size_t)ROWS_N * W_P_ROW;
|
||||
const int r_w = tid >> 1;
|
||||
const int h_w = tid & 1;
|
||||
const int pr_t = phys_row_fn(r_w);
|
||||
const uint4* wp16 = reinterpret_cast<const uint4*>(base_p + (size_t)pr_t * W_P_ROW);
|
||||
const uint8_t* ws = base_s + (size_t)pr_t * W_S_ROW;
|
||||
|
||||
for (int k32 = 0; k32 < N32; ++k32) {
|
||||
__half2 wh[8];
|
||||
mx8_decode16_h2(wp16[k32 * 2 + h_w], wh);
|
||||
__half2* dw = reinterpret_cast<__half2*>(s_w[r_w]) + h_w * 8;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) dw[j] = wh[j];
|
||||
if (h_w == 0) s_wsc[r_w][0] = ue8m0_to_f32(ws[k32]);
|
||||
load_acts(k32);
|
||||
__syncthreads();
|
||||
|
||||
const float sc0 = s_wsc[wr0][0];
|
||||
const float sc1 = s_wsc[wr1][0];
|
||||
uint32_t a[2][4];
|
||||
#pragma unroll
|
||||
for (int sub = 0; sub < 2; ++sub) {
|
||||
const int ko = sub * 16;
|
||||
a[sub][0] = *reinterpret_cast<const uint32_t*>(s_w[wr0] + ko + 2 * tig);
|
||||
a[sub][1] = *reinterpret_cast<const uint32_t*>(s_w[wr1] + ko + 2 * tig);
|
||||
a[sub][2] = *reinterpret_cast<const uint32_t*>(s_w[wr0] + ko + 8 + 2 * tig);
|
||||
a[sub][3] = *reinterpret_cast<const uint32_t*>(s_w[wr1] + ko + 8 + 2 * tig);
|
||||
}
|
||||
#pragma unroll
|
||||
for (int n = 0; n < NT; ++n) {
|
||||
if (n < nnt) {
|
||||
float cv[4] = {};
|
||||
#pragma unroll
|
||||
for (int sub = 0; sub < 2; ++sub) {
|
||||
const int ko = sub * 16;
|
||||
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_a[8 * n + g] + ko + 2 * tig);
|
||||
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_a[8 * n + g] + ko + 8 + 2 * tig);
|
||||
mma_m16n8k16_f16(cv, a[sub][0], a[sub][1], a[sub][2], a[sub][3], b0, b1);
|
||||
}
|
||||
acc[n][0] = fmaf(sc0, cv[0], acc[n][0]);
|
||||
acc[n][1] = fmaf(sc0, cv[1], acc[n][1]);
|
||||
acc[n][2] = fmaf(sc1, cv[2], acc[n][2]);
|
||||
acc[n][3] = fmaf(sc1, cv[3], acc[n][3]);
|
||||
}
|
||||
}
|
||||
if (k32 + 1 < N32) __syncthreads();
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (GU) {
|
||||
const int feat = fb * 64 + 8 * warp + g;
|
||||
#pragma unroll
|
||||
for (int n = 0; n < NT; ++n) {
|
||||
if (n < nnt) {
|
||||
#pragma unroll
|
||||
for (int c = 0; c < 2; ++c) {
|
||||
const int r = 8 * n + 2 * tig + c;
|
||||
if (r < nrows) {
|
||||
out_buf[(size_t)(row0 + r) * kInkInter + feat] = situ_f32(acc[n][c], acc[n][2 + c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const int rbase = fb * kWR + 16 * warp;
|
||||
#pragma unroll
|
||||
for (int n = 0; n < NT; ++n) {
|
||||
if (n < nnt) {
|
||||
#pragma unroll
|
||||
for (int c = 0; c < 2; ++c) {
|
||||
const int r = 8 * n + 2 * tig + c;
|
||||
if (r < nrows) {
|
||||
const float sc = s_rw[r];
|
||||
float* dst = out_buf + (size_t)tb.pairs[row0 + r] * kInkHidden;
|
||||
dst[rbase + g + 0] = acc[n][c] * sc;
|
||||
dst[rbase + g + 8] = acc[n][2 + c] * sc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)sec4_base; (void)experts; (void)tb; (void)act_f32; (void)topk_w; (void)out_buf;
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user