diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index ddf8a18a..cf517690 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -953,8 +953,11 @@ public: /// priority - the profile's ranked pairs the GPU cache will NOT hold, then the unranked ones. open() merges /// neighbours into byte ranges and registers only those (PinnedArena::ByNeed); pinned()/device_alias()/pcie_layer() /// then follow the registered ranges instead of a prefix. Never called: the old prefix pinning, unchanged. - void set_pin_priority(std::vector> ranked) { + /// `n_first`: the first n_first pairs are the non-resident estimate; the rest (the pairs taken as GPU-resident, which + /// only get what the cap leaves over) never merge into a run with them, so their ranges come after all of the first. + void set_pin_priority(std::vector> ranked, size_t n_first = (size_t) -1) { pin_pairs_ = std::move(ranked); + pin_first_ = n_first; pin_by_need_ = true; } /// Pin by need, after open(): the registered ranges (0 when the prefix rule, or nothing, is in force) @@ -1000,6 +1003,7 @@ private: /// where blob (layer, expert) starts in the arena: the ONE address function of this class uint64_t arena_off(int64_t layer, int64_t expert) const; std::vector> pin_pairs_; + size_t pin_first_ = (size_t) -1; // pin_pairs_[0, pin_first_) are the first tier (see set_pin_priority) std::vector pin_start_, pin_end_; std::vector pin_dev_; std::vector layer_pcie_; diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index eb6de98c..4878c111 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -4619,7 +4619,7 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 if (by_need) { // every blob starts on a page and owns its whole stride (pad included), so a run of adjacent blobs is one exact // range: [first start, last start + stride). The best rank of a run's members is its priority. - struct Ext { uint64_t off, len; size_t rank; }; + struct Ext { uint64_t off, len; size_t rank; int tier; }; std::vector ex; ex.reserve(pin_pairs_.size()); std::vector seen((size_t) (n_layers * n_expert), 0); @@ -4627,12 +4627,12 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 const int64_t l = pin_pairs_[i].first, e = pin_pairs_[i].second; if (l < 0 || l >= n_layers || e < 0 || e >= n_expert || seen[(size_t) (l * n_expert + e)]) continue; seen[(size_t) (l * n_expert + e)] = 1; - ex.push_back({arena_off(l, e), pad_.stride[(size_t) l], i}); + ex.push_back({arena_off(l, e), pad_.stride[(size_t) l], i, i < pin_first_ ? 0 : 1}); } std::sort(ex.begin(), ex.end(), [](const Ext& a, const Ext& b) { return a.off < b.off; }); std::vector merged; for (const Ext& x : ex) { - if (!merged.empty() && merged.back().off + merged.back().len == x.off) { + if (!merged.empty() && merged.back().off + merged.back().len == x.off && merged.back().tier == x.tier) { merged.back().len += x.len; merged.back().rank = std::min(merged.back().rank, x.rank); } else { diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 2f54d7cc..ba00f94d 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4924,7 +4924,8 @@ int main(int argc, char** argv) { // order) are what the arena pins, hottest first, until the driver refuses. R is under-estimated a little // (x0.98): a resident pair pinned by mistake costs a few MiB, a hot non-resident one skipped costs DMA. // STRATA_PIN_BY_NEED_SKIP= forces R. Single GPU, profile given, no helper caches only. - std::vector> pin_cand; // the non-resident estimate, by decreasing rank + std::vector> pin_cand; // the non-resident estimate, by decreasing rank, then the resident one + size_t pin_nonres = 0; // (how many of them are the non-resident estimate) if (const char* pn = std::getenv("STRATA_PIN_BY_NEED"); pn != nullptr && pn[0] != '\0' && pn[0] != '0') { const char* off_why = profile.empty() ? "it needs --expert-profile (the ranking)" : multi_gpu || o.peer_device >= 1 ? "not with a layer split or a peer GPU" @@ -4975,10 +4976,14 @@ int main(int argc, char** argv) { for (int64_t l = 0; l < g.n_layers; ++l) for (int64_t e = 0; e < g.n_expert; ++e) if (!seen[(size_t) (l * g.n_expert + e)]) pin_cand.emplace_back((int32_t) l, (int32_t) e); + pin_nonres = pin_cand.size(); // (the report counts these) + // what the cap leaves over goes to the pairs taken as GPU-resident (last in line): the adaptive tier + // swaps them out as the conversation moves, and the prompt read streams whatever is not resident then + pin_cand.insert(pin_cand.end(), profile.begin(), profile.begin() + (long) skip); std::fprintf(stderr, "strata generate: pin by need: %zu of the %zu ranked pairs taken as GPU-resident " "(STRATA_PIN_BY_NEED_SKIP= sets it), %zu candidates to pin\n", - skip, profile.size(), pin_cand.size()); - arena_src.set_pin_priority(pin_cand); + skip, profile.size(), pin_nonres); + arena_src.set_pin_priority(pin_cand, pin_nonres); } } if (!arena_src.open(o.pack, g.n_layers, g.n_expert, /*threads=*/6, err, pin_limit, @@ -4989,7 +4994,8 @@ int main(int argc, char** argv) { if (!pin_cand.empty()) { const auto& pl = strata::kernels::cpu::expert_layout(); uint64_t cand_b = 0, pin_b = 0; - for (const auto& pr : pin_cand) { + for (size_t ci = 0; ci < pin_nonres; ++ci) { + const auto& pr = pin_cand[ci]; const uint64_t b = pl.blob_bytes(pr.first); cand_b += b; if (arena_src.pinned(pr.first, pr.second)) pin_b += b;