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);