From 3315072467222fc0f070b0fbff70812289e4b2ef Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 16 Oct 2026 01:52:48 +0200 Subject: [PATCH] peer experts: peer access is used only if a test copy each way arrives exact (#2666) On 2x RTX A4000 behind a host bridge cudaDeviceCanAccessPeer says 1 and cudaDeviceEnablePeerAccess succeeds, and cudaMemcpyPeerAsync then returns success having moved nothing (1,048,576 of 1,048,576 floats wrong in both directions in a stand-alone test; right without peer access enabled, the driver stages it). The peer prompt path trusted it: the peer's input rows were zeros and its results never reached the primary, the first prompt's MoE output was garbage and the logits NaN (--peer-device 1, --peer-reserve-mib 1500..2100; STRATA_PF_PEER_COMPACT is not the cause). PeerExperts::open now copies 64 KiB each way once after enabling access; a wrong result turns peer access off again and takes the host route (the one STRATA_PEER_NO_P2P=1 asks for), and says so. A test that cannot run keeps the old trust. Reported by @CaptRauti on #2666 (WDDM, 2x RTX 3060: the no-P2P route is the one they ran). --- src/core/peer_experts.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/core/peer_experts.cpp b/src/core/peer_experts.cpp index 6c8f567e..9ca2ee99 100644 --- a/src/core/peer_experts.cpp +++ b/src/core/peer_experts.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace strata::core { @@ -41,6 +42,45 @@ struct On { ~On() { int cur = 0; cudaGetDevice(&cur); if (cur != prev) cudaSetDevice(prev); } }; +/// #2666: `cudaDeviceCanAccessPeer` said yes and `cudaDeviceEnablePeerAccess` succeeded, and on a pair of GPUs behind a host bridge +/// (2x RTX A4000, "PHB" in nvidia-smi topo) `cudaMemcpyPeerAsync` then returned success having moved nothing: the peer's input rows +/// were zeros and the rows it sent back never arrived, so the first prompt's MoE output was garbage and the logits NaN. Without +/// peer access enabled the same copies are right (the driver stages them). So the pair is asked once, with a small copy each way, +/// whether the direct path really carries data. true = both ways exact (or nothing could be tested: the old trust is kept). +bool p2p_copies_data(int device) { + constexpr size_t kN = 1 << 14; // floats + std::vector h(kN), back(kN); + for (size_t i = 0; i < kN; ++i) h[i] = 1.0f + (float) ((i * 2654435761u) % 4093u); + float* d[2] = {nullptr, nullptr}; + const int dev[2] = {0, device}; + bool ok = true, tested = true; + for (int k = 0; k < 2 && tested; ++k) { + On on(dev[k]); + if (cudaMalloc((void**) &d[k], kN * 4) != cudaSuccess) { cudaGetLastError(); tested = false; } + } + for (int dir = 0; dir < 2 && tested && ok; ++dir) { // 0: device 0 -> peer, 1: peer -> device 0 + const int from = dir == 0 ? 0 : 1, to = 1 - from; + { + On on(dev[from]); + if (cudaMemcpy(d[from], h.data(), kN * 4, cudaMemcpyHostToDevice) != cudaSuccess) { tested = false; break; } + } + { + On on(dev[to]); + if (cudaMemset(d[to], 0, kN * 4) != cudaSuccess) { tested = false; break; } + } + if (cudaMemcpyPeer(d[to], dev[to], d[from], dev[from], kN * 4) != cudaSuccess) { tested = false; break; } + { + On on(dev[to]); + if (cudaMemcpy(back.data(), d[to], kN * 4, cudaMemcpyDeviceToHost) != cudaSuccess) { tested = false; break; } + } + ok = std::memcmp(back.data(), h.data(), kN * 4) == 0; + } + for (int k = 0; k < 2; ++k) + if (d[k] != nullptr) { On on(dev[k]); cudaFree(d[k]); } + if (!tested) { cudaGetLastError(); return true; } + return ok; +} + bool ck(cudaError_t e, const char* what, std::string& err) { if (e == cudaSuccess) return true; err = std::string("peer experts: ") + what + ": " + cudaGetErrorString(e); @@ -108,6 +148,15 @@ bool PeerExperts::open(int device, const std::vector cudaError_t e1 = cudaDeviceEnablePeerAccess(0, 0); if (e1 == cudaErrorPeerAccessAlreadyEnabled) { cudaGetLastError(); e1 = cudaSuccess; } p2p_ = e0 == cudaSuccess && e1 == cudaSuccess; + // recommend, never force: the direct path is used only if a test copy each way arrives exact (#2666) + if (p2p_ && !p2p_copies_data(device)) { + std::fprintf(stderr, "strata: peer GPU %d: peer access is enabled but a test copy between the two GPUs arrived wrong " + "(a host bridge between them, no real P2P): the prompt path takes the host route instead " + "(STRATA_PEER_NO_P2P=1 does that by hand)\n", device); + { On q0(0); cudaDeviceDisablePeerAccess(device); cudaGetLastError(); } + { On q1(device); cudaDeviceDisablePeerAccess(0); cudaGetLastError(); } + p2p_ = false; + } } } On on(device);