Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

route-resident: one warp per token in the swap kernel

Ethan33fd01b4133f
diff --git a/src/kernels/cuda/native_router.cu b/src/kernels/cuda/native_router.cu
index c89b6738c..526b29c70 100644
--- a/src/kernels/cuda/native_router.cu
+++ b/src/kernels/cuda/native_router.cu
@@ -182,49 +182,73 @@ void native_router_top10_multi(const float* logits, int32_t* ids, float* weights
// 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.
+//
+// One warp per token. This used to be one THREAD per token scanning all 512 experts (4 ranks x 512 x 12 scalar
+// ops on up to 6 threads): 45 us per layer, 2.2 ms of a 19.4 ms window, which is exactly what the option saved in
+// PCIe experts (~2.4 ms) - the swap cost as much as the misses it removed. The ranks are still visited in order
+// (a swap changes the set already picked), but the scan is coalesced (lane j reads f = j, j + 32, ...) and reduced
+// within the warp. The bits are unchanged: a strictly greater logit wins, the smallest expert index breaks ties,
+// and the softmax is the same expression.
__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;
+ const int t = blockIdx.x;
if (t >= n_tok) return;
+ const int lane = (int) threadIdx.x;
const float* l = logits + (size_t) t * 512;
int32_t* id = ids + (size_t) t * 10;
+ int32_t my[10];
+ for (int r = 0; r < 10; ++r) my[r] = id[r];
int swaps = 0, tail = 0, before = 0;
- for (int r = 0; r < 10; ++r) before += res[id[r]] < 0;
+ if (lane == 0)
+ for (int r = 0; r < 10; ++r) before += res[my[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;
+ const int e = my[r];
+ if (res[e] >= 0) continue; // already resident: uniform across the warp
+ if (lane == 0) ++tail;
+ float bv = -INFINITY;
+ int bi = -1;
+ for (int f = lane; f < 512; f += 32) {
+ if (res[f] < 0) continue;
bool used = false;
- for (int q = 0; q < 10; ++q) used |= id[q] == f;
- if (!used) { best = f; bl = l[f]; }
+ for (int q = 0; q < 10; ++q) used |= my[q] == f;
+ if (used) continue;
+ if (l[f] > bv) { bv = l[f]; bi = f; }
+ }
+ #pragma unroll
+ for (int off = 16; off > 0; off >>= 1) {
+ const float ov = __shfl_down_sync(0xffffffffu, bv, off);
+ const int oi = __shfl_down_sync(0xffffffffu, bi, off);
+ if (ov > bv || (ov == bv && oi >= 0 && (bi < 0 || oi < bi))) { bv = ov; bi = oi; }
}
- if (best >= 0 && l[e] - bl <= margin) { id[r] = best; ++swaps; }
+ if (lane == 0 && bi >= 0 && l[e] - bv <= margin) { my[r] = bi; ++swaps; }
+ my[r] = __shfl_sync(0xffffffffu, my[r], 0); // all lanes keep the same picked set
}
int after = 0;
- for (int r = 0; r < 10; ++r) after += res[id[r]] < 0;
- if (swaps) {
+ if (lane == 0)
+ for (int r = 0; r < 10; ++r) after += res[my[r]] < 0;
+ const int sw = __shfl_sync(0xffffffffu, swaps, 0);
+ if (sw) {
float m = -INFINITY;
- for (int r = 0; r < 10; ++r) m = fmaxf(m, l[id[r]]);
+ for (int r = 0; r < 10; ++r) m = fmaxf(m, l[my[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) { ex[r] = expf(l[my[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);
+ if (lane == 0) {
+ for (int r = 0; r < 10; ++r) id[r] = my[r];
+ if (stats) {
+ atomicAdd(stats + 0, (unsigned long long) tail);
+ atomicAdd(stats + 1, (unsigned long long) sw);
+ 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)>>>(
+ route_resident_k<<<(unsigned) n_tok, 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));