radiance 1.9.0

New models:
- LTX-2.5 (base and distilled): video with a stereo soundtrack from text or from text and a first
  frame, on one card, with its duration head, tiled VAE decoding, the diffusion decoder and the x2
  latent upsampler
- Krea 2 Raw and Turbo, and FLUX.3 [klein] and its base: text to image, and for FLUX.3 edits from up
  to ten reference pictures, on one card
- GLM-5.3-Flash from AMD's Quark MXFP4 export, on eight cards (John McDonough, Nunze)
- Gemma 3 / TranslateGemma 27B with an optional DFlash2 drafter (mici81)

Engine:
- --kv-cache-dtype fp8_v4 and k6_v4 for Gemma 3 and Qwen3.8-Flash-Next (mici81)
- the rotated int8 trunk (i8a8h) for Qwen3.8-27B (eddie168168)
- eight-rank all-reduce bands, weighted shards and pieces (Nunze); --host-headroom-mib (sfjes)
- the dashboard shows the single-stream decode floor and its most recent window (Memoria)

Fixes: prefix-cache reservations kept until their save lands (baKon), tool calls in Responses
namespaces (#119), system messages merged when a template refuses several (#137), a prefill
continued with no open step (#127), calibration of a Gram accumulation (#122), JSON Schema patterns
read unanchored (davetha), GLM-style tool calls, the KV floor counting every sequence's state.
Plugin ABI 24.
This commit is contained in:
Deadcode committed 2026-10-19 21:58:07 +02:00
1 parent 778cbf315e
commit 685ec5bd85
136 files changed
+18917 -3821

No files matched your search

+38 -84
View File
@@ -1,24 +1,24 @@
/* tests/r4d_emu: gemm_nt_f32 (r4d_glm.hip) on the host SIMT emulator. Every tile form
* (RADIANCE_F32_GEMM_TILE) must match the one-wave-per-output legacy kernel bit for bit, and the
* legacy kernel must match its arithmetic written out here: lane l folds k = l, l+32, ... with
* fmaf, then the xor butterfly over 16, 8, 4, 2, 1. Tail rows and columns and the pitch's padding
* stay untouched.
/* tests/r4d_emu: gemm_nt_f32 (r4d_glm.hip) on the host SIMT emulator. The form the launcher picks
* at each shape (one wave an output at one row, else a tile of rows x columns a wave, by M; the
* shapes reach every one) must match the arithmetic written out here bit for bit: lane l folds
* k = l, l+32, ... with fmaf, then the xor butterfly over 16, 8, 4, 2, 1. Tail rows and columns
* and the pitch's padding stay untouched.
*
* router_sigmoid_topk from the same unit: the wave-per-row select must match the serial scan
* (RADIANCE_ROUTER_SELECT=old) bit for bit, and the scan its arithmetic written out here, on
* random and on tied ranks, with the outputs' pitch padding untouched. */
* router_sigmoid_topk from the same unit: the wave-per-row select must match a serial scan written
* out here bit for bit, on random and on tied ranks, with the outputs' pitch padding 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 float legacy_model(const uint16_t* x, const float* w, int k) {
extern const char* emu_last_kernel;
static float gemm_model(const uint16_t* x, const float* w, int k) {
float lane[32];
for (int l = 0; l < 32; ++l) {
float s = 0;
@@ -115,62 +115,41 @@ static void router_cases(std::mt19937& rng, int& cases, int& fails) {
a.n_t = 4;
a.p = p;
a.n_p = 6;
auto run = [&](const char* form, std::vector<int>& ids, std::vector<float>& w) {
ids.assign((size_t)M * ip, -77);
w.assign((size_t)M * wp, -7.25f);
t[2].data = ids.data();
t[3].data = w.data();
if (form)
setenv("RADIANCE_ROUTER_SELECT", form, 1);
else
unsetenv("RADIANCE_ROUTER_SELECT");
return r4d_rad_router_sigmoid_topk(&a, nullptr);
};
std::vector<int> ids_old, ids_new;
std::vector<float> w_old, w_new;
std::vector<int> ids((size_t)M * ip, -77);
std::vector<float> w((size_t)M * wp, -7.25f);
t[2].data = ids.data();
t[3].data = w.data();
++cases;
if (run("old", ids_old, w_old) != RAD_OK) {
std::printf("FAIL router scan launch M=%d ne=%d\n", M, ne);
++fails;
continue;
}
const int rc = r4d_rad_router_sigmoid_topk(&a, nullptr);
int bad = 0;
for (int r = 0; r < M; ++r) {
for (int r = 0; r < M && rc == RAD_OK; ++r) {
int mi[32];
float mw[32];
router_model(&lg[(size_t)r * lp], bias.data(), bp, ne, k, sh.norm, factor, mi, mw);
bad += std::memcmp(mi, &ids_old[(size_t)r * ip], (size_t)k * 4) != 0;
bad += std::memcmp(mw, &w_old[(size_t)r * wp], (size_t)k * 4) != 0;
for (int c = k; c < ip; ++c) bad += ids_old[(size_t)r * ip + c] != -77;
for (int c = k; c < wp; ++c) bad += w_old[(size_t)r * wp + c] != -7.25f;
bad += std::memcmp(mi, &ids[(size_t)r * ip], (size_t)k * 4) != 0;
bad += std::memcmp(mw, &w[(size_t)r * wp], (size_t)k * 4) != 0;
for (int c = k; c < ip; ++c) bad += ids[(size_t)r * ip + c] != -77;
for (int c = k; c < wp; ++c) bad += w[(size_t)r * wp + c] != -7.25f;
}
if (bad) {
std::printf("FAIL router scan against its model at M=%d ne=%d k=%d: %d\n", M, ne, k,
bad);
++fails;
}
++cases;
const int rc = run(nullptr, ids_new, w_new);
if (rc != RAD_OK || ids_new != ids_old ||
std::memcmp(w_new.data(), w_old.data(), w_new.size() * 4) != 0) {
std::printf("FAIL router wave select at M=%d ne=%d k=%d%s (rc %d)\n", M, ne, k,
sh.ties ? " tied" : "", rc);
if (rc != RAD_OK || bad) {
std::printf("FAIL router wave select against the scan at M=%d ne=%d k=%d%s (rc %d): "
"%d\n",
M, ne, k, sh.ties ? " tied" : "", rc, bad);
++fails;
}
}
unsetenv("RADIANCE_ROUTER_SELECT");
}
int main() {
std::mt19937 rng(17);
std::normal_distribution<float> nd(0.f, 1.f);
const char* forms[] = { "1x1", "2x1", "4x1", "8x1", "4x2", "8x2",
"4x4", "8x4", "16x2", "16x4", nullptr };
struct Shape {
int M, N, K;
};
const Shape shapes[] = { { 1, 288, 512 }, { 3, 36, 129 }, { 8, 288, 129 }, { 33, 32, 512 },
{ 70, 36, 129 }, { 70, 4, 512 }, { 7, 31, 129 }, { 65, 13, 130 } };
// One wave an output (M 1), the 2x1, 4x1 (M under 16 or N up to 8), 4x4 and 8x4 tiles.
const Shape shapes[] = { { 1, 288, 512 }, { 2, 288, 129 }, { 3, 36, 129 },
{ 8, 288, 129 }, { 33, 32, 512 }, { 70, 36, 129 },
{ 70, 4, 512 }, { 7, 31, 129 }, { 65, 13, 130 } };
int fails = 0, cases = 0;
for (const Shape& sh : shapes) {
const int M = sh.M, N = sh.N, K = sh.K, xp = K + 5, yp = N + 3;
@@ -211,49 +190,24 @@ int main() {
a.n_t = 3;
a.p = p;
a.n_p = 4;
auto run = [&](const char* form, std::vector<float>& y) {
y.assign((size_t)M * yp, -7.25f);
t[2].data = y.data();
if (form)
setenv("RADIANCE_F32_GEMM_TILE", form, 1);
else
unsetenv("RADIANCE_F32_GEMM_TILE");
return r4d_rad_gemm_nt_f32(&a, nullptr);
};
std::vector<float> legacy, got;
std::vector<float> y((size_t)M * yp, -7.25f);
t[2].data = y.data();
++cases;
if (run("legacy", legacy) != RAD_OK) {
std::printf("FAIL legacy launch M=%d N=%d K=%d\n", M, N, K);
++fails;
continue;
}
const int rc = r4d_rad_gemm_nt_f32(&a, nullptr);
int bad = 0;
for (int r = 0; r < M; ++r) {
for (int r = 0; r < M && rc == RAD_OK; ++r) {
for (int c = 0; c < N; ++c) {
const float e = legacy_model(&x[(size_t)r * xp], &w[(size_t)c * K], K);
bad += std::memcmp(&e, &legacy[(size_t)r * yp + c], 4) != 0;
const float e = gemm_model(&x[(size_t)r * xp], &w[(size_t)c * K], K);
bad += std::memcmp(&e, &y[(size_t)r * yp + c], 4) != 0;
}
for (int c = N; c < yp; ++c) bad += legacy[(size_t)r * yp + c] != -7.25f;
for (int c = N; c < yp; ++c) bad += y[(size_t)r * yp + c] != -7.25f;
}
if (bad) {
std::printf("FAIL legacy kernel against its model at M=%d N=%d K=%d: %d words\n", M, N,
K, bad);
if (rc != RAD_OK || bad) {
std::printf("FAIL %s at M=%d N=%d K=%d against its model (rc %d): %d words\n",
emu_last_kernel, M, N, K, rc, bad);
++fails;
}
for (const char* const* f = forms;; ++f) {
++cases;
const int rc = run(*f, got);
const bool same =
rc == RAD_OK && std::memcmp(got.data(), legacy.data(), got.size() * 4) == 0;
if (!same) {
std::printf("FAIL form %s at M=%d N=%d K=%d (rc %d)\n", *f ? *f : "default", M, N,
K, rc);
++fails;
}
if (!*f) break;
}
}
unsetenv("RADIANCE_F32_GEMM_TILE");
router_cases(rng, cases, fails);
std::printf("%d case(s), %d failure(s)\n", cases, fails);
return fails ? 1 : 0;
+38 -30
View File
@@ -1,20 +1,22 @@
/* 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. */
* emulator. The form the rule picks at each shape (the decode form at one and two column tiles a
* wave and one to eight waves a block, the 16- and 32-row tiles) must match the legacy 16x64 tile,
* which serves an activation pitch off 16 bytes, 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>
extern const char* emu_last_kernel;
static RadTensor mat(void* p, uint32_t dt, int64_t rows, int64_t cols, int64_t ld) {
RadTensor t{};
t.data = p;
@@ -32,19 +34,26 @@ int main() {
std::normal_distribution<float> nd(0.f, 1.f);
struct Shape {
int M, N, K;
const char* kernel;
};
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 };
const char* const dec1 = "(fp8_f32scale_decode<1, 1>)";
const char* const dec2 = "(fp8_f32scale_decode<1, 2>)";
const char* const rows = "fp8_f32scale_rows<R>";
// Decode at eight, four, two and one waves a block (K blocks 12/32, 4, 2, 1), at two column
// tiles a wave from 17 rows over N 1024 and 640; the 16-row tile, and the 32-row one at N 4096.
const Shape shapes[] = { { 1, 256, 512, dec1 }, { 6, 128, 1536, dec1 },
{ 17, 256, 512, dec1 }, { 48, 128, 512, dec1 },
{ 2, 128, 256, dec1 }, { 5, 256, 128, dec1 },
{ 3, 128, 4096, dec1 }, { 40, 1024, 512, dec2 },
{ 20, 640, 256, dec2 }, { 70, 128, 384, rows },
{ 130, 128, 256, rows }, { 33, 4096, 512, rows },
{ 70, 4096, 128, rows } };
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);
const int M = sh.M, N = sh.N, K = sh.K, kb = K / 128, ap = K + 16, lp = K + 8, yp = N + 8;
std::vector<uint8_t> a((size_t)M * ap), al((size_t)M * lp), w((size_t)N * K);
for (auto& v : a) v = rad_f32_to_fp8e4m3(nd(rng));
for (int r = 0; r < M; ++r) std::memcpy(&al[(size_t)r * lp], &a[(size_t)r * ap], K);
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
@@ -67,10 +76,12 @@ int main() {
p[4].key = "dtype";
p[4].kind = RAD_P_STR;
p[4].sval = "fp8a8f32";
auto run = [&](const char* form, std::vector<uint16_t>& y) {
// The pitch off 16 bytes takes the legacy tile.
auto run = [&](bool legacy, std::vector<uint16_t>& y) {
y.assign((size_t)M * yp, 0x7777);
t[0] = legacy ? mat(al.data(), RAD_F8E4M3, M, K, lp)
: mat(a.data(), RAD_F8E4M3, M, K, ap);
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;
@@ -80,8 +91,8 @@ int main() {
};
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);
if (run(true, legacy) != RAD_OK || std::strcmp(emu_last_kernel, "fp8_f32scale")) {
std::printf("FAIL legacy M=%d N=%d K=%d ran %s\n", M, N, K, emu_last_kernel);
++fails;
continue;
}
@@ -92,19 +103,16 @@ int main() {
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;
}
++cases;
const int rc = run(false, got);
if (rc != RAD_OK || std::strcmp(emu_last_kernel, sh.kernel) || got != legacy) {
size_t i = 0;
while (rc == RAD_OK && i < got.size() && got[i] == legacy[i]) ++i;
std::printf("FAIL %s (want %s) at M=%d N=%d K=%d (rc %d, first word %zu)\n",
emu_last_kernel, sh.kernel, 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;
}
+65 -35
View File
@@ -1,15 +1,19 @@
/* 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. */
/* tests/r4d_emu: kda_update (r4d_kda.hip) on the host SIMT emulator, held to libref's kda_update
* (from $RADIANCE_HOME, which ctest sets) over the output and the whole state buffer, 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, so a byte the reference leaves alone must stay as it was. The kernel's lane
* reductions sum in another order than libref's loops: f32 state within 2e-5 + 2e-5 |x|, bf16
* output within one rounding step. */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <random>
#include <string>
#include <vector>
@@ -33,6 +37,32 @@ static uint16_t f2b(float f) {
return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
}
/* libref's kda_update, through the plugin's own exports, or null. */
static RadLaunchFn load_libref() {
const char* home = std::getenv("RADIANCE_HOME");
if (!home || !*home) return nullptr;
const std::string p = std::string(home) + "/kernels/libref.so";
void* h = dlopen(p.c_str(), RTLD_NOW | RTLD_LOCAL);
if (!h) return nullptr;
auto open = (int (*)(void))dlsym(h, "rad_plugin_open"); /* as the loader does */
if (open && open() < 0) return nullptr;
auto cnt = (int (*)(void))dlsym(h, "rad_kernel_count");
auto at = (const RadKernelInfo* (*)(int))dlsym(h, "rad_kernel_at");
if (!cnt || !at) return nullptr;
for (int i = 0; i < cnt(); ++i) {
const RadKernelInfo* k = at(i);
if (k && k->op && !std::strcmp(k->op, "kda_update")) return k->launch;
}
return nullptr;
}
static float bf16_value(uint16_t b) {
const uint32_t u = (uint32_t)b << 16;
float f;
std::memcpy(&f, &u, 4);
return f;
}
struct Case {
const char* name;
const char* mode;
@@ -43,6 +73,11 @@ struct Case {
};
int main() {
const RadLaunchFn reference = load_libref();
if (!reference) {
std::printf("FAIL libref's kda_update not loadable (set RADIANCE_HOME)\n");
return 1;
}
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;
@@ -93,12 +128,6 @@ int main() {
{ 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;
@@ -106,7 +135,7 @@ int main() {
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) {
auto run = [&](RadLaunchFn launch, 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 }),
@@ -149,39 +178,40 @@ int main() {
a.n_t = 13;
a.p = p;
a.n_p = 9;
return r4d_rad_kda_update(&a, nullptr);
return launch(&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);
++checks;
if (run(reference, ref_out, ref_state) != RAD_OK ||
run(r4d_rad_kda_update, out, state) != RAD_OK) {
std::printf("FAIL %s: 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;
size_t bad = 0;
double out_err = 0, state_err = 0;
for (size_t i = 0; i < out.size(); ++i) {
if (out[i] == ref_out[i]) continue;
const float x = bf16_value(out[i]), y = bf16_value(ref_out[i]);
const double e = std::fabs((double)x - y);
out_err = std::max(out_err, e);
if (!std::isfinite(x) || ref_out[i] == 0x7777 || out[i] == 0x7777 ||
e > 1e-6 + 0.008 * std::fabs(y))
++bad;
}
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);
}
for (size_t i = 0; i < state.size(); ++i) {
if (state[i] == ref_state[i]) continue;
const double e = std::fabs((double)state[i] - ref_state[i]);
state_err = std::max(state_err, e);
if (!std::isfinite(state[i]) || e > 2e-5 + 2e-5 * std::fabs(ref_state[i])) ++bad;
}
unsetenv("RADIANCE_KDA_ROWS");
unsetenv("RADIANCE_KDA_PREFETCH");
std::printf("%-24s output max %.3g, state max %.3g%s\n", c.name, out_err, state_err,
!written ? " NO OUTPUT" : bad ? " FAIL" : "");
if (!written || bad) ++fails;
}
std::printf("kda_update forms against the old kernel: %d of %d bit-identical\n", checks - fails,
checks);
std::printf("kda_update against libref: %d of %d within bounds\n", checks - fails, checks);
return fails ? 1 : 0;
}
+123 -160
View File
@@ -1,8 +1,9 @@
/* tests/r4d_emu: GLM's mHC read split by residual chunks (r4d_mhc.hip) on the host SIMT emulator.
* Each rank's mhc_pre_partial over its chunks, the partials joined row by row as the
* row-interleaved all_gather lays them out, then mhc_pre_finish, must give mhc_pre's x, post and
* comb bit for bit, under each of mhc_pre's kernel switches; the partial matrix's padding slots
* are zero. Every form is also held to the old dispatch, bit for bit. */
* comb bit for bit; the partial matrix's padding slots are zero. On 16-byte rows the vector forms
* run, and they are held bit for bit to the same operands on pitched rows, where the scalar forms
* run. */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
@@ -42,148 +43,123 @@ int main() {
std::vector<float> fn((size_t)(mix * W)), scale = { 0.7f, 0.9f, 1.1f }, base((size_t)mix);
for (auto& v : fn) v = nd(rng) * 0.01f;
for (auto& v : base) v = nd(rng) * 0.5f;
struct Form {
const char* env;
const char* value;
};
const Form forms[] = { { nullptr, nullptr },
{ "RADIANCE_MHC_PREFILL_COEFF4", "0" },
{ "RADIANCE_MHC_PREFILL_ROWS4", "0" },
{ "RADIANCE_MHC_PREFILL_WARP", "0" } };
int fails = 0, cases = 0;
for (int64_t rows : { 1, 5, 37 }) {
const int64_t hp = W + 6, xp = n + 2;
std::vector<uint16_t> h((size_t)(rows * hp));
for (auto& v : h) v = f2b(nd(rng));
for (const Form& f : forms) {
if (f.env) setenv(f.env, f.value, 1);
RadParam coef[8]{};
coef[0] = { "M", RAD_P_INT, rows };
coef[1] = { "n", RAD_P_INT, n };
coef[2] = { "hc", RAD_P_INT, hc };
coef[3] = { "iterations", RAD_P_INT, 20 };
coef[4].key = "eps";
coef[4].kind = RAD_P_F64;
coef[4].dval = 1e-5;
coef[5].key = "hc_eps";
coef[5].kind = RAD_P_F64;
coef[5].dval = 1e-6;
coef[6].key = "dtype";
coef[6].kind = RAD_P_STR;
coef[6].sval = "bf16";
std::vector<uint16_t> x((size_t)(rows * xp), 0x7777);
std::vector<float> post((size_t)(rows * hc)), comb((size_t)(rows * hc * hc));
RadTensor t[7] = { mat(h.data(), RAD_BF16, rows, W, hp),
mat(fn.data(), RAD_F32, mix, W, W),
vec(scale.data(), 3),
vec(base.data(), mix),
mat(x.data(), RAD_BF16, rows, n, xp),
mat(post.data(), RAD_F32, rows, hc, hc),
mat(comb.data(), RAD_F32, rows, hc * hc, hc * hc) };
RadArgs a{};
a.t = t;
a.n_t = 7;
a.p = coef;
a.n_p = 7;
if (r4d_rad_mhc_pre(&a, nullptr) != RAD_OK) {
std::printf("FAIL mhc_pre M=%lld\n", (long long)rows);
RadParam coef[8]{};
coef[0] = { "M", RAD_P_INT, rows };
coef[1] = { "n", RAD_P_INT, n };
coef[2] = { "hc", RAD_P_INT, hc };
coef[3] = { "iterations", RAD_P_INT, 20 };
coef[4].key = "eps";
coef[4].kind = RAD_P_F64;
coef[4].dval = 1e-5;
coef[5].key = "hc_eps";
coef[5].kind = RAD_P_F64;
coef[5].dval = 1e-6;
coef[6].key = "dtype";
coef[6].kind = RAD_P_STR;
coef[6].sval = "bf16";
std::vector<uint16_t> x((size_t)(rows * xp), 0x7777);
std::vector<float> post((size_t)(rows * hc)), comb((size_t)(rows * hc * hc));
RadTensor t[7] = { mat(h.data(), RAD_BF16, rows, W, hp),
mat(fn.data(), RAD_F32, mix, W, W),
vec(scale.data(), 3),
vec(base.data(), mix),
mat(x.data(), RAD_BF16, rows, n, xp),
mat(post.data(), RAD_F32, rows, hc, hc),
mat(comb.data(), RAD_F32, rows, hc * hc, hc * hc) };
RadArgs a{};
a.t = t;
a.n_t = 7;
a.p = coef;
a.n_p = 7;
if (r4d_rad_mhc_pre(&a, nullptr) != RAD_OK) {
std::printf("FAIL mhc_pre M=%lld\n", (long long)rows);
++fails;
continue;
}
for (int world : { 2, 4, 8 }) {
++cases;
const int64_t mine = total / world;
std::vector<float> gathered((size_t)(rows * total * S));
bool padded = true, launched = true;
for (int rank = 0; rank < world; ++rank) {
std::vector<float> part((size_t)(rows * mine * S), 7.0f);
RadTensor pt[3] = { t[0], t[1],
mat(part.data(), RAD_F32, rows, mine * S, mine * S) };
RadParam pp[7]{};
pp[0] = { "M", RAD_P_INT, rows };
pp[1] = { "n", RAD_P_INT, n };
pp[2] = { "hc", RAD_P_INT, hc };
pp[3] = { "chunk", RAD_P_INT, 2048 };
pp[4] = { "first", RAD_P_INT, rank * mine };
pp[5] = { "chunks", RAD_P_INT, mine };
pp[6].key = "dtype";
pp[6].kind = RAD_P_STR;
pp[6].sval = "bf16";
RadArgs pa{};
pa.t = pt;
pa.n_t = 3;
pa.p = pp;
pa.n_p = 7;
launched = launched && r4d_rad_mhc_pre_partial(&pa, nullptr) == RAD_OK;
for (int64_t r = 0; r < rows; ++r) {
for (int64_t c = 0; c < mine; ++c)
for (int64_t j = mix + 1; j < S; ++j)
padded = padded && part[(size_t)(r * mine * S + c * S + j)] == 0.0f;
std::memcpy(&gathered[(size_t)(r * total * S + rank * mine * S)],
&part[(size_t)(r * mine * S)], (size_t)(mine * S * 4));
}
}
std::vector<uint16_t> x2((size_t)(rows * xp), 0x7777);
std::vector<float> post2(post.size()), comb2(comb.size());
RadTensor ft[7] = { mat(gathered.data(), RAD_F32, rows, total * S, total * S),
t[0],
t[2],
t[3],
mat(x2.data(), RAD_BF16, rows, n, xp),
mat(post2.data(), RAD_F32, rows, hc, hc),
mat(comb2.data(), RAD_F32, rows, hc * hc, hc * hc) };
RadParam fp[8];
std::memcpy(fp, coef, sizeof coef);
fp[6] = { "chunk", RAD_P_INT, 2048 };
fp[7].key = "dtype";
fp[7].kind = RAD_P_STR;
fp[7].sval = "bf16";
RadArgs fa{};
fa.t = ft;
fa.n_t = 7;
fa.p = fp;
fa.n_p = 8;
launched = launched && r4d_rad_mhc_pre_finish(&fa, nullptr) == RAD_OK;
const bool same = launched && x2 == x &&
std::memcmp(post2.data(), post.data(), post.size() * 4) == 0 &&
std::memcmp(comb2.data(), comb.data(), comb.size() * 4) == 0;
if (!same || !padded) {
std::printf("FAIL M=%lld world %d: %s\n", (long long)rows, world,
!launched ? "launch"
: !padded ? "padding"
: "differs");
++fails;
continue;
}
for (int world : { 2, 4, 8 }) {
++cases;
const int64_t mine = total / world;
std::vector<float> gathered((size_t)(rows * total * S));
bool padded = true, launched = true;
for (int rank = 0; rank < world; ++rank) {
std::vector<float> part((size_t)(rows * mine * S), 7.0f);
RadTensor pt[3] = { t[0], t[1],
mat(part.data(), RAD_F32, rows, mine * S, mine * S) };
RadParam pp[7]{};
pp[0] = { "M", RAD_P_INT, rows };
pp[1] = { "n", RAD_P_INT, n };
pp[2] = { "hc", RAD_P_INT, hc };
pp[3] = { "chunk", RAD_P_INT, 2048 };
pp[4] = { "first", RAD_P_INT, rank * mine };
pp[5] = { "chunks", RAD_P_INT, mine };
pp[6].key = "dtype";
pp[6].kind = RAD_P_STR;
pp[6].sval = "bf16";
RadArgs pa{};
pa.t = pt;
pa.n_t = 3;
pa.p = pp;
pa.n_p = 7;
launched = launched && r4d_rad_mhc_pre_partial(&pa, nullptr) == RAD_OK;
for (int64_t r = 0; r < rows; ++r) {
for (int64_t c = 0; c < mine; ++c)
for (int64_t j = mix + 1; j < S; ++j)
padded = padded && part[(size_t)(r * mine * S + c * S + j)] == 0.0f;
std::memcpy(&gathered[(size_t)(r * total * S + rank * mine * S)],
&part[(size_t)(r * mine * S)], (size_t)(mine * S * 4));
}
}
std::vector<uint16_t> x2((size_t)(rows * xp), 0x7777);
std::vector<float> post2(post.size()), comb2(comb.size());
RadTensor ft[7] = { mat(gathered.data(), RAD_F32, rows, total * S, total * S),
t[0],
t[2],
t[3],
mat(x2.data(), RAD_BF16, rows, n, xp),
mat(post2.data(), RAD_F32, rows, hc, hc),
mat(comb2.data(), RAD_F32, rows, hc * hc, hc * hc) };
RadParam fp[8];
std::memcpy(fp, coef, sizeof coef);
fp[6] = { "chunk", RAD_P_INT, 2048 };
fp[7].key = "dtype";
fp[7].kind = RAD_P_STR;
fp[7].sval = "bf16";
RadArgs fa{};
fa.t = ft;
fa.n_t = 7;
fa.p = fp;
fa.n_p = 8;
launched = launched && r4d_rad_mhc_pre_finish(&fa, nullptr) == RAD_OK;
const bool same = launched && x2 == x &&
std::memcmp(post2.data(), post.data(), post.size() * 4) == 0 &&
std::memcmp(comb2.data(), comb.data(), comb.size() * 4) == 0;
if (!same || !padded) {
std::printf("FAIL M=%lld world %d %s%s%s: %s\n", (long long)rows, world,
f.env ? f.env : "default", f.env ? "=" : "", f.env ? f.value : "",
!launched ? "launch"
: !padded ? "padding"
: "differs");
++fails;
}
}
if (f.env) unsetenv(f.env);
}
}
/* Every form against the old dispatch (one-wave finish from 32 rows, scalar mix and read
* below, scalar-row and rows4 partials, the per-output mhc_post) on 16-byte rows, where the
* vector paths run: mhc_pre's x, post and comb, mhc_pre_partial's matrix and mhc_post's
* output, bit for bit. */
struct Pin {
const char* what;
const char* finish;
const char* vec;
const char* prow;
};
const Pin pins[] = { { "rule", nullptr, nullptr, nullptr },
{ "finish scalar", "scalar", nullptr, nullptr },
{ "partials four-slot, 1 row a wave", nullptr, "1", "1" },
{ "partials four-slot, 2 rows a wave", nullptr, "1", "2" },
{ "partials four-slot, 4 rows a wave", nullptr, "1", "4" } };
auto pin = [](const char* k, const char* v) {
if (v)
setenv(k, v, 1);
else
unsetenv(k);
};
/* The vector forms (four-slot partials, the finish's 16-byte collapse, the eight-column
* mhc_post) on 16-byte rows against the scalar forms (one slot a thread, four rows and four
* coefficients a block from 32 rows, the per-output mhc_post) on the same operands with the
* residual pitched by two elements: mhc_pre's x, post and comb, mhc_pre_partial's matrix and
* mhc_post's output, bit for bit. */
for (int64_t rows : { 1, 5, 16, 37, 64 }) {
std::vector<uint16_t> h((size_t)(rows * W)), y((size_t)(rows * n));
for (auto& v : h) v = f2b(nd(rng));
for (auto& v : y) v = f2b(nd(rng));
const int64_t pitched = W + 2;
std::vector<uint16_t> hp((size_t)(rows * pitched), 0x7777);
for (int64_t r = 0; r < rows; ++r)
std::memcpy(&hp[(size_t)(r * pitched)], &h[(size_t)(r * W)], (size_t)W * 2);
RadParam coef[7]{};
coef[0] = { "M", RAD_P_INT, rows };
coef[1] = { "n", RAD_P_INT, n };
@@ -202,13 +178,13 @@ int main() {
std::vector<uint16_t> x, o;
std::vector<float> p, c, part;
};
auto run = [&](Out* r) {
auto run = [&](Out* r, const uint16_t* hd, int64_t ld) {
r->x.assign((size_t)(rows * n), 0x7777);
r->o.assign((size_t)(rows * W), 0x7777);
r->p.assign((size_t)(rows * hc), 0);
r->c.assign((size_t)(rows * hc * hc), 0);
r->part.assign((size_t)(rows * total * S), 7.0f);
RadTensor t[7] = { mat(h.data(), RAD_BF16, rows, W, W),
RadTensor t[7] = { mat((void*)hd, RAD_BF16, rows, W, ld),
mat(fn.data(), RAD_F32, mix, W, W),
vec(scale.data(), 3),
vec(base.data(), mix),
@@ -249,34 +225,21 @@ int main() {
return okay && r4d_rad_mhc_post(&oa, nullptr) == RAD_OK;
};
Out ref, got;
pin("RADIANCE_MHC_FINISH", "old");
pin("RADIANCE_MHC_PARTIAL_VEC", "0");
pin("RADIANCE_MHC_POST_VEC", "0");
const bool ref_ok = run(&ref);
pin("RADIANCE_MHC_POST_VEC", nullptr);
for (const Pin& f : pins) {
++cases;
pin("RADIANCE_MHC_FINISH", f.finish);
pin("RADIANCE_MHC_PARTIAL_VEC", f.vec);
pin("RADIANCE_MHC_PARTIAL_ROWS", f.prow);
const bool launched = ref_ok && run(&got);
const char* bad = !launched ? "launch"
: got.x != ref.x ? "x"
: std::memcmp(got.p.data(), ref.p.data(), ref.p.size() * 4) ? "post"
: std::memcmp(got.c.data(), ref.c.data(), ref.c.size() * 4) ? "comb"
: std::memcmp(got.part.data(), ref.part.data(), ref.part.size() * 4)
? "partials"
: got.o != ref.o ? "mhc_post"
: nullptr;
if (bad) {
std::printf("FAIL M=%lld %s against the old dispatch: %s differs\n",
(long long)rows, f.what, bad);
++fails;
}
++cases;
const bool launched = run(&ref, hp.data(), pitched) && run(&got, h.data(), W);
const char* bad = !launched ? "launch"
: got.x != ref.x ? "x"
: std::memcmp(got.p.data(), ref.p.data(), ref.p.size() * 4) ? "post"
: std::memcmp(got.c.data(), ref.c.data(), ref.c.size() * 4) ? "comb"
: std::memcmp(got.part.data(), ref.part.data(), ref.part.size() * 4)
? "partials"
: got.o != ref.o ? "mhc_post"
: nullptr;
if (bad) {
std::printf("FAIL M=%lld 16-byte rows against pitched rows: %s differs\n",
(long long)rows, bad);
++fails;
}
for (const char* k :
{ "RADIANCE_MHC_FINISH", "RADIANCE_MHC_PARTIAL_VEC", "RADIANCE_MHC_PARTIAL_ROWS" })
unsetenv(k);
}
std::printf("%d case(s), %d failure(s)\n", cases, fails);
return fails ? 1 : 0;
+21 -10
View File
@@ -3,7 +3,9 @@
* tile (eight heads of two neighbouring rows) on the tables it meets in serving: a prefill chunk
* whose rows share one table, and rows past the DSA budget, where each row selects its own top
* blocks and neighbours share most blocks but rarely a table position. The conflict counters
* (RADIANCE_MLA_CONFLICT_STATS) show which of the two the pairing saw. */
* (RADIANCE_MLA_CONFLICT_STATS) show which of the two the pairing saw. Both pairings run on the
* same operands: the union at the case's table width, the positional pairing on the table
* widened past the union's lists (kUnionBlocks, 520 entries) with absent entries no row reaches. */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
@@ -22,7 +24,7 @@ extern "C" void r4d_mla_matrix_stats(unsigned long long* out);
namespace {
constexpr int D = 512, BS = 4;
constexpr int D = 512, BS = 4, kUnionBlocks = 520;
enum class Fill { prefill, topk, ragged, repeat };
@@ -35,6 +37,7 @@ struct Case {
bool scratch = false; /* lets the launcher split the history */
int form = 0; /* r4d_mla_matrix_pin_form */
int expect_second = -1; /* positional pairing: 0 no second pass, 1 some, -1 either */
/* Unsplit only: with scratch the widened table gives the positional run more splits. */
bool bitwise_a = true; /* the union's request r equals the positional pairing's bit for bit */
bool bitwise_all = false; /* both requests do (the tables agree position by position) */
bool union_falls_back = false; /* the table is wider than the union's lists */
@@ -234,14 +237,24 @@ int run(const Case& c, std::mt19937& rng) {
want[((size_t)r * H + h) * D + d] = sum > 0 ? acc[d] / sum : 0.0;
}
/* Both pairings on the same operands: the positional one (RADIANCE_MLA_PAIR=pos) and the
* union, which must never take a second pass. */
/* Both pairings on the same operands: the positional one, on the table widened past the
* union's lists, and at the case's own width the launcher's choice, the union, which must
* never take a second pass. A tile of 16 heads has no pairing and runs once. */
const bool paired = H % 16 == 8;
const int wide = std::max(B, kUnionBlocks + 1);
std::vector<int> bt_wide((size_t)M * wide, -1);
for (int r = 0; r < M; ++r)
std::copy(bt.begin() + (size_t)r * B, bt.begin() + (size_t)(r + 1) * B,
bt_wide.begin() + (size_t)r * wide);
std::vector<uint16_t> by_pos;
int fails = 0;
for (const char* pairing : { "pos", "union" }) {
if (!paired && !std::strcmp(pairing, "union")) break;
setenv("RADIANCE_MLA_PAIR", pairing, 1);
const bool by_page = !std::strcmp(pairing, "union");
if (!paired && by_page) break;
const bool widened = paired && !by_page;
t[2] = widened ? tens(bt_wide.data(), RAD_I32, { M, wide })
: tens(bt.data(), RAD_I32, { M, B });
p[3].ival = widened ? wide : B;
std::fill(out.begin(), out.end(), 0xffff);
unsigned long long stats[3];
r4d_mla_matrix_stats(stats);
@@ -249,7 +262,6 @@ int run(const Case& c, std::mt19937& rng) {
const int rc = r4d_rad_mla_attn_matrix(&a, nullptr);
r4d_mla_matrix_pin_form(0);
r4d_mla_matrix_stats(stats);
unsetenv("RADIANCE_MLA_PAIR");
if (rc != RAD_OK) {
std::printf("%-40s %-5s launch returned %d\n", c.name.c_str(), pairing, rc);
++fails;
@@ -265,7 +277,6 @@ int run(const Case& c, std::mt19937& rng) {
}
bool ok = bad == 0;
std::string note;
const bool by_page = !std::strcmp(pairing, "union");
if (!by_page) {
by_pos = out;
if (c.expect_second >= 0 && (stats[1] > 0) != (c.expect_second == 1)) {
@@ -297,11 +308,11 @@ int run(const Case& c, std::mt19937& rng) {
std::snprintf(buf, sizeof buf, " vs pos: %zu/%zu differ (r/r+1), max abs %.2e",
first_diff_a, diff_b, max_ab);
note += buf;
if (c.bitwise_a && first_diff_a) {
if (c.bitwise_a && !c.scratch && first_diff_a) {
note += " (request r differs)";
ok = false;
}
if (c.bitwise_all && diff_b) {
if (c.bitwise_all && !c.scratch && diff_b) {
note += " (request r+1 differs)";
ok = false;
}
+129 -229
View File
@@ -1,30 +1,28 @@
/* tests/r4d_emu: the MXFP4 expert GEMM (r4d_moe_mxfp4.hip) on the host SIMT emulator. Every form
* the RADIANCE_MXFP4_* switches reach must match the arithmetic-decode row-group form bit for bit,
* and both a host restatement of the per-element sequence: per 128 of K in order, per 32-wide group
* a fresh 32-product sum in k order (the emulated WMMA pair), folded as raw * activation scale *
* E8M0 scale with two roundings and an add. Runs of 0, 1, 15, 16, 17, 63, 64, 65 and 130 rows,
* dropped slots inside runs and past the routed rows, columns an odd expert does not have, a
* shorter K for odd experts, E8M0 codes 0 and 255, parts 2, token and sorted rows, stacked planes
* and pointer tables. The P64 forms (RADIANCE_MXFP4_PREFILL_P64) run at every BM, BN, stage and
* column-tile count, pinned below their row bound, with jobs cut at 32 and 64 rows, column
* blocks past N and the output through LDS at aligned and odd pitches. The table decode is also
* held to the arithmetic one over every code. */
/* tests/r4d_emu: the MXFP4 expert GEMM (r4d_moe_mxfp4.hip) on the host SIMT emulator. The form the
* rule picks at each case (named per case, so a moved bound shows) must match a host restatement
* of the per-element sequence bit for bit: per 128 of K in order, per 32-wide group a fresh
* 32-product sum in k order (the emulated WMMA pair), folded as raw * activation scale * E8M0
* scale with two roundings and an add. Away from GLM's slices the expert-major forms (K 256 and a
* longer K) and the row groups; at GLM's TP4 down slice the GLM prefill form, at its TP8 gate_up
* slice the P64 form with streamed A. Runs of 0, 1, 15, 16, 17, 63, 64, 65 and 130 rows, dropped
* slots inside runs and past the routed rows, columns an odd expert does not have, a shorter K for
* odd experts, E8M0 codes 0 and 255, parts 2, token and sorted rows, stacked planes and pointer
* tables. The table decode is also held to the arithmetic one over every code. */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
#include "r4d_mxfp4.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <string>
#include <vector>
extern const char* emu_last_kernel;
/* The arithmetic decode the kernels used before the table (r4d_moe_mxfp4.hip, fp4x4 and its
* byte interleave), restated as the reference of r4d_mxfp4_unpack8_exact. */
/* The bytewise arithmetic decode, the reference of r4d_mxfp4_unpack8_exact: per byte, magnitudes
* from 2 map to (m + 12) * 4, 0 and 1 to 0 and 48, the sign to bit 7; then the low and high
* nibbles' bytes interleave in k order. */
static uint32_t fp4x4_ref(uint32_t n) {
const uint32_t mag = n & 0x07070707u, flag = (mag + 0x06060606u) & 0x08080808u;
const uint32_t mask = (flag << 5) - (flag >> 3);
@@ -76,6 +74,7 @@ struct Case {
bool sorted_a, tables, exceptional;
std::vector<int> runs;
int tail; // dropped slots past the routed rows
const char* kernel; // the form the rule picks
};
/* Byte equality with every bf16 NaN taken as one value. On x86 inf - inf gives the negative
@@ -87,9 +86,6 @@ static size_t first_diff(const std::vector<uint16_t>& a, const std::vector<uint1
if (a[i] != b[i] && !(nan16(a[i]) && nan16(b[i]))) return i;
return a.size() == b.size() ? a.size() : std::min(a.size(), b.size());
}
static bool same16(const std::vector<uint16_t>& a, const std::vector<uint16_t>& b) {
return a.size() == b.size() && first_diff(a, b) == a.size();
}
static RadTensor ten(void* p, uint32_t dt, std::vector<int64_t> shape,
std::vector<int64_t> stride) {
@@ -104,18 +100,30 @@ static RadTensor ten(void* p, uint32_t dt, std::vector<int64_t> shape,
return t;
}
/* GLM's 288 experts: the given runs at experts spread over both classes, every other one empty. */
static std::vector<int> glm_runs(std::vector<int> live) {
static const int at[] = { 2, 3, 50, 101, 150, 199, 200, 241, 286, 287 };
std::vector<int> runs(288, 0);
for (size_t i = 0; i < live.size() && i < sizeof at / sizeof at[0]; ++i) runs[at[i]] = live[i];
return runs;
}
int main() {
int fails = check_decode(), cases = 1;
if (fails) std::printf("FAIL table decode: %d mismatches\n", fails);
const std::vector<int> runs9 = { 0, 1, 15, 16, 17, 63, 64, 65, 130, 141 };
const char* const staged = "(moe_mxfp4_expert_staged_kernel<128, 4>)";
const char* const expert = "(moe_mxfp4_expert_kernel<4, 256>)";
const char* const groups = "moe_mxfp4_fp8_kernel";
const Case all[] = {
{ "gate_up-like, token rows", 128, 384, 128, 384, 2, 4, false, false, false, runs9, 6 },
{ "gate_up-like, token rows", 128, 384, 128, 384, 2, 4, false, false, false, runs9, 6,
groups },
{ "odd experts narrower and shorter", 128, 384, 64, 256, 2, 4, false, true, false, runs9,
2 },
{ "down-like, sorted rows", 64, 256, 64, 256, 1, 2, true, true, false, runs9, 4 },
{ "exceptional E8M0 codes", 64, 256, 64, 256, 2, 4, false, false, true, runs9, 4 },
{ "decode band, row groups of 4",
2, groups },
{ "down-like, sorted rows", 64, 256, 64, 256, 1, 2, true, true, false, runs9, 4, groups },
{ "exceptional E8M0 codes", 64, 256, 64, 256, 2, 4, false, false, true, runs9, 4, groups },
{ "expert-major, K 384",
64,
384,
64,
@@ -126,8 +134,9 @@ int main() {
true,
false,
{ 0, 1, 15, 16, 17, 30 },
3 },
{ "decode band, row groups of 1",
3,
expert },
{ "expert-major, K 256, exceptional codes",
64,
256,
32,
@@ -138,8 +147,9 @@ int main() {
false,
true,
{ 1, 0, 3, 15, 1 },
2 },
{ "P64 jobs at 32 and 64 rows, N under a block",
2,
staged },
{ "expert-major, K 512:384, runs past 64 rows, N under a block",
96,
512,
96,
@@ -150,8 +160,9 @@ int main() {
false,
false,
{ 32, 33, 31, 96, 128, 129, 0, 2 },
9 },
{ "P64 resident A, column tiles cut",
9,
expert },
{ "expert-major, K 256:128, column tiles cut",
192,
256,
128,
@@ -162,83 +173,47 @@ int main() {
true,
true,
{ 64, 63, 65, 1, 0, 127, 17 },
5 },
};
struct Form {
const char* env;
int stage, tiles, lut;
const char* kernel;
bool prefill;
bool pipe = false;
};
/* env: DECODE_EXPERTS, ALIGNED_JOBS, PREFILL_N32, PREFILL_M32, PREFILL_N64, PREFILL_CACHE_ROWS */
const Form forms[] = {
{ "100000", 256, 1, 0, "moe_mxfp4_expert_kernel<4, 256, false>", false },
{ "100000", 256, 1, 1, "moe_mxfp4_expert_kernel<4, 256, true>", false },
{ "100000", 256, 4, 0, "moe_mxfp4_expert_staged_kernel<256, 4, false>", false },
{ "100000", 256, 4, 1, "moe_mxfp4_expert_staged_kernel<256, 4, true>", false },
{ "100000", 128, 1, 0, "moe_mxfp4_expert_staged_kernel<128, 1, false>", false },
{ "100000", 128, 1, 1, "moe_mxfp4_expert_staged_kernel<128, 1, true>", false },
{ "100000", 128, 4, 0, "moe_mxfp4_expert_staged_kernel<128, 4, false>", false },
{ "100000", 128, 4, 1, "moe_mxfp4_expert_staged_kernel<128, 4, true>", false },
{ "000000", 0, 0, 1, "moe_mxfp4_fp8_kernel<1, 1, false, true>", false },
{ "010000", 0, 0, 1, "moe_mxfp4_fp8_kernel<1, 1, false, true>", true },
{ "001000", 0, 0, 1, "moe_mxfp4_fp8_kernel<2, 1, false, true>", true },
{ "011000", 0, 0, 1, "moe_mxfp4_fp8_kernel<2, 1, false, true>", true },
{ "011100", 0, 0, 1, "moe_mxfp4_fp8_kernel<2, 2, false, true>", true },
{ "011110", 0, 0, 1, "moe_mxfp4_fp8_kernel<4, 2, false, true>", true },
{ "011111", 0, 0, 0, "moe_mxfp4_fp8_kernel<4, 2, true, false>", true },
{ "011111", 0, 0, 1, "moe_mxfp4_fp8_kernel<4, 2, true, true>", true },
{ "011111", 0, 0, 0, "moe_mxfp4_prefill_kernel<false>", true, true },
{ "011111", 0, 0, 1, "moe_mxfp4_prefill_kernel<true>", true, true },
};
/* P64 at every raster point, column tiles 4 (the resident form's default) and 1, and the LDS
* output at the default; the K of a case picks streamed or resident A. */
struct P64 {
int bm, bn, sk, ct, out, db = 0;
};
std::vector<P64> p64s;
for (int bm : { 32, 64 })
for (int bn : { 64, 128 })
for (int sk : { 128, 256 })
for (int ct : { 1, 4 }) p64s.push_back({ bm, bn, sk, ct, 0 });
p64s.push_back({ 64, 128, 128, 4, 1 });
p64s.push_back({ 32, 64, 256, 1, 1 });
/* The double buffer at every BM x BN x stage: where two stages do not fit in LDS, and with
* resident A, the single-buffer form runs. */
for (int bm : { 32, 64 })
for (int bn : { 64, 128 })
for (int sk : { 128, 256 }) p64s.push_back({ bm, bn, sk, 4, 0, 1 });
p64s.push_back({ 64, 128, 128, 4, 1, 1 });
static const char* const kP64[] = { "RADIANCE_MXFP4_PREFILL_P64",
"RADIANCE_MXFP4_PREFILL_P64_MIN_T_GATE_UP",
"RADIANCE_MXFP4_PREFILL_P64_MIN_T_DOWN",
"RADIANCE_MXFP4_PREFILL_P64_BM",
"RADIANCE_MXFP4_PREFILL_P64_BN",
"RADIANCE_MXFP4_PREFILL_P64_STAGE_K",
"RADIANCE_MXFP4_PREFILL_P64_COLTILES",
"RADIANCE_MXFP4_PREFILL_P64_STAGE_OUT",
"RADIANCE_MXFP4_PREFILL_P64_T_BM64",
"RADIANCE_MXFP4_PREFILL_P64_T_BN128",
"RADIANCE_MXFP4_PREFILL_P64_T_DOWN_WIDE",
"RADIANCE_MXFP4_PREFILL_P64_DB" };
/* The rule's tile with nothing pinned, by its row bounds (unset: this test's few rows are
* under all of them); the widest tiles take the LDS output. */
struct Rule {
const char* t_bm64;
const char* t_bn128;
const char* t_down;
const char* streamed;
const char* resident;
};
const Rule rules[] = { { nullptr, nullptr, nullptr, "<32, 64, 256, 1, false>",
"<32, 64, 128, 4, true>" },
{ "0", nullptr, nullptr, "<64, 64, 256, 1, false>", "<32, 64, 128, 4, true>" },
{ "0", "0", "0", "<64, 128, 128, 1, false>", "<64, 128, 128, 4, true>" } };
static const char* const kEnv[] = {
"RADIANCE_MXFP4_DECODE_EXPERTS", "RADIANCE_MXFP4_ALIGNED_JOBS",
"RADIANCE_MXFP4_PREFILL_N32", "RADIANCE_MXFP4_PREFILL_M32",
"RADIANCE_MXFP4_PREFILL_N64", "RADIANCE_MXFP4_PREFILL_CACHE_ROWS"
5,
staged },
{ "GLM TP4 down, the GLM prefill form",
4096,
512,
4096,
512,
1,
8,
true,
true,
true,
glm_runs({ 63, 96, 1, 130, 32, 31, 33, 64, 65 }),
13,
"moe_mxfp4_prefill_kernel" },
{ "GLM TP8 gate_up, P64 BM32 BN64 K256",
512,
4096,
512,
4096,
2,
8,
false,
true,
false,
glm_runs({ 200, 33, 300, 31, 200 }),
20,
"(moe_mxfp4_p64_kernel<32, 64, 256, 1, false>)" },
{ "GLM TP8 down, P64 BM32 BN64 K128 x4, A resident",
4096,
256,
4096,
256,
1,
8,
true,
true,
true,
glm_runs({ 900, 801, 779, 700, 700, 581, 600, 600, 63, 1 }),
40,
"(moe_mxfp4_p64_kernel<32, 64, 128, 4, true>)" },
};
for (const Case& c : all) {
@@ -264,21 +239,37 @@ int main() {
// A dropped slot about every eleventh row, the first rows of some runs included.
sorted[r] = (r * 13 + e) % 11 == 0 ? -1 : (int)(rng() % (uint32_t)T);
}
/* Stacked planes hold every expert; a pointer table names one of at most three slices a
* class, so GLM's 288 experts stay small. */
const int n[2] = { c.N, c.N_odd }, k[2] = { c.K, c.K_odd },
nexp[2] = { (E + 1) / 2, E / 2 };
std::vector<uint8_t> w[2], s[2];
std::vector<const void*> wt[2], st[2];
std::vector<std::vector<float>> wf[2];
int slices[2];
for (int q = 0; q < 2; ++q) {
w[q].resize((size_t)nexp[q] * n[q] * k[q] / 2);
s[q].resize((size_t)nexp[q] * n[q] * k[q] / 32);
slices[q] = c.tables ? std::min(nexp[q], 3) : nexp[q];
w[q].resize((size_t)slices[q] * n[q] * k[q] / 2);
s[q].resize((size_t)slices[q] * n[q] * k[q] / 32);
for (auto& v : w[q]) v = (uint8_t)rng();
static const uint8_t special[] = { 0, 255, 1, 254, 127 };
for (size_t i = 0; i < s[q].size(); ++i)
s[q][i] = c.exceptional && rng() % 9 == 0 ? special[rng() % 5]
: (uint8_t)(118 + rng() % 16);
for (int x = 0; x < nexp[q]; ++x) {
wt[q].push_back(w[q].data() + (size_t)x * n[q] * k[q] / 2);
st[q].push_back(s[q].data() + (size_t)x * n[q] * k[q] / 32);
wt[q].push_back(w[q].data() + (size_t)(x % slices[q]) * n[q] * k[q] / 2);
st[q].push_back(s[q].data() + (size_t)(x % slices[q]) * n[q] * k[q] / 32);
}
/* each slice's codes as the E4M3 values the table decode gives */
for (int x = 0; x < slices[q]; ++x) {
std::vector<float> f((size_t)n[q] * k[q]);
const uint8_t* src = w[q].data() + (size_t)x * n[q] * k[q] / 2;
for (size_t i = 0; i < f.size(); ++i) {
uint32_t lo, hi;
r4d_mxfp4_unpack8_exact((src[i / 2] >> (4 * (i & 1))) & 15u, &lo, &hi);
f[i] = rad_fp8e4m3_to_f32((uint8_t)(lo & 0xffu));
}
wf[q].push_back(std::move(f));
}
}
auto plane = [&](int q, bool scale) {
@@ -291,6 +282,8 @@ int main() {
};
/* the per-element sequence, on the host */
std::vector<float> af(a.size());
for (size_t i = 0; i < a.size(); ++i) af[i] = rad_fp8e4m3_to_f32(a[i]);
std::vector<uint16_t> ref((size_t)T * yp, 0x7777);
for (int r = 0; r < T; ++r)
for (int col = 0; col < OUT; ++col) {
@@ -301,21 +294,15 @@ int main() {
const int pw = OUT / c.parts, cw = n[q] / c.parts, within = col % pw;
const bool valid = within < cw;
const int wn = (col / pw) * cw + within;
const uint8_t* wr =
w[q].data() + (size_t)(e / 2) * n[q] * k[q] / 2 + (size_t)wn * k[q] / 2;
const uint8_t* sr =
s[q].data() + (size_t)(e / 2) * n[q] * k[q] / 32 + (size_t)wn * k[q] / 32;
const float* wr = wf[q][(size_t)((e / 2) % slices[q])].data() + (size_t)wn * k[q];
const uint8_t* sr = (const uint8_t*)st[q][(size_t)(e / 2)] + (size_t)wn * k[q] / 32;
const float* arow = af.data() + (size_t)ar * ap;
float total = 0.f;
for (int kb = 0; kb < k[q]; kb += 128)
for (int g = 0; g < 4; ++g) {
float raw = 0.f;
for (int kk = kb + g * 32; kk < kb + g * 32 + 32; ++kk) {
const uint32_t code = valid ? (wr[kk / 2] >> (4 * (kk & 1))) & 15u : 0u;
uint32_t lo, hi;
r4d_mxfp4_unpack8_exact(code, &lo, &hi);
raw += rad_fp8e4m3_to_f32(a[(size_t)ar * ap + kk]) *
rad_fp8e4m3_to_f32((uint8_t)(lo & 0xffu));
}
for (int kk = kb + g * 32; kk < kb + g * 32 + 32; ++kk)
raw += arow[kk] * (valid ? wr[kk] : 0.f);
const uint32_t x = valid ? sr[kb / 32 + g] : 127u;
uint32_t bits = x == 255 ? 0x7fc00000u : x ? x << 23 : 0x00400000u;
float scale;
@@ -329,6 +316,12 @@ int main() {
out = (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >>
16); // f32_to_bf16, r4d_common.h
}
int live = 0;
for (uint16_t v : ref) live += v != 0 && v != 0x7777;
if (live < T * OUT / 3) {
std::printf("FAIL %s: output mostly zero\n", c.name);
++fails;
}
RadTensor t[9] = { ten(a.data(), RAD_F8E4M3, { Arows, NK }, { ap, 1 }),
ten(as.data(), RAD_F32, { Arows, NK / 128 }, { asp, 1 }),
@@ -365,118 +358,25 @@ int main() {
p[8] = pi("group", 128);
p[9] = ps("a_order", c.sorted_a ? "sorted" : "token");
p[10] = ps("dtype", "mxfp4a8");
auto run = [&](const Form& f, std::vector<uint16_t>& y) {
y.assign((size_t)T * yp, 0x7777);
t[8] = ten(y.data(), RAD_BF16, { T, OUT }, { yp, 1 });
for (int i = 0; i < 6; ++i) setenv(kEnv[i], f.env[i] == '1' ? "1" : "0", 1);
if (f.stage) {
setenv("RADIANCE_MXFP4_EXPERTS_STAGE_K", std::to_string(f.stage).c_str(), 1);
setenv("RADIANCE_MXFP4_EXPERTS_COLTILES", std::to_string(f.tiles).c_str(), 1);
}
setenv("RADIANCE_MXFP4_LUT", f.lut ? "1" : "0", 1);
setenv("RADIANCE_MXFP4_PREFILL_PIPE", f.pipe ? "1" : "0", 1);
RadArgs ar{};
ar.t = t;
ar.n_t = 9;
ar.p = p;
ar.n_p = 11;
emu_last_kernel = "";
return r4d_rad_moe_mxfp4(&ar, nullptr);
};
auto run_p64 = [&](const P64& f, std::vector<uint16_t>& y) {
const int v[8] = { 1, 0, 0, f.bm, f.bn, f.sk, f.ct, f.out };
for (int i = 0; i < 8; ++i) setenv(kP64[i], std::to_string(v[i]).c_str(), 1);
setenv(kP64[11], f.db ? "1" : "0", 1);
const Form base = { "000000", 0, 0, 1, "", false };
const int rc = run(base, y);
setenv(kP64[0], "0", 1);
return rc;
};
const Form alt = { "000000", 0, 0, 0, "moe_mxfp4_fp8_kernel<1, 1, false, false>", false };
std::vector<uint16_t> legacy, got;
std::vector<uint16_t> got((size_t)T * yp, 0x7777);
t[8] = ten(got.data(), RAD_BF16, { T, OUT }, { yp, 1 });
RadArgs ar{};
ar.t = t;
ar.n_t = 9;
ar.p = p;
ar.n_p = 11;
emu_last_kernel = "";
++cases;
int rc = run(alt, legacy);
if (rc != RAD_OK || !std::strstr(emu_last_kernel, alt.kernel)) {
std::printf("FAIL %s: reference form rc %d kernel %s\n", c.name, rc, emu_last_kernel);
const int rc = r4d_rad_moe_mxfp4(&ar, nullptr);
const size_t i = first_diff(got, ref);
if (rc != RAD_OK || std::strcmp(emu_last_kernel, c.kernel) || i != got.size()) {
std::printf("FAIL %s (T %d): ran %s (want %s), rc %d, first word %zu (row %zu col %zu: "
"%04x, want %04x)\n",
c.name, T, emu_last_kernel, c.kernel, rc, i, i / yp, i % yp,
i < got.size() ? got[i] : 0, i < ref.size() ? ref[i] : 0);
++fails;
continue;
}
if (!same16(legacy, ref)) {
const size_t i = first_diff(legacy, ref);
std::printf("FAIL %s: reference form against the host sequence at row %zu col %zu: "
"%04x, want %04x\n",
c.name, i / yp, i % yp, legacy[i], ref[i]);
++fails;
}
int live = 0;
for (uint16_t v : ref) live += v != 0 && v != 0x7777;
if (live < T * OUT / 3) {
std::printf("FAIL %s: output mostly zero\n", c.name);
++fails;
}
for (const Form& f : forms) {
if (f.prefill && T < 512) continue;
++cases;
rc = run(f, got);
const bool reached = std::strstr(emu_last_kernel, f.kernel) != nullptr;
if (rc != RAD_OK || !reached || !same16(got, legacy)) {
const size_t i = first_diff(got, legacy);
std::printf(
"FAIL %s: form %s stage %d tiles %d lut %d ran %s (want %s), rc %d, first "
"word %zu\n",
c.name, f.env, f.stage, f.tiles, f.lut, emu_last_kernel, f.kernel, rc, i);
++fails;
}
}
const bool resident = NK <= 256;
for (const Rule& u : rules) {
++cases;
for (const char* v : kP64) unsetenv(v);
setenv(kP64[0], "1", 1);
setenv(kP64[1], "0", 1);
setenv(kP64[2], "0", 1);
if (u.t_bm64) setenv(kP64[8], u.t_bm64, 1);
if (u.t_bn128) setenv(kP64[9], u.t_bn128, 1);
if (u.t_down) setenv(kP64[10], u.t_down, 1);
const Form base = { "000000", 0, 0, 1, "", false };
rc = run(base, got);
for (const char* v : kP64) unsetenv(v);
const std::string want =
std::string("moe_mxfp4_p64_kernel") + (resident ? u.resident : u.streamed);
if (rc != RAD_OK || !std::strstr(emu_last_kernel, want.c_str()) ||
!same16(got, legacy)) {
std::printf("FAIL %s: P64 rule ran %s (want %s), rc %d, first word %zu\n", c.name,
emu_last_kernel, want.c_str(), rc, first_diff(got, legacy));
++fails;
}
}
for (const P64& f : p64s) {
++cases;
rc = run_p64(f, got);
const bool db = f.db && !resident && (f.sk == 128 || (f.bm == 32 && f.bn == 64));
const std::string want = "moe_mxfp4_p64_kernel<" + std::to_string(f.bm) + ", " +
std::to_string(f.bn) + ", " + std::to_string(f.sk) + ", " +
(resident ? std::to_string(f.ct) : "1") + ", " +
(resident ? "true" : "false") + (db ? ", true>" : ">");
const bool reached = std::strstr(emu_last_kernel, want.c_str()) != nullptr;
if (rc != RAD_OK || !reached || !same16(got, legacy)) {
const size_t i = first_diff(got, legacy);
std::printf("FAIL %s: P64 bm %d bn %d stage %d tiles %d out %d db %d ran %s (want "
"%s), rc %d, "
"first word %zu (row %zu col %zu: %04x, want %04x)\n",
c.name, f.bm, f.bn, f.sk, f.ct, f.out, f.db, emu_last_kernel,
want.c_str(), rc, i, i / yp, i % yp, i < got.size() ? got[i] : 0,
i < legacy.size() ? legacy[i] : 0);
++fails;
}
}
}
for (const char* v : kEnv) unsetenv(v);
for (const char* v : kP64) unsetenv(v);
unsetenv("RADIANCE_MXFP4_EXPERTS_STAGE_K");
unsetenv("RADIANCE_MXFP4_EXPERTS_COLTILES");
unsetenv("RADIANCE_MXFP4_LUT");
unsetenv("RADIANCE_MXFP4_PREFILL_PIPE");
std::printf("%d case(s), %d failure(s)\n", cases, fails);
return fails ? 1 : 0;
}