| 3699 | } |
| 3700 | |
| 3701 | void RouterLookahead::run() { |
| 3702 | std::vector<float> logits((size_t) (8 * n_expert_)); |
| 3703 | std::vector<int32_t> order((size_t) n_expert_); |
| 3704 | std::vector<int64_t> want; |
| 3705 | std::vector<float> wconf; // 149-prefetch: per wanted expert, its best rank's confidence (warm_scored) |
| 3706 | for (;;) { |
| 3707 | int64_t layer, nt; |
| 3708 | const int32_t* host_res; // res_: layer_'s row first (submit leaves it alone while busy_) |
| 3709 | ForesightSwap* fs; |
| 3710 | uint64_t sgen; |
| 3711 | { |
| 3712 | std::unique_lock<std::mutex> lk(mu_); |
| 3713 | cv_.wait(lk, [&] { return quit_ || pending_; }); |
| 3714 | if (quit_) return; |
| 3715 | pending_ = false; |
| 3716 | busy_ = true; |
| 3717 | layer = layer_; |
| 3718 | nt = n_tok_; |
| 3719 | work_x_.assign(x_.data(), x_.data() + (size_t) (nt * n_embd_)); |
| 3720 | if (has_res_) { |
| 3721 | work_res_ = res_; |
| 3722 | host_res = work_res_.data(); |
| 3723 | } else { |
| 3724 | host_res = nullptr; |
| 3725 | } |
| 3726 | fs = fs_; |
| 3727 | sgen = sub_gen_; |
| 3728 | } |
| 3729 | const auto t0 = std::chrono::steady_clock::now(); |
| 3730 | const int64_t layer0 = layer; |
| 3731 | const int depth = src_ != nullptr && !src_->read_ahead_open() ? 1 : depth_; // 149: the gate closed |
| 3732 | for (int dj = 0; dj < depth && layer0 + dj < (int64_t) routers_.size(); ++dj) { |
| 3733 | layer = layer0 + dj; |
| 3734 | want.clear(); |
| 3735 | wconf.clear(); |
| 3736 | const bool scored = src_->wants_scores(); |
| 3737 | // The router dot is AVX2 (kq_avx2.cpp); a CPU without AVX2 (the experimental older-CPU builds) takes the AVX1 |
| 3738 | // one (kq_avx1.cpp) or, without AVX, the same sums in plain C++. From the Strata_Dirigo fork (rwkeyes): an |
| 3739 | // AVX-only Xeon E5-2687W died here (vpmovzxwd) on its first request. An estimate only (which experts to |
| 3740 | // prefetch); the order of the additions differs between the three, the output does not depend on it. |
| 3741 | #if defined(__aarch64__) && !defined(STRATA_GPU_METAL) // Metal (Apple silicon) keeps the plain loop below: its bf16_rows_dot_multi is a stub |
| 3742 | strata::kernels::cpu::bf16_rows_dot_multi(routers_[(size_t) layer].data(), (int) n_expert_, (int) n_embd_, |
| 3743 | work_x_.data(), (int) nt, logits.data()); |
| 3744 | #else |
| 3745 | if (strata::kernels::cpu::cpu_avx2_ok()) { |
| 3746 | strata::kernels::cpu::bf16_rows_dot_multi(routers_[(size_t) layer].data(), (int) n_expert_, (int) n_embd_, |
| 3747 | work_x_.data(), (int) nt, logits.data()); |
| 3748 | } else if (strata::kernels::cpu::cpu_avx1_ok()) { |
| 3749 | strata::kernels::cpu::bf16_rows_dot_multi_avx1(routers_[(size_t) layer].data(), (int) n_expert_, |
| 3750 | (int) n_embd_, work_x_.data(), (int) nt, logits.data()); |
| 3751 | } else { |
| 3752 | // a mul and an add, not std::fma: without an FMA instruction that is a libm call per element (74x |
| 3753 | // slower on a Xeon E5-2665, measured by the fork) |
| 3754 | const uint16_t* rw = routers_[(size_t) layer].data(); |
| 3755 | for (int64_t r = 0; r < n_expert_; ++r) { |
| 3756 | const uint16_t* wr = rw + (size_t) r * (size_t) n_embd_; |
| 3757 | for (int64_t t = 0; t < nt; ++t) { |
| 3758 | const float* xr = work_x_.data() + (size_t) t * (size_t) n_embd_; |
no test coverage detected