MCPcopy Create free account
hub / github.com/Niko1221/Strata / adapt

Method adapt

engine/src/core/peer_experts.cpp:607–684  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

605}
606
607bool PeerExperts::adapt(const float* usage, const int32_t* res0, int max_swaps, std::string& err) {
608 if (!pending_.empty() || max_swaps <= 0) return true;
609 const auto& lay = strata::kernels::cpu::expert_layout();
610 struct Swap { float gain; int32_t layer, in, out; };
611 std::vector<Swap> swaps;
612 std::vector<std::pair<float, int32_t>> cand, vict;
613 for (int64_t l = 0; l < n_layers_; ++l) {
614 cand.clear();
615 vict.clear();
616 const float* u = usage + l * n_expert_;
617 const int32_t* r0 = res0 + l * n_expert_;
618 const int32_t* r1 = res_.data() + l * n_expert_;
619 for (int32_t e = 0; e < (int32_t) n_expert_; ++e) {
620 if (r1[e] >= 0) vict.emplace_back(u[e], e);
621 else if (r0[e] < 0 && u[e] >= 2.0f) cand.emplace_back(u[e], e);
622 }
623 if (cand.empty() || vict.empty()) continue;
624 std::sort(cand.begin(), cand.end(), [](auto& a, auto& b) { return a.first > b.first; });
625 const size_t nc = std::min(cand.size(), vict.size());
626 std::partial_sort(vict.begin(), vict.begin() + (ptrdiff_t) nc, vict.end(),
627 [](auto& a, auto& b) { return a.first < b.first; });
628 for (size_t i = 0; i < nc; ++i) {
629 if (cand[i].first < vict[i].first + 1.5f) break;
630 swaps.push_back({cand[i].first - vict[i].first, (int32_t) l, cand[i].second, vict[i].second});
631 }
632 }
633 if (swaps.empty()) return true;
634 std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; });
635 if ((int) swaps.size() > max_swaps) swaps.resize((size_t) max_swaps);
636 const bool gated = gate_landed_ != nullptr;
637 if (gated) { // the copies go in layer order and each layer's landing raises the gate's counter
638 std::stable_sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.layer < b.layer; });
639 gate_landed_->store(-1, std::memory_order_release);
640 if (h_gate_ != nullptr) *h_gate_ = -1;
641 gate_has_->assign((size_t) n_layers_, 0);
642 *gate_last_ = -1;
643 }
644 On on(device_);
645 int32_t issued_layer = -1; // the layer whose copies are on the stream and not yet ticked
646 auto tick = [&](int32_t layer) -> bool {
647 if ((h_gate_ != nullptr
648 ? cudaMemcpyAsync((void*) h_gate_, d_gate_vals_ + layer, 4, cudaMemcpyDeviceToHost, refill_)
649 : cudaLaunchHostFunc(refill_, [](void* p) {
650 auto* t = (Tick*) p;
651 int32_t cur = t->counter->load(std::memory_order_relaxed);
652 while (cur < t->layer && !t->counter->compare_exchange_weak(cur, t->layer, std::memory_order_release)) {}
653 }, &ticks_[(size_t) layer])) == cudaSuccess)
654 return true;
655 (void) cudaGetLastError();
656 cudaStreamSynchronize(refill_); // no callback: wait here, the gate opens at once
657 gate_landed_->store((int32_t) n_layers_, std::memory_order_release);
658 return false;
659 };
660 for (const Swap& s : swaps) {
661 const size_t in = (size_t) (s.layer * n_expert_ + s.in), out = (size_t) (s.layer * n_expert_ + s.out);
662 const int32_t slot = res_[out];
663 const uint8_t* b = src_->blob(s.layer, s.in);
664 if (slot < 0 || b == nullptr) continue;

Callers

nothing calls this directly

Calls 15

expert_layoutFunction · 0.85
sortFunction · 0.85
stable_sortFunction · 0.85
cudaStreamQueryFunction · 0.85
push_backMethod · 0.80
assignMethod · 0.80
compare_exchange_weakMethod · 0.80
ckFunction · 0.70
minFunction · 0.50
cudaMemcpyAsyncFunction · 0.50
cudaLaunchHostFuncFunction · 0.50
cudaGetLastErrorFunction · 0.50

Tested by

no test coverage detected