router lookahead: an AVX1 dot and a plain fallback for CPUs without AVX2 (Strata_Dirigo)

The routing-aware prefetch's router dot (bf16_rows_dot_multi) lives in kq_avx2.cpp, so an AVX-only
CPU started fine and died on the first request (SIGILL in vpmovzxwd on a Xeon E5-2687W). It now
takes the AVX2 kernel where the CPU has AVX2, kq_avx1.cpp's AVX dot (mul + add, 128-bit integer
widening) where it has AVX, and the same sums in plain C++ below that. cpu_avx1_ok() probes AVX +
OSXSAVE + the YMM state. An estimate only (which experts to prefetch). A CPU with AVX2 runs exactly
what it ran before.

From rwkeyes' Strata_Dirigo fork (MIT; 0d00a66, e0754d3), with MSVC's /arch:AVX added and
__builtin_memcpy replaced by std::memcpy for MSVC.
This commit is contained in:
rwkeyes authored and Niko1221 committed 2026-10-03 23:31:16 +02:00
1 parent 3f32e4b66a
commit 51a60af92a
6 files changed
+151 -3

No files matched your search

+30 -2
View File
@@ -10,6 +10,7 @@
#include "strata/kernels/elementwise.hpp"
#include "strata/kernels/quantize_act.hpp"
#include "strata/kernels/s2_expert_grouped.hpp"
#include "strata/kernels/cpu/kq_avx1.hpp"
#include "strata/kernels/cpu/kq_avx2.hpp"
#include <cuda_runtime.h>
@@ -1219,8 +1220,35 @@ void RouterLookahead::run() {
}
const auto t0 = std::chrono::steady_clock::now();
want.clear();
strata::kernels::cpu::bf16_rows_dot_multi(routers_[(size_t) layer].data(), (int) n_expert_, (int) n_embd_,
x_.data(), (int) nt, logits.data());
// The router dot is AVX2 (kq_avx2.cpp); a CPU without AVX2 (the experimental older-CPU builds) takes the AVX1
// one (kq_avx1.cpp) or, without AVX, the same sums in plain C++. From the Strata_Dirigo fork (rwkeyes): an
// AVX-only Xeon E5-2687W died here (vpmovzxwd) on its first request. An estimate only (which experts to
// prefetch); the order of the additions differs between the three, the output does not depend on it.
if (strata::kernels::cpu::cpu_avx2_ok()) {
strata::kernels::cpu::bf16_rows_dot_multi(routers_[(size_t) layer].data(), (int) n_expert_, (int) n_embd_,
x_.data(), (int) nt, logits.data());
} else if (strata::kernels::cpu::cpu_avx1_ok()) {
strata::kernels::cpu::bf16_rows_dot_multi_avx1(routers_[(size_t) layer].data(), (int) n_expert_,
(int) n_embd_, x_.data(), (int) nt, logits.data());
} else {
// a mul and an add, not std::fma: without an FMA instruction that is a libm call per element (74x
// slower on a Xeon E5-2665, measured by the fork)
const uint16_t* rw = routers_[(size_t) layer].data();
for (int64_t r = 0; r < n_expert_; ++r) {
const uint16_t* wr = rw + (size_t) r * (size_t) n_embd_;
for (int64_t t = 0; t < nt; ++t) {
const float* xr = x_.data() + (size_t) t * (size_t) n_embd_;
float acc = 0.0f;
for (int64_t c = 0; c < n_embd_; ++c) {
const uint32_t bits = (uint32_t) wr[c] << 16;
float wf;
std::memcpy(&wf, &bits, sizeof wf);
acc += wf * xr[c];
}
logits[(size_t) t * (size_t) n_expert_ + (size_t) r] = acc;
}
}
}
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;