mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 07:18:50 +00:00
fix(inkling cuda): the auto prefill chunk leaves room for the K/V that still has to grow (regression fix for faceebb50)
faceebb50chose the chunk from the memory left after the experts only. On a 24 GB card with UD-Q4_K_XL at 32K the 1024 chunk took the room the global K/V needs to grow, so the K/V tail went to host RAM and decode at 30K tokens fell with 2 slots from 80.9 to 36.0 tok/s (3 slots 14.7). The chunk now also has to leave slots x (context - the window allocated at start) x bytes per position free (none when K/V offload is on). RTX 4090, UD-Q4_K_XL, 32K, decode tok/s at 30K tokens, 1 / 2 / 3 slots:faceebb5087.7 / 36.0 / 14.7 this change 86.6 / 79.8 / 24.0 STRATA_INK_CHUNK_AUTO=0 (chunk 512, the old default) 87.7 / 80.9 / 25.4 3 slots spill by the engine's own auto host-offload decision, not by the chunk (the same with the old default). ink_chunk_test covers the three cases.
This commit is contained in:
1 parent
faceebb50c
commit
751d55647f
3 files changed
+23
-8
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 (;;) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user