From b490766d40e617a15b7eaefe4d8041d9aa3c6284 Mon Sep 17 00:00:00 2001 From: Sarah Date: Sat, 5 Dec 2026 13:31:33 +0100 Subject: [PATCH] [Engine] poly: opt-in --pin-workers (Linux): keep each CPU expert thread on one CPU strata-poly leaves its CPU expert threads to the scheduler on Linux. --pin-workers (or STRATA_POLY_PIN_WORKERS) pins each worker thread to one CPU: `auto` puts one thread on every performance core but the first (where the calling thread runs), then the efficiency cores (/sys/devices/cpu_atom/cpus), then the cores' second hardware threads; a list such as 2,4,6-9 pins them in that order. It reads the process's own affinity mask, so it stays within the NUMA node the engine already binds to. Default off: nothing changes unless it is set. Not on Windows (logs that the threads are not pinned). Qwen4-35B-A3B UD-IQ4_XS, RTX 4060 Laptop + i9-12900H, --kv q8_0, 300 tokens, interleaved, 5 rounds each: balance_performance EPP (default): unpinned 80.85 +- 3.68, auto 80.24 +- 1.03 tok/s performance EPP: unpinned 77.31 +- 3.61, auto 75.90 +- 3.01 tok/s Not faster; less run-to-run spread with the default power setting only. workers_test: CPU list parsing, the order for a hybrid Intel and an AMD-like layout, and pinning a real pool. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW --- docs/GLM.md | 1 + docs/SETTINGS.md | 3 +- engine/src/poly/cpu_experts.cpp | 117 +++++++++++++++++++++++++++ engine/src/poly/cpu_experts.hpp | 22 +++++ engine/src/poly/engine.cpp | 20 +++++ engine/src/poly/engine.hpp | 3 + engine/src/poly/main.cpp | 4 + engine/src/poly/workers_test.cpp | 48 +++++++++++ terminal/data/settings-register.json | 35 ++++++++ 9 files changed, 252 insertions(+), 1 deletion(-) diff --git a/docs/GLM.md b/docs/GLM.md index b0d8a18a2c..6f81223e04 100644 --- a/docs/GLM.md +++ b/docs/GLM.md @@ -256,6 +256,7 @@ goes on from where it stopped, and an edit goes back to the nearest checkpoint. | `--expert-profile FILE` | | expert use counts, read at the start and written back after an answer, every 10 seconds at most, and at the end | | `--no-gpu-experts` | | keep every routed expert on the CPU | | `--any-node` | | on a PC with several NUMA nodes, let the CPU threads run on any of them | +| `--pin-workers MODE` | off | Linux: keep each CPU thread on one CPU. `auto`: one thread a performance core but the first, then the efficiency cores, then the second threads of the cores; or a list such as `2,4,6-9`; also `STRATA_POLY_PIN_WORKERS`. On an i9-12900H laptop it was not faster; with the default `balance_performance` power setting the speed varied less from run to run | | `--no-graphs` | | launch a token's kernels one by one, not as the CUDA graph a layer that is recorded at the first token | | `--expert-swaps N` | 64 | while an answer is written, experts on their way into VRAM at a time, each in place of a cached one the answer uses less; 0 = the cache stays as it was loaded | | `--window N` | 4; 8 for the other models | the most tokens that go through the model together while an answer is written: the known one and guesses after it: the draft block's three at most, and more than three only from a repeat of eight tokens or more; 1 = no guesses | diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index d653eace9f..64eed3a29f 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -11,7 +11,7 @@ A number or a word is set like any environment variable, for example `STRATA_KVA (`terminal/strata-.json`), which the server passes to the engine. The Strata project's Terminal writes it there with `strata launch --set NAME=value`. -## Switches you can turn on (365) +## Switches you can turn on (366) Off unless you set them. The last column says where each one is described. @@ -267,6 +267,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_PIPE_BYIELD_NODRAIN` | on / off | NVIDIA and AMD | [BATCHING.md](BATCHING.md) | | `STRATA_PLE_COMPACT` | on / off | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | | `STRATA_PLE_COPY_STREAM` | on / off | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | +| `STRATA_POLY_PIN_WORKERS` | text | NVIDIA and AMD | [GLM.md](GLM.md) | | `STRATA_POLY_STAGE_RING` | on / off | NVIDIA and AMD | [inkling.md](models/inkling.md) | | `STRATA_POLY_STREAM_STAGE` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | | `STRATA_POOL_CORES` | on / off | NVIDIA and AMD, Intel Arc | [DETAILS.md](DETAILS.md), [release-notes-0.2.2.md](release-notes-0.2.2.md) | diff --git a/engine/src/poly/cpu_experts.cpp b/engine/src/poly/cpu_experts.cpp index f4b3c73f83..948331031f 100644 --- a/engine/src/poly/cpu_experts.cpp +++ b/engine/src/poly/cpu_experts.cpp @@ -1,6 +1,11 @@ // src/poly/cpu_experts.cpp - see the header. #include "cpu_experts.hpp" +#if defined(__linux__) +#include +#include +#endif + #include "strata/platform/cpu_relax.hpp" #include "strata/poly/cpu_topology.hpp" #include "strata/quant/act.hpp" @@ -11,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -140,6 +146,117 @@ Workers::Workers(int threads) : busy_((size_t) std::max(threads - 1, 0)) { for (int i = 1; i < threads; ++i) th_.emplace_back([this, i] { loop(i - 1); }); } +int Workers::pin(const std::vector& cpus) { + if (cpus.empty()) return 0; +#if defined(__linux__) + int done = 0; + for (size_t i = 0; i < th_.size(); ++i) { + const int c = cpus[i % cpus.size()]; + if (c < 0 || c >= CPU_SETSIZE) continue; + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(c, &set); + if (pthread_setaffinity_np(th_[i].native_handle(), sizeof set, &set) == 0) ++done; + } + return done; +#else + return 0; +#endif +} + +bool parse_cpu_list(const std::string& s, std::vector& out) { + out.clear(); + size_t i = 0; + const auto number = [&](int& v) { + if (i >= s.size() || s[i] < '0' || s[i] > '9') return false; + long n = 0; + for (; i < s.size() && s[i] >= '0' && s[i] <= '9'; ++i) { + n = n * 10 + (s[i] - '0'); + if (n > 1 << 20) return false; + } + v = (int) n; + return true; + }; + while (i < s.size()) { + int a = 0, b = 0; + if (!number(a)) return false; + b = a; + if (i < s.size() && s[i] == '-') { + ++i; + if (!number(b) || b < a) return false; + } + for (int c = a; c <= b; ++c) out.push_back(c); + if (i < s.size()) { + if (s[i] != ',' || i + 1 == s.size()) return false; + ++i; + } + } + return !out.empty(); +} + +std::vector worker_cpu_order(std::vector cpus) { + std::vector order; + if (cpus.empty()) return order; + std::sort(cpus.begin(), cpus.end(), [](const CpuSlot& a, const CpuSlot& b) { return a.cpu < b.cpu; }); + // the first performance core (or the first core of all where there is none): the caller's + int first = -1; + for (const CpuSlot& c : cpus) + if (!c.efficient) { + first = c.core; + break; + } + if (first < 0) first = cpus.front().core; + std::vector seen, firsts, efficient, seconds, own; + for (const CpuSlot& c : cpus) { + if (c.core == first) { + own.push_back(c.cpu); + } else if (c.efficient) { + efficient.push_back(c.cpu); + } else if (std::find(seen.begin(), seen.end(), c.core) == seen.end()) { + seen.push_back(c.core); + firsts.push_back(c.cpu); + } else { + seconds.push_back(c.cpu); + } + } + for (const std::vector* v : {&firsts, &efficient, &seconds, &own}) order.insert(order.end(), v->begin(), v->end()); + return order; +} + +std::vector cpu_slots() { + std::vector out; +#if defined(__linux__) + cpu_set_t may; + if (sched_getaffinity(0, sizeof may, &may) != 0) return out; + const auto read_int = [](const std::string& path, int& v) { + FILE* f = std::fopen(path.c_str(), "r"); + if (!f) return false; + const bool ok = std::fscanf(f, "%d", &v) == 1; + std::fclose(f); + return ok; + }; + std::vector atom; // a hybrid Intel CPU's efficiency cores (the cpu_atom PMU's CPUs) + if (FILE* f = std::fopen("/sys/devices/cpu_atom/cpus", "r")) { + char buf[512] = {}; + if (std::fgets(buf, sizeof buf, f)) { + std::string s(buf); + while (!s.empty() && (s.back() == '\n' || s.back() == ' ')) s.pop_back(); + if (!parse_cpu_list(s, atom)) atom.clear(); + } + std::fclose(f); + } + for (int c = 0; c < CPU_SETSIZE; ++c) { + if (!CPU_ISSET(c, &may)) continue; + const std::string dir = "/sys/devices/system/cpu/cpu" + std::to_string(c) + "/topology/"; + int core = c, pkg = 0; + read_int(dir + "core_id", core); + read_int(dir + "physical_package_id", pkg); + out.push_back({c, pkg * 65536 + core, std::find(atom.begin(), atom.end(), c) != atom.end()}); + } +#endif + return out; +} + Workers::~Workers() { quit_.store(true); posted_.fetch_add(1); diff --git a/engine/src/poly/cpu_experts.hpp b/engine/src/poly/cpu_experts.hpp index 60f493e447..5b48675765 100644 --- a/engine/src/poly/cpu_experts.hpp +++ b/engine/src/poly/cpu_experts.hpp @@ -31,6 +31,9 @@ class Workers { void run(int n, const std::function& fn); int threads() const { return (int) th_.size() + 1; } + /// --pin-workers: worker i (not the calling thread) runs on CPU cpus[i % size] from now on. The workers pinned; + /// 0 where the system has no way to (only Linux here). + int pin(const std::vector& cpus); private: // Every thread takes tasks from next and counts what it ran down from left once, when it finds no more: with a @@ -58,6 +61,25 @@ class Workers { std::atomic quit_{false}; }; +/// A logical CPU for --pin-workers: its number, its physical core (package and core), and whether it is an +/// efficiency core of a hybrid CPU. +struct CpuSlot { + int cpu = 0; + int core = 0; + bool efficient = false; +}; + +/// The order --pin-workers auto gives the workers the CPUs in: one thread of each performance core but the first, +/// then the efficiency cores, then the performance cores' second threads, then the first core's. While the +/// workers are fewer, the first core and the second threads stay to the calling thread and the GPU driver's threads, +/// and two workers do not share a core. (Measured on an i9-12900H: workers on both threads of the P-cores were +/// slower than one a core.) +std::vector worker_cpu_order(std::vector cpus); +/// The CPUs this process may run on, with their cores and kinds: Linux's /sys; empty elsewhere. +std::vector cpu_slots(); +/// A CPU list as Linux writes one ("2,4,6-9") into its numbers; false on anything else. +bool parse_cpu_list(const std::string& s, std::vector& out); + /// A thread of its own that runs one function at a time beside the caller: start() hands it over, finish() returns /// once it has run and throws what it threw. Workers::run() returns when every task of a batch is done, so work /// handed to one of its tasks held the whole batch up while it ran. diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 746492db87..d3308f1608 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -4045,6 +4045,8 @@ void Engine::load(const Options& o) { if (block_ != dg_.get()) dg_.reset(); hi_ = end_ < 0 ? n_layer_ : end_; + // STRATA_POLY_PIN_WORKERS=auto|off|LIST: --pin-workers + if (const char* v = std::getenv("STRATA_POLY_PIN_WORKERS"); v && *v) opt_.pin_workers = v; // STRATA_POLY_STAGE_RING=1: --stage-ring 1 if (const char* v = std::getenv("STRATA_POLY_STAGE_RING"); v && *v) opt_.stage_ring = v[0] != '0'; // STRATA_POLY_STAGED_TILES=8,16,32: a prompt batch's staged experts go to llama.cpp's MMQ in size classes, the @@ -4507,6 +4509,24 @@ void Engine::load(const Options& o) { node_ = stay_near(spans, files, threads, node_share_); } workers_ = std::make_shared(threads); + if (!opt_.pin_workers.empty() && opt_.pin_workers != "off") { // --pin-workers + std::vector cpus; + if (opt_.pin_workers == "auto") { + cpus = worker_cpu_order(cpu_slots()); + } else if (!parse_cpu_list(opt_.pin_workers, cpus)) { + throw std::invalid_argument("--pin-workers takes auto, off or a CPU list such as 2,4,6-9, not '" + + opt_.pin_workers + "'"); + } + const int pinned = workers_->pin(cpus); + std::string used; + for (int i = 0; i < pinned; ++i) used += (i ? "," : "") + std::to_string(cpus[(size_t) i % cpus.size()]); + if (pinned > 0) + std::fprintf(stderr, "strata-poly: %d CPU threads pinned to CPUs %s (--pin-workers %s; the first " + "thread is not)\n", pinned, used.c_str(), opt_.pin_workers.c_str()); + else + std::fprintf(stderr, "strata-poly: --pin-workers %s: the CPU threads are not pinned (Linux only, or " + "no CPU to give them)\n", opt_.pin_workers.c_str()); + } } if (!aside_) aside_ = std::make_unique