This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/tests/r4d_emu/test_kda.cpp
T
NunzeandJohn McDonough 78c58841bf libr4d: GLM-5.3's kernels -- mHC, KDA, the DSA indexer, MLA, f32-scaled FP8, MXFP4 experts
The device rows for GLM-5.3-Flash's ops, at its geometry and at TP4 and TP8 slices:

- mhc_pre / mhc_post / mhc_contract, and mhc_pre_partial / mhc_pre_finish, which run mhc_pre's
  own projection and finishing kernels with its chunk-sum order, so the pair is byte-identical
  to mhc_pre at n 4096, hc 4, chunk 2048.
- kda_update_k128_v128_bf16, one, two or four value rows a wave, both state forms.
- dsa_pool_fp8, dsa_query_fp8 and dsa_score_fp8 (fp8 WMMA, 32 heads of 128).
- mla_attn: a matrix row whose WMMA tile holds a query's heads, or pairs eight heads of two
  neighbouring queries over the union of their page tables, above the wave row; mla_project
  with up to eight rows a thread or wave.
- gemm_nt_f32 and router_sigmoid_topk in tiled and one-wave-a-row forms; gemm_fp8a8_f32scale
  and moe_gemm_fp8a8_f32scale read f32 block scales in place; moe_gemm_mxfp4a8 decodes e2m1 into
  exact E4M3 bytes for fp8 WMMA, with an expert-major decode form and a wide prefill form.
- swiglu_clamped, and the device-only fusions of GLM's MoE block: swiglu_clamped_quant_fp8,
  router_sigmoid_topk_scatter, and moe_scatter_inv / moe_gather_inv, which pass the gather's slot
  table from the sort instead of rebuilding it.

moe_gather takes f32 routing weights in its column-per-thread kernel. The sort and the gather
kernels move from r4d_moe.hip into r4d_moe_sort.h and r4d_moe_gather.h, so the fused router and
gather instantiate the same code.

Every form that replaces another gives its bytes: tests/r4d_emu runs the kernels on the host
(test_f32, test_fp8f32, test_kda, test_mhc, test_mla, test_moefuse, test_moemx), r4d_selftest
compares them on a card against libref and against each other, and plugin_test pins the
mla_attn band. docs/OPS.md states what libr4d serves.

Co-Authored-By: John McDonough <18016200+JohnCMcDonough@users.noreply.github.com>
2026-10-19 20:13:47 +02:00

188 lines
6.8 KiB
C++

/* tests/r4d_emu: kda_update (r4d_kda.hip) on the host SIMT emulator. Every row and prefetch form
* must give the old one-row kernel's output and whole state buffer bit for bit, in all four modes:
* prefill per candidate and anchored, verify per candidate and anchored (after the replay of the
* accepted prefix). Cold starts, an unseated sequence, an empty sequence and an unwritten slot
* are mixed in. The GPU check of the same claim is r4d_selftest's kda_forms. */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <string>
#include <vector>
static RadTensor tens(void* p, uint32_t dt, std::vector<int64_t> shape) {
RadTensor t{};
t.data = p;
t.dtype = dt;
t.rank = (uint32_t)shape.size();
int64_t st = 1;
for (int i = (int)shape.size() - 1; i >= 0; --i) {
t.shape[i] = shape[i];
t.stride[i] = st;
st *= shape[i];
}
return t;
}
static uint16_t f2b(float f) {
uint32_t u;
std::memcpy(&u, &f, 4);
return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
}
struct Case {
const char* name;
const char* mode;
bool anchor;
int64_t width;
std::vector<int> cu, idx, accepted, init;
std::vector<std::pair<int64_t, float>> counts;
};
int main() {
std::mt19937 rng(41);
std::uniform_real_distribution<float> u(-1.f, 1.f);
const int64_t H = 2, D = 128, slots = 16, tokens = 9;
std::vector<float> state0((size_t)(slots * H * D * D)), alog((size_t)H), bias((size_t)(H * D));
for (auto& v : state0) v = u(rng);
for (auto& v : alog) v = 0.5f * u(rng);
for (auto& v : bias) v = u(rng);
std::vector<uint16_t> in[5];
for (int i = 0; i < 5; ++i) {
in[i].resize((size_t)(tokens * H * (i == 4 ? 1 : D)));
for (auto& v : in[i]) v = f2b((i >= 3 ? 2.f : 1.f) * u(rng));
}
const std::vector<Case> cases = {
{ "prefill, per candidate",
"prefill",
false,
1,
{ 0, 4, 4, 9 },
{ 3, 4, -1 },
{ 1, 1, 1 },
{ 0, 1, 1 },
{} },
{ "prefill, anchored",
"prefill",
true,
2,
{ 0, 5, 9 },
{ 1, 2, 3, 4 },
{ 1, 1 },
{ 1, 0 },
{} },
{ "verify, per candidate",
"verify",
false,
4,
{ 0, 4, 7, 9 },
{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -1 },
{ 3, 2, 1 },
{ 1, 0, 1 },
{} },
{ "verify, anchored",
"verify",
true,
4,
{ 0, 4, 7, 9 },
{ 1, 9, 5, 9, 2, 9, 6, 9, 3, 9, 7, 9 },
{ 3, 2, 1 },
{ 1, 1, 0 },
{ { 5, 4.f }, { 6, 3.f }, { 7, 0.f } } },
};
struct Form {
const char* rows;
const char* prefetch;
};
const Form forms[] = { { "1", "1" }, { "2", "1" }, { "4", "1" },
{ "1", "0" }, { "2", "0" }, { "4", "0" } };
int fails = 0, checks = 0;
for (const Case& c : cases) {
std::vector<float> start = state0;
for (const auto& sc : c.counts)
for (int64_t h = 0; h < H; ++h) start[(size_t)((sc.first * H + h) * D * D)] = sc.second;
const int64_t seqs = (int64_t)c.cu.size() - 1;
std::vector<int> idx = c.idx, cu = c.cu, acc = c.accepted, init = c.init;
auto run = [&](std::vector<uint16_t>& out, std::vector<float>& state) {
out.assign((size_t)(tokens * H * D), 0x7777);
state = start;
RadTensor t[13] = { tens(in[0].data(), RAD_BF16, { tokens, H, D }),
tens(in[1].data(), RAD_BF16, { tokens, H, D }),
tens(in[2].data(), RAD_BF16, { tokens, H, D }),
tens(in[3].data(), RAD_BF16, { tokens, H, D }),
tens(in[4].data(), RAD_BF16, { tokens, H }),
tens(alog.data(), RAD_F32, { H }),
tens(bias.data(), RAD_F32, { H * D }),
tens(state.data(), RAD_F32, { slots, H, D, D }),
tens(idx.data(), RAD_I32, { seqs, c.width }),
tens(cu.data(), RAD_I32, { seqs + 1 }),
tens(acc.data(), RAD_I32, { seqs }),
tens(init.data(), RAD_I32, { seqs }),
tens(out.data(), RAD_BF16, { tokens, H, D }) };
RadParam p[10]{};
p[0] = { "heads", RAD_P_INT, H };
p[1] = { "head_k", RAD_P_INT, D };
p[2] = { "head_v", RAD_P_INT, D };
p[3].key = "lower_bound";
p[3].kind = RAD_P_F64;
p[3].dval = -5;
p[4].key = "scale";
p[4].kind = RAD_P_F64;
p[4].dval = 1.0 / std::sqrt(128.0);
p[5].key = "l2_eps";
p[5].kind = RAD_P_F64;
p[5].dval = 1e-6;
p[6].key = "mode";
p[6].kind = RAD_P_STR;
p[6].sval = c.mode;
p[7].key = "state_form";
p[7].kind = RAD_P_STR;
p[7].sval = c.anchor ? "anchor" : "per_candidate";
p[8].key = "dtype";
p[8].kind = RAD_P_STR;
p[8].sval = "bf16";
RadArgs a{};
a.t = t;
a.n_t = 13;
a.p = p;
a.n_p = 9;
return r4d_rad_kda_update(&a, nullptr);
};
std::vector<uint16_t> ref_out, out;
std::vector<float> ref_state, state;
setenv("RADIANCE_KDA_FORM", "old", 1);
if (run(ref_out, ref_state) != RAD_OK) {
std::printf("FAIL %s: old form refused\n", c.name);
++fails;
continue;
}
unsetenv("RADIANCE_KDA_FORM");
bool written = false;
for (size_t i = 0; i < ref_out.size(); ++i) written = written || ref_out[i] != 0x7777;
if (!written) {
std::printf("FAIL %s: the old form wrote no output\n", c.name);
++fails;
}
for (const Form& f : forms) {
setenv("RADIANCE_KDA_ROWS", f.rows, 1);
setenv("RADIANCE_KDA_PREFETCH", f.prefetch, 1);
++checks;
const bool same = run(out, state) == RAD_OK && out == ref_out &&
std::memcmp(state.data(), ref_state.data(), state.size() * 4) == 0;
if (!same) {
++fails;
std::printf("FAIL %s, %s rows a wave, prefetch %s: differs from the old form\n",
c.name, f.rows, f.prefetch);
}
}
unsetenv("RADIANCE_KDA_ROWS");
unsetenv("RADIANCE_KDA_PREFETCH");
}
std::printf("kda_update forms against the old kernel: %d of %d bit-identical\n", checks - fails,
checks);
return fails ? 1 : 0;
}