Archived
r4d mla_attn: E4M3 latent cache twins of the three rows
mla_attn takes an optional `kv_dtype`; at fp8_e4m3 the cache is E4M3 as kv_store_fp8 writes it at its default descale of 1. mla_attn_fp8kv, mla_attn_matrix_fp8kv and mla_attn_rows_fp8kv are the bf16 rows' kernels with the cache element as a template parameter: the key load decodes E4M3 to bf16 (v_cvt_pk_f32_fp8, then the high half), which is exact since every E4M3 value is a bf16, so the tiles and all arithmetic past the load are the bf16 instance's. The twins carry the bf16 rows' constraints plus kv_dtype fp8_e4m3 and rank above all three bf16 rows, in their order. At uint16_t the loads are the plain loads the bf16 kernels made; their launchers still refuse any cache that is not bf16. r4d_emu test_mla runs every kernel form (wave, matrix and its element load, rows forms 0-3) on E4M3 codes against its bf16 instance on the decoded cache: bit-identical on all 23 form/case pairs, and each launcher refuses the other cache dtype.
This commit is contained in:
1 parent
b632c72f5d
commit
17f0eae420
6 files changed
+249
-24
No files matched your search
@@ -14,6 +14,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -787,6 +788,99 @@ int run_paged_verify(int heads, int context, std::mt19937& rng) {
|
||||
return fails;
|
||||
}
|
||||
|
||||
/* The E4M3-cache twins (r4d_rad_mla_attn*_fp8kv): every E4M3 value is a bf16, so each kernel form
|
||||
* on a cache of codes must give the bytes its bf16 instance gives on the same cache decoded to
|
||||
* bf16. Each launcher also refuses the other cache dtype. */
|
||||
int run_fp8kv(const Case& c, std::mt19937& rng) {
|
||||
const int M = c.rows, H = c.heads, B = c.blocks;
|
||||
std::vector<int> bt, len;
|
||||
const int pages = fill_tables(c, rng, bt, len);
|
||||
std::normal_distribution<float> nrm(0.f, 1.f);
|
||||
std::vector<uint16_t> qb((size_t)M * H * D), kvb((size_t)pages * BS * D);
|
||||
std::vector<uint8_t> kv8(kvb.size());
|
||||
for (auto& v : qb) v = rad_f32_to_bf16(0.25f * nrm(rng));
|
||||
for (size_t i = 0; i < kv8.size(); ++i) {
|
||||
/* Codes over most exponents, subnormals included. */
|
||||
const float x = nrm(rng);
|
||||
kv8[i] = rad_f32_to_fp8e4m3(x * x * x * 4.0f);
|
||||
kvb[i] = rad_f32_to_bf16(rad_fp8e4m3_to_f32(kv8[i]));
|
||||
if (rad_bf16_to_f32(kvb[i]) != rad_fp8e4m3_to_f32(kv8[i])) {
|
||||
std::printf("%-40s E4M3 code %u is not a bf16 FAIL\n", c.name.c_str(), kv8[i]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
std::vector<char> scratch;
|
||||
typedef int (*Launch)(const RadArgs*, RadStream);
|
||||
auto launch = [&](Launch fn, bool fp8, std::vector<uint16_t>& out) {
|
||||
RadTensor t[5] = { tens(qb.data(), RAD_BF16, { M, H, D }),
|
||||
fp8 ? tens(kv8.data(), RAD_F8E4M3, { pages, 1, BS, D })
|
||||
: tens(kvb.data(), RAD_BF16, { pages, 1, BS, D }),
|
||||
tens(bt.data(), RAD_I32, { M, B }), tens(len.data(), RAD_I32, { M }),
|
||||
tens(out.data(), RAD_BF16, { M, H, D }) };
|
||||
RadParam p[6]{};
|
||||
const char* keys[5] = { "M", "latent_dim", "heads", "blocks", "block_size" };
|
||||
const long long vals[5] = { M, D, H, B, BS };
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
p[i].key = keys[i];
|
||||
p[i].kind = RAD_P_INT;
|
||||
p[i].ival = vals[i];
|
||||
}
|
||||
p[5].key = "scale";
|
||||
p[5].kind = RAD_P_F64;
|
||||
p[5].dval = 0.0625;
|
||||
RadArgs a{};
|
||||
a.t = t;
|
||||
a.n_t = 5;
|
||||
a.p = p;
|
||||
a.n_p = 6;
|
||||
if (c.scratch) {
|
||||
const int64_t need = r4d_rad_mla_attn_matrix_scratch(&a);
|
||||
scratch.assign((size_t)need + 16, 0);
|
||||
a.scratch = scratch.data() + ((16 - ((uintptr_t)scratch.data() & 15)) & 15);
|
||||
a.scratch_bytes = need;
|
||||
}
|
||||
return fn(&a, nullptr);
|
||||
};
|
||||
struct Form {
|
||||
const char* name;
|
||||
Launch bf16, fp8;
|
||||
void (*pin)(int);
|
||||
int form;
|
||||
};
|
||||
std::vector<Form> forms = { { "wave", r4d_rad_mla_attn, r4d_rad_mla_attn_fp8kv, nullptr, 0 } };
|
||||
if (H % 8 == 0) {
|
||||
forms.push_back({ "matrix", r4d_rad_mla_attn_matrix, r4d_rad_mla_attn_matrix_fp8kv,
|
||||
r4d_mla_matrix_pin_form, 0 });
|
||||
forms.push_back({ "matrix, element load", r4d_rad_mla_attn_matrix,
|
||||
r4d_rad_mla_attn_matrix_fp8kv, r4d_mla_matrix_pin_form, 1 });
|
||||
}
|
||||
if (H % 32 == 0)
|
||||
for (int form : { 0, 3, 1, 2 })
|
||||
forms.push_back({ "rows", r4d_rad_mla_attn_rows, r4d_rad_mla_attn_rows_fp8kv,
|
||||
r4d_mla_rows_pin_form, form });
|
||||
int fails = 0;
|
||||
for (const Form& f : forms) {
|
||||
std::vector<uint16_t> want((size_t)M * H * D, 0xffff), got(want.size(), 0xffff);
|
||||
if (f.pin) f.pin(f.form);
|
||||
const int rb = launch(f.bf16, false, want), rf = launch(f.fp8, true, got);
|
||||
std::vector<uint16_t> spare(want.size(), 0xffff);
|
||||
const int refuse_b = launch(f.bf16, true, spare), refuse_f = launch(f.fp8, false, spare);
|
||||
if (f.pin) f.pin(0);
|
||||
size_t differ = 0;
|
||||
for (size_t i = 0; i < got.size(); ++i) differ += got[i] != want[i];
|
||||
const bool untouched = std::all_of(spare.begin(), spare.end(),
|
||||
[](uint16_t v) { return v == 0xffff; });
|
||||
const bool ok = rb == RAD_OK && rf == RAD_OK && !differ && refuse_b == RAD_E_DTYPE &&
|
||||
refuse_f == RAD_E_DTYPE && untouched;
|
||||
std::printf("%-40s fp8kv %-20s form %d rc %d/%d %zu differ from bf16, refusals %d/%d "
|
||||
"%s\n",
|
||||
c.name.c_str(), f.name, f.form, rb, rf, differ, refuse_b, refuse_f,
|
||||
ok ? "PASS" : "FAIL");
|
||||
fails += !ok;
|
||||
}
|
||||
return fails;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
@@ -839,6 +933,19 @@ int main(int argc, char** argv) {
|
||||
std::mt19937 rng(20261020u + (unsigned)i);
|
||||
fails += run_rows(rows_cases[i], rng);
|
||||
}
|
||||
/* The E4M3-cache twins against their bf16 instances. */
|
||||
std::vector<Case> fp8_cases = {
|
||||
{ "fp8kv: ragged, holes and bad pages", 7, 8, 17, Fill::ragged },
|
||||
{ "fp8kv: prefill, 16 heads, split", 4, 16, 33, Fill::prefill, 0, 0, true },
|
||||
{ "fp8kv: topk d=4, 8 heads", 6, 8, 33, Fill::topk, 4, 400 },
|
||||
{ "fp8kv: rows, ragged", 3, 64, 17, Fill::ragged },
|
||||
{ "fp8kv: rows, topk d=4, split", 2, 64, 33, Fill::topk, 4, 400, true },
|
||||
};
|
||||
for (size_t i = 0; i < fp8_cases.size(); ++i) {
|
||||
if (filter && fp8_cases[i].name.find(filter) == std::string::npos) continue;
|
||||
std::mt19937 rng(20261028u + (unsigned)i);
|
||||
fails += run_fp8kv(fp8_cases[i], rng);
|
||||
}
|
||||
if (!filter || std::strstr("rowwise", filter)) {
|
||||
std::mt19937 rng(20261022u);
|
||||
fails += run_rowwise(rng);
|
||||
|
||||
Reference in new issue
Block a user