| 220 | } |
| 221 | |
| 222 | bool PeerExperts::open(int device, const std::vector<std::pair<int32_t, int32_t>>& ranked, const ExpertCache& primary, |
| 223 | ExpertSource& src, int64_t n_layers, int64_t n_expert, int reserve_mib, int64_t max_slots, |
| 224 | std::string& err) { |
| 225 | close(); |
| 226 | int count = 0; |
| 227 | if (!ck(cudaGetDeviceCount(&count), "cudaGetDeviceCount", err)) return false; |
| 228 | if (device < 0 || device >= count || device == primary_device()) { |
| 229 | err = "peer experts: CUDA device " + std::to_string(device) + " is not a valid secondary GPU (" + std::to_string(count) + |
| 230 | " devices, primary=" + std::to_string(primary_device()) + "; set CUDA_VISIBLE_DEVICES)"; |
| 231 | return false; |
| 232 | } |
| 233 | const auto& lay = strata::kernels::cpu::expert_layout(); |
| 234 | n_layers_ = n_layers; |
| 235 | n_expert_ = n_expert; |
| 236 | src_ = &src; |
| 237 | res_.assign((size_t) (n_layers * n_expert), kNotResident); |
| 238 | { // direct access both ways (NVLink or another P2P path): the prompt path copies activations and results over it |
| 239 | int a = 0, b = 0; |
| 240 | cudaDeviceCanAccessPeer(&a, primary_device(), device); |
| 241 | cudaDeviceCanAccessPeer(&b, device, primary_device()); |
| 242 | if (const char* np = std::getenv("STRATA_PEER_NO_P2P"); np != nullptr && std::atoi(np) != 0) a = 0; // test knob: pretend the pair has no P2P |
| 243 | p2p_ = a && b; |
| 244 | if (p2p_ && !p2p_topology_safe(primary_device(), device)) { |
| 245 | std::fprintf(stderr, "strata: peer GPU %d: GPUs sit behind a PCIe host bridge (PHB/SYS, no shared PCIe switch or NVLink); " |
| 246 | "using portable host-pinned staging instead of P2P (STRATA_PEER_NO_P2P=0 overrides)\n", device); |
| 247 | p2p_ = false; |
| 248 | } |
| 249 | if (p2p_) { |
| 250 | On on0(primary_device()); |
| 251 | cudaError_t e0 = cudaDeviceEnablePeerAccess(device, 0); |
| 252 | if (e0 == cudaErrorPeerAccessAlreadyEnabled) { cudaGetLastError(); e0 = cudaSuccess; } |
| 253 | On on1(device); |
| 254 | cudaError_t e1 = cudaDeviceEnablePeerAccess(primary_device(), 0); |
| 255 | if (e1 == cudaErrorPeerAccessAlreadyEnabled) { cudaGetLastError(); e1 = cudaSuccess; } |
| 256 | p2p_ = e0 == cudaSuccess && e1 == cudaSuccess; |
| 257 | // recommend, never force: the direct path is used only if a test copy each way arrives exact (#2666) |
| 258 | if (p2p_ && !p2p_copies_data(device)) { |
| 259 | std::fprintf(stderr, "strata: peer GPU %d: peer access is enabled but a test copy between the two GPUs arrived wrong " |
| 260 | "(a host bridge between them, no real P2P): the prompt path takes the host route instead " |
| 261 | "(STRATA_PEER_NO_P2P=1 does that by hand)\n", device); |
| 262 | { On q0(primary_device()); cudaDeviceDisablePeerAccess(device); cudaGetLastError(); } |
| 263 | { On q1(device); cudaDeviceDisablePeerAccess(primary_device()); cudaGetLastError(); } |
| 264 | p2p_ = false; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | On on(device); |
| 269 | device_ = device; |
| 270 | // scratch first, so the slots take what is really left |
| 271 | int64_t ff = strata::kernels::cpu::FF; |
| 272 | if (lay.native) |
| 273 | for (const auto& f : lay.fmt) ff = std::max<int64_t>(ff, f.n_ff); |
| 274 | const size_t scratch = std::max<size_t>((size_t) strata::kernels::moe_hit_grouped_scratch_bytes(CAP, H, ff), |
| 275 | strata::kernels::native_expert_scratch_bytes(CAP, ff)); |
| 276 | const bool alloc_ok = |
| 277 | ck(cudaStreamCreateWithFlags(&stream_, cudaStreamNonBlocking), "stream", err) && |
| 278 | ck(cudaStreamCreateWithFlags(&refill_, cudaStreamNonBlocking), "refill stream", err) && |
| 279 | ck(cudaEventCreateWithFlags(&refill_ev_, cudaEventDisableTiming), "event", err) && |