Archived
gemm_w4a8i8_tiled / gemm_w5a8i8_tiled: the weight loaded a slab ahead
As #197 did for gemm_mxfp4a8_tiled (a85f87e): the weight words a thread stages (the nibbles, the
fifth bits, the zero word) are loaded into registers a slab ahead, right after the staging
barrier, so their DRAM round trip runs under the slab's WMMAs instead of in front of the next
unpack. The slab's weight scales are loaded after the WMMAs, just before the fold: held from the
top of the slab next to the words they need 248/251 VGPRs (int4/int5), 5 waves a SIMD instead of
6, and the kernel ran 4-8 % slower than before; loaded late it is 224/227 VGPRs (was 235/237),
no spills. Same fold, same WMMA order: bit for bit.
r4d_selftest --perfwpa (new: warmed min/median and an FNV hash of the output), one R9700 at
230 W, M = 1024, old and new interleaved, 12 rounds, median: int5 gate_up 34816x5120 2769 ->
2572 us, down 5120x17408 1394 -> 1310, delta-net qkv|z 1297 -> 1219, ssm_out 461 -> 430,
q|gate 937 -> 889 (int4 -6..-9 %). Hashes equal the old kernel's on ten shapes at 1024 and 2048
rows. End to end, Qwen3.8-27B PARO int5 at tp 1 with 1024-token chunks (BetterBench, old / new /
old): 23.5k-token prompt 2096 / 2151 / 2085 tok/s, 47k 1934 / 1979 / 1927.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aa989db567
commit
2e1a5022a9
2 files changed
+134
-21
No files matched your search
@@ -22,6 +22,13 @@
|
||||
// nearly every element (perplexity equals the f16 path's). Accumulators are exact int32: a slab is 128
|
||||
// products of at most 31 * 127.
|
||||
//
|
||||
// The weight words a thread stages (the nibbles, the fifth bits, the zero word) are loaded a slab ahead,
|
||||
// into registers, right after the staging barrier, so their DRAM round trip runs under the slab's WMMAs
|
||||
// instead of in front of the next unpack (as r4d_gemm_mxfp4a8_tiled.hip does). The slab's weight scales
|
||||
// are loaded after the WMMAs, just before the fold: held from the top of the slab next to the words they
|
||||
// need 240+ VGPRs (spills, and 5 waves a SIMD instead of 6); loaded late the kernel is 224 (4-bit) / 227
|
||||
// (5-bit) VGPRs, no spills. Same fold, same WMMA order: bit for bit.
|
||||
//
|
||||
// Staging: for a code byte c (0..31) and zero z (0..31), (c | 0x80) - z = 128 + (c - z) never drops
|
||||
// below 97 or borrows across bytes, and XOR 0x80 gives the two's complement byte of (c - z). Three ALU
|
||||
// ops per dword of four weights.
|
||||
@@ -125,6 +132,20 @@ __global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) tot[i][j][e] = 0.f;
|
||||
|
||||
// The slab's weight words, loaded a slab ahead (the first one here).
|
||||
wpt_u4 wvn[NW];
|
||||
unsigned hbn[NW], szn[NW];
|
||||
auto wload = [&](int k0_, int g_) {
|
||||
#pragma unroll
|
||||
for (int it = 0; it < NW; ++it) {
|
||||
const size_t blk = (size_t)wt[it] * kpb + k0_ / 64 + wkb[it];
|
||||
wvn[it] = *(const wpt_u4*)&Wq[blk * 128 + wlane[it] * 4];
|
||||
if constexpr (BITS == 5) hbn[it] = Whi[blk * 32 + wlane[it]]; // byte s: the fifth bits of k step s
|
||||
szn[it] = Wsz[((size_t)wt[it] * nsz + g_) * 16 + (wlane[it] & 15)];
|
||||
}
|
||||
};
|
||||
wload(0, 0);
|
||||
|
||||
for (int k0 = 0, g = 0; k0 < K; k0 += LBK, ++g) {
|
||||
// A fragments for the slab, one clause, consumed only at the WMMAs.
|
||||
wpt_v2i af[TM][NS];
|
||||
@@ -134,34 +155,18 @@ __global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
for (int st = 0; st < NS; ++st)
|
||||
af[i][st] = *(const wpt_v2i*)(abase[i] +
|
||||
(aoff + (unsigned int)(k0 * 16) + (unsigned int)(st * 256)));
|
||||
// The scales of this slab, issued ahead of the staging that hides their latency.
|
||||
// The activation scales of this slab, issued ahead of the staging that hides their latency.
|
||||
float sa[TM];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TM; ++i) sa[i] = arow[i][g];
|
||||
float ws[TN][8];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TN; ++j) {
|
||||
int t = (n0 + wn * TN * 16 + j * 16) >> 4;
|
||||
t = t < ntile_last ? t : ntile_last;
|
||||
const wpt_u4* p = (const wpt_u4*)(Wsz + ((size_t)t * nsz + g) * 16 + kb8);
|
||||
const wpt_u4 v0 = p[0], v1 = p[1];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 4; ++e) {
|
||||
ws[j][e] = __half2float(__ushort_as_half((unsigned short)(v0[e] & 0xFFFFu)));
|
||||
ws[j][4 + e] = __half2float(__ushort_as_half((unsigned short)(v1[e] & 0xFFFFu)));
|
||||
}
|
||||
}
|
||||
// W slab -> LDS as int8 (code - zero), by true k. A dword's element e is nibble 2e (e < 4) or
|
||||
// 2(e-4)+1, and k = 8*(e>>2) + 4*(lane>>4) + (e&3): a lane writes its elements 0..3 at
|
||||
// k = 4*(lane>>4) and 4..7 at +8, and the WMMA reads eight consecutive bytes.
|
||||
#pragma unroll
|
||||
for (int it = 0; it < NW; ++it) {
|
||||
const size_t blk = (size_t)wt[it] * kpb + k0 / 64 + wkb[it];
|
||||
const wpt_u4 wv = *(const wpt_u4*)&Wq[blk * 128 + wlane[it] * 4];
|
||||
unsigned hb4 = 0;
|
||||
if constexpr (BITS == 5) hb4 = Whi[blk * 32 + wlane[it]]; // byte s: the fifth bits of k step s
|
||||
const unsigned sz = Wsz[((size_t)wt[it] * nsz + g) * 16 + (wlane[it] & 15)];
|
||||
const unsigned zz = ((sz >> 16) & 0x1Fu) * 0x01010101u; // high half is -(1024 + zero) in f16
|
||||
const wpt_u4 wv = wvn[it];
|
||||
const unsigned hb4 = BITS == 5 ? hbn[it] : 0u;
|
||||
const unsigned zz = ((szn[it] >> 16) & 0x1Fu) * 0x01010101u; // high half is -(1024 + zero) in f16
|
||||
#pragma unroll
|
||||
for (int s_ = 0; s_ < 4; ++s_) {
|
||||
const unsigned w = wv[s_];
|
||||
@@ -177,6 +182,7 @@ __global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
}
|
||||
}
|
||||
lds_barrier();
|
||||
if (k0 + LBK < K) wload(k0 + LBK, g + 1); // the next slab's weight flies across this slab's WMMAs
|
||||
|
||||
wpt_v8i gacc[TM][TN];
|
||||
#pragma unroll
|
||||
@@ -197,6 +203,21 @@ __global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
? __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, wf[j], true, af[i][step], (wpt_v8i)(0), false)
|
||||
: __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, wf[j], true, af[i][step], gacc[i][j], false);
|
||||
}
|
||||
// The weight scales of this slab, loaded only now: held from the top of the slab they would not fit
|
||||
// next to the words held ahead.
|
||||
float ws[TN][8];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TN; ++j) {
|
||||
int t = (n0 + wn * TN * 16 + j * 16) >> 4;
|
||||
t = t < ntile_last ? t : ntile_last;
|
||||
const wpt_u4* p = (const wpt_u4*)(Wsz + ((size_t)t * nsz + g) * 16 + kb8);
|
||||
const wpt_u4 v0 = p[0], v1 = p[1];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 4; ++e) {
|
||||
ws[j][e] = __half2float(__ushort_as_half((unsigned short)(v0[e] & 0xFFFFu)));
|
||||
ws[j][4 + e] = __half2float(__ushort_as_half((unsigned short)(v1[e] & 0xFFFFu)));
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TM; ++i)
|
||||
#pragma unroll
|
||||
|
||||
+93
-1
@@ -4905,6 +4905,86 @@ static void wpa8i8_numeric(const Plugin& pl, int bits, int64_t M, int64_t N, int
|
||||
}
|
||||
|
||||
|
||||
/* --perfwpa N K M [iters] [pb1]: the int4/int5 x int8 tiled GEMM, warmed, with an FNV hash of the
|
||||
* output (the inputs are deterministic, so two builds can be compared bit for bit). */
|
||||
static void wpa_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int iters, int64_t pb1) {
|
||||
for (int bits = 5; bits >= 4; --bits) {
|
||||
const char* row = bits == 4 ? "gemm_w4a8i8_tiled" : "gemm_w5a8i8_tiled";
|
||||
const PartCut cut(N, pb1, 0);
|
||||
const int parts = cut.parts;
|
||||
const RadKernelInfo* r = find(pl, row);
|
||||
if (!r || !r->layout || !r->relayout) { std::printf("--perfwpa: row %s absent\n", row); return; }
|
||||
const int64_t G = K / 128;
|
||||
const Planes p = asym_planes(bits, N, K, RAD_U8);
|
||||
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 128), PS("dtype", bits == 4 ? "w4a8i8_asym" : "w5a8i8") };
|
||||
std::vector<uint8_t> wq, ws;
|
||||
if (stored(r, lp, 2, p, { "codes" }, &wq) != RAD_OK || stored(r, lp, 3, p, { "scale", "zero" }, &ws) != RAD_OK) {
|
||||
std::printf("--perfwpa: relayout failed\n"); return;
|
||||
}
|
||||
const int64_t PK = parts * K, PG = parts * G;
|
||||
std::vector<uint8_t> ha((size_t)(M * PK));
|
||||
std::vector<float> as((size_t)(M * PG));
|
||||
for (int64_t i = 0; i < M; ++i) {
|
||||
for (int64_t c = 0; c < PG; ++c)
|
||||
as[(size_t)(i * PG + c)] = 0.002f * (float)(1 + st_mix32((uint32_t)(i * PG + c) ^ 0xa5u) % 97);
|
||||
for (int64_t k = 0; k < PK; ++k)
|
||||
ha[(size_t)(i * PK + k)] = (uint8_t)(int8_t)((int)(st_mix32((uint32_t)(i * PK + k) ^ 0x3cu) % 255u) - 127);
|
||||
}
|
||||
std::vector<uint16_t> hy((size_t)(M * N), 0);
|
||||
void* d_a = dalloc(ha.size());
|
||||
void* d_s = dalloc(as.size() * 4);
|
||||
void* d_w = dalloc(wq.size());
|
||||
void* d_e = dalloc(ws.size());
|
||||
void* d_y = dalloc(hy.size() * 2);
|
||||
if (!d_a || !d_s || !d_w || !d_e || !d_y) { std::printf("--perfwpa: hipMalloc failed\n"); return; }
|
||||
st_memcpy(d_a, ha.data(), ha.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_s, as.data(), as.size() * 4, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_w, wq.data(), wq.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_e, ws.data(), ws.size(), hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_a, RAD_I8, { M, PK }), T(d_s, RAD_F32, { M, PG }),
|
||||
T(d_w, RAD_U8, { (int64_t)wq.size() }), T(d_e, RAD_U8, { (int64_t)ws.size() }),
|
||||
T(d_y, RAD_BF16, { M, N }), T(nullptr, RAD_F32, { 1 }), T(nullptr, RAD_F32, { 1 }),
|
||||
};
|
||||
absent(ts[5]); absent(ts[6]);
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("N", N), PI("K", K), PI("group", 128),
|
||||
PS("dtype", bits == 4 ? "w4a8i8_asym" : "w5a8i8") };
|
||||
if (pb1) ps.push_back(PI("pb1", pb1));
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
a.p = ps.data(); a.n_p = (int)ps.size();
|
||||
a.world_size = 1;
|
||||
if (r->scratch) {
|
||||
const int64_t sb = r->scratch(&a);
|
||||
if (sb > 0) { a.scratch = dalloc((size_t)sb); a.scratch_bytes = sb; }
|
||||
}
|
||||
for (int i = 0; i < 3; ++i) r->launch(&a, nullptr);
|
||||
st_stream_sync(nullptr);
|
||||
st_memcpy(hy.data(), d_y, hy.size() * 2, hipMemcpyDeviceToHost);
|
||||
uint64_t hash = 1469598103934665603ull;
|
||||
for (uint16_t x : hy) { hash ^= x; hash *= 1099511628211ull; }
|
||||
std::vector<double> us;
|
||||
for (int round = 0; round < 10; ++round) {
|
||||
hipEvent_t e0, e1;
|
||||
st_ev_create(&e0); st_ev_create(&e1);
|
||||
st_ev_record(e0, nullptr);
|
||||
for (int i = 0; i < iters; ++i) r->launch(&a, nullptr);
|
||||
st_ev_record(e1, nullptr);
|
||||
st_ev_sync(e1);
|
||||
float ms = 0;
|
||||
st_ev_elapsed(&ms, e0, e1);
|
||||
st_ev_destroy(e0); st_ev_destroy(e1);
|
||||
us.push_back((double)ms * 1000.0 / iters);
|
||||
}
|
||||
std::sort(us.begin(), us.end());
|
||||
std::printf("perfwpa w%d N=%lld K=%lld M=%lld pb1=%lld min %9.1f med %9.1f us %6.1f TF/s(min) hash %016llx\n",
|
||||
bits, (long long)N, (long long)K, (long long)M, (long long)pb1, us.front(), us[us.size() / 2],
|
||||
2.0 * M * N * K / (us.front() * 1e6), (unsigned long long)hash);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Two decode GEMMs the engine may overlap (gemm_mxfp4a8_decode is a concurrent row: its split
|
||||
* partial is the op's scratch and its arrival counters are keyed on the output). Each GEMM has its
|
||||
* own operands and a disjoint slice of one scratch allocation, with a split (ks > 1) so the partial
|
||||
@@ -28678,6 +28758,8 @@ static void usage(void) {
|
||||
" u64 at byte 0> + the tensor's own data_offsets[0].\n"
|
||||
" --perf N K M [iters]: time the fp8 decode GEMMs at one shape and print the\n"
|
||||
" weight bandwidth each reaches.\n"
|
||||
" --perfwpa N K M [iters] [pb1]: the int5 and int4 x int8 tiled GEMMs (gemm_w5a8i8_tiled,\n"
|
||||
" gemm_w4a8i8_tiled), warmed, min / median time and a hash of the output.\n"
|
||||
" --perfmxd N K M [iters]: the dense MXFP4 GEMM rows (gemv, decode, skinny, tiled) at\n"
|
||||
" one shape, with the weight bandwidth each reaches.\n"
|
||||
" --perfbf16 N K M [iters]: the bf16 skinny GEMM at every variant it has.\n"
|
||||
@@ -28792,7 +28874,8 @@ int main(int argc, char** argv) {
|
||||
const char* real_file = nullptr;
|
||||
long long real_woff = 0, real_soff = 0, real_n = 0, real_k = 0;
|
||||
long long perf_n = 0, perf_k = 0, perf_m = 0;
|
||||
int perf_it = 50, perf_w2 = 0, perf_bf = 0, perf_w8 = 0, perf_f32 = 0, perf_q = 0, perf_mx = 0;
|
||||
int perf_it = 50, perf_w2 = 0, perf_bf = 0, perf_w8 = 0, perf_f32 = 0, perf_q = 0, perf_mx = 0, perf_wpa = 0;
|
||||
long long perf_pb1 = 0;
|
||||
long long sop_m = 0, sop_k = 0;
|
||||
int sop_it = 2000;
|
||||
long long qsa_ctx = 0, qsa_m = 4, dsa_ctx = 0, dsa_m = 6;
|
||||
@@ -28853,6 +28936,14 @@ int main(int argc, char** argv) {
|
||||
perf_k = std::strtoll(argv[++i], nullptr, 0);
|
||||
perf_m = std::strtoll(argv[++i], nullptr, 0);
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') perf_it = (int)std::strtol(argv[++i], nullptr, 0);
|
||||
} else if (!std::strcmp(argv[i], "--perfwpa")) {
|
||||
if (i + 3 >= argc) { usage(); return 2; }
|
||||
perf_wpa = 1;
|
||||
perf_n = std::strtoll(argv[++i], nullptr, 0);
|
||||
perf_k = std::strtoll(argv[++i], nullptr, 0);
|
||||
perf_m = std::strtoll(argv[++i], nullptr, 0);
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') perf_it = (int)std::strtol(argv[++i], nullptr, 0);
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') perf_pb1 = std::strtoll(argv[++i], nullptr, 0);
|
||||
} else if (!std::strcmp(argv[i], "--perfmxd")) {
|
||||
if (i + 3 >= argc) { usage(); return 2; }
|
||||
perf_mx = 1;
|
||||
@@ -29261,6 +29352,7 @@ int main(int argc, char** argv) {
|
||||
#ifdef RAD_HAVE_HIP
|
||||
if (perf_f32) f32_forms(pl, perf_m, perf_n, perf_k, perf_it, true);
|
||||
else if (perf_q) q_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_wpa) wpa_perf(pl, perf_n, perf_k, perf_m, perf_it, perf_pb1);
|
||||
else if (perf_mx) mx_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_bf) bf16_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_w8) w8_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
|
||||
Reference in new issue
Block a user