Stage buffers: fence a pinned buffer's queued copy before the buffer is recycled (#1237)
A page-locked stage buffer is the source of the cache fill's cudaMemcpyAsync, which the DMA reads after the call has returned; a pageable one is staged by the driver before it returns. The pool recycles a buffer after kStageAge layers or 256 assemblies, so a 4096-token prompt read (up to ~500 distinct experts per layer) could overwrite a buffer whose copy had not run yet: the corrupted IQ3_S decode of #1237. note_async_read(blob, stream) records an event per pinned buffer after each queued copy (hit-path fill, peer fill, remote-expert swap); claim_stage waits for it before it hands the buffer out again. Pageable buffers and every other source are unchanged. STRATA_STAGE_PIN stays opt-in.
This commit is contained in:
1 parent
fb58e0dbc8
commit
f71c5307c6
4 files changed
+71
-2
No files matched your search
@@ -146,6 +146,11 @@ public:
|
||||
/// A blob pointer that stays valid (not recycled) for as long as the source is open: for a copy that runs
|
||||
/// asynchronously long after the call (the adaptive tier's swap-ins). Defaults to `blob`.
|
||||
virtual const uint8_t* blob_stable(int64_t layer, int64_t expert) { return blob(layer, expert); }
|
||||
/// The caller has queued an asynchronous copy out of `blob` (a pointer this source handed out) on `stream` (a
|
||||
/// cudaStream_t): the source must not overwrite or recycle that memory until the copy has finished. A source whose
|
||||
/// pointers are never recycled ignores it. (#1237: a page-locked stage buffer is read by the DMA after the copy call
|
||||
/// returned, where a pageable one is staged by the driver before it returns.)
|
||||
virtual void note_async_read(const uint8_t* blob, void* stream) { (void) blob; (void) stream; }
|
||||
|
||||
/// Blobs touched, for the driver to report. A source that does not count returns 0.
|
||||
virtual int64_t reads() const { return 0; }
|
||||
@@ -603,6 +608,7 @@ public:
|
||||
const uint8_t* blob(int64_t layer, int64_t expert) override;
|
||||
/// With io prefetch in the experts.bin tier, the mapping itself (blob() may hand out a recycled staging buffer).
|
||||
const uint8_t* blob_stable(int64_t layer, int64_t expert) override;
|
||||
void note_async_read(const uint8_t* blob, void* stream) override;
|
||||
/// Windows, opt-in with STRATA_FILE_RELEASE=1: trim the file mapping's full pages after their expert reaches
|
||||
/// VRAM. Never touches the resident complement or staging buffers. Disabled by default.
|
||||
uint64_t release(int64_t layer, int64_t expert) override;
|
||||
@@ -728,6 +734,13 @@ private:
|
||||
int64_t last_layer_ = -1;
|
||||
bool stage_grew_ = false;
|
||||
bool stage_pin_said_ = false, stage_pin_failed_said_ = false; ///< the one-time notes in claim_stage
|
||||
// #1237: per stage buffer, the event recorded after the last asynchronous copy out of it (note_async_read); a
|
||||
// buffer is refilled only once that event has completed. cudaEvent_t, stored untyped (no cuda_runtime.h here).
|
||||
std::vector<void*> stage_ev_;
|
||||
std::vector<char> stage_ev_live_; ///< a copy was queued since the buffer was last waited for
|
||||
std::unordered_map<const uint8_t*, size_t> stage_idx_; ///< buffer address -> index (the buffers never move)
|
||||
void stage_free_events();
|
||||
std::atomic<uint64_t> stage_fence_waits_{0}; ///< refills that had to wait for a queued copy
|
||||
std::atomic<int64_t> ram_reads_{0};
|
||||
std::atomic<uint64_t> file_read_bytes_{0};
|
||||
// ---- the Linux I/O path (set_io_prefetch)
|
||||
|
||||
@@ -958,6 +958,9 @@ void FileExpertSource::close() {
|
||||
direct_.clear();
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
// the buffers' last copies end before they are freed (#1237)
|
||||
if (!stage_ev_.empty()) (void) cudaDeviceSynchronize();
|
||||
stage_free_events();
|
||||
stage_buf_.clear();
|
||||
stage_key_.clear();
|
||||
stage_epoch_.clear();
|
||||
@@ -1207,6 +1210,9 @@ bool FileExpertSource::claim_stage(int64_t key, size_t& v, bool& fill, bool ahea
|
||||
uint8_t* raw = pinned ? (uint8_t*) p : new (std::nothrow) uint8_t[(size_t) stage_blob_];
|
||||
if (raw == nullptr) return false;
|
||||
stage_buf_.emplace_back(raw, StageBufFree{pinned});
|
||||
stage_idx_[raw] = stage_buf_.size() - 1;
|
||||
stage_ev_.push_back(nullptr);
|
||||
stage_ev_live_.push_back(0);
|
||||
if (pinned && !stage_pin_said_) {
|
||||
stage_pin_said_ = true;
|
||||
std::fprintf(stderr, "FileExpertSource: the stage buffers are pinned (cudaHostAlloc)\n");
|
||||
@@ -1228,6 +1234,19 @@ bool FileExpertSource::claim_stage(int64_t key, size_t& v, bool& fill, bool ahea
|
||||
} else {
|
||||
stage_of_.erase(stage_key_[v]);
|
||||
if (stage_pf_[v]) { stage_pf_[v] = 0; io_pf_unused_.fetch_add(1, std::memory_order_relaxed); } // read ahead, never asked for
|
||||
// #1237: a page-locked buffer is read by the DMA after the copy call returned: it is refilled only once the
|
||||
// last copy queued out of it (note_async_read) has finished. Rare: the pool is sized so that a buffer asked
|
||||
// for has long been consumed; a wait here means the stream is more than a pool behind.
|
||||
if (v < stage_ev_live_.size() && stage_ev_live_[v]) {
|
||||
stage_ev_live_[v] = 0;
|
||||
if (stage_ev_[v] != nullptr) {
|
||||
if (cudaEventQuery((cudaEvent_t) stage_ev_[v]) == cudaErrorNotReady) {
|
||||
stage_fence_waits_.fetch_add(1, std::memory_order_relaxed);
|
||||
(void) cudaEventSynchronize((cudaEvent_t) stage_ev_[v]);
|
||||
}
|
||||
(void) cudaGetLastError();
|
||||
}
|
||||
}
|
||||
}
|
||||
stage_key_[v] = key;
|
||||
stage_epoch_[v] = epoch_;
|
||||
@@ -1263,6 +1282,38 @@ void FileExpertSource::publish_stage(size_t v, int64_t layer, bool ok, double us
|
||||
stage_cv_.notify_all();
|
||||
}
|
||||
|
||||
void FileExpertSource::note_async_read(const uint8_t* blob, void* stream) {
|
||||
if (blob == nullptr) return;
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
const auto it = stage_idx_.find(blob);
|
||||
if (it == stage_idx_.end()) return; // not a stage buffer: the mapping or the resident arena
|
||||
const size_t v = it->second;
|
||||
if (!stage_buf_[v].get_deleter().pinned) return; // pageable: the driver copied it before the call returned
|
||||
if (stage_ev_[v] == nullptr) {
|
||||
cudaEvent_t ev = nullptr;
|
||||
if (cudaEventCreateWithFlags(&ev, cudaEventDisableTiming) != cudaSuccess) {
|
||||
(void) cudaGetLastError();
|
||||
(void) cudaStreamSynchronize((cudaStream_t) stream); // no event: wait for the copy itself
|
||||
return;
|
||||
}
|
||||
stage_ev_[v] = ev;
|
||||
}
|
||||
if (cudaEventRecord((cudaEvent_t) stage_ev_[v], (cudaStream_t) stream) != cudaSuccess) {
|
||||
(void) cudaGetLastError();
|
||||
(void) cudaStreamSynchronize((cudaStream_t) stream);
|
||||
return;
|
||||
}
|
||||
stage_ev_live_[v] = 1;
|
||||
}
|
||||
|
||||
void FileExpertSource::stage_free_events() {
|
||||
for (void*& e : stage_ev_)
|
||||
if (e != nullptr) { (void) cudaEventDestroy((cudaEvent_t) e); e = nullptr; }
|
||||
stage_ev_.clear();
|
||||
stage_ev_live_.clear();
|
||||
stage_idx_.clear();
|
||||
}
|
||||
|
||||
const uint8_t* FileExpertSource::staged_blob(int64_t layer, int64_t expert) {
|
||||
const int64_t key = layer * n_expert_ + expert;
|
||||
size_t v = 0;
|
||||
@@ -3422,6 +3473,7 @@ void expert_hit_run(void* user, void* stream, HitPhase phase, const int32_t* ids
|
||||
d.fail_expert = e;
|
||||
return;
|
||||
}
|
||||
d.src->note_async_read(b, cs); // #1237: a page-locked stage buffer is not recycled under the queued copy
|
||||
++d.cache_admitted;
|
||||
slot = cand;
|
||||
} else {
|
||||
|
||||
@@ -170,6 +170,7 @@ bool PeerExperts::open(int device, const std::vector<std::pair<int32_t, int32_t>
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
src.note_async_read(b, refill_); // #1237
|
||||
res_[(size_t) (pr.first * n_expert + pr.second)] = slot;
|
||||
}
|
||||
if (!ck(cudaStreamSynchronize(refill_), "fill", err)) { close(); return false; }
|
||||
|
||||
@@ -178,9 +178,12 @@ bool RemoteExpertOpt::adapt(const std::vector<float>& usage, const std::vector<i
|
||||
if ((int) swaps.size() > max_swaps) swaps.resize((size_t) max_swaps);
|
||||
if (swaps.empty()) continue;
|
||||
const OnDevice on(r.device_);
|
||||
for (const auto& s : swaps)
|
||||
if (!r.cache_.fill_slot(r.cache_.slot_of(s.layer, s.out), source.blob(s.layer, s.in),
|
||||
for (const auto& s : swaps) {
|
||||
const uint8_t* in_blob = source.blob(s.layer, s.in);
|
||||
if (!r.cache_.fill_slot(r.cache_.slot_of(s.layer, s.out), in_blob,
|
||||
r.stream_, err, (int64_t) lay.blob_bytes(s.layer))) return false;
|
||||
source.note_async_read(in_blob, r.stream_); // #1237
|
||||
}
|
||||
if (!check(cudaStreamSynchronize(r.stream_), err)) return false;
|
||||
for (const auto& s : swaps) {
|
||||
r.cache_.replace(s.layer, s.out, s.in);
|
||||
|
||||
Reference in new issue
Block a user