diff --git a/engine/include/strata/core/low_vram_rules.hpp b/engine/include/strata/core/low_vram_rules.hpp index 0f38621cd9d..dd5f3006ef5 100644 --- a/engine/include/strata/core/low_vram_rules.hpp +++ b/engine/include/strata/core/low_vram_rules.hpp @@ -28,6 +28,7 @@ constexpr uint64_t kLowVram8GiB = (8ull << 30) + (512ull << 20); // 8.5 GiB ( constexpr uint64_t kLowVram10GiB = (9ull << 30) + (512ull << 20); // 9.5 GiB floor (covers 10/12 GB cards: 9.8-12.0 GiB) constexpr uint64_t kLowVram12GiB = (12ull << 30) + (512ull << 20); // 12.5 GiB (covers 10/12 GB cards: 9.8-12.0 GiB) constexpr uint64_t kLowVram13GiB = 13ull << 30; // 13.0 GiB ceiling for low-VRAM fastpaths +constexpr uint64_t kLowVram16GiB = 16ull << 30; // 16.0 GiB: the largest card the tiny-cache pinning was measured on constexpr uint64_t kWddm16GiB = 15ull << 30; // 15.0 GiB floor (16 GB cards report ~15.9 GiB) constexpr int kDefaultReserveMib = 700; @@ -102,9 +103,16 @@ inline int low_vram_adapt_swaps(int cur_swaps, bool swaps_given, int64_t residen } /// Number of top-ranked expert-profile.bin slots to pin against eviction on tiny caches (< 25% resident). +/// `total_vram_bytes`: the card's memory. The pinning was measured on 8-16 GiB consumer cards, where the +/// cache holds a small share AND PCIe swaps are dear; a 24 GiB card with a large MoE also falls under 25% +/// resident but has a large host-RAM copy, and pinning 25% of its slots there fights the mid-coverage +/// adaptive rule (#4870) and cost 4.5% decode on an RTX 4090 with UD-Q4_K_XL (engine 0.1.66). So it stays +/// off above 16 GiB; 0 (unknown) keeps the old fraction-only rule. /// `pin_env` is STRATA_CACHE_PIN_FRAC (nullptr/empty = default 0.25; "0" = off; otherwise clamped 0.0..0.75). -inline int64_t low_vram_pin_slots(int64_t resident_slots, int64_t total_experts, const char* pin_env) noexcept { +inline int64_t low_vram_pin_slots(int64_t resident_slots, int64_t total_experts, uint64_t total_vram_bytes, + const char* pin_env) noexcept { if (resident_slots <= 0 || total_experts <= 0) return 0; + if (total_vram_bytes > kLowVram16GiB) return 0; double frac = 0.0; if (pin_env != nullptr && pin_env[0] != '\0') { frac = std::max(0.0, std::min(0.75, std::atof(pin_env))); diff --git a/engine/src/core/low_vram_rules_test.cpp b/engine/src/core/low_vram_rules_test.cpp index b092af60af4..e4ba0052643 100644 --- a/engine/src/core/low_vram_rules_test.cpp +++ b/engine/src/core/low_vram_rules_test.cpp @@ -33,14 +33,17 @@ int main() { CHECK(low_vram_adapt_swaps(12, false, 1400, kTotal) == 12); // never raises smaller value CHECK(low_vram_adapt_swaps(0, false, 1400, kTotal) == 0); - // 3. Zipf-head profile pinning on tiny caches (< 25% resident) - CHECK(low_vram_pin_slots(0, kTotal, nullptr) == 0); - CHECK(low_vram_pin_slots(1400, kTotal, nullptr) == 350); // 25% of 1400 - CHECK(low_vram_pin_slots(2560, kTotal, nullptr) == 640); // 25% of 2560 - CHECK(low_vram_pin_slots(7000, kTotal, nullptr) == 0); // >= 25% resident: off by default - CHECK(low_vram_pin_slots(1400, kTotal, "0") == 0); // explicit off - CHECK(low_vram_pin_slots(1400, kTotal, "0.5") == 700); // explicit 50% - CHECK(low_vram_pin_slots(1400, kTotal, "0.99") == 1050); // clamped to 75% + // 3. Zipf-head profile pinning on tiny caches (< 25% resident), low-VRAM cards only (<= 16 GiB) + CHECK(low_vram_pin_slots(0, kTotal, 12 * GiB, nullptr) == 0); + CHECK(low_vram_pin_slots(1400, kTotal, 8 * GiB, nullptr) == 350); // 5.7% (8 GB RTX 3070) + CHECK(low_vram_pin_slots(2560, kTotal, 12 * GiB, nullptr) == 640); // 10.4% (12 GB RTX 4070 Ti / 5070) + CHECK(low_vram_pin_slots(4500, kTotal, 16 * GiB, nullptr) == 1125); // 18.3% (16 GB) + CHECK(low_vram_pin_slots(5000, kTotal, 24 * GiB, nullptr) == 0); // 20.3% on a 24 GB card: off (fights #4870; 4090 UD-Q4_K_XL lost 4.5% decode) + CHECK(low_vram_pin_slots(1400, kTotal, 0, nullptr) == 350); // unknown VRAM: the old fraction-only rule + CHECK(low_vram_pin_slots(7000, kTotal, 12 * GiB, nullptr) == 0); // >= 25% resident: off by default + CHECK(low_vram_pin_slots(1400, kTotal, 12 * GiB, "0") == 0); // explicit off + CHECK(low_vram_pin_slots(1400, kTotal, 12 * GiB, "0.5") == 700); // explicit 50% + CHECK(low_vram_pin_slots(1400, kTotal, 12 * GiB, "0.99") == 1050); // clamped to 75% // 4. Auto STRATA_L3_PF=1 (<= 13 GiB, > 50% host experts, >= 8 workers) CHECK(l3_pf_auto_default(nullptr, 12 * GiB, 2560, kTotal, 9)); // 12 GB + 9 workers: ON diff --git a/engine/src/program/generate.cpp b/engine/src/program/generate.cpp index 65d0b054d6a..279da16f66f 100644 --- a/engine/src/program/generate.cpp +++ b/engine/src/program/generate.cpp @@ -14882,7 +14882,10 @@ int main(int argc, char** argv) { } std::vector pinned_pair; if (!profile.empty() && !host_res.empty() && live_gpu_slots > 0 && !all_experts_resident) { - const int64_t pin_n = strata::core::low_vram_pin_slots(live_gpu_slots, total_expert_pairs, std::getenv("STRATA_CACHE_PIN_FRAC")); + size_t fb_pin = 0, tb_pin = 0; + if (cudaMemGetInfo(&fb_pin, &tb_pin) != cudaSuccess) { (void) cudaGetLastError(); tb_pin = 0; } + const int64_t pin_n = strata::core::low_vram_pin_slots(live_gpu_slots, total_expert_pairs, + (uint64_t) tb_pin, std::getenv("STRATA_CACHE_PIN_FRAC")); if (pin_n > 0) { pinned_pair.assign((size_t) total_expert_pairs, 0); int64_t pinned_ct = 0;