diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp index 0dc35940..e73ce24a 100644 --- a/src/core/mtp.cpp +++ b/src/core/mtp.cpp @@ -896,7 +896,7 @@ bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) { } else { for (int t = 0; t < T; ++t) { bf16_gemv_fp32_mmvf(mixed_ + t * N, bf16("mlp.gate.weight"), logits_ + t * g.n_expert, (int) N, (int) g.n_expert, cs); - if (native_router_enabled()) native_router_top10(logits_ + t * g.n_expert, ids_ + t * K, w_ + t * K, cs); + if (native_router_enabled() && g.n_expert == 512 && K == 10) native_router_top10(logits_ + t * g.n_expert, ids_ + t * K, w_ + t * K, cs); else router_top10(logits_ + t * g.n_expert, 1, (int) g.n_expert, (int) K, ids_ + t * K, w_ + t * K, cs); } } diff --git a/src/kernels/native_multi_parity.cpp b/src/kernels/native_multi_parity.cpp index 6c88b50b..8c925ad9 100644 --- a/src/kernels/native_multi_parity.cpp +++ b/src/kernels/native_multi_parity.cpp @@ -10,9 +10,11 @@ // once: memcmp. #include "strata/kernels/native_moe.hpp" #include "strata/kernels/native_router.hpp" +#include "strata/kernels/router_top10.hpp" #include +#include #include #include #include @@ -91,6 +93,40 @@ int main(int argc, char**) { bad += mode_bad; } + // ---- #1357: a model that does not have 512 experts takes the generic router (the native one reads 512 floats a + // row, so mtp.cpp's per-token call is guarded by n_expert == 512 like its siblings): router_top10 on 256-wide + // rows picks the host reference's ids in order, with its renormalised softmax weights + { + const int n = 3, NE = 256, K = 10; + std::vector lg((size_t) n * NE); + for (auto& v : lg) v = gauss(rng) * 2.0f; + float* d_l = up(lg); + int32_t* d_i = nullptr; + float* d_w = nullptr; + check(cudaMalloc(&d_i, (size_t) n * K * 4), "i"); + check(cudaMalloc(&d_w, (size_t) n * K * 4), "w"); + router_top10(d_l, n, NE, K, d_i, d_w, st); + check(cudaDeviceSynchronize(), "sync"); + const auto gi = down(d_i, (size_t) n * K); + const auto gw = down(d_w, (size_t) n * K); + int rbad = 0; + for (int t = 0; t < n; ++t) { + std::vector ord(NE); + for (int i = 0; i < NE; ++i) ord[i] = i; + const float* row = lg.data() + (size_t) t * NE; + std::sort(ord.begin(), ord.end(), [&](int a, int b) { return row[a] != row[b] ? row[a] > row[b] : a < b; }); + double sum = 0; + for (int j = 0; j < K; ++j) sum += std::exp((double) row[ord[j]] - row[ord[0]]); + for (int j = 0; j < K; ++j) { + const double want = std::exp((double) row[ord[j]] - row[ord[0]]) / sum; + if (gi[(size_t) t * K + j] != ord[j] || std::fabs(gw[(size_t) t * K + j] - want) > 1e-5) ++rbad; + } + } + std::printf(" %-52s %s\n", "router_top10 on 256 experts (non-512 fallback)", rbad ? "*** NO ***" : "matches the host reference"); + bad += rbad; + cudaFree(d_l); cudaFree(d_i); cudaFree(d_w); + } + // ---- the combine struct Cfg { const char* name; int k; bool shared; int out_off; }; const Cfg cfgs[] = {{"k=10 + shared (float4 kernel)", 10, true, 0}, {"k=10 + shared, output misaligned", 10, true, 1},