diff --git a/core/device/device.h b/core/device/device.h index 5634025a..65dac58c 100644 --- a/core/device/device.h +++ b/core/device/device.h @@ -45,6 +45,10 @@ extern "C" void rad_dev_host_wrote(void); * plugin measured a gain (RadArchProbe::chain_pays): with its drafter decoding speculatively, or * with and without one (rad_dev_chain_infer, from the engine before any row resolves). */ extern "C" int rad_dev_chain_enabled(void); +/* VRAM the runtime keeps resident as queue scratch on `props`' card once a kernel with a private + * segment has run (0 on the host backend and on unified memory). Charged to the VRAM budget with + * the hardware queues. */ +extern "C" int64_t rad_dev_resident_scratch_bytes(const RadDeviceProps* props); extern "C" void rad_dev_chain_infer(int pays); /* One range of a ranged copy: `bytes` from source offset `src` to destination offset `dst`, all diff --git a/core/device/dispatch.cpp b/core/device/dispatch.cpp index 3ec5bd48..97ca577f 100644 --- a/core/device/dispatch.cpp +++ b/core/device/dispatch.cpp @@ -934,6 +934,16 @@ int rad_event_sync(RadEvent e) { } void rad_dev_host_wrote(void) { dev()->host_wrote(); } +/* The VRAM the runtime keeps as queue scratch on `p`'s card once kernels with a private segment + * (spill, or a local array: step_advance's) have been dispatched: the runtime hands a queue + * scratch memory at the first such dispatch, and the card showed +114 MiB after the first + * speculative step and kept it, 150 MiB with a larger segment. The budget did not count it, so + * serving left 27-65 MiB free. 5 MiB a CU (the R9700 reports 32), 160 MiB, over the measured. */ +extern "C" int64_t rad_dev_resident_scratch_bytes(const RadDeviceProps* p) { + if (!p || p->is_host_backend || p->unified) return 0; + return (int64_t)p->n_cu * (5LL * 1024 * 1024); +} + /* Whether launches chain, by engine: rad_dev_chain_infer on a thread of an engine (EngineScope) * records that engine's answer and rad_dev_chain_enabled on any of its threads reads it. A process * serves several models, each with its own plugin's answer; a single answer for the process was diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 5c8d6d19..72609061 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -1753,6 +1753,7 @@ int Engine::plan() { for (RadStream s : q) if (s) rad_stream_destroy(s); } f.n_queues = 2 + (r->program.scratch_regions > 1 ? 1 : 0); + f.queue_scratch = 0; /* set below, once props are read */ /* The weights' cost, split into the fixed floor and the elastic plane * (weight_footprint in core/place/planner.h). Per rank, so already this card's share * under TP. */ @@ -1773,6 +1774,7 @@ int Engine::plan() { } RadDeviceProps props{}; rad_dev_props(r->device, &props); + f.queue_scratch = rad_dev_resident_scratch_bytes(&props); f.capacity = props.vram_bytes; f.free = props.vram_free; /* Unified memory: the pinned host pool is the same RAM as the card, so the diff --git a/core/mem/vram_budget.cpp b/core/mem/vram_budget.cpp index b0994eb2..dca4901b 100644 --- a/core/mem/vram_budget.cpp +++ b/core/mem/vram_budget.cpp @@ -51,8 +51,8 @@ static int resolve(Config& cfg, const std::vector& per_rank, VramBudg b.arena = std::max(b.arena, f.arena); b.staging = std::max(b.staging, f.staging); b.prefetch = std::max(b.prefetch, f.prefetch); - if ((int64_t)f.n_queues * f.queue_bytes > b.queues) { - b.queues = (int64_t)f.n_queues * f.queue_bytes; + if ((int64_t)f.n_queues * f.queue_bytes + f.queue_scratch > b.queues) { + b.queues = (int64_t)f.n_queues * f.queue_bytes + f.queue_scratch; b.n_queues = f.n_queues; b.queue_bytes = f.queue_bytes; } @@ -168,7 +168,7 @@ static int resolve(Config& cfg, const std::vector& per_rank, VramBudg " --gpu-headroom-mib %10s\n" " activation arena %10s (buffer plan + kernel scratch)\n" " step staging %10s (block tables, media and KV tier staging)\n" - " hardware queues %10s (%d at %s each, measured)\n" + " hardware queues %10s (%d at %s each, measured, + resident queue scratch)\n" " expert prefetch %10s (its ring of expert units and buffers)\n" " ------------------------------\n" " short by %10s\n" @@ -461,7 +461,7 @@ void vram_budget_need(const std::vector& per_rank, int64_t* need, int arena = std::max(arena, f.arena); staging = std::max(staging, f.staging); prefetch = std::max(prefetch, f.prefetch); - queues = std::max(queues, (int64_t)f.n_queues * f.queue_bytes); + queues = std::max(queues, (int64_t)f.n_queues * f.queue_bytes + f.queue_scratch); statics = std::max(statics, f.statics); floor = std::max(floor, f.expert_floor); kv_one = std::max(kv_one, f.kv_one); diff --git a/core/mem/vram_budget.h b/core/mem/vram_budget.h index d7f6418c..e1d11930 100644 --- a/core/mem/vram_budget.h +++ b/core/mem/vram_budget.h @@ -65,6 +65,7 @@ struct VramFacts { * per queue on an R9700, 8x the ring). */ int n_queues = 0; int64_t queue_bytes = 0; + int64_t queue_scratch = 0; /* resident queue scratch (rad_dev_resident_scratch_bytes), once a card */ /* The next-layer expert prefetch's ring and buffers (xprefetch.h), when enabled; its stream is * one of `n_queues`. */ int64_t prefetch = 0; diff --git a/docs/GUIDE.md b/docs/GUIDE.md index a10b9549..ab1d06da 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1068,7 +1068,8 @@ one for one with the key); a larger headroom that lost part of itself is reporte What the engine allocates after reading the card is charged before the split, not to the headroom: the step staging, the KV tiers' copy staging and the hardware queues. The driver backs each queue with VRAM beyond its ring (33 MiB on an R9700), so startup measures one and the receipt's `hardware -queues` line shows the count and price. +queues` line shows the count and price. It also includes the queue scratch the runtime keeps once a kernel with a private +segment has run (5 MiB a compute unit, 160 MiB on an R9700). ### 4.4 Long context diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 1053f90d..df594e55 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -654,6 +654,40 @@ TEST(the_engine_s_queues_come_off_the_card_before_the_split) { CHECK(unclaimed < 114 * MiB); } +/* The runtime keeps queue scratch resident once a kernel with a private segment has run + * (rad_dev_resident_scratch_bytes); it is part of the hardware queues' term, the largest rank's. */ +TEST(resident_queue_scratch_is_charged_with_the_hardware_queues) { + Config c = small_config(); + c.vram_weights_mib = 0; + c.vram_kv_mib = 0; + c.gpu_headroom_mib = 96; + VramFacts f{}; + f.capacity = 32LL << 30; + f.free = f.capacity - (512LL << 20); + f.arena = 144LL << 20; + f.staging = 98LL << 20; + f.statics = 2LL << 30; + f.experts = 200LL << 30; + f.kv_ceiling = 100LL << 30; + f.n_queues = 2; f.queue_bytes = 33LL << 20; + VramBudget b0, b1; + Config c0 = c, c1 = c; + CHECK_OK(vram_budget_resolve(c0, { f }, &b0)); + f.queue_scratch = 160LL << 20; + CHECK_OK(vram_budget_resolve(c1, { f }, &b1)); + CHECK_EQ(b0.queues, 66LL << 20); + CHECK_EQ(b1.queues, (66LL << 20) + (160LL << 20)); + CHECK_EQ(b1.claimable, b0.claimable - (160LL << 20)); + /* The minimum the fleet asks of the card moves by the same bytes. */ + int64_t need0 = 0, want0 = 0, need1 = 0, want1 = 0; + f.queue_scratch = 0; + vram_budget_need({ f }, &need0, &want0); + f.queue_scratch = 160LL << 20; + vram_budget_need({ f }, &need1, &want1); + CHECK_EQ(need1 - need0, 160LL << 20); + CHECK_EQ(want1 - want0, 160LL << 20); +} + TEST(the_smallest_card_binds_and_a_short_one_refuses_by_name) { Config c = small_config(); c.vram_weights_mib = 0;