libref, libavx mla_attn: an optional kv_dtype, the cache shaped from it

Both schemas take `kv_dtype` beside `dtype`; libref's shape gives the cache operand that dtype when
it is named, the query's otherwise. The kernels already read the cache through ldt, so an E4M3
cache is read as its codes' values. ref_test covers an E4M3 cache under a bf16 query.
This commit is contained in:
AnimusNull authored and AnimusNull committed 2026-11-05 14:10:58 +00:00
1 parent 17f0eae420
commit e494369ed9
4 files changed
+36 -3

No files matched your search

+2 -1
View File
@@ -166,8 +166,9 @@ static const RadOperandSpec oLiliWalk[] = { OPD("edges"), OPD("aout"), OPD("cand
static const RadParamSpec pSwigluClamped[] = { P_INT("M"), P_INT("n"), P_F64("limit"), P_STR("dtype") };
static const RadParamSpec pDsaPool[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_STR("dtype"), P_INTO("block_size") };
/* `kv_dtype`: the latent cache's dtype when it is not the query's (fp8_e4m3, at descale 1). */
static const RadParamSpec pMlaAttn[] = { P_INT("M"), P_INT("latent_dim"), P_INT("heads"),
P_INT("blocks"), P_INT("block_size"), P_F64("scale"), P_STR("dtype") };
P_INT("blocks"), P_INT("block_size"), P_F64("scale"), P_STR("dtype"), P_STRO("kv_dtype") };
static const RadParamSpec pMlaProject[] = { P_INT("M"), P_INT("heads"), P_INT("head_dim"),
P_INT("latent_dim"), P_STR("mode"), P_STR("dtype") };
static const RadOperandSpec oMlaAttn[] = { OPD("q"), OPD("kv"), OPD("bt"), OPD("length"), OUT("out") };
+2 -1
View File
@@ -197,8 +197,9 @@ static const RadOperandSpec oLiliWalk[] = { OPD("edges"), OPD("aout"), OPD("cand
static const RadParamSpec pSwigluClamped[] = { P_INT("M"), P_INT("n"), P_F64("limit"), P_STR("dtype") };
static const RadParamSpec pDsaPool[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_STR("dtype"), P_INTO("block_size") };
/* `kv_dtype`: the latent cache's dtype when it is not the query's (fp8_e4m3, at descale 1). */
static const RadParamSpec pMlaAttn[] = { P_INT("M"), P_INT("latent_dim"), P_INT("heads"),
P_INT("blocks"), P_INT("block_size"), P_F64("scale"), P_STR("dtype") };
P_INT("blocks"), P_INT("block_size"), P_F64("scale"), P_STR("dtype"), P_STRO("kv_dtype") };
static const RadParamSpec pMlaProject[] = { P_INT("M"), P_INT("heads"), P_INT("head_dim"),
P_INT("latent_dim"), P_STR("mode"), P_STR("dtype") };
static const RadOperandSpec oMlaAttn[] = { OPD("q"), OPD("kv"), OPD("bt"), OPD("length"), OUT("out") };
+4 -1
View File
@@ -929,9 +929,12 @@ int ref_mla_attn_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
pages = std::max((int64_t)2, B * M);
if (M < 1 || D < 1 || D > 2048 || H < 1 || B < 1 || bs < 1) return RAD_E_UNSUPPORTED;
const uint32_t dtype = act_dt(p, n_p);
/* The cache is the query's dtype unless `kv_dtype` names another (fp8_e4m3). */
const char* kvs = gs(p, n_p, "kv_dtype");
const uint32_t kv = kvs && dt_parse(kvs) != (uint32_t)RAD_DT_INVALID ? dt_parse(kvs) : dtype;
Opd o;
o.t(dtype, { M, H, D });
o.t(dtype, { pages, 1, bs, D });
o.t(kv, { pages, 1, bs, D });
o.t_idx({ M, B }, pages, -1, 0);
o.t_idx({ M }, 0, B * bs, 0);
o.t(dtype, { M, H, D });
+28
View File
@@ -597,6 +597,34 @@ TEST(mla_attention_reads_one_latent_and_masks_the_last_selected_page) {
CHECK_EQ(out.at(1), 0);
}
/* An E4M3 cache (kv_dtype fp8_e4m3, kv_store_fp8 at descale 1) is read as the codes' values: the
* first case above with the latent as E4M3 codes and a bf16 query and output. */
TEST(mla_attention_reads_an_e4m3_latent_cache_as_its_values) {
T q(RAD_BF16, { 1, 1, 2 }), kv(RAD_F8E4M3, { 2, 1, 2, 2 }), bt(RAD_I32, { 1, 2 });
T len(RAD_I32, { 1 }), out(RAD_BF16, { 1, 1, 2 });
q.f({ 0, 0 });
kv.f({ 3, 5, 448, -448, 1, 0, 0, 1 });
bt.n({ 1, 0 });
len.n({ 3 });
A a;
a.t(q)
.t(kv)
.t(bt)
.t(len)
.t(out)
.i("M", 7)
.i("latent_dim", 2)
.i("heads", 1)
.i("blocks", 2)
.i("block_size", 2)
.d("scale", 1)
.s("dtype", "bf16")
.s("kv_dtype", "fp8_e4m3");
REQUIRE_EQ(run("mla_attn", a), RAD_OK);
CHECK_NEAR(out.at(0), 4.0 / 3, 1e-2);
CHECK_EQ(out.at(1), 2);
}
TEST(dsa_score_relu_is_per_head_before_signed_weights_and_masks_invalid_history) {
T q(RAD_F8E4M3, { 2, 2, 2 }), qs(RAD_F32, { 2, 2 }), gate(RAD_F32, { 2, 2 });
T k(RAD_F8E4M3, { 3, 2 }), ks(RAD_F32, { 3 }), bt(RAD_I32, { 2, 3 });