vram: charge the runtime's resident queue scratch to the card budget

Once a kernel with a private segment has been dispatched (rad_step_advance has
544 bytes a lane), the HSA runtime hands its queue scratch memory and keeps it:
about +114 MiB on a card after the first speculative step, +150 MiB with a
larger segment, with or without HSA_NO_SCRATCH_RECLAIM. The VRAM budget does
not count it, so a serving process was left with only 27-65 MiB free after its
first speculative steps -- a thin margin for any kernel that later takes a
bigger private segment (the runtime then faults the queue with OUT_OF_RESOURCES
instead of failing the load).

The backend now reports rad_dev_resident_scratch_bytes(): 5 MiB per CU (160 MiB
on an R9700, which reports 32), over the measured 114-150 MiB, and 0 on the
host backend and on unified memory. The engine puts it into VramFacts::
queue_scratch, and the budget charges it with the hardware queues (the
resolver, vram_budget_need, and the report line).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-17 18:18:56 +01:00
1 parent b2228c176b
commit 7bd195d394
7 files changed
+57 -5

No files matched your search

+4
View File
@@ -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
+10
View File
@@ -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
+2
View File
@@ -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
+4 -4
View File
@@ -51,8 +51,8 @@ static int resolve(Config& cfg, const std::vector<VramFacts>& 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<VramFacts>& 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<VramFacts>& 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);
+1
View File
@@ -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;
+2 -1
View File
@@ -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
+34
View File
@@ -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;