auto_placement.h¶
src/inference/auto_placement.h
@file auto_placement.h @brief Derive a model's placement from its shape and the free VRAM.
Owners: @tvanfossen
derive_auto_placement
AutoPlacement entropic::derive_auto_placement(const GgufShape &shape, const FootprintInputs &base, uint64_t free_vram_bytes, int n_ubatch=0)
Choose the placement that uses the card best without overcommitting.
src/inference/auto_placement.h:331
Called by SCENARIO, SCENARIO, TEST_CASE, choose_partial_reserve, derive_tier_auto_placement
Calls compute_allowance_bytes, try_expert_offload, try_fewer_layers, try_fully_resident, try_smaller_ubatch
Source · 48 lines
inline AutoPlacement derive_auto_placement(const GgufShape& shape,
const FootprintInputs& base,
uint64_t free_vram_bytes,
int n_ubatch = 0) {
AutoPlacement out;
if (!shape.known || free_vram_bytes == 0) {
out.reason = "GGUF shape or free VRAM unknown";
return out;
}
// The headroom the operator asked for is already inside the estimate
// (vram_reserve_mb), so it is not subtracted twice — that double-count
// is what v2.13.0 shipped.
//
// What IS subtracted is a compute-buffer allowance, because the
// estimator explicitly does not count graph/activation scratch and says
// so: llama.cpp sizes it by ubatch and model internals, and
// vram_footprint.h measured 1222 MiB for one gemma-4 E4B MTP context at
// a 512-token ubatch — more than twice the 512 MB default reserve, and
// paid TWICE by a speculative configuration holding two contexts.
//
// Auto is choosing on the operator's behalf, so it must leave room for
// the thing it cannot price rather than hand back a placement that fits
// the estimate and fails the load. This is an allowance, not a
// derivation; it is deliberately generous, and the cost of being wrong
// is a layer left on the host rather than a failed load.
const uint64_t allowance = compute_allowance_bytes(base, n_ubatch);
const uint64_t budget =
free_vram_bytes > allowance ? free_vram_bytes - allowance : 0;
// Order is the measurement: ubatch costs ONE axis, experts and layers
// cost two. Spend the cheap currency first.
if (!try_fully_resident(shape, base, budget, &out)
&& !try_smaller_ubatch(shape, base, free_vram_bytes, n_ubatch, &out)
&& !try_expert_offload(shape, base, budget, &out)
&& !try_fewer_layers(shape, base, budget, &out)) {
out.reason = "nothing fits, even with no layers offloaded";
return out;
}
// The budget already has the compute allowance netted out, so this is
// the real physical remainder, not an accounting one. Recomputed rather
// than threaded back out of four call sites: `try_smaller_ubatch` moves
// the budget, so the rung it chose is the one to price against.
const uint64_t chosen = compute_allowance_bytes(
base, out.n_ubatch > 0 ? out.n_ubatch : n_ubatch);
const uint64_t effective =
free_vram_bytes > chosen ? free_vram_bytes - chosen : 0;
out.margin_bytes = effective > out.bytes ? effective - out.bytes : 0;
return out;
}Activity · 2 steps
Requirements
try_expert_offload
bool entropic::try_expert_offload(const GgufShape &shape, const FootprintInputs &base, uint64_t budget, AutoPlacement *out)
Try keeping every layer resident by moving experts host-side.
src/inference/auto_placement.h:221
Source · 20 lines
inline bool try_expert_offload(const GgufShape& shape,
const FootprintInputs& base,
uint64_t budget, AutoPlacement* out) {
if (shape.expert_count <= 0 || shape.expert_bytes == 0) { return false; }
uint64_t used = 0;
for (int moe = 1; moe <= shape.block_count; ++moe) {
if (!placement_fits(base, shape, shape.block_count, moe,
budget, &used)) {
continue;
}
out->known = true;
out->gpu_layers = -1; // all layers; experts are what moved.
out->fully_resident = true;
out->cpu_moe_layers = moe;
out->bytes = used;
out->reason = "all layers resident, experts host-side";
return true;
}
return false;
}Activity · 14 steps
Requirements
try_fewer_layers
bool entropic::try_fewer_layers(const GgufShape &shape, const FootprintInputs &base, uint64_t budget, AutoPlacement *out)
Last resort: take the largest layer count that fits.
src/inference/auto_placement.h:252
Source · 16 lines
inline bool try_fewer_layers(const GgufShape& shape,
const FootprintInputs& base,
uint64_t budget, AutoPlacement* out) {
uint64_t used = 0;
for (int layers = shape.block_count - 1; layers >= 0; --layers) {
if (!placement_fits(base, shape, layers, 0, budget, &used)) {
continue;
}
out->known = true;
out->gpu_layers = layers;
out->bytes = used;
out->reason = "partial offload";
return true;
}
return false;
}Activity · 10 steps
Requirements
try_smaller_ubatch
bool entropic::try_smaller_ubatch(const GgufShape &shape, const FootprintInputs &base, uint64_t free_vram_bytes, int configured, AutoPlacement *out)
Try full residency at each ubatch below the configured one.
src/inference/auto_placement.h:286
Source · 17 lines
inline bool try_smaller_ubatch(const GgufShape& shape,
const FootprintInputs& base,
uint64_t free_vram_bytes, int configured,
AutoPlacement* out) {
const int current = configured > 0 ? configured : kDefaultUbatch;
for (const int ub : kUbatchLadder) {
if (ub >= current) { continue; }
const uint64_t allowance = compute_allowance_bytes(base, ub);
const uint64_t budget =
free_vram_bytes > allowance ? free_vram_bytes - allowance : 0;
if (!try_fully_resident(shape, base, budget, out)) { continue; }
out->n_ubatch = ub;
out->reason = "fully resident at a reduced n_ubatch";
return true;
}
return false;
}