okami.mom/strata

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

Niko1221engine: fix a race in the CPU expert pool, and never hang silently again (issue #29, engine 0.1.12)9ffd6c946

main
4.1 KiB99 linesraw
1// src/kernels/cpu/pool_stress.cpp - issue #29: the expert pool under the load a big-VRAM card gives it.
2//
3// With most experts resident on the GPU, many layers hand the pool nothing, the workers go to sleep in the middle
4// of a request, and the next tiny batch (one or two experts) is often finished by the host and the awake workers
5// before a sleeper has even woken.  That late wake-up is what the park/claim protocol has to survive.  This drives
6// hundreds of thousands of such batches - 0-4 jobs, run() and run_split(), with pauses long enough for the workers
7// to sleep - and checks that every batch returns, with every job's output written.  A watchdog thread turns a
8// hang into a failure instead of a frozen test.
9//
10//   pool_stress [seconds]        (default 20)
11#include "strata/kernels/cpu/pool.hpp"
12#include "strata/kernels/cpu/expert.hpp"
13
14#include <atomic>
15#include <chrono>
16#include <cstdio>
17#include <cstdlib>
18#include <cstring>
19#include <random>
20#include <thread>
21#include <vector>
22
23namespace cpu = strata::kernels::cpu;
24
25int main(int argc, char** argv) {
26    const double seconds = argc > 1 ? std::atof(argv[1]) : 20.0;
27    const cpu::CpuFeatures feat = cpu::cpu_features();
28    if (!feat.usable()) {
29        std::printf("CPU lacks %s: pool_stress SKIPPED\n", feat.reason());
30        return 0;
31    }
32    constexpr int NJ = 4;
33    std::vector<std::vector<uint8_t>> blobs(NJ, std::vector<uint8_t>(cpu::BLOB, 0));
34    std::vector<float> x(cpu::H, 0.25f);
35    cpu::ActQ act;
36    cpu::act_quant_q8_1(x.data(), cpu::H, act);
37    std::vector<float> out((size_t) NJ * cpu::H);
38    std::vector<cpu::ExpertJob> jobs(NJ);
39    for (int e = 0; e < NJ; ++e) {
40        jobs[(size_t) e].blob = blobs[(size_t) e].data();
41        jobs[(size_t) e].act = &act;
42        jobs[(size_t) e].out = out.data() + (size_t) e * cpu::H;
43        jobs[(size_t) e].slot = e;
44    }
45
46    cpu::ExpertPool pool;
47    std::printf("pool_stress: %d workers + host, %.0f s\n", pool.workers(), seconds);
48
49    // the watchdog: a batch that does not return within 10 s is a hang
50    std::atomic<long long> beat{0};
51    std::atomic<bool> finished{false};
52    std::thread dog([&] {
53        long long last = -1;
54        auto since = std::chrono::steady_clock::now();
55        while (!finished.load()) {
56            std::this_thread::sleep_for(std::chrono::milliseconds(200));
57            const long long b = beat.load();
58            const auto now = std::chrono::steady_clock::now();
59            if (b != last) { last = b; since = now; continue; }
60            if (now - since > std::chrono::seconds(10)) {
61                std::printf("\npool_stress: HANG - batch %lld has not returned for 10 s\n", b);
62                std::fflush(stdout);
63                std::_Exit(3);
64            }
65        }
66    });
67
68    std::mt19937 rng(29);
69    const float SENTINEL = -1.2345e33f;
70    long long batches = 0, sleeps = 0, missed = 0;
71    const auto t0 = std::chrono::steady_clock::now();
72    while (std::chrono::steady_clock::now() - t0 < std::chrono::duration<double>(seconds)) {
73        // now and then a gap long enough for every worker to go to sleep (the pool sleeps after 20 ms)
74        if (rng() % 64 == 0) {
75            std::this_thread::sleep_for(std::chrono::milliseconds(21 + (int) (rng() % 10)));
76            ++sleeps;
77        } else if (rng() % 4 == 0) {
78            std::this_thread::sleep_for(std::chrono::microseconds(rng() % 300));
79        }
80        const int n = (int) (rng() % (NJ + 1));
81        for (int e = 0; e < n; ++e) std::fill(jobs[(size_t) e].out, jobs[(size_t) e].out + cpu::H, SENTINEL);
82        if (rng() % 2) pool.run(jobs.data(), n);
83        else pool.run_split(jobs.data(), n);
84        for (int e = 0; e < n; ++e)
85            if (jobs[(size_t) e].out[0] == SENTINEL || jobs[(size_t) e].out[cpu::H - 1] == SENTINEL) ++missed;
86        beat.fetch_add(1);
87        ++batches;
88    }
89    finished.store(true);
90    dog.join();
91    std::printf("pool_stress: %lld batches (%lld after the workers slept), %lld jobs not run\n", batches, sleeps,
92                missed);
93    if (missed) {
94        std::printf("pool_stress: FAILED\n");
95        return 1;
96    }
97    std::printf("pool_stress OK\n");
98    return 0;
99}