From c08fd90262efeb0e3346d1335228b66ff4ff9bd5 Mon Sep 17 00:00:00 2001 From: Olav Lindekleiv Date: Sun, 11 Oct 2026 20:39:33 +0200 Subject: [PATCH] generate: warm CUDA callbacks before host-core pinning (opt-in) --- bench/micro/cuda_callback_affinity.cpp | 67 +++++++++++++++ .../README.md | 84 +++++++++++++++++++ docs/DETAILS.md | 9 +- src/program/generate.cpp | 12 +++ 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 bench/micro/cuda_callback_affinity.cpp create mode 100644 bench/results/2026-10-11-rtx-5090-callback-affinity/README.md diff --git a/bench/micro/cuda_callback_affinity.cpp b/bench/micro/cuda_callback_affinity.cpp new file mode 100644 index 00000000..f7b9132f --- /dev/null +++ b/bench/micro/cuda_callback_affinity.cpp @@ -0,0 +1,67 @@ +// Linux/x86 CUDA reproducer for #1975. Run "lazy" and "warm" as separate processes. +// See bench/results/2026-10-11-rtx-5090-callback-affinity/README.md for the build command. +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +struct State { + std::atomic done{false}; + cpu_set_t mask{}; +}; + +static void callback(void* p) { + auto* s = (State*) p; + sched_getaffinity(0, sizeof s->mask, &s->mask); + s->done.store(true, std::memory_order_release); +} + +static void check(cudaError_t e) { + if (e != cudaSuccess) { + std::fprintf(stderr, "%s\n", cudaGetErrorString(e)); + std::exit(1); + } +} + +int main(int argc, char** argv) { + const bool warm = argc > 1 && std::strcmp(argv[1], "warm") == 0; + cudaStream_t stream; + check(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking)); + State state; + if (warm) { + check(cudaLaunchHostFunc(stream, callback, &state)); + check(cudaStreamSynchronize(stream)); + } + cpu_set_t host; + CPU_ZERO(&host); + CPU_SET(0, &host); + if (sched_setaffinity(0, sizeof host, &host) != 0) { + std::perror("sched_setaffinity"); + return 1; + } + std::vector times; + for (int i = 0; i < 200; ++i) { + state.done.store(false, std::memory_order_release); + const auto start = std::chrono::steady_clock::now(); + check(cudaLaunchHostFunc(stream, callback, &state)); + while (!state.done.load(std::memory_order_acquire)) { + asm volatile("pause"); + if (std::chrono::steady_clock::now() - start > std::chrono::seconds(5)) return 2; + } + times.push_back(std::chrono::duration(std::chrono::steady_clock::now() - start).count()); + } + std::sort(times.begin(), times.end()); + std::printf("%s: callback affinity:", warm ? "before pin warm-up" : "after pin lazy creation"); + for (int i = 0; i < CPU_SETSIZE; ++i) + if (CPU_ISSET(i, &state.mask)) std::printf(" %d", i); + std::printf("; median %.2f us, p95 %.2f us\n", times[100], times[190]); + check(cudaStreamSynchronize(stream)); + check(cudaStreamDestroy(stream)); +} diff --git a/bench/results/2026-10-11-rtx-5090-callback-affinity/README.md b/bench/results/2026-10-11-rtx-5090-callback-affinity/README.md new file mode 100644 index 00000000..950f5f0b --- /dev/null +++ b/bench/results/2026-10-11-rtx-5090-callback-affinity/README.md @@ -0,0 +1,84 @@ +# RTX 5090: adaptive-overlap callback affinity (#1975) + +## Machine and engine + +- RTX 5090, 32 GB; Ryzen 9 9950X, 16 cores / 32 logical CPUs; 123 GiB usable RAM. +- NVIDIA 610.57.04; CUDA 13.3.33; Release build for sm_120. +- Fedora Silverblue host, Ubuntu 26.04 NVIDIA distrobox. +- v0.1.43, source `7695859aa13afb248bdb4d36f9b2e95707db079d`. +- Qwen3.8-Flash-Next GSQ-RCO IQ3_S, CPU vision; 409600 maximum context, + int8 KV, 32768 resident KV cells, 2048 MiB VRAM reserve. +- Existing calibration retained: `--adapt-every 1 --adapt-swaps 160 --adapt-decay 0.97`. +- 15 CPU pool workers on CPUs 1–15; decoding host on CPU 0; auxiliary CPU placement off. + +## Mechanism and standalone reproduction + +After the caller is pinned, CUDA's first host callback can lazily create a worker +with that same single-CPU affinity. The host spinning for completion and the +callback worker then compete on one logical CPU. Every layer's callback also +holds back subsequent expert copies on its stream until the callback executes. + +The standalone test needs Linux/x86, a CUDA toolkit, an NVIDIA GPU and CPU 0 in +the process's allowed CPU set. From the repository root, inside the CUDA environment: + +```bash +g++ -O2 -std=c++20 -I/usr/local/cuda/include bench/micro/cuda_callback_affinity.cpp \ + -L/usr/local/cuda/lib64 -Wl,-rpath,/usr/local/cuda/lib64 -lcudart \ + -o bench/micro/cuda_callback_affinity.bin +./bench/micro/cuda_callback_affinity.bin lazy +./bench/micro/cuda_callback_affinity.bin warm +``` + +Run the two modes as separate processes so the second does not reuse a callback +worker from the first. Each measures 200 callback/spin handshakes, without profiling: + +| Callback worker initialized | Observed affinity | Median latency | p95 | +|---|---|---:|---:| +| After host pinning | CPU 0 | 984.51 us | 998.06 us | +| Before host pinning | CPUs 0–31 | 4.01 us | 15.95 us | + +This is a scheduling reproducer, not an inference benchmark. Nsight separately +showed callback bodies averaging 0.62 us but much longer dispatch gaps; profiler +latencies should not be treated as unprofiled production measurements. + +## Opt-in warm-up and inference comparison + +Set `STRATA_ADAPT_CALLBACK_WARMUP=1` and leave `STRATA_ADAPT_OVERLAP=1`. +The candidate initializes and synchronizes a no-op CUDA host callback before +constructing `ExpertPool`, leaving its worker free to run on the process's +original allowed CPUs. It does not change swap selection, notifications, +per-layer waits, residency commits, or GPU math. The flag is off by default +and its code is guarded to Linux CUDA; overlap-off remains a valid workaround. + +Four starts in normal/warm-up/warm-up/normal order. No competing inference +workload: PiForge and the persistent Strata service stopped for the tests. +Same binary and existing calibration, fixed `--pcie-frac 0.4`, auto expert cache, +temperature 0, 512-token output caps. Calibration's three prompts were prefixed +with synthetic sensor-record documents at 4K; only the code prompt at 100K. +Each context had one warm-up round followed by two measured rounds per start. + +| Case | Normal callbacks, mean | Early warm-up, mean | Gain | Requests per setting | +|---|---:|---:|---:|---:| +| 4K, three prompts | 142.82 TPS | 207.75 TPS | 45.5% | 12 | +| 100K, warmed code prompt | 232.96 TPS | 252.19 TPS | 8.3% | 4 | + +Across both starts, including warm-up, adaptive-gate waiting dropped from +15043.63 ms / 3453 active windows (4.357 ms/window) to 1.16 ms / 3378 windows +(0.000343 ms/window). A matched 512-token 4K code output (token-array JSON SHA256 +prefix `1956a5e9aec0`) averaged 180.35 TPS normally (three measured requests) +versus 236.08 TPS with early warm-up (two requests). + +## Validation limits + +- The inference A/B used an experimental build that also contained an opt-in + GPU-signal alternative, **disabled in every arm**. Only the early warm-up flag + changed between these arms. The minimal warm-up-only patch compiled separately + with the same installed Release build's flags/libraries. +- Not all token arrays matched, including between existing-path runs. Auto-sized + cache capacities varied slightly; full-suite bit-exact equivalence is not claimed. +- These means are a new paired suite, not replacements for #1975's original + 126 TPS overlap-on / 170 TPS overlap-off means (a different mix of long-context cases). +- Only this NVIDIA/Linux/CUDA combination was tested. Other drivers/GPUs, + multi-GPU behavior and HIP/SYCL builds remain unverified. No global default changed. +- Production was restored with the original engine, overlap disabled and its + calibration unchanged; PiForge was restarted. diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 49c49d1b..d7d87d4f 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -1704,12 +1704,19 @@ the pairs in which the switched arm won). They are here so you can try them on y `STRATA_ADAPT_NOWAIT` or `STRATA_DMA_BATCH`; otherwise the log says why it is off. RTX A4000, 6 interleaved pairs each, decode median tok/s: Q2_0 16 GB 62.5 -> 66.2 (+5.9%), Q2_0 as a 12 GB card 51.3 -> 54.2 (+5.7%), IQ3_XXS 16 GB 48.6 -> 52.6 (+8.3%), IQ3_XXS as 12 GB 39.3 -> 41.5 (+5.5%), 6/6 pairs faster every time. +- **`STRATA_ADAPT_CALLBACK_WARMUP=1` (Linux CUDA, opt-in): initialize the CUDA host-callback thread before the CPU + pool pins the host thread.** A callback thread created later can inherit the host's one-CPU affinity and compete + with the verifier spinning for its signal (#1975). The warm-up runs and synchronizes one no-op host callback before + constructing the pool; swap selection, per-layer callbacks and residency updates stay unchanged. On an RTX 5090 / + Ryzen 9 9950X, a paired 4K three-prompt test averaged 142.82 -> 207.75 tok/s. Other GPUs and drivers remain untested; + see the [reproducer, settings and limitations](../bench/results/2026-10-11-rtx-5090-callback-affinity/README.md). ## Thread placement: `--aux-cpus` (Linux, off by default) The CPU pool pins its workers (one logical CPU per physical core) and the host thread pins itself to the pool's reserved core. A thread the engine starts after that inherits the host's one CPU: the adaptive tier's job thread, the prefill -helpers, the router look-ahead. The threads the CUDA driver starts are free to run on any CPU, the workers' included. +helpers, the router look-ahead. CUDA driver threads created before pinning can run on any allowed CPU, the workers' +included; a lazily created host-callback thread can instead inherit the pinned host's one CPU (see the warm-up above). `--aux-cpus` puts every thread that is neither a pool worker nor the host thread on spare CPUs. It moves threads only and never changes a result. diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 4aa057db..884e3839 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4741,6 +4741,18 @@ int main(int argc, char** argv) { #endif srcp = &arena_src; } +#if defined(__linux__) && !defined(STRATA_USE_HIP) + if (const char* e = std::getenv("STRATA_ADAPT_CALLBACK_WARMUP"); e && e[0] == '1' && adapt_overlap()) { + // CUDA creates its host-callback thread lazily. Create it before ExpertPool pins this thread: + // otherwise it inherits the host core and competes with the verifier's busy wait for its signal. + if (cudaLaunchHostFunc(nullptr, [](void*) {}, nullptr) != cudaSuccess || + cudaStreamSynchronize(nullptr) != cudaSuccess) { + std::fprintf(stderr, "strata generate: cannot warm up the CUDA host-callback thread\n"); + return 1; + } + std::fprintf(stderr, "strata generate: CUDA host-callback thread warmed before host-core pinning\n"); + } +#endif 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",