| 263 | } |
| 264 | |
| 265 | bool RemoteExperts::begin(int64_t layer, const float* x, const int32_t* ids, int64_t n_tok, |
| 266 | int64_t k, const int32_t* kind, const int32_t* primary_res, |
| 267 | std::string& err) { |
| 268 | // cumulative host time in here (staging and launches), reported per request by the driver |
| 269 | struct Timer { double& acc; std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now(); |
| 270 | ~Timer() { acc += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count(); } } timer{ms_begin_}; |
| 271 | const int64_t n = n_tok * k; |
| 272 | if (n <= 0 || n > CAP || n_tok > strata::kernels::kWindowMaxT || k != (int) strata::core::kCompiledGeometry.n_expert_used || layer < 0 || |
| 273 | (size_t) layer >= layers_present_.size() || device_ < 0) { |
| 274 | err = "CUDA" + std::to_string(device_) + " experts: invalid layer, routing width or window size"; |
| 275 | return false; |
| 276 | } |
| 277 | std::fill(owned_.begin(), owned_.begin() + n, 0); |
| 278 | group_id_.clear(); ptr_.clear(); start_.clear(); dst_.clear(); tok_.clear(); original_row_.clear(); |
| 279 | if (!layers_present_[(size_t) layer]) return true; |
| 280 | std::fill(group_of_.begin(), group_of_.begin() + n, -1); |
| 281 | for (int64_t i = 0; i < n; ++i) { |
| 282 | const int32_t e = ids[i]; |
| 283 | if ((kind && kind[i] != -1) || e < 0 || e >= n_expert_ || |
| 284 | (primary_res && primary_res[(size_t) layer * (size_t) n_expert_ + (size_t) e] >= 0)) continue; |
| 285 | const int32_t slot = cache_.slot_of(layer, e); |
| 286 | if (slot < 0) continue; |
| 287 | owned_[(size_t) i] = 1; |
| 288 | int32_t group = -1; |
| 289 | for (size_t g = 0; g < group_id_.size(); ++g) |
| 290 | if (group_id_[g] == e) { group = (int32_t) g; break; } |
| 291 | if (group < 0) { |
| 292 | group = (int32_t) group_id_.size(); |
| 293 | group_id_.push_back(e); |
| 294 | ptr_.push_back((unsigned long long) cache_.device_slot(slot)); |
| 295 | } |
| 296 | group_of_[(size_t) i] = group; |
| 297 | ++computed_; |
| 298 | } |
| 299 | if (group_id_.empty()) return true; |
| 300 | for (size_t g = 0; g < group_id_.size(); ++g) { |
| 301 | start_.push_back((int32_t) dst_.size()); |
| 302 | for (int64_t i = 0; i < n; ++i) if (group_of_[(size_t) i] == (int32_t) g) { |
| 303 | // The grouped kernels read the activation from `tok`, so their |
| 304 | // output row can instead be packed densely for the USB4 return. |
| 305 | original_row_.push_back((int32_t) i); |
| 306 | dst_.push_back((int32_t) dst_.size()); |
| 307 | tok_.push_back((int32_t) (i / k)); |
| 308 | } |
| 309 | } |
| 310 | start_.push_back((int32_t) dst_.size()); |
| 311 | // Private pinned buffers survive until this GPU has consumed them. The CPU |
| 312 | // pool can write other output rows without a cross-device race. |
| 313 | std::memcpy(h_x_, x, (size_t) n_tok * H * sizeof(float)); |
| 314 | auto* meta = (RemoteMeta*) h_meta_; |
| 315 | std::memcpy(meta->ptr, ptr_.data(), ptr_.size() * sizeof(ptr_[0])); |
| 316 | std::memcpy(meta->start, start_.data(), start_.size() * sizeof(start_[0])); |
| 317 | std::memcpy(meta->dst, dst_.data(), dst_.size() * sizeof(dst_[0])); |
| 318 | std::memcpy(meta->tok, tok_.data(), tok_.size() * sizeof(tok_[0])); |
| 319 | meta->count = (int32_t) group_id_.size(); |
| 320 | const bool reduce = remote_opt_ && remote_opt_->active(); |
| 321 | if (reduce) remote_opt_->prepare(*this, meta + 1); |
| 322 | DeviceScope scope(device_); |
nothing calls this directly
no test coverage detected