STRATA_ADAPT_OVERLAP=1 (opt-in): the adaptive tier's copies land while the next decode window runs, a layer waits for its own copies

This commit is contained in:
Niko1221 committed 2026-10-10 18:49:32 +02:00
1 parent 86070e7845
commit 9ef4484248
4 files changed
+199

No files matched your search

+71
View File
@@ -1320,6 +1320,16 @@ int adapt_lag() {
return v;
}
/// 0.1.43 STRATA_ADAPT_OVERLAP=1: the adaptive tier's copies land while the next decode window already runs. The window
/// sees the swapped-in experts as resident from its first layer, exactly as after #463's wait (so the tokens are the same
/// ones); what changes is only when the bytes arrive: the copies go in layer order and the verify window holds a layer's
/// plan back until that layer's copies have landed (AdaptGate in strata/core/verify.hpp). The ~10 ms a 96-swap round
/// took at the next window's start (PCIe 3.0 x16, Q2_0) is then hidden behind the window. Off by default.
bool adapt_overlap() {
static const bool v = [] { const char* e = std::getenv("STRATA_ADAPT_OVERLAP"); return e && e[0] == '1'; }();
return v;
}
/// --adapt-async: one background job at a time (the asynchronous adaptive tier's copies and memcpys). `post` hands
/// it over, `idle` says whether it has finished; the owner never posts while a job runs.
class JobThread {
@@ -8121,6 +8131,36 @@ int main(int argc, char** argv) {
std::vector<void*> pin_live; // the swaps' locked arena pages (pin_blob), unlocked once they have landed
cudaEvent_t adapt_ev = nullptr;
cudaEventCreateWithFlags(&adapt_ev, cudaEventDisableTiming);
// 0.1.43 STRATA_ADAPT_OVERLAP=1 (adapt_overlap above): the single-GPU, plain-RAM-arena decode loop only
strata::core::AdaptGate agate;
bool aflipped = false; // the pending swaps are in the tables and their copies may still be landing
bool aov = false;
int64_t aov_flips = 0;
if (adapt_overlap()) {
const char* 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"
: src.complement_ready() ? "not with the resident RAM mode"
: pin_blobs_on() ? "not with page-locked swap sources"
: strata::core::dma_batch_mode() != 0 ? "not with STRATA_DMA_BATCH"
: adapt_lag() != 1 || adapt_nowait() ? "not with STRATA_ADAPT_LAG / STRATA_ADAPT_NOWAIT"
: drive.d.usage.empty() ? "the adaptive tier is off"
: nullptr;
if (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};
agate.has.assign((size_t) g.n_layers, 0);
if (!ver.set_adapt_gate(&agate)) why = "the device-planned layers (STRATA_VERIFY_DEVICE_PLAN) are on";
}
aov = why == nullptr;
if (aov)
std::fprintf(stderr, "strata serve: STRATA_ADAPT_OVERLAP=1: the adaptive tier's copies land while the next window runs "
"(same tokens, a layer waits for its own copies)\n");
else
std::fprintf(stderr, "strata serve: STRATA_ADAPT_OVERLAP=1 is off: %s\n", why);
}
// a layer split's later stages keep a copy of the residency table on their devices, and swap on their own
auto res_upload = [&]() {
if (d_res != nullptr)
@@ -8149,6 +8189,8 @@ int main(int argc, char** argv) {
}
pending.clear();
pending_age = 0;
agate.active.store(false, std::memory_order_release); // 0.1.43: the copies have landed (STRATA_ADAPT_OVERLAP)
aflipped = false;
res_upload();
};
// --pipeline-windows: set while the tier adapts beside windows in flight (see its use in adapt)
@@ -8181,6 +8223,12 @@ int main(int argc, char** argv) {
}
std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; });
if ((int) swaps.size() > o.adapt_swaps) swaps.resize((size_t) o.adapt_swaps);
if (aov) { // 0.1.43: the copies go in layer order and each layer's landing is told to the verify window
std::stable_sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.layer < b.layer; });
agate.landed.store(-1, std::memory_order_release);
agate.has.assign((size_t) g.n_layers, 0);
for (const Swap& s : swaps) agate.has[(size_t) s.layer] = 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 {
const int stn = multi_gpu ? stage_of(layer) : 0;
@@ -8250,6 +8298,16 @@ int main(int argc, char** argv) {
host_res[out] = strata::core::kNotResident; // evicted now: the CPU computes it meanwhile
srcp->prefetch(s.layer, s.out); // a file-backed arena released its pages: read them back ahead
pending.emplace_back((int32_t) in, slot); // resident once the copy has landed
if (aov && (si + 1 == swaps.size() || swaps[si + 1].layer != s.layer) &&
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)) {}
}, &agate.ticks[(size_t) s.layer]) != cudaSuccess) {
(void) cudaGetLastError();
cudaStreamSynchronize(adapt_stream); // no callback: wait here, the gate opens at once
agate.landed.store((int32_t) g.n_layers, std::memory_order_release);
}
}
if (!cp_dst.empty() && strata::core::copy_blobs(cp_dst.data(), cp_src.data(), cp_bytes.data(), cp_dst.size(),
adapt_stream, dma_mode) != cudaSuccess) {
@@ -11575,6 +11633,15 @@ int main(int argc, char** argv) {
// expert ran on the GPU or the CPU (they round differently) depended on the copy's timing
// (STRATA_ADAPT_NOWAIT=1: 0.1.37's non-blocking query, the A/B)
if (adapt_nowait()) apply_pending(false);
else if (aov && !pending.empty() && !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)
for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot;
res_upload();
aflipped = true;
++aov_flips;
agate.active.store(true, std::memory_order_release);
}
else if (pending.empty() || ++pending_age >= adapt_lag()) apply_pending(true);
if (!adapt_tick(false)) { // --adapt-async: the round in flight moves on a step when it can
std::printf("ERR an adaptive refill failed\n");
@@ -11606,6 +11673,7 @@ int main(int argc, char** argv) {
std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str());
return 1;
}
if (aflipped) apply_pending(true); // 0.1.43: every layer's copies were waited for in the window; the bookkeeping now
// Diagnostics (as in generate): STRATA_DUMP_FIRST_LOGITS=<path> writes the logits of each request's first
// window to <path>.<n>, so the prompt paths (a peer card's share, a split) can be compared by KL
if (first_window) {
@@ -11724,6 +11792,9 @@ int main(int argc, char** argv) {
(d1.actq - ds0.actq) / w, (d1.jobs - ds0.jobs) / w, (d1.run - ds0.run) / w,
(d1.host - ds0.host) / w, dt_commit / w, dt_draft / w, (d1.misses - ds0.misses) / (w * L),
(d1.entries - ds0.entries) / (w * L), (d1.hits - ds0.hits) / (w * L), (d1.pcie - ds0.pcie) / (w * L));
if (aov)
std::fprintf(stderr, "strata decode adapt overlap: %lld windows took swaps still landing, a layer waited %.2f ms in all so far\n",
(long long) aov_flips, ver.ms_gate);
for (int st = 0; st < n_stages; ++st) { // every stage's GPU profile, not only the first card's
const std::string pr = stage_ver(st).profile_report();
if (pr.empty()) continue;