Skip to content

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++

cpu: --pool-tasks N sets the row tasks per batched expert phase

Unmaple6b1e303f16d5
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 41dca1a43..d9c895914 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1203,6 +1203,10 @@ if(STRATA_NATIVE_EXPERTS)
add_executable(router_dot_parity src/kernels/cpu/router_dot_parity.cpp)
target_link_libraries(router_dot_parity PRIVATE strata_kernels_cpu)
if(STRATA_BUILD_TESTS)
+ add_executable(pool_tasks_test src/kernels/cpu/pool_tasks_test.cpp)
+ target_link_libraries(pool_tasks_test PRIVATE strata_kernels_cpu)
+ add_test(NAME pool_tasks_test COMMAND pool_tasks_test)
+ set_tests_properties(pool_tasks_test PROPERTIES TIMEOUT 180)
add_test(NAME router_dot_parity COMMAND router_dot_parity)
add_test(NAME router_dot_parity_avx COMMAND ${CMAKE_COMMAND} -E env STRATA_FORCE_ISA=avx $<TARGET_FILE:router_dot_parity>)
endif()
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 5822913c7..1009a189d 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -466,6 +466,16 @@ speed with each setting and keeps one only when it is more than 3% faster. The r
Measuring the worker count needs a fresh engine, so the model is loaded more than once: the PC is busy, and can
stop responding for a minute or two, once per restart. It then starts the model, like a plain `START-HERE.bat`.
+**Manual CPU task granularity.** `--pool-tasks N` sets the target total number of row tasks in each batched
+CPU expert Gate/Up and Down phase, not the number of threads or tasks per expert. The default `0` keeps
+three tasks per participating thread (including the host when enabled). Values `1..4096` are capped by
+each phase's row count; native batches larger than the pool's capacity apply the target to each sub-batch.
+For example, `--pool-workers 13 --pool-tasks 192` uses 14 participating threads with the default host worker.
+More tasks can reduce imbalance between cores, but also add scheduling overhead: compare against `0` with
+the same worker count and workload. This does not change kernels, phase barriers, or PCIe placement, and
+does not affect the legacy single-token/oracle fallback. Setup's `--calibrate` does not tune it yet.
+For the server, add `"--pool-tasks", "192"` to the existing `args` list in its configuration, then restart it.
+
### Running it at startup (Task Scheduler)
To have the model up at logon, people start the serve from **Task Scheduler** (or a service). Beware: Windows
diff --git a/include/strata/kernels/cpu/pool.hpp b/include/strata/kernels/cpu/pool.hpp
index 1b25493bc..6796e3917 100644
--- a/include/strata/kernels/cpu/pool.hpp
+++ b/include/strata/kernels/cpu/pool.hpp
@@ -148,7 +148,9 @@ public:
/// 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,
- PoolAffinity affinity = PoolAffinity::All);
+ PoolAffinity affinity = PoolAffinity::All, int tasks = 0);
+ /// Batched row phases: 0 keeps three tasks per participating thread; explicit counts are capped by rows.
+ static constexpr int kMaxTasks = 4096;
/// The watchdog's view of the pool (issue #31): the batch, the counters, every thread's state.
void diag(std::FILE* f) const;
~ExpertPool();
@@ -217,6 +219,8 @@ public:
static constexpr std::chrono::seconds kStall{60};
private:
+ int phase_tasks(int64_t rows) const;
+ int tasks_ = 0;
void worker(int id);
void drain(int id, ExpertScratch& scratch, uint32_t epoch);
void run_phase(int mode, int n_tasks);
diff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cpp
index 69411ee92..16e3fb2d8 100644
--- a/src/kernels/cpu/pool.cpp
+++ b/src/kernels/cpu/pool.cpp
@@ -11,6 +11,7 @@
#include <cstdio>
#include <cstdlib>
+#include <stdexcept>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
@@ -432,8 +433,9 @@ 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, PoolAffinity affinity)
- : host_works_(host_works), affinity_(affinity), topo_(detect_cpu_topology(true, affinity)) {
+ExpertPool::ExpertPool(int n_workers, bool pin, bool host_works, PoolAffinity affinity, int tasks)
+ : tasks_(tasks), host_works_(host_works), affinity_(affinity), topo_(detect_cpu_topology(true, affinity)) {
+ if (tasks < 0 || tasks > kMaxTasks) throw std::invalid_argument("expert pool tasks must be in 0..4096");
if (const char* e = std::getenv("STRATA_POOL_SPIN_US")) // a test knob; see kSpinBeforeSleep
spin_before_sleep_ = std::chrono::microseconds((std::max)(0, std::atoi(e)));
if (n_workers > 0) {
@@ -711,6 +713,10 @@ void ExpertPool::run_split(ExpertJob* jobs, int n) {
ms_drain_ += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
}
+int ExpertPool::phase_tasks(int64_t rows) const {
+ return tasks_ == 0 ? 3 * (n_ + (host_works_ ? 1 : 0)) : (int) (std::min)(rows, (int64_t) tasks_);
+}
+
void ExpertPool::run_split_multi(ExpertJobMulti* jobs, int n) {
if (n <= 0) return;
if (n > kMaxSplitMulti || expert_oracle_q8_0_enabled()) {
@@ -730,9 +736,8 @@ void ExpertPool::run_split_multi(ExpertJobMulti* jobs, int n) {
}
const auto t0 = std::chrono::steady_clock::now();
mjobs_ = jobs;
- const int threads = n_ + (host_works_ ? 1 : 0);
- mtasks_ = 3 * threads;
mrows_ = (int64_t) n * FF;
+ mtasks_ = phase_tasks(mrows_);
run_phase(3, mtasks_);
const auto t1 = std::chrono::steady_clock::now();
for (int e = 0; e < n; ++e)
@@ -740,6 +745,7 @@ void ExpertPool::run_split_multi(ExpertJobMulti* jobs, int n) {
act_quant_q8_1(split_multi_[(size_t) e].ff[t], FF, split_multi_[(size_t) e].a2[t]);
const auto t2 = std::chrono::steady_clock::now();
mrows_ = (int64_t) n * H;
+ mtasks_ = phase_tasks(mrows_);
run_phase(4, mtasks_);
const auto t3 = std::chrono::steady_clock::now();
ms_multi_gu += std::chrono::duration<double, std::milli>(t1 - t0).count();
@@ -758,9 +764,8 @@ void ExpertPool::run_split_multi_native(const NativeFmt& f, ExpertJobMulti* jobs
const int nb = (std::min)(kMaxSplitMulti, n - b0);
mjobs_ = jobs + b0;
nfmt_ = &f;
- const int threads = n_ + (host_works_ ? 1 : 0);
- mtasks_ = 3 * threads;
mrows_ = (int64_t) nb * FF;
+ mtasks_ = phase_tasks(mrows_);
const auto a = std::chrono::steady_clock::now();
run_phase(5, mtasks_);
const auto b = std::chrono::steady_clock::now();
@@ -770,6 +775,7 @@ void ExpertPool::run_split_multi_native(const NativeFmt& f, ExpertJobMulti* jobs
else native_quant_h(f, split_multi_[(size_t) e].ff[t], split_multi_[(size_t) e].hq[t]);
const auto c = std::chrono::steady_clock::now();
mrows_ = (int64_t) nb * H;
+ mtasks_ = phase_tasks(mrows_);
run_phase(6, mtasks_);
const auto d = std::chrono::steady_clock::now();
ms_multi_gu += std::chrono::duration<double, std::milli>(b - a).count();
diff --git a/src/kernels/cpu/pool_tasks_test.cpp b/src/kernels/cpu/pool_tasks_test.cpp
new file mode 100644
index 000000000..e08cf262b
--- /dev/null
+++ b/src/kernels/cpu/pool_tasks_test.cpp
@@ -0,0 +1,90 @@
+// CPU-only task partition parity. Optional real expert: pool_tasks_test GU_TYPE DOWN_TYPE BLOB_FILE
+#include "strata/kernels/cpu/pool.hpp"
+#include "strata/kernels/cpu/expert_layout.hpp"
+#include "ggml.h"
+
+#include <algorithm>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <limits>
+#include <stdexcept>
+#include <vector>
+
+namespace cpu = strata::kernels::cpu;
+
+int main(int argc, char** argv) {
+ if (argc != 1 && argc != 4) return 2;
+ cpu::NativeFmt f;
+ std::string err;
+ if (!cpu::native_fmt(argc == 4 ? std::atoi(argv[1]) : GGML_TYPE_Q4_0,
+ argc == 4 ? std::atoi(argv[2]) : GGML_TYPE_Q4_0, cpu::H, cpu::FF, f, err)) {
+ std::fprintf(stderr, "%s\n", err.c_str());
+ return 1;
+ }
+ std::vector<uint8_t> blob(f.bytes);
+ if (argc == 4) {
+ std::FILE* file = std::fopen(argv[3], "rb");
+ if (!file) return 2;
+ const size_t got = std::fread(blob.data(), 1, blob.size(), file);
+ std::fclose(file);
+ if (got != blob.size()) return 2;
+ } else {
+ std::vector<float> weights((size_t) cpu::H * cpu::FF);
+ for (size_t i = 0; i < weights.size(); ++i) weights[i] = 0.02f * std::sin((float) i * 0.17f);
+ ggml_quantize_chunk(GGML_TYPE_Q4_0, weights.data(), blob.data(), 0, cpu::FF, cpu::H, nullptr);
+ ggml_quantize_chunk(GGML_TYPE_Q4_0, weights.data(), blob.data() + f.up_off, 0, cpu::FF, cpu::H, nullptr);
+ ggml_quantize_chunk(GGML_TYPE_Q4_0, weights.data(), blob.data() + f.down_off, 0, cpu::H, cpu::FF, nullptr);
+ }
+ for (int tasks : {-1, cpu::ExpertPool::kMaxTasks + 1}) {
+ try { cpu::ExpertPool invalid(1, false, true, cpu::PoolAffinity::All, tasks); return 1; }
+ catch (const std::invalid_argument&) {}
+ }
+ std::vector<std::vector<uint8_t>> act(cpu::MAXT, std::vector<uint8_t>(cpu::kNativeActBytes));
+ std::vector<cpu::ActQ> qact(cpu::MAXT);
+ std::vector<float> x(cpu::H);
+ for (int t = 0; t < cpu::MAXT; ++t) {
+ for (int i = 0; i < cpu::H; ++i) x[i] = std::sin((float) (i + 13 * t) * 0.031f);
+ cpu::native_quant_act(f, x.data(), act[t].data());
+ cpu::act_quant_any(x.data(), cpu::H, qact[t]);
+ }
+ int checks = 0;
+ for (int n : {1, 7, cpu::ExpertPool::kMaxSplitMulti + 1}) {
+ std::vector<cpu::ExpertJobMulti> jobs(n);
+ std::vector<float> out((size_t) n * cpu::MAXT * cpu::H);
+ for (int e = 0; e < n; ++e) {
+ jobs[e].blob = blob.data();
+ jobs[e].nt = e % 3 == 0 ? 1 : e % 3 == 1 ? 4 : cpu::MAXT;
+ for (int t = 0; t < jobs[e].nt; ++t) {
+ jobs[e].nact[t] = act[t].data();
+ jobs[e].act[t] = &qact[t];
+ jobs[e].out[t] = out.data() + ((size_t) e * cpu::MAXT + t) * cpu::H;
+ }
+ }
+ { // The default partition is the arithmetic reference; inactive token slots remain zero.
+ cpu::ExpertPool baseline(3, false);
+ baseline.run_split_multi_native(f, jobs.data(), n);
+ }
+ const auto ref = out;
+ for (float v : ref) if (!std::isfinite(v)) return 1;
+ for (bool host : {false, true}) for (int workers : {1, 3})
+ for (int tasks : {0, 1, 41, 128, 192, 256, cpu::ExpertPool::kMaxTasks}) {
+ cpu::ExpertPool pool(workers, false, host, cpu::PoolAffinity::All, tasks);
+ for (int repeat = 0; repeat < 2; ++repeat) {
+ std::fill(out.begin(), out.end(), 0.0f);
+ for (const auto& job : jobs) for (int t = 0; t < job.nt; ++t)
+ std::fill(job.out[t], job.out[t] + cpu::H, std::numeric_limits<float>::quiet_NaN());
+ pool.run_split_multi_native(f, jobs.data(), n);
+ if (std::memcmp(ref.data(), out.data(), out.size() * sizeof(float)) != 0) {
+ std::fprintf(stderr, "mismatch n=%d workers=%d host=%d tasks=%d repeat=%d\n",
+ n, workers, host, tasks, repeat);
+ return 1;
+ }
+ ++checks;
+ }
+ }
+ }
+ std::printf("pool tasks: %d bitwise comparisons passed\n", checks);
+ return 0;
+}
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index cdab27066..fbce51449 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -408,6 +408,7 @@ struct Options {
bool graph_only = false;
bool gpu_only_full = false; ///< R0.3: pre + post + head, the true per-token GPU floor
int pool_workers = 0; ///< R2.2: 0 = "all physical cores minus the host's"; >0 overrides
+ int pool_tasks = 0; ///< Batched CPU expert tasks per phase; 0 keeps the existing policy
/// #272: the pool's core layout; `all` (the default) is the layout it always had, auto / p-cores are opt-in
strata::kernels::cpu::PoolAffinity pool_affinity = strata::kernels::cpu::PoolAffinity::All;
/// --host-core first|last (STRATA_HOST_CORE): the host thread's core (see HostCore in pool.hpp)
@@ -839,6 +840,8 @@ void usage() {
" except the one the host loop spins on (with --pool-affinity auto or\n"
" p-cores on a hybrid CPU: P-cores minus 1). A sweep is how the pool's\n"
" deviation from `cpu_s2` is attributed.\n"
+ " --pool-tasks N Batched CPU expert tasks per GU/Down phase (0..4096). Default 0 =\n"
+ " 3 per participating thread; positive counts are capped by row count.\n"
" --host-core WHERE The host thread's core: first (the default: the first physical core, as always) or\n"
" last (the last physical core, the workers on the others). Windows sends a GPU's\n"
" interrupts to one logical processor, usually the first, and every copy that lands\n"
@@ -1654,6 +1657,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-tasks") {
+ const char* v = next("--pool-tasks");
+ char* end = nullptr;
+ const long tasks = std::strtol(v, &end, 10);
+ if (end == v || *end != '\0' || tasks < 0 || tasks > strata::kernels::cpu::ExpertPool::kMaxTasks) {
+ std::fprintf(stderr, "strata generate: --pool-tasks expects an integer in 0..4096 (0 = automatic)\n");
+ return 2;
+ }
+ o.pool_tasks = (int) tasks;
+ }
else if (a == "--host-core") {
o.host_core = next("--host-core");
if (o.host_core != "first" && o.host_core != "last") {
@@ -4095,7 +4108,12 @@ int main(int argc, char** argv) {
#endif
srcp = &arena_src;
}
- strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker, o.pool_affinity);
+ strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker,
+ o.pool_affinity, o.pool_tasks);
+ std::fprintf(stderr, "strata generate: CPU pool tasks/phase: %d%s, participating threads: %d\n",
+ o.pool_tasks ? o.pool_tasks : 3 * (pool.workers() + (pool.host_works() ? 1 : 0)),
+ o.pool_tasks ? " (capped by rows)" : " (automatic)",
+ pool.workers() + (pool.host_works() ? 1 : 0));
if (pool.is_hybrid() && pool.affinity() != strata::kernels::cpu::PoolAffinity::All) {
const char* aff_str = pool.affinity() == strata::kernels::cpu::PoolAffinity::PCores ? "p-cores" :
pool.affinity() == strata::kernels::cpu::PoolAffinity::All ? "all" : "auto";