Files
Niko1221 06fd2f0700 mgpu: strata_mgpu phase 0, the shared multi-GPU layer's foundation (no engine behaviour change)
New library engine/include/strata/mgpu + engine/src/mgpu, wired in cmake/backends/mgpu.cmake:

- DeviceSet: the topology data model (devices, per-pair links, measured figures), the
  primary-card rule, JSON in and out (schema_version 1), synthetic builders for tests.
- DeviceGuard: RAII current-device switching.
- probe: PCIe link (sysfs / NVML), copy and memory bandwidth, mapped-host access, per pair
  the staged route, peer copy where proven (the #2666 host-bridge rule and an exact data test
  first), and the mapped-flag round trip. CUDA, and HIP through the hip_compat shim.
- strata-topo program and `strata topo [--json]` in the Terminal (new subcommand only).
- Transport interface: peer copy and host-staged implemented, doorbell a stub for P1;
  choose_mode is a pure function of the measured link.
- Placement planner: plan_experts (cost model of one decode window, f = argmin instead of
  STRATA_PEER_HOT; Rows / OrderedPartials / SumPartials; N cards) and plan_layers (the layer
  split's fitted model as a pure function). Unit tests on synthetic pairs and quads.
- terminal/tools/mgpu_identity.py: greedy token-hash identity of a family's goldens, one
  GPU against several.

Nothing in the engine calls any of it yet.
2026-11-30 23:49:37 +01:00

512 lines
28 KiB
C++

// tests/mgpu/placement_test.cpp - the placement planner and the layer-split planner on synthetic topologies. CPU only, no GPU.
//
// The topologies: a symmetric pair behind a host bridge (the 2x RTX A4000 shape: no P2P, Gen4 x16 each), an NVLink pair, an asymmetric pair whose
// second card sits on a Gen4 x2 link (the PR #7310 rig's shape), and four equal cards. The model's numbers are estimates (placement.hpp); these tests
// pin its INVARIANTS (capacity, determinism, ordering, monotonicity) and a few anchors to generate.cpp's fitted constants, not measured speeds.
#include "strata/mgpu/placement.hpp"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
using namespace strata::mgpu;
namespace {
int g_fail = 0;
void check(bool ok, const std::string& what) {
if (ok) return;
std::fprintf(stderr, "FAIL: %s\n", what.c_str());
++g_fail;
}
constexpr uint64_t GiB = 1ull << 30;
// Qwen3.8-Flash-Next IQ3_S shape: 48 layers, 512 experts, top-10, about 2 MB a blob (49 GiB of experts)
ExpertGeom qfn() {
ExpertGeom g;
g.n_layers = 48;
g.n_experts = 512;
g.top_k = 10;
g.layer_blob_bytes = {2'060'000};
return g;
}
// a quarter-size geometry for the many small scenarios (24 layers x 128 experts, top-8, 4 MiB blobs: 12 GiB)
ExpertGeom small() {
ExpertGeom g;
g.n_layers = 24;
g.n_experts = 128;
g.top_k = 8;
g.layer_blob_bytes = {4ull << 20};
return g;
}
SyntheticDevice a4000() {
SyntheticDevice d;
d.name = "RTX A4000";
d.sms = 48;
d.clock_ghz = 1.56;
d.vram_total = 16 * GiB;
d.mem_bw_gbps = 380.0;
d.h2d_gbps = d.d2h_gbps = 24.0;
d.h2d_us = d.d2h_us = 12.0;
return d;
}
Workload qfn_window() {
Workload w;
w.window_rows = 8;
w.hidden = 2560;
w.dense_bytes = 3500ull << 20;
return w;
}
PlanInput make(const ExpertGeom& g, DeviceSet d, std::vector<uint64_t> budgets, const Workload& w, uint32_t seed = 1) {
PlanInput in;
in.geom = g;
in.hot = synthetic_ranking(g, seed);
in.devices = std::move(d);
in.budget_bytes = std::move(budgets);
in.wl = w;
return in;
}
// invariants that hold for every plan
void invariants(const PlanInput& in, const Placement& p, const std::string& tag) {
check(p.ok, tag + ": the plan is ok: " + p.why);
if (!p.ok) return;
const size_t N = (size_t) in.geom.n_layers * (size_t) in.geom.n_experts;
check(p.tier_of.size() == N, tag + ": one tier per pair");
uint64_t bytes_by_tier[16] = {0};
bool range_ok = true;
for (size_t id = 0; id < N; ++id) {
if (p.tier_of[id] >= p.tiers.size()) { range_ok = false; break; }
bytes_by_tier[p.tier_of[id]] += in.geom.blob((int) (id / (size_t) in.geom.n_experts), (int) (id % (size_t) in.geom.n_experts));
}
check(range_ok, tag + ": every tier index is valid");
if (!range_ok) return;
uint64_t total = 0, gpu = 0;
size_t gi = 0;
for (size_t t = 0; t < p.tiers.size(); ++t) {
total += bytes_by_tier[t];
if (p.tiers[t].kind == Tier::Primary || p.tiers[t].kind == Tier::Peer) {
const uint64_t budget = in.budget_bytes[(size_t) p.tiers[t].device];
check(bytes_by_tier[t] <= budget, tag + ": GPU tier " + std::to_string(t) + " stays inside its budget");
check(gi < p.loads.size() && p.loads[gi].bytes == bytes_by_tier[t] && p.loads[gi].device == p.tiers[t].device, tag + ": the load table matches the tiers");
gpu += bytes_by_tier[t];
++gi;
} else if (p.tiers[t].kind == Tier::PinnedRam) {
check(bytes_by_tier[t] == p.ram_bytes && bytes_by_tier[t] <= in.host_ram_budget, tag + ": RAM bytes add up and respect the RAM budget");
} else {
check(bytes_by_tier[t] == p.ssd_bytes, tag + ": SSD bytes add up");
}
}
check(total == in.geom.total_bytes(), tag + ": every byte of experts has a home");
check(p.tiers[0].kind == Tier::Primary, tag + ": tier 0 is the primary");
double s = 0;
for (double f : p.share) { s += f; check(f >= 0.0 && f <= 1.0, tag + ": each share is a fraction"); }
check(s <= 1.0 + 1e-9, tag + ": the shares leave a non-negative weight for the primary");
check(p.est_window_ms > 0.0 && p.est_window_ms_primary_only > 0.0, tag + ": predicted times are positive");
check(p.gpu_mass >= 0.0 && p.gpu_mass <= 1.0 + 1e-9, tag + ": GPU mass is a fraction");
check(!p.why.empty(), tag + ": the plan explains itself");
// determinism: the same input twice, the same plan
const Placement q = plan_experts(in);
check(q.tier_of == p.tier_of && q.est_window_ms == p.est_window_ms && q.share == p.share, tag + ": planning twice gives the same plan");
}
} // namespace
int main() {
// ---- geometry and ranking helpers
{
ExpertGeom g = small();
std::string err;
check(g.valid(err), "the small geometry is valid");
check(g.total_bytes() == 24ull * 128 * (4ull << 20), "total bytes");
ExpertGeom bad = g;
bad.top_k = 0;
check(!bad.valid(err) && !err.empty(), "top_k = 0 is refused");
bad = g;
bad.top_k = 200;
check(!bad.valid(err), "top_k above n_experts is refused");
bad = g;
bad.layer_blob_bytes.clear();
check(!bad.valid(err), "no byte table is refused");
bad = g;
bad.layer_blob_bytes = {1, 2, 3};
check(!bad.valid(err), "a layer table of the wrong length is refused");
ExpertGeom o = g;
o.expert_bytes.assign((size_t) g.n_layers * g.n_experts, 0);
o.expert_bytes[5] = 123;
check(o.valid(err) && o.blob(0, 5) == 123 && o.blob(0, 6) == (4ull << 20), "an expert_bytes entry overrides the layer figure, a zero does not");
ExpertGeom v = g;
v.layer_blob_bytes.assign(24, 0);
for (int l = 0; l < 24; ++l) v.layer_blob_bytes[(size_t) l] = (uint64_t) (1 + l) << 20;
check(v.valid(err) && v.blob(0, 0) == (1ull << 20) && v.blob(23, 0) == (24ull << 20), "per-layer blob sizes");
const RankedPairs r1 = synthetic_ranking(g, 7), r2 = synthetic_ranking(g, 7), r3 = synthetic_ranking(g, 8);
check(r1.order.size() == (size_t) g.n_layers * g.n_experts, "the synthetic ranking lists every pair");
bool same = r1.order.size() == r2.order.size(), diff = false;
std::set<int> seen;
for (size_t i = 0; i < r1.order.size(); ++i) {
if (r1.order[i].layer != r2.order[i].layer || r1.order[i].expert != r2.order[i].expert) same = false;
if (r1.order[i].layer != r3.order[i].layer || r1.order[i].expert != r3.order[i].expert) diff = true;
seen.insert(r1.order[i].layer * 1000 + r1.order[i].expert);
}
check(same && diff && seen.size() == r1.order.size(), "the synthetic ranking is a deterministic permutation that depends on its seed");
}
// ---- 1. a symmetric pair behind a host bridge, the model does not fit one card (R3): capacity first
{
const ExpertGeom g = qfn();
PlanInput in = make(g, make_synthetic(Backend::Cuda, {a4000(), a4000()}), {12 * GiB, 14 * GiB}, qfn_window());
const Placement p = plan_experts(in);
invariants(in, p, "symmetric R3");
if (p.ok) {
check(p.loads[0].bytes + 4 * 2'060'000ull >= 12 * GiB && p.loads[1].bytes + 4 * 2'060'000ull >= 14 * GiB, "capacity first: both cards are filled to within a few blobs of their budgets");
check(p.est_window_ms < 0.7 * p.est_window_ms_primary_only, "two cards beat one by a wide margin when the host tier is the bottleneck");
check(p.gpu_mass > 0.85, "the GPUs serve most routed picks");
check(p.ram_bytes > 0 && p.ssd_bytes == 0, "the rest is in RAM");
// hotness is honoured: the hottest pair is on a GPU, the coldest are in RAM
const RankedPair h = in.hot.order.front(), c = in.hot.order.back();
check(p.tiers[p.tier_of[(size_t) h.layer * 512 + (size_t) h.expert]].kind != Tier::PinnedRam, "the hottest pair is on a GPU");
check(p.tiers[p.tier_of[(size_t) c.layer * 512 + (size_t) c.expert]].kind == Tier::PinnedRam, "the coldest pair is in RAM");
check(p.share.size() == 1 && p.tiers.size() == 4 && p.tiers[1].kind == Tier::Peer && p.tiers[1].device != p.tiers[0].device, "one peer, a different card from the primary");
}
// Rows against SumPartials: the exact mode moves more bytes, so it is never faster in this model
PlanInput sum = in;
sum.wl.exact = Exact::SumPartials;
const Placement ps = plan_experts(sum);
check(ps.ok && ps.est_window_ms <= p.est_window_ms + 1e-9, "SumPartials is not slower than Rows");
}
// ---- 2. R2: everything fits across the two cards; dense bytes 0 and equal cards: an even-ish split, shrinking as the peer's link narrows
{
const ExpertGeom g = qfn();
Workload w = qfn_window();
w.dense_bytes = 0;
double prev = 2.0, first = 0.0, last = 0.0;
double prev_est = 0.0;
for (double gbps : {24.0, 12.0, 3.5, 1.0}) {
DeviceSet d = make_synthetic(Backend::Cuda, {a4000(), a4000()});
set_host_link(d, 1, 4, 16, gbps, gbps, 12.0);
PlanInput in = make(g, d, {30 * GiB, 30 * GiB}, w);
const Placement p = plan_experts(in);
invariants(in, p, "R2 link " + std::to_string(gbps));
if (!p.ok) continue;
check(p.ram_bytes == 0 && p.ssd_bytes == 0 && std::fabs(p.gpu_mass - 1.0) < 1e-6, "R2: every expert is on a GPU");
check(p.share[0] <= prev + 1e-9, "the peer's share does not grow as its link narrows (" + std::to_string(gbps) + " GB/s: " + std::to_string(p.share[0]) + ")");
check(p.est_window_ms + 1e-9 >= prev_est, "the predicted window does not get faster as the link narrows");
check(p.est_window_ms < p.est_window_ms_primary_only, "the second card still pays at " + std::to_string(gbps) + " GB/s with this much to move");
prev = p.share[0];
prev_est = p.est_window_ms;
if (gbps == 24.0) first = p.share[0];
last = p.share[0];
}
check(first >= 0.25 && first <= 0.6, "fast link, equal cards, no dense bytes: an even-ish split (got " + std::to_string(first) + ")");
check(last < 0.5 * first, "a 1 GB/s peer gets under half the share of a 24 GB/s one");
}
// ---- 3. the dense bytes sit on the primary, so the peer takes more of the experts
{
const ExpertGeom g = qfn();
Workload lean = qfn_window(), heavy = qfn_window();
lean.dense_bytes = 0;
heavy.dense_bytes = 6000ull << 20;
const Placement a = plan_experts(make(g, make_synthetic(Backend::Cuda, {a4000(), a4000()}), {30 * GiB, 30 * GiB}, lean));
const Placement b = plan_experts(make(g, make_synthetic(Backend::Cuda, {a4000(), a4000()}), {30 * GiB, 30 * GiB}, heavy));
check(a.ok && b.ok && b.share[0] > a.share[0], "more dense bytes on the primary: a larger peer share");
}
// ---- 4. an NVLink pair is faster than the same cards through the host
{
const ExpertGeom g = qfn();
DeviceSet host = make_synthetic(Backend::Cuda, {a4000(), a4000()});
DeviceSet nv = host;
set_p2p(nv, 0, 1, 50.0, 8.0, true);
const Placement a = plan_experts(make(g, host, {30 * GiB, 30 * GiB}, qfn_window()));
const PlanInput in = make(g, nv, {30 * GiB, 30 * GiB}, qfn_window());
const Placement b = plan_experts(in);
invariants(in, b, "NVLink");
check(a.ok && b.ok && b.est_window_ms < a.est_window_ms, "NVLink is predicted faster than the host route");
}
// ---- 5. the PR #7310 rig's shape: a strong primary and a weaker card on a Gen4 x2 link
{
const ExpertGeom g = qfn();
SyntheticDevice big = a4000(), weak = a4000();
big.name = "strong";
big.sms = 84; big.clock_ghz = 2.6; big.mem_bw_gbps = 900; big.h2d_gbps = big.d2h_gbps = 25;
weak.name = "weak";
weak.sms = 34; weak.clock_ghz = 2.5; weak.mem_bw_gbps = 280;
DeviceSet d = make_synthetic(Backend::Cuda, {weak, big}); // the stronger card is index 1: it must become primary
set_host_link(d, 0, 4, 2, 3.5, 3.5, 25.0);
PlanInput in = make(g, d, {14 * GiB, 12 * GiB}, qfn_window());
const Placement p = plan_experts(in);
invariants(in, p, "asymmetric x2");
if (p.ok) {
check(p.tiers[0].kind == Tier::Primary && p.tiers[0].device == 1, "the stronger card is the primary, whichever index it has");
check(p.share[0] <= 0.05, "the peer on a Gen4 x2 link is dealt (almost) none of the hot pairs (got " + std::to_string(p.share[0]) + ")");
check(p.loads[1].bytes + 4 * 2'060'000ull >= 14 * GiB, "capacity first: it still holds experts up to its budget, the colder ones");
check(p.est_window_ms <= p.est_window_ms_primary_only, "and the plan is not slower than the primary alone");
}
// the person's flag wins: a fixed 0.45 is used as given
PlanInput fx = in;
fx.opt.fixed_shares = {0.45};
const Placement pf = plan_experts(fx);
invariants(fx, pf, "asymmetric fixed");
check(pf.ok && std::fabs(pf.share[0] - 0.45) < 1e-12 && pf.why.find("fixed by the caller") != std::string::npos, "fixed_shares is used as given and says so");
if (pf.ok) {
// among the 2000 hottest pairs the peer holds about 45 percent
size_t on_peer = 0;
for (size_t i = 0; i < 2000; ++i) {
const RankedPair r = fx.hot.order[i];
on_peer += pf.tiers[pf.tier_of[(size_t) r.layer * 512 + (size_t) r.expert]].kind == Tier::Peer;
}
check(on_peer > 800 && on_peer < 1000, "a fixed 0.45 deals the peer about 45 percent of the hottest pairs (" + std::to_string(on_peer) + " of 2000)");
}
}
// ---- 6. a peer that costs more than it gives: capacity first keeps it and says so; capacity_first = false leaves it out
{
const ExpertGeom g = small();
Workload w;
w.window_rows = 8;
w.hidden = 2048;
w.dense_bytes = 256ull << 20;
SyntheticDevice dev = a4000();
DeviceSet d = make_synthetic(Backend::Cuda, {dev, dev});
set_host_link(d, 1, 3, 1, 0.02, 0.02, 400.0); // a 20 MB/s link
PlanInput in = make(g, d, {4 * GiB, 4 * GiB}, w);
const Placement keep = plan_experts(in);
invariants(in, keep, "useless peer, capacity first");
check(keep.ok && keep.loads.size() == 2 && keep.loads[1].bytes > 0, "capacity first fills the peer");
check(keep.ok && keep.est_window_ms > keep.est_window_ms_primary_only * 1.01, "and the model says it costs time");
bool noted = false;
for (const std::string& n : keep.notes) noted = noted || n.find("do not pay") != std::string::npos;
check(noted, "the plan says the extra card does not pay");
PlanInput off = in;
off.opt.capacity_first = false;
const Placement lean = plan_experts(off);
invariants(off, lean, "useless peer, capacity_first=false");
check(lean.ok && lean.est_window_ms <= lean.est_window_ms_primary_only * 1.001, "capacity_first = false is at least as good as the primary alone");
check(lean.ok && lean.tiers.size() == 3, "the planner left the peer out of the tiers");
}
// ---- 7. four equal cards
{
const ExpertGeom g = qfn();
PlanInput in = make(g, make_synthetic(Backend::Hip, {a4000(), a4000(), a4000(), a4000()}), {10 * GiB, 12 * GiB, 12 * GiB, 12 * GiB}, qfn_window());
const Placement p = plan_experts(in);
invariants(in, p, "4 GPUs");
if (p.ok) {
check(p.tiers.size() == 6 && p.loads.size() == 4 && p.share.size() == 3, "four GPU tiers, a RAM tier and an SSD tier");
for (size_t g2 = 0; g2 < 4; ++g2) check(p.loads[g2].bytes > 0, "every card holds experts");
check(p.est_window_ms < 0.5 * p.est_window_ms_primary_only, "four cards are far ahead of one");
check(p.gpu_mass > 0.95, "four cards serve nearly all routed picks");
// the hottest pairs are spread over cards, not stacked on one
std::set<int> devs;
for (size_t i = 0; i < 40; ++i) {
const RankedPair r = in.hot.order[i];
const Tier& t = p.tiers[p.tier_of[(size_t) r.layer * 512 + (size_t) r.expert]];
if (t.kind != Tier::PinnedRam && t.kind != Tier::Ssd) devs.insert(t.device);
}
check(devs.size() >= 2, "the 40 hottest pairs sit on more than one card");
}
// more cards never plan slower than fewer, same per-card budgets
PlanInput three = make(g, make_synthetic(Backend::Hip, {a4000(), a4000(), a4000()}), {10 * GiB, 12 * GiB, 12 * GiB}, qfn_window());
const Placement p3 = plan_experts(three);
invariants(three, p3, "3 GPUs");
check(p.ok && p3.ok && p.est_window_ms <= p3.est_window_ms + 1e-9, "four cards are not predicted slower than three");
}
// ---- 8. budgets: a card with none is not used; RAM budget spills to SSD; one card alone
{
const ExpertGeom g = small();
Workload w;
w.window_rows = 8;
w.hidden = 2048;
w.dense_bytes = 256ull << 20;
DeviceSet d = make_synthetic(Backend::Cuda, {a4000(), a4000(), a4000()});
PlanInput in = make(g, d, {3 * GiB, 0, 3 * GiB}, w);
const Placement p = plan_experts(in);
invariants(in, p, "zero budget card");
bool used1 = false;
for (const Tier& t : p.tiers) used1 = used1 || t.device == 1;
check(p.ok && !used1, "a card with no budget has no tier");
PlanInput spill = in;
spill.budget_bytes = {2 * GiB, 0, 0};
spill.host_ram_budget = 3 * GiB;
const Placement ps = plan_experts(spill);
invariants(spill, ps, "RAM budget");
check(ps.ok && ps.ssd_bytes > 0 && ps.ram_bytes <= 3 * GiB && ps.ram_bytes + 4ull * (4ull << 20) >= 3 * GiB, "experts beyond the RAM budget go to the SSD tier, RAM is filled first");
check(ps.ok && ps.loads.size() == 1 && ps.share.empty(), "one card: no peer, no share");
PlanInput one = make(g, make_synthetic(Backend::Cuda, {a4000()}), {20 * GiB}, w);
const Placement po = plan_experts(one);
invariants(one, po, "one card, all fits");
check(po.ok && po.ram_bytes == 0 && po.ssd_bytes == 0 && std::fabs(po.gpu_mass - 1.0) < 1e-6 && po.est_window_ms == po.est_window_ms_primary_only, "one big card holds everything and the plan equals the primary alone");
}
// ---- 9. input checking
{
PlanInput in = make(small(), make_synthetic(Backend::Cuda, {a4000(), a4000()}), {1 * GiB, 1 * GiB}, Workload());
PlanInput bad = in;
bad.budget_bytes = {1 * GiB};
check(!plan_experts(bad).ok, "a budget list of the wrong size is refused");
bad = in;
bad.geom.n_layers = 0;
check(!plan_experts(bad).ok, "a bad geometry is refused");
bad = in;
bad.devices = DeviceSet();
bad.budget_bytes.clear();
check(!plan_experts(bad).ok && !plan_experts(bad).why.empty(), "an empty device set is refused with a reason");
bad = in;
bad.hot.order.push_back({999, 0});
bad.hot.order.push_back({0, 0});
bad.hot.order.push_back({0, 0});
const Placement pb = plan_experts(bad);
bool noted = false;
for (const std::string& n : pb.notes) noted = noted || n.find("ignored") != std::string::npos;
check(pb.ok && noted, "an out-of-range or repeated ranked pair is ignored and noted");
bad = in;
bad.hot.order.clear(); // no ranking at all: every pair is 'unlisted', in index order
check(plan_experts(bad).ok, "an empty ranking is allowed");
}
// ---- 10. an explicit mass table is honoured
{
const ExpertGeom g = small();
PlanInput in = make(g, make_synthetic(Backend::Cuda, {a4000(), a4000()}), {1 * GiB, 1 * GiB}, Workload());
in.hot.order.clear();
in.hot.mass.clear();
// one spike per layer on expert 100, listed in rank order: the 24 spikes come first, then the layers' other experts rank by rank
for (int r = 0; r < 128; ++r)
for (int l = 0; l < g.n_layers; ++l) in.hot.order.push_back({l, (r + 100) % 128});
for (size_t i = 0; i < in.hot.order.size(); ++i) in.hot.mass.push_back(in.hot.order[i].expert == 100 ? 0.5 : 0.5 / 127.0);
const Placement p = plan_experts(in);
invariants(in, p, "explicit mass");
check(p.ok && p.gpu_mass > 0.5, "the GPUs hold the spike experts and so most of the mass");
if (p.ok)
for (int l = 0; l < g.n_layers; ++l)
check(p.tiers[p.tier_of[(size_t) l * 128 + 100]].kind != Tier::PinnedRam, "the spike expert of layer " + std::to_string(l) + " is on a GPU");
}
// ---- 11. layer split: anchors to generate.cpp's fitted constants, balance, speed, capacity, links
{
const ExpertGeom g = small();
const RankedPairs hot = synthetic_ranking(g, 3);
SyntheticDevice r5080 = a4000();
r5080.name = "5080-like";
r5080.sms = 84;
r5080.clock_ghz = 2.617;
r5080.vram_total = 16 * GiB;
auto costs = [&](std::vector<uint64_t> budgets) {
LayerCosts c;
c.geom = g;
c.hot = hot;
c.weight_bytes = {64ull << 20};
c.budget_bytes = std::move(budgets);
return c;
};
// one card that holds everything: 0.33 ms a layer at 84 SMs x 2.617 GHz, nothing missed
{
DeviceSet d = make_synthetic(Backend::Cuda, {r5080});
const LayerSplit s = plan_layers(costs({40 * GiB}), d, 16ull << 20);
check(s.ok && s.first_layer == std::vector<int>({0}) && s.device == std::vector<int>({0}), "one card: one stage over every layer");
check(s.ok && std::fabs(s.est_window_ms - 24 * 0.33) < 1e-6, "one card that holds everything: layers x 0.33 ms (the fitted figure of the 5080)");
}
// two identical cards that hold everything: balanced halves
{
DeviceSet d = make_synthetic(Backend::Cuda, {r5080, r5080});
const LayerSplit s = plan_layers(costs({40 * GiB, 40 * GiB}), d, 16ull << 20);
check(s.ok && s.first_layer.size() == 2 && s.first_layer[0] == 0 && s.first_layer[1] == 12, "two identical cards: the tie goes to the balanced split (12 + 12)");
check(s.ok && std::fabs(s.est_window_ms - 24 * 0.33) < 1e-6, "and the window time is the same as one card's");
}
// a card half as fast takes fewer layers
{
SyntheticDevice slow = r5080;
slow.sms = 42;
DeviceSet d = make_synthetic(Backend::Cuda, {r5080, slow});
const LayerSplit s = plan_layers(costs({40 * GiB, 40 * GiB}), d, 16ull << 20);
check(s.ok && s.first_layer.size() == 2, "two cards split");
if (s.ok) {
const int fast_layers = s.device[0] == 0 ? s.first_layer[1] - s.first_layer[0] : 24 - s.first_layer[1];
check(fast_layers >= 15, "the faster card takes most layers when speed is the only difference (got " + std::to_string(fast_layers) + " of 24)");
}
}
// capacity: a card that cannot hold its layers' weights is not given them
{
DeviceSet d = make_synthetic(Backend::Cuda, {r5080, r5080});
LayerCosts c = costs({1 * GiB, 40 * GiB}); // 1 GiB holds at most 12 layers of weights (64 MiB + 16 MiB KV each)
const LayerSplit s = plan_layers(c, d, 16ull << 20);
check(s.ok, "a feasible split exists");
if (s.ok) {
int on0 = 0;
for (size_t k = 0; k < s.device.size(); ++k) if (s.device[k] == 0) on0 = (k + 1 < s.first_layer.size() ? s.first_layer[k + 1] : 24) - s.first_layer[k];
check(on0 <= 12, "the small card holds no more layers than its budget allows (" + std::to_string(on0) + ")");
}
LayerCosts none = costs({100ull << 20, 100ull << 20}); // neither card can hold the weights
check(!plan_layers(none, d, 16ull << 20).ok, "no feasible split is reported, not guessed");
}
// a card with no budget is not used; more cards than layers is refused
{
DeviceSet d = make_synthetic(Backend::Cuda, {r5080, r5080, r5080});
const LayerSplit s = plan_layers(costs({40 * GiB, 0, 40 * GiB}), d, 16ull << 20);
check(s.ok && s.device.size() == 2 && s.device[0] != 1 && s.device[1] != 1, "a card with no budget is not a stage");
ExpertGeom tiny = g;
tiny.n_layers = 2;
LayerCosts c = costs({40 * GiB, 40 * GiB, 40 * GiB});
c.geom = tiny;
c.hot = synthetic_ranking(tiny, 1);
check(!plan_layers(c, d, 0).ok, "three cards for two layers is refused");
}
// a stage on a link 4x narrower than the fastest pays more for what its cache misses: it is given fewer layers than on an equal link
{
LayerCosts c = costs({3 * GiB, 3 * GiB}); // caches too small for their layers: misses matter
DeviceSet even = make_synthetic(Backend::Cuda, {r5080, r5080});
DeviceSet narrow = even;
set_host_link(narrow, 1, 3, 4, 5.0, 5.0, 20.0); // 25 vs 5 GB/s: a gap of 5, over the 4x threshold
const LayerSplit a = plan_layers(c, even, 16ull << 20), b = plan_layers(c, narrow, 16ull << 20);
check(a.ok && b.ok, "both plans exist");
if (a.ok && b.ok) {
auto layers_on = [&](const LayerSplit& s, int dev) {
for (size_t k = 0; k < s.device.size(); ++k)
if (s.device[k] == dev) return (k + 1 < s.first_layer.size() ? s.first_layer[k + 1] : 24) - s.first_layer[k];
return 0;
};
check(layers_on(b, 1) <= layers_on(a, 1), "the card on the narrow link is given no more layers than on an equal link");
check(b.est_window_ms >= a.est_window_ms - 1e-9, "and the narrow link is not predicted faster");
}
}
// four cards: four stages, increasing cuts, every layer once; deterministic
{
DeviceSet d = make_synthetic(Backend::Hip, {r5080, r5080, r5080, r5080});
const LayerCosts c = costs({4 * GiB, 4 * GiB, 4 * GiB, 4 * GiB});
const LayerSplit s = plan_layers(c, d, 16ull << 20), t = plan_layers(c, d, 16ull << 20);
check(s.ok && s.first_layer.size() == 4 && s.first_layer[0] == 0, "four stages");
bool inc = true;
for (size_t k = 1; k < s.first_layer.size(); ++k) inc = inc && s.first_layer[k] > s.first_layer[k - 1];
check(inc && s.first_layer.back() < 24, "the cuts increase and leave the last stage a layer");
std::set<int> devs(s.device.begin(), s.device.end());
check(devs.size() == 4, "every card is a stage once");
check(s.first_layer == t.first_layer && s.device == t.device && s.est_window_ms == t.est_window_ms, "planning twice gives the same split");
}
// hand-off pricing: switched on, a stage border costs; with one card there is none
{
DeviceSet d = make_synthetic(Backend::Cuda, {r5080, r5080});
LayerCosts c = costs({40 * GiB, 40 * GiB});
const LayerSplit a = plan_layers(c, d, 16ull << 20);
c.price_handoff = true;
c.handoff_bytes = 8 * 2560 * 4;
const LayerSplit b = plan_layers(c, d, 16ull << 20);
check(a.ok && b.ok && b.est_window_ms > a.est_window_ms, "pricing the hand-off adds the border's time");
}
}
if (g_fail != 0) { std::fprintf(stderr, "%d check(s) failed\n", g_fail); return 1; }
std::puts("mgpu_placement_test: all checks passed");
return 0;
}