Files
Strata/metal/tests/route_parity.cpp
T
Niko1221 943e67ea98 Metal tests: the parity programs take their shape from the compiled geometry
gr, gdn, gdn_prompt, qsa, qsa_prompt, route, prompt, expert and moe_mm parity (and gemv_parity --nin) read hidden, expert width, hc_lr,
head counts, GDN heads, top-k and expert count from engine/geo.h instead of 2560 / 640 / 320 / 24 / 48 / 10 / 512, and compile their
kernel libraries with geo::prelude(); qsa_parity dispatches the qsa_prep groups the model has (QH + 2 QKV + IH + 1, was a fixed 33).
Built with -DSTRATA_GEOMETRY_FILE=<header> they are the kernel parity of that shape; without it they are what they were.
2026-11-08 09:04:39 +01:00

93 lines
3.9 KiB
C++

// metal/tests/route_parity.cpp - moe_route (softmax + top-10 + the expert plan) against a CPU replica of
// native_router.cu's rule, for 1..8 tokens; logits with deliberate ties. Experts and experts per token: the shape this build is compiled for.
#include "mtl_rt.h"
#include "ref_quant.h"
#include "engine/geo.h"
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
namespace {
constexpr int TK = strata_metal::geo::TOPK, NE = strata_metal::geo::NEXP; // the shape this build is compiled for (engine/geo.h); the kernel's logits row stride is 512
constexpr int P_NG = 0, P_MAXE = 128, P_GEXP = 16, P_GSTART = P_GEXP + P_MAXE, P_ETOK = P_GSTART + P_MAXE + 4,
P_EW = P_ETOK + P_MAXE, P_TOKENT = P_EW + P_MAXE, P_INTS = P_TOKENT + P_MAXE;
struct MoeArgs {
uint64_t gate_off, up_off, down_off, gu_stride, d_stride;
uint32_t gu_row, d_row, n_embd, n_ff, n_tok, topk, n_expert, gx_gu, gx_dn, pad;
};
} // namespace
int main() {
mtl::Context cx;
const int lib = cx.compile({"quant.metal", "units.metal", "moe.metal"}, strata_metal::geo::prelude());
mtl::Pipeline p = cx.pipeline(lib, "moe_route");
mtl::Buffer lg = cx.alloc(4 * 8 * 512), ids = cx.alloc(4 * 8 * TK), w = cx.alloc(4 * 8 * TK), plan = cx.alloc(4 * P_INTS);
ref::Rng r(9);
int fails = 0;
double worst_w = 0;
for (int trial = 0; trial < 200; ++trial) {
const int nt = 1 + trial % 8;
for (int i = 0; i < nt * 512; ++i) {
float v = r.sym() * 4.0f;
if (trial % 3 == 0) v = std::round(v * 4.0f) / 4.0f; // many exact ties
lg.as<float>()[i] = v;
}
MoeArgs a{};
a.n_tok = nt;
a.topk = TK;
a.n_expert = NE;
a.gx_gu = 1;
a.gx_dn = 1;
mtl::Dispatch d;
d.p = &p;
d.args = {mtl::Arg::B(lg), mtl::Arg::B(ids), mtl::Arg::B(w), mtl::Arg::B(plan), mtl::Arg::V(a)};
d.tg[0] = 512;
cx.run({d});
for (int t = 0; t < nt; ++t) {
const float* l = lg.as<float>() + t * 512;
float mx = -INFINITY;
for (int e = 0; e < NE; ++e) mx = std::max(mx, l[e]);
std::vector<double> pr(NE);
double s = 0;
for (int e = 0; e < NE; ++e) s += (pr[e] = std::exp((double)l[e] - mx));
for (int e = 0; e < NE; ++e) pr[e] /= s;
// top 10: largest probability, ties -> lower index (probabilities of equal logits are equal)
std::vector<int> order(NE);
for (int e = 0; e < NE; ++e) order[e] = e;
std::stable_sort(order.begin(), order.end(), [&](int x, int y) { return l[x] > l[y]; });
double ssum = 0;
for (int k = 0; k < TK; ++k) ssum += pr[order[k]];
for (int k = 0; k < TK; ++k) {
if (ids.as<int>()[t * TK + k] != order[k]) { ++fails; break; }
const double ref = pr[order[k]] / std::max(ssum, 6.103515625e-5);
worst_w = std::max(worst_w, std::fabs(w.as<float>()[t * TK + k] - ref) / ref);
}
}
// the plan
const int* P = plan.as<int>();
std::set<int> distinct(ids.as<int>(), ids.as<int>() + nt * TK);
bool ok = P[P_NG] == (int)distinct.size();
int gi = 0;
for (int e : distinct) {
if (P[P_GEXP + gi] != e) ok = false;
int j = P[P_GSTART + gi];
for (int i = 0; i < nt * TK; ++i)
if (ids.as<int>()[i] == e) {
if (P[P_ETOK + j] != i / TK || P[P_TOKENT + i] != j) ok = false;
++j;
}
if (j != P[P_GSTART + gi + 1]) ok = false;
++gi;
}
fails += !ok;
}
std::printf("route parity, 200 windows of 1..8 tokens (a third with exact ties): ids/plan mismatches %d, worst weight "
"rel err %.2e %s\n", fails, worst_w, fails == 0 && worst_w < 2e-6 ? "PASS" : "FAIL");
return fails ? 1 : 0;
}