From 9abc623f2c99e6dbe2a9e4b8a28c5fb8203903c0 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 2 Oct 2026 18:41:01 +0200 Subject: [PATCH] PCIe probe (#485): best of four timed bursts, a continuous share, the samples logged The probe timed one 1 GiB burst; an x16 PCIe 4 laptop (RTX A3000) read 18.5, 6.9 and 5.8 GB/s in three starts, and the low reading set pcie_frac (0.12-0.15 where calibrate.py found 0.35). The same 1 GiB is now four bursts of 256 MiB, each timed on its own (events between them on the stream; on Windows HIP the host clock, #377), and the fastest is the link's figure: a disturbance only ever slows a copy, while the median would still follow one lasting through half the bursts. The probe copies no more than before. The share was base above 20 GB/s and base * bw / 26 below (19.9 GB/s -> 0.42, 20.0 -> 0.55; 4.0 -> 0.08, 3.9 -> 0). It is now base * min(1, bw / 20): continuous, unchanged from 20 GB/s up (an x16 PCIe 4/5 link reads ~26-28 GB/s, so the default stays 0.55 there), in proportion to the link below, so the link's time on its share stays about what the x16 share costs. The rule is one function for CUDA0 and the layer split's stages. The log names every burst: "PCIe probe: 28.9 GB/s host->device (best of 28.9 28.9 28.9 28.9)" (the RTX 5070 here; pcie_frac 0.55 as before). Noted, not changed: a given --pcie-frac is every stage's share and skips the stages' probes. --- docs/MULTI_GPU.md | 3 +- src/program/generate.cpp | 105 ++++++++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 35 deletions(-) diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.md index 1008620e..6e0b0d96 100644 --- a/docs/MULTI_GPU.md +++ b/docs/MULTI_GPU.md @@ -8,7 +8,8 @@ speed comes from (decode then barely touches the CPU pool). This is pipeline (layer) parallelism, not tensor parallelism: a token crosses from one card to the next once per verify window (a few hundred KB through pinned RAM), not twice per layer. No NVLink or peer-to-peer access is -needed; cards on x4 or x1 slots work, and the PCIe share of each card is probed on its own link. +needed; cards on x4 or x1 slots work, and the PCIe share of each card is probed on its own link. A `--pcie-frac` you give +is every card's share and skips those probes; there is no per-card setting yet. ## Using it diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 609179ba..f73cfca4 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -956,47 +956,81 @@ bool load_control_vectors(const Options& o, const strata::core::ModelGeometry& g // The effective host->device bandwidth of the PCIe link: copies from pinned host memory, as the expert arena's // reads are. The native default share (0.55) was measured on x16 links (~26-28 GB/s); a x8 card in a x8 slot // carries about half of that. Returns < 0 when the probe cannot run (then the caller keeps the default). -double probe_pcie_h2d_gbps() { +// +// #485: a single timed burst read an x16 PCIe 4 link (RTX A3000 laptop) at 18.5, 6.9 and 5.8 GB/s in three starts - +// a link still in a low-power state, other DMA, a context not yet up to speed - and the low reading set the share. +// Such a disturbance only ever slows a copy: nothing makes a correctly timed copy faster than the link carries. So +// the same 1 GiB is now copied as four bursts of 256 MiB, timed one by one, and the fastest is the link's figure (the +// median would still follow a disturbance that lasts through half the bursts). Each burst takes ~10 ms on an x16 +// PCIe 4 link, so the probe takes no longer than the one 1 GiB burst did. `samples`, when given, gets every +// burst's reading for the log. +double probe_pcie_h2d_gbps(std::string* samples = nullptr) { constexpr size_t kBytes = 256ull << 20; - constexpr int kIters = 4; + constexpr int kBursts = 4; void* h = nullptr; void* d = nullptr; - cudaEvent_t ev0, ev1; if (cudaMallocHost(&h, kBytes) != cudaSuccess) return -1.0; - if (cudaMalloc(&d, kBytes) != cudaSuccess || cudaEventCreate(&ev0) != cudaSuccess || - cudaEventCreate(&ev1) != cudaSuccess) { - if (d != nullptr) cudaFree(d); + if (cudaMalloc(&d, kBytes) != cudaSuccess) { cudaFreeHost(h); return -1.0; } std::memset(h, 0, kBytes); // fault the pages in before timing cudaMemcpyAsync(d, h, kBytes, cudaMemcpyHostToDevice); // warmup: context up, copy engine primed + float ms[kBursts] = {}; #if defined(STRATA_USE_HIP) && defined(_WIN32) // Windows HIP: the events do not bracket the copies there (an RX 6800 read 3,300-26,000 GB/s, so every link kept - // the x16 share), so the copies are timed on the host: 1 GiB takes tens of milliseconds, so the synchronize - // around them hardly matters - const bool warm = cudaDeviceSynchronize() == cudaSuccess; - const Clock::time_point t0 = Clock::now(); - for (int i = 0; i < kIters; ++i) cudaMemcpyAsync(d, h, kBytes, cudaMemcpyHostToDevice); - const bool ok = warm && cudaDeviceSynchronize() == cudaSuccess; - const float ms = (float) std::chrono::duration(Clock::now() - t0).count(); - const bool timed = ok && ms > 0.01f; + // the x16 share), so each burst is timed on the host between two synchronizes: 256 MiB takes ~10 ms, so the + // synchronize around it hardly matters + bool ok = cudaDeviceSynchronize() == cudaSuccess; + for (int b = 0; b < kBursts && ok; ++b) { + const Clock::time_point t0 = Clock::now(); + cudaMemcpyAsync(d, h, kBytes, cudaMemcpyHostToDevice); + ok = cudaDeviceSynchronize() == cudaSuccess; + ms[b] = (float) std::chrono::duration(Clock::now() - t0).count(); + } #else - cudaEventRecord(ev0); - for (int i = 0; i < kIters; ++i) cudaMemcpyAsync(d, h, kBytes, cudaMemcpyHostToDevice); - cudaEventRecord(ev1); - const bool ok = cudaEventSynchronize(ev1) == cudaSuccess; - float ms = 0.f; - const bool timed = ok && cudaEventElapsedTime(&ms, ev0, ev1) == cudaSuccess && ms > 0.01f; + // the bursts run back to back on the stream, an event between each two + cudaEvent_t ev[kBursts + 1]; + int n_ev = 0; + while (n_ev <= kBursts && cudaEventCreate(&ev[n_ev]) == cudaSuccess) ++n_ev; + bool ok = n_ev == kBursts + 1; + if (ok) { + cudaEventRecord(ev[0]); + for (int b = 0; b < kBursts; ++b) { + cudaMemcpyAsync(d, h, kBytes, cudaMemcpyHostToDevice); + cudaEventRecord(ev[b + 1]); + } + ok = cudaEventSynchronize(ev[kBursts]) == cudaSuccess; + for (int b = 0; b < kBursts && ok; ++b) ok = cudaEventElapsedTime(&ms[b], ev[b], ev[b + 1]) == cudaSuccess; + } + for (int i = 0; i < n_ev; ++i) cudaEventDestroy(ev[i]); #endif - const double bw = timed ? ((double) kIters * (double) kBytes / (ms * 1e-3)) / 1e9 : -1.0; - cudaEventDestroy(ev0); - cudaEventDestroy(ev1); + double bw = -1.0; + if (samples != nullptr) samples->clear(); + for (int b = 0; b < kBursts && ok; ++b) { + const double s = ms[b] > 0.01f ? (double) kBytes / (ms[b] * 1e-3) / 1e9 : -1.0; + bw = std::max(bw, s); + if (samples != nullptr) { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%s%.1f", b == 0 ? "" : " ", s); + *samples += buf; + } + } + if (!ok) cudaGetLastError(); cudaFree(d); cudaFreeHost(h); return bw; } +// The PCIe share of the missed experts for a link measured at `gbps`: `base` (the share measured on x16 links) from +// 20 GB/s up, and below that in proportion to the bandwidth, so the time the link spends on its share stays about +// what the x16 share costs. Continuous (#485): before, 19.9 GB/s gave 0.42 and 20.0 the full 0.55 (and 4.0 GB/s +// gave 0.08, 3.9 none), so a reading near either edge moved the share by a quarter of its range. From 20 GB/s up +// (an x16 PCIe 4/5 link: ~26-28 GB/s) the share is unchanged. +double pcie_frac_for_gbps(double gbps, double base) { + return gbps <= 0.0 ? base : base * std::min(1.0, gbps / 20.0); +} + } // namespace int main(int argc, char** argv) { @@ -1289,6 +1323,8 @@ int main(int argc, char** argv) { // the next visible ones); "auto" places the K from each GPU's free VRAM once the weights are in (below). Across // GPUs, not yet: KV streaming, images, control vectors, the helper caches (--expert-cache-remote), and lending // cache slots to the prompt path (each stage's prompt path has its own buffers). + // --pcie-frac given: that one share is every stage's (the stages' own link probes are skipped), so a split over + // a fast and a slow link cannot set the two apart from the command line (#485) const bool pcie_given = o.pcie_frac >= 0.0; std::vector split_at; std::vector split_devs; @@ -1744,15 +1780,14 @@ int main(int argc, char** argv) { // pack's 0.2 was never measured against the link, so it is left alone. `--calibrate` measures it outright. if (o.pcie_frac < 0.0) { const double base = native_pack ? 0.55 : 0.2; - const double bw = native_pack ? probe_pcie_h2d_gbps() : -1.0; + std::string bursts; + const double bw = native_pack ? probe_pcie_h2d_gbps(&bursts) : -1.0; if (!native_pack) { o.pcie_frac = base; } else if (bw > 0.0) { - // below ~4 GB/s (an x1 link: ~0.9 GB/s) a missed expert's 1.4 MB takes longer to cross than the CPU - // pool takes to compute it, so none of them go over the link - o.pcie_frac = bw >= 20.0 ? base : bw < 4.0 ? 0.0 : std::min(base, std::max(0.05, base * (bw / 26.0))); - std::fprintf(stderr, "strata generate: PCIe probe: %.1f GB/s host->device -> pcie_frac %.2f (default %.2f)\n", - bw, o.pcie_frac, base); + o.pcie_frac = pcie_frac_for_gbps(bw, base); + std::fprintf(stderr, "strata generate: PCIe probe: %.1f GB/s host->device (best of %s) -> pcie_frac %.2f " + "(default %.2f)\n", bw, bursts.c_str(), o.pcie_frac, base); } else { o.pcie_frac = base; std::fprintf(stderr, "strata generate: PCIe probe failed -> pcie_frac default %.2f\n", base); @@ -2253,13 +2288,15 @@ int main(int argc, char** argv) { } strata::kernels::mrope_table_set(st.mrope); } - // its own PCIe share of the missed experts (the same rule as CUDA0's above: its link is probed) + // its own PCIe share of the missed experts (the same rule as CUDA0's above: its link is probed). A given + // --pcie-frac is every stage's share and skips these probes (pcie_given); there is no per-stage setting yet. st.pcie_frac = o.pcie_frac; if (!pcie_given && native_pack) { - const double bw = probe_pcie_h2d_gbps(); - if (bw > 0.0) st.pcie_frac = bw >= 20.0 ? 0.55 : bw < 4.0 ? 0.0 : std::min(0.55, std::max(0.05, 0.55 * (bw / 26.0))); - std::fprintf(stderr, "strata generate: layer split: CUDA%d PCIe probe %.1f GB/s -> pcie_frac %.2f\n", st.dev, - bw, st.pcie_frac); + std::string bursts; + const double bw = probe_pcie_h2d_gbps(&bursts); + if (bw > 0.0) st.pcie_frac = pcie_frac_for_gbps(bw, 0.55); + std::fprintf(stderr, "strata generate: layer split: CUDA%d PCIe probe %.1f GB/s (best of %s) -> pcie_frac " + "%.2f\n", st.dev, bw, bursts.c_str(), st.pcie_frac); } size_t fb = 0, tb = 0; cudaMemGetInfo(&fb, &tb);