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++

Adaptive overlap: tell each layer's landing with a stream-ordered copy into pinned memory, not a host function

Niko122125e0a2ef79d9
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp
index e58a05ade..ffaea0591 100644
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@ -49,6 +49,14 @@ struct AdaptGate {
std::atomic<bool> active{false};
std::vector<uint8_t> has; ///< per layer: a swap copies into this layer's slots
std::vector<Tick> ticks; ///< host-function contexts, one per layer and the final one (stable storage)
+ const int32_t* dvals = nullptr; ///< device [0..n_layers]: the source of those copies
+ volatile int32_t* hland = nullptr; ///< pinned host int the copy stream writes (a D2H copy of the layer number), instead of a host function; null: host functions
+ int32_t reached() const {
+ const int32_t a = landed.load(std::memory_order_acquire);
+ if (hland == nullptr) return a;
+ const int32_t b = *hland;
+ return a > b ? a : b;
+ }
};
class NativeHead;
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index df61724fc..3995ae9dd 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -2120,7 +2120,7 @@ void Verifier::accumulate_profile(const unsigned long long* stamps) {
bool Verifier::adapt_gate_wait(int32_t layer, std::string& err) {
const Clock::time_point t0 = Clock::now();
uint32_t spins = 0;
- while (adapt_gate_->landed.load(std::memory_order_acquire) < layer) {
+ while (adapt_gate_->reached() < layer) {
_mm_pause();
if ((++spins & 1023u) != 0) continue;
if (Clock::now() - t0 > std::chrono::seconds(20)) {
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 4592c3103..e4c8632ab 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -8311,6 +8311,21 @@ int main(int argc, char** argv) {
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";
+ if (why == nullptr && !(std::getenv("STRATA_ADAPT_SIGNAL") && std::getenv("STRATA_ADAPT_SIGNAL")[0] == 'h')) {
+ // the landing of each layer's copies is told by a stream-ordered D2H copy of the layer number into pinned memory
+ // (STRATA_ADAPT_SIGNAL=host: a host function instead). No callback thread has to be scheduled while the pool's workers spin on every other CPU
+ int32_t* vals = nullptr; void* hp = nullptr;
+ std::vector<int32_t> iota((size_t) g.n_layers + 1);
+ for (size_t i = 0; i < iota.size(); ++i) iota[i] = (int32_t) i;
+ if (cudaMalloc((void**) &vals, iota.size() * 4) == cudaSuccess &&
+ cudaMemcpy(vals, iota.data(), iota.size() * 4, cudaMemcpyHostToDevice) == cudaSuccess &&
+ cudaHostAlloc(&hp, 64, cudaHostAllocPortable) == cudaSuccess) {
+ *(volatile int32_t*) hp = -1;
+ agate.hland = (volatile int32_t*) hp;
+ agate.dvals = vals;
+ std::fprintf(stderr, "strata serve: adaptive overlap signal: stream-ordered copies (STRATA_ADAPT_SIGNAL=host: a host function)\n");
+ } else { (void) cudaGetLastError(); }
+ }
}
aov = why == nullptr;
if (aov)
@@ -8384,6 +8399,7 @@ int main(int argc, char** argv) {
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);
+ 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;
}
@@ -8464,11 +8480,13 @@ int main(int argc, char** argv) {
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) {
+ (agate.hland != nullptr
+ ? 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)) {}
- }, &agate.ticks[(size_t) s.layer]) != cudaSuccess) {
+ }, &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);