mtp: guard the drafter's per-token native router call with n_expert == 512 and k == 10

The native router reads 512 logits a row; the window path two lines above already tested n_expert == 512 and K == 10, the
per-token fallback tested only native_router_enabled(), so a model with fewer experts read past each row. It now takes the
generic router_top10 like its siblings. native_multi_parity checks the generic router on 256-wide rows.

Reported-by: rwkeyes (#1357)
This commit is contained in:
Niko1221 committed 2026-10-07 16:16:11 +02:00
1 parent f7610d2c32
commit 6c0fca38ee
2 files changed
+37 -1

No files matched your search

+1 -1
View File
@@ -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);
}
}
+36
View File
@@ -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 <cuda_runtime.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
@@ -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<float> 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<int> 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},