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_fp8f32.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

111 lines
4.4 KiB
C++

/* tests/r4d_emu: the dense E4M3 GEMM with F32 block scales (r4d_fp8_f32scale.hip) on the host SIMT
* emulator. The decode and row-tile forms (RADIANCE_FP8F32_FORM, each R x S x C and row count) must
* match the legacy 16x64 tile
* bit for bit: the same fragments, the same mma chain per 128-K block and the same fold order.
* Ragged row counts, pitched rows and a K that leaves the decode form's last wave group partly
* idle are covered; the output's pitch padding stays untouched. */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
#include "ref_host.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <vector>
static RadTensor mat(void* p, uint32_t dt, int64_t rows, int64_t cols, int64_t ld) {
RadTensor t{};
t.data = p;
t.dtype = dt;
t.rank = 2;
t.shape[0] = rows;
t.shape[1] = cols;
t.stride[0] = ld;
t.stride[1] = 1;
return t;
}
int main() {
std::mt19937 rng(31);
std::normal_distribution<float> nd(0.f, 1.f);
struct Shape {
int M, N, K;
};
const Shape shapes[] = { { 1, 256, 512 }, { 6, 128, 1536 }, { 17, 256, 512 },
{ 48, 128, 512 }, { 70, 128, 384 }, { 130, 128, 256 },
{ 3, 128, 4096 } };
const char* forms[] = { "decode", "decode1x4", "decode1x32", "decode2x8",
"decode3x16", "decode1x8x2", "decode1x4x4", "decode2x8x2",
"decode3x4x2", "decode2x4x4", "rows1", "rows2",
"rows4", nullptr };
int fails = 0, cases = 0;
for (const Shape& sh : shapes) {
const int M = sh.M, N = sh.N, K = sh.K, kb = K / 128, ap = K + 16, yp = N + 8;
std::vector<uint8_t> a((size_t)M * ap), w((size_t)N * K);
for (auto& v : a) v = rad_f32_to_fp8e4m3(nd(rng));
for (auto& v : w) v = rad_f32_to_fp8e4m3(nd(rng) * 0.5f);
std::vector<float> as((size_t)M * kb), ws((size_t)(N / 128) * kb);
// Scales over twelve octaves: K blocks of very different weight cancel, so a changed
// summation order shows through the bf16 output.
std::uniform_real_distribution<float> oct(-6.f, 6.f);
for (auto& v : as) v = std::exp2(oct(rng));
for (auto& v : ws) v = std::exp2(oct(rng)) * 0.01f;
RadTensor t[7] = { mat(a.data(), RAD_F8E4M3, M, K, ap),
mat(as.data(), RAD_F32, M, kb, kb),
mat(w.data(), RAD_F8E4M3, N, K, K),
mat(ws.data(), RAD_F32, N / 128, kb, kb),
RadTensor{},
RadTensor{},
RadTensor{} };
RadParam p[5]{};
p[0] = { "M", RAD_P_INT, M };
p[1] = { "N", RAD_P_INT, N };
p[2] = { "K", RAD_P_INT, K };
p[3] = { "group", RAD_P_INT, 128 };
p[4].key = "dtype";
p[4].kind = RAD_P_STR;
p[4].sval = "fp8a8f32";
auto run = [&](const char* form, std::vector<uint16_t>& y) {
y.assign((size_t)M * yp, 0x7777);
t[4] = mat(y.data(), RAD_BF16, M, N, yp);
setenv("RADIANCE_FP8F32_FORM", form, 1);
RadArgs ar{};
ar.t = t;
ar.n_t = 7;
ar.p = p;
ar.n_p = 5;
return r4d_rad_fp8_f32scale(&ar, nullptr);
};
std::vector<uint16_t> legacy, got;
++cases;
if (run("legacy", legacy) != RAD_OK) {
std::printf("FAIL legacy M=%d N=%d K=%d\n", M, N, K);
++fails;
continue;
}
int nonzero = 0;
for (int r = 0; r < M; ++r)
for (int c = 0; c < N; ++c) nonzero += legacy[(size_t)r * yp + c] != 0;
if (nonzero < M * N / 2) {
std::printf("FAIL legacy output mostly zero at M=%d\n", M);
++fails;
}
for (const char* const* f = forms; *f; ++f) {
++cases;
const int rc = run(*f, got);
if (rc != RAD_OK || got != legacy) {
size_t i = 0;
while (rc == RAD_OK && i < got.size() && got[i] == legacy[i]) ++i;
std::printf("FAIL form %s at M=%d N=%d K=%d (rc %d, first word %zu)\n", *f, M, N, K,
rc, i);
++fails;
}
}
}
unsetenv("RADIANCE_FP8F32_FORM");
std::printf("%d case(s), %d failure(s)\n", cases, fails);
return fails ? 1 : 0;
}