diff --git a/include/strata/core/kfd_quiet.hpp b/include/strata/core/kfd_quiet.hpp index bf5b998402..312bfdc173 100644 --- a/include/strata/core/kfd_quiet.hpp +++ b/include/strata/core/kfd_quiet.hpp @@ -76,6 +76,87 @@ inline LoadMoves load_moves() { return load_moves(class_flag().load(std::memory_order_relaxed), std::getenv("STRATA_KFD_QUIET"), std::getenv(fix_env(Fix::Load))); } +// ---- XFER: the device class from the KFD topology (before HIP starts) ------------------------------------------------------------ + +/// One node's properties text: `gfx_target_version` and `simd_count` (0 when absent). A CPU node has simd_count 0. +struct Node { + int64_t gfx = 0; + int64_t simds = 0; +}; + +inline Node parse_node_properties(const std::string& text) { + Node n; + size_t i = 0; + while (i < text.size()) { + size_t e = text.find('\n', i); + if (e == std::string::npos) e = text.size(); + const std::string line = text.substr(i, e - i); + i = e + 1; + const size_t sp = line.find(' '); + if (sp == std::string::npos) continue; + const std::string key = line.substr(0, sp); + int64_t* dst = key == "gfx_target_version" ? &n.gfx : key == "simd_count" ? &n.simds : nullptr; + if (dst == nullptr) continue; + int64_t v = 0; + size_t j = sp; + while (j < line.size() && line[j] == ' ') ++j; + bool any = false; + for (; j < line.size() && line[j] >= '0' && line[j] <= '9' && v < (int64_t) 1000000000000000LL; ++j) { + v = v * 10 + (line[j] - '0'); + any = true; + } + if (any) *dst = v; + } + return n; +} + +/// gfx1103 = 110003, gfx1150 = 110500, gfx1151 = 110501 (major * 10000 + minor * 100 + stepping). All three exist only as +/// integrated GPUs. +inline bool gfx_in_class(int64_t gfx) { return gfx == 110003 || gfx == 110500 || gfx == 110501; } + +/// The class: at least one GPU node, and every GPU node one of the three. A machine with any other GPU beside them (a discrete +/// card) is not the class, so its path stays the old one. +inline bool nodes_in_class(const std::vector& nodes) { + int gpus = 0; + for (const Node& n : nodes) { + if (n.simds <= 0) continue; + ++gpus; + if (!gfx_in_class(n.gfx)) return false; + } + return gpus > 0; +} + +#if defined(__linux__) +/// The nodes under `topo` (normally /sys/class/kfd/kfd/topology/nodes); empty when the folder is not there. +inline std::vector read_topology(const std::string& topo) { + std::vector out; + DIR* d = opendir(topo.c_str()); + if (d == nullptr) return out; + while (const dirent* e = readdir(d)) { + if (e->d_name[0] < '0' || e->d_name[0] > '9') continue; + const std::string path = topo + "/" + e->d_name + "/properties"; + std::FILE* f = std::fopen(path.c_str(), "r"); + if (f == nullptr) continue; + std::string text; + char buf[4096]; + size_t got = 0; + while ((got = std::fread(buf, 1, sizeof buf, f)) > 0) text.append(buf, got); + std::fclose(f); + out.push_back(parse_node_properties(text)); + } + closedir(d); + return out; +} +#endif + +/// XFER: the value the engine sets, or nullptr when nothing is to be set. `in_class`: the topology rule; `already`: +/// GPU_PINNED_MIN_XFER_SIZE as the caller set it (a value the caller set wins). 1000000 is what STRATA_ARENA_MMAP=1 sets already +/// (generate.cpp): the runtime reads it in MiB, so no pageable copy of ours reaches the pin-in-place path. +inline const char* xfer_value(bool in_class, const char* all, const char* one, const char* already) { + if (!in_class || !switch_on(all, one) || already != nullptr) return nullptr; + return "1000000"; +} + // ---- EXIT: the teardown order --------------------------------------------------------------------------------------------------- /// The bound of the stop, seconds. The server stops its engine with STOP + QUIT and waits 20 s, then sends SIGTERM and waits 5 s diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 141d0c76ce..4d2ed7dc56 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -3587,6 +3587,21 @@ int main(int argc, char** argv) { // ROCclr stages through its own pinned buffers instead; decode is unchanged (49.06 vs 49.16 ms/window). if (std::getenv("STRATA_ARENA_MMAP") && std::getenv("STRATA_ARENA_MMAP")[0] == '1') setenv("GPU_PINNED_MIN_XFER_SIZE", "1000000", 0); // MiB; an explicit setting wins +#endif +#if defined(STRATA_USE_HIP) && defined(__linux__) + // XFER (kfd_quiet.hpp): on an integrated gfx11 (every GPU node of the KFD topology a gfx1103 / gfx1150 / gfx1151: the class is + // read from the topology files, since the device properties need HIP up) the same setting by default, so that no pageable + // source of a copy (the check's vectors at load, the lent refill's blobs without a registered arena) becomes a userptr the + // kernel can move under a running dispatch. A value the caller set wins; STRATA_KFD_QUIET(_XFER)=0 sets nothing. + { + const bool cls = strata::core::kfd_quiet::nodes_in_class(strata::core::kfd_quiet::read_topology("/sys/class/kfd/kfd/topology/nodes")); + if (const char* v = strata::core::kfd_quiet::xfer_value(cls, std::getenv("STRATA_KFD_QUIET"), std::getenv("STRATA_KFD_QUIET_XFER"), + std::getenv("GPU_PINNED_MIN_XFER_SIZE"))) { + setenv("GPU_PINNED_MIN_XFER_SIZE", v, 0); + if (std::getenv("STRATA_TRACE") != nullptr) + std::fprintf(stderr, "strata trace: kfd quiet: GPU_PINNED_MIN_XFER_SIZE=%s (pageable copies staged by the runtime)\n", v); + } + } #endif // Load every CUDA kernel when the context is created, before the expert cache takes the free VRAM. With the // default lazy loading, a kernel first used mid-prompt (MMQ for IQ3_XXS at 64K+ on a 12 GB card) found no VRAM diff --git a/tests/core/kfd_quiet_test.cpp b/tests/core/kfd_quiet_test.cpp index c1fa839fd8..eafcb2fec7 100644 --- a/tests/core/kfd_quiet_test.cpp +++ b/tests/core/kfd_quiet_test.cpp @@ -5,6 +5,13 @@ #include #include #include +#include + +#if defined(__linux__) +#include +#include +#include +#endif using namespace strata::core::kfd_quiet; @@ -47,6 +54,74 @@ void load_order() { CHECK(!class_flag().load()); CHECK(!on(Fix::Load) && !on(Fix::Exit)); } +// the class from the topology text, as /sys/class/kfd/kfd/topology/nodes/N/properties reads (shortened) +const char* kCpuNode = "cpu_cores_count 16\nsimd_count 0\nmem_banks_count 1\ngfx_target_version 0\nvendor_id 0\n"; +const char* k780M = "cpu_cores_count 0\nsimd_count 24\nmem_banks_count 1\ngfx_target_version 110003\nvendor_id 4098\ndevice_id 5567\n"; +const char* k890M = "simd_count 32\ngfx_target_version 110500\n"; +const char* k8060S = "simd_count 80\r\ngfx_target_version 110501\r\n"; // \r tolerated: the number ends at the first non-digit +const char* k7900 = "simd_count 192\ngfx_target_version 110000\n"; // a discrete RX 7900 XTX +const char* kR9700 = "simd_count 128\ngfx_target_version 120001\n"; + +void topology() { + { + const Node n = parse_node_properties(k780M); + CHECK(n.gfx == 110003 && n.simds == 24); + const Node c = parse_node_properties(kCpuNode); + CHECK(c.gfx == 0 && c.simds == 0); + CHECK(parse_node_properties(k8060S).gfx == 110501); + CHECK(parse_node_properties("").gfx == 0); + CHECK(parse_node_properties("gfx_target_version\nsimd_count x\n").simds == 0); // no number: not read + CHECK(parse_node_properties("xgfx_target_version 110003\n").gfx == 0); // another key + } + auto cls = [](std::vector nodes) { + std::vector v; + for (const char* t : nodes) v.push_back(parse_node_properties(t)); + return nodes_in_class(v); + }; + CHECK(cls({kCpuNode, k780M})); + CHECK(cls({kCpuNode, k890M})); + CHECK(cls({kCpuNode, k8060S})); + CHECK(!cls({})); // no topology (no KFD, another OS, a container without /sys/class/kfd) + CHECK(!cls({kCpuNode})); // no GPU node + CHECK(!cls({kCpuNode, k7900})); // a discrete gfx11 + CHECK(!cls({kCpuNode, kR9700})); + CHECK(!cls({kCpuNode, k780M, k7900})); // a 780M beside a discrete card: not the class (the card keeps its path) + CHECK(!cls({kCpuNode, k890M, kR9700})); + // the value: only in the class, only with the switches on, never over the caller's own value + CHECK(xfer_value(true, nullptr, nullptr, nullptr) != nullptr && std::string(xfer_value(true, nullptr, nullptr, nullptr)) == "1000000"); + CHECK(xfer_value(false, nullptr, nullptr, nullptr) == nullptr); + CHECK(xfer_value(true, "0", nullptr, nullptr) == nullptr); + CHECK(xfer_value(true, nullptr, "0", nullptr) == nullptr); + CHECK(xfer_value(true, nullptr, nullptr, "4") == nullptr); + CHECK(xfer_value(true, nullptr, nullptr, "") == nullptr); // set, even empty: the caller's +#if defined(__linux__) + // the folder reader on a temporary tree: two nodes, a stray file, a node without properties + char tmpl[] = "/tmp/kfdqXXXXXX"; + if (mkdtemp(tmpl) != nullptr) { + const std::string root = tmpl; + auto put = [](const std::string& path, const char* text) { + if (std::FILE* f = std::fopen(path.c_str(), "w")) { std::fputs(text, f); std::fclose(f); } + }; + mkdir((root + "/0").c_str(), 0700); + mkdir((root + "/1").c_str(), 0700); + mkdir((root + "/2").c_str(), 0700); + put(root + "/0/properties", kCpuNode); + put(root + "/1/properties", k780M); + put(root + "/README", "not a node"); + const std::vector v = read_topology(root); + CHECK(v.size() == 2); + CHECK(nodes_in_class(v)); + put(root + "/2/properties", k7900); + CHECK(!nodes_in_class(read_topology(root))); + CHECK(read_topology(root + "/none").empty()); + for (const char* p : {"/0/properties", "/1/properties", "/2/properties", "/README"}) std::remove((root + p).c_str()); + for (const char* p : {"/0", "/1", "/2", ""}) rmdir((root + p).c_str()); + } else { + CHECK(!"mkdtemp failed"); + } +#endif +} + // a fake GPU layer for the stop: a clock in seconds, a GPU that finishes after `busy_s` once released (never when `hung`) struct FakeGpu { double t = 0.0; @@ -117,6 +192,7 @@ void stop_order() { int main() { switches(); load_order(); + topology(); stop_order(); if (failed) { std::fprintf(stderr, "kfd_quiet_test: %d check(s) failed\n", failed);