mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-06 08:18:54 +00:00
The adaptive warm tier (default with in-place reads) takes the coldest RAM slots as spares and relies on the SSD for them; in Mode B it makes those experts unreachable. The driver sets STRATA_L32_ADAPT=0 in Mode B (--adapt 1 forces it). l32sy_spec_legs.py runs every leg of the spec-span test without stopping at the first difference.
252 lines
14 KiB
C++
252 lines
14 KiB
C++
// tests/ling32/ling32_sy_tier_check.cpp - the Ling tier store (Mode B / C) driven without the engine's kernels, for the SYCL (Intel) build.
|
|
//
|
|
// ling32_sy_tier_check FILE.strata [--vr N] [--rr N] [--dyn N] [--ring N] [--fwds N] [--zc 0|1] [--pf 0|1] [--seed S] [--tokens a,b,c] [--noisy 0|1]
|
|
//
|
|
// What it does: plans a split by hand (ranks on the card / in pinned RAM, dynamic slots, SSD ring: the same forced-split rules as `strata-ling32 --force-*`), creates the ExpertStore (the start-up fill, the
|
|
// verification reads, pinned allocations, DirectFile reads), and then plays routing like the engine does: per forward and MoE layer prepare() / next_pass() with random top-8 sets, lookahead predictions
|
|
// (perfect or noisy) for the layers ahead. Two checks run on every table the store hands out:
|
|
// host side STRATA_L32_TIER_CHECK=1 (set by this program): the store compares every slab of the pass with the file bytes after its copies (`tier store: ... compared N slab reads, M wrong`)
|
|
// device side a SYCL kernel dereferences the table entries on the compute stream exactly as the MoE kernels do (in-place reads of pinned RAM and ring memory included) and compares the slab
|
|
// bytes with a device copy of Section 4
|
|
// Exit code 0 = both checks found no wrong slab. The fixture's Section 4 is small; the real file is too big for the mirror (the host check is off above 256 MiB).
|
|
#include "ling32_model.hpp"
|
|
#include "ling32_tier_store.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace strata::ling32;
|
|
|
|
namespace {
|
|
double now_s() { return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
|
|
|
|
struct Verifier {
|
|
uint8_t* d_mirror = nullptr;
|
|
uint64_t* d_off = nullptr;
|
|
uint32_t* d_bytes = nullptr;
|
|
unsigned long long* d_cnt = nullptr; // [0] slabs checked, [1] wrong bytes, [2] slabs with a wrong byte
|
|
int E = 0, first_k = 0;
|
|
bool init(const Ling32File& f, const GpuModel& M, std::string& err) {
|
|
const uint64_t n = f.section_bytes(4);
|
|
if (n > (1ull << 30)) { err = "Section 4 is too big for the device-side mirror"; return false; }
|
|
std::vector<uint8_t> mir(n);
|
|
if (!f.reader().read_at(f.section_offset(4), mir.data(), n, err)) return false;
|
|
std::vector<uint64_t> off(M.experts.size());
|
|
std::vector<uint32_t> by(M.experts.size());
|
|
for (size_t i = 0; i < M.experts.size(); ++i) { off[i] = M.experts[i].off; by[i] = static_cast<uint32_t>(M.experts[i].body_bytes); }
|
|
E = f.cfg().n_exp;
|
|
first_k = f.cfg().first_k_dense;
|
|
if (cudaMalloc(&d_mirror, n) || cudaMalloc(&d_off, off.size() * 8) || cudaMalloc(&d_bytes, by.size() * 4) || cudaMalloc(&d_cnt, 3 * 8)) { err = "cudaMalloc (verifier)"; return false; }
|
|
cudaMemcpy(d_mirror, mir.data(), n, cudaMemcpyHostToDevice);
|
|
cudaMemcpy(d_off, off.data(), off.size() * 8, cudaMemcpyHostToDevice);
|
|
cudaMemcpy(d_bytes, by.data(), by.size() * 4, cudaMemcpyHostToDevice);
|
|
cudaMemset(d_cnt, 0, 3 * 8);
|
|
return true;
|
|
}
|
|
// compare every slab the table (window of layer il) points to, on the compute stream
|
|
void run(const uint8_t* const* tab, int il, cudaStream_t st) {
|
|
sycl::queue& q = strata::sy::queue_of(st);
|
|
const uint8_t* const* t = tab + static_cast<size_t>(il - first_k) * E;
|
|
const int gbase = (il - first_k) * E;
|
|
uint8_t* mir = d_mirror;
|
|
uint64_t* off = d_off;
|
|
uint32_t* by = d_bytes;
|
|
unsigned long long* cnt = d_cnt;
|
|
constexpr int WG = 128;
|
|
q.parallel_for(sycl::nd_range<1>(static_cast<size_t>(E) * WG, WG), [=](sycl::nd_item<1> it) {
|
|
const int e = static_cast<int>(it.get_group(0));
|
|
const uint8_t* p = t[e];
|
|
if (!p) return;
|
|
const int gi = gbase + e;
|
|
const uint32_t n = by[gi];
|
|
const uint8_t* ref = mir + off[gi];
|
|
unsigned bad = 0;
|
|
for (uint32_t i = static_cast<uint32_t>(it.get_local_id(0)); i < n; i += WG) bad += p[i] != ref[i];
|
|
const unsigned tot = sycl::reduce_over_group(it.get_group(), bad, sycl::plus<unsigned>());
|
|
if (it.get_local_id(0) == 0) {
|
|
sycl::atomic_ref<unsigned long long, sycl::memory_order::relaxed, sycl::memory_scope::device> a0(cnt[0]), a1(cnt[1]), a2(cnt[2]);
|
|
a0.fetch_add(1);
|
|
if (tot) { a1.fetch_add(tot); a2.fetch_add(1); }
|
|
}
|
|
});
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) { std::printf("usage: ling32_sy_tier_check FILE.strata [--vr N] [--rr N] [--dyn N] [--ring N] [--fwds N] [--zc 0|1] [--pf 0|1] [--seed S] [--tokens a,b,c] [--noisy 0|1]\n"); return 2; }
|
|
const std::string path = argv[1];
|
|
int vr = 0, rr = 0, dyn = 24, ring = 0, fwds = 12, zc = 0, pf = 1, noisy = 1, adapt = -1;
|
|
unsigned seed = 7;
|
|
std::vector<int> tokens = {1, 1, 1, 2, 5, 32, 130, 256};
|
|
for (int i = 2; i + 1 < argc; i += 2) {
|
|
const std::string a = argv[i];
|
|
const std::string v = argv[i + 1];
|
|
if (a == "--vr") vr = std::atoi(v.c_str());
|
|
else if (a == "--rr") rr = std::atoi(v.c_str());
|
|
else if (a == "--dyn") dyn = std::atoi(v.c_str());
|
|
else if (a == "--ring") ring = std::atoi(v.c_str());
|
|
else if (a == "--fwds") fwds = std::atoi(v.c_str());
|
|
else if (a == "--zc") zc = std::atoi(v.c_str());
|
|
else if (a == "--pf") pf = std::atoi(v.c_str());
|
|
else if (a == "--noisy") noisy = std::atoi(v.c_str());
|
|
else if (a == "--adapt") adapt = std::atoi(v.c_str());
|
|
else if (a == "--seed") seed = static_cast<unsigned>(std::atoi(v.c_str()));
|
|
else if (a == "--tokens") {
|
|
tokens.clear();
|
|
size_t p = 0;
|
|
while (p < v.size()) { tokens.push_back(std::atoi(v.c_str() + p)); const size_t c = v.find(',', p); if (c == std::string::npos) break; p = c + 1; }
|
|
} else { std::printf("unknown option %s\n", a.c_str()); return 2; }
|
|
}
|
|
#ifdef _WIN32
|
|
_putenv_s("STRATA_L32_TIER_CHECK", "1");
|
|
#else
|
|
setenv("STRATA_L32_TIER_CHECK", "1", 1);
|
|
#endif
|
|
std::string err;
|
|
Ling32File f;
|
|
if (!f.open(path, err)) { std::printf("open: %s\n", err.c_str()); return 2; }
|
|
const Cfg& c = f.cfg();
|
|
const int E = c.n_exp, top_k = c.top_k, first_k = c.first_k_dense, n_moe = c.n_moe();
|
|
const auto& dir = f.reader().expert_directory();
|
|
RankTable rt;
|
|
if (!build_rank_table(dir.data(), dir.size(), E, rt, err)) { std::printf("rank table: %s\n", err.c_str()); return 2; }
|
|
|
|
// the split, by the rules of ling32_main.cpp make_plan (forced part)
|
|
TierPlan plan;
|
|
plan.ok = true;
|
|
vr = std::min(vr, rt.n_exp);
|
|
rr = std::min(rr, rt.n_exp - vr);
|
|
plan.vram_ranks = vr;
|
|
plan.ram_ranks = rr;
|
|
plan.vram_prefix_bytes = rt.bytes_of_ranks(vr);
|
|
plan.ram_prefix_bytes = rt.bytes_of_ranks(vr + rr) - plan.vram_prefix_bytes;
|
|
plan.ssd_bytes = rt.total - plan.vram_prefix_bytes - plan.ram_prefix_bytes;
|
|
plan.mode = vr >= rt.n_exp ? TierMode::A : plan.ssd_bytes == 0 ? TierMode::B : TierMode::C;
|
|
if (plan.mode == TierMode::A) { std::printf("this split is Mode A: nothing for the store to do\n"); return 2; }
|
|
plan.lookahead_k = 4;
|
|
plan.prefetch = pf != 0;
|
|
plan.prefetch_width = 4;
|
|
plan.io_qd = 16;
|
|
plan.dyn_slots = dyn;
|
|
plan.dyn_big = 0;
|
|
plan.slot_small = 0;
|
|
plan.slot_bytes = rt.max_slab;
|
|
plan.dyn_bytes = static_cast<uint64_t>(dyn) * rt.max_slab;
|
|
plan.ring_slots = plan.mode == TierMode::C ? std::max(ring, 4) : 0;
|
|
plan.ring_bytes = static_cast<uint64_t>(plan.ring_slots) * rt.max_slab;
|
|
std::printf("plan: %s, vram ranks %d (%.3f MiB), ram ranks %d (%.3f MiB), ssd %.3f MiB, dyn slots %d, ring slots %d, slab %llu B max; zero copy %d, prefetch %d\n", tier_name(plan.mode), vr, plan.vram_prefix_bytes / 1048576.0, rr,
|
|
plan.ram_prefix_bytes / 1048576.0, plan.ssd_bytes / 1048576.0, dyn, plan.ring_slots, static_cast<unsigned long long>(rt.max_slab), zc, pf);
|
|
|
|
// the adaptive warm tier (on by default with in-place reads) takes the coldest RAM slots as spares and relies on those experts being on the SSD: with no SSD tier (Mode B) it makes them unreachable
|
|
// ("the dynamic slots / ring are too small for one expert"), also on CUDA; --miss zc is opt-in there. Off by default here in Mode B; --adapt 1 forces it.
|
|
if (adapt < 0 && plan.mode == TierMode::B) adapt = 0;
|
|
if (adapt >= 0) {
|
|
#ifdef _WIN32
|
|
_putenv_s("STRATA_L32_ADAPT", adapt ? "1" : "0");
|
|
#else
|
|
setenv("STRATA_L32_ADAPT", adapt ? "1" : "0", 1);
|
|
#endif
|
|
}
|
|
GpuModel M;
|
|
LoadOptions lo;
|
|
lo.load_experts = false;
|
|
lo.load_lookahead = plan.prefetch;
|
|
lo.lookahead_ks = 2;
|
|
lo.verify_upload = false;
|
|
if (!M.load(f, lo, err)) { std::printf("GpuModel::load: %s\n", err.c_str()); return 1; }
|
|
StoreOptions so;
|
|
so.max_tokens = 256;
|
|
so.zero_copy = zc != 0;
|
|
auto store = ExpertStore::create(f, M, plan, rt, so, err);
|
|
if (!store) { std::printf("ExpertStore::create: %s\n", err.c_str()); return 1; }
|
|
std::printf("store ready: %.3f MiB on the card, %.3f MiB pinned\n", store->gpu_bytes() / 1048576.0, store->pinned_bytes() / 1048576.0);
|
|
|
|
Verifier V;
|
|
if (!V.init(f, M, err)) { std::printf("verifier: %s\n", err.c_str()); return 1; }
|
|
cudaStream_t st = nullptr;
|
|
cudaStreamCreate(&st);
|
|
int* d_ridx = nullptr;
|
|
cudaMalloc(&d_ridx, static_cast<size_t>(so.max_tokens) * top_k * sizeof(int));
|
|
// experts of the better ranks (card / RAM tier) so that the routing mixes hits and misses
|
|
std::vector<int> rank_of(static_cast<size_t>(E), 0), by_rank(static_cast<size_t>(E), 0);
|
|
for (int e = 0; e < E; ++e) rank_of[static_cast<size_t>(e)] = static_cast<int>(dir[static_cast<size_t>(e)].aux_offsets[0]);
|
|
for (int e = 0; e < E; ++e) by_rank[static_cast<size_t>(rank_of[static_cast<size_t>(e)])] = e;
|
|
|
|
std::mt19937 rng(seed);
|
|
auto pick_set = [&](std::vector<int>& out) {
|
|
out.clear();
|
|
while (static_cast<int>(out.size()) < top_k) {
|
|
int e;
|
|
const unsigned r = rng() % 100;
|
|
if (r < 45) e = by_rank[rng() % static_cast<unsigned>(std::max(1, std::min(E, vr + rr)))]; // a card / RAM tier expert
|
|
else if (r < 70) e = by_rank[static_cast<size_t>(E - 1 - static_cast<int>(rng() % static_cast<unsigned>(std::max(1, E - vr - rr))))]; // the coldest (SSD) ones
|
|
else e = static_cast<int>(rng() % static_cast<unsigned>(E));
|
|
if (std::find(out.begin(), out.end(), e) == out.end()) out.push_back(e);
|
|
}
|
|
};
|
|
const double t0 = now_s();
|
|
uint64_t layers = 0, passes_total = 0;
|
|
for (int fw = 0; fw < fwds; ++fw) {
|
|
const int n = std::min(tokens[static_cast<size_t>(fw) % tokens.size()], so.max_tokens);
|
|
std::vector<std::vector<int>> route(static_cast<size_t>(n_moe));
|
|
std::vector<int> one;
|
|
for (int L = 0; L < n_moe; ++L) {
|
|
auto& r = route[static_cast<size_t>(L)];
|
|
r.reserve(static_cast<size_t>(n) * top_k);
|
|
for (int t = 0; t < n; ++t) {
|
|
if (n > 8 && t > 0 && (rng() % 3)) { // a batch: neighbouring tokens share experts, as prompts do
|
|
pick_set(one);
|
|
const auto prev = std::vector<int>(r.end() - top_k, r.end());
|
|
for (int k = 0; k < top_k / 2; ++k) one[static_cast<size_t>(k)] = prev[static_cast<size_t>(k)];
|
|
std::sort(one.begin(), one.end());
|
|
one.erase(std::unique(one.begin(), one.end()), one.end());
|
|
while (static_cast<int>(one.size()) < top_k) { const int e = static_cast<int>(rng() % static_cast<unsigned>(E)); if (std::find(one.begin(), one.end(), e) == one.end()) one.push_back(e); }
|
|
} else {
|
|
pick_set(one);
|
|
}
|
|
r.insert(r.end(), one.begin(), one.end());
|
|
}
|
|
}
|
|
store->begin_forward(n);
|
|
for (int L = 0; L < n_moe; ++L) {
|
|
const int il = first_k + L;
|
|
cudaMemcpy(d_ridx, route[static_cast<size_t>(L)].data(), static_cast<size_t>(n) * top_k * sizeof(int), cudaMemcpyHostToDevice);
|
|
const uint8_t* const* tab = store->prepare(il, n, d_ridx, top_k, st);
|
|
if (!tab) { std::printf("prepare returned no table (forward %d layer %d)\n", fw, il); return 1; }
|
|
V.run(tab, il, st);
|
|
const int np = store->passes();
|
|
for (int j = 1; j < np; ++j) {
|
|
tab = store->next_pass(j, st);
|
|
if (!tab) { std::printf("next_pass %d returned no table\n", j); return 1; }
|
|
V.run(tab, il, st);
|
|
}
|
|
++layers;
|
|
passes_total += static_cast<uint64_t>(np);
|
|
// lookahead predictions for the layer plan.lookahead_k ahead (the ranked lists the engine's predictor would send): the true routing, or with some wrong experts
|
|
const int tl = L + plan.lookahead_k;
|
|
if (plan.prefetch && tl < n_moe && store->wants_prediction(n)) {
|
|
std::vector<int> pr = route[static_cast<size_t>(tl)];
|
|
if (noisy) for (auto& e : pr) if (rng() % 4 == 0) e = static_cast<int>(rng() % static_cast<unsigned>(E));
|
|
store->on_prediction(first_k + tl, plan.lookahead_k, pr.data(), n, top_k);
|
|
}
|
|
cudaStreamSynchronize(st); // the layer's compute (none here) ends
|
|
}
|
|
store->end_forward();
|
|
}
|
|
cudaDeviceSynchronize();
|
|
unsigned long long cnt[3] = {};
|
|
cudaMemcpy(cnt, V.d_cnt, sizeof cnt, cudaMemcpyDeviceToHost);
|
|
std::printf("%d forwards, %llu layers, %llu passes in %.2f s\n%s\n", fwds, static_cast<unsigned long long>(layers), static_cast<unsigned long long>(passes_total), now_s() - t0, store->report().c_str());
|
|
std::printf("device-side check: %llu slabs read through the table by a kernel, %llu wrong (%llu wrong bytes)\n", cnt[0], cnt[2], cnt[1]);
|
|
store.reset(); // prints the host-side check line
|
|
std::printf("%s\n", cnt[2] == 0 && cnt[0] > 0 ? "RESULT PASS device-side" : "RESULT FAIL device-side");
|
|
return cnt[2] == 0 && cnt[0] > 0 ? 0 : 1;
|
|
}
|