From 279214f520329835598a88be0bd9e394b7448fd2 Mon Sep 17 00:00:00 2001 From: Nunze Date: Sun, 18 Oct 2026 18:55:36 +0200 Subject: [PATCH] libr4d: the eight-rank two-shot's shard weights from the PCIe links by default Weights from a variable describe one board on one day. Unset or "auto", RADIANCE_AR8_SHARD_WEIGHTS now reads them from the links: each rank takes its card's bus id from the runtime (hipDeviceGetPCIBusId), walks /sys/bus/pci/devices from the root port down to the card as core/diag/sysinfo.cpp's read_card does, and keeps the slowest hop's current speed x width x line code (8b/10b under 8 GT/s, 128b/130b from there) / 8. The R9700 sits behind an on-card switch whose endpoint reports Gen5 x16 in any slot, so a degraded slot shows only at the root port. The weight is round(4 x (rate / fastest)^2): x16 4, half the rate 1, a quarter 0. On the board of the previous commits that is the measured best, 4,1,0,4,0,1,4,4, and after both Gen4 links retrained at Gen5 x8 it is 4,1,1,4,1,1,4,4, where the 16 MiB two-shot takes 1360 us. "off" keeps the shards even and eight integers set them, as before. The ranks publish their links in the rendezvous (ar8_settle) and, after one barrier, derive the same table from the same eight entries before anything is sized; settings that differ between ranks fail init on every rank. Equal weights run the unweighted kernel, and a rank whose path says nothing (no /sys/bus/pci in a container) leaves the shards even, with a warning naming it. Startup logs one line, "[r4d] eight-rank two-shot shard weights from the slowest PCIe hop of each card: r0 32.0 GT/s x16 63.0 GB/s w4, r1 32.0 GT/s x8 31.5 GB/s w1, ...". The links are read once, at init: a link that falls back while the server runs changes the weights at the next start. r4d_ar_links_test builds that board's sysfs under a temporary root (root port, on-card switch, endpoint) and checks the bottleneck hop, the weights before and after the repair, equal links, the line code and a path that says nothing; it needs only libr4d's include path. r4d_selftest runs auto and off at the default floors, for bits only since what auto derives depends on the box, and --perfar8 derives the weights the same way and names each rank's link. 206 of 206 on the cards. Co-Authored-By: Claude Opus 5.5 --- libr4d/r4d_ar_entry.hip | 165 ++++++++++++++++++++++++++---------- libr4d/r4d_ar_links.h | 85 +++++++++++++++++++ libr4d/r4d_selftest.cpp | 64 +++++++++++--- tests/CMakeLists.txt | 3 +- tests/r4d_ar_links_test.cpp | 165 ++++++++++++++++++++++++++++++++++++ 5 files changed, 424 insertions(+), 58 deletions(-) create mode 100644 libr4d/r4d_ar_links.h create mode 100644 tests/r4d_ar_links_test.cpp diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index b0e2c9a9..999ae62b 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -21,6 +21,7 @@ #include "r4d_args.h" #include "r4d.h" #include "r4d_ar_shards.h" +#include "r4d_ar_links.h" #include #include @@ -71,10 +72,15 @@ struct Rendezvous { long flags [R4D_MAX_RANKS] = { 0 }; long seq [R4D_MAX_RANKS] = { 0 }; int dev [R4D_MAX_RANKS] = { 0 }; - int shard_w[R4D_MAX_RANKS][R4D_MAX_RANKS] = { { 0 } }; - int pieces [R4D_MAX_RANKS] = { 0 }; - long long weights_min[R4D_MAX_RANKS] = { 0 }; - long long pieces_min [R4D_MAX_RANKS] = { 0 }; + /* What each rank read for the eight-rank two-shot: settings that must agree, and its own + * card's link, which the weights are derived from. */ + struct Ar8Seen { + int mode = 0; + int w[R4D_MAX_RANKS] = { 0 }; + int pieces = 0; + long long weights_min = 0, pieces_min = 0; + R4DArLink link; + } ar8[R4D_MAX_RANKS]; int failed = 0; }; @@ -145,26 +151,35 @@ long long pgeti_hi(const RadParam* p, int n_p, const char* k, long long dflt) { long align16(long v) { return (v + 15) & ~15L; } /* RADIANCE_AR8_SHARD_WEIGHTS, read at every eight-rank build so a process can change it between - * instances; each rank reads it and build() checks that they agree. */ -bool ar8_shard_weights(int* w) { + * instances: unset or "auto" derives the weights from the links, "off" keeps the shards even, + * eight integers set them. Anything else is "off", with one warning. */ +enum { AR8_EVEN = 0, AR8_SET = 1, AR8_AUTO = 2 }; +int ar8_weights_mode(int* w) { const char* e = std::getenv(R4D_AR8_SHARD_WEIGHTS_ENV); - if (!e || !*e) return false; - static std::once_flag said_bad, said_on; - int got[8]; - if (!r4d_ar_shard_weights_parse(e, 8, got)) { - std::call_once(said_bad, [e] { - std::fprintf(stderr, "[r4d] %s=\"%s\" ignored (needs eight non-negative integers, " - "comma-separated, sum > 0); eight-rank two-shot shards stay even\n", - R4D_AR8_SHARD_WEIGHTS_ENV, e); - }); - return false; - } - std::memcpy(w, got, sizeof(got)); - std::call_once(said_on, [w] { - std::fprintf(stderr, "[r4d] eight-rank two-shot shards weighted %d,%d,%d,%d,%d,%d,%d,%d\n", - w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]); + if (!e || !*e || !std::strcmp(e, "auto")) return AR8_AUTO; + if (!std::strcmp(e, "off")) return AR8_EVEN; + if (r4d_ar_shard_weights_parse(e, 8, w)) return AR8_SET; + static std::once_flag said; + std::call_once(said, [e] { + std::fprintf(stderr, "[r4d] %s=\"%s\" ignored (needs auto, off or eight non-negative " + "integers, comma-separated, sum > 0); eight-rank two-shot shards stay even\n", + R4D_AR8_SHARD_WEIGHTS_ENV, e); }); - return true; + return AR8_EVEN; +} + +/* This rank's card's slowest PCIe hop, from sysfs by the runtime's bus id. */ +bool ar8_own_link(R4DArLink* l) { + int dev = 0; + char bus[64] = { 0 }; + if (hipGetDevice(&dev) != hipSuccess || + hipDeviceGetPCIBusId(bus, (int)sizeof bus, dev) != hipSuccess) + return false; + unsigned dom = 0, b = 0, d = 0, f = 0; + if (std::sscanf(bus, "%x:%x:%x.%x", &dom, &b, &d, &f) != 4) return false; + char bdf[32]; + std::snprintf(bdf, sizeof bdf, "%04x:%02x:%02x.%x", dom, b, d, f); + return r4d_ar_link_bottleneck("", bdf, l); } /* RADIANCE_AR8_PIECES and the two size floors, read like the weights. */ @@ -210,6 +225,84 @@ long long ar8_pieces_for(const ArInst* in, long numel, long long asked) { return asked > 0 ? asked : r4d_ar8_auto_pieces(numel); } +/* The eight-rank two-shot's settings, agreed before anything is sized: each rank publishes what + * it read and its own card's link, and after one barrier every rank derives the same weights from + * the same eight entries. Settings that differ between ranks would cut the message at different + * words or take different handshake steps; all ranks compare the same entries, so all refuse + * together. */ +int ar8_settle(ArInst* in, int rank, int world) { + static std::once_flag said_w, said_p, said_links, said_unknown, said_set; + Rendezvous::Ar8Seen me; + me.mode = ar8_weights_mode(me.w); + me.pieces = ar8_pieces(); + me.weights_min = ar8_min_elems(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, &said_w); + me.pieces_min = ar8_min_elems(R4D_AR8_PIECES_MIN_ENV, &said_p); + if (me.mode == AR8_AUTO && !ar8_own_link(&me.link)) me.link = R4DArLink(); + { + std::lock_guard lk(g_rv.m); + g_rv.ar8[rank] = me; + } + const int rc = rv_barrier(world); + if (rc != RAD_OK) return rc; + + const Rendezvous::Ar8Seen* all = g_rv.ar8; + for (int r = 0; r < world; ++r) + if (all[r].mode != me.mode || std::memcmp(all[r].w, me.w, sizeof(me.w)) || + all[r].pieces != me.pieces || all[r].weights_min != me.weights_min || + all[r].pieces_min != me.pieces_min) { + std::fprintf(stderr, "[r4d] ranks %d and %d read different eight-rank two-shot " + "settings (%s, %s and their _MIN_ELEMS)\n", rank, r, + R4D_AR8_SHARD_WEIGHTS_ENV, R4D_AR8_PIECES_ENV); + return RAD_E_STATE; + } + in->pieces = me.pieces; + in->weights_min = me.weights_min; + in->pieces_min = me.pieces_min; + + int w[R4D_MAX_RANKS] = { 0 }; + if (me.mode == AR8_SET) { + std::memcpy(w, me.w, sizeof(w)); + std::call_once(said_set, [&w] { + std::fprintf(stderr, "[r4d] eight-rank two-shot shards weighted " + "%d,%d,%d,%d,%d,%d,%d,%d (%s)\n", w[0], w[1], w[2], w[3], w[4], w[5], + w[6], w[7], R4D_AR8_SHARD_WEIGHTS_ENV); + }); + } else if (me.mode == AR8_AUTO) { + double gbs[R4D_MAX_RANKS] = { 0 }; + for (int r = 0; r < world; ++r) gbs[r] = r4d_ar_link_gbs(all[r].link); + if (!r4d_ar_weights_from_rates(gbs, world, w)) { + std::call_once(said_unknown, [all, world] { + std::string who; + for (int r = 0; r < world; ++r) + if (r4d_ar_link_gbs(all[r].link) <= 0.0) who += " " + std::to_string(r); + std::fprintf(stderr, "[r4d] eight-rank two-shot: no PCIe link speed in sysfs for " + "rank%s; shards stay even\n", who.c_str()); + }); + return RAD_OK; + } + std::call_once(said_links, [all, world, &w] { + std::string l; + for (int r = 0; r < world; ++r) { + char one[96]; + std::snprintf(one, sizeof one, "%s r%d %.1f GT/s x%d %.1f GB/s w%d", + r ? "," : "", r, all[r].link.gts, all[r].link.width, + r4d_ar_link_gbs(all[r].link), w[r]); + l += one; + } + std::fprintf(stderr, "[r4d] eight-rank two-shot shard weights from the slowest PCIe " + "hop of each card:%s\n", l.c_str()); + }); + bool same = true; + for (int r = 1; r < world; ++r) same = same && w[r] == w[0]; + if (same) return RAD_OK; /* equal links: the even split, on the unweighted path */ + } else { + return RAD_OK; + } + in->shard_weighted = 1; + std::memcpy(in->shard_w, w, sizeof(in->shard_w)); + return RAD_OK; +} + /* Sequence counter allocation in words: 2 * nbw (r4d_ar_seq.h), then r4d_ar_link_landed's block * counter, padded to a cache line. */ long kSeqWords(long nbw) { return 2L * nbw + 32; } @@ -239,13 +332,10 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code, in->slot_bytes = align16((long)max_numel * in->esize); in->slot16 = in->slot_bytes / 16; if (world == 8) { - static std::once_flag said_w, said_p; - in->pieces = ar8_pieces(); - in->weights_min = ar8_min_elems(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, &said_w); - in->pieces_min = ar8_min_elems(R4D_AR8_PIECES_MIN_ENV, &said_p); + const int rc8 = ar8_settle(in, rank, world); + if (rc8 != RAD_OK) return rc8; } - if (world == 8 && ar8_shard_weights(in->shard_w)) { - in->shard_weighted = 1; + if (in->shard_weighted) { const long need16 = 2 * r4d_ar_shard_max_bound((long)max_numel * in->esize / 16, 8, in->shard_w); /* Sized only if some message this instance carries is weighted. */ @@ -324,29 +414,10 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code, g_rv.flags[rank] = fl; g_rv.seq[rank] = sq; g_rv.dev[rank] = dev; - for (int i = 0; i < R4D_MAX_RANKS; ++i) - g_rv.shard_w[rank][i] = in->shard_weighted ? in->shard_w[i] : -1; - g_rv.pieces[rank] = in->pieces; - g_rv.weights_min[rank] = in->weights_min; - g_rv.pieces_min[rank] = in->pieces_min; } int rc = rv_barrier(world); if (rc != RAD_OK) return rc; - /* Every rank must cut the message at the same words, or an owner reduces words its peers - * pushed elsewhere, and take the same handshake steps, or the flags drift apart. All ranks - * compare the same values, so all refuse together. */ - for (int r = 0; r < world; ++r) - if (std::memcmp(g_rv.shard_w[r], g_rv.shard_w[rank], sizeof(g_rv.shard_w[rank])) || - g_rv.pieces[r] != g_rv.pieces[rank] || - g_rv.weights_min[r] != g_rv.weights_min[rank] || - g_rv.pieces_min[r] != g_rv.pieces_min[rank]) { - std::fprintf(stderr, "[r4d] ranks %d and %d read different eight-rank two-shot " - "settings (%s, %s and their _MIN_ELEMS)\n", rank, r, - R4D_AR8_SHARD_WEIGHTS_ENV, R4D_AR8_PIECES_ENV); - return RAD_E_STATE; - } - /* Peer access is per context, so each rank enables its own, after the publish barrier where * peer device ordinals are known. A host can expose more HIP devices than ranks (e.g. an * integrated GPU), so the ordinal comes from the peer rather than the rank. */ diff --git a/libr4d/r4d_ar_links.h b/libr4d/r4d_ar_links.h new file mode 100644 index 00000000..32ccfe35 --- /dev/null +++ b/libr4d/r4d_ar_links.h @@ -0,0 +1,85 @@ +#pragma once +// r4d_ar_links.h: the eight-rank two-shot's shard weights from the PCIe links (host only). +// +// A card's link to the CPU is as fast as its slowest hop. The R9700 sits behind an on-card switch +// whose endpoint reports full speed in any slot, so a degraded slot shows only at the root port: +// every hop from the root port down is read, as core/diag/sysinfo.cpp (read_card) reads it, from +// the kernel's sysfs under `root` ("" on a real machine, a directory tree in tests). Nothing that +// a file does not say is guessed; a card with no readable hop has no rate. +// +// Weight from the rate relative to the fastest card: w = round(4 * (rate / max)^2), so x16 4, +// half the rate 1, a quarter 0. A rank's bytes per direction grow with its shard, so the weights +// fall faster than the rate; the measured best on four Gen5 x16, two Gen5 x8 and two Gen4 x8 was +// 4,1,0,4,0,1,4,4 (docs/GUIDE.md, 4.7). +#include +#include +#include +#include +#include +#include + +struct R4DArLink { + std::string bdf; // the slowest hop, "" when none was readable + double gts = 0.0; // its current speed in GT/s + int width = 0; // its current width in lanes +}; + +// Payload GB/s of one direction: 8b/10b below 8 GT/s, 128b/130b from Gen3 on. +inline double r4d_ar_link_gbs(const R4DArLink& l) { + if (l.gts <= 0.0 || l.width <= 0) return 0.0; + const double enc = l.gts < 8.0 ? 8.0 / 10.0 : 128.0 / 130.0; + return l.gts * l.width * enc / 8.0; +} + +inline std::string r4d_ar_sysfs_line(const std::string& path) { + std::ifstream f(path); + std::string s; + if (!f || !std::getline(f, s)) return std::string(); + return s; +} + +inline bool r4d_ar_is_bdf(const std::string& s) { + unsigned d, b, dv, f; + char tail; + return s.size() == 12 && std::sscanf(s.c_str(), "%4x:%2x:%2x.%1x%c", &d, &b, &dv, &f, &tail) == 4; +} + +// The slowest hop between the root port and the card `bdf` ("0000:03:00.0", lower case). False +// when no hop on the path says its current speed and width. +inline bool r4d_ar_link_bottleneck(const std::string& root, const std::string& bdf, + R4DArLink* out) { + namespace fs = std::filesystem; + const std::string devs = root + "/sys/bus/pci/devices/"; + std::error_code ec; + const fs::path real = fs::canonical(devs + bdf, ec); + if (ec) return false; + R4DArLink best; + for (const fs::path& part : real) { + const std::string p = part.string(); + if (!r4d_ar_is_bdf(p)) continue; + R4DArLink h; + h.bdf = p; + h.gts = std::strtod(r4d_ar_sysfs_line(devs + p + "/current_link_speed").c_str(), nullptr); + h.width = std::atoi(r4d_ar_sysfs_line(devs + p + "/current_link_width").c_str()); + const double r = r4d_ar_link_gbs(h); + if (r <= 0.0) continue; + if (best.bdf.empty() || r < r4d_ar_link_gbs(best)) best = h; + } + if (best.bdf.empty()) return false; + *out = best; + return true; +} + +// Weights from every rank's rate (GB/s). False when a rank's rate is unknown. +inline bool r4d_ar_weights_from_rates(const double* gbs, int ws, int* w) { + double top = 0.0; + for (int r = 0; r < ws; ++r) { + if (!(gbs[r] > 0.0)) return false; + if (gbs[r] > top) top = gbs[r]; + } + for (int r = 0; r < ws; ++r) { + const double q = gbs[r] / top; + w[r] = (int)std::lround(4.0 * q * q); + } + return true; +} diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 5562421d..36c7e482 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -36,6 +36,7 @@ #include "rad_plugin.h" #include "r4d_args.h" #include "r4d_ar_shards.h" +#include "r4d_ar_links.h" #include "r4d_hc_fp8.h" #include "r4d_fp8_frag.h" #include "r4d_fp8_lm.h" @@ -16375,7 +16376,7 @@ static void ar_wide_pattern(const Plugin& pl, const std::vector& devs, cons * weights whatever the environment. 40 bf16 elements are 5 words (5 % 4 != 0), 72 are 9 * (9 % 8 != 0). */ if (world >= 4 && std::strstr(name, "twoshot")) { - ScopedEnv even(R4D_AR8_SHARD_WEIGHTS_ENV, nullptr); + ScopedEnv even(R4D_AR8_SHARD_WEIGHTS_ENV, "off"); const int64_t odd = world == 4 ? 40 : 72; const std::string w = std::to_string(world); std::vector t((size_t)world); @@ -16432,7 +16433,7 @@ static void ar_wide_weighted(const Plugin& pl, const std::vector& devs, con for (const char* k : { "1", "2", "4" }) { ScopedEnv env_k(R4D_AR8_PIECES_ENV, k); for (const char* w : { "", "0,3,1,0,5,1,2,4", "1,0,0,0,0,0,0,0", "4,1,0,4,0,1,4,4" }) { - ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, *w ? w : nullptr); + ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, *w ? w : "off"); const std::string label = std::string(name) + " pieces " + k + (*w ? std::string(" weights ") + w : std::string(" even")); @@ -16451,6 +16452,17 @@ static void ar_wide_weighted(const Plugin& pl, const std::vector& devs, con mixed); } } + /* The weights from this box's links ("auto", the default) and switched off, at the default + * floors and piece rule: what the engine runs without settings. Bits only, since what auto + * derives depends on the box (the startup line names it). */ + for (const char* mode : { "auto", "off" }) { + ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, mode); + ScopedEnv env_k(R4D_AR8_PIECES_ENV, nullptr); + ScopedEnv floor_w(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, nullptr); + ScopedEnv floor_k(R4D_AR8_PIECES_MIN_ENV, nullptr); + const ArPatternRun run = { "bf16", { 4194304, 524288, 8256, 8388608, 196608 }, 1 }; + if (!ar_pattern_check(row, devs, std::string(name) + " weights " + mode, W, run)) return; + } /* The floors at their defaults (R4D_AR8_MIN_ELEMS_DEFAULT elements) with weights, first at the * size rule's piece counts (131072 runs one piece, 262144 two, 1048576 four, 4194304 eight), * then at four pieces set. Which side of the weight floor a message fell on shows in what the @@ -16672,6 +16684,39 @@ static void ar8_perf_worker(Ar8Perf* t) { for (void* p : { in, out, csrc, cdst }) if (p) st_free(p); } +/* The eight-rank two-shot's weights as the plugin derives them at init (r4d_ar_entry.hip, + * ar8_settle): RADIANCE_AR8_SHARD_WEIGHTS unset or "auto" from each rank's slowest PCIe hop, "off" + * or anything invalid even, eight integers as given; equal derived weights are the even split. + * `how` names the source and, for "auto", every rank's link. */ +static bool ar8_weights_as_plugin(const std::vector& devs, int* w, std::string* how) { + const char* e = std::getenv(R4D_AR8_SHARD_WEIGHTS_ENV); + if (e && *e && std::strcmp(e, "auto")) { + if (!std::strcmp(e, "off")) { *how = " (off)"; return false; } + if (r4d_ar_shard_weights_parse(e, 8, w)) { *how = " (set)"; return true; } + *how = fmt(" (%s=\"%s\" is invalid)", R4D_AR8_SHARD_WEIGHTS_ENV, e); + return false; + } + double gbs[8] = { 0 }; + std::string links; + for (int r = 0; r < 8 && r < (int)devs.size(); ++r) { + char bus[64] = { 0 }; + unsigned dom = 0, b = 0, d = 0, f = 0; + R4DArLink l; + if (hipDeviceGetPCIBusId(bus, (int)sizeof bus, devs[(size_t)r]) == hipSuccess && + std::sscanf(bus, "%x:%x:%x.%x", &dom, &b, &d, &f) == 4) + r4d_ar_link_bottleneck("", fmt("%04x:%02x:%02x.%x", dom, b, d, f), &l); + gbs[r] = r4d_ar_link_gbs(l); + links += fmt("%s r%d %.1f GT/s x%d", r ? "," : "", r, l.gts, l.width); + } + if (!r4d_ar_weights_from_rates(gbs, 8, w)) { + *how = " (auto: a link is unreadable;" + links + ")"; + return false; + } + *how = " (auto:" + links + ")"; + for (int r = 1; r < 8; ++r) if (w[r] != w[0]) return true; + return false; +} + static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dtype) { constexpr int W = 8; if (iters <= 0) iters = 200; @@ -16717,17 +16762,16 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty g_cold > 0 ? fmt("cold (%lld MiB read before each; last arriver / with skew, " "medians)", (long long)(g_cold >> 20)).c_str() : "warm (back to back, period over the span, slowest rank)"); - /* The plugin reads the same variable through the same parser at init, so this is the table - * the two-shot runs. */ + /* The plugin reads the same variable and the same sysfs files through the same functions at + * init, so this is the table the two-shot runs. */ int wts[W]; - const char* wenv = std::getenv(R4D_AR8_SHARD_WEIGHTS_ENV); - const bool weighted = wenv && *wenv && r4d_ar_shard_weights_parse(wenv, W, wts); - std::printf("two-shot shard weights: %s\n", + std::string how; + const bool weighted = ar8_weights_as_plugin(devs, wts, &how); + std::printf("two-shot shard weights: %s%s\n", weighted ? fmt("%d,%d,%d,%d,%d,%d,%d,%d", wts[0], wts[1], wts[2], wts[3], wts[4], wts[5], wts[6], wts[7]).c_str() - : wenv && *wenv ? fmt("even (%s=\"%s\" is invalid)", R4D_AR8_SHARD_WEIGHTS_ENV, - wenv).c_str() - : "even"); + : "even", + how.c_str()); /* The floors as the plugin reads them; under them a message runs even and in one piece, the * `pieces` parameter included. */ auto floor_of = [](const char* name) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 82e007d7..51e97566 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -45,7 +45,8 @@ set(RAD_TESTS_WITH_LEGACY chatparse_test chatparse_gemma4_test) # relayout_parity_test checks each kernel library's relayout of libquant's planes against a # recorded hash of the stored bytes. libr4d's layout sources are host-only, so they are compiled in, # with -ffp-contract=off as libr4d uses, so scale conversions round identically. -set(RAD_TESTS_WITH_R4D_HOST relayout_parity_test) +# r4d_ar_links_test checks libr4d's header-only link reader, so it needs only the include path. +set(RAD_TESTS_WITH_R4D_HOST relayout_parity_test r4d_ar_links_test) # Chat templates (copied from the model repositories) and media samples, passed by absolute path so # test binaries can run from anywhere. diff --git a/tests/r4d_ar_links_test.cpp b/tests/r4d_ar_links_test.cpp new file mode 100644 index 00000000..a91e84f6 --- /dev/null +++ b/tests/r4d_ar_links_test.cpp @@ -0,0 +1,165 @@ +/* r4d_ar_links_test.cpp -- the eight-rank two-shot's shard weights from the PCIe links + * (libr4d/r4d_ar_links.h). + * + * Each case builds sysfs under a root directory, laid out like the eight-R9700 box the weights + * were measured on: each card behind its on-card switch (upstream and downstream port at Gen5 + * x16, endpoint at Gen5 x16) under a root port whose link is the real slot. The slot shows only + * at the root port, so a reader that stops at the card's own link sees eight equal cards. + * + * Bottleneck the slowest hop of a card's path, root port included + * Weights round(4 * (rate / max)^2): two Gen4 x8 links give 4,1,0,4,0,1,4,4, the + * repaired ones 4 and 1; a card whose path says nothing leaves the weights unknown + */ +#include "rad_test.h" + +#include "r4d_ar_links.h" + +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +namespace { + +struct Root { + std::string dir; + Root() { + std::string tmpl = radtest::tmp_path("root.XXXXXX"); + dir = mkdtemp(tmpl.data()) ? tmpl : ""; + } + void put(const std::string& rel, const std::string& text) const { + const fs::path p = fs::path(dir) / rel.substr(1); + fs::create_directories(p.parent_path()); + std::ofstream(p) << text; + } + void link(const std::string& rel, const std::string& target) const { + const fs::path p = fs::path(dir) / rel.substr(1); + fs::create_directories(p.parent_path()); + fs::create_symlink(target, p); + } +}; + +/* A device under /sys/devices at `path` with its /sys/bus/pci/devices link; `speed` "" writes no + * link files, as some bridges inside a switch do. */ +void pci(const Root& r, const std::string& path, const std::string& speed, int width) { + const std::string bdf = path.substr(path.rfind('/') + 1); + const std::string d = "/sys/devices/" + path; + r.put(d + "/class", "0x060400\n"); + if (!speed.empty()) { + r.put(d + "/current_link_speed", speed + "\n"); + r.put(d + "/current_link_width", std::to_string(width) + "\n"); + } + r.link("/sys/bus/pci/devices/" + bdf, "../../../devices/" + path); +} + +std::string bdf_on(int bus) { + char b[16]; + std::snprintf(b, sizeof b, "0000:%02x:00.0", bus); + return b; +} + +/* Card `i` behind root port 0000:00:0i.1 running `speed` x`width`; returns the card's BDF. */ +std::string card(const Root& r, int i, const std::string& speed, int width) { + const std::string rp = "pci0000:00/0000:00:0" + std::to_string(i) + ".1"; + const std::string up = rp + "/" + bdf_on(0x10 + 3 * i); + const std::string down = up + "/" + bdf_on(0x11 + 3 * i); + const std::string ep = down + "/" + bdf_on(0x12 + 3 * i); + pci(r, rp, speed, width); + pci(r, up, "32.0 GT/s PCIe", 16); + pci(r, down, "32.0 GT/s PCIe", 16); + pci(r, ep, "32.0 GT/s PCIe", 16); + return bdf_on(0x12 + 3 * i); +} + +struct Slot { const char* speed; int width; }; + +/* The weights the eight cards give, or "unknown". */ +std::string weights(const Root& r, const Slot* slots) { + std::string bdf[8]; + for (int i = 0; i < 8; ++i) bdf[i] = card(r, i, slots[i].speed, slots[i].width); + double gbs[8] = { 0 }; + for (int i = 0; i < 8; ++i) { + R4DArLink l; + if (r4d_ar_link_bottleneck(r.dir, bdf[i], &l)) gbs[i] = r4d_ar_link_gbs(l); + } + int w[8]; + if (!r4d_ar_weights_from_rates(gbs, 8, w)) return "unknown"; + std::string s; + for (int i = 0; i < 8; ++i) s += (i ? "," : "") + std::to_string(w[i]); + return s; +} + +const char* G5 = "32.0 GT/s PCIe"; +const char* G4 = "16.0 GT/s PCIe"; + +} // namespace + +TEST(the_slowest_hop_is_the_root_port_behind_an_on_card_switch) { + Root r; + const std::string bdf = card(r, 2, G4, 8); + R4DArLink l; + REQUIRE(r4d_ar_link_bottleneck(r.dir, bdf, &l)); + CHECK_EQ(l.bdf, std::string("0000:00:02.1")); + CHECK(l.gts == 16.0); + CHECK_EQ(l.width, 8); + CHECK(std::fabs(r4d_ar_link_gbs(l) - 16.0 * 8 * 128.0 / 130.0 / 8.0) < 1e-9); +} + +TEST(two_degraded_links_give_the_measured_weights) { + Root r; + const Slot s[8] = { { G5, 16 }, { G5, 8 }, { G4, 8 }, { G5, 16 }, + { G4, 8 }, { G5, 8 }, { G5, 16 }, { G5, 16 } }; + CHECK_EQ(weights(r, s), std::string("4,1,0,4,0,1,4,4")); +} + +TEST(repaired_links_give_four_and_one) { + Root r; + const Slot s[8] = { { G5, 16 }, { G5, 8 }, { G5, 16 }, { G5, 16 }, + { G5, 8 }, { G5, 8 }, { G5, 16 }, { G5, 16 } }; + CHECK_EQ(weights(r, s), std::string("4,1,4,4,1,1,4,4")); +} + +TEST(equal_links_give_equal_weights) { + Root r; + Slot s[8]; + for (Slot& x : s) x = { G5, 16 }; + CHECK_EQ(weights(r, s), std::string("4,4,4,4,4,4,4,4")); +} + +/* A Gen2 link moves 8 of every 10 bits, Gen3 on 128 of 130: 5.0 GT/s x16 is 8.0 GB/s, an eighth + * of Gen5 x16 at 63.0, so its weight rounds to 0. */ +TEST(the_line_code_is_counted) { + R4DArLink l; + l.gts = 5.0; l.width = 16; + CHECK(std::fabs(r4d_ar_link_gbs(l) - 8.0) < 1e-9); + l.gts = 8.0; l.width = 16; + CHECK(std::fabs(r4d_ar_link_gbs(l) - 8.0 * 16 * 128.0 / 130.0 / 8.0) < 1e-9); +} + +/* A hop without link files (a bridge inside a switch) is skipped; a path where no hop says + * anything has no rate, and one such card leaves all weights unknown, not guessed. */ +TEST(a_silent_path_has_no_rate_and_no_weights) { + Root r; + const Slot s[8] = { { G5, 16 }, { G5, 8 }, { G4, 8 }, { G5, 16 }, + { G4, 8 }, { G5, 8 }, { G5, 16 }, { G5, 16 } }; + std::string bdf[8]; + for (int i = 0; i < 8; ++i) bdf[i] = card(r, i, s[i].speed, s[i].width); + pci(r, "pci0000:00/0000:00:09.1", "", 0); + pci(r, "pci0000:00/0000:00:09.1/0000:40:00.0", "", 0); + R4DArLink l; + CHECK(!r4d_ar_link_bottleneck(r.dir, "0000:40:00.0", &l)); + CHECK(!r4d_ar_link_bottleneck(r.dir, "0000:41:00.0", &l)); + double gbs[8] = { 0 }; + for (int i = 0; i < 7; ++i) { + R4DArLink x; + if (r4d_ar_link_bottleneck(r.dir, bdf[i], &x)) gbs[i] = r4d_ar_link_gbs(x); + } + int w[8]; + CHECK(!r4d_ar_weights_from_rates(gbs, 8, w)); +} + +RAD_TEST_MAIN()