sm_86: reserve split lend regions before cold complement tail under RAM budget (#6583)

This commit is contained in:
Niko1221 committed 2026-11-22 15:41:53 +01:00
1 parent 2a8265942f
commit 307ccd1bab
3 files changed
+97 -3

No files matched your search

+26 -2
View File
@@ -3982,6 +3982,30 @@ bool FileExpertSource::pin_cache_complement(
}
#endif
std::vector<uint64_t> ranked(offsets.size(), kNoComplement);
// #6583: on a multi-GPU layer split under a RAM budget, reserve room for the stages' prompt-path lend
// regions BEFORE filling the coldest tail of the complement. Borrowed slots hold GPU-resident hot experts
// that are evicted and re-read on every prompt chunk; letting the coldest complement experts crowd them out
// turns every chunk into hundreds of uncached SSD blob reads.
uint64_t stage_lend_reserve = 0;
if (!stage_lend_regions_.empty()) {
std::vector<uint8_t> seen(offsets.size(), 0);
for (const auto& region : stage_lend_regions_) {
for (const auto& pair : region) {
if (pair.first < 0 || pair.first >= n_layers_ || pair.second < 0 || pair.second >= n_expert_) continue;
const size_t idx = (size_t) pair.first * (size_t) n_expert_ + (size_t) pair.second;
if (offsets[idx] != kNoComplement || seen[idx]) continue;
seen[idx] = 1;
const uint64_t b = layer_blob_bytes_[(size_t) pair.first];
if (b > budget_bytes - stage_lend_reserve) {
stage_lend_reserve = budget_bytes;
break;
}
stage_lend_reserve += b;
}
if (stage_lend_reserve >= budget_bytes) break;
}
}
const uint64_t comp_cap = budget_bytes - stage_lend_reserve;
uint64_t at = 0;
int64_t held = 0;
if (rank != nullptr)
@@ -3990,12 +4014,12 @@ bool FileExpertSource::pin_cache_complement(
const size_t i = (size_t) pr.first * (size_t) n_expert_ + (size_t) pr.second;
if (offsets[i] == kNoComplement || ranked[i] != kNoComplement) continue; // on a GPU, or twice
const uint64_t b = layer_blob_bytes_[(size_t) pr.first];
if (b > budget_bytes - at) continue;
if (b > comp_cap - at) continue;
ranked[i] = at;
at += b;
++held;
}
if (what_fits && held == 0) { // #467: nothing to keep - the caller's plain mmap fallback, not an empty copy
if (what_fits && held == 0 && stage_lend_reserve == 0) { // #467: nothing to keep - the caller's plain mmap fallback, not an empty copy
err = "FileExpertSource: available memory cannot hold any expert of the complement";
return false;
}
+35 -1
View File
@@ -11687,7 +11687,41 @@ int main(int argc, char** argv) {
std::vector<std::vector<std::pair<int32_t, int32_t>>> stage_lends;
decide_split_ring(); // the ring the prompt path really uses, BEFORE bytes_needed sizes the regions below
if (pf_borrow && d_res != nullptr && o.prefill_chunk > 0 && multi_gpu && !stages.empty()) {
const int64_t bound = o.prefill_auto ? auto_ceiling : o.prefill_chunk;
int64_t bound = o.prefill_auto ? auto_ceiling : o.prefill_chunk;
if (o.prefill_auto && bound > 256) {
// #6583: size the split lend regions for the largest chunk the auto scan can actually pick
// (every stage within g_lend_keep and kAutoLendPct), rather than over-reserving for an unreachable
// auto_ceiling (e.g. 32768 when the cards fit 8192).
auto split_auto_fits = [&](int64_t c) -> bool {
if (c > 8192 && (c > o.prefill_auto_max || c > o.max_context)) return false;
if (o.prefill_auto_cap > 0 && c > o.prefill_auto_cap) return false;
for (size_t i = 0; i <= stages.size(); ++i) {
const strata::core::ExpertCache& xc = i == 0 ? xcache : stages[i - 1]->cache;
const strata::core::SessionState& ses = i == 0 ? ss : stages[i - 1]->ss;
const int64_t dev = i == 0 ? -1 : stages[i - 1]->dev;
const int64_t lb = i == 0 ? 0 : stages[i - 1]->lb;
const int64_t le = i == 0 ? split_at[0] : stages[i - 1]->le;
if (xc.slots() <= 0 || le <= lb) continue;
uint64_t need = 0;
{
const strata::core::OnDevice on((int) dev);
need = strata::prefill::Prefill::bytes_needed(g, ses, c, srcp != nullptr);
}
const int64_t k = strata::program::split_lend_slots(
xc.slots(), xc.bytes(), xc.slot_offsets(),
(uint64_t) strata::kernels::cpu::expert_layout().max_blob, need);
if (k <= 0 || k + g_lend_keep > xc.slots() || k * 100 > kAutoLendPct * xc.slots()) return false;
}
return true;
};
int64_t lo = 1, hi = bound / 256, best = 0;
while (lo <= hi) {
const int64_t mid = lo + (hi - lo) / 2;
if (split_auto_fits(mid * 256)) { best = mid * 256; lo = mid + 1; }
else hi = mid - 1;
}
if (best >= 256) bound = best;
}
static const bool own_ok = [] {
const char* v = std::getenv("STRATA_SPLIT_OWN_BUFFERS");
return v == nullptr || v[0] != '0';
@@ -203,6 +203,41 @@ void test_split_without_regions_keeps_nothing() {
require(!rig.src.has_resident(l, e), "an expert neither in a cache's complement region entered the copy");
}
// #6583: under a --resident-budget-gib smaller than (complement + stage lend regions), the copy reserves
// room for the borrowed stage lend regions before filling the coldest complement tail, so every borrowed
// slot stays RAM-backed instead of reading from SSD on every prompt chunk.
void test_split_lend_region_budget_with_complement() {
Rig rig("split lend region budget with complement");
std::string err;
// Rebuild stage0 and stage1 holding only 3 of the 4 experts per layer (e = 0,1,2), leaving (0,3) and (1,3)
// in the complement: rank puts (0,3) hot and (1,3) cold.
strata::core::ExpertCache s0, s1;
require(s0.open(3, kLayers, kExperts, rig.blob, err), err);
require(s1.open(3, kLayers, kExperts, rig.blob, err), err);
for (int64_t e = 0; e < 3; ++e) {
const int32_t a0 = s0.admit(0, e);
require(a0 >= 0 && s0.fill_slot_blocking(a0, rig.truth[(size_t) e].data(), err), err);
const int32_t a1 = s1.admit(1, e);
require(a1 >= 0 && s1.fill_slot_blocking(a1, rig.truth[(size_t) kExperts + e].data(), err), err);
}
std::vector<std::pair<int32_t, int32_t>> s1_pairs{{1, 0}, {1, 1}, {1, 2}};
std::vector<std::pair<int32_t, int32_t>> rank{{0, 3}, {1, 3}};
// Each stage borrows 2 slots (4 blobs total), plus 2 complement blobs = 6 blobs; budget is 5 blobs.
rig.src.stage_lend_regions({{{0, 2}, {0, 1}}, {{1, 2}, {1, 1}}});
const uint64_t budget = 5 * (uint64_t) rig.blob;
require(rig.src.pin_cache_complement(s0, err, false, s1_pairs, -1, 0, budget, &rank), err);
require(rig.src.resident_bytes() == budget, "copy did not fill the 5-blob budget");
require(rig.src.resident_lent_slots() == 4, "all 4 borrowed stage lend slots must be kept in RAM");
require(rig.src.has_resident(0, 2) && rig.src.has_resident(0, 1) &&
rig.src.has_resident(1, 2) && rig.src.has_resident(1, 1),
"borrowed lend slots were crowded out by the cold complement tail");
require(rig.src.has_resident(0, 3), "hottest complement expert should fit in the remaining 1 blob");
require(!rig.src.has_resident(1, 3), "coldest complement expert should be the one left on file fallback");
}
} // namespace
int main() {
@@ -210,6 +245,7 @@ int main() {
test_split_lend_region_served_from_ram();
test_split_lend_region_budget();
test_split_without_regions_keeps_nothing();
test_split_lend_region_budget_with_complement();
std::cout << "split_lend_complement_test: PASS\n";
return 0;
} catch (const std::exception& error) {