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

Show on the map →

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
lines 365–365out.reason = "nothing fits, even with no layers offloaded";lines 366–366return out;

Requirements

REQ-INFER-019

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

Show on the map →

Called by derive_auto_placement

Calls placement_fits

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
lines 224–224◇ if (shape.expert_count <= 0 || shape.expert_bytes == 0)lines 224–224return false;lines 225–225uint64_t used = 0;lines 226–226◇ for (int moe = 1; moe <= shape.block_count; ++moe)lines 227–228◇ if (!placement_fits(base, shape, shape.block_count, moe, budget, &used))lines 229–229continue;lines 231–231out->known = true;lines 232–232out->gpu_layers = -1;lines 233–233out->fully_resident = true;lines 234–234out->cpu_moe_layers = moe;lines 235–235out->bytes = used;lines 236–236out->reason = "all layers resident, experts host-side";lines 237–237return true;lines 239–239return false;

Requirements

REQ-INFER-019

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

Show on the map →

Called by derive_auto_placement

Calls placement_fits

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
lines 255–255uint64_t used = 0;lines 256–256◇ for (int layers = shape.block_count - 1; layers >= 0; --layers)lines 257–257◇ if (!placement_fits(base, shape, layers, 0, budget, &used))lines 258–258continue;lines 260–260out->known = true;lines 261–261out->gpu_layers = layers;lines 262–262out->bytes = used;lines 263–263out->reason = "partial offload";lines 264–264return true;lines 266–266return false;

Requirements

REQ-INFER-019

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

Show on the map →

Called by derive_auto_placement

Calls compute_allowance_bytes, try_fully_resident

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;
}
Activity · 10 steps
lines 290–290const int current = configured > 0 ? configured : kDefaultUbatch;lines 291–292for (const int ub : kUbatchLadder) { if (ub >= current)lines 292–292continue;lines 293–293const uint64_t allowance = compute_allowance_bytes(base, ub);lines 294–295const uint64_t budget = free_vram_bytes > allowance ? free_vram_bytes - allowance : 0;lines 296–296◇ if (!try_fully_resident(shape, base, budget, out))lines 296–296continue;lines 297–297out->n_ubatch = ub;lines 298–298out->reason = "fully resident at a reduced n_ubatch";lines 299–299return true;

Requirements

REQ-INFER-019