libhalo selftest: rope_rows and rmsnorm_rows give rope's and rmsnorm's bytes

At LFM3's shapes: the per-head norm over head_dim-wide rows, and the full
layers' partial rotary (64 of 128 at theta 1e6 on the head ladder) with
positions past 250000.
This commit is contained in:
DokterSmol authored and StillDeadcode committed 2026-11-17 18:12:32 +01:00
1 parent 1f00224501
commit 09ad42e633
1 file changed
+66
+66
View File
@@ -6935,6 +6935,70 @@ static void scale_case(const Plugin& pl) {
/* rope_bf16 against an f32 mirror of rope_kernel at each ladder width. `den` is the ladder's
* width (`rot` for `rotary`/absent, `head_dim` for `head`). The V head must stay untouched. rel_l2
* catches a wrong angle, which magnitude alone would not (rotation preserves length). */
/* The row-form names (rope_rows, rmsnorm_rows) against rope and rmsnorm: the same launch on this
* part, so the same bytes. LFM3's shapes: a per-head norm over head_dim-wide rows with a bf16
* gain, and its full layers' rotary, half of each 128-wide head at theta 1e6 on the head
* ladder, over 16 query heads. Each row must also have changed its input. */
static void row_form_name_case(const Plugin& pl, bool rope) {
const char* base = rope ? "rope_bf16" : "rmsnorm_bf16";
const char* rows = rope ? "rope_rows_bf16" : "rmsnorm_rows_bf16";
const std::string what = std::string(rows) + " gives " + base + "'s bytes";
const RadKernelInfo* r0 = find(pl, base);
const RadKernelInfo* r1 = find(pl, rows);
if (!r0 || !r1) { ok(what.c_str(), false, "row absent"); return; }
const int64_t M = rope ? 96 : 96 * 16, hd = 128, n_head = 16, n = rope ? n_head * hd : hd;
std::vector<uint16_t> x((size_t)(M * n));
for (size_t i = 0; i < x.size(); ++i)
x[i] = h_to_bf16((float)((int)((i * 2654435761u >> 7) % 401) - 200) * 0.01f);
std::vector<uint16_t> w((size_t)hd);
for (size_t i = 0; i < w.size(); ++i) w[i] = h_to_bf16(0.5f + (float)(i % 7) * 0.25f);
std::vector<int32_t> pos((size_t)M);
for (size_t i = 0; i < pos.size(); ++i) pos[i] = (int32_t)(i * 2731 + (i % 3 ? 0 : 250000));
void* d_x = dalloc(x.size() * 2);
void* d_w = dalloc(w.size() * 2);
void* d_pos = dalloc(pos.size() * 4);
void* d_y = dalloc(x.size() * 2);
if (!d_x || !d_w || !d_pos || !d_y) { ok(what.c_str(), false, "hipMalloc failed"); return; }
st_memcpy(d_w, w.data(), w.size() * 2, hipMemcpyHostToDevice);
st_memcpy(d_pos, pos.data(), pos.size() * 4, hipMemcpyHostToDevice);
auto run = [&](const RadKernelInfo* r, std::vector<uint16_t>& out) {
std::vector<RadTensor> ts;
std::vector<RadParam> ps;
if (rope) {
st_memcpy(d_y, x.data(), x.size() * 2, hipMemcpyHostToDevice); /* in place */
ts = { T(d_y, RAD_BF16, { M, n }), T(d_pos, RAD_I32, { M }) };
ps = { PI("M", M), PI("head_dim", hd), PI("n_head", n_head), PI("n_head_kv", 0),
PF("theta", 1e6), PF("scale", 1.0), PS("mode", "neox"), PI("rotary_dim", 64),
PS("ladder", "head") };
} else {
st_memcpy(d_x, x.data(), x.size() * 2, hipMemcpyHostToDevice);
ts = { T(d_x, RAD_BF16, { M, n }), T(d_w, RAD_BF16, { n }), T(d_y, RAD_BF16, { M, n }) };
ps = { PI("M", M), PI("n", n), PF("eps", 1e-5), PS("dtype", "bf16") };
}
RadArgs a;
std::memset(&a, 0, sizeof(a));
a.t = ts.data(); a.n_t = (int)ts.size();
a.p = ps.data(); a.n_p = (int)ps.size();
const int rc = r->launch(&a, nullptr);
st_stream_sync(nullptr);
if (rc != RAD_OK) return rc;
out.assign(x.size(), 0);
st_memcpy(out.data(), d_y, out.size() * 2, hipMemcpyDeviceToHost);
return 0;
};
std::vector<uint16_t> a, b;
const int rc0 = run(r0, a), rc1 = run(r1, b);
if (rc0 || rc1) {
ok(what.c_str(), false, "launch rc " + std::to_string(rc0) + " / " + std::to_string(rc1));
return;
}
size_t diff = 0, moved = 0;
for (size_t i = 0; i < a.size(); ++i) { diff += a[i] != b[i]; moved += b[i] != x[i]; }
ok(what.c_str(), diff == 0 && moved > a.size() / 8,
std::to_string(diff) + " of " + std::to_string(a.size()) + " differ, " +
std::to_string(moved) + " changed from the input");
}
static void rope_ladder_case(const Plugin& pl) {
const char* what = "rope ladder vs an f32 mirror of rope_kernel at each width";
const RadKernelInfo* r = find(pl, "rope_bf16");
@@ -32838,6 +32902,8 @@ static void device_phase(const Plugin& pl) {
if (!g_case) std::printf("\nthe rotary ladder, against an f32 mirror of the kernel at each width\n");
rope_ladder_case(pl);
}
if (sel("rope_rows_bf16")) row_form_name_case(pl, true);
if (sel("rmsnorm_rows_bf16")) row_form_name_case(pl, false);
/* The audio generator's ops and the optional forms a speech model adds, each against libref's
* arithmetic. */