diff --git a/docs/DETAILS.md b/docs/DETAILS.md index bdd6acf0..12105a25 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -272,6 +272,31 @@ GPU nor the RAM budget holds (only pages - the experts computed are the same; `S is what runs [Unsloth's UD-Q4_K_XL](UNSLOTH_Q4.md) (72 GiB of experts) on a 64 GB PC: 7-8.5 tokens/s at N = 40 on an RTX 5070, against ~3 tokens/s before these changes. +**Opt-in switches measured on a hybrid Intel CPU under Windows (Core i7-13700K, 8 P + 8 E cores, RTX 5090, 64 GB RAM, +Windows 11, Swift 1.5 IQ3_XXS at 128K):** +- `--pool-affinity measure` (hybrid CPUs, native packs): the `all` and `p-cores` layouts are timed at start on the + pack's own experts (about a second) and the faster one is taken. Engine 0.1.41 built from source, the same prompt: + `all` (15 workers) spent 6.7 ms per verify window in the CPU pool and decoded 148 tok/s, `p-cores` (7 workers) 2.2 ms + and 188-191 tok/s; in another run of the same day `all` took 2.4 ms, so the difference comes and goes. The + measurement is taken once, at start: over four server starts of one day it read `all` at 14.8, 21.9 and 25.3 ms + against 6.1 to 6.2 ms for `p-cores` and took `p-cores`, and once read `all` at 6.6 ms against 8.6 ms and took `all`. + It does not look again during a session, so where the E-cores' speed changes while the engine runs, + `--pool-affinity p-cores` is the steady choice. Not measured: other CPUs, Linux, AMD cards. + +**Exact one-token expert rows (0.1.45, opt-in, `STRATA_CPU_IQ_ALL_EXACT1=1`, #1415 #1544):** below the #152 rule +(a lone token in an expert's group) an AVX-2 CPU without AVX-512 runs ggml-cpu's `vec_dot` for the i-quant gate/up rows. +With the switch, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS and IQ3_S take Strata's AVX-2 decode instead, with ggml's float order +(the same per-lane integer sums, one fmadd per block, the format constant after `hsum_float_8`), so the output does not +change, only the time (`iq_avx2_parity`: 0 of 5,120 outputs per format differ from ggml's; the identity gate with the +switch on is 10/10 plus exact_pp 4K/20K identical on RTX 3060, 2x A4000 and R9700 boxes). It is used only where +ggml-cpu itself is the AVX2 build (not an ISA-floor build, not AVX-512) and not with `STRATA_IQ_MT_MIN=1`. Per expert, +gate/up, one thread: IQ3_S -32% on a Threadripper PRO 3975WX (Zen 2) and -11% on a Ryzen 5 5600X (Zen 3), IQ2_XXS -5..-6%, +IQ3_XXS -5% (Zen 2) / +1% (Zen 3), IQ2_S 0% (Zen 2) / +7% (Zen 3, slower); on a Core Ultra 7 265 (VNNI, gather) IQ3_S -60%, +IQ3_XXS -39%, IQ2_XXS -34%, IQ2_XS -24%, IQ2_S -3% on a P-core. End to end, with the engine's decode timing: the CPU part of a +window falls 5% on a Xeon E5-2643 v3 + RTX A4000 (22.2 -> 21.1 ms of a 46.9 ms window, decode +0.5% over 10 interleaved pairs, ++2.5% on the story and code prompts) and on a Core Ultra 7 265 + RTX 3060 (8.2 -> 7.8 ms of 39.5 ms, decode +0.4%), and does not +move on a Threadripper PRO 3975WX + R9700 (14.5 ms, +0.4%, 5 pairs); no case reached +3% end to end, so the switch stays off. + **Switches added in 0.1.40 (all off unless noted; none changes the default output):** - `--kv-grow` (or `STRATA_KV_GROW=1`; `--no-kv-grow` turns it off): the K/V takes VRAM only for the cells the requests reach, and the expert cache holds the rest, giving slots back as the context grows. It needs one GPU, an expert @@ -279,7 +304,11 @@ RTX 5070, against ~3 tokens/s before these changes. and with `--batch`, `--vram-elastic` or `--peer-device`, the engine says so and stays off. - `--host-core last` (or `STRATA_HOST_CORE=last`, Windows): the host thread runs on the last physical core and the workers take the first. Windows sends a GPU's interrupts to the first core, where a host spinning on the GPU's flags - waits for them (`--host-core first` is the default; the startup log names the cores). + waits for them (`--host-core first` is the default; the startup log names the cores). It leaves a hybrid CPU as it + is. `--host-core sibling` (or `STRATA_HOST_CORE=sibling`) keeps the host on the first core but on its other + hardware thread (SMT), so the interrupts keep the first logical processor and the workers keep every core; it works + on hybrid CPUs too, and is `first` on a core without SMT. On a 4060 Ti + 5080 layer split with an i9-14900KF it + turned a decode that swung by ±6% from run to run into a steady one, 8% faster on average. - `STRATA_ADAPT_LAG=2` (#764): a window takes the adaptive tier's swaps once they are two windows old (default 1, as 0.1.39). `STRATA_PREFILL_EQUAL=1` (#693): a prompt segment is read in chunks of equal size, not full chunks and a short last one (changes the rounding). `STRATA_OWNED_PRICE=exact` (#796): the cache sizing prices the prompt path's diff --git a/include/strata/kernels/cpu/iq_avx2.hpp b/include/strata/kernels/cpu/iq_avx2.hpp index 519efa2c..3147ed55 100644 --- a/include/strata/kernels/cpu/iq_avx2.hpp +++ b/include/strata/kernels/cpu/iq_avx2.hpp @@ -11,6 +11,11 @@ namespace strata::kernels::cpu { bool iq256_supported(int ggml_type) noexcept; +/// Bit-identical to ggml's singleton gate/up for IQ2_XXS, IQ2_XS, IQ3_XXS and IQ3_S. +/// The caller checks AVX2 support and passes one Q8_K activation. +void iq256_gu_rows_exact_one(int ggml_type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, + const void* act, float* ff, int r0, int r1); + /// ff[t][r] = silu(gate_r . a[t]) * (up_r . a[t]), rows [r0, r1); gate rows at blob, up rows at blob + up_off. void iq256_gu_rows(int ggml_type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, int nt, float* const* ff, int r0, int r1); diff --git a/include/strata/kernels/cpu/pool.hpp b/include/strata/kernels/cpu/pool.hpp index 6796e391..686abd6e 100644 --- a/include/strata/kernels/cpu/pool.hpp +++ b/include/strata/kernels/cpu/pool.hpp @@ -78,15 +78,19 @@ struct CpuTopology { int e_cores = 0; ///< Efficient cores std::vector worker_cores; ///< Ordered CPU IDs; on Windows, group * 64 + processor within the group int host_core = -1; ///< Logical core reserved for host thread + int host_sibling = -1; ///< The host core's SMT sibling (-1: none), the logical processor `Sibling` takes }; /// Which core the host thread - the layer loop that spins on the GPU's flags - takes when `skip_first` reserves one -/// (--host-core first|last, STRATA_HOST_CORE). `First` is the layout the pool has always used. `Last` (eddoursul's -/// fork, F12) puts the host on the last physical core and lets the workers have the first: Windows sends a GPU's -/// interrupts to one logical processor, usually the first, and every copy that lands raises one (~13 us in the ISR -/// and a DPC), which delays a spinning host there. The placement changes no result, only where threads run. A hybrid -/// CPU keeps `First` (its first core is the best P-core). -enum class HostCore { First, Last }; +/// (--host-core first|last|sibling, STRATA_HOST_CORE). `First` is the layout the pool has always used. `Last` +/// (eddoursul's fork, F12) puts the host on the last physical core and lets the workers have the first: Windows sends +/// a GPU's interrupts to one logical processor, usually the first, and every copy that lands raises one (~13 us in the +/// ISR and a DPC), which delays a spinning host there. The placement changes no result, only where threads run. A +/// hybrid CPU keeps `First` under `Last` (its first core is the best P-core). `Sibling` keeps the host on that first +/// core but on its other hardware thread: the interrupts stay on the first logical processor, no worker shares the +/// core (the workers take one logical processor per core), and it applies to hybrid CPUs too. Without an SMT sibling +/// it is `First`. +enum class HostCore { First, Last, Sibling }; void set_host_core(HostCore where); HostCore host_core_setting(); diff --git a/src/core/session.cpp b/src/core/session.cpp index 157c4cc4..0629b327 100644 --- a/src/core/session.cpp +++ b/src/core/session.cpp @@ -557,7 +557,8 @@ bool SessionLoopScratch::init(size_t parts_bytes_in, std::string& err) { // core or its SMT sibling. The symptom is not an error: it is a CPU path at 26.9 GB/s where the same pool // runs at 36.32. It was being done and undone on EVERY token, which is a syscall pair on the critical path // for a property that wants to hold for the whole session. - // The host's core is the pool's reserved one: the first physical core, or the last with --host-core last (F12). + // The host's core is the pool's reserved one: the first physical core, or the last with --host-core last (F12), + // or the first core's SMT sibling with --host-core sibling. const int host_core = strata::kernels::cpu::planned_host_core(); if (host_core >= 0) { pinned_core = strata::kernels::cpu::pin_current_thread(host_core); diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cpp index 2884fcde..3e10aa6e 100644 --- a/src/kernels/cpu/iq_avx2.cpp +++ b/src/kernels/cpu/iq_avx2.cpp @@ -453,6 +453,40 @@ void q8k_quant_avx2(const float* x, void* vy, int64_t k) { } } +void iq256_gu_rows_exact_one(int type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, + const void* act, float* ff, int r0, int r1) { + const auto* y = (const block_q8_K*) act; +#if STRATA_AVXVNNI + if (vnni_on()) { + if (type == 16) vnni::gu_ggml_one<16>(blob, gu_row, up_off, n, y, ff, r0, r1); + else if (type == 17) vnni::gu_ggml_one<17>(blob, gu_row, up_off, n, y, ff, r0, r1); + else if (type == 18) { + if (iq256_variant() & kIq256Gather) vnni::gu_ggml_one<118>(blob, gu_row, up_off, n, y, ff, r0, r1); + else vnni::gu_ggml_one<18>(blob, gu_row, up_off, n, y, ff, r0, r1); + } else if (type == 21) { + if (iq256_variant() & kIq256Gather) vnni::gu_ggml_one<121>(blob, gu_row, up_off, n, y, ff, r0, r1); + else vnni::gu_ggml_one<21>(blob, gu_row, up_off, n, y, ff, r0, r1); + } else if (type == 22) { + if (iq256_variant() & kIq256Gather) vnni::gu_ggml_one<122>(blob, gu_row, up_off, n, y, ff, r0, r1); + else vnni::gu_ggml_one<22>(blob, gu_row, up_off, n, y, ff, r0, r1); + } + return; + } +#endif + if (type == 16) plain::gu_ggml_one<16>(blob, gu_row, up_off, n, y, ff, r0, r1); + else if (type == 17) plain::gu_ggml_one<17>(blob, gu_row, up_off, n, y, ff, r0, r1); + else if (type == 18) { + if (iq256_variant() & kIq256Gather) plain::gu_ggml_one<118>(blob, gu_row, up_off, n, y, ff, r0, r1); + else plain::gu_ggml_one<18>(blob, gu_row, up_off, n, y, ff, r0, r1); + } else if (type == 21) { + if (iq256_variant() & kIq256Gather) plain::gu_ggml_one<121>(blob, gu_row, up_off, n, y, ff, r0, r1); + else plain::gu_ggml_one<21>(blob, gu_row, up_off, n, y, ff, r0, r1); + } else if (type == 22) { + if (iq256_variant() & kIq256Gather) plain::gu_ggml_one<122>(blob, gu_row, up_off, n, y, ff, r0, r1); + else plain::gu_ggml_one<22>(blob, gu_row, up_off, n, y, ff, r0, r1); + } +} + int iq256_variant() noexcept { // STRATA_IQ256_GATHER=0/1 for every core; unset, where the calling thread's core gathers faster (per thread: a // hybrid CPU's pool runs on P- and E-cores). AVX-VNNI where cpu_avxvnni_ok(). The same bits either way. diff --git a/src/kernels/cpu/iq_avx2_parity.cpp b/src/kernels/cpu/iq_avx2_parity.cpp index 8de7d87d..67544c9d 100644 --- a/src/kernels/cpu/iq_avx2_parity.cpp +++ b/src/kernels/cpu/iq_avx2_parity.cpp @@ -248,6 +248,25 @@ int check_gate_up(int type) { const double r_ggml = rel(one_g.data(), ref.data(), one_g.size()); std::printf(" %-8s gate rows vs ggml vec_dot: rel %.2e\n", type_name(type), r_ggml); if (!(r_ggml <= 1e-5)) { std::printf(" %-8s MISMATCH against ggml\n", type_name(type)); ++failures; } + if (type == 16 || type == 17 || type == 18 || type == 21 || type == 22) { + size_t different = 0; + std::vector exact(R); + const ggml_vec_dot_t dot = traits_cpu(type)->vec_dot; + for (int t = 0; t < kMaxT; ++t) { + cpu::iq256_gu_rows_exact_one(type, blob.data(), f.gu_row, f.up_off, (int) kH, a.gup[t], + exact.data(), 0, (int) kFF); + for (size_t r = 0; r < R; ++r) { + float g = 0.f, u = 0.f; + dot((int) kH, &g, 0, blob.data() + r * f.gu_row, 0, a.gup[t], 0, 1); + dot((int) kH, &u, 0, blob.data() + f.up_off + r * f.gu_row, 0, a.gup[t], 0, 1); + const float want = (g / (1.f + std::exp(-g))) * u; + different += std::memcmp(&exact[r], &want, sizeof(float)) != 0; + } + } + std::printf(" %-8s exact singleton vs ggml: %zu of %zu expert outputs differ bitwise\n", + type_name(type), different, (size_t) kMaxT * R); + if (different) ++failures; + } for (int v : subsets(gu_variants())) { size_t differ = 0; std::vector g(kMaxT * R), ff(kMaxT * R); @@ -449,6 +468,12 @@ int bench(const std::vector& pairs, const std::vector& nts, in for (int t = 0; t < nt; ++t) ddot((int) kFF, &out[t][r], 0, blob + f.down_off + (size_t) r * f.d_row, 0, a.dnp[t], 0, 1); }}); + if (gt == 16 || gt == 17 || gt == 18 || gt == 21 || gt == 22) + ms.push_back({"exact1", [&, gt](const uint8_t* blob, int nt) { + for (int t = 0; t < nt; ++t) + cpu::iq256_gu_rows_exact_one(gt, blob, f.gu_row, f.up_off, (int) kH, + a.gup[t], ff[t], 0, (int) kFF); + }, down_v(0)}); ms.push_back({"engine", // what the pool calls: native_gu_rows, native_down_rows or (Q2_0) q2_rows_any [&](const uint8_t* blob, int nt) { cpu::native_gu_rows(f, blob, a.gup, nt, ff, 0, (int) kFF); }, [&, dt](const uint8_t* blob, int nt) { diff --git a/src/kernels/cpu/iq_avx2_rows.inl b/src/kernels/cpu/iq_avx2_rows.inl index a8af07fb..5489d8ee 100644 --- a/src/kernels/cpu/iq_avx2_rows.inl +++ b/src/kernels/cpu/iq_avx2_rows.inl @@ -28,6 +28,56 @@ STRATA_ROWS_FN inline __m256i dot4u(__m256i u8, __m256i s8) { #endif } +STRATA_ROWS_FN inline float ggml_hsum8(__m256 x) { + __m128 s = _mm_add_ps(_mm256_extractf128_ps(x, 1), _mm256_castps256_ps128(x)); + s = _mm_add_ps(s, _mm_movehl_ps(s, s)); + s = _mm_add_ss(s, _mm_movehdup_ps(s)); + return _mm_cvtss_f32(s); +} + +template STRATA_ROWS_FN +inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res); + +template STRATA_ROWS_FN +inline float row_dot_ggml_one(const uint8_t* row, int nblocks, const block_q8_K* y) { + if constexpr (TY == 17) { + const block_q8_K* act[1] = {y}; + float result = 0.0f; + row_dot_iq2xs<1, true>(row, nblocks, act, &result); + return result; + } + __m256 accum = _mm256_setzero_ps(); + const int pf = prefetch_distance(); + for (int i = 0; i < nblocks; ++i) { + const uint8_t* blk = row + (size_t) i * Fmt32::bytes; + rows_ahead(blk, pf); + __m256i sums[2] = {_mm256_setzero_si256(), _mm256_setzero_si256()}; + for (int j = 0; j < 4; ++j) + for (int half = 0; half < 2; ++half) { + __m256i g, sign, scale; + Fmt32::decode(blk, j, half, g, sign, scale); + const __m256i act = _mm256_loadu_si256((const __m256i*) (y[i].qs + 64 * j + 32 * half)); + const __m256i signed_act = _mm256_sign_epi8(act, sign); + sums[half] = madd_add(sums[half], _mm256_maddubs_epi16(g, signed_act), scale); + } + const float d = h2f(u16(blk)) * y[i].d; + accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sums[0], sums[1])), accum); + } + if constexpr (TY == 16 || TY == 17 || TY == 22 || TY == 122) return 0.125f * ggml_hsum8(accum); + else if constexpr (TY == 18 || TY == 118) return 0.25f * ggml_hsum8(accum); + else return ggml_hsum8(accum); +} + +template STRATA_ROWS_FN +void gu_ggml_one(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const block_q8_K* y, float* ff, + int r0, int r1) { + for (int r = r0; r < r1; ++r) { + const float g = row_dot_ggml_one(blob + (size_t) r * gu_row, n / QK_K, y); + const float u = row_dot_ggml_one(blob + up_off + (size_t) r * gu_row, n / QK_K, y); + ff[r] = (g / (1.f + std::exp(-g))) * u; + } +} + template STRATA_ROWS_FN inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) { const int pf = prefetch_distance(); @@ -63,7 +113,7 @@ inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, // 32-byte load. The 8 scale bytes become all 16 half-scales with ggml's unpack trick. The grid lookups // stay scalar loads into set_epi64x (ggml does the same). Every token still only pays a load, a sign, // a maddubs, a madd and an add per half, into alternating accumulators. -template STRATA_ROWS_FN +template STRATA_ROWS_FN inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) { static const uint8_t bit_sel[32] = { 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, @@ -137,12 +187,14 @@ inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* con } } } - const float dx = h2f(u16(blk)) * 0.125f; - for (int t = 0; t < NT; ++t) - accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), + for (int t = 0; t < NT; ++t) { + const float d = h2f(u16(blk)) * (GGML ? y[t][i].d : 0.125f); + const float scale = GGML ? d : d * y[t][i].d; + accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(scale), _mm256_cvtepi32_ps(_mm256_add_epi32(acc[t][0], acc[t][1])), accf[t]); + } } - for (int t = 0; t < NT; ++t) res[t] = hsum8(accf[t]); + for (int t = 0; t < NT; ++t) res[t] = GGML ? 0.125f * ggml_hsum8(accf[t]) : hsum8(accf[t]); } template STRATA_ROWS_FN diff --git a/src/kernels/cpu/kq_avx1.cpp b/src/kernels/cpu/kq_avx1.cpp index fbdf73f4..738d94cd 100644 --- a/src/kernels/cpu/kq_avx1.cpp +++ b/src/kernels/cpu/kq_avx1.cpp @@ -43,7 +43,7 @@ inline __m256 bf16x8_to_f32x8_avx1(const uint16_t* p) { } // namespace void bf16_rows_dot_multi_avx1(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) { - // each row is read once for all `nt` (<= 8) tokens, exactly as the AVX2 kernel does + // each row is read once for all `nt` (<= 8) tokens for (int r = 0; r < rows; ++r) { const uint16_t* wr = w + (size_t) r * (size_t) cols; __m256 acc[8]; diff --git a/src/kernels/cpu/kq_avx2.cpp b/src/kernels/cpu/kq_avx2.cpp index f7228732..89ecc1fe 100644 --- a/src/kernels/cpu/kq_avx2.cpp +++ b/src/kernels/cpu/kq_avx2.cpp @@ -145,10 +145,11 @@ void q4k_dot(const block_q4_K* x, int nb, const void* const* act, int nt, float* } // ---- Q5_1 x Q8_1 (ggml_vec_dot_q5_1_q8_1, __AVX2__) -void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float* res) { - __m256 acc[MAXT]; - float summs[MAXT]; - for (int t = 0; t < nt; ++t) { acc[t] = _mm256_setzero_ps(); summs[t] = 0.0f; } +template +void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, float* res) { + __m256 acc[NT]; + float summs[NT]; + for (int t = 0; t < NT; ++t) { acc[t] = _mm256_setzero_ps(); summs[t] = 0.0f; } for (int ib = 0; ib < nb; ++ib) { const __m256 dx = _mm256_set1_ps(half_at(&x[ib], 0)); const float xm = half_at(&x[ib], 1); @@ -156,7 +157,7 @@ void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float __m256i bxhi = bytes_from_bits_32(x[ib].qh); bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10)); qx = _mm256_or_si256(qx, bxhi); - for (int t = 0; t < nt; ++t) { + for (int t = 0; t < NT; ++t) { const block_q8_1* y = (const block_q8_1*) act[t] + ib; summs[t] += xm * half_at(y, 1); const __m256 dy = _mm256_set1_ps(half_at(y, 0)); @@ -165,18 +166,19 @@ void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float acc[t] = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc[t]); } } - for (int t = 0; t < nt; ++t) res[t] = hsum_float_8(acc[t]) + summs[t]; + for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]) + summs[t]; } // ---- Q8_0 x Q8_0 (ggml_vec_dot_q8_0_q8_0, __AVX2__; mul_sum_i8_pairs_float without AVX-VNNI-INT8) -void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, int nt, float* res) { - __m256 acc[MAXT]; - for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps(); +template +void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, float* res) { + __m256 acc[NT]; + for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps(); for (int ib = 0; ib < nb; ++ib) { const float xd = h2f(x[ib].d); const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs); const __m256i ax = _mm256_sign_epi8(qx, qx); - for (int t = 0; t < nt; ++t) { + for (int t = 0; t < NT; ++t) { const block_q8_0* y = (const block_q8_0*) act[t] + ib; const __m256 d = _mm256_set1_ps(xd * h2f(y->d)); const __m256i qy = _mm256_loadu_si256((const __m256i*) y->qs); @@ -185,14 +187,38 @@ void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, int nt, float acc[t] = _mm256_fmadd_ps(d, q, acc[t]); } } - for (int t = 0; t < nt; ++t) res[t] = hsum_float_8(acc[t]); + for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]); +} + +void q5_1_dot_nt(const block_q5_1* x, int nb, const void* const* act, int nt, float* res) { + for (int t0 = 0; t0 < nt; t0 += 4) { + const int k = nt - t0 < 4 ? nt - t0 : 4; + switch (k) { + case 1: q5_1_dot<1>(x, nb, act + t0, res + t0); break; + case 2: q5_1_dot<2>(x, nb, act + t0, res + t0); break; + case 3: q5_1_dot<3>(x, nb, act + t0, res + t0); break; + default: q5_1_dot<4>(x, nb, act + t0, res + t0); break; + } + } +} + +void q8_0_dot_nt(const block_q8_0* x, int nb, const void* const* act, int nt, float* res) { + for (int t0 = 0; t0 < nt; t0 += 4) { + const int k = nt - t0 < 4 ? nt - t0 : 4; + switch (k) { + case 1: q8_0_dot<1>(x, nb, act + t0, res + t0); break; + case 2: q8_0_dot<2>(x, nb, act + t0, res + t0); break; + case 3: q8_0_dot<3>(x, nb, act + t0, res + t0); break; + default: q8_0_dot<4>(x, nb, act + t0, res + t0); break; + } + } } void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int nt, float* res) { switch (type) { case 12: q4k_dot((const block_q4_K*) row, n / QK_K, act, nt, res); break; - case 7: q5_1_dot((const block_q5_1*) row, n / QK5_1, act, nt, res); break; - case 8: q8_0_dot((const block_q8_0*) row, n / QK8_0, act, nt, res); break; + case 7: q5_1_dot_nt((const block_q5_1*) row, n / QK5_1, act, nt, res); break; + case 8: q8_0_dot_nt((const block_q8_0*) row, n / QK8_0, act, nt, res); break; default: break; } } @@ -200,17 +226,29 @@ void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int n } // namespace void bf16_rows_dot_multi(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) { - // each row is read once for all `nt` (<= 8) tokens: the router (2.6 MB per layer) streams once per prediction - for (int r = 0; r < rows; ++r) { - const uint16_t* wr = w + (size_t) r * (size_t) cols; - __m256 acc[8]; - for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps(); - for (int c = 0; c + 8 <= cols; c += 8) { - const __m256 wf = _mm256_castsi256_ps( - _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16)); - for (int t = 0; t < nt; ++t) acc[t] = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x + (size_t) t * cols + c), acc[t]); + for (int t0 = 0; t0 < nt; t0 += 4) { + const int m = nt - t0 < 4 ? nt - t0 : 4; + const float* x0 = x + (size_t) t0 * cols; + const float* x1 = x0 + cols; + const float* x2 = x1 + cols; + const float* x3 = x2 + cols; + for (int r = 0; r < rows; ++r) { + const uint16_t* wr = w + (size_t) r * (size_t) cols; + __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(), a2 = _mm256_setzero_ps(), + a3 = _mm256_setzero_ps(); + for (int c = 0; c + 8 <= cols; c += 8) { + const __m256 wf = _mm256_castsi256_ps( + _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16)); + a0 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x0 + c), a0); + if (m > 1) a1 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x1 + c), a1); + if (m > 2) a2 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x2 + c), a2); + if (m > 3) a3 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x3 + c), a3); + } + out[(size_t) t0 * rows + r] = hsum_float_8(a0); + if (m > 1) out[(size_t) (t0 + 1) * rows + r] = hsum_float_8(a1); + if (m > 2) out[(size_t) (t0 + 2) * rows + r] = hsum_float_8(a2); + if (m > 3) out[(size_t) (t0 + 3) * rows + r] = hsum_float_8(a3); } - for (int t = 0; t < nt; ++t) out[(size_t) t * rows + r] = hsum_float_8(acc[t]); // cols % 8 == 0 (2560) } } diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp index 1ab75b64..c6cab9fd 100644 --- a/src/kernels/cpu/native_expert.cpp +++ b/src/kernels/cpu/native_expert.cpp @@ -15,6 +15,7 @@ #include "ggml-cpu.h" #include +#include #include #include #include @@ -164,6 +165,22 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const* // as the AVX-2 kernels, and STRATA_IQ_MT_MIN=1 takes the kernel for one token too (its rows are then // independent of the drafting: iq_avx1_parity checks every width bit for bit). static const bool avx1 = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_IQ128") == nullptr; + // The exact rows are ggml-cpu's AVX2 single-token dot bit for bit, so they need ggml-cpu itself built for AVX2 and + // not AVX-512 (ggml_cpu_has_*): a STRATA_ISA_FLOOR build runs ggml's AVX1/SSE dots, a native build on an AVX-512 CPU + // run with STRATA_FORCE_AVX2=1 runs its AVX-512 one, and both round differently (#2215's guard). + static const bool exact_one = [] { + const char* v = std::getenv("STRATA_CPU_IQ_ALL_EXACT1"); + return v && v[0] == '1' && v[1] == 0 && ggml_cpu_has_avx2() && !ggml_cpu_has_avx512(); + }(); + // only where ggml's dot runs today (below the #152 multi-token rule): with STRATA_IQ_MT_MIN=1 a lone token keeps the + // multi-token kernel, so a token's rows still do not depend on how many tokens share its expert + if (exact_one && nt == 1 && nt < native_gu_mt_min(f.gu_type) && avx2 && !cpu_avx512_ok() && + (f.gu_type == 16 || f.gu_type == 17 || f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22)) { + static std::once_flag announced; + std::call_once(announced, [] { std::fprintf(stderr, "strata ab: GATE=STRATA_CPU_IQ_ALL_EXACT1 active=1\n"); }); + iq256_gu_rows_exact_one(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act[0], ff[0], r0, r1); + return; + } const int mt_min = native_gu_mt_min(f.gu_type); // #152 // Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot (any group // size, no #152 rule). Opt-in, STRATA_KQ256=1: measured no faster in the engine (a window's expert groups hold diff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cpp index 3bf8c93f..9641ba6a 100644 --- a/src/kernels/cpu/pool.cpp +++ b/src/kernels/cpu/pool.cpp @@ -89,6 +89,13 @@ CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity) { topo.worker_cores.insert(topo.worker_cores.begin(), topo.host_core); topo.host_core = last; } + // --host-core sibling: the host on its core's other hardware thread; the first logical processor stays to the + // interrupts and to nothing else (a worker on it would share the host's core) + if (skip_first && host_core_setting() == HostCore::Sibling && topo.host_sibling >= 0) { + topo.worker_cores.erase(std::remove(topo.worker_cores.begin(), topo.worker_cores.end(), topo.host_sibling), + topo.worker_cores.end()); + topo.host_core = topo.host_sibling; + } return topo; } @@ -180,6 +187,7 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !topo.worker_cores.empty()) { topo.host_core = topo.worker_cores.front(); topo.worker_cores.erase(topo.worker_cores.begin()); + if (descs.front().lps.size() > 1) topo.host_sibling = descs.front().lps[1]; } return topo; } @@ -207,6 +215,11 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !p_primaries.empty()) { topo.host_core = p_primaries.front(); p_primaries.erase(p_primaries.begin()); + for (const auto& c : descs) + if (c.lps[0] == topo.host_core) { + if (c.lps.size() > 1) topo.host_sibling = c.lps[1]; + break; + } } for (int cpu : p_primaries) topo.worker_cores.push_back(cpu); @@ -332,6 +345,15 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini topo.p_threads = (int) all_cpus.size(); } + // the host core's SMT sibling, an allowed CPU of the same (package, core) (--host-core sibling) + auto sibling_of = [&](int cpu) { + for (const auto& h : all_cpus) + if (h.cpu == cpu && h.pkg >= 0 && h.core >= 0) + for (const auto& cl : all_cpus) + if (cl.cpu != cpu && cl.pkg == h.pkg && cl.core == h.core) return cl.cpu; + return -1; + }; + if (affinity == PoolAffinity::All || !topo.is_hybrid) { if (topo.is_hybrid) // #642: the P-cores first (see the Windows branch) std::stable_sort(all_cpus.begin(), all_cpus.end(), @@ -344,6 +366,7 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !topo.worker_cores.empty()) { topo.host_core = topo.worker_cores.front(); topo.worker_cores.erase(topo.worker_cores.begin()); + topo.host_sibling = sibling_of(topo.host_core); } return topo; } @@ -365,6 +388,7 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !p_primaries.empty()) { topo.host_core = p_primaries.front(); p_primaries.erase(p_primaries.begin()); + topo.host_sibling = sibling_of(topo.host_core); } for (int cpu : p_primaries) topo.worker_cores.push_back(cpu); diff --git a/src/kernels/cpu/pool_affinity_test.cpp b/src/kernels/cpu/pool_affinity_test.cpp index 052ae59b..6a947a49 100644 --- a/src/kernels/cpu/pool_affinity_test.cpp +++ b/src/kernels/cpu/pool_affinity_test.cpp @@ -106,7 +106,32 @@ static bool inherited_default(int core, ULONG id, int temporary_core, ULONG temp return check(ok && restored, "restore inherited process default"); } +// --host-core sibling: the host on the first layout's host core's SMT sibling, which no worker takes, and the first +// logical processor left to nothing; `first` on a core without SMT. Every affinity mode. +static bool host_sibling_layout() { + bool ok = true; + for (const auto affinity : {cpu::PoolAffinity::All, cpu::PoolAffinity::Auto, cpu::PoolAffinity::PCores}) { + cpu::set_host_core(cpu::HostCore::First); + const cpu::CpuTopology first = cpu::detect_cpu_topology(true, affinity); + cpu::set_host_core(cpu::HostCore::Sibling); + const cpu::CpuTopology sib = cpu::detect_cpu_topology(true, affinity); + cpu::set_host_core(cpu::HostCore::First); + const auto has = [&](int core) { + return std::find(sib.worker_cores.begin(), sib.worker_cores.end(), core) != sib.worker_cores.end(); + }; + if (first.host_sibling < 0) + ok = check(sib.host_core == first.host_core && sib.worker_cores == first.worker_cores, + "--host-core sibling without SMT must be first") && ok; + else + ok = check(sib.host_core == first.host_sibling && sib.host_core != first.host_core && !has(sib.host_core) && + !has(first.host_core), + "--host-core sibling: the host on the sibling, no worker on its core") && ok; + } + return ok; +} + int main() { + if (!host_sibling_layout()) return 1; const auto all = cpu::physical_cores(false); std::vector cores; std::vector ids; diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cpp index 3c979ebb..bbb1551a 100644 --- a/src/kernels/native_expert_parity.cpp +++ b/src/kernels/native_expert_parity.cpp @@ -100,6 +100,25 @@ int check_blob(const cpu::NativeFmt& f, const std::vector& blob, int se a[k] = act[k].data(); ffp[k] = ff[k].data(); } + if ((f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22) && cpu::cpu_avx2_ok() && !cpu::cpu_avx512_ok()) { + const auto dot = ggml_get_type_traits_cpu((ggml_type) f.gu_type)->vec_dot; + size_t differ = 0; + std::vector exact((size_t) FF); + for (int k = 0; k < NT; ++k) { + cpu::iq256_gu_rows_exact_one(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a[k], + exact.data(), 0, (int) FF); + for (int r = 0; r < FF; ++r) { + float g = 0.f, u = 0.f; + dot((int) H, &g, 0, blob.data() + (size_t) r * f.gu_row, 0, a[k], 0, 1); + dot((int) H, &u, 0, blob.data() + f.up_off + (size_t) r * f.gu_row, 0, a[k], 0, 1); + const float want = (g / (1.f + std::exp(-g))) * u; + differ += std::memcmp(&want, &exact[(size_t) r], sizeof(float)) != 0; + } + } + std::printf("%s exact singleton: %zu of %lld real gate/up rows differ from ggml\n", + label.c_str(), differ, (long long) (NT * FF)); + if (differ) ++failures; + } cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF); // either multi-token kernel: IQ4_XS has an AVX-2 one and no AVX-512 one, so the gate cannot be // iq512_supported alone - that would leave the format untested on every CPU. diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 62be0cff..638b764c 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -508,7 +508,10 @@ struct Options { 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) + /// --pool-affinity measure (opt-in, hybrid CPUs, native packs): `all` and `p-cores` are timed on this pack's + /// own experts at start and the faster layout is taken (measure_pool_affinity) + bool pool_measure = false; + /// --host-core first|last|sibling (STRATA_HOST_CORE): the host thread's core (see HostCore in pool.hpp) std::string host_core; /// --aux-cpus off|auto|LIST (STRATA_AUX_CPUS): where the non-pool, non-host threads run (strata/platform/aux_cpus.hpp) std::string aux_cpus; @@ -982,7 +985,9 @@ void usage() { " 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" " raises one: a spinning host there waits for them. Moves threads only, never a\n" - " result. Not on hybrid CPUs (STRATA_HOST_CORE sets it too).\n" + " result. Not on hybrid CPUs. sibling: the first core's other hardware thread\n" + " (SMT), so the interrupts keep the first logical processor and the workers keep\n" + " every core; hybrid CPUs too, first without SMT (STRATA_HOST_CORE sets it too).\n" " --aux-cpus SET Where the threads that are neither pool workers nor the host thread run (the\n" " adaptive tier's job thread, the prefill helpers, the CUDA driver's threads, ...):\n" " off (the default: wherever they were created), auto (the SMT siblings of the host's\n" @@ -992,7 +997,9 @@ void usage() { " result. STRATA_AUX_CPUS sets it too; a request's strata_tune aux_cpus=0|1 beats it.\n" " --pool-affinity MODE Worker CPU affinity: all (default: one worker per physical core, as\n" " always), auto (hybrid CPUs: P-cores first, then their SMT siblings,\n" - " then E-cores) or p-cores (P-cores and their siblings only).\n" + " then E-cores) or p-cores (P-cores and their siblings only). measure (opt-in,\n" + " hybrid CPUs, native packs): all and p-cores are timed on the pack's own experts\n" + " at start (about a second) and the faster one is taken\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" @@ -1911,6 +1918,103 @@ double pcie_frac_for_gbps(double gbps, double base) { return gbps <= 0.0 ? base : base * std::min(1.0, gbps / 20.0); } +// --pool-affinity measure: `all` against `p-cores` on a hybrid CPU, timed on this pack's own experts. +// +// The pool's default layout puts a worker on every physical core, and a layer's experts are done when the slowest +// worker is. Measured on a Core i7-13700K (8 P + 8 E), IQ3_XXS, RTX 5090, same prompt and build: `all` (15 workers) +// spent 6.7 ms per verify window in the pool and decoded 148 tok/s, `p-cores` (7 workers) 2.2 ms and 188-191 tok/s; +// in an earlier run of the same day `all` took 2.4 ms, so the difference is not constant. Hence a measurement at +// start and not a rule. +// +// What is timed is the decode's own call: two missed experts of four tokens per layer (run_split_multi_native on +// blobs from the arena), every layer in turn, with the host spinning ~150 us between two layers as it does on the +// GPU's flags. The two layouts take turns in kBlocks blocks and the medians are compared; `p-cores` is taken +// unless `all` is more than 10% faster (fewer threads wait on fewer cores when the two are close). The layout +// changes no result, only where threads run. It is one reading at start and is not repeated: on the same PC one +// server start in four read `all` as the faster layout (6.6 ms against 8.6), where the others read it 2.4 to 4 times +// slower. +strata::kernels::cpu::PoolAffinity measure_pool_affinity(strata::core::ExpertSource* src, int workers, bool host_works, + int tasks) { + namespace cpu = strata::kernels::cpu; + using Clock = std::chrono::steady_clock; + const cpu::ExpertLayout& lay = cpu::expert_layout(); + const cpu::CpuTopology topo = cpu::detect_cpu_topology(true, cpu::PoolAffinity::PCores); + if (!topo.is_hybrid) { + std::fprintf(stderr, "strata generate: --pool-affinity measure: this CPU is not a hybrid one; the layout stays all\n"); + return cpu::PoolAffinity::All; + } + constexpr int kExperts = 2, kTok = 4, kRounds = 10, kBlocks = 3; + const int64_t nl = lay.n_layers; + std::vector blobs((size_t) nl * kExperts, nullptr); + bool have = src != nullptr && lay.native && (int64_t) lay.fmt.size() >= nl && nl > 0; + uint32_t seed = 12345u; + for (int64_t l = 0; have && l < nl; ++l) + for (int j = 0; j < kExperts; ++j) { + seed = seed * 1664525u + 1013904223u; + const uint8_t* b = src->blob(l, (int64_t) ((seed >> 8) % (uint32_t) lay.n_expert)); + if (b == nullptr) { have = false; break; } + blobs[(size_t) l * kExperts + (size_t) j] = b; + } + if (!have) { + std::fprintf(stderr, "strata generate: --pool-affinity measure needs a native pack with its experts in RAM; " + "the layout stays all\n"); + return cpu::PoolAffinity::All; + } + std::vector nact((size_t) nl * kTok * cpu::kNativeActBytes); + std::vector qact((size_t) nl * kTok); + std::vector x((size_t) cpu::H), out((size_t) kExperts * kTok * cpu::H); + for (int64_t l = 0; l < nl; ++l) + for (int t = 0; t < kTok; ++t) { + for (int i = 0; i < cpu::H; ++i) x[(size_t) i] = std::sin((float) (i + 13 * t + 7 * (int) l) * 0.031f); + const size_t at = (size_t) l * kTok + (size_t) t; + if (cpu::q2_native_kernels(lay.fmt[(size_t) l].gu_type)) cpu::act_quant_any(x.data(), cpu::H, qact[at]); + else cpu::native_quant_act(lay.fmt[(size_t) l], x.data(), nact.data() + at * cpu::kNativeActBytes); + } + int n_workers[2] = {0, 0}; + auto block = [&](cpu::PoolAffinity aff, int which) -> double { + cpu::ExpertPool pool(workers, /*pin=*/true, host_works, aff, tasks); + n_workers[which] = pool.workers(); + const int host = cpu::planned_host_core(aff); + cpu::ThreadAffinity prev; + if (host >= 0) prev = cpu::pin_current_thread(host); + cpu::ExpertJobMulti jobs[kExperts]; + double ms = 0.0; + for (int r = -1; r < kRounds; ++r) // round -1 is not timed: the workers start and the rows are touched + for (int64_t l = 0; l < nl; ++l) { + for (int j = 0; j < kExperts; ++j) { + jobs[j].blob = blobs[(size_t) l * kExperts + (size_t) j]; + jobs[j].nt = kTok; + for (int t = 0; t < kTok; ++t) { + const size_t at = (size_t) l * kTok + (size_t) t; + jobs[j].act[t] = &qact[at]; + jobs[j].nact[t] = nact.data() + at * cpu::kNativeActBytes; + jobs[j].out[t] = out.data() + ((size_t) j * kTok + (size_t) t) * cpu::H; + } + } + const Clock::time_point t0 = Clock::now(); + pool.run_split_multi_native(lay.fmt[(size_t) l], jobs, kExperts); + const Clock::time_point t1 = Clock::now(); + if (r >= 0) ms += std::chrono::duration(t1 - t0).count(); + while (std::chrono::duration(Clock::now() - t1).count() < 150.0) {} + } + if (host >= 0) cpu::restore_thread_affinity(prev); + return ms / kRounds; + }; + double all_ms[kBlocks], p_ms[kBlocks]; + for (int b = 0; b < kBlocks; ++b) { + all_ms[b] = block(cpu::PoolAffinity::All, 0); + p_ms[b] = block(cpu::PoolAffinity::PCores, 1); + } + std::sort(all_ms, all_ms + kBlocks); + std::sort(p_ms, p_ms + kBlocks); + const double a = all_ms[kBlocks / 2], p = p_ms[kBlocks / 2]; + const bool take_p = p <= a * 1.10; + std::fprintf(stderr, "strata generate: --pool-affinity measure: %d experts x %d tokens per layer over %lld layers: " + "all (%d workers) %.2f ms, p-cores (%d workers) %.2f ms (medians of %d) -> %s\n", + kExperts, kTok, (long long) nl, n_workers[0], a, n_workers[1], p, kBlocks, take_p ? "p-cores" : "all"); + return take_p ? cpu::PoolAffinity::PCores : cpu::PoolAffinity::All; +} + // 0.1.42, CUDA single-GPU serve, no --pcie-frac: the share refined from what the CPU pool measures in the first decode // windows. The balance share of the missed experts is link / (link + CPU rate). The link is the start-up probe; the CPU // rate is the expert bytes over the pool's wall time per missed expert (ExpertDispatch::ms_run / multi_misses), which the @@ -2175,8 +2279,9 @@ int main(int argc, char** argv) { } else if (a == "--host-core") { o.host_core = next("--host-core"); - if (o.host_core != "first" && o.host_core != "last") { - std::fprintf(stderr, "strata generate: unknown --host-core value '%s' (expected first or last)\n", o.host_core.c_str()); + if (o.host_core != "first" && o.host_core != "last" && o.host_core != "sibling") { + std::fprintf(stderr, "strata generate: unknown --host-core value '%s' (expected first, last or sibling)\n", + o.host_core.c_str()); return 2; } } @@ -2194,8 +2299,9 @@ int main(int argc, char** argv) { 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 if (v == "measure") o.pool_measure = true; else { - std::fprintf(stderr, "strata generate: unknown --pool-affinity value '%s' (expected auto, p-cores, or all)\n", v.c_str()); + std::fprintf(stderr, "strata generate: unknown --pool-affinity value '%s' (expected auto, p-cores, all or measure)\n", v.c_str()); return 2; } } @@ -2480,6 +2586,7 @@ int main(int argc, char** argv) { if (hc.empty()) if (const char* e = std::getenv("STRATA_HOST_CORE")) hc = e; if (hc == "last") strata::kernels::cpu::set_host_core(strata::kernels::cpu::HostCore::Last); + if (hc == "sibling") strata::kernels::cpu::set_host_core(strata::kernels::cpu::HostCore::Sibling); } { // --aux-cpus / STRATA_AUX_CPUS: read the CPUs this process may use now, before any thread is pinned; the plan // follows once the pool exists (it needs the workers' CPUs) @@ -2753,10 +2860,13 @@ int main(int argc, char** argv) { o.mtp_max_t = o.spec; o.spec = std::min(o.spec + 2, 8); // kVerifyMaxT } - // --lookup-chain (opt-in): the MTP keeps its windows; a lookup chained after its drafts may lengthen one by up to K + // --lookup-chain (opt-in): the MTP keeps its windows; a lookup chained after its drafts may lengthen one by up to K. + // Sized from the cap actually in force: the suffix block above may already have raised --spec to --spec+2, so sizing + // from mtp_max_t left K=1 and K=2 with a window identical to not passing the flag at all. if (o.lookup_chain > 0 && o.spec >= 2) { if (o.mtp_max_t == 0) o.mtp_max_t = o.spec; - o.spec = std::max(o.spec, std::min(o.mtp_max_t + o.lookup_chain, 8)); // kVerifyMaxT + const int chain_base = o.spec; + o.spec = std::max(chain_base, std::min(std::max(chain_base, o.mtp_max_t) + o.lookup_chain, 8)); // kVerifyMaxT } strata::core::layer_set_shared_early(!o.shared_late); if (!o.native_preset.empty()) { @@ -5340,6 +5450,8 @@ int main(int argc, char** argv) { } } #endif + if (o.pool_measure) + o.pool_affinity = measure_pool_affinity(srcp, o.pool_workers, !o.no_host_worker, o.pool_tasks); 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", @@ -5360,7 +5472,9 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: %d pool workers on logical processors %s, the host thread on %d%s " "(--host-core %s)\n", pool.workers(), on.c_str(), ht.host_core, pool.host_works() ? " (draining too)" : "", - strata::kernels::cpu::host_core_setting() == strata::kernels::cpu::HostCore::Last ? "last" : "first"); + strata::kernels::cpu::host_core_setting() == strata::kernels::cpu::HostCore::Last ? "last" + : strata::kernels::cpu::host_core_setting() == strata::kernels::cpu::HostCore::Sibling ? "sibling" + : "first"); // --aux-cpus: the plan, now the workers' CPUs are known; the threads that exist already (the CUDA driver's) move // now when it is on, the ones created later pin themselves (strata/platform/aux_cpus.hpp) const size_t nw = std::min((size_t) pool.workers(), ht.worker_cores.size());