expert source: STRATA_RAM_ADAPT copies the incoming expert out of its stage buffer when it is still there, and moves up to 4 pairs a layer (#3060)
STRATA_RAM_ADAPT (opt-in, --serve, one GPU, needs --adapt-every) lets the resident RAM copy follow the conversation: experts the windows keep routing to take the RAM places of unused experts of their layer. Each move re-read the incoming expert from the file and a layer moved one pair per window, although the incoming expert was usually read for the window before and its blob still sits in that read's stage buffer. What changed: - FileExpertSource::replace_resident_staged(layer, in, out): the same move as replace_resident, with the bytes copied out of the finished stage buffer of `in` (no file read). The copy runs under stage_mu_, so claim_stage cannot give the buffer to another blob meanwhile; it needs a non-busy buffer whose key is `in`, and returns false (nothing changed) otherwise, and the pair then goes the old way. The Foresight hold/unhold around the copy is the one install_resident uses. - generate.cpp: a layer pairs up to 4 of its most routed outside experts with up to 4 of its least routed ones in the copy (outgoing count below 0.5, incoming 2 or more), largest difference first. A pair whose incoming expert is staged moves by copy; `n` now caps only the pairs that need a file read. The log line counts the moves and how many came out of a stage buffer (ram_staged()). - docs/DETAILS.md: the RAM_ADAPT bullet in the low-RAM list in plain words and one row of the measured table (the author's numbers: RTX 3060, IQ2_XS and IQ3_XXS, 120 ShareGPT requests, 3 interleaved runs each). Unset (the default) nothing runs: the block is guarded by ram_adapt_n > 0 as before. The weights moved are byte copies of the same blob; which experts sit in RAM changes, as it already does with RAM_ADAPT, so greedy token ids may differ from run to run once it is on. Changed from the PR: the DETAILS.md hunk was rewritten against the current, shorter bullet. Tests: file_expert_source_test (new staged cases; the positive case needs unbuffered load and is skipped without it). Not covered: no model run. Model gate written down: RTX 3060 ShareGPT replay =8 against =1.
This commit is contained in:
1 parent
f217b23bd3
commit
2b13a44933
5 files changed
+87
-12
No files matched your search
@@ -4341,6 +4341,33 @@ bool FileExpertSource::replace_resident(int64_t layer, int64_t in, int64_t out)
|
||||
return install_resident(layer, in, out, ram_adapt_buf_.data());
|
||||
}
|
||||
|
||||
bool FileExpertSource::replace_resident_staged(int64_t layer, int64_t in, int64_t out) {
|
||||
if (!complement_ready_ || complement_host_ == nullptr || exchange_storage_.active() || !staged_.empty() ||
|
||||
layer < 0 || layer >= n_layers_ || in < 0 || out < 0 || in >= n_expert_ || out >= n_expert_ || in == out)
|
||||
return false;
|
||||
const size_t i_in = (size_t) layer * (size_t) n_expert_ + (size_t) in;
|
||||
const size_t i_out = (size_t) layer * (size_t) n_expert_ + (size_t) out;
|
||||
if (i_in >= complement_offsets_.size() || i_out >= complement_offsets_.size()) return false;
|
||||
if (!override_.empty() && (override_[i_in] != nullptr || override_[i_out] != nullptr)) return false;
|
||||
const uint64_t at = complement_offsets_[i_out], bytes = layer_blob_bytes_[(size_t) layer];
|
||||
if (at == kNoComplement || complement_offsets_[i_in] != kNoComplement || at > complement_bytes_ ||
|
||||
bytes > complement_bytes_ - at || bytes > stage_blob_) return false;
|
||||
ForesightSwap* fs = fs_swap_.load(std::memory_order_acquire);
|
||||
bool moved = false;
|
||||
{
|
||||
// held across the copy: claim_stage cannot hand the buffer to another blob meanwhile
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
const auto it = stage_of_.find((int64_t) i_in);
|
||||
if (it == stage_of_.end() || stage_busy_[it->second] || stage_key_[it->second] != (int64_t) i_in) return false;
|
||||
if (fs != nullptr) fs->hold(layer, (int32_t) out);
|
||||
std::memcpy((uint8_t*) complement_host_ + (size_t) at, stage_buf_[it->second].get(), (size_t) bytes);
|
||||
moved = detail::exchange_cache_complement(complement_offsets_, i_out, i_in);
|
||||
if (fs != nullptr) fs->unhold(layer, (int32_t) out);
|
||||
}
|
||||
if (moved) { ++ram_adapts_; ++ram_staged_; }
|
||||
return moved;
|
||||
}
|
||||
|
||||
// `in`'s bytes (src) into `out`'s place of the compact copy, and the two offsets swapped - the checks again, as the caller
|
||||
// may have read `src` a while ago (149-prefetch: ram_install)
|
||||
bool FileExpertSource::install_resident(int64_t layer, int64_t in, int64_t out, const uint8_t* src) {
|
||||
|
||||
Reference in new issue
Block a user