okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-q4-metal (ed1f58d27): the Mac (Metal) engine follows the compiled geometry (q4_lite/q4_mid/q4_big kernel parity 40/40, gate 2 like CUDA), plus the PLE row-buffer fix in src/program/generate.cpp (sized by the compiled hidden size, not 2560; it overran 24 KB at hidden 4096 and crashed Metal serve); default build unchanged (CUDA SASS 3573/3573 and 160/160 host objects, HIP gfx1201/gfx1151 string offsets only, SYCL 129/129 objects and 22/22 checksums, Metal per-token functions identical, UD-Q4_K_XL 10/10 tokens, CUDA IQ3_S 26/26 tokens) (Qwen4 prep)7f46672a4

main
3.9 KiB92 linesraw
1// metal/tests/route_parity.cpp - moe_route (softmax + top-10 + the expert plan) against a CPU replica of
2// 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.
3#include "mtl_rt.h"
4#include "ref_quant.h"
5#include "engine/geo.h"
6
7#include <algorithm>
8#include <cfloat>
9#include <cmath>
10#include <cstdio>
11#include <cstring>
12#include <set>
13#include <vector>
14
15namespace {
16constexpr 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
17constexpr int P_NG = 0, P_MAXE = 128, P_GEXP = 16, P_GSTART = P_GEXP + P_MAXE, P_ETOK = P_GSTART + P_MAXE + 4,
18              P_EW = P_ETOK + P_MAXE, P_TOKENT = P_EW + P_MAXE, P_INTS = P_TOKENT + P_MAXE;
19struct MoeArgs {
20    uint64_t gate_off, up_off, down_off, gu_stride, d_stride;
21    uint32_t gu_row, d_row, n_embd, n_ff, n_tok, topk, n_expert, gx_gu, gx_dn, pad;
22};
23}  // namespace
24
25int main() {
26    mtl::Context cx;
27    const int lib = cx.compile({"quant.metal", "units.metal", "moe.metal"}, strata_metal::geo::prelude());
28    mtl::Pipeline p = cx.pipeline(lib, "moe_route");
29    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);
30    ref::Rng r(9);
31    int fails = 0;
32    double worst_w = 0;
33    for (int trial = 0; trial < 200; ++trial) {
34        const int nt = 1 + trial % 8;
35        for (int i = 0; i < nt * 512; ++i) {
36            float v = r.sym() * 4.0f;
37            if (trial % 3 == 0) v = std::round(v * 4.0f) / 4.0f;   // many exact ties
38            lg.as<float>()[i] = v;
39        }
40        MoeArgs a{};
41        a.n_tok = nt;
42        a.topk = TK;
43        a.n_expert = NE;
44        a.gx_gu = 1;
45        a.gx_dn = 1;
46        mtl::Dispatch d;
47        d.p = &p;
48        d.args = {mtl::Arg::B(lg), mtl::Arg::B(ids), mtl::Arg::B(w), mtl::Arg::B(plan), mtl::Arg::V(a)};
49        d.tg[0] = 512;
50        cx.run({d});
51        for (int t = 0; t < nt; ++t) {
52            const float* l = lg.as<float>() + t * 512;
53            float mx = -INFINITY;
54            for (int e = 0; e < NE; ++e) mx = std::max(mx, l[e]);
55            std::vector<double> pr(NE);
56            double s = 0;
57            for (int e = 0; e < NE; ++e) s += (pr[e] = std::exp((double)l[e] - mx));
58            for (int e = 0; e < NE; ++e) pr[e] /= s;
59            // top 10: largest probability, ties -> lower index (probabilities of equal logits are equal)
60            std::vector<int> order(NE);
61            for (int e = 0; e < NE; ++e) order[e] = e;
62            std::stable_sort(order.begin(), order.end(), [&](int x, int y) { return l[x] > l[y]; });
63            double ssum = 0;
64            for (int k = 0; k < TK; ++k) ssum += pr[order[k]];
65            for (int k = 0; k < TK; ++k) {
66                if (ids.as<int>()[t * TK + k] != order[k]) { ++fails; break; }
67                const double ref = pr[order[k]] / std::max(ssum, 6.103515625e-5);
68                worst_w = std::max(worst_w, std::fabs(w.as<float>()[t * TK + k] - ref) / ref);
69            }
70        }
71        // the plan
72        const int* P = plan.as<int>();
73        std::set<int> distinct(ids.as<int>(), ids.as<int>() + nt * TK);
74        bool ok = P[P_NG] == (int)distinct.size();
75        int gi = 0;
76        for (int e : distinct) {
77            if (P[P_GEXP + gi] != e) ok = false;
78            int j = P[P_GSTART + gi];
79            for (int i = 0; i < nt * TK; ++i)
80                if (ids.as<int>()[i] == e) {
81                    if (P[P_ETOK + j] != i / TK || P[P_TOKENT + i] != j) ok = false;
82                    ++j;
83                }
84            if (j != P[P_GSTART + gi + 1]) ok = false;
85            ++gi;
86        }
87        fails += !ok;
88    }
89    std::printf("route parity, 200 windows of 1..8 tokens (a third with exact ties): ids/plan mismatches %d, worst weight "
90                "rel err %.2e  %s\n", fails, worst_w, fails == 0 && worst_w < 2e-6 ? "PASS" : "FAIL");
91    return fails ? 1 : 0;
92}