Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
feat(cpu): add hybrid architecture awareness and --pool-affinity for P/E-core CPUs
Pravesh Khatana69743d3a5ccc
diff --git a/include/strata/kernels/cpu/pool.hpp b/include/strata/kernels/cpu/pool.hppindex dce9c2bfd..f3cd54e2a 100644--- a/include/strata/kernels/cpu/pool.hpp+++ b/include/strata/kernels/cpu/pool.hpp@@ -63,13 +63,35 @@ struct ExpertJobMulti {const void* nact[MAXT] = {};};+/// One logical processor per PHYSICAL core, so a worker is never scheduled onto an SMT sibling of another+/// worker. On the 6-core/12-thread machine this project measures on, `hardware_concurrency()/2` workers on+/// logical processors 0..5 would put every worker on a sibling pair and halve the useful bandwidth - which is+/// exactly the kind of error that shows up as "the CPU path is slower than the model says" with no clue why.+/// How worker threads are allocated across physical/logical CPU cores.+enum class PoolAffinity {+ Auto, ///< Hybrid: prioritize physical P-cores, then SMT, then E-cores (defaults to P-core count)+ PCores, ///< Restrict workers strictly to Performance cores and their SMT siblings+ All, ///< Legacy behavior: linear physical core order without hybrid distinction+};++struct CpuTopology {+ bool is_hybrid = false;+ int p_cores = 0; ///< Physical performance cores+ int p_threads = 0; ///< Total logical threads on performance cores+ int e_cores = 0; ///< Efficient cores+ std::vector<int> worker_cores; ///< Ordered logical core IDs for workers (excluding host if skip_first)+ int host_core = -1; ///< Logical core reserved for host thread+};++CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity = PoolAffinity::Auto);+/// One logical processor per PHYSICAL core, so a worker is never scheduled onto an SMT sibling of another/// worker. On the 6-core/12-thread machine this project measures on, `hardware_concurrency()/2` workers on/// logical processors 0..5 would put every worker on a sibling pair and halve the useful bandwidth - which is/// exactly the kind of error that shows up as "the CPU path is slower than the model says" with no clue why.////// `skip_first` drops the first core, which P2.S3 reserves for the host loop.-std::vector<int> physical_cores(bool skip_first);+std::vector<int> physical_cores(bool skip_first, PoolAffinity affinity = PoolAffinity::Auto);/// **THE RESERVATION IS A FICTION UNLESS THE HOST IS ACTUALLY PUT THERE.**///@@ -101,7 +123,8 @@ public:/// With `host_works`, `run()` claims jobs itself instead of spinning on `done_`, and the pool is six/// threads on six cores. `false` is the A/B arm and exists so the change is measurable rather than/// asserted - the counter it moves is `pool phases ... drain`, which is host-side and needs no profiler.- explicit ExpertPool(int n_workers = 0, bool pin = true, bool host_works = true);+ explicit ExpertPool(int n_workers = 0, bool pin = true, bool host_works = true,+ PoolAffinity affinity = PoolAffinity::Auto);/// The watchdog's view of the pool (issue #31): the batch, the counters, every thread's state.void diag(std::FILE* f) const;~ExpertPool();@@ -113,6 +136,12 @@ public:/// is on" is only true if the engine says which adaptation it took.bool host_works() const { return host_works_; }+ bool is_hybrid() const { return topo_.is_hybrid; }+ int p_cores() const { return topo_.p_cores; }+ int p_threads() const { return topo_.p_threads; }+ int e_cores() const { return topo_.e_cores; }+ PoolAffinity affinity() const { return affinity_; }+/// Publish `n` jobs, then block until every one has been claimed AND every worker has parked./// `jobs` must outlive the call (it does, and the workers never touch it afterwards).void run(ExpertJob* jobs, int n);@@ -239,6 +268,8 @@ private:};const NativeFmt* nfmt_ = nullptr;std::vector<SplitBufMulti> split_multi_;+ PoolAffinity affinity_ = PoolAffinity::Auto;+ CpuTopology topo_;};} // namespace strata::kernels::cpudiff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cppindex e2e5023e0..45e434703 100644--- a/src/kernels/cpu/pool.cpp+++ b/src/kernels/cpu/pool.cpp@@ -27,8 +27,8 @@ constexpr uint64_t pack_head(uint32_t epoch, uint32_t n, uint32_t i) {}} // namespace-std::vector<int> physical_cores(bool skip_first) {- std::vector<int> cores;+CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity) {+ CpuTopology topo;#if defined(_WIN32)// Ask the OS rather than assuming a layout. `hardware_concurrency()` returns LOGICAL processors, and on// every SMT machine half of them are siblings - pinning one worker to each of the first N would put two@@ -36,30 +36,111 @@ std::vector<int> physical_cores(bool skip_first) {DWORD len = 0;GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len);if (len == 0) {- for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) cores.push_back((int) i);- } else {- std::vector<char> buf(len);- if (GetLogicalProcessorInformationEx(RelationProcessorCore,- (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf.data(), &len)) {- const char* p = buf.data();- const char* end = p + len;- while (p < end) {- const auto* e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p;- if (e->Relationship == RelationProcessorCore) {- const GROUP_AFFINITY& g = e->Processor.GroupMask[0];- for (int bit = 0; bit < 64; ++bit)- if (g.Mask & (1ull << bit)) { cores.push_back((int) (g.Group * 64 + bit)); break; }+ for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) topo.worker_cores.push_back((int) i);+ if (skip_first && !topo.worker_cores.empty()) {+ topo.host_core = topo.worker_cores.front();+ topo.worker_cores.erase(topo.worker_cores.begin());+ }+ return topo;+ }+ std::vector<char> buf(len);+ if (GetLogicalProcessorInformationEx(RelationProcessorCore,+ (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf.data(), &len)) {+ const char* p = buf.data();+ const char* end = p + len;+ struct CoreDesc {+ uint8_t efficiency = 0;+ bool has_smt = false;+ std::vector<int> lps;+ };+ std::vector<CoreDesc> descs;+ while (p < end) {+ const auto* e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p;+ if (e->Relationship == RelationProcessorCore) {+ CoreDesc cd;+ cd.efficiency = e->Processor.EfficiencyClass;+ cd.has_smt = (e->Processor.Flags & LTP_PC_SMT) != 0;+ const GROUP_AFFINITY& g = e->Processor.GroupMask[0];+ for (int bit = 0; bit < 64; ++bit) {+ if (g.Mask & (1ull << bit)) {+ cd.lps.push_back((int) (g.Group * 64 + bit));+ }+ }+ if (!cd.lps.empty()) {+ descs.push_back(std::move(cd));+ }+ }+ p += e->Size;+ }++ uint8_t min_eff = 255, max_eff = 0;+ for (const auto& c : descs) {+ min_eff = (std::min)(min_eff, c.efficiency);+ max_eff = (std::max)(max_eff, c.efficiency);+ }++ topo.is_hybrid = (max_eff > min_eff);+ if (topo.is_hybrid) {+ for (const auto& c : descs) {+ if (c.efficiency == max_eff) {+ topo.p_cores++;+ topo.p_threads += (int) c.lps.size();+ } else {+ topo.e_cores++;+ }+ }+ } else {+ topo.p_cores = (int) descs.size();+ for (const auto& c : descs) topo.p_threads += (int) c.lps.size();+ }++ if (affinity == PoolAffinity::All || !topo.is_hybrid) {+ for (const auto& c : descs) topo.worker_cores.push_back(c.lps[0]);+ if (skip_first && !topo.worker_cores.empty()) {+ topo.host_core = topo.worker_cores.front();+ topo.worker_cores.erase(topo.worker_cores.begin());+ }+ return topo;+ }++ // Hybrid CPU with Auto or PCores affinity:+ // Prioritize Performance cores:+ // 1. Primary logical processor of each P-core (avoids SMT resource contention)+ // 2. SMT sibling logical processors of P-cores+ // 3. E-cores (only as overflow in Auto mode)+ std::vector<int> p_primaries;+ std::vector<int> p_siblings;+ std::vector<int> e_cores;++ for (const auto& c : descs) {+ if (c.efficiency == max_eff) {+ p_primaries.push_back(c.lps[0]);+ for (size_t s = 1; s < c.lps.size(); ++s) {+ p_siblings.push_back(c.lps[s]);}- p += e->Size;+ } else {+ for (int lp : c.lps) e_cores.push_back(lp);}}++ if (skip_first && !p_primaries.empty()) {+ topo.host_core = p_primaries.front();+ p_primaries.erase(p_primaries.begin());+ }++ for (int cpu : p_primaries) topo.worker_cores.push_back(cpu);+ for (int cpu : p_siblings) topo.worker_cores.push_back(cpu);+ if (affinity != PoolAffinity::PCores) {+ for (int cpu : e_cores) topo.worker_cores.push_back(cpu);+ }+ return topo;}#else// The logical CPUs this process may run on, ONE PER PHYSICAL CORE (issue #40): SMT siblings share a core's// load/store bandwidth, so a worker on each would put two workers on one core, as the Windows branch above// explains. sysfs names each CPU's (package, core); the first allowed CPU of each pair is kept, so a taskset// that leaves out the first sibling still gets its core. Without sysfs every allowed CPU counts, as before.- auto topo = [](int cpu, const char* what) -> long {+ auto topo_read = [](int cpu, const char* what) -> long {char path[96];std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/topology/%s", cpu, what);long v = -1;@@ -69,6 +150,17 @@ std::vector<int> physical_cores(bool skip_first) {}return v;};+ auto cap_read = [](int cpu) -> long {+ char path[96];+ std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cpu_capacity", cpu);+ long v = -1;+ if (std::FILE* f = std::fopen(path, "r")) {+ if (std::fscanf(f, "%ld", &v) != 1) v = -1;+ std::fclose(f);+ }+ return v;+ };+std::vector<int> allowed;cpu_set_t set;CPU_ZERO(&set);@@ -78,19 +170,96 @@ std::vector<int> physical_cores(bool skip_first) {} else {for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) allowed.push_back((int) i);}- std::vector<std::pair<long, long>> seen;++ struct CoreLinux {+ int cpu = -1;+ long pkg = -1;+ long core = -1;+ long cap = -1;+ bool is_sibling = false;+ };+ std::vector<CoreLinux> all_cpus;+ std::vector<std::pair<long, long>> seen_phys;+ long max_cap = 0, min_cap = 1000000;+for (int cpu : allowed) {- const long pkg = topo(cpu, "physical_package_id"), core = topo(cpu, "core_id");- if (pkg >= 0 && core >= 0) {- const std::pair<long, long> key{pkg, core};- if (std::find(seen.begin(), seen.end(), key) != seen.end()) continue; // an SMT sibling- seen.push_back(key);+ CoreLinux cl;+ cl.cpu = cpu;+ cl.pkg = topo_read(cpu, "physical_package_id");+ cl.core = topo_read(cpu, "core_id");+ cl.cap = cap_read(cpu);+ if (cl.cap > 0) {+ max_cap = (std::max)(max_cap, cl.cap);+ min_cap = (std::min)(min_cap, cl.cap);+ }+ if (cl.pkg >= 0 && cl.core >= 0) {+ const std::pair<long, long> key{cl.pkg, cl.core};+ if (std::find(seen_phys.begin(), seen_phys.end(), key) != seen_phys.end()) {+ cl.is_sibling = true;+ } else {+ seen_phys.push_back(key);+ }}- cores.push_back(cpu);+ all_cpus.push_back(cl);}++ topo.is_hybrid = (max_cap > 0 && max_cap > min_cap);+ if (topo.is_hybrid) {+ for (const auto& cl : all_cpus) {+ if (cl.cap == max_cap) {+ if (!cl.is_sibling) topo.p_cores++;+ topo.p_threads++;+ } else {+ if (!cl.is_sibling) topo.e_cores++;+ }+ }+ } else {+ topo.p_cores = (int) seen_phys.size();+ topo.p_threads = (int) all_cpus.size();+ }++ if (affinity == PoolAffinity::All || !topo.is_hybrid) {+ for (const auto& cl : all_cpus) {+ if (!cl.is_sibling) topo.worker_cores.push_back(cl.cpu);+ }+ if (skip_first && !topo.worker_cores.empty()) {+ topo.host_core = topo.worker_cores.front();+ topo.worker_cores.erase(topo.worker_cores.begin());+ }+ return topo;+ }++ // Hybrid CPU on Linux:+ std::vector<int> p_primaries;+ std::vector<int> p_siblings;+ std::vector<int> e_cores;++ for (const auto& cl : all_cpus) {+ if (cl.cap == max_cap) {+ if (!cl.is_sibling) p_primaries.push_back(cl.cpu);+ else p_siblings.push_back(cl.cpu);+ } else {+ e_cores.push_back(cl.cpu);+ }+ }++ if (skip_first && !p_primaries.empty()) {+ topo.host_core = p_primaries.front();+ p_primaries.erase(p_primaries.begin());+ }++ for (int cpu : p_primaries) topo.worker_cores.push_back(cpu);+ for (int cpu : p_siblings) topo.worker_cores.push_back(cpu);+ if (affinity != PoolAffinity::PCores) {+ for (int cpu : e_cores) topo.worker_cores.push_back(cpu);+ }+ return topo;#endif- if (skip_first && !cores.empty()) cores.erase(cores.begin());- return cores;+ return topo;+}++std::vector<int> physical_cores(bool skip_first, PoolAffinity affinity) {+ return detect_cpu_topology(skip_first, affinity).worker_cores;}namespace {@@ -173,11 +342,17 @@ void ExpertPool::diag(std::FILE* f) const {std::fprintf(f, " for %lld ms\n", (long long) (now_ms() - hstate_ms_.load()));}-ExpertPool::ExpertPool(int n_workers, bool pin, bool host_works) : host_works_(host_works) {+ExpertPool::ExpertPool(int n_workers, bool pin, bool host_works, PoolAffinity affinity)+ : host_works_(host_works), affinity_(affinity), topo_(detect_cpu_topology(true, affinity)) {if (const char* e = std::getenv("STRATA_POOL_SPIN_US")) // a test knob; see kSpinBeforeSleepspin_before_sleep_ = std::chrono::microseconds((std::max)(0, std::atoi(e)));- const std::vector<int> cores = physical_cores(true);- n_ = n_workers > 0 ? n_workers : (int) cores.size();+ if (n_workers > 0) {+ n_ = n_workers;+ } else if (topo_.is_hybrid && affinity_ != PoolAffinity::All) {+ n_ = (std::max)(1, topo_.p_cores - 1);+ } else {+ n_ = (int) topo_.worker_cores.size();+ }if (n_ < 1) n_ = 1;scratch_.resize((size_t) n_);wstate_.reset(new std::atomic<int32_t>[(size_t) n_]);@@ -189,7 +364,7 @@ ExpertPool::ExpertPool(int n_workers, bool pin, bool host_works) : host_works_(hsplit_multi_.resize((size_t) kMaxSplitMulti);threads_.reserve((size_t) n_);for (int i = 0; i < n_; ++i) {- const int core = pin ? (i < (int) cores.size() ? cores[(size_t) i] : -1) : -1;+ const int core = pin ? (i < (int) topo_.worker_cores.size() ? topo_.worker_cores[(size_t) i] : -1) : -1;threads_.emplace_back([this, i, core] {pin_this_thread(core);worker(i);diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 45412f906..740952035 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -256,6 +256,7 @@ struct Options {bool graph_only = false;bool gpu_only_full = false; ///< R0.3: pre + post + head, the true per-token GPU floorint pool_workers = 0; ///< R2.2: 0 = "all physical cores minus the host's"; >0 overrides+ strata::kernels::cpu::PoolAffinity pool_affinity = strata::kernels::cpu::PoolAffinity::Auto;/// R2.2's first half, as an A/B arm. **ON by default**, because the measurement that justifies it is the/// pool's own drain: 33.7 GB/s against 5/6 x 44.14 = 36.8 for five workers, on a machine whose sixth core/// is reserved for a host thread that has nothing to do while the drain runs.@@ -550,8 +551,11 @@ void usage() {" pool is six threads on six cores instead of five plus an idle core;\n"" this flag restores the five-worker form for comparison on `pool phases`.\n"" --pool-workers N R2.2: CPU expert pool worker count. Default 0 = every physical core\n"- " except the one the host loop spins on. A sweep is how the pool's\n"- " deviation from `cpu_s2` is attributed.\n"+ " except the one the host loop spins on (or P-core count minus 1 on\n"+ " hybrid CPUs). A sweep is how the pool's deviation from `cpu_s2` is attributed.\n"+ " --pool-affinity MODE Worker CPU affinity: auto (default: prioritize P-cores), p-cores\n"+ " (strictly performance cores), or all (all physical cores without\n"+ " hybrid distinction).\n"" --coupled-draft enable coupled draft sampling for MTP drafter under sampling (STRATA_SPEC_COUPLED)\n"" --no-coupled-draft disable coupled draft sampling (propose argmax drafts)\n"" --mmap-experts R2.1: opt OUT of the resident expert arena, back to MapViewOfFile.\n"@@ -1081,6 +1085,16 @@ int main(int argc, char** argv) {else if (a == "--graph-only") o.graph_only = true;else if (a == "--gpu-only-full") o.gpu_only_full = true;else if (a == "--pool-workers") o.pool_workers = std::atoi(next("--pool-workers"));+ else if (a == "--pool-affinity") {+ const std::string v = next("--pool-affinity");+ if (v == "auto") o.pool_affinity = strata::kernels::cpu::PoolAffinity::Auto;+ else if (v == "p-cores" || v == "pcores") o.pool_affinity = strata::kernels::cpu::PoolAffinity::PCores;+ else if (v == "all") o.pool_affinity = strata::kernels::cpu::PoolAffinity::All;+ else {+ std::fprintf(stderr, "strata generate: unknown --pool-affinity value '%s' (expected auto, p-cores, or all)\n", v.c_str());+ return 2;+ }+ }else if (a == "--no-host-worker") o.no_host_worker = true;else if (a == "--no-ple-prefetch") o.no_ple_prefetch = true;else if (a == "--coupled-draft") o.coupled_draft = true;@@ -2521,7 +2535,13 @@ int main(int argc, char** argv) {#endifsrcp = &arena_src;}- strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker);+ strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker, o.pool_affinity);+ if (pool.is_hybrid()) {+ const char* aff_str = pool.affinity() == strata::kernels::cpu::PoolAffinity::PCores ? "p-cores" :+ pool.affinity() == strata::kernels::cpu::PoolAffinity::All ? "all" : "auto";+ std::fprintf(stderr, "strata generate: hybrid CPU detected (%d P-cores / %d threads, %d E-cores), pool workers: %d, affinity: %s\n",+ pool.p_cores(), pool.p_threads(), pool.e_cores(), pool.workers(), aff_str);+ }if (o.no_ple_prefetch) strata::kernels::ple_prefetch_enable(false);// ---- R4's slot storage. Allocated AFTER the weights and the session, so `cudaMemGetInfo` inside `open`// sees the memory this process actually has left rather than the card's idle figure - and refuses with both