okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142b-cuda into f-14254d7f7d7c

main
3.3 KiB79 linesraw
1// src/kernels/cuda/route_prior.cu - STRATA_ROUTE_PRIOR=lambda: cache-conditioned routing (opt-in, changes the output).
2//
3// "Mixture of Cache-Conditional Experts" (arXiv 2412.00099), without training: when a token's 10 experts are picked,
4// an expert already in VRAM gets a `lambda` bonus on its logit.  The SELECTION uses the biased logits; the WEIGHTS stay
5// the router's (softmax over the 512 unbiased logits), renormalised over the 10 picked like the native router.  Fewer
6// missed experts to copy over PCIe or compute on the CPU.  Unlike STRATA_ROUTE_RESIDENT (a missed tail expert swapped
7// for the best resident one within a logit margin), every rank can move, by an amount the logits decide.
8#include "strata/kernels/route_prior.hpp"
9
10#include <cuda_runtime.h>
11
12#include <cfloat>
13#include <cmath>
14
15namespace strata::kernels {
16namespace {
17
18__device__ __forceinline__ float wmax(float v) {
19#pragma unroll
20    for (int m = 16; m; m >>= 1) v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, m, 32));
21    return v;
22}
23__device__ __forceinline__ float wsum(float v) {
24#pragma unroll
25    for (int m = 16; m; m >>= 1) v += __shfl_xor_sync(0xffffffffu, v, m, 32);
26    return v;
27}
28
29// one warp per token; 512 experts = 16 values per lane (as the native router)
30__global__ void route_prior_k(const float* __restrict__ logits, const int32_t* __restrict__ res, float lambda,
31                                  int32_t* __restrict__ ids, float* __restrict__ weights) {
32    const float* lg = logits + (size_t) blockIdx.x * 512;
33    int32_t* id = ids + (size_t) blockIdx.x * 10;
34    float* w = weights + (size_t) blockIdx.x * 10;
35    const int lane = threadIdx.x;
36    float p[16], s[16];
37    float mx = -INFINITY;
38#pragma unroll
39    for (int i = 0; i < 16; ++i) { p[i] = lg[lane + 32 * i]; mx = fmaxf(mx, p[i]); }
40    mx = wmax(mx);
41    float sum = 0.0f;
42#pragma unroll
43    for (int i = 0; i < 16; ++i) {
44        const int e = lane + 32 * i;
45        s[i] = p[i] + (res[e] >= 0 ? lambda : 0.0f);      // the selection score
46        p[i] = expf(p[i] - mx);                            // the router's probability (unnormalised)
47        sum += p[i];
48    }
49    (void) wsum(sum);
50    float sel_p = 0.0f, sel_sum = 0.0f;
51    for (int rank = 0; rank < 10; ++rank) {
52        float best = -FLT_MAX, bp = 0.0f;
53        int be = 1 << 30;
54#pragma unroll
55        for (int i = 0; i < 16; ++i)
56            if (s[i] > best) { best = s[i]; be = lane + 32 * i; bp = p[i]; }
57#pragma unroll
58        for (int m = 16; m; m >>= 1) {
59            const float ob = __shfl_xor_sync(0xffffffffu, best, m, 32);
60            const int oe = __shfl_xor_sync(0xffffffffu, be, m, 32);
61            const float op = __shfl_xor_sync(0xffffffffu, bp, m, 32);
62            if (ob > best || (ob == best && oe < be)) { best = ob; be = oe; bp = op; }
63        }
64        if ((be & 31) == lane) s[be >> 5] = -FLT_MAX;
65        if (lane == rank) { id[rank] = be; sel_p = bp; }
66        sel_sum += bp;                                      // the same on every lane
67    }
68    if (lane < 10) w[lane] = sel_p / fmaxf(sel_sum, 1e-30f);
69}
70
71}  // namespace
72
73void route_prior_top10(const float* logits, const int32_t* res_layer, float lambda, int32_t* ids, float* weights,
74                             int n_tok, void* stream) {
75    if (n_tok <= 0) return;
76    route_prior_k<<<n_tok, 32, 0, (cudaStream_t) stream>>>(logits, res_layer, lambda, ids, weights);
77}
78
79}  // namespace strata::kernels