peer tier: STRATA_ADAPT_OVERLAP=1 with --peer-device (the peer's copies land while the next window runs)

In peer mode the adaptive overlap was refused, so every decode window first waited for the previous round's copies. With the overlap the peer
tier issues its copies in layer order, signals each layer's landing the way the primary does (a stream-ordered D2H copy of the layer number into
a pinned int, or host functions), its residency table takes the swaps at the window's start (flip_pending), and a layer waits for a tier's copies
only when that tier swapped in it (AdaptGate: peer_landed, peer_has, per-tier last). The wait before a window clears the peer's marks with the
primary's. Without the variable nothing changes.

Ported from #2720 (bulhenvaster), rebased by hand onto the current gate (the watchdog label stays). Not ported: STRATA_PEER_LAUNCH_IN_POOL
(the author measured no gain on his build), the new decode timing line and the documentation paragraph.

(cherry picked from commit e78b64a7348d685dfd11799fdb395661f2120436)
This commit is contained in:
bulhenvaster authored and Niko1221 committed 2026-11-06 15:47:43 +01:00
1 parent 2b9f05093d
commit bca7931f3d
5 files changed
+159 -14

No files matched your search

+29 -8
View File
@@ -12809,9 +12809,10 @@ int main(int argc, char** argv) {
int64_t aov_flips = 0;
// #2010: the overlap is opt-in on every backend now, so the off state is the default and has to be visible too.
// The predicates are side-effect free, so they are read in both states: they name why it could not run.
// A peer tier (--peer-device) joins the overlap: measured on one rig so far (RTX 5060 Ti x8 + RTX 3060, IQ3_XXS),
// where the blocking wait took 3-8 ms of a 27-43 ms window.
const bool aov_want = adapt_overlap();
const char* aov_why = multi_gpu || !stages.empty() ? "not on a layer split"
: peer.valid() ? "not with --peer-device"
: remote_opt ? "not with --remote-expert-opt"
: o.adapt_async ? "not with --adapt-async"
: o.pipeline_windows > 0 ? "not with --pipeline-windows"
@@ -12828,8 +12829,9 @@ int main(int argc, char** argv) {
if (aov_want) {
if (aov_why == nullptr) {
agate.ticks.resize((size_t) g.n_layers + 1);
for (int64_t l = 0; l <= g.n_layers; ++l) agate.ticks[(size_t) l] = {&agate, (int32_t) l};
for (int64_t l = 0; l <= g.n_layers; ++l) agate.ticks[(size_t) l] = {&agate.landed, (int32_t) l};
agate.has.assign((size_t) g.n_layers, 0);
agate.peer_has.assign((size_t) g.n_layers, 0);
if (!ver.set_adapt_gate(&agate)) aov_why = "the device-planned layers (STRATA_VERIFY_DEVICE_PLAN) are on";
// #2188: on Windows the default is the host function. Under WDDM a stream's work sits in the driver's
// command buffer until something flushes it, and nothing does while the host only spins on the pinned
@@ -12859,11 +12861,16 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata serve: adaptive overlap signal: stream-ordered copies (STRATA_ADAPT_SIGNAL=host: a host function)\n");
} else { (void) cudaGetLastError(); }
}
// the peer tier's copies land over its own link and are gated the same way, with the same kind of signal
if (aov_why == nullptr && peer.valid())
agate.peer_hland = peer.set_overlap_gate(&agate.peer_landed, &agate.peer_has, &agate.peer_last, sig_copy);
}
aov = aov_why == nullptr;
if (aov)
std::fprintf(stderr, "strata serve: adaptive overlap (STRATA_ADAPT_OVERLAP=0 turns it off): the adaptive tier's copies land while the next window runs "
"(same tokens, a layer waits for its own copies)\n");
"(same tokens, a layer waits for its own copies)%s\n",
!peer.valid() ? "" : agate.peer_hland != nullptr ? "; the peer tier's copies too, over its own link (stream-ordered copies)"
: "; the peer tier's copies too, over its own link (host functions)");
else if (std::getenv("STRATA_ADAPT_OVERLAP") != nullptr) // the env var was set: say what stopped it
std::fprintf(stderr, "strata serve: STRATA_ADAPT_OVERLAP=1 is off: %s\n", aov_why);
} else
@@ -12881,7 +12888,15 @@ int main(int argc, char** argv) {
};
auto apply_pending = [&](bool wait) {
if (peer.valid()) peer.apply_pending(wait);
if (pending.empty()) return;
if (pending.empty()) {
// a round where only the peer swapped: its copies have landed (or, without the wait, are still in
// flight), and the gate closes with them
if (!(peer.valid() && peer.has_pending())) {
agate.active.store(false, std::memory_order_release);
aflipped = false;
}
return;
}
if (wait) cudaEventSynchronize(adapt_ev);
else if (cudaEventQuery(adapt_ev) != cudaSuccess) return;
for (auto& st : stages)
@@ -13051,7 +13066,12 @@ int main(int argc, char** argv) {
agate.landed.store(-1, std::memory_order_release);
if (agate.hland != nullptr) *agate.hland = -1;
agate.has.assign((size_t) g.n_layers, 0);
for (const Swap& s : swaps) agate.has[(size_t) s.layer] = 1;
agate.last = -1;
for (const Swap& s : swaps) { agate.has[(size_t) s.layer] = 1; agate.last = std::max(agate.last, s.layer); }
// the peer's marks of the previous round go too (its adapt below sets this round's, if it swaps anything)
agate.peer_landed.store(-1, std::memory_order_release);
agate.peer_has.assign((size_t) g.n_layers, 0);
agate.peer_last = -1;
}
// the copies back read the cache of the card that owns each layer (see resident_stage_swaps)
auto home_of = [&](int64_t layer) -> SwapHome {
@@ -13137,8 +13157,8 @@ int main(int argc, char** argv) {
? cudaMemcpyAsync((void*) agate.hland, agate.dvals + s.layer, 4, cudaMemcpyDeviceToHost, adapt_stream)
: cudaLaunchHostFunc(adapt_stream, [](void* p) {
auto* t = (strata::core::AdaptGate::Tick*) p;
int32_t cur = t->gate->landed.load(std::memory_order_relaxed);
while (cur < t->layer && !t->gate->landed.compare_exchange_weak(cur, t->layer, std::memory_order_release)) {}
int32_t cur = t->counter->load(std::memory_order_relaxed);
while (cur < t->layer && !t->counter->compare_exchange_weak(cur, t->layer, std::memory_order_release)) {}
}, &agate.ticks[(size_t) s.layer])) != cudaSuccess) {
(void) cudaGetLastError();
cudaStreamSynchronize(adapt_stream); // no callback: wait here, the gate opens at once
@@ -18932,7 +18952,7 @@ int main(int argc, char** argv) {
const bool asw_skip = first_window || merged; // the session's tier choice counts steady decode windows only
double asw_wait_ms = 0;
if (adapt_nowait()) apply_pending(false);
else if (aov && !pending.empty() && !aflipped) {
else if (aov && (!pending.empty() || (peer.valid() && peer.has_pending())) && !aflipped) {
// 0.1.43 STRATA_ADAPT_OVERLAP: the tables take the swaps now, exactly where the wait would have ended;
// the copies still landing are waited for per layer inside the window (AdaptGate)
// (resident RAM mode: the staged evicted experts look committed to the planner at once, the RAM
@@ -18941,6 +18961,7 @@ int main(int argc, char** argv) {
apply_pending(true); // not possible here: the plain wait
} else {
for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot;
if (peer.valid()) peer.flip_pending(); // its copies land over its own link, gated the same way
res_upload();
aflipped = true;
++aov_flips;