okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
5.3 KiB115 linesraw
1// The conversation copy streams and events are pooled across threads (#3350, #3470): short-lived threads (the layer
2// split's later stages run each prompt chunk on a fresh std::async thread) must not leave a stream and an event behind
3// each.  Synthetic 4 KiB copies, no model; needs a CUDA device (skips, exit 77, without one).
4#include "conversation_copy.hpp"
5
6#include <atomic>
7#include <cstdio>
8#include <cstdlib>
9#include <thread>
10#include <vector>
11
12using namespace strata::core::conversation_detail;
13
14namespace {
15int checks = 0;
16void check(bool ok, const char* label) {
17    ++checks;
18    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
19}
20
21// one thread's work on device `dev`: a queued copy, the ordering event, a synchronous copy
22bool one_thread(int dev, void* d, void* h, size_t bytes) {
23    if (cudaSetDevice(dev) != cudaSuccess) return false;
24    if (copy_nonblocking(d, h, bytes, true) != cudaSuccess) return false;
25    if (order_after_queued() != cudaSuccess) return false;
26    if (copy_nonblocking(h, d, bytes, false) != cudaSuccess) return false;
27    return cudaDeviceSynchronize() == cudaSuccess;
28}
29}  // namespace
30
31int main() {
32    int n_dev = 0;
33    if (cudaGetDeviceCount(&n_dev) != cudaSuccess || n_dev < 1) {
34        (void) cudaGetLastError();
35        std::printf("conversation_copy_streams_test: no CUDA device, skipped\n");
36        return 77;
37    }
38    constexpr size_t bytes = 4096;
39    void* d = nullptr;
40    check(cudaMalloc(&d, bytes) == cudaSuccess, "device buffer");
41    std::vector<char> h(bytes, 7);
42
43    // 0. #2409: what the first copy stream and event of a card cost, on a context that is warm (the buffer above is
44    //    allocated, and a first copy has loaded the copy path's code on a thread that is then discarded)
45    {
46        bool fresh_ready = true;
47        std::thread probe([&] { fresh_ready = copy_stream_ready(0); });
48        probe.join();
49        check(!fresh_ready, "before any copy no thread has a stream for the card (copy_stream_ready is false)");
50        cudaDeviceSynchronize();
51        size_t free0 = 0, free1 = 0, tot = 0;
52        check(cudaMemGetInfo(&free0, &tot) == cudaSuccess, "free memory before the first stream");
53        bool ok = false, ready_after = false;
54        std::thread t([&] {
55            ok = one_thread(0, d, h.data(), bytes);
56            ready_after = copy_stream_ready(0);
57        });
58        t.join();
59        check(ok && ready_after, "the first copy creates the stream, and the thread then has it");
60        check(cudaMemGetInfo(&free1, &tot) == cudaSuccess, "free memory after the first stream");
61        const long long used = (long long) free0 - (long long) free1;
62        std::printf("conversation_copy_streams_test: the first copy stream + event of the card took %lld KiB of device memory\n",
63                    used / 1024);
64        // the restore's guard (program/conv_cache.hpp kRestoreFirstUseMib = 64) is 4x this bound
65        check(used <= (16ll << 20), "a card's first copy stream and event take under 16 MiB");
66        bool pooled = false;
67        std::thread t2([&] { pooled = copy_stream_ready(0); });
68        t2.join();
69        check(pooled, "a finished thread's stream is in the pool: the next thread needs no new one");
70    }
71
72    // 1. 200 threads, one after the other (a chunk per std::async thread): the handles are reused, not created per thread
73    const long s0 = copy_streams_created(), e0 = copy_events_created();
74    for (int i = 0; i < 200; ++i) {
75        bool ok = false;
76        std::thread t([&] { ok = one_thread(0, d, h.data(), bytes); });
77        t.join();
78        check(ok, "a short-lived thread's copies succeed");
79    }
80    check(copy_streams_created() - s0 <= 1, "200 sequential threads create at most one stream");
81    check(copy_events_created() - e0 <= 1, "200 sequential threads create at most one event");
82
83    // 2. threads at the same time each have their own (the number is bounded by the concurrency)
84    const long s1 = copy_streams_created();
85    std::atomic<int> fails{0};
86    for (int round = 0; round < 10; ++round) {
87        std::vector<std::thread> ts;
88        for (int k = 0; k < 6; ++k)
89            ts.emplace_back([&] { for (int j = 0; j < 5; ++j) if (!one_thread(0, d, h.data(), bytes)) ++fails; });
90        for (auto& t : ts) t.join();
91    }
92    check(fails.load() == 0, "concurrent threads' copies succeed");
93    check(copy_streams_created() - s1 <= 6, "ten rounds of 6 concurrent threads create at most 6 more streams");
94
95    // 3. a second device has its own
96    if (n_dev > 1) {
97        const long s2 = copy_streams_created();
98        for (int i = 0; i < 50; ++i) {
99            bool ok = false;
100            std::thread t([&] {
101                ok = cudaSetDevice(1) == cudaSuccess;
102                void* d1 = nullptr;
103                ok = ok && cudaMalloc(&d1, bytes) == cudaSuccess && one_thread(1, d1, h.data(), bytes);
104                if (d1) cudaFree(d1);
105            });
106            t.join();
107            check(ok, "device 1: a short-lived thread's copies succeed");
108        }
109        check(copy_streams_created() - s2 <= 1, "device 1: 50 sequential threads create at most one stream");
110    }
111    cudaFree(d);
112    std::printf("conversation_copy_streams_test: %d checks passed (%ld streams, %ld events created in all)\n", checks,
113                copy_streams_created(), copy_events_created());
114    return 0;
115}