Look-ahead stats: vote-ranked top-M coverage of the misses and of the PCIe share
This commit is contained in:
1 parent
1d735110ae
commit
b02a03d97c
2 files changed
+64
-3
No files matched your search
@@ -2149,11 +2149,13 @@ void RouterLookahead::enable_stats(int64_t n_layers) {
|
||||
pstamp_.reset(new std::atomic<uint64_t>[(size_t) n_layers]);
|
||||
for (int64_t i = 0; i < n_layers * 3 * words_; ++i) pmask_[(size_t) i].store(0, std::memory_order_relaxed);
|
||||
for (int64_t i = 0; i < n_layers; ++i) pstamp_[(size_t) i].store(0, std::memory_order_relaxed);
|
||||
pvote_.reset(new std::atomic<int32_t>[(size_t) (n_layers * kVoteN)]);
|
||||
for (int64_t i = 0; i < n_layers * kVoteN; ++i) pvote_[(size_t) i].store(0, std::memory_order_relaxed);
|
||||
stats_on_ = true;
|
||||
}
|
||||
|
||||
// the dispatch thread: score the prediction made for `layer` against the experts it routes outside the GPU cache
|
||||
void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res) {
|
||||
void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res, int pcie_num) {
|
||||
if (!stats_on_ || layer < 1 || layer >= (int64_t) routers_.size()) return;
|
||||
++st_layers_;
|
||||
if (pstamp_[(size_t) layer].load(std::memory_order_acquire) != gen_.load(std::memory_order_relaxed) + 1) {
|
||||
@@ -2170,6 +2172,35 @@ void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n,
|
||||
uint64_t na = 0;
|
||||
for (int64_t w = 0; w < words_; ++w) na += (uint64_t) la_pc64(act[(size_t) w]);
|
||||
st_actual_ += na;
|
||||
{ // the vote-ranked top M (the experts most tokens predict) against the misses, and against the PCIe share
|
||||
static const int vm[3] = {4, 8, 12};
|
||||
int32_t vt[kVoteN];
|
||||
int nv = 0;
|
||||
for (int i = 0; i < kVoteN; ++i) {
|
||||
const int32_t v = pvote_[(size_t) (layer * kVoteN + i)].load(std::memory_order_relaxed);
|
||||
if (v == 0) break;
|
||||
vt[nv++] = v - 1;
|
||||
}
|
||||
// the misses in routing order (first appearance), the PCIe share = the last (n_miss * pcie_num / 256) of them
|
||||
std::vector<int32_t> order_miss;
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const int32_t e = ids[i];
|
||||
if (e < 0 || e >= n_expert_) continue;
|
||||
if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue;
|
||||
if (std::find(order_miss.begin(), order_miss.end(), e) == order_miss.end()) order_miss.push_back(e);
|
||||
}
|
||||
const int m = ((int) order_miss.size() * pcie_num) >> 8;
|
||||
st_pch_ += (uint64_t) m;
|
||||
for (int s = 0; s < 3; ++s) {
|
||||
const int top = std::min(vm[s], nv);
|
||||
st_vpred_[s] += (uint64_t) top;
|
||||
for (int j = 0; j < top; ++j) {
|
||||
if ((act[(size_t) (vt[j] >> 6)] >> (vt[j] & 63)) & 1ull) ++st_vhit_[s];
|
||||
const auto it = std::find(order_miss.begin(), order_miss.end(), vt[j]);
|
||||
if (it != order_miss.end() && (int) (it - order_miss.begin()) >= (int) order_miss.size() - m) ++st_pcov_[s];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int s = 0; s < 3; ++s) {
|
||||
uint64_t hit = 0, np = 0;
|
||||
for (int64_t w = 0; w < words_; ++w) {
|
||||
@@ -2195,6 +2226,14 @@ void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n,
|
||||
st_actual_ ? (double) st_hit_[s] / (double) st_actual_ : 0.0,
|
||||
st_pred_[s] ? (double) st_hit_[s] / (double) st_pred_[s] : 0.0,
|
||||
scored ? (double) st_pred_[s] / (double) scored : 0.0);
|
||||
static const int vm[3] = {4, 8, 12};
|
||||
for (int s = 0; s < 3; ++s)
|
||||
std::fprintf(stderr, " votes top%d: %.2f/layer, covers %.3f of the misses (precision %.3f), %.3f of the PCIe share (%.2f/layer);",
|
||||
vm[s], scored ? (double) st_vpred_[s] / (double) scored : 0.0,
|
||||
st_actual_ ? (double) st_vhit_[s] / (double) st_actual_ : 0.0,
|
||||
st_vpred_[s] ? (double) st_vhit_[s] / (double) st_vpred_[s] : 0.0,
|
||||
st_pch_ ? (double) st_pcov_[s] / (double) st_pch_ : 0.0,
|
||||
scored ? (double) st_pch_ / (double) scored : 0.0);
|
||||
std::fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
@@ -2273,6 +2312,25 @@ void RouterLookahead::run() {
|
||||
}
|
||||
}
|
||||
if (stats_on_ && dj == 0) {
|
||||
// votes: how many tokens put each non-resident expert in their top k_
|
||||
std::vector<std::pair<int, int32_t>> votes; // (sum of 64 - rank, expert)
|
||||
for (int64_t t = 0; t < nt; ++t) {
|
||||
const float* lt = logits.data() + (size_t) (t * n_expert_);
|
||||
for (int64_t e = 0; e < n_expert_; ++e) order[(size_t) e] = (int32_t) e;
|
||||
std::partial_sort(order.begin(), order.begin() + k_, order.end(),
|
||||
[&](int32_t a, int32_t b) { return lt[(size_t) a] > lt[(size_t) b]; });
|
||||
for (int j = 0; j < k_; ++j) {
|
||||
const int32_t e = order[(size_t) j];
|
||||
if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue;
|
||||
auto it = std::find_if(votes.begin(), votes.end(), [&](const auto& v) { return v.second == e; });
|
||||
if (it == votes.end()) votes.emplace_back(64 - j, e);
|
||||
else it->first += 64 - j;
|
||||
}
|
||||
}
|
||||
std::sort(votes.begin(), votes.end(), [](const auto& a, const auto& b) { return a.first > b.first; });
|
||||
for (int i = 0; i < kVoteN; ++i)
|
||||
pvote_[(size_t) (layer * kVoteN + i)].store(i < (int) votes.size() ? votes[(size_t) i].second + 1 : 0,
|
||||
std::memory_order_relaxed);
|
||||
for (int64_t i = 0; i < 3 * words_; ++i)
|
||||
pmask_[(size_t) (layer * 3 * words_ + i)].store(sm[(size_t) i], std::memory_order_relaxed);
|
||||
pstamp_[(size_t) layer].store(sgen + 1, std::memory_order_release);
|
||||
@@ -3460,7 +3518,7 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
|
||||
if (d.lookahead != nullptr) {
|
||||
if (d.lookahead->stats_on()) {
|
||||
if (d.layers == 0) d.lookahead->stats_window_start();
|
||||
d.lookahead->stats_score(d.layers, ids, n_tok * k, d.host_res);
|
||||
d.lookahead->stats_score(d.layers, ids, n_tok * k, d.host_res, d.pcie_num);
|
||||
}
|
||||
d.lookahead->submit(d.layers, x_f, n_tok, d.host_res); // CS-T: warm layer + 1
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user