From f80e2dd79aa98092dc6f2044426ab2f3b098c040 Mon Sep 17 00:00:00 2001 From: cobra91 Date: Mon, 23 Nov 2026 16:29:17 +0100 Subject: [PATCH] [Engine] peer_experts: direct hardware GPU-to-GPU PCIe flag handoff and zero CPU wait --- docs/SETTINGS.md | 3 +- engine/include/strata/core/expert_source.hpp | 2 + engine/include/strata/core/peer_experts.hpp | 7 ++- engine/include/strata/core/verify.hpp | 1 + engine/include/strata/kernels/elementwise.hpp | 3 +- engine/src/core/expert_source.cpp | 34 +++++++++------ engine/src/core/peer_experts.cpp | 43 +++++++++++++++++-- engine/src/core/verify.cpp | 10 ++++- engine/src/kernels/cuda/elementwise.cu | 14 +++--- engine/src/program/generate.cpp | 2 + 10 files changed, 93 insertions(+), 26 deletions(-) diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index bb8fe2806..8b1d06510 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -11,7 +11,7 @@ A number or a word is set like any environment variable, for example `STRATA_KVA (`terminal/strata-.json`), which the server passes to the engine. The Strata project's Terminal writes it there with `strata launch --set NAME=value`. -## Switches you can turn on (347) +## Switches you can turn on (348) Off unless you set them. The last column says where each one is described. @@ -211,6 +211,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_PCIE_FRAC_DEFAULT` | on / off | NVIDIA and AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md), [DETAILS.md](DETAILS.md), [README.md](bench-results/2026-10-20-0.2.1-idle-gpu/README.md) | | `STRATA_PCIE_K_RULE` | text | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | | `STRATA_PCIE_MIN1` | number | NVIDIA and AMD | [README.md](bench-results/2026-10-11-community-rtx3090-5600x-arena-pin-by-need/README.md) | +| `STRATA_PEER_DIRECT_SIGNAL` | number | NVIDIA and AMD | - | | `STRATA_PEER_HEAD_SPLIT` | text | NVIDIA | [NVIDIA_V100.md](NVIDIA_V100.md) | | `STRATA_PEER_NO_P2P` | number | NVIDIA and AMD | [SECOND_GPU.md](SECOND_GPU.md) | | `STRATA_PENALTY_PROMPT` | on / off | NVIDIA and AMD, Intel Arc | [DETAILS.md](DETAILS.md) | diff --git a/engine/include/strata/core/expert_source.hpp b/engine/include/strata/core/expert_source.hpp index 66c13c797..aa260e84e 100644 --- a/engine/include/strata/core/expert_source.hpp +++ b/engine/include/strata/core/expert_source.hpp @@ -418,6 +418,8 @@ struct GpuPlanSink { int32_t cpu_jobs = 0; ///< host only: the distinct experts the pool computes for the layer just published float* hit_out_dev = nullptr; ///< device address of this group's hit_out buffer on the primary GPU (P2P peer scatter) bool device_hits_only = false; ///< STRATA_VERIFY_DEVICE_HITS=1: GPU built its own hit plan; host only marks kind[] + uint32_t want_flag = 0; ///< direct GPU-to-GPU: flag value to signal upon peer completion + bool peer_signals_flag = false; ///< direct GPU-to-GPU: set when peer GPU directly signals the flag over PCIe }; /// Verifier::set_pcie_balance: a verify window's costs, fitted to each layer's measured times (ms, least squares with diff --git a/engine/include/strata/core/peer_experts.hpp b/engine/include/strata/core/peer_experts.hpp index a21543687..e9d27aa64 100644 --- a/engine/include/strata/core/peer_experts.hpp +++ b/engine/include/strata/core/peer_experts.hpp @@ -64,7 +64,7 @@ class PeerExperts { /// multi-GPU: with `out` (pinned, portable host rows) the peer writes its rows straight into /// out[entry] with a scatter kernel (no D2H copy + host memcpy in finish); STRATA_PEER_DIRECT=0: the old way. bool launch(int64_t layer, const float* x, const int32_t* ids, int64_t n_tok, int64_t k, const int32_t* kind, - std::string& err, float* out = nullptr); + std::string& err, float* out = nullptr, uint32_t want_flag = 0); /// Waits for the launched share and writes its rows into `out` (row i = entry i, n_embd floats). bool finish(float* out, std::string& err); @@ -92,6 +92,8 @@ class PeerExperts { /// whether a launch given host rows writes them itself (STRATA_PEER_DIRECT, default on): then the caller need not /// zero a peer row at all, whether the launch has happened yet or not static bool direct_mode(); + void set_target_flag(volatile uint32_t* h_flag); + bool signaled_target_flag() const { return signaled_target_flag_; } double gib() const { return cache_.gib(); } int64_t entries() const { return entries_; } ///< routed entries the peer computed int64_t experts() const { return experts_; } ///< distinct (layer, expert) the peer computed @@ -131,6 +133,9 @@ class PeerExperts { volatile uint32_t* h_done_seq_ = nullptr; uint32_t* d_done_seq_ = nullptr; int* d_blocks_finished_ = nullptr; + volatile uint32_t* h_target_flag_ = nullptr; + uint32_t* d_target_flag_ = nullptr; + bool signaled_target_flag_ = false; uint32_t seq_ = 0; }; diff --git a/engine/include/strata/core/verify.hpp b/engine/include/strata/core/verify.hpp index b142101ec..a96b5ab31 100644 --- a/engine/include/strata/core/verify.hpp +++ b/engine/include/strata/core/verify.hpp @@ -421,6 +421,7 @@ class Verifier { /// The GPU plan the pool writes each layer (VRAM hits + the PCIe share of the misses); give it to the /// dispatch (`ExpertDispatch::plan`) before the first `run`. GpuPlanSink* plan_sink() { return &sink_; } + uint32_t* h_flag() const { return h_flag_; } /// Plan v0.3 P6: split the window into two token groups and pipeline the CPU experts of one with the GPU work /// of the other (default on). Set before the first `run`. void set_split(bool on) { split_ = on; } diff --git a/engine/include/strata/kernels/elementwise.hpp b/engine/include/strata/kernels/elementwise.hpp index 9ca1c831a..2f25c32b1 100644 --- a/engine/include/strata/kernels/elementwise.hpp +++ b/engine/include/strata/kernels/elementwise.hpp @@ -107,7 +107,8 @@ void copy_from_mapped(float* dst, const float* src, int64_t n, void* stream); /// multi-GPU: dst[rows[r] * width ..] = src[r * width ..] for r < n (float4; dst may be mapped host memory). void scatter_rows_f32(const float* src, float* dst, const int32_t* rows, int64_t n, int64_t width, void* stream); void scatter_rows_and_signal_f32(const float* src, float* dst, const int32_t* rows, int64_t n, int64_t width, - uint32_t* d_done_ptr, uint32_t val, int* d_blocks_finished, void* stream); + uint32_t* d_done_ptr, uint32_t val, int* d_blocks_finished, void* stream, + uint32_t* d_flag_ptr = nullptr, uint32_t flag_val = 0); void signal_stream_done(uint32_t* d_ptr, uint32_t val, void* stream); void copy_rows_from_mapped(float* dst, const float* src, int64_t rows, int64_t width, const int32_t* hit_rows, const int32_t* count, void* stream); diff --git a/engine/src/core/expert_source.cpp b/engine/src/core/expert_source.cpp index 73e3bc3dd..7d4271d5f 100644 --- a/engine/src/core/expert_source.cpp +++ b/engine/src/core/expert_source.cpp @@ -5702,18 +5702,38 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 : (e >= 0 && e < d.n_expert && d.peer != nullptr && d.peer->has(d.layers, e)) ? 2 : -1; } } + static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }(); + // the GPU's copy_rows_from_mapped zeroes the rows it computed itself (dec_batch); STRATA_VERIFY_DEVICE_PLAN / + // STRATA_VERIFY_DEVICE_HITS's copy_or_zero_from_mapped copies every row, so there the host still zeroes them + static const bool device_plan = [] { + const char* p = std::getenv("STRATA_VERIFY_DEVICE_PLAN"); + const char* h = std::getenv("STRATA_VERIFY_DEVICE_HITS"); + return (p != nullptr && std::atoi(p) != 0) || (h != nullptr && std::atoi(h) != 0); + }(); + const bool gpu_zeroes_hits = (d.plan != nullptr && n <= kMaxWindowEntries && n <= d.plan->cap && dec_batch && !device_plan); + bool any_cpu = false; + bool any_tail = false; + for (int64_t i = 0; i < n; ++i) { + if (kind[i] == -1) any_cpu = true; // -2: skipped (STRATA_ROUTE_TAIL_SKIP), host zeroes row below + else if (kind[i] == -2) any_tail = true; + } + const bool can_peer_signal = (!any_cpu && !any_tail && gpu_zeroes_hits && d.remote_count == 0 && d.plan != nullptr && d.plan->want_flag > 0); + const uint32_t peer_want = can_peer_signal ? d.plan->want_flag : 0; if (d.peer != nullptr) { // multi-GPU: start the second GPU's share before the CPU's own work std::string perr; float* peer_out = (d.plan != nullptr && d.peer->p2p() && d.plan->hit_out_dev != nullptr && d.peer->direct_mode()) ? d.plan->hit_out_dev : out; - if (!d.peer->launch(d.layers, x_f, ids, n_tok, k, kind, perr, peer_out)) { + if (!d.peer->launch(d.layers, x_f, ids, n_tok, k, kind, perr, peer_out, peer_want)) { std::fprintf(stderr, "strata: %s (layer %lld)\n", perr.c_str(), (long long) d.layers); d.failed = true; d.fail = "the peer GPU's experts could not be launched"; d.fail_layer = d.layers; return; } + if (d.plan != nullptr && d.peer->signaled_target_flag()) { + d.plan->peer_signals_flag = true; + } } if (d.remote_count > 0) { static thread_local std::string remote_error; @@ -5724,18 +5744,6 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 for (int64_t i = 0; i < n; ++i) if (d.remote[r]->owns(i)) kind[i] = 2; } } - static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }(); - // the GPU's copy_rows_from_mapped zeroes the rows it computed itself (dec_batch); STRATA_VERIFY_DEVICE_PLAN / - // STRATA_VERIFY_DEVICE_HITS's copy_or_zero_from_mapped copies every row, so there the host still zeroes them - static const bool device_plan = [] { - const char* p = std::getenv("STRATA_VERIFY_DEVICE_PLAN"); - const char* h = std::getenv("STRATA_VERIFY_DEVICE_HITS"); - return (p != nullptr && std::atoi(p) != 0) || (h != nullptr && std::atoi(h) != 0); - }(); - const bool gpu_zeroes_hits = (d.plan != nullptr && n <= kMaxWindowEntries && n <= d.plan->cap && dec_batch && !device_plan); - bool any_cpu = false; - for (int64_t i = 0; i < n; ++i) - if (kind[i] == -1) { any_cpu = true; break; } // -2: skipped (STRATA_ROUTE_TAIL_SKIP), no work if (d.dres_check != nullptr && any_cpu) { // STRATA_ADAPT_CHECKRES bool published = false; for (int64_t i = 0; i < n && !published; ++i) diff --git a/engine/src/core/peer_experts.cpp b/engine/src/core/peer_experts.cpp index 1948d2538..f3a75134a 100644 --- a/engine/src/core/peer_experts.cpp +++ b/engine/src/core/peer_experts.cpp @@ -192,6 +192,9 @@ void PeerExperts::close() { h_done_seq_ = nullptr; d_done_seq_ = nullptr; d_blocks_finished_ = nullptr; + h_target_flag_ = nullptr; + d_target_flag_ = nullptr; + signaled_target_flag_ = false; seq_ = 0; stream_ = refill_ = nullptr; refill_ev_ = nullptr; @@ -339,8 +342,26 @@ bool PeerExperts::direct_mode() { return v; } +void PeerExperts::set_target_flag(volatile uint32_t* h_flag) { + h_target_flag_ = h_flag; + d_target_flag_ = nullptr; + if (h_flag != nullptr && device_ >= 0) { + On on(device_); + cudaError_t e = cudaHostGetDevicePointer((void**) &d_target_flag_, (void*) h_flag, 0); + if (e != cudaSuccess) { + std::fprintf(stderr, "strata peer: cudaHostGetDevicePointer failed for target flag: %s\n", + cudaGetErrorString(e)); + (void) cudaGetLastError(); + d_target_flag_ = nullptr; + } else { + std::fprintf(stderr, "strata peer: direct hardware flag handoff enabled (d_target_flag_=%p)\n", + (void*) d_target_flag_); + } + } +} + bool PeerExperts::launch(int64_t layer, const float* x, const int32_t* ids, int64_t n_tok, int64_t k, - const int32_t* kind, std::string& err, float* out) { + const int32_t* kind, std::string& err, float* out, uint32_t want_flag) { const bool direct = direct_mode() && out != nullptr; struct Timer { double& acc; @@ -348,6 +369,7 @@ bool PeerExperts::launch(int64_t layer, const float* x, const int32_t* ids, int6 ~Timer() { acc += std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); } } timer{ms_launch}; launched_direct_ = false; + signaled_target_flag_ = false; launched_rows_ = 0; row_of_.clear(); const int64_t n = n_tok * k; @@ -404,12 +426,19 @@ bool PeerExperts::launch(int64_t layer, const float* x, const int32_t* ids, int6 return false; } ++seq_; + static const bool direct_signal_env = [] { + const char* e = std::getenv("STRATA_PEER_DIRECT_SIGNAL"); + return e != nullptr && std::atoi(e) != 0; + }(); + uint32_t* const flag_target = (direct && direct_signal_env && want_flag > 0) ? d_target_flag_ : nullptr; if (direct) { // the rows straight into the host `out` (zero-copy, coalesced float4 writes) try { - if (d_done_seq_ != nullptr && d_blocks_finished_ != nullptr) { - strata::kernels::scatter_rows_and_signal_f32(d_out_, out, dm->out_row, rows, H, d_done_seq_, seq_, d_blocks_finished_, s); + if (d_blocks_finished_ != nullptr) { + strata::kernels::scatter_rows_and_signal_f32(d_out_, out, dm->out_row, rows, H, d_done_seq_, seq_, + d_blocks_finished_, s, flag_target, want_flag); } else { strata::kernels::scatter_rows_f32(d_out_, out, dm->out_row, rows, H, s); + if (flag_target != nullptr) strata::kernels::signal_stream_done(flag_target, want_flag, s); if (d_done_seq_ != nullptr) strata::kernels::signal_stream_done(d_done_seq_, seq_, s); } } catch (const std::exception& e) { err = std::string("peer experts: scatter: ") + e.what(); return false; } @@ -419,12 +448,20 @@ bool PeerExperts::launch(int64_t layer, const float* x, const int32_t* ids, int6 return false; if (d_done_seq_ != nullptr) strata::kernels::signal_stream_done(d_done_seq_, seq_, s); } + if (flag_target != nullptr) { + signaled_target_flag_ = true; + } return true; } bool PeerExperts::finish(float* out, std::string& err) { if (launched_rows_ == 0) return true; ++launches; + if (signaled_target_flag_) { + signaled_target_flag_ = false; + launched_rows_ = 0; + return true; + } const auto t0 = std::chrono::steady_clock::now(); if (d_done_seq_ != nullptr) { const uint32_t target = seq_; diff --git a/engine/src/core/verify.cpp b/engine/src/core/verify.cpp index 200ee638d..9ea21b802 100644 --- a/engine/src/core/verify.cpp +++ b/engine/src/core/verify.cpp @@ -4074,6 +4074,8 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, !adapt_gate_wait((int32_t) l, err)) return false; cur_layer_ = want - 1; set_plan_slot(grp); + sink_.want_flag = (k == 0 && do_ple) ? 0u : want; + sink_.peer_signals_flag = false; const int tb = gtb[grp], n = gte[grp] - gtb[grp]; progress_at("verify window: the CPU experts of layer", l); if (remote_opt_) remote_opt_->begin(h_w_ + (size_t) tb * ss.k, tb, n); @@ -4115,7 +4117,9 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, strata_store_fence(); ms_host += ms_since(tp); } - if (!(test_stall && k + 1 == steps)) *flag = want; + if (!(test_stall && k + 1 == steps)) { + if (!sink_.peer_signals_flag) *flag = want; + } if (h_bal_ != nullptr) bal_p_[(size_t) ((k / G) * 2 + grp)] = ms_since(b); ms_wait += std::chrono::duration(b - a).count(); ms_pool += ms_since(b); @@ -6068,6 +6072,8 @@ int Verifier::service(PoolMultiFn pool, void* user, std::string& err) { const int grp = (int) (fl_k_ % G); cur_layer_ = want - 1; set_plan_slot(grp); + sink_.want_flag = (fl_k_ == 0 && fl_ple_) ? 0u : want; + sink_.peer_signals_flag = false; const int tb = gtb[grp], n = gte[grp] - gtb[grp]; progress_at("verify window (pipelined): the CPU experts of layer", l); if (pool != nullptr) @@ -6089,7 +6095,7 @@ int Verifier::service(PoolMultiFn pool, void* user, std::string& err) { raise_flag_b_ordered(want); // behind any copy still queued from an earlier layer } if (fl_k_ == 0 && !gather_ple()) return -1; - *(volatile uint32_t*) h_flag_ = want; + if (!sink_.peer_signals_flag) *(volatile uint32_t*) h_flag_ = want; ++fl_k_; ms_pool += ms_since(b); fl_since_ms_ = fl_flush_ms_ = now_ms(); diff --git a/engine/src/kernels/cuda/elementwise.cu b/engine/src/kernels/cuda/elementwise.cu index 8adcc3d8e..aa416c35a 100644 --- a/engine/src/kernels/cuda/elementwise.cu +++ b/engine/src/kernels/cuda/elementwise.cu @@ -269,18 +269,21 @@ __global__ void scatter_rows_kernel(const float4* __restrict__ src, float4* dst, } __global__ void scatter_rows_and_signal_kernel(const float4* __restrict__ src, float4* dst, const int32_t* __restrict__ rows, int64_t w4, uint32_t* __restrict__ done_ptr, uint32_t val, - int* __restrict__ d_blocks_finished) { + int* __restrict__ d_blocks_finished, + uint32_t* __restrict__ flag_ptr, uint32_t flag_val) { const int64_t r = blockIdx.x; const float4* s = src + r * w4; float4* d = dst + (int64_t) rows[r] * w4; for (int64_t i = threadIdx.x; i < w4; i += blockDim.x) d[i] = s[i]; - if (done_ptr != nullptr) { + if (done_ptr != nullptr || flag_ptr != nullptr) { __syncthreads(); if (threadIdx.x == 0) { __threadfence_system(); if (atomicAdd(d_blocks_finished, 1) == (int) gridDim.x - 1) { *d_blocks_finished = 0; - *done_ptr = val; + __threadfence_system(); + if (flag_ptr != nullptr) *flag_ptr = flag_val; + if (done_ptr != nullptr) *done_ptr = val; } } } @@ -306,14 +309,15 @@ void scatter_rows_f32(const float* src, float* dst, const int32_t* rows, int64_t scatter_rows_kernel<<<(unsigned) n, 128, 0, (cudaStream_t) stream>>>((const float4*) src, (float4*) dst, rows, width / 4); } void scatter_rows_and_signal_f32(const float* src, float* dst, const int32_t* rows, int64_t n, int64_t width, - uint32_t* d_done_ptr, uint32_t val, int* d_blocks_finished, void* stream) { + uint32_t* d_done_ptr, uint32_t val, int* d_blocks_finished, void* stream, + uint32_t* d_flag_ptr, uint32_t flag_val) { if (n <= 0) return; if ((width & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) { std::fprintf(stderr, "scatter_rows_and_signal_f32: width must be a multiple of 4 and both pointers 16-byte aligned\n"); std::exit(1); } scatter_rows_and_signal_kernel<<<(unsigned) n, 128, 0, (cudaStream_t) stream>>>( - (const float4*) src, (float4*) dst, rows, width / 4, d_done_ptr, val, d_blocks_finished); + (const float4*) src, (float4*) dst, rows, width / 4, d_done_ptr, val, d_blocks_finished, d_flag_ptr, flag_val); } void copy_rows_from_mapped(float* dst, const float* src, int64_t rows, int64_t width, const int32_t* hit_rows, const int32_t* count, void* stream) { diff --git a/engine/src/program/generate.cpp b/engine/src/program/generate.cpp index 977fa6c98..9a9039d3b 100644 --- a/engine/src/program/generate.cpp +++ b/engine/src/program/generate.cpp @@ -13430,6 +13430,7 @@ int main(int argc, char** argv) { return 1; } if (prefill_head_alias_bound) mtp.set_window_R(ver.final_R_all()); + if (peer.valid()) peer.set_target_flag(ver.h_flag()); #if defined(STRATA_HAVE_DFLASH2) #if defined(STRATA_HAVE_DFLASH2) // DFlash2 prompt taps on a layer split. Every stage's prompt path taps the requested layers it runs into its own staging and hands them to its @@ -24407,6 +24408,7 @@ int main(int argc, char** argv) { return 1; } const bool use_mtp = !o.mtp.empty(); + if (peer.valid()) peer.set_target_flag(ver.h_flag()); if (use_mtp && !mtp.bind(wt, &native_head, ver.final_R_all(), err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1;