Adaptive overlap: tell each layer's landing with a stream-ordered copy into pinned memory, not a host function
The per-layer host function made the overlap depend on the driver's callback thread being scheduled: with the pool's workers spinning on every other CPU it still cost 7 ms per window on a 20-core desktop even with the thread on all CPUs (RTX 3060, Core Ultra 7 265: 46.5 tok/s with the warmed callbacks against 56.5 with the overlap off). The copy stream now ends each layer's copies with a 4-byte device-to-host copy of the layer number into a pinned int the gate polls; same stream order, no host thread. STRATA_ADAPT_SIGNAL=host brings the host functions back. Swaps, residency and answers are unchanged.
This commit is contained in:
1 parent
0e20d9aee9
commit
25e0a2ef79
3 files changed
+29
-3
No files matched your search
@@ -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;
|
||||
|
||||
+1
-1
@@ -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)) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user