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++

prefill: the peer's per-token sums in one launch per group instead of one per expert

Thigda0024fed170
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp
index 52d4122e8..34cc5f662 100644
--- a/include/strata/prefill/kernels.hpp
+++ b/include/strata/prefill/kernels.hpp
@@ -97,6 +97,11 @@ void moe_combine_peer(const float* D, const int32_t* slot, const float* w, const
/// (no token twice: launches in a fixed expert order make the sums repeatable). After eddoursul/Strata's
/// moe_scatter_add (f8de703).
void peer_scatter_add(float* sum, const float* rows, const float* wk, const int32_t* pair, int64_t n, void* stream);
+/// The same for a group of experts in one launch (a token may recur): token tok[b] adds its rows list[start[b] ..
+/// start[b + 1]) in that order (the experts' order), row r at rows[r - r0]: bitwise the per-expert launches'.
+/// eddoursul/Strata's moe_gather_add, with the weight looked up through the row's pair.
+void peer_gather_add(float* sum, const float* rows, int64_t r0, const float* wk, const int32_t* pair, const int32_t* tok,
+ const int32_t* start, const int32_t* list, int64_t n_tok, void* stream);
/// moe_combine_peer with the peer's sums in FP16 (`peer16`, may be mapped host memory)
void moe_combine_peer16(const float* D, const int32_t* slot, const float* w, const float* shared, const float* sg,
const uint16_t* peer16, int64_t rows_local, float* bo, int64_t T, void* stream);
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index c091dbd2f..3a5f28182 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -1743,6 +1743,21 @@ __global__ void moe_combine_peer16_kernel(const float* __restrict__ Dm, const in
}
bo[i] = (s + __half2float(__ushort_as_half(peer16[i]))) + shared[i] * sigm(sg[t]);
}
+__global__ void peer_gather_add_kernel(float* __restrict__ sum, const float* __restrict__ rows, int64_t r0,
+ const float* __restrict__ wk, const int32_t* __restrict__ pair,
+ const int32_t* __restrict__ tok, const int32_t* __restrict__ start,
+ const int32_t* __restrict__ list) {
+ const int64_t d = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
+ if (d >= N) return;
+ const int b = blockIdx.y;
+ float* o = sum + (int64_t) tok[b] * N + d;
+ float s = *o;
+ for (int i = start[b]; i < start[b + 1]; ++i) {
+ const int r = list[i];
+ s = fmaf(wk[pair[r]], rows[(r - r0) * N + d], s);
+ }
+ *o = s;
+}
__global__ void sums_to_f16_kernel(const float* __restrict__ x, uint16_t* __restrict__ y, int64_t n) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) y[i] = hf_sat(x[i]);
@@ -1763,6 +1778,13 @@ void moe_combine_peer16(const float* Dm, const int32_t* slot, const float* w, co
rows_local, bo, T);
check("moe_combine_peer16");
}
+void peer_gather_add(float* sum, const float* rows, int64_t r0, const float* wk, const int32_t* pair, const int32_t* tok,
+ const int32_t* start, const int32_t* list, int64_t n_tok, void* stream) {
+ if (n_tok <= 0) return;
+ peer_gather_add_kernel<<<dim3((unsigned) ((N + 255) / 256), (unsigned) n_tok), 256, 0, (cudaStream_t) stream>>>(
+ sum, rows, r0, wk, pair, tok, start, list);
+ check("peer_gather_add");
+}
void sums_to_f16(const float* x, uint16_t* y, int64_t n, void* stream) {
if (n <= 0) return;
sums_to_f16_kernel<<<blocks_for(n), 256, 0, (cudaStream_t) stream>>>(x, y, n);
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 72320199f..fae3863f9 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -566,6 +566,13 @@ struct PeerPrefill {
// adds it to its own experts' rows (eddoursul/Strata's second-GPU prompt path, f8de703).
bool sums = false;
bool f16 = false; // the sums mode's transfers in FP16 (STRATA_PF_PEER_F16=0: FP32)
+ // the sums in one launch per group (STRATA_PF_PEER_GATHER=0: per expert): per group [tokens | starts | rows] at
+ // adds_at[g], built on the host from the rows' pairs, uploaded once per layer
+ bool gather = false;
+ int32_t *adds = nullptr, *host_adds = nullptr;
+ size_t adds_cap = 0;
+ std::vector<size_t> adds_at;
+ std::vector<int32_t> ntok, tcur, touched;
uint16_t *sum16 = nullptr, *host_x16 = nullptr, *host_sum16 = nullptr;
float *sum = nullptr, *wk = nullptr;
int32_t* pair = nullptr;
@@ -603,6 +610,7 @@ struct PeerPrefill {
if (host_pair) cudaFreeHost(host_pair);
if (host_x16) cudaFreeHost(host_x16);
if (host_sum16) cudaFreeHost(host_sum16);
+ if (host_adds) cudaFreeHost(host_adds);
}
};
@@ -1544,6 +1552,13 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e
const char* v = std::getenv("STRATA_PF_PEER_F16");
pp->f16 = (v == nullptr || std::atoi(v) != 0) && N % 8 == 0;
if (pp->f16) pp->sum16 = (uint16_t*) take((size_t) m.T_max * N * 2);
+ const char* gv = std::getenv("STRATA_PF_PEER_GATHER");
+ pp->gather = gv == nullptr || std::atoi(gv) != 0;
+ if (pp->gather) { // tokens and starts (<= 2 rows + 1 per group) and rows: <= 3 x the rows + 2 per expert
+ pp->adds_cap = 3 * (size_t) Rt + 2 * NE + 64;
+ pp->adds = (int32_t*) take(pp->adds_cap * 4);
+ pp->tcur.assign((size_t) m.T_max, 0);
+ }
pp->sum = (float*) take((size_t) m.T_max * N * 4);
pp->wk = (float*) take((size_t) m.T_max * K * 4);
pp->pair = (int32_t*) take((size_t) Rt * 4);
@@ -1578,6 +1593,8 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e
if (ok && pp->f16) host(pp->host_sum16, sum16_cap, (size_t) m.T_max * N);
else if (ok) host(pp->host_sum, sum_cap, (size_t) m.T_max * N);
if (ok) host(pp->host_pair, pair_cap, (size_t) pp->cap_rows);
+ size_t adds_cap = 0;
+ if (ok && pp->gather) host(pp->host_adds, adds_cap, pp->adds_cap);
} else if (ok) {
host(pp->host_rows, pp->host_rows_cap, (size_t) pp->cap_rows * N);
}
@@ -3057,6 +3074,41 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
std::memcpy(P.host_bounds, P.bounds_host.data(), P.bounds_host.size() * 4);
copy_i32(P.bounds, P.host_bounds, (int64_t) P.bounds_host.size(), ps);
}
+ if (P.sums && P.gather) { // each group's tokens, their row lists in the experts' order
+ P.adds_at.resize(ng);
+ P.ntok.resize(ng);
+ size_t at = 0;
+ for (size_t g2 = 0; g2 < ng; ++g2) {
+ const int32_t r0 = P.bounds_host[P.groups[g2].first], r1 = P.bounds_host[P.groups[g2].second];
+ P.touched.clear();
+ for (int32_t r = r0; r < r1; ++r) {
+ const int32_t t = P.host_pair[r] / (int32_t) K;
+ if (P.tcur[(size_t) t]++ == 0) P.touched.push_back(t);
+ }
+ const size_t nt = P.touched.size();
+ int32_t* a = P.host_adds + at; // [tokens nt | starts nt + 1 | rows]
+ a[nt] = 0;
+ for (size_t i = 0; i < nt; ++i) {
+ const int32_t t = P.touched[i];
+ a[i] = t;
+ a[nt + 1 + i] = a[nt + i] + P.tcur[(size_t) t];
+ P.tcur[(size_t) t] = a[nt + i]; // the fill cursor
+ }
+ int32_t* list = a + 2 * nt + 1;
+ for (int32_t r = r0; r < r1; ++r)
+ list[P.tcur[(size_t) (P.host_pair[r] / (int32_t) K)]++] = r;
+ for (int32_t t : P.touched) P.tcur[(size_t) t] = 0;
+ P.adds_at[g2] = at;
+ P.ntok[g2] = (int32_t) nt;
+ at += 2 * nt + 1 + (size_t) (r1 - r0);
+ }
+ if (at > P.adds_cap) {
+ err = "prefill: the peer's group tables overflow";
+ cudaSetDevice(prevd);
+ return false;
+ }
+ copy_i32(P.adds, P.host_adds, (int64_t) at, ps);
+ }
pe.mark(kPeMoeGemm, ps);
const auto& f = lay.fmt[(size_t) l];
for (size_t g2 = 0; g2 < ng; ++g2) {
@@ -3115,6 +3167,12 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
dn.ids = P.ident; dn.total_rows = nr; dn.max_rows = maxr; dn.dst = P.Dm_b[b];
dn.ld_dst = N;
P.run_ctx->run(dn, ps);
+ if (P.sums && P.gather) { // into the per-token sums, one launch for the group
+ const int32_t* a = P.adds + P.adds_at[g2];
+ const int64_t nt = P.ntok[g2];
+ peer_gather_add(P.sum, P.Dm_b[b], r0, P.wk, P.pair, a, a + nt, a + 2 * nt + 1, nt, ps);
+ continue;
+ }
if (P.sums) { // into the per-token sums on this stream: Dm_b[b] is free after it
for (size_t j = j0; j < j1; ++j)
peer_scatter_add(P.sum, P.Dm_b[b] + (size_t) (P.bounds_host[j] - r0) * N, P.wk,