diff --git a/engine/src/inkling2/cuda/ink_cuda_backend.cu b/engine/src/inkling2/cuda/ink_cuda_backend.cu index 9c76994d2e..7683a9d7db 100644 --- a/engine/src/inkling2/cuda/ink_cuda_backend.cu +++ b/engine/src/inkling2/cuda/ink_cuda_backend.cu @@ -151,6 +151,7 @@ struct VramPlan { bool c64_host_kv = false; int init_glob = 0; size_t min_kv_bytes = 0; + size_t swa_host_bytes = 0; // the sliding-window rings the plan did not count because COMPACT keeps them in host memory (16 slots and more) size_t dspark_pad = 0; size_t ws_pad = 0; size_t min_reserve_bytes = 0; @@ -198,7 +199,11 @@ static VramPlan plan_vram_reserve(const VramPlanIn& in) { : ((in.kv_offload_mode == 0 && in.total_vram <= (13ull << 30)) ? std::min(std::max(256, opts.max_ctx), 2048) : std::max(256, std::min(std::max(256, opts.max_ctx), (in.max_slots == 1) ? 8192 : 4096))); - p.min_kv_bytes = (p.c64_host_kv ? 0ull : (size_t)40 * in.max_slots * p.swa_r * p.per_visit_pos) + // The rings of the 40 sliding-window visits are in mapped host memory from 16 slots on whenever the table's COMPACT runs (the runner: swa_in_host = compact_vram && slots >= 16), with or without + // the C64_SPEC part: the plan must not hold 5.7 GB of VRAM back for them at 64 slots (the 12 / 8 GB hybrid rows have no C64_SPEC and kept 0 experts on the card: PLAN-0225 item 23). + const bool swa_host = (in.max_slots >= 16) && (p.c64_host_kv || opt5060.compact_vram); + p.swa_host_bytes = swa_host ? (size_t)40 * in.max_slots * p.swa_r * p.per_visit_pos : 0ull; + p.min_kv_bytes = (swa_host ? 0ull : (size_t)40 * in.max_slots * p.swa_r * p.per_visit_pos) + (size_t)strata::ink::kInkVisits * in.max_slots * 3 * strata::ink::kInkSconvCh * 4 + (size_t)8 * in.max_slots * p.init_glob * p.per_visit_pos; // Include O(1) prefill ws (~114 MiB) + DSpark (~224 MiB when enabled) + activation ws (WDDM floor added inside loader!) @@ -705,6 +710,7 @@ public: } strata::ink::sm120_plan_slots_ref().store(max_slots_); + strata::ink::sm120_plan_swa_host_bytes_ref().store(plan.swa_host_bytes); // Load .strata v1.1 / .gguf (Unsloth + ggml-org) / HF safetensors trunk + experts (Single-GPU, EP2, Layer-Split, or 4-tier GPU+RAM+SSD!) // A card with too little free memory for even the shared layers (the trunk) is a refusal with a message, not the fatal GPU error that ends the engine (PLAN-0225 item 24). bool loaded_ok = false; diff --git a/engine/src/inkling2/cuda/ink_runner.cuh b/engine/src/inkling2/cuda/ink_runner.cuh index 2773c748ce..fc4f3622af 100644 --- a/engine/src/inkling2/cuda/ink_runner.cuh +++ b/engine/src/inkling2/cuda/ink_runner.cuh @@ -365,6 +365,7 @@ struct InkCudaModel { double t0 = now_s(); const auto& opt5060_ld = ink_rtx5060ti_cfg(primary_gpu); + const bool compact_was_on_pre = opt5060_ld.compact_vram; // the plan was made with COMPACT's memory plan; a refusal below changes the table (PLAN-0225 item 23) size_t vfree_pre = 0, vtot_pre = 0; INK_CK(ink_cuda_mem_info(&vfree_pre, &vtot_pre)); bool do_compact_vram = opt5060_ld.compact_vram && !ssd_experts && vram_experts_gb <= 0.0 && @@ -424,7 +425,7 @@ struct InkCudaModel { // PLAN-0225 item 17: a 16 GB row (COMPACT without the hybrid) whose card cannot hold every expert because a display or another program holds part of it: the experts are short by // a few hundred MiB, not by the trunk. COMPACT_HYBRID (the 12 GB row's set) then runs instead of the old path (5060 Ti 16 GB, SQ-20 v2, 800 MiB held: decode 51 -> 108 tok/s on chat with // DFlash 2, 48 -> 82 on code), when at least 3 GiB of experts still fit next to the compact trunk (below that the old path stays) and the run is one slot. - if (do_compact_vram && !opt5060_ld.compact_hybrid && all_sf4_or_mx8_pre && plan_slots_pre == 1 && vfree_pre < est_trunk + est_sec4 + compact_margin && + if (do_compact_vram && !opt5060_ld.compact_hybrid && all_sf4_or_mx8_pre && (plan_slots_pre == 1 || plan_slots_pre >= 16) && vfree_pre < est_trunk + est_sec4 + compact_margin && vfree_pre >= est_trunk + (3ull << 30) && sm120_compact_to_hybrid(primary_gpu)) { std::fprintf(stderr, "[Ink5060Ti] COMPACT needs %zu MiB for every expert, %zu MiB are free (a display or another program holds the rest): COMPACT_HYBRID runs instead of the old path " "(the compact trunk, the experts that fit on the card, the rest in RAM; PACK / PREFILL / PLD / CORE_GRAPH / C64_SPEC off as on the 12 GB row; STRATA_INK_SM120_COMPACT_HYBRID=0 refuses COMPACT here)\n", @@ -470,6 +471,15 @@ struct InkCudaModel { } } } + if (compact_was_on_pre && !ink_rtx5060ti_cfg(primary_gpu).compact_vram) { + // COMPACT was refused after the VRAM plan counted the sliding-window rings of 16 slots and more as host memory: the old path puts them on the card, so the experts are placed with them held back + // (5060 Ti 16 GB, SQ-20 v2, 64 slots, 800 MiB held by a display: 206 MiB short of COMPACT, 1,884 experts placed, then "not enough free GPU memory for the working buffers" at every chunk size) + const size_t swa_planned = sm120_plan_swa_host_bytes_ref().load(); + if (swa_planned > 0) { + extra_reserve_bytes += swa_planned; + std::fprintf(stderr, "[InkCUDA] COMPACT refused: the sliding-window K/V of %d slots (%zu MiB) stays on the card on the old path, held back from the experts\n", sm120_plan_slots_ref().load(), swa_planned >> 20); + } + } if (do_compact_vram) { // Opt-in RTX 5060 Ti 16 GB 100%-VRAM Compact Trunk Loader: // - embed.weight (883 MiB BF16) -> pinned mapped host RAM (0 MiB VRAM!) diff --git a/engine/src/inkling2/cuda/ink_sm120_cfg.cuh b/engine/src/inkling2/cuda/ink_sm120_cfg.cuh index fc306d1aac..541bed8b84 100644 --- a/engine/src/inkling2/cuda/ink_sm120_cfg.cuh +++ b/engine/src/inkling2/cuda/ink_sm120_cfg.cuh @@ -388,6 +388,12 @@ inline std::atomic& sm120_plan_slots_ref() noexcept { static std::atomic v{1}; return v; } +/// PLAN-0225 item 23: the bytes of sliding-window K/V that the VRAM plan left out because COMPACT keeps them in host memory from 16 slots on (set by the backend's plan, 0 when it counted them). When +/// the loader then refuses COMPACT (the card is a few hundred MiB short of every expert) the old path puts these rings on the card, and the experts must be placed with them reserved. +inline std::atomic& sm120_plan_swa_host_bytes_ref() noexcept { + static std::atomic v{0}; + return v; +} inline void sm120_compact_refuse(int dev) noexcept { Sm120Cfg& s = const_cast(sm120_cfg(dev)); // static, non-const storage underneath