okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-container 11e0176bb into the batch 16 candidate (test files and a Python tool: ggml_cpu_init in native_expert_parity, pool tests on a 2-node host, sm_89 keys in check_cuda_spills.py)af6226a3c

main
6.6 KiB132 linesraw
1// pool_cores_test: where the CPU pool's workers are placed, and --pool-cores (experimental, off by default).
2// Builds real pools (no model, no GPU).  The default placement must be what detect_cpu_topology() says (one logical
3// processor per physical core, the host's core left out); a list given to set_pool_cores() must be taken as it is.
4#include "strata/kernels/cpu/pool.hpp"
5
6#include <algorithm>
7#include <cstdio>
8#include <cstdlib>
9#include <string>
10#include <vector>
11
12namespace cpu = strata::kernels::cpu;
13
14static int g_fail = 0;
15
16static void expect(bool ok, const char* what) {
17    if (!ok) {
18        ++g_fail;
19        std::fprintf(stderr, "FAIL: %s\n", what);
20    }
21}
22
23static std::string join(const std::vector<int>& v) {
24    std::string s;
25    for (size_t i = 0; i < v.size(); ++i) s += (i ? "," : "") + std::to_string(v[i]);
26    return s;
27}
28
29int main() {
30    // On a machine with two NUMA nodes (a two-socket CPU, or a container on one) the pool splits itself over both nodes by
31    // default and takes cores from each, so its workers no longer follow the topology's order: this test is about that
32    // order, so the split is switched off here (pool_tasks_test covers the split).
33#if defined(_WIN32)
34    _putenv_s("STRATA_NUMA_POOL", "0");
35#else
36    setenv("STRATA_NUMA_POOL", "0", 1);
37#endif
38    const std::vector<cpu::LogicalCpuInfo> layout = cpu::logical_cpu_layout();
39    std::printf("pool_cores_test: %zu logical processors\n", layout.size());
40    expect(!layout.empty(), "the system names its logical processors");
41    const cpu::CpuTopology topo = cpu::detect_cpu_topology(true);
42    expect(!topo.worker_cores.empty() || topo.host_core >= 0, "the topology has cores");
43
44    // 1. the default: nothing set, the placement is the topology's, one worker per physical core
45    {
46        cpu::set_pool_cores({});
47        const int n = (int) std::min<size_t>(2, topo.worker_cores.size());
48        if (n >= 1) {
49            cpu::ExpertPool pool(n, true, true);
50            expect(!pool.explicit_cores(), "default: not an explicit list");
51            expect(pool.workers() == n, "default: the worker count asked for");
52            expect(std::equal(topo.worker_cores.begin(), topo.worker_cores.begin() + n, pool.worker_cores().begin()),
53                   "default: the workers sit on the topology's cores, in its order");
54            const std::string line = pool.placement_line();
55            std::printf("  default: %s\n", line.c_str());
56            expect(line.rfind("CPU workers on logical processors ", 0) == 0, "the start-up line starts as documented");
57            std::vector<long> cores;
58            bool known = true;
59            for (int i = 0; i < n; ++i) {
60                long c = -1;
61                for (const auto& l : layout) if (l.cpu == pool.worker_cores()[(size_t) i]) c = l.core;
62                known = known && c >= 0;
63                cores.push_back(c);
64            }
65            if (known) {
66                std::sort(cores.begin(), cores.end());
67                expect(std::unique(cores.begin(), cores.end()) == cores.end(), "default: no two workers on one physical core");
68                expect(line.find("one per physical core") != std::string::npos, "the line says one per physical core");
69            } else {
70                expect(line.find("one per physical core") == std::string::npos, "unknown layout: the line does not claim it");
71            }
72            for (const auto& l : layout) if (l.cpu == topo.host_core)
73                for (int i = 0; i < n; ++i)
74                    for (const auto& m : layout)
75                        if (m.cpu == pool.worker_cores()[(size_t) i] && l.core >= 0)
76                            expect(m.core != l.core, "default: no worker on the host's core");
77        }
78    }
79
80    // 2. --pool-cores: the list as given, in its order; the count is the list's length
81    if (topo.worker_cores.size() >= 3) {
82        const std::vector<int> want{topo.worker_cores[2], topo.worker_cores[0]};
83        cpu::set_pool_cores(want);
84        cpu::ExpertPool pool(0, true, true);
85        expect(pool.explicit_cores(), "list: explicit");
86        expect(pool.workers() == 2, "list: the count is the list's length");
87        expect(std::equal(want.begin(), want.end(), pool.worker_cores().begin()), "list: the workers sit on the listed processors, in order");
88        expect(std::string(pool.plan_limit()).find("--pool-cores") != std::string::npos, "list: the start-up line names the list");
89        const std::string line = pool.placement_line();
90        std::printf("  list %s: %s\n", join(want).c_str(), line.c_str());
91        expect(line.find("your --pool-cores list") != std::string::npos, "list: the line says it is the person's list");
92
93        // --pool-workers under the list: the first N entries
94        cpu::ExpertPool one(1, true, true);
95        expect(one.workers() == 1 && one.worker_cores()[0] == want[0], "list + --pool-workers 1: the first entry");
96    }
97
98    // 3. a processor that does not exist is left out (warning), the rest stays; nothing usable falls back to the default
99    if (topo.worker_cores.size() >= 2) {
100        cpu::set_pool_cores({99999, topo.worker_cores[1]});
101        cpu::ExpertPool pool(0, true, true);
102        expect(pool.explicit_cores() && pool.workers() == 1 && pool.worker_cores()[0] == topo.worker_cores[1],
103               "an unusable entry is left out, the usable one kept");
104        cpu::set_pool_cores({99999});
105        cpu::ExpertPool fallback(1, true, true);
106        expect(!fallback.explicit_cores() && fallback.worker_cores()[0] == topo.worker_cores[0],
107               "nothing usable: the default placement");
108    }
109
110    // 4. two SMT siblings of one core are kept (and warned about by check_pool_cores, tested in pool_plan_test)
111    for (const auto& a : layout) {
112        const cpu::LogicalCpuInfo* b = nullptr;
113        for (const auto& c : layout) if (c.cpu != a.cpu && a.core >= 0 && c.core == a.core) { b = &c; break; }
114        if (b == nullptr) continue;
115        cpu::set_pool_cores({a.cpu, b->cpu});
116        cpu::ExpertPool pool(0, true, true);
117        expect(pool.explicit_cores() && pool.workers() == 2 && pool.worker_cores()[0] == a.cpu && pool.worker_cores()[1] == b->cpu,
118               "SMT siblings in the list are kept as given");
119        const std::string line = pool.placement_line();
120        std::printf("  siblings %d,%d: %s\n", a.cpu, b->cpu, line.c_str());
121        expect(line.find("more than one worker") != std::string::npos, "the line says two workers share a core");
122        break;
123    }
124    cpu::set_pool_cores({});
125
126    if (g_fail) {
127        std::fprintf(stderr, "pool_cores_test: %d failure(s)\n", g_fail);
128        return 1;
129    }
130    std::printf("pool_cores_test: OK\n");
131    return 0;
132}