Experimental STRATA_ROUTE_RESIDENT: residency-biased routing in verify windows (opt-in, changes the output)

This commit is contained in:
Niko1221 committed 2026-10-08 07:54:09 +02:00
1 parent d5ea713374
commit 5df35dcb47
3 files changed
+100

No files matched your search

+4
View File
@@ -16,4 +16,8 @@ bool native_router_enabled();
void native_router_top10(const float* logits, int32_t* ids, float* weights, void* stream);
/// n_tok rows at once (logits [n,512], ids/weights [n,10]); each row exactly as the single call.
void native_router_top10_multi(const float* logits, int32_t* ids, float* weights, int n_tok, void* stream);
/// STRATA_ROUTE_RESIDENT: swap non-resident tail-rank experts for resident ones within `margin` logits (see the .cu).
/// `stats` is nullptr or 4 device uint64 counters: tail entries seen, swaps, non-resident before, non-resident after.
void native_route_resident(const float* logits, int32_t* ids, float* weights, const int32_t* res_layer, int n_tok, float margin,
int rank_lo, int rank_hi, unsigned long long* stats, void* stream);
}
+43
View File
@@ -64,6 +64,40 @@ using Clock = std::chrono::steady_clock;
double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }
const bool g_dbg = std::getenv("STRATA_VERIFY_DEBUG") != nullptr;
inline bool g_lfuse() { static const bool on = [] { const char* v = std::getenv("STRATA_LFUSE"); return v != nullptr && v[0] == '1'; }(); return on; }
// STRATA_ROUTE_RESIDENT=<margin logits> (+ STRATA_ROUTE_RESIDENT_RANKS=lo-hi, default 6-9): residency-biased routing.
struct RouteResidentCfg;
RouteResidentCfg& route_resident_cfg();
struct RouteResidentCfg {
float margin = 0.0f;
int lo = 6, hi = 9;
unsigned long long* d_stats = nullptr;
unsigned long long* stats() {
if (d_stats == nullptr && margin > 0.0f) {
cudaMalloc((void**) &d_stats, 8 * sizeof(unsigned long long));
cudaMemset(d_stats, 0, 8 * sizeof(unsigned long long));
std::atexit([] {
unsigned long long h[8] = {};
RouteResidentCfg& c = route_resident_cfg();
cudaDeviceSynchronize();
cudaMemcpy(h, c.d_stats, sizeof(h), cudaMemcpyDeviceToHost);
std::fprintf(stderr, "route-resident: margin=%g ranks=%d-%d windows T>8 (prompt reads): tail_nonres=%llu swaps=%llu (%.1f%%) nonres_entries before=%llu after=%llu\n",
c.margin, c.lo, c.hi, h[0], h[1], h[0] ? 100.0 * h[1] / h[0] : 0.0, h[2], h[3]);
std::fprintf(stderr, "route-resident: margin=%g ranks=%d-%d windows T<=8 (decode): tail_nonres=%llu swaps=%llu (%.1f%%) nonres_entries before=%llu after=%llu\n",
c.margin, c.lo, c.hi, h[4], h[5], h[4] ? 100.0 * h[5] / h[4] : 0.0, h[6], h[7]);
});
}
return d_stats;
}
};
RouteResidentCfg& route_resident_cfg() {
static RouteResidentCfg c = [] {
RouteResidentCfg r;
if (const char* v = std::getenv("STRATA_ROUTE_RESIDENT")) r.margin = (float) std::atof(v);
if (const char* v = std::getenv("STRATA_ROUTE_RESIDENT_RANKS")) std::sscanf(v, "%d-%d", &r.lo, &r.hi);
return r;
}();
return c;
}
inline bool g_lfuse_gate() { static const bool on = [] { const char* v = std::getenv("STRATA_LFUSE_GATE"); return v == nullptr || v[0] != '0'; }(); return on; }
inline bool g_lfuse_pair() { static const bool on = [] { const char* v = std::getenv("STRATA_LFUSE_PAIR"); return v == nullptr || v[0] != '0'; }(); return on; }
inline bool g_qdedup() { static const bool on = [] { const char* v = std::getenv("STRATA_VERIFY_QDEDUP"); return v != nullptr && std::atoi(v) != 0; }(); return on; }
@@ -378,6 +412,7 @@ Verifier::~Verifier() {
bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& ss, const VerifyHits& hits,
const NativeHead* head, int max_t, std::string& err) {
g_diag_verifier.store(this);
(void) route_resident_cfg().stats(); // STRATA_ROUTE_RESIDENT: the counters are allocated outside graph capture
diag_verify_fn().store(&diag_active_verifier);
for (auto& slot : g_live) {
Verifier* none = nullptr;
@@ -1282,6 +1317,14 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
mb.logits = logits_ + t * NE; mb.ids = ids_ + t * K; mb.weights = w_ + t * K;
if (!moe_route(wt, g, l, K, mb, mixed_ + t * N, cs, err, nullptr)) return false;
}
if (route_resident_cfg().margin > 0.0f && NE == 512 && K == 10 && hits_.d_res != nullptr) {
// STRATA_ROUTE_RESIDENT: after the router, before the plan/doorbell read ids_/w_ (opt-in, changes the output)
try {
native_route_resident(logits_ + tb * NE, ids_ + tb * K, w_ + tb * K, hits_.d_res + l * g.n_expert, n,
route_resident_cfg().margin, route_resident_cfg().lo, route_resident_cfg().hi,
route_resident_cfg().stats() + (n <= 8 ? 4 : 0), cs);
} catch (const std::exception& e) { err = "verify route-resident: " + std::string(e.what()); return false; }
}
if (ar_on()) {
resident_plan(ids_ + tb * K, n * (int) K, (int) K, hits_.d_res + l * g.n_expert, (int) g.n_expert,
hits_.cache_base, slot_off_d_, (long long) hits_.blob,
+53
View File
@@ -176,4 +176,57 @@ void native_router_top10_multi(const float* logits, int32_t* ids, float* weights
const auto error = cudaGetLastError();
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
}
// STRATA_ROUTE_RESIDENT (EXPERIMENTAL, opt-in, changes the output): residency-biased routing for 512 x 10.
// After the top-10: for the ranks lo..hi a non-resident expert is replaced by the best resident expert not already
// picked when its logit is within `margin` of the replaced one; the weights are then the softmax of the selected
// logits (the router's renormalisation). Tokens without a swap keep the router's exact bits.
// stats (device, 4 x uint64): [0] tail entries seen, [1] swaps, [2] non-resident entries before, [3] after.
__global__ void route_resident_k(const float* __restrict__ logits, int32_t* __restrict__ ids, float* __restrict__ weights,
const int32_t* __restrict__ res, int n_tok, float margin, int lo, int hi,
unsigned long long* __restrict__ stats) {
const int t = blockIdx.x * blockDim.x + threadIdx.x;
if (t >= n_tok) return;
const float* l = logits + (size_t) t * 512;
int32_t* id = ids + (size_t) t * 10;
int swaps = 0, tail = 0, before = 0;
for (int r = 0; r < 10; ++r) before += res[id[r]] < 0;
for (int r = lo; r <= hi && r < 10; ++r) {
const int e = id[r];
if (res[e] >= 0) continue;
++tail;
int best = -1;
float bl = -INFINITY;
for (int f = 0; f < 512; ++f) {
if (res[f] < 0 || l[f] <= bl) continue;
bool used = false;
for (int q = 0; q < 10; ++q) used |= id[q] == f;
if (!used) { best = f; bl = l[f]; }
}
if (best >= 0 && l[e] - bl <= margin) { id[r] = best; ++swaps; }
}
int after = 0;
for (int r = 0; r < 10; ++r) after += res[id[r]] < 0;
if (swaps) {
float m = -INFINITY;
for (int r = 0; r < 10; ++r) m = fmaxf(m, l[id[r]]);
float ex[10], sum = 0.0f;
for (int r = 0; r < 10; ++r) { ex[r] = expf(l[id[r]] - m); sum += ex[r]; }
for (int r = 0; r < 10; ++r) weights[(size_t) t * 10 + r] = ex[r] / sum;
}
if (stats) {
atomicAdd(stats + 0, (unsigned long long) tail);
atomicAdd(stats + 1, (unsigned long long) swaps);
atomicAdd(stats + 2, (unsigned long long) before);
atomicAdd(stats + 3, (unsigned long long) after);
}
}
void native_route_resident(const float* logits, int32_t* ids, float* weights, const int32_t* res_layer, int n_tok, float margin,
int rank_lo, int rank_hi, unsigned long long* stats, void* stream) {
if (!stream || n_tok < 1 || !res_layer) throw std::invalid_argument("native_route_resident: bad arguments");
route_resident_k<<<(unsigned) ((n_tok + 31) / 32), 32, 0, static_cast<cudaStream_t>(stream)>>>(
logits, ids, weights, res_layer, n_tok, margin, rank_lo, rank_hi, stats);
const auto error = cudaGetLastError();
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
}
}