diff --git a/engine/include/strata/core/ink_chunk.hpp b/engine/include/strata/core/ink_chunk.hpp index 74b14b4983..ff5e667e92 100644 --- a/engine/include/strata/core/ink_chunk.hpp +++ b/engine/include/strata/core/ink_chunk.hpp @@ -20,9 +20,13 @@ constexpr double kInkChunkBaseMiB = 1460.0; // runner + K/V at chunk 512 (R constexpr double kInkChunkMiBPerToken = 0.87; // working memory per chunk token above 512 (4090: 1822 / 2710 / 4558 MiB at 1024 / 2048 / 4096, 0.86 MiB per token) constexpr double kInkChunkMarginMiB = 1500.0; // left free after the runner: K/V growth, the drafter, the desktop -// Memory (MiB) that must be free after the experts are placed for `chunk` to be chosen. -inline double ink_chunk_need_mib(int chunk) { - return kInkChunkBaseMiB + kInkChunkMiBPerToken * (chunk > kInkChunkDefault ? chunk - kInkChunkDefault : 0) + kInkChunkMarginMiB; +// Memory (MiB) that must be free after the experts are placed for `chunk` to be chosen. kv_grow_mib: the global K/V that still has to be +// allocated for every slot to reach the context (slots x (max_ctx - the window allocated at start) x the bytes of a position): working memory the +// chunk must not take, or the K/V cannot grow on the card and its tail goes to host RAM (RTX 4090, UD-Q4_K_XL, 2 slots at 32K: decode at 30K tokens +// fell from 91 to 36 tok/s, 3 slots 14.7, with the K/V on the card 87). +inline double ink_chunk_need_mib(int chunk, double kv_grow_mib = 0.0) { + return kInkChunkBaseMiB + kInkChunkMiBPerToken * (chunk > kInkChunkDefault ? chunk - kInkChunkDefault : 0) + kInkChunkMarginMiB + + (kv_grow_mib > 0.0 ? kv_grow_mib : 0.0); } // Slots that share one step: the more of them, the smaller the prefill share one step may carry (a decoding slot waits for the whole step). @@ -36,11 +40,11 @@ inline int ink_chunk_cap_for_slots(int slots) { } // The largest of 2048 / 1024 / 512 that fits `free_after_load_mib` and the slot cap; 512 when the memory is not known (< 0). -inline int ink_auto_chunk(double free_after_load_mib, int slots) { +inline int ink_auto_chunk(double free_after_load_mib, int slots, double kv_grow_mib = 0.0) { if (free_after_load_mib < 0.0) return kInkChunkDefault; const int cap = ink_chunk_cap_for_slots(slots); for (int c : {2048, 1024}) { - if (c <= cap && free_after_load_mib >= ink_chunk_need_mib(c)) return c; + if (c <= cap && free_after_load_mib >= ink_chunk_need_mib(c, kv_grow_mib)) return c; } return kInkChunkDefault; } diff --git a/engine/src/inkling2/cuda/ink_cuda_backend.cu b/engine/src/inkling2/cuda/ink_cuda_backend.cu index 5934aa26a3..a234a2babf 100644 --- a/engine/src/inkling2/cuda/ink_cuda_backend.cu +++ b/engine/src/inkling2/cuda/ink_cuda_backend.cu @@ -281,9 +281,12 @@ public: if (opts.chunk_auto && !model_.ls_active) { const char* auto_env = std::getenv("STRATA_INK_CHUNK_AUTO"); if (!(auto_env && std::atoi(auto_env) == 0)) { - chunk_try = std::max(chunk_try, strata::core::ink_auto_chunk(led_loaded, max_slots_)); - std::fprintf(stderr, "[InkCUDA] prefill chunk %d (auto: %.0f MiB free after the experts, %d slot(s); --chunk N sets it)\n", - chunk_try, led_loaded, max_slots_); + // the K/V still to be allocated for every slot to reach the context stays free of the chunk's working memory (kv offload on: it is host RAM) + const double kv_grow_mib = (kv_offload_mode == 1) ? 0.0 + : (double)8 * max_slots_ * std::max(0, std::max(256, opts.max_ctx) - init_glob) * (double)per_visit_pos / 1048576.0; + chunk_try = std::max(chunk_try, strata::core::ink_auto_chunk(led_loaded, max_slots_, kv_grow_mib)); + std::fprintf(stderr, "[InkCUDA] prefill chunk %d (auto: %.0f MiB free after the experts, %.0f MiB of K/V still to grow, %d slot(s); --chunk N sets it)\n", + chunk_try, led_loaded, kv_grow_mib, max_slots_); } } for (;;) { diff --git a/engine/tests/core/ink_chunk_test.cpp b/engine/tests/core/ink_chunk_test.cpp index 125823a2ce..67874866e3 100644 --- a/engine/tests/core/ink_chunk_test.cpp +++ b/engine/tests/core/ink_chunk_test.cpp @@ -39,6 +39,14 @@ int main() { CHECK(ink_auto_chunk(12000.0, 64) == 512); CHECK(ink_auto_chunk(3500.0, 8) == 1024); CHECK(ink_auto_chunk(2000.0, 8) == 512); + // the K/V that still has to grow stays free of the chunk (RTX 4090, UD-Q4_K_XL, 5255 MiB free after the experts, 32K context, fp16: 896 MiB per slot to grow): + // 1 slot keeps 2048 (decode at 30K tokens 87.7 tok/s), 2 slots 1024, 3 slots 512 (with the K/V on the card 87; spilled to host 36 and 14.7 tok/s) + CHECK(ink_auto_chunk(5255.0, 1, 896.0) == 2048); + CHECK(ink_auto_chunk(5255.0, 2, 1792.0) == 1024); + CHECK(ink_auto_chunk(5255.0, 3, 2688.0) == 512); + CHECK(ink_auto_chunk(12000.0, 3, 2688.0) == 2048); // a card with room for both + CHECK(ink_chunk_need_mib(2048, 1000.0) == ink_chunk_need_mib(2048) + 1000.0); + CHECK(ink_chunk_need_mib(512, -5.0) == ink_chunk_need_mib(512)); // monotone: more free memory never gives a smaller chunk int last = 0; for (double f = 0.0; f <= 20000.0; f += 50.0) {