This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/mem/vram_budget.h
T
Deadcode ddcb9c69bb radiance 2.0.0
Radiance becomes a multi-model engine: one process serves several models behind one port, loads
each when a request names it, parks it in host memory or restores it from its container, and
shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the
command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP).

Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an
int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block
FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such
weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5,
Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B,
the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0
serves all of them.

Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new
Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill
reworked, and a reworked adaptive draft window.

Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its
configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool
in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server,
each accelerator's memory by model, model states over time.

Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by
AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests
keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410);
eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d
large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi
(#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon
(#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and
multi-model serving as asked for in #367 and #109.

Plugin ABI 39: out-of-tree plugins need to be rebuilt.

Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
2026-11-14 22:32:28 +01:00

163 lines
11 KiB
C++

/* vram_budget.h -- how many bytes each VRAM pool gets, decided once and printed in full.
*
* Spec §6 forbids deriving budgets by inference, such as from the free VRAM at startup, which
* moves whenever another process touches the card. This is arithmetic on known, printed, stable
* terms instead:
*
* capacity totalGlobalMem, fixed for the part.
* already held capacity - free VRAM, measured once after every kernel plugin has
* initialised and its code objects are resident: the driver context and
* loaded code. The only observed term, printed on its own line so a
* desktop on the card shows up there instead of shrinking a pool.
* --gpu-headroom-mib what the operator leaves alone (default 96 MiB).
* --gpu-memory-mib the inverse: claim this much of the card; headroom is the rest.
* Not with --gpu-headroom-mib.
* arena activation arena plus scratch, from arena_plan_bytes() on the buffer
* plan, the same call that later allocates it.
*
* What is left is claimable, spent in two steps:
*
* 1. Static weights come off the top. Every non-expert weight (attention projections, norms,
* embedding, head) must be resident; the one exception is a token embedding that
* --embedding-placement puts in the pinned host pool, which weight_footprint then excludes.
* Under tensor parallelism each rank's share is already divided (declare runs per rank,
* spec §9). They are a floor, not a share, so the cache can never push attention weights to
* host memory. If they do not fit, startup refuses with the shortfall named.
*
* 2. The rest is divided between the expert plane and the KV cache. When the cache lends idle
* blocks to the expert slab, the experts get what the pinned host pool cannot hold and the
* cache gets the rest up to its ceiling, lending back every block no live request holds.
* Without a lender an unused block would sit empty, so experts come first and the cache keeps
* one --max-model-len sequence. The cache never gets less than that sequence.
*
* Both sides are capped at what they can use (the cache at max_seqs x max_ctx tokens,
* KVManager::ceiling; the experts at the whole plane) and one side's excess goes to the other.
*
* No flag states a pool size: unbudgeted VRAM is unusable, and on a model whose experts do not
* fit every unbudgeted GiB costs decode time. Operators move the inputs instead: --gpu-headroom-mib
* or --gpu-memory-mib, --max-model-len and --max-num-seqs (cache bound), --host-pool-mib (which
* experts must stay on the card).
*
* The host backend uses half the machine's memory as its capacity (host_vram_bytes) and splits it
* like a card; its weights pool holds what a host kernel cannot read where the container stores it
* (mapped_unit in core/place/planner.h). A device reporting no capacity gets what each pool needs:
* the weights the planner copies, and the cache at its ceiling.
*/
#pragma once
#include "../rad_core.h"
#include <string>
#include <vector>
namespace rad {
/* One rank's card and program. Every field is read from the device or computed from the declared
* program; none is policy. */
struct VramFacts {
int64_t capacity = 0; /* RadDeviceProps::vram_bytes -- totalGlobalMem */
int64_t free = 0; /* RadDeviceProps::vram_free, measured now */
int64_t arena = 0; /* arena_plan_bytes() for this rank's program, total incl. scratch */
int64_t staging = 0; /* BatchBuilder::device_bytes(): the step slab and the media staging,
* and TierExec::device_bytes() when a KV tier is configured */
/* Hardware queues this rank creates after `free` was read (compute stream, expert mover, and
* the second compute lane if declared), at `queue_bytes` each. Measured by creating one: the
* driver backs every queue with its own VRAM beside the engine's kernel-argument ring (33 MiB
* per queue on an R9700, 8x the ring). */
int n_queues = 0;
int64_t queue_bytes = 0;
/* The next-layer expert prefetch's ring and buffers (xprefetch.h), when enabled; its stream is
* one of `n_queues`. */
int64_t prefetch = 0;
int64_t kv_ceiling = 0; /* KVManager::ceiling(max_ctx) for this rank's declared groups */
int64_t kv_fixed = 0; /* KVManager::fixed_bytes(): the part of the ceiling that is state */
/* weight_footprint() for this rank; under tensor parallelism already this card's share. */
int64_t statics = 0; /* non-expert weights -- the floor, not a share of anything */
int64_t experts = 0; /* the expert plane: what trades against the KV cache */
int64_t expert_layer_max = 0; /* the most expert bytes one routed layer holds */
/* Experts the pinned host pool cannot hold, plus the slab's spare slots: the expert slab's
* floor when the cache lends (expert_vram_floor, core/place/planner.h). */
int64_t expert_floor = 0;
int64_t kv_one = 0; /* all configured fixed state plus pages for one full-length context */
/* The cache lends idle blocks to the expert slab: a placement that moves units (expert_tiered
* or auto), not --deterministic, on a device with virtual memory management. A lending cache
* can be sized for what live requests could need; a non-lending one would hold it empty. */
bool lends = false;
/* A loaded control vector's direction table: device bytes the engine already holds, which are
* not part of the program or the pools and must not shrink what the plan may claim. It comes
* out of the headroom, as every deployment keeps that unclaimed, so a deployment that only
* offers a vector plans its expert cache against the same card as one that does not. */
int64_t held_out = 0;
/* Integrated graphics: host memory the card must leave to the host, since device memory is
* host memory there. 0 on a discrete card. */
int64_t host_reserve = 0;
/* Unified memory (RadDeviceProps::unified): the pinned host pools are charged against
* the same ledger as the device pools, because both are the same RAM. `unified` says
* the device is one; `host_pinned` is this rank's pinned host pool bytes. Both are 0 /
* false on a discrete card, where the host pool is charged to host RAM the budget does
* not govern. */
bool unified = false;
int64_t host_pinned = 0;
};
/* The resolved split in bytes, with every term it came from. Always filled, including for a
* device that reports no capacity, because the placement and pool reports want the totals. */
struct VramBudget {
int64_t capacity = 0, already_held = 0, headroom = 0, arena = 0, staging = 0;
int64_t drift = 0; /* kDriverDrift: what already_held moves by after it is read */
int64_t queues = 0; /* n_queues * queue_bytes, from the rank that needs most */
int64_t prefetch = 0; /* the expert prefetch's ring and buffers */
int n_queues = 0;
int64_t queue_bytes = 0;
int64_t claimable = 0; /* after the fixed terms above */
int64_t statics = 0; /* off the top, before the split sees anything */
int64_t elastic = 0; /* claimable - statics: what experts and cache share */
int64_t expert_floor = 0; /* the experts the host pool cannot hold */
bool lends = false; /* every rank's cache lends its idle blocks */
int64_t kv_ceiling = 0, expert_ceiling = 0;
int64_t kv_fixed = 0; /* the state part of kv_ceiling, which no context uses */
int64_t experts = 0, kv = 0; /* the two shares, each capped at its ceiling */
int64_t unusable = 0; /* elastic neither side could take -- both were full */
/* Prefill staging region: two buffers, each one layer's share of the experts left off the
* card, funded by cache slack or expert residency above the host-pool floor, appended to the
* top of the activation arena (core/place/stager.h). Zero when every expert is resident or the
* placement cannot move any. */
int64_t stage = 0;
int64_t weights = 0; /* statics + experts: what vram_weights is set to */
bool unified = false; /* a unified-memory device: one RAM ledger */
int64_t host_pinned = 0; /* pinned host pools charged to that ledger */
std::string report; /* the block the engine prints at startup */
};
/* Writes cfg.vram_weights_mib / cfg.vram_kv_mib, the one number every downstream consumer reads
* (an uneven world's cheaper rank re-splits it with vram_budget_rank_split). Takes the smallest
* capacity and free VRAM across ranks and the largest arena, so the split fits every card.
* Refuses, by name and size, when the fixed terms alone exceed the card. */
int vram_budget_resolve(Config& cfg, const std::vector<VramFacts>& per_rank, VramBudget* out);
/* The same split on a copy of `cfg`, without writing or logging anything: for choosing between
* two programs before resolving one (Engine::plan's table placement). */
int vram_budget_trial(const Config& cfg, const std::vector<VramFacts>& per_rank, VramBudget* out);
/* Bytes a card must give this program, as vram_budget_resolve charges them: `need` holds the fixed
* terms, the static weights, the experts the host pool cannot hold and one full-length sequence's
* cache; `want` the whole expert plane and the cache at its ceiling instead. A fleet grants a
* claim between the two (Config::vram_claim, docs/MULTIMODEL.md §6.3). */
void vram_budget_need(const std::vector<VramFacts>& per_rank, int64_t* need, int64_t* want);
/* Re-split for an uneven world's cheaper rank. The resolver derives the most expensive rank's
* split, and one block table serves every rank, so a rank whose cache costs less per token (e.g.
* one with no attention cache) would over-reserve KV. Given the common split, the MiB this rank
* needs to page the same tokens, and the bytes of all its weights resident (statics + all experts
* + spare slots), moves the difference to the weights pool up to that cap; the remainder stays
* unclaimed. No change when the need is not below the common pool. */
struct RankSplit {
int64_t weights_mib = 0;
int64_t kv_mib = 0;
int64_t unusable_mib = 0; /* saved from the KV pool but past the weights cap: never allocated */
bool resplit = false;
};
RankSplit vram_budget_rank_split(int64_t common_weights_mib, int64_t common_kv_mib,
int64_t need_kv_mib, int64_t weights_cap_bytes);
} /* namespace rad */