Archived
libr4d: int8 prefill band for ParoQuant int4/int5 (gemm_w4a8i8/w5a8i8 tiled, givens_quant_act_i8g); LinearFP8 uses it above 64 rows
This commit is contained in:
1 parent
cfe03f8ad8
commit
6091039926
16 files changed
+709
-64
No files matched your search
+253
-6
@@ -3741,8 +3741,8 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
* must sit within the E4M3 grid of the reference (half a step at the top of the range is 1/16 of
|
||||
* the group's maximum, so 7% of it is a bound a wrong pair or a wrong angle cannot meet). */
|
||||
static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, int stages,
|
||||
bool rot_only = false) {
|
||||
const char* row = rot_only ? "givens_rotate" : "givens_quant_act_fp8";
|
||||
bool rot_only = false, bool i8 = false) {
|
||||
const char* row = rot_only ? "givens_rotate" : i8 ? "givens_quant_act_i8g" : "givens_quant_act_fp8";
|
||||
if (!sel(row)) return;
|
||||
char nm[160];
|
||||
std::snprintf(nm, sizeof nm, "%s numeric M=%lld K=%lld parts=%d stages=%d", row, (long long)M,
|
||||
@@ -3792,7 +3792,7 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_x, RAD_BF16, { M, K }), T(d_p, RAD_I32, { NPS, K }), T(d_a, RAD_F32, { NPS, K / 2 }),
|
||||
T(d_r, RAD_F32, { (int64_t)parts, K }),
|
||||
T(d_q, rot_only ? (uint32_t)RAD_F16 : (uint32_t)RAD_F8E4M3, { M, parts * K }),
|
||||
T(d_q, rot_only ? (uint32_t)RAD_F16 : i8 ? (uint32_t)RAD_I8 : (uint32_t)RAD_F8E4M3, { M, parts * K }),
|
||||
T(d_s, RAD_F32, { M, parts * ng }),
|
||||
};
|
||||
if (rot_only) ts.pop_back();
|
||||
@@ -3835,16 +3835,17 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
continue;
|
||||
}
|
||||
const double sc = hs[(size_t)(i * parts * ng + p * ng + g)];
|
||||
worst_scale = std::max(worst_scale, std::fabs(sc - amax / 448.0) / (amax / 448.0 + 1e-30));
|
||||
const double qmax = i8 ? 127.0 : 448.0;
|
||||
worst_scale = std::max(worst_scale, std::fabs(sc - amax / qmax) / (amax / qmax + 1e-30));
|
||||
for (int k = 0; k < G; ++k) {
|
||||
const double deq = (double)rad_load_f32(hq.data(), RAD_F8E4M3, i * parts * K + p * K + g * G + k) * sc;
|
||||
const double deq = (double)rad_load_f32(hq.data(), i8 ? RAD_I8 : RAD_F8E4M3, i * parts * K + p * K + g * G + k) * sc;
|
||||
worst_val = std::max(worst_val, std::fabs(deq - b[(size_t)(g * G + k)]) / (amax + 1e-30));
|
||||
}
|
||||
}
|
||||
}
|
||||
char d[96];
|
||||
std::snprintf(d, sizeof d, "scale rel %.2e, value/amax %.3f", worst_scale, worst_val);
|
||||
ok(nm, rot_only ? worst_val < 1e-3 : (worst_scale < 1e-4 && worst_val < 0.07), d);
|
||||
ok(nm, rot_only ? worst_val < 1e-3 : (worst_scale < 1e-4 && worst_val < (i8 ? 0.01 : 0.07)), d);
|
||||
}
|
||||
|
||||
|
||||
@@ -3956,6 +3957,226 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int
|
||||
}
|
||||
|
||||
|
||||
/* TIMING OF THE 5/4-BIT GEMM AT ONE PREFILL CHUNK, over the row's tuned axes. `--case w5a16_perf`
|
||||
* only (it is a measurement, not a check). One weight buffer is replicated past the last-level cache
|
||||
* and walked in turn, as perf_row does, so the GB/s printed is the weight stream's. */
|
||||
static void w5a16_perf(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t K) {
|
||||
if (!g_case || std::strcmp(g_case, "w5a16_perf")) return;
|
||||
const char* row = bits == 5 ? "gemm_w5a16_nt_m64" : "gemm_w4a16_asym_nt_m64";
|
||||
const RadKernelInfo* r = find(pl, row);
|
||||
if (!r) { std::printf("row absent\n"); return; }
|
||||
const int64_t G = K / 128;
|
||||
Planes p;
|
||||
p.enc = rad_enc_affine(bits == 5 ? RAD_U5 : RAD_U4, RAD_F16, 1, 128);
|
||||
rad_enc_add_plane(&p.enc, "zero", RAD_U8, 1, 128);
|
||||
p.size(N, K);
|
||||
for (auto& d : p.data[0]) d = (uint8_t)st_mix32((uint32_t)(&d - p.data[0].data()));
|
||||
for (int64_t i = 0; i < N * G; ++i) {
|
||||
rad_store_f32(p.data[1].data(), RAD_F16, i, 0.01f);
|
||||
p.data[2][(size_t)i] = 8;
|
||||
}
|
||||
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 128),
|
||||
PS("dtype", bits == 5 ? "w5a16" : "w4a16_asym") };
|
||||
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("relayout failed\n");
|
||||
return;
|
||||
}
|
||||
const size_t copies = std::max<size_t>(2, (size_t)(256u << 20) / wq.size() + 1);
|
||||
void* d_w = dalloc(wq.size() * copies);
|
||||
void* d_s = dalloc(ws.size() * copies);
|
||||
void* d_a = dalloc((size_t)(M * K * 2));
|
||||
void* d_y = dalloc((size_t)(M * N * 2));
|
||||
if (!d_w || !d_s || !d_a || !d_y) { std::printf("hipMalloc failed\n"); return; }
|
||||
for (size_t c = 0; c < copies; ++c) {
|
||||
st_memcpy((uint8_t*)d_w + c * wq.size(), wq.data(), wq.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy((uint8_t*)d_s + c * ws.size(), ws.data(), ws.size(), hipMemcpyHostToDevice);
|
||||
}
|
||||
std::printf("%s M=%lld N=%lld K=%lld (weight %.1f MiB)\n", row, (long long)M, (long long)N,
|
||||
(long long)K, wq.size() / 1048576.0);
|
||||
struct Cfg { int wv, sk, mb, npw, nt; };
|
||||
std::vector<Cfg> cfgs;
|
||||
for (int sk : { 0, 1, 2, 4, 5, 8 })
|
||||
for (int mb : { 0, 1, 2, 4 })
|
||||
for (int npw : { 1, 4 })
|
||||
for (int wv : { 1, 2, 4 }) cfgs.push_back({ wv, sk, mb, npw, 1 });
|
||||
double best = 1e30;
|
||||
Cfg bc{};
|
||||
for (const Cfg& c : cfgs) {
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_a, RAD_F16, { M, K }), T(nullptr, RAD_F32, { 1 }), T(d_w, RAD_U8, { (int64_t)wq.size() }),
|
||||
T(d_s, 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[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 == 5 ? "w5a16" : "w4a16_asym"),
|
||||
PI("wv", c.wv), PI("sk", c.sk), PI("mb", c.mb), PI("npw", c.npw),
|
||||
PI("nt", c.nt) };
|
||||
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->launch(&a, nullptr) != RAD_OK || st_stream_sync(nullptr) != hipSuccess) continue;
|
||||
hipEvent_t e0, e1;
|
||||
st_ev_create(&e0); st_ev_create(&e1);
|
||||
st_ev_record(e0, nullptr);
|
||||
const int iters = 12;
|
||||
for (int i = 0; i < iters; ++i) {
|
||||
ts[2].data = (uint8_t*)d_w + (size_t)(i % copies) * wq.size();
|
||||
ts[3].data = (uint8_t*)d_s + (size_t)(i % copies) * ws.size();
|
||||
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);
|
||||
const double us = (double)ms * 1000.0 / iters;
|
||||
const double tf = 2.0 * M * N * K / (us * 1e6);
|
||||
std::printf(" wv=%d sk=%d mb=%d npw=%d %9.1f us %6.1f TF/s %6.1f GB/s\n", c.wv, c.sk, c.mb,
|
||||
c.npw, us, tf, (double)wq.size() / (us * 1e3));
|
||||
if (us < best) { best = us; bc = c; }
|
||||
}
|
||||
std::printf("best wv=%d sk=%d mb=%d npw=%d: %.1f us\n", bc.wv, bc.sk, bc.mb, bc.npw, best);
|
||||
}
|
||||
|
||||
|
||||
/* THE 4/5-BIT ASYMMETRIC x INT8 PREFILL GEMM against what the planes mean: u4/u5 codes, an f16 scale and
|
||||
* a u8 zero a 128 of K, an int8 activation with an f32 scale a (row, 128), parts side by side. */
|
||||
static void wpa8i8_numeric(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t K, int64_t pb1 = 0,
|
||||
int64_t pb2 = 0) {
|
||||
const char* row = bits == 4 ? "gemm_w4a8i8_tiled" : "gemm_w5a8i8_tiled";
|
||||
if (!sel(row)) return;
|
||||
const int64_t e1 = pb1 > 0 ? std::min(pb1, N) : N;
|
||||
const int64_t e2 = pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N;
|
||||
const int parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
|
||||
auto part_of = [&](int64_t n) { return (int)(n >= e1) + (int)(n >= e2); };
|
||||
char nm[160];
|
||||
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d", row, (long long)M,
|
||||
(long long)N, (long long)K, parts);
|
||||
const RadKernelInfo* r = find(pl, row);
|
||||
if (!r || !r->layout || !r->relayout) { ok(nm, false, "row or hooks absent"); return; }
|
||||
const int64_t G = K / 128;
|
||||
Planes p;
|
||||
p.enc = rad_enc_affine(bits == 4 ? RAD_U4 : RAD_U5, RAD_F16, 1, 128);
|
||||
rad_enc_add_plane(&p.enc, "zero", RAD_U8, 1, 128);
|
||||
p.size(N, K);
|
||||
const int ic = p.find("codes"), is = p.find("scale"), iz = p.find("zero");
|
||||
const int64_t rb = p.row_bytes(ic);
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
for (int64_t k = 0; k < K; ++k) {
|
||||
const uint32_t q = st_mix32((uint32_t)(n * K + k) ^ 0x5eedu) & ((1u << bits) - 1);
|
||||
const int64_t bit = k * bits;
|
||||
uint8_t* b = p.data[(size_t)ic].data() + n * rb + (bit >> 3);
|
||||
const uint32_t v = q << (bit & 7);
|
||||
b[0] |= (uint8_t)(v & 0xFF);
|
||||
if ((bit & 7) > 3) b[1] |= (uint8_t)(v >> 8);
|
||||
}
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
const uint32_t h = st_mix32((uint32_t)(n * G + g) ^ 0x2545u);
|
||||
rad_store_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g, 0.002f * (float)(1 + h % 40));
|
||||
p.data[(size_t)iz][(size_t)(n * G + g)] = (uint8_t)((h >> 8) % (1u << bits));
|
||||
}
|
||||
}
|
||||
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;
|
||||
const int rw = stored(r, lp, 2, p, { "codes" }, &wq);
|
||||
const int rs = stored(r, lp, 3, p, { "scale", "zero" }, &ws);
|
||||
if (rw != RAD_OK || rs != RAD_OK) {
|
||||
ok(nm, false, "relayout rc " + std::to_string(rw) + "/" + std::to_string(rs));
|
||||
return;
|
||||
}
|
||||
const int64_t PK = parts * K, PG = parts * G;
|
||||
std::vector<uint8_t> ha((size_t)(M * PK));
|
||||
std::vector<float> av((size_t)(M * PK)), 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) {
|
||||
const int q8 = (int)(st_mix32((uint32_t)(i * PK + k) ^ 0x3cu) % 255u) - 127;
|
||||
ha[(size_t)(i * PK + k)] = (uint8_t)(int8_t)q8;
|
||||
av[(size_t)(i * PK + k)] = (float)q8;
|
||||
}
|
||||
}
|
||||
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) { ok(nm, false, "hipMalloc failed"); 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));
|
||||
if (pb2) ps.push_back(PI("pb2", pb2));
|
||||
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; }
|
||||
}
|
||||
const int rc = r->launch(&a, (RadStream) nullptr);
|
||||
if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) { ok(nm, false, "rc=" + std::to_string(rc)); return; }
|
||||
st_memcpy(hy.data(), d_y, hy.size() * 2, hipMemcpyDeviceToHost);
|
||||
const int64_t nstep = N > 96 ? N / 96 : 1;
|
||||
double num = 0, den = 0;
|
||||
for (int64_t i = 0; i < M; i += (M > 64 ? 3 : 1))
|
||||
for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) {
|
||||
const int pt = part_of(n);
|
||||
double acc = 0;
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
double grp = 0;
|
||||
const double z = p.data[(size_t)iz][(size_t)(n * G + g)];
|
||||
const double sc = rad_load_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g);
|
||||
for (int64_t k = g * 128; k < (g + 1) * 128; ++k) {
|
||||
const int64_t bit = k * bits;
|
||||
const uint8_t* rowp = p.data[(size_t)ic].data() + n * rb;
|
||||
uint32_t v = rowp[bit >> 3];
|
||||
if ((bit & 7) > 3) v |= (uint32_t)rowp[(bit >> 3) + 1] << 8;
|
||||
const double q = (v >> (bit & 7)) & ((1u << bits) - 1);
|
||||
grp += (double)av[(size_t)(i * PK + pt * K + k)] * (q - z);
|
||||
}
|
||||
acc += grp * sc * (double)as[(size_t)(i * PG + pt * G + g)];
|
||||
}
|
||||
const double d = (double)h_bf16(hy[(size_t)(i * N + n)]) - acc;
|
||||
num += d * d;
|
||||
den += acc * acc;
|
||||
}
|
||||
const double rel = den > 0 ? std::sqrt(num / den) : std::sqrt(num);
|
||||
char d[96];
|
||||
std::snprintf(d, sizeof d, "rel_l2 %.3e", rel);
|
||||
if (M >= 1024) { // a prefill chunk: say how fast it ran
|
||||
hipEvent_t e0, e1;
|
||||
st_ev_create(&e0); st_ev_create(&e1);
|
||||
st_ev_record(e0, nullptr);
|
||||
for (int i = 0; i < 10; ++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);
|
||||
std::snprintf(d, sizeof d, "rel_l2 %.3e, %.0f us, %.1f TF/s", rel, ms * 100.0,
|
||||
2.0 * M * N * K / ((double)ms * 100.0 * 1e6));
|
||||
std::printf(" %s\n", d);
|
||||
}
|
||||
ok(nm, rel < 1e-2, d);
|
||||
}
|
||||
|
||||
|
||||
/* 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
|
||||
* weights, activations and output and a DISJOINT slice of one scratch allocation, with a split
|
||||
@@ -12895,6 +13116,24 @@ static void device_phase(const Plugin& pl) {
|
||||
moe_w4_rot(pl);
|
||||
}
|
||||
|
||||
if (g_case && !std::strcmp(g_case, "w5a16_perf")) {
|
||||
w5a16_perf(pl, 4, 64, 17408, 5120);
|
||||
w5a16_perf(pl, 4, 64, 5120, 17408);
|
||||
w5a16_perf(pl, 4, 16, 17408, 5120);
|
||||
}
|
||||
for (int bits : { 4, 5 }) {
|
||||
const char* row = bits == 4 ? "gemm_w4a8i8_tiled" : "gemm_w5a8i8_tiled";
|
||||
if (!sel(row)) continue;
|
||||
if (!g_case) std::printf("\nParoQuant int%d prefill GEMM (int8 WMMA), against the planes' meaning\n", bits);
|
||||
wpa8i8_numeric(pl, bits, 300, 400, 640);
|
||||
wpa8i8_numeric(pl, bits, 70, 272, 1280);
|
||||
wpa8i8_numeric(pl, bits, 300, 400, 640, 192);
|
||||
wpa8i8_numeric(pl, bits, 600, 512, 2560, 128, 384);
|
||||
wpa8i8_numeric(pl, bits, 2100, 34816, 5120, 17408);
|
||||
wpa8i8_numeric(pl, bits, 2048, 17408, 5120);
|
||||
wpa8i8_numeric(pl, bits, 2048, 5120, 17408);
|
||||
}
|
||||
|
||||
if (sel("gemm_w5a16_nt_m64")) {
|
||||
if (!g_case) std::printf("\nParoQuant's 5-bit GEMM, against the planes' meaning\n");
|
||||
w5a16_numeric(pl, 8, 272, 640);
|
||||
@@ -12913,6 +13152,14 @@ static void device_phase(const Plugin& pl) {
|
||||
givens_numeric(pl, 300, 6144, 2, 8, true);
|
||||
}
|
||||
|
||||
if (sel("givens_quant_act_i8g")) {
|
||||
if (!g_case) std::printf("\nParoQuant's rotated activation, int8 output, against libm's rotation\n");
|
||||
givens_numeric(pl, 1, 128, 1, 8, false, true);
|
||||
givens_numeric(pl, 3, 5120, 1, 8, false, true);
|
||||
givens_numeric(pl, 5, 5120, 2, 8, false, true);
|
||||
givens_numeric(pl, 64, 17408, 1, 8, false, true);
|
||||
givens_numeric(pl, 300, 6144, 2, 8, false, true);
|
||||
}
|
||||
if (sel("givens_quant_act_fp8")) {
|
||||
if (!g_case) std::printf("\nParoQuant's rotate-and-quantise, against an f64 rotation\n");
|
||||
givens_numeric(pl, 1, 128, 1, 8);
|
||||
|
||||
Reference in new issue
Block a user