mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
repository paths inside tests.
engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.
No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
93 lines
3.9 KiB
C++
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;
|
|
}
|