arena: pin by need (STRATA_PIN_BY_NEED, opt-in) + PCIe share floor (STRATA_PCIE_MIN1, opt-in)

On Windows cudaHostRegister of the whole arena fails (the WDDM shared budget is about half the RAM) and the
sliced fallback registers the first ~30 GiB: with Qwen3.8-Flash-Next IQ3_S layers 0-31, so layers 32-47 never
get a PCIe share, while part of the registered prefix holds GPU-resident experts that DMA never reads.

STRATA_PIN_BY_NEED=1 (the arena on one GPU):
- every blob gets a page-aligned stride (PadMap in pinned.hpp, ArenaExpertSource::arena_off, +0.15 % RAM), so any
  set of blobs can be registered exactly; the loaders place each blob at its padded offset
- the expert profile's pairs past the estimated GPU-resident count are merged into ranges by rank and registered
  before any page is touched, up to STRATA_ARENA_PIN_GIB (29 GiB by default on Windows) or the first refusal
- pinned() / device_alias() / pcie_layer() work on the ranges (a blob is pinned only if wholly inside one);
  lock_resident_ranges() keeps the holes between them resident
- one start-up line: "pin by need: N ranges, X GiB pinned, Y % of the non-resident experts"

STRATA_PCIE_MIN1=1: the PCIe share (nmiss * pcie_num) >> 8 rounds to 0 at 1-2 misses per layer; with two or more
misses one goes over PCIe (when its blob is pinned).

RTX 3090 + Ryzen 5 5600X: 28.96 GiB registered = 92 % of the non-resident experts, all 48 layers with a PCIe
share; prompt read +5-7 % at 56K-117K tokens, decode level alone and +2-3 % with --adapt-async 1 and
--pcie-frac 0.6 (bench/results/2026-10-11-community-rtx3090-5600x-arena-pin-by-need, docs/DETAILS.md).

Both opt-in; without them the layout, the loaders and the sliced pin are those of v0.1.42.
This commit is contained in:
RubenTorney authored and Niko1221 committed 2026-10-13 14:50:46 +02:00
1 parent 1aa0abf63a
commit ab6bce05de
16 files changed
+1052 -16

No files matched your search

+83
View File
@@ -4914,11 +4914,94 @@ int main(int argc, char** argv) {
else if (pin_wddm_cap)
std::fprintf(stderr, "strata generate: multi-GPU under WDDM: at most 8 GiB of the expert arena is pinned "
"(STRATA_ARENA_PIN_GIB changes it)\n");
// ---- STRATA_PIN_BY_NEED=1 (default off: the prefix pinning above, byte-identical). WDDM does not let the
// pin grow past ~RAM/2, and the prefix spends it on layers 0-31 whatever the GPU holds: layers 32-47 stay 100 %
// CPU while ~10 GiB of pinned blobs sit in GPU-resident experts no DMA ever reads. Pinning is registered
// BEFORE the arena's pages are touched (pinned.cu), so the set must be chosen here, before arena_src.open: the
// profile is already read, and the GPU cache's size - opened much later - is ESTIMATED from the same free-VRAM
// reading its auto sizing uses (the weights, session and drafter are loaded by now, host registration takes no
// VRAM): the first R ranked pairs are taken as GPU-resident, and the rest (then the unranked pairs, in layer
// 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=<n> forces R. Single GPU, profile given, no helper caches only.
std::vector<std::pair<int32_t, int32_t>> pin_cand; // the non-resident estimate, by decreasing rank
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"
: std::any_of(o.expert_cache_remote.begin(), o.expert_cache_remote.end(),
[](int n) { return n > 0; }) ? "not with helper caches on other GPUs"
: nullptr;
if (off_why != nullptr) {
std::fprintf(stderr, "strata generate: pin by need: off (%s); the prefix pinning stays\n", off_why);
} else {
const auto& pl = strata::kernels::cpu::expert_layout();
size_t skip = profile.size();
if (const char* sk = std::getenv("STRATA_PIN_BY_NEED_SKIP"); sk != nullptr && sk[0] != '\0') {
skip = std::min<size_t>(profile.size(), (size_t) std::max<long long>(std::atoll(sk), 0));
} else {
// the bytes the GPU cache will hold: --expert-cache N slots of the largest blob, or what auto sizing
// finds free (the same reserve terms as "expert cache auto" below, without its small-card rescue,
// which only ever grants MORE slots)
uint64_t cap_bytes;
if (o.expert_cache > 0) {
cap_bytes = (uint64_t) o.expert_cache * pl.max_blob;
} else {
const int64_t pf_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;
int64_t mtp_b = (!o.mtp.empty() && native_head.loaded())
? (int64_t) mtp.bind_bytes(native_head.row_bytes(), n_vocab) : 0;
if (stages.empty())
for (const auto& d : slot_mtp) mtp_b += (int64_t) d->bind_bytes(native_head.row_bytes(), n_vocab);
const int64_t pipe_b = o.pipeline_windows <= 0 ? 0
: (kPipeWindowMib << 20) + (o.pipeline_windows >= 2 ? 2 * (int64_t) gdn_snapshot_bytes(ss) : 0);
const int64_t room = (int64_t) strata::core::device_free_bytes() -
((((int64_t) o.vram_reserve_mib + pf_mib) << 20) + mtp_b + pipe_b);
cap_bytes = room > 0 ? (uint64_t) room : 0;
}
cap_bytes = cap_bytes / 100 * 98;
uint64_t used = 0;
skip = 0;
for (const auto& pr : profile) {
const uint64_t b = pl.native ? (pl.blob_bytes(pr.first) + 255) / 256 * 256 : (uint64_t) pl.max_blob;
if (used + b > cap_bytes) break;
used += b;
++skip;
}
}
std::vector<uint8_t> seen((size_t) (g.n_layers * g.n_expert), 0);
for (const auto& pr : profile)
if (pr.first >= 0 && pr.first < g.n_layers && pr.second >= 0 && pr.second < g.n_expert)
seen[(size_t) (pr.first * g.n_expert + pr.second)] = 1;
pin_cand.assign(profile.begin() + (long) skip, profile.end());
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);
std::fprintf(stderr, "strata generate: pin by need: %zu of the %zu ranked pairs taken as GPU-resident "
"(STRATA_PIN_BY_NEED_SKIP=<n> sets it), %zu candidates to pin\n",
skip, profile.size(), pin_cand.size());
arena_src.set_pin_priority(pin_cand);
}
}
if (!arena_src.open(o.pack, g.n_layers, g.n_expert, /*threads=*/6, err, pin_limit,
o.shared_expert_arena)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
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) {
const uint64_t b = pl.blob_bytes(pr.first);
cand_b += b;
if (arena_src.pinned(pr.first, pr.second)) pin_b += b;
}
int64_t pcie_layers = 0;
for (int64_t l = 0; l < g.n_layers; ++l) pcie_layers += arena_src.pcie_layer(l) ? 1 : 0;
std::fprintf(stderr, "strata generate: pin by need: %zu ranges, %.2f GiB pinned, %.1f %% of the non-resident "
"experts (%lld of %lld layers have a PCIe share)\n",
arena_src.pin_ranges(), (double) pin_b / 1073741824.0,
cand_b > 0 ? 100.0 * (double) pin_b / (double) cand_b : 0.0, (long long) pcie_layers,
(long long) g.n_layers);
}
if (!arena_src.ram_warning().empty()) // #633: said before the load's numbers, which it explains
std::fprintf(stderr, "strata generate: WARNING: %s\n", arena_src.ram_warning().c_str());
std::fprintf(stderr, "strata generate: expert arena: %s\n", arena_src.note().c_str());