| 723 | } |
| 724 | |
| 725 | void ExpertStream::begin_layer(int l, int T, int topk, const int32_t* ids, const float* pred, const float* pred2) { |
| 726 | const double t0 = now_ms(); |
| 727 | const bool prompt = T > 8; // a prompt chunk (the GEMM path): nearly every expert of the layer is used |
| 728 | mode_ = prompt ? 2 : 1; |
| 729 | if (l == 0) { if (prompt) ++p_chunks_; else ++windows_; } |
| 730 | ++stamp_; // the slots this layer uses are pinned until the next handshake |
| 731 | bool used[NE] = {}; |
| 732 | int want[NE]; |
| 733 | int nw = 0; |
| 734 | for (int i = 0; i < T * topk; ++i) { |
| 735 | const int e = ids[i]; |
| 736 | if (e < 0 || e >= NE || used[e]) continue; |
| 737 | used[e] = true; |
| 738 | want[nw++] = e; |
| 739 | } |
| 740 | pend_l_ = l; |
| 741 | pend_T_ = T; |
| 742 | pend_prompt_ = prompt; |
| 743 | pend_fake_ = fake_; |
| 744 | pend_pred_ = pred; |
| 745 | pend_pred2_ = pred2; |
| 746 | pend_wait_.clear(); |
| 747 | pend_any_miss_ = false; |
| 748 | pend_ahead_now_ = false; |
| 749 | if (fake_) { |
| 750 | for (int j = 0; j < nw; ++j) |
| 751 | if (where_[(size_t) (l * NE + want[j])] < 0) smap_.as<uint32_t>()[l * NE + want[j]] = code_[0]; |
| 752 | host_ms_ += now_ms() - t0; |
| 753 | return; |
| 754 | } |
| 755 | std::vector<int>& wait = pend_wait_; |
| 756 | bool& any_miss = pend_any_miss_; // the wait includes a read the demand path started itself (not only late read-aheads) |
| 757 | { |
| 758 | std::lock_guard<std::mutex> g(mu_); |
| 759 | // read-aheads of this layer (or older) that were not started and are not wanted: dropped (the slot goes free) |
| 760 | { |
| 761 | std::vector<int> drop; |
| 762 | for (const Job& j : pre_q_) { |
| 763 | if (j.layer > l || slots_[(size_t) j.slot].state.load() != 1) continue; |
| 764 | if (!(j.layer == l && used[j.e]) && std::find(drop.begin(), drop.end(), j.slot) == drop.end()) drop.push_back(j.slot); |
| 765 | } |
| 766 | for (int s : drop) cancel_slot(s); |
| 767 | } |
| 768 | for (int j = 0; j < nw; ++j) // pass 1: pin what is resident (or on its way), so no miss below evicts it |
| 769 | if (where_[(size_t) (l * NE + want[j])] >= 0) slots_[(size_t) where_[(size_t) (l * NE + want[j])]].pin = stamp_; |
| 770 | for (int j = 0; j < nw; ++j) { |
| 771 | const int e = want[j], key = l * NE + e; |
| 772 | uint64_t& lk = prompt ? p_lookups_ : lookups_; |
| 773 | ++lk; |
| 774 | // The byte hit rate (stream.h: byte_hit_rate) is what the arena is sized by: an expert's bytes are |
| 775 | // demanded whether it is resident or not, and counted as missed when they have to be read. |
| 776 | if (!prompt) d_bytes_ += expert_bytes(l); |
| 777 | int s = where_[(size_t) key]; |
| 778 | if (s >= 0) { |
| 779 | Slot& x = slots_[(size_t) s]; |
| 780 | x.pin = stamp_; |
| 781 | const int st = x.state.load(); |
| 782 | if (st == 0) { if (x.pre) ++(prompt ? p_pre_ : pre_hits_); else ++(prompt ? p_hits_ : hits_); } |