diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp index f7a058c1..c7f67315 100644 --- a/include/strata/prefill/kernels.hpp +++ b/include/strata/prefill/kernels.hpp @@ -88,6 +88,15 @@ void gather_rows16(const uint16_t* x16, const int32_t* src, uint16_t* dst16, int /// bo[t, :] = shared[t, :] * sigmoid(sg[t]) + sum_k w[t, k] * D[slot[t, k], :] void moe_combine(const float* D, const int32_t* slot, const float* w, const float* shared, const float* sg, float* bo, int64_t T, void* stream); +/// --peer-device's prompt share as sums: bo[t, :] = (sum over the k with slot[t, k] < rows_local of w[t, k] * +/// D[slot[t, k], :], in k order) + peer[t, :] + shared[t, :] * sigmoid(sg[t]). `peer` (the peer's per-token sums) +/// may be mapped host memory. +void moe_combine_peer(const float* D, const int32_t* slot, const float* w, const float* shared, const float* sg, + const float* peer, int64_t rows_local, float* bo, int64_t T, void* stream); +/// sum[t, :] += wk[p] * rows[r, :] with p = pair[r], t = p / 10 (the routed pair's token), for n rows of ONE expert +/// (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); // ---- QSA helpers /// In place: x[r, :] = x[r, :] * rsqrt(mean x^2 + eps) * w over rows of `cols` (row stride `ld`). diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 3f14fb16..baebfd54 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -1703,6 +1703,43 @@ void gather_rows16(const uint16_t* x16, const int32_t* src, uint16_t* dst16, int gather_rows16_kernel<<>>(x16, src, dst16, n, width); check("gather_rows16"); } +namespace { +__global__ void moe_combine_peer_kernel(const float* __restrict__ Dm, const int32_t* __restrict__ slot, + const float* __restrict__ w, const float* __restrict__ shared, + const float* __restrict__ sg, const float* __restrict__ peer, int64_t rows_local, + float* __restrict__ bo, int64_t T) { + const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; + if (i >= T * N) return; + const int64_t t = i / N, d = i % N; + float s = 0.0f; +#pragma unroll + for (int k = 0; k < 10; ++k) { + const int32_t r = slot[t * 10 + k]; + if (r < rows_local) s = fmaf(w[t * 10 + k], Dm[(int64_t) r * N + d], s); + } + bo[i] = (s + peer[i]) + shared[i] * sigm(sg[t]); +} +__global__ void peer_scatter_add_kernel(float* __restrict__ sum, const float* __restrict__ rows, + const float* __restrict__ wk, const int32_t* __restrict__ pair, int64_t n) { + const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n * N) return; + const int64_t r = i / N, d = i % N; + const int32_t p = pair[r]; + float* o = sum + (int64_t) (p / 10) * N + d; + *o = fmaf(wk[p], rows[i], *o); +} +} // namespace +void moe_combine_peer(const float* Dm, const int32_t* slot, const float* w, const float* shared, const float* sg, + const float* peer, int64_t rows_local, float* bo, int64_t T, void* stream) { + moe_combine_peer_kernel<<>>(Dm, slot, w, shared, sg, peer, rows_local, + bo, T); + check("moe_combine_peer"); +} +void peer_scatter_add(float* sum, const float* rows, const float* wk, const int32_t* pair, int64_t n, void* stream) { + if (n <= 0) return; + peer_scatter_add_kernel<<>>(sum, rows, wk, pair, n); + check("peer_scatter_add"); +} void moe_combine(const float* Dm, const int32_t* slot, const float* w, const float* shared, const float* sg, float* bo, int64_t T, void* stream) { #ifndef STRATA_W_NO_COMB diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 6105b935..8ab2a5c8 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -560,6 +560,15 @@ struct PeerPrefill { size_t host_x_cap = 0, host_rows_cap = 0, host_src_cap = 0; // elements static constexpr size_t kHostBounds = 2 * (NE + NE / 16 + 2) + 64; int64_t back_at = 0, back_rows = 0; + // SUMS (the host route's default, STRATA_PF_PEER_SUMS=0: rows): the peer adds its rows into one weighted sum per + // token (`sum`, from the routing weights `wk` and each row's routed pair `pair`) and sends that back (`host_sum`, + // T x N) instead of its rows (rows_peer x N, up to K times more over the primary's link); the primary's combine + // adds it to its own experts' rows (eddoursul/Strata's second-GPU prompt path, f8de703). + bool sums = false; + float *sum = nullptr, *wk = nullptr; + int32_t* pair = nullptr; + float *host_w = nullptr, *host_sum = nullptr; + int32_t* host_pair = nullptr; std::vector bounds_host; std::vector owned; int64_t layers = 0, experts = 0, rows = 0, over_cap = 0; // stats @@ -587,6 +596,9 @@ struct PeerPrefill { if (host_rows) cudaFreeHost(host_rows); if (host_src) cudaFreeHost(host_src); if (host_bounds) cudaFreeHost(host_bounds); + if (host_w) cudaFreeHost(host_w); + if (host_sum) cudaFreeHost(host_sum); + if (host_pair) cudaFreeHost(host_pair); } }; @@ -1520,6 +1532,15 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e pp->Dm = (float*) take((size_t) R * N * 4); } const int64_t Rt = pp->cap_rows; + if (!pp->p2p && pp->compact) { + const char* v = std::getenv("STRATA_PF_PEER_SUMS"); + pp->sums = v == nullptr || std::atoi(v) != 0; + } + if (pp->sums) { + 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); + } pp->src = (int32_t*) take((size_t) Rt * 4); pp->ident = (int32_t*) take((size_t) Rt * 4); pp->bounds = (int32_t*) take((size_t) (2 * (NE + NE / MMQ_GROUP + 2)) * 4); @@ -1541,9 +1562,15 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e } cap = n; }; - size_t bounds_cap = 0; + size_t bounds_cap = 0, w_cap = 0, sum_cap = 0, pair_cap = 0; host(pp->host_x, pp->host_x_cap, (size_t) m.T_max * N); - if (ok) host(pp->host_rows, pp->host_rows_cap, (size_t) pp->cap_rows * N); + if (ok && pp->sums) { + host(pp->host_w, w_cap, (size_t) m.T_max * K); + 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); + } else if (ok) { + host(pp->host_rows, pp->host_rows_cap, (size_t) pp->cap_rows * N); + } if (ok) host(pp->host_src, pp->host_src_cap, (size_t) pp->cap_rows); if (ok) host(pp->host_bounds, bounds_cap, PeerPrefill::kHostBounds); if (!ok) { @@ -1559,7 +1586,8 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e if (!ok) { err = "prefill peer: the peer's buffers do not fit (raise --peer-reserve-mib or lower --peer-prefill-rows)"; return false; } std::fprintf(stderr, "strata prefill: peer GPU %d computes its experts' rows of each prompt chunk (up to %lld rows per " "layer%s%s); %zu MiB left free on it\n", pp->dev, (long long) pp->cap_rows, - pp->p2p ? "" : ", no P2P: through mapped host memory", + pp->p2p ? "" : (pp->sums ? ", no P2P: through mapped host memory, one weighted sum per token back" + : ", no P2P: through mapped host memory"), pp->compact ? (pp->ps_frac > 0.0 ? (", compact group buffers, streams " + std::to_string((int) (pp->ps_frac * 100 + 0.5)) + "% of the primary's streamed experts through a " + std::to_string(pp->RP) + "-slot ring").c_str() : ", compact group buffers") : "", fb >> 20); @@ -2958,6 +2986,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri if (peer_now) { PeerPrefill& P = *m.pp; if (!P.p2p) cudaMemcpyAsync(P.host_x, m.mixed, (size_t) T * N * 4, cudaMemcpyDeviceToHost, m.cs); + if (P.sums) { // the routing weights, and each peer row's routed pair (t * K + k) + cudaMemcpyAsync(P.host_w, m.w, (size_t) T * K * 4, cudaMemcpyDeviceToHost, m.cs); + for (int64_t i = 0; i < T * K; ++i) + if (const int32_t r = slot_h[(size_t) i]; r >= rows_local) P.host_pair[r - rows_local] = (int32_t) i; + } cudaEventRecord(P.ev_in, m.cs); int prevd = 0; cudaGetDevice(&prevd); @@ -2978,6 +3011,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri copy_i32(P.src, P.host_src, rows_peer, ps); } const size_t n = order_peer.size(); + if (P.sums) { + copy_f32_wide(P.wk, P.host_w, T * K, ps); + copy_i32(P.pair, P.host_pair, rows_peer, ps); + cudaMemsetAsync(P.sum, 0, (size_t) T * N * 4, ps); + } if (P.compact) { // groups of up to MMQ_GROUP experts and at most G rows, each computed from its own rows P.groups.clear(); @@ -3064,6 +3102,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) { // 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, + P.pair + P.bounds_host[j], P.bounds_host[j + 1] - P.bounds_host[j], ps); + continue; + } cudaEventRecord(P.ev_grp[g2 % PeerPrefill::kGrpEv], ps); cudaStreamWaitEvent(P.s_out, P.ev_grp[g2 % PeerPrefill::kGrpEv], 0); if (P.p2p) @@ -3132,7 +3176,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } } pe.mark(kPeMoeOut, ps); - if (P.out_pipe) { + if (P.sums) { // the sums go back: T x N + cudaMemcpyAsync(P.host_sum, P.sum, (size_t) T * N * 4, cudaMemcpyDeviceToHost, ps); + cudaEventRecord(P.ev_done, ps); + P.out_pending = true; + } else if (P.out_pipe) { cudaEventRecord(P.ev_done, P.s_out); P.out_pending = true; } else { @@ -3396,10 +3444,13 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } if (peer_now) { // multi-GPU: the peer's rows are in Dm (or, without P2P, in host memory) cudaStreamWaitEvent(m.cs, m.pp->ev_done, 0); - if (!m.pp->p2p) + if (!m.pp->p2p && !m.pp->sums) copy_f32_wide(m.Dm + (size_t) m.pp->back_at * N, m.pp->host_rows, m.pp->back_rows * N, m.cs); } - moe_combine(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.bo, T, m.cs); + if (peer_now && m.pp->sums) + moe_combine_peer(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.pp->host_sum, m.pp->back_at, m.bo, T, m.cs); + else + moe_combine(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.bo, T, m.cs); // debug: STRATA_DBG_NAN=1 reports the first layer of a chunk whose MoE produced non-finite values if (static const bool dbg = std::getenv("STRATA_DBG_NAN") != nullptr; dbg) { cudaStreamSynchronize(m.cs);