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
@@ -15344,25 +15344,40 @@ int main(int argc, char** argv) {
|
||||
if (ram_adapt_n > 0 && !first_window && !drive.d.usage.empty() && pending.empty() &&
|
||||
(!ajob || astate == AState::Idle) && src.complement_ready() && !multi_gpu && !peer.valid() &&
|
||||
!remote_opt && o.batch == 0) {
|
||||
// up to kRamAdaptPerLayer pairs a layer: the layer's most routed experts outside the copy against its least
|
||||
// routed ones in it, paired in that order while the outgoing one is still unused
|
||||
constexpr float kRamAdaptRouted = 2.0f, kRamAdaptUnused = 0.5f;
|
||||
constexpr size_t kRamAdaptPerLayer = 4;
|
||||
struct RamSwap { float gain; int32_t layer, in, out; };
|
||||
std::vector<RamSwap> swaps;
|
||||
std::vector<int32_t> ins, outs;
|
||||
for (int64_t l = 0; l < g.n_layers; ++l) {
|
||||
const float* u = drive.d.usage.data() + l * g.n_expert;
|
||||
const int32_t* r = host_res.data() + l * g.n_expert;
|
||||
int32_t in = -1, out = -1;
|
||||
ins.clear();
|
||||
outs.clear();
|
||||
for (int32_t e = 0; e < (int32_t) g.n_expert; ++e) {
|
||||
if (r[e] >= 0) continue; // in VRAM: the RAM copy does not hold it
|
||||
if (src.has_resident(l, e)) { if (out < 0 || u[e] < u[out]) out = e; }
|
||||
else if (u[e] >= kRamAdaptRouted && (in < 0 || u[e] > u[in])) in = e;
|
||||
if (src.has_resident(l, e)) outs.push_back(e);
|
||||
else if (u[e] >= kRamAdaptRouted) ins.push_back(e);
|
||||
}
|
||||
if (in >= 0 && out >= 0 && u[out] < kRamAdaptUnused)
|
||||
swaps.push_back({u[in] - u[out], (int32_t) l, in, out});
|
||||
const size_t ni = std::min(ins.size(), kRamAdaptPerLayer), no = std::min(outs.size(), kRamAdaptPerLayer);
|
||||
std::partial_sort(ins.begin(), ins.begin() + (std::ptrdiff_t) ni, ins.end(),
|
||||
[&](int32_t a, int32_t b) { return u[a] > u[b]; });
|
||||
std::partial_sort(outs.begin(), outs.begin() + (std::ptrdiff_t) no, outs.end(),
|
||||
[&](int32_t a, int32_t b) { return u[a] < u[b]; });
|
||||
for (size_t i = 0; i < ni && i < no && u[outs[i]] < kRamAdaptUnused; ++i)
|
||||
swaps.push_back({u[ins[i]] - u[outs[i]], (int32_t) l, ins[i], outs[i]});
|
||||
}
|
||||
std::sort(swaps.begin(), swaps.end(),
|
||||
[](const RamSwap& a, const RamSwap& b) { return a.gain > b.gain; });
|
||||
if ((int) swaps.size() > ram_adapt_n) swaps.resize((size_t) ram_adapt_n);
|
||||
for (const RamSwap& x : swaps) src.replace_resident(x.layer, x.in, x.out);
|
||||
// the incoming expert was usually just read for the window before: a pair whose `in` still sits in that
|
||||
// read's stage buffer moves with a copy out of it (no file read); n caps only the pairs that need the file read
|
||||
int file_left = ram_adapt_n;
|
||||
for (const RamSwap& x : swaps) {
|
||||
if (src.replace_resident_staged(x.layer, x.in, x.out)) continue;
|
||||
if (file_left > 0) { --file_left; src.replace_resident(x.layer, x.in, x.out); }
|
||||
}
|
||||
}
|
||||
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");
|
||||
@@ -15881,8 +15896,9 @@ int main(int argc, char** argv) {
|
||||
(long long) src.ram_staged_reads(),
|
||||
(long long) ram_adapt2.installed, (long long) (src.ram_install_skips() + src.ram_install_gpu()), (long long) src.ram_install_gpu());
|
||||
if (src.ram_adapts() > 0) // STRATA_RAM_ADAPT, cumulative
|
||||
std::fprintf(stderr, "strata serve: resident RAM: %lld experts moved in from the file in place of "
|
||||
"unused ones (STRATA_RAM_ADAPT)\n", (long long) src.ram_adapts());
|
||||
std::fprintf(stderr, "strata serve: resident RAM: %lld experts moved in in place of unused ones, %lld of "
|
||||
"them out of a stage buffer (STRATA_RAM_ADAPT)\n", (long long) src.ram_adapts(),
|
||||
(long long) src.ram_staged());
|
||||
if (ajob) // --adapt-async, cumulative (a round may still be in flight: it lands before the next request)
|
||||
std::fprintf(stderr, "strata serve: asynchronous adaptive tier: %lld rounds, %lld experts swapped in, "
|
||||
"%.1f ms per round (start to flip, between windows)",
|
||||
|
||||
Reference in new issue
Block a user