From e494369ed9aab6376b81d12afe162ad8fb624df0 Mon Sep 17 00:00:00 2001 From: AnimusNull Date: Wed, 28 Oct 2026 04:04:37 +0000 Subject: [PATCH] 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. --- libavx/avx_registry.cpp | 3 ++- libref/ref_registry.cpp | 3 ++- libref/ref_shapes.cpp | 5 ++++- tests/ref_test.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/libavx/avx_registry.cpp b/libavx/avx_registry.cpp index b0388cb0..1dbd2e55 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -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") }; diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index 3c90c101..e7a05c23 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -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") }; diff --git a/libref/ref_shapes.cpp b/libref/ref_shapes.cpp index c7720727..f1284d26 100644 --- a/libref/ref_shapes.cpp +++ b/libref/ref_shapes.cpp @@ -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 }); diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index decf28af..0ce0088d 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -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 });