Problem. #2408 / PR #2409: restoring a large parked conversation on a layer split ended the server with a CUDA out of memory on a later card. The PR checks the free VRAM of every card against --vram-reserve-mib before a restore. Its own review points out that a loaded card is always below that reserve, so the check would skip nearly every restore on a split (and on one GPU). It is not ported. What a restore allocates. conversation_snapshot_restore writes into the session's existing buffers (pools(st), the GDN state, the indexer tails); refresh_residency only launches kv_stream_reset or copies into the existing ring. The elastic K/V growth (kvg_ensure) allocates, but only on one GPU. The one thing a restore can create on a card is the calling thread's private copy stream for it (conversation_copy.hpp private_stream), which it takes from the pool of finished threads when there is one (#3350). A failed restore is fatal by contract (the session may be half written). So the need is one stream and one event, and only for a card whose stream does not exist yet (a snapshot loaded from disk, or a restore on a thread that never copied on that card). Change. conv_cache.hpp: kRestoreFirstUseMib (64) and restore_fits(free, stream_ready, n, need): a restore is refused only for a card that has no copy stream yet and less than 64 MiB free. conversation_copy_stream_ready(device) says whether a card has one. generate.cpp: before the restore, each card of the run is checked; a snapshot that would not fit goes back to the cache and the prompt is read again, with a log line. A card that has its stream is never refused, however full. No sample (a failed cudaMemGetInfo) means the restore goes ahead as it did. Tests. conv_cache_test: the predicate (cards with a stream are admitted at any free size; the edge at the need; no sample / no cards / no need). conversation_copy_streams_test: copy_stream_ready before and after the first copy, in the pool after a thread ends, and the measured cost of a card's first copy stream and event (printed; the test requires under 16 MiB, so 64 MiB leaves a wide margin). conversation_snapshot_test: after the first restore, six more restores of two images leave the free VRAM where it was. Not covered. The pure predicate and the generate.cpp code build on z440 (CUDA 13.0); the GPU tests are listed in the stream notes. No model run: gates/2409-restore-guard.sh (a held-VRAM start on a layer split) shows the skip line and an unchanged answer on the second turn.
116 lines
5.3 KiB
C++
116 lines
5.3 KiB
C++
// The conversation copy streams and events are pooled across threads (#3350, #3470): short-lived threads (the layer
|
|
// split's later stages run each prompt chunk on a fresh std::async thread) must not leave a stream and an event behind
|
|
// each. Synthetic 4 KiB copies, no model; needs a CUDA device (skips, exit 77, without one).
|
|
#include "conversation_copy.hpp"
|
|
|
|
#include <atomic>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using namespace strata::core::conversation_detail;
|
|
|
|
namespace {
|
|
int checks = 0;
|
|
void check(bool ok, const char* label) {
|
|
++checks;
|
|
if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
|
|
}
|
|
|
|
// one thread's work on device `dev`: a queued copy, the ordering event, a synchronous copy
|
|
bool one_thread(int dev, void* d, void* h, size_t bytes) {
|
|
if (cudaSetDevice(dev) != cudaSuccess) return false;
|
|
if (copy_nonblocking(d, h, bytes, true) != cudaSuccess) return false;
|
|
if (order_after_queued() != cudaSuccess) return false;
|
|
if (copy_nonblocking(h, d, bytes, false) != cudaSuccess) return false;
|
|
return cudaDeviceSynchronize() == cudaSuccess;
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
int n_dev = 0;
|
|
if (cudaGetDeviceCount(&n_dev) != cudaSuccess || n_dev < 1) {
|
|
(void) cudaGetLastError();
|
|
std::printf("conversation_copy_streams_test: no CUDA device, skipped\n");
|
|
return 77;
|
|
}
|
|
constexpr size_t bytes = 4096;
|
|
void* d = nullptr;
|
|
check(cudaMalloc(&d, bytes) == cudaSuccess, "device buffer");
|
|
std::vector<char> h(bytes, 7);
|
|
|
|
// 0. #2409: what the first copy stream and event of a card cost, on a context that is warm (the buffer above is
|
|
// allocated, and a first copy has loaded the copy path's code on a thread that is then discarded)
|
|
{
|
|
bool fresh_ready = true;
|
|
std::thread probe([&] { fresh_ready = copy_stream_ready(0); });
|
|
probe.join();
|
|
check(!fresh_ready, "before any copy no thread has a stream for the card (copy_stream_ready is false)");
|
|
cudaDeviceSynchronize();
|
|
size_t free0 = 0, free1 = 0, tot = 0;
|
|
check(cudaMemGetInfo(&free0, &tot) == cudaSuccess, "free memory before the first stream");
|
|
bool ok = false, ready_after = false;
|
|
std::thread t([&] {
|
|
ok = one_thread(0, d, h.data(), bytes);
|
|
ready_after = copy_stream_ready(0);
|
|
});
|
|
t.join();
|
|
check(ok && ready_after, "the first copy creates the stream, and the thread then has it");
|
|
check(cudaMemGetInfo(&free1, &tot) == cudaSuccess, "free memory after the first stream");
|
|
const long long used = (long long) free0 - (long long) free1;
|
|
std::printf("conversation_copy_streams_test: the first copy stream + event of the card took %lld KiB of device memory\n",
|
|
used / 1024);
|
|
// the restore's guard (program/conv_cache.hpp kRestoreFirstUseMib = 64) is 4x this bound
|
|
check(used <= (16ll << 20), "a card's first copy stream and event take under 16 MiB");
|
|
bool pooled = false;
|
|
std::thread t2([&] { pooled = copy_stream_ready(0); });
|
|
t2.join();
|
|
check(pooled, "a finished thread's stream is in the pool: the next thread needs no new one");
|
|
}
|
|
|
|
// 1. 200 threads, one after the other (a chunk per std::async thread): the handles are reused, not created per thread
|
|
const long s0 = copy_streams_created(), e0 = copy_events_created();
|
|
for (int i = 0; i < 200; ++i) {
|
|
bool ok = false;
|
|
std::thread t([&] { ok = one_thread(0, d, h.data(), bytes); });
|
|
t.join();
|
|
check(ok, "a short-lived thread's copies succeed");
|
|
}
|
|
check(copy_streams_created() - s0 <= 1, "200 sequential threads create at most one stream");
|
|
check(copy_events_created() - e0 <= 1, "200 sequential threads create at most one event");
|
|
|
|
// 2. threads at the same time each have their own (the number is bounded by the concurrency)
|
|
const long s1 = copy_streams_created();
|
|
std::atomic<int> fails{0};
|
|
for (int round = 0; round < 10; ++round) {
|
|
std::vector<std::thread> ts;
|
|
for (int k = 0; k < 6; ++k)
|
|
ts.emplace_back([&] { for (int j = 0; j < 5; ++j) if (!one_thread(0, d, h.data(), bytes)) ++fails; });
|
|
for (auto& t : ts) t.join();
|
|
}
|
|
check(fails.load() == 0, "concurrent threads' copies succeed");
|
|
check(copy_streams_created() - s1 <= 6, "ten rounds of 6 concurrent threads create at most 6 more streams");
|
|
|
|
// 3. a second device has its own
|
|
if (n_dev > 1) {
|
|
const long s2 = copy_streams_created();
|
|
for (int i = 0; i < 50; ++i) {
|
|
bool ok = false;
|
|
std::thread t([&] {
|
|
ok = cudaSetDevice(1) == cudaSuccess;
|
|
void* d1 = nullptr;
|
|
ok = ok && cudaMalloc(&d1, bytes) == cudaSuccess && one_thread(1, d1, h.data(), bytes);
|
|
if (d1) cudaFree(d1);
|
|
});
|
|
t.join();
|
|
check(ok, "device 1: a short-lived thread's copies succeed");
|
|
}
|
|
check(copy_streams_created() - s2 <= 1, "device 1: 50 sequential threads create at most one stream");
|
|
}
|
|
cudaFree(d);
|
|
std::printf("conversation_copy_streams_test: %d checks passed (%ld streams, %ld events created in all)\n", checks,
|
|
copy_streams_created(), copy_events_created());
|
|
return 0;
|
|
}
|