vram_footprint.h

src/inference/vram_footprint.h

@file vram_footprint.h @brief gh#142: a VRAM footprint estimate honest enough to refuse a load on.

Owners: @tvanfossen

classify_offload

Offload entropic::classify_offload(int gpu_layers)

Classify a tier's offload request into a priceable placement.

src/inference/vram_footprint.h:184

Source · 9 lines
inline Offload classify_offload(int gpu_layers) {
    Offload placement = Offload::partial_unknown;
    if (gpu_layers < 0 || gpu_layers >= kAllLayersSentinel) {
        placement = Offload::full;
    } else if (gpu_layers == 0) {
        placement = Offload::none;
    }
    return placement;
}
Activity · 7 steps
lines 185–185Offload placement = Offload::partial_unknown;lines 186–186◇ if (gpu_layers < 0 || gpu_layers >= kAllLayersSentinel)lines 187–187placement = Offload::full;lines 188–188elselines 188–188◇ if (gpu_layers == 0)lines 189–189placement = Offload::none;lines 191–191return placement;

Requirements

REQ-INFER-019

estimate_vram_footprint

FootprintEstimate entropic::estimate_vram_footprint(const FootprintInputs &in)

Estimate the VRAM a tier will occupy, or report that it cannot.

src/inference/vram_footprint.h:219

Show on the map →

Called by SCENARIO, choose_partial_reserve, estimate_footprint_bytes, placement_fits, recommend_context_length

Calls classify_offload, kv_bytes_per_token

Source · 50 lines
inline FootprintEstimate estimate_vram_footprint(const FootprintInputs& in) {
    FootprintEstimate est;
    const Offload placement = classify_offload(in.gpu_layers);
    // v2.13.1: a partial offload IS priceable once the GGUF's shape has been
    // read. Without it we still decline, because guessing here refuses
    // configurations that demonstrably work (gh#142) — but the shape is now
    // available from a metadata-only read, so declining is the fallback
    // rather than the rule.
    const bool have_shape = in.block_count > 0 && in.block_bytes > 0;
    if (placement == Offload::partial_unknown && !have_shape) {
        est.reason = "partial offload: layer count is not knowable without "
                     "GGUF metadata, so the budget gate stays open";
        return est;
    }
    uint64_t resident = 0;
    if (placement == Offload::full) {
        // The projector follows the weights: offload none and it lives in
        // host RAM.
        resident = in.weights_bytes + in.mmproj_bytes;
    } else if (placement == Offload::partial_unknown) {
        // Per-block cost from the block tensors ALONE. Dividing the whole
        // file by the layer count folds in embeddings and the output head,
        // which do not move with `gpu_layers`, and so overstates every
        // layer.
        const auto per_block =
            in.block_bytes / static_cast<uint64_t>(in.block_count);
        const auto offloaded = static_cast<uint64_t>(in.gpu_layers);
        resident = per_block * offloaded;
    }
    // Experts held host-side by `cpu_moe_layers` are not on the card.
    resident -= std::min(resident, in.host_expert_bytes);
    // The draft head is resident whenever speculative decode is configured,
    // at any placement.
    resident += in.draft_bytes;
    const int ctx = in.context_length > 0 ? in.context_length : 0;
    const int sessions = in.max_sessions > 0 ? in.max_sessions : 1;
    // gh#144 (v2.12.0): context_length is per session; the pool allocates
    // ctx * sessions cells. Deliberately an over-count for an iSWA model
    // like Gemma-4: kBaseKvPerTokenF16 is a flat rate and does not model
    // sliding-window layers (5:1 SWA at window 512 under swa_full=false),
    // so the real figure is well below this. That is the safe direction
    // per this header's own rule — never silently under-count.
    const uint64_t kv = static_cast<uint64_t>(
        static_cast<double>(ctx) * static_cast<double>(sessions)
        * kv_bytes_per_token(in));
    est.known = true;
    est.bytes = resident + kv
        + static_cast<uint64_t>(in.vram_reserve_mb) * 1024ull * 1024ull;
    return est;
}
Activity · 23 steps
lines 220–220FootprintEstimate est;lines 221–221const Offload placement = classify_offload(in.gpu_layers);lines 227–227const bool have_shape = in.block_count > 0 && in.block_bytes > 0;lines 228–228◇ if (placement == Offload::partial_unknown && !have_shape)lines 229–230est.reason = "partial offload: layer count is not knowable without " "GGUF metadata, so the budget gate stays open";lines 231–231return est;lines 233–233uint64_t resident = 0;lines 234–234◇ if (placement == Offload::full)lines 237–237resident = in.weights_bytes + in.mmproj_bytes;lines 238–238elselines 238–238◇ if (placement == Offload::partial_unknown)lines 243–244const auto per_block = in.block_bytes / static_cast<uint64_t>(in.block_count);lines 245–245const auto offloaded = static_cast<uint64_t>(in.gpu_layers);lines 246–246resident = per_block * offloaded;lines 249–249resident -= std::lines 249–249min(resident, in.host_expert_bytes);lines 252–252resident += in.draft_bytes;lines 253–253const int ctx = in.context_length > 0 ? in.context_length : 0;lines 254–254const int sessions = in.max_sessions > 0 ? in.max_sessions : 1;lines 261–263const uint64_t kv = static_cast<uint64_t>( static_cast<double>(ctx) * static_cast<double>(sessions) * kv_bytes_per_token(in));lines 264–264est.known = true;lines 265–266est.bytes = resident + kv + static_cast<uint64_t>(in.vram_reserve_mb) * 1024ull * 1024ull;lines 267–267return est;

Requirements

REQ-INFER-019

kv_bytes_per_token

double entropic::kv_bytes_per_token(const FootprintInputs &in)

Per-token KV cost in bytes for this tier's cache configuration.

src/inference/vram_footprint.h:205

Source · 5 lines
inline double kv_bytes_per_token(const FootprintInputs& in) {
    const double avg = (kv_scale_for_cache_type(in.cache_type_k)
                        + kv_scale_for_cache_type(in.cache_type_v)) / 2.0;
    return static_cast<double>(kBaseKvPerTokenF16) * avg;
}
Activity · 2 steps
lines 206–207const double avg = (kv_scale_for_cache_type(in.cache_type_k) + kv_scale_for_cache_type(in.cache_type_v)) / 2.0;lines 208–208return static_cast<double>(kBaseKvPerTokenF16) * avg;

Requirements

REQ-INFER-019

kv_scale_for_cache_type

double entropic::kv_scale_for_cache_type(const std::string &type)

Cost of a KV cache type relative to f16.

src/inference/vram_footprint.h:151

Show on the map →

Called by SCENARIO, kv_bytes_per_token

Calls none

Source · 20 lines
inline double kv_scale_for_cache_type(const std::string& type) {
    static const std::pair<const char*, double> kScales[] = {
        {"f32",  2.0},      // 4 B/elem
        {"f16",  1.0},      // 2 B/elem  — the baseline
        {"bf16", 1.0},      // 2 B/elem
        {"q8_0", 0.53125},  // 34 B / 32 elem = 1.0625 B/elem
        {"q5_1", 0.375},    // 24 B / 32 elem = 0.75   B/elem
        {"q5_0", 0.34375},  // 22 B / 32 elem = 0.6875 B/elem
        {"q4_1", 0.3125},   // 20 B / 32 elem = 0.625  B/elem
        {"q4_0", 0.28125},  // 18 B / 32 elem = 0.5625 B/elem
    };
    double scale = 1.0;
    for (const auto& entry : kScales) {
        if (type == entry.first) {
            scale = entry.second;
            break;
        }
    }
    return scale;
}
Activity · 3 steps
lines 152–161static const std::pair<const char*, double> kScales[] = { {"f32", 2.0}, // 4 B/elem {"f16", 1.0}, // 2 B/elem — the baseline {"bf16", 1.0}, // 2 B/elem {"q8_0", 0.53125}, // 34 B / 32 elem = 1.0625 B/elem {"q5_1", 0.375}, // 24 B / 32 elem = 0.75 B/elem {"q5_0", 0.34375}, // 22 B / 32 elem = 0.6875 B/elem {"q4_1", 0.3125}, // 20 B / 32 elem = 0.625 B/elem {"q4_0", 0.28125}, // 18 B / 32 elem = 0.5625 B/elem };lines 162–162double scale = 1.0;lines 163–169for (const auto& entry : kScales) { if (type == entry.first) { scale = entry.second; break; } } return scale;

Requirements

REQ-INFER-019

recommend_context_length

int entropic::recommend_context_length(const FootprintInputs &in, uint64_t available_bytes)

Largest context length that fits, for the "won't fit" recommendation.

src/inference/vram_footprint.h:284

Source · 24 lines
inline int recommend_context_length(const FootprintInputs& in,
                                    uint64_t available_bytes) {
    FootprintInputs probe = in;
    probe.context_length = 0;
    const FootprintEstimate fixed = estimate_vram_footprint(probe);
    if (!fixed.known || fixed.bytes >= available_bytes) {
        return 0;
    }
    // gh#144 (v2.12.0): divide the budget across the pool, or the
    // recommendation is a PER-SESSION window N times too large — handing an
    // operator a config that aborts, from the very function whose job is to
    // offer one that works.
    const int sessions = in.max_sessions > 0 ? in.max_sessions : 1;
    const double per_token =
        kv_bytes_per_token(in) * static_cast<double>(sessions);
    const uint64_t kv_budget = available_bytes - fixed.bytes;
    const uint64_t fits = static_cast<uint64_t>(
        static_cast<double>(kv_budget) / per_token);
    const uint64_t requested = in.context_length > 0
        ? static_cast<uint64_t>(in.context_length) : 0ull;
    uint64_t chosen = fits < requested ? fits : requested;
    chosen -= chosen % 512ull;
    return static_cast<int>(chosen);
}
Activity · 13 steps
lines 286–286FootprintInputs probe = in;lines 287–287probe.context_length = 0;lines 288–288const FootprintEstimate fixed = estimate_vram_footprint(probe);lines 289–289◇ if (!fixed.known || fixed.bytes >= available_bytes)lines 290–290return 0;lines 296–296const int sessions = in.max_sessions > 0 ? in.max_sessions : 1;lines 297–298const double per_token = kv_bytes_per_token(in) * static_cast<double>(sessions);lines 299–299const uint64_t kv_budget = available_bytes - fixed.bytes;lines 300–301const uint64_t fits = static_cast<uint64_t>( static_cast<double>(kv_budget) / per_token);lines 302–303const uint64_t requested = in.context_length > 0 ? static_cast<uint64_t>(in.context_length) : 0ull;lines 304–304uint64_t chosen = fits < requested ? fits : requested;lines 305–305chosen -= chosen % 512ull;lines 306–306return static_cast<int>(chosen);

Requirements

REQ-INFER-019