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 no-P2P sums mode's two transfers in FP16

Thig550159fc59bd
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp
index c7f673155..52d4122e8 100644
--- a/include/strata/prefill/kernels.hpp
+++ b/include/strata/prefill/kernels.hpp
@@ -97,6 +97,13 @@ 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);
+/// 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);
+/// y = FP16 bits of x, saturated at +-65504 (the peer's sums on their way back; eddoursul/Strata 2acfac4)
+void sums_to_f16(const float* x, uint16_t* y, int64_t n, void* stream);
+/// dst[0, n) = FP32 of the FP16 src[0, n) (n a multiple of 8, 16-byte aligned; src may be mapped host memory)
+void f16_to_f32_wide(float* dst, const uint16_t* src, 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 baebfd547..c091dbd2f 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -1728,7 +1728,52 @@ __global__ void peer_scatter_add_kernel(float* __restrict__ sum, const float* __
float* o = sum + (int64_t) (p / 10) * N + d;
*o = fmaf(wk[p], rows[i], *o);
}
+__global__ void moe_combine_peer16_kernel(const float* __restrict__ Dm, const int32_t* __restrict__ slot,
+ const float* __restrict__ w, const float* __restrict__ shared,
+ const float* __restrict__ sg, const uint16_t* __restrict__ peer16,
+ 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 + __half2float(__ushort_as_half(peer16[i]))) + shared[i] * sigm(sg[t]);
+}
+__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]);
+}
+__global__ void f16_to_f32_wide_kernel(float4* __restrict__ dst, const uint4* __restrict__ src, int64_t n8) {
+ const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; // eight values
+ if (i >= n8) return;
+ const uint4 v = src[i];
+ const __half2* h = reinterpret_cast<const __half2*>(&v);
+ const float2 a = __half22float2(h[0]), b = __half22float2(h[1]), c = __half22float2(h[2]), e = __half22float2(h[3]);
+ dst[2 * i] = make_float4(a.x, a.y, b.x, b.y);
+ dst[2 * i + 1] = make_float4(c.x, c.y, e.x, e.y);
+}
} // namespace
+void moe_combine_peer16(const float* Dm, 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) {
+ moe_combine_peer16_kernel<<<blocks_for(T * N), 256, 0, (cudaStream_t) stream>>>(Dm, slot, w, shared, sg, peer16,
+ rows_local, bo, T);
+ check("moe_combine_peer16");
+}
+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);
+ check("sums_to_f16");
+}
+void f16_to_f32_wide(float* dst, const uint16_t* src, int64_t n, void* stream) {
+ if (n <= 0) return;
+ f16_to_f32_wide_kernel<<<blocks_for(n / 8), 256, 0, (cudaStream_t) stream>>>(reinterpret_cast<float4*>(dst),
+ reinterpret_cast<const uint4*>(src), n / 8);
+ check("f16_to_f32_wide");
+}
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<<<blocks_for(T * N), 256, 0, (cudaStream_t) stream>>>(Dm, slot, w, shared, sg, peer, rows_local,
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 8ab2a5c8e..72320199f 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -565,6 +565,8 @@ struct PeerPrefill {
// 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;
+ bool f16 = false; // the sums mode's transfers in FP16 (STRATA_PF_PEER_F16=0: FP32)
+ uint16_t *sum16 = nullptr, *host_x16 = nullptr, *host_sum16 = nullptr;
float *sum = nullptr, *wk = nullptr;
int32_t* pair = nullptr;
float *host_w = nullptr, *host_sum = nullptr;
@@ -599,6 +601,8 @@ struct PeerPrefill {
if (host_w) cudaFreeHost(host_w);
if (host_sum) cudaFreeHost(host_sum);
if (host_pair) cudaFreeHost(host_pair);
+ if (host_x16) cudaFreeHost(host_x16);
+ if (host_sum16) cudaFreeHost(host_sum16);
}
};
@@ -1537,6 +1541,9 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e
pp->sums = v == nullptr || std::atoi(v) != 0;
}
if (pp->sums) {
+ 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);
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);
@@ -1563,10 +1570,13 @@ bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& e
cap = n;
};
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);
+ size_t x16_cap = 0, sum16_cap = 0;
+ if (pp->f16) host(pp->host_x16, x16_cap, (size_t) m.T_max * N);
+ else host(pp->host_x, pp->host_x_cap, (size_t) m.T_max * 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 && 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);
} else if (ok) {
host(pp->host_rows, pp->host_rows_cap, (size_t) pp->cap_rows * N);
@@ -1586,7 +1596,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 ? "" : (pp->sums ? ", no P2P: through mapped host memory, one weighted sum per token back"
+ pp->p2p ? "" : (pp->sums ? (pp->f16 ? ", no P2P: through mapped host memory in FP16, one weighted sum per token back"
+ : ", 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()
@@ -2985,7 +2996,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
peer_now = use_mmq && !order_peer.empty();
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.p2p && P.f16) cudaMemcpyAsync(P.host_x16, m.mixed_h, (size_t) T * N * 2, cudaMemcpyDeviceToHost, m.cs);
+ else 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)
@@ -3000,6 +3012,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
if (P.out_pending) { cudaStreamWaitEvent(ps, P.ev_done, 0); P.out_pending = false; }
pe.mark(kPeMoeIn, ps);
if (P.p2p) cudaMemcpyPeerAsync(P.mixed, P.dev, m.mixed, prevd, (size_t) T * N * 4, ps);
+ else if (P.f16) f16_to_f32_wide(P.mixed, P.host_x16, T * N, ps);
else copy_f32_wide(P.mixed, P.host_x, T * N, ps);
P.back_at = rows_local;
P.back_rows = rows_peer;
@@ -3177,7 +3190,12 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
}
pe.mark(kPeMoeOut, ps);
if (P.sums) { // the sums go back: T x N
- cudaMemcpyAsync(P.host_sum, P.sum, (size_t) T * N * 4, cudaMemcpyDeviceToHost, ps);
+ if (P.f16) {
+ sums_to_f16(P.sum, P.sum16, T * N, ps);
+ cudaMemcpyAsync(P.host_sum16, P.sum16, (size_t) T * N * 2, cudaMemcpyDeviceToHost, ps);
+ } else {
+ 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) {
@@ -3447,7 +3465,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
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);
}
- if (peer_now && m.pp->sums)
+ if (peer_now && m.pp->sums && m.pp->f16)
+ moe_combine_peer16(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.pp->host_sum16, m.pp->back_at, m.bo, T, m.cs);
+ else 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);