the dispatch thread: score the prediction made for `layer` against the experts it routes outside the GPU cache
| 3616 | |
| 3617 | // the dispatch thread: score the prediction made for `layer` against the experts it routes outside the GPU cache |
| 3618 | void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res, int pcie_num) { |
| 3619 | if (!stats_on_ || layer < 1 || layer >= (int64_t) routers_.size()) return; |
| 3620 | ++st_layers_; |
| 3621 | if (pstamp_[(size_t) layer].load(std::memory_order_acquire) != gen_.load(std::memory_order_relaxed) + 1) { |
| 3622 | ++st_nopred_; |
| 3623 | return; |
| 3624 | } |
| 3625 | std::vector<uint64_t> act((size_t) words_, 0); |
| 3626 | for (int64_t i = 0; i < n; ++i) { |
| 3627 | const int32_t e = ids[i]; |
| 3628 | if (e < 0 || e >= n_expert_) continue; |
| 3629 | if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue; |
| 3630 | act[(size_t) (e >> 6)] |= 1ull << (e & 63); |
| 3631 | } |
| 3632 | uint64_t na = 0; |
| 3633 | for (int64_t w = 0; w < words_; ++w) na += (uint64_t) la_pc64(act[(size_t) w]); |
| 3634 | st_actual_ += na; |
| 3635 | { // the vote-ranked top M (the experts most tokens predict) against the misses, and against the PCIe share |
| 3636 | static const int vm[3] = {4, 8, 12}; |
| 3637 | int32_t vt[kVoteN]; |
| 3638 | int nv = 0; |
| 3639 | for (int i = 0; i < kVoteN; ++i) { |
| 3640 | const int32_t v = pvote_[(size_t) (layer * kVoteN + i)].load(std::memory_order_relaxed); |
| 3641 | if (v == 0) break; |
| 3642 | vt[nv++] = v - 1; |
| 3643 | } |
| 3644 | // the misses in routing order (first appearance), the PCIe share = the last (n_miss * pcie_num / 256) of them |
| 3645 | std::vector<int32_t> order_miss; |
| 3646 | for (int64_t i = 0; i < n; ++i) { |
| 3647 | const int32_t e = ids[i]; |
| 3648 | if (e < 0 || e >= n_expert_) continue; |
| 3649 | if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue; |
| 3650 | if (std::find(order_miss.begin(), order_miss.end(), e) == order_miss.end()) order_miss.push_back(e); |
| 3651 | } |
| 3652 | const int m = ((int) order_miss.size() * pcie_num) >> 8; |
| 3653 | st_pch_ += (uint64_t) m; |
| 3654 | for (int s = 0; s < 3; ++s) { |
| 3655 | const int top = std::min(vm[s], nv); |
| 3656 | st_vpred_[s] += (uint64_t) top; |
| 3657 | for (int j = 0; j < top; ++j) { |
| 3658 | if ((act[(size_t) (vt[j] >> 6)] >> (vt[j] & 63)) & 1ull) ++st_vhit_[s]; |
| 3659 | const auto it = std::find(order_miss.begin(), order_miss.end(), vt[j]); |
| 3660 | if (it != order_miss.end() && (int) (it - order_miss.begin()) >= (int) order_miss.size() - m) ++st_pcov_[s]; |
| 3661 | } |
| 3662 | } |
| 3663 | } |
| 3664 | for (int s = 0; s < 3; ++s) { |
| 3665 | uint64_t hit = 0, np = 0; |
| 3666 | for (int64_t w = 0; w < words_; ++w) { |
| 3667 | const uint64_t pm = pmask_[(size_t) ((layer * 3 + s) * words_ + w)].load(std::memory_order_relaxed); |
| 3668 | hit += (uint64_t) la_pc64(pm & act[(size_t) w]); |
| 3669 | np += (uint64_t) la_pc64(pm); |
| 3670 | } |
| 3671 | st_hit_[s] += hit; |
| 3672 | st_pred_[s] += np; |
| 3673 | } |
| 3674 | if (layer == (int64_t) routers_.size() - 1 && (++st_windows_ % 100) == 0) { |
| 3675 | const uint64_t scored = st_layers_ - st_nopred_; |